4

空格前缀缓冲区是指名称以空格开头的缓冲区。不确定这种缓冲区的官方术语是什么。

我认为空间前缀缓冲区的唯一区别是它们禁用了撤消,但似乎还有其他差异导致包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))
4

2 回答 2

5

前导空格表示临时或无意义的缓冲区。这些没有撤消历史记录,许多命令将这些缓冲区放在不太显眼的位置,甚至完全忽略它们。请参阅Emacs Lisp 参考,缓冲区名称

短暂且用户通常不感兴趣的缓冲区的名称以空格开头,因此 list-buffers 和 buffer-menu 命令不会提及它们(但如果这样的缓冲区访问文件,则会提及)。以空格开头的名称最初也会禁用记录撤消信息;请参阅撤消。

内置命令没有进一步的区别,但是任何命令都可以自由地以特殊方式处理这些缓冲区。您可能需要查阅 htmlize 源以确定不同行为的原因。

于 2013-08-24T12:28:31.530 回答
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
于 2013-08-25T10:29:11.843 回答