LoginSignup
1

More than 1 year has passed since last update.

Scratch buffer を Kill させないようにする:emacs-lock-mode

Last updated at Posted at 2019-03-28

わざわざscratch bufferをkillする人はいないと思いますが、"kill-other-windows" などを定義していると、うっかりscratchも一緒に消えてしまって困ることがありますね。

あやまってscratch bufferをkillしたら新しいscratchを自動再生してくれるとか、kill出来ないようにする…というようなTipsも多いのですが、そんな回りくどいことをしなくてEmacsの標準機能だけて簡単に対応できることがわかりました。

emacs-lock-mode を使う##

パッケージも何もいりません。標準機能の emacs-lock-mode を設定します。下記を試してみてください。私は *Messages* も一緒に設定しています。

;; Set buffer that can not be killed.
(with-current-buffer "*scratch*"
  (emacs-lock-mode 'kill))
(with-current-buffer "*Messages*"
  (emacs-lock-mode 'kill))

この設定を有効にしたあとscratchをkill bufferしようとすると、

Buffer "*scratch*" is locked and cannot be killed

と叱られます。つまりkillされないようにロックが掛けられるのです。
emacs-lock-mode というのはもともとemacsの内蔵コマンドにあるのです。

Scratchを自動保存する

私もそうなのですが、ちょこっとした付箋的なメモをするためにscratchを使うという人も多いと思います。そんな人には、Emacsを再起動させても終了前のscratchが復帰してほしい…というニーズがあります。こうした目的のためのパッケージも何種類かありますが、私は、auto-seve-buffers-enhanced.el の機能を利用しています。このパッケージを使っているEmacserは多いと思うので、わざわざ専用のパッケージを使わずともscratchの保存再生が可能になります。

(leaf auto-save-buffers-enhanced
  :ensure t
  :custom
 `((auto-save-buffers-enhanced-exclude-regexps . '("^/ssh:" "^/scp:" "/sudo:"))
			(auto-save-buffers-enhanced-quiet-save-p . t)
			(auto-save-buffers-enhanced-save-scratch-buffer-to-file-p . t)
			;; Disable to prevent freeze in tramp-mode
			(auto-save-buffers-enhanced-include-only-checkout-path . nil))
  :config
  (auto-save-buffers-enhanced t))

scratchを瞬時にpopup表示させる

scratch-pop という便利なパッケージもあるのですが、私にとって余り必要と思わない機能もついているのでシンプルに設定を書いてみました。

popwinを導入した上で下記をinitファイルに書き、適当なキーバインドを設定すると一発でscratch bufferがpopupし、"C-g" で隠れてくれます。

(defun my:pop-scratch ()
  "Popup the scratch buffer."
  (interactive)
  (setq popwin:special-display-config '(("*scratch*")))
  (display-buffer "*scratch*"))

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
1