2011-08-30 40 views
5

我試圖從Elisp Cookbook一些代碼的內容無名緩衝區,我最初因子評分,此代碼:elisp的創建文件

(defun process-file (file) 
    "Read the contents of a file into a temp buffer and then do 
something there." 
    (when (file-readable-p file) 
    (with-temp-buffer 
     (insert-file-contents file) 
     (goto-char (point-min)) 
     (while (not (eobp)) 
     ;; do something here with buffer content 
     (forward-line))))) 

將創建一個新的(未命名/未保存的)緩衝器我Emacs窗口上,有該文件的內容(也許在前臺打開它)。但是,這不會發生。你能指導我走向這個嗎?

編輯:我嘗試了一下,並得到了這一點:

(defun myTest (file) 
    (interactive "f") 
    ; check if file is readable 
    (when (file-readable-p file) 
     ; create a new "untitled" buffer 
     (let ((myBuf (get-buffer-create "untitled"))) 
      ; make it the current displayed buffer 
      (switch-to-buffer myBuf) 
      (insert "Hello"))))   

這是爲了做到這一點?

由於這是一個名爲「無標題」的緩衝區,因此我只能在會話中使用其中的一個。有什麼我可以用來超過一個,而不訴諸隨機數字?

回答

3

elisp生成唯一緩衝區名稱的方法是使用generate-new-buffer-name。該文檔是:

(generate-new-buffer-name NAME &optional IGNORE) 

返回一個字符串,它是基於名稱的現有緩衝區的名字。 如果沒有名爲NAME的實時緩衝區,則返回NAME。否則 通過追加`'修改名稱,遞增NUMBER(從 2開始),直到找到未使用的名稱,然後返回該名稱。可選 第二個參數IGNORE指定一個可以使用的名稱(如果它在要嘗試的序列中爲 ),即使存在具有該名稱的緩衝區。