6
6

More than 1 year has passed since last update.

Gist on Emacs

Last updated at Posted at 2021-09-01

みなさん、Emacsで Gist 使ってますか?

私も気軽に使っているのですが GitHub Gist の Webページを開いて直接コード書き込んだりコピペするというのは何げに中途半端ですね。

ローカルに cloneして git管理するという Tipsも見かけましたが、そもそもバージョン管理がしたいのであればGistにこだわらず、素直にリポジトリで管理すれば済むことです。

何がしたいのか整理してみる

  1. Emacsで開いている Buffer(or Region)からそのまま即 POSTしたい。
  2. Diredからも POSTしたい。
  3. POSTに成功したら、結果をブラウザで開いてほしい。
  4. 自分の GitHub Gist ページを一発で開きたい。

gist.elを使えば簡単なのですが、全ての操作をEmacsでやりたいのではなく、基本的には上記のPOST機能だけで十分なので簡単な関数を作ってみました。稚拙な emacs-lispなのでもう少しスマートなやり方があれば教えてください。

ターミナルで gist コマンドが扱えること

このTipsは、ターミナルからコマンドラインで gist が扱えるように設定済であることが前提です。

ターミナルで gist -help を打つといろいろオプションパラメターがわかります。

Usage: gist [-o|-c|-e] [-p] [-s] [-R] [-d DESC] [-u URL]
                          [--skip-empty] [-P] [-f NAME|-t EXT]* FILE*
       gist --login
       gist [-l|-r]

        --login                      Authenticate gist on this computer.
    -f, --filename [NAME.EXTENSION]  Sets the filename and syntax type.
    -t, --type [EXTENSION]           Sets the file extension and syntax type.
    -p, --private                    Makes your gist private.
        --no-private
    -d, --description DESCRIPTION    Adds a description to your gist.
    -s, --shorten                    Shorten the gist URL using git.io.
    -u, --update [ URL | ID ]        Update an existing gist.
    -c, --copy                       Copy the resulting URL to the clipboard
    -e, --embed                      Copy the embed code for the gist to the clipboard
    -o, --open                       Open the resulting URL in a browser
        --no-open
        --skip-empty                 Skip gisting empty files
    -P, --paste                      Paste from the clipboard to gist
    -R, --raw                        Display raw URL of the new gist
    -l, --list [USER]                List all gists for user
    -r, --read ID [FILENAME]         Read a gist and print out the contents
        --delete [ URL | ID ]        Delete a gist
    -h, --help                       Show this message.
    -v, --version                    Print the version.

Emacsの設定

Emacsに設定するのは下記の4つのコマンドだけです。

1. gist-region-or-buffer

名前の通り Emacsで開いている Buffer(or region)を postしてくれます。
Descriptionはミニバッファーから入力し、bufferファイル名を自動取得してファイ名にします。
regionからPOSTするときは、Descriotionとファイル名のどちらもミニバッファーから入力しています。

2. dired-do-gist

Emacsで開いているDiredからカーソル位置のファイルを gist postするための設定です。

3. chromium-gist

プラウザで自分のGistページを開くための設定です。<gist-user> にユーザー名をいれます。

4. open-lepton

クライアントアプリ Leptonを開くための設定です。私の場合、編集や削除などはLeptonでしています。


init.elへの設定

(leaf my:gist-configuration
  :config
  (bind-key "@" 'dired-do-gist dired-mode-map)
  (bind-key "C-c g" 'gist-region-or-buffer)
  (bind-key "C-c v" 'chromium-gist)
  (bind-key "C-c l" 'lepton)
  :init
  (defun gist-description ()
    (shell-quote-argument (read-from-minibuffer "Gist description: ")))
   (defun gist-filename ()
    "The character string entered in minibuffer is used as file-name.
If enter is pressed without file-name, file-name will be buffer-file-neme."
    (let ((file (file-name-nondirectory (buffer-file-name (current-buffer)))))
      (read-from-minibuffer (format "File name (%s): " file) file)))

  (defun gist-region-or-buffer ()
    "If region is selected, input file-name in minibuffer then post from region.
If region isn't selected, use buffer-file-name to post from buffer."
    (interactive)
    (let ((file (buffer-file-name)))
      (if (not (use-region-p))
          (compile (concat "gist -od " (gist-description) " " file))
        (compile (concat "gist -oPd " (gist-description) " -f " (gist-filename)))))
    (delete-other-windows))

  (defun dired-do-gist ()
    "Dired-get-filename do gist and open in browser."
    (interactive)
    (let ((file (dired-get-filename nil t)))
      (compile (concat "gist -od " (gist-description) " " file)))
    (delete-other-windows))

  (defun chromium-gist ()
    "Open GitHub Gist page with browse-url."
    (interactive)
    (browse-url "https://gist.github.com/<gist-user>"))

  (defun lepton ()
    "Open Lepton application."
    (interactive)
    (compile "~/Appimage/Lepton-1.10.0.AppImage")
    (delete-other-windows)))

クライアントアプリの紹介

もう少しビジュアルにGistを管理したいという方には、マルチプラートホームで利用可能な Lepton というアプリがお薦めです。WEBページよりは見やすいですし、編集、削除も可能です。New fileをコピペして投稿することもできます。ただ新規のPOSTは、上述した gist-buffer-or-region の方が遥かに便利です。

私はAppImage版をダウンロードして使っています

## makefile for install Lepton.AppImage
lepton: ## Init lepton
    mkdir -p ${HOME}/Appimage
    cd ${HOME}/Appimage && \
    wget https://github.com/hackjutsu/Lepton/releases/download/v1.10.0/Lepton-1.10.0.AppImage
    chmod a+x Lepton-1.10.0.AppImage
    ln -vsfn {${PWD},${HOME}}/.local/share/applications/lepton.desktop
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6