Emacs中普通缓冲区和空格前缀缓冲区之间的区别?

时间:2013-08-24 11:28:41

标签: emacs elisp

通过空格前缀缓冲区,我的意思是名称以空格开头的缓冲区。不确定这些缓冲区的官方术语是什么。

我认为空间前缀缓冲区的唯一区别是它们已禁用撤消,但似乎存在其他差异导致包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 个答案:

答案 0 :(得分:3)

前导空格表示短暂或无趣的缓冲区。它们没有撤消历史记录,许多命令将这些缓冲区放在显着位置,甚至完全忽略它们。见Emacs Lisp Reference, Buffer Names

  

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

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

答案 1 :(得分: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