2013-08-24 43 views
2

通过空间前缀缓冲区,我指的是名称以空格开头的缓冲区。不知道这种缓冲区的官方术语是什么。Emacs中的正常缓冲区和空间前缀缓冲区之间的区别?

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

回答

3

前导空格表示临时或无意义的缓冲区。这些没有撤销历史记录,许多命令将这些缓冲区放在较不突出的位置,甚至完全忽略它们。见Emacs Lisp Reference, Buffer Names

缓冲器是短暂的,一般用户不感兴趣的名称以一个空间,让列表缓冲区和缓冲区菜单命令不提他们(但如果这种缓冲访问一个文件,它被提到)。以空格开头的名称最初也会禁用录制撤销信息;请参阅撤消。

内置命令没有进一步的区别,但任何命令都可以自由地以特殊方式处理这些缓冲区。您可能需要咨询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-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