2013-08-24 108 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