4
3

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 5 years have passed since last update.

バッファの一部だけ read-only にする

Posted at

バッファ全体を read-only にするときは buffer-read-only を使いますが,これでは一部だけを編集から保護することができません.

材料

  • inhibit-read-only
    • この変数が nil だと read-only の設定・解除ができません.
  • put-text-property
    • この関数である範囲の属性を変更できます.

詳しくは describe variable/function でお調べください.

サンプル

;; Example: from バッファの一部だけ read-only to

(let ((inhibit-read-only t))
  (put-text-property (progn
                       (search-backward "Example:")
                       (search-forward "from")
                       (backward-word)
                       (point))
                     (progn
                       (search-forward "to")
                       (point))
                     'read-only
                     t))

上のサンプルを *scratch* などの Emacs Lisp モードのバッファにコピーして,(let ... で始まる式を C-x C-e (eval-last-sexp) などで評価すると,一行目コメントの from から to までが read-only になります.

Read-only を解除するには,(let ... で始まる式の一番最後の tnil にしてからもう一度 C-x C-e します.

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?