Emacs中普通缓冲区和带前缀空格缓冲区的区别是什么?

6

通过一个以空格为前缀的缓冲区,我指的是名称以空格开头的缓冲区。不确定这种缓冲区的官方术语是什么。

我曾认为空格前缀缓冲区唯一的区别在于它们的撤销功能被禁用,但似乎还有其他差异导致包htmlize对空格前缀缓冲区做出了不同的反应。

(require 'htmlize)

;; function to write stuff on current buffer and call htmlize-region
(defun my-test-htmlize ()
  (insert "1234567")
  (emacs-lisp-mode)
  ;; (put-text-property 1 2 'font-lock-face "bold")
  (put-text-property 3 4 'font-lock-face 'bold)
  (with-current-buffer (htmlize-region (point-min)
                                       (point-max))
    (buffer-string)))

;; function that makes a (failed) attempt to make current buffer behave like a normal buffer
(defun my-make-buffer-normal ()
  (buffer-enable-undo))

;; like with-temp-buffer, except it uses a buffer that is not a space prefix buffer.
(defmacro my-with-temp-buffer-with-no-space-prefix (&rest body)
  (declare (indent 0) (debug t))
  (let ((temp-buffer (make-symbol "temp-buffer")))
    `(let ((,temp-buffer (generate-new-buffer "*tempwd2kemgv*")))
       (with-current-buffer ,temp-buffer
         (unwind-protect
             (progn ,@body)
           (and (buffer-name ,temp-buffer)
                (kill-buffer ,temp-buffer)))))))

;; In a normal buffer, bold face is htmlized.
(my-with-temp-buffer-with-no-space-prefix
  (my-test-htmlize))

;; In a space prefix buffer, bold face is not htmlized.
(with-temp-buffer
  (my-test-htmlize))

;; Bold face is still not htmlized.
(with-temp-buffer
  (my-make-buffer-normal)
  (my-test-htmlize))
2个回答

7
前导空格表示临时或无趣的缓冲区。这些缓冲区没有撤销历史记录,许多命令将这些缓冲区放置得不那么突出,甚至完全忽略它们。请参见Emacs Lisp Reference, Buffer Names
引用块: 无趣对于用户而言通常是短暂的缓冲区名称以空格开头,所以list-buffers和buffer-menu命令不会提到它们(但如果这样的缓冲区访问文件,则会提到)。名称以空格开头也最初禁用了撤销信息的记录;请参阅Undo。
内置命令没有进一步的差异,但任何命令都可以自由地以特殊方式处理这些缓冲区。您可能需要查阅htmlize源代码来确定不同行为的原因。

1
发现了以空格开头的命名缓冲区的另一个不同之处。这些缓冲区将无法使用额外的手动字体锁定。
(defmacro my-with-temp-buffer-with-name (buffername &rest body)
  (declare (indent 1) (debug t))
  (let ((temp-buffer (make-symbol "temp-buffer")))
    `(let ((,temp-buffer (generate-new-buffer ,buffername)))
       (with-current-buffer ,temp-buffer
         (unwind-protect
             (progn ,@body)
           (and (buffer-name ,temp-buffer)
                (kill-buffer ,temp-buffer)))))))

(my-with-temp-buffer-with-name "*temp*"
  (insert "1234567")
  (emacs-lisp-mode)
  (put-text-property 3 4 'font-lock-face 'bold)
  (print (next-single-property-change 1 'face)))
;; => 3

(my-with-temp-buffer-with-name " *temp*" ; with a space prefix
  (insert "1234567")
  (emacs-lisp-mode)
  (put-text-property 3 4 'font-lock-face 'bold)
  (print (next-single-property-change 1 'face)))
;; => nil

更新:额外的字体锁定无法生效是因为font-lock-mode主动避免与这样的缓冲区一起工作(请参见font-lock-mode的定义)。使其生效的一种方法是直接调用font-lock-default-function

(my-with-temp-buffer-with-name " *temp*" ; with a space prefix
  (insert "1234567")
  (emacs-lisp-mode)
  (put-text-property 3 4 'font-lock-face 'bold)
  (font-lock-default-function t) ; <==
  (print (next-single-property-change 1 'face)))
;; => 3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接