3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

c-quick: Lisp系のソースコードを効率的に編集する (カーソル移動以外の機能)

Last updated at Posted at 2022-12-22

2024/02/23 更新

最新版の c-quick.el は以下のGitHubリポジトリからダウンロードできます。

https://github.com/emacs-pkg/c-quick/releases/latest

インストール方法 (c-quick.el)

とりあえず使ってみたい(または最新の変更に追随したい)方は、.emacs または init.el に以下を追加しておくと自動的にロードされます。

~/.emacs or ~/.emacs.d/init.el
(unless (featurep 'get-feature)
  (defun get-feature (feature-name &optional url file-name)
    (if (featurep feature-name) t
      (unless url (setq url (format "https://github.com/emacs-pkg/%s/raw/main/%s.el"
                                    feature-name feature-name)))
      (unless file-name (setq file-name (format "%s.el" feature-name)))
      (let ((make-backup-files nil)
            (file-path (expand-file-name file-name user-emacs-directory)))
        (ignore-errors
          (url-copy-file url file-path 'ok-if-already-exists))
        (ignore-errors
          (load file-path nil 'nomessage))
        (featurep feature-name))))
  (get-feature 'get-feature))

(get-feature 'c-quick)

straight.el でロードしたい場合は以下を .emacs または init.el に入れてください(但し git のインストールが必要です):

~/.emacs or ~/.emacs.d/init.el
(defvar bootstrap-version)
(let ((bootstrap-file
       (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
      (bootstrap-version 6))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
        (url-retrieve-synchronously
         "https://raw.githubusercontent.com/radian-software/straight.el/develop/install.el"
         'silent 'inhibit-cookies)
      (goto-char (point-max))
      (eval-print-last-sexp)))
  (load bootstrap-file nil 'nomessage))

(straight-use-package '(c-quick :type git :host github :repo "emacs-pkg/c-quick"))
(require 'c-quick)

尚、straight.el ではパッケージを最新化するのに手動で操作をする必要があります。以下の記事をご覧ください:


以前以下の記事を書きましたが、そこで書ききれなかった機能やキーバインディングをご紹介します。


1. キーバインディング(C-z)

Ctrl-Z は undo にバインディングされます。

2. ローテイトバッファー(バッファーの回転)

Ctrl-TAB または Ctrl-RETURN (Ctrl-ENTER) でカレントウィンドウのバッファーが入れ替わります。C-x b だとバッファ名(またはファイル名)を入力しなければならないので「バッファーの回転」を使った方が楽な場合が多いです。
また、他のウィンドウに表示されているバッファーは回転で出てこないようになっています。

3. キーバインディング(C-F4)

Ctrl-F4 はカレントウィンドウのバッファを閉じますが、その再にカレントウィンドウも閉じます(つまり、ウィンドウが減ります)。
ウィンドウを分割しすぎて、要らないバッファと要らないウィンドウが増えた時に便利です。

4. キーバインディング(S-F4)

Shift-F4 は、カレントウィンドウ以外のウィンドウ内のバッファとウィンドウを全て閉じます。
これも、ウィンドウを分割しすぎて、要らないバッファと要らないウィンドウが増えた時に便利です。

5. キーバインディング(C-M-\)

Ctrl-Alt-\ または Ctrl-Alt-¥ で、S式を再インデントできます。
以下の例はS式インデントをなくしています。
㊀(S式の先頭)にカーソルがある際に、Ctrl-Alt-\ または Ctrl-Alt-¥ を入力すると:

㊀(defun c-quick-forward-sexp ()
    (interactive)
    (cond
    ((eobp) (c-quick-ding))
    ((c-quick-within-string (point)) (c-quick-forward-within-string))
    ((looking-at "\\s)") (c-quick-ding))
    ((looking-at "\\s.")
    (forward-char))
    ((looking-at "\\s-*\\s<")
    (let ((opoint (point)))
    (forward-line)
    (while (looking-at "\\s-*\\s<")
    (setq opoint (point))
    (forward-line))
    (goto-char (max opoint (save-excursion (beginning-of-line) (point))))))
    ((looking-at "\\s-") (while (looking-at "\\s-") (forward-char)))
    ((looking-at "\n")
    (let ((bol? (bolp)))
    (forward-char)
    (when bol?
    (while (and (bolp) (looking-at "\n"))
    (forward-char)))))
    (t (ignore-errors (forward-sexp)))))

以下のようにS式全体が一気にインデントされます。

(defun c-quick-forward-sexp ()
  (interactive)
  (cond
   ((eobp) (c-quick-ding))
   ((c-quick-within-string (point)) (c-quick-forward-within-string))
   ((looking-at "\\s)") (c-quick-ding))
   ((looking-at "\\s.")
    (forward-char))
   ((looking-at "\\s-*\\s<")
    (let ((opoint (point)))
      (forward-line)
      (while (looking-at "\\s-*\\s<")
        (setq opoint (point))
        (forward-line))
      (goto-char (max opoint (save-excursion (beginning-of-line) (point))))))
   ((looking-at "\\s-") (while (looking-at "\\s-") (forward-char)))
   ((looking-at "\n")
    (let ((bol? (bolp)))
      (forward-char)
      (when bol?
        (while (and (bolp) (looking-at "\n"))
          (forward-char)))))
   (t (ignore-errors (forward-sexp)))))

また、㊁にカーソルがある場合に、S式を再インデントするために㊀に移動するには、前回の記事(1.2 トップレベルのS式の位置づけ)で紹介した Ctrl-up (Ctrl+上矢印) が有効です。Ctrl-上矢印を押すことで一瞬で㊀にカーソルが移動します。

㊀(defun c-quick-forward-sexp ()
    (interactive)
    (cond
    ((eobp) (c-quick-ding))
    ((c-quick-within-string (point)) (c-quick-forward-within-string))
    ((looking-at "\\s)") (c-quick-ding))
    ((looking-at "\\s.")
    (forward-char))
    ((looking-at "\\s-*\\s<")
    (let ((opoint (point)))
    (forward-line)
    (while (looking-at "\\s-*\\s<")
    (setq opoint (point))
    (forward-line))㊁
    (goto-char (max opoint (save-excursion (beginning-of-line) (point))))))
    ((looking-at "\\s-") (while (looking-at "\\s-") (forward-char)))
    ((looking-at "\n")
    (let ((bol? (bolp)))
    (forward-char)
    (when bol?
    (while (and (bolp) (looking-at "\n"))
    (forward-char)))))
    (t (ignore-errors (forward-sexp)))))

6. キーバインディング(M-F4)

Alt-F4 で Emacs を終了します。

7. キーバインディング(F5 or C-F5 or S-F5 or M-F5 or C-x F5)【v2.3.0以降】

ファイルを開いているバッファ上のカーソルがあるポイントにブックマーク(しおり)を設定します。
ブックマークを設定しておけばファイルを閉じてしまっても次の項目で説明するブックマークリストかブックマークのファイル・ポイントに瞬時に移動できます。
Emacs の生のブックマーク機能では名前を付ける手間があるのですが、F5キーでは自動的に名前が付けられます。(F6キーの一覧からリネームすることもできます)

8. キーバインディング(F6 or C-F6 or S-F6 or M-F6 or C-x F6)【v2.3.0以降】

*Bookmark List* というバッファが開きます。F5 で登録したブックマークが一覧で表示されます。
上下に移動して、ENTER を押すとブックマークが開きます。
ブックマークを消したい場合には d を押すと「D」というマークがつきます。(マークを解除したい場合は u を押します)
d でマークを付けた後(複数可)、x を押すと実際にブックマークが消えます。
なお、F5 を使った場合は、ブックマークの名前が自動的に付けられますが、r キーを押してミニバッファで文字列を指定してリネームすることもできます。
ブックマークを消してもファイル自体は消えません。

9. キーバインディング(F7 or C-F7 or S-F7 or M-F7 or C-x F7)【v2.3.0以降】

ファイルに関連付けられたバッファーの一覧が開きます。
上下に移動して、ENTER を押すとファイルのバッファーが開きます。
ファイルを閉じたい場合には d を押すと「D」というマークがつきます。(マークを解除したい場合は u を押します)
d でマークを付けた後(複数可)、x を押すとファイルを開いているバッファーが閉じます(ファイルは削除されません)。

10. キーバインディング(F8 or C-F8 or S-F8 or M-F8 or C-x F8)【v2.3.0以降】

ファイルに関連付けられてないものも含めて全バッファーの一覧が開きます。
上下に移動して、ENTER を押すとバッファーが開きます。
バッファーを閉じたい場合には d を押すと「D」というマークがつきます。(マークを解除したい場合は u を押します)
d でマークを付けた後(複数可)、x を押すとバッファーが閉じます(ファイルは削除されません)。

11. キーバインディング(F10 or C-F10 or S-F10 or M-F10 or C-x F10)【v2.3.0以降】

シェルスクリプトやスクリプト言語で書かれたファイルを開いている場合に、eshell バッファー上で実行します。
詳細は別記事にしたいと思います。

12. キーバインディング(Ctrl-Shift-F10)【v2.3.0以降】

現在のバッファーのカレントディレクトリに対して、eshell を開きます。すでに eshell が開いている時は先にそのバッファーを閉じてから eshell を再度開きます。
今開いているファイル(スクリプト・C++のソース等)のディレクトリで、スクリプト実行やコンパイル等を即座に行いたいときに使います。
これも詳細は別記事にしたいと思います。


最後に

記事の内容を精査して、前回の記事の内容とマージして別記事を上げたいと思います。


ご不明の点または何かお気づきの点がございましたら、コメント等で教えていただけると助かります。

3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?