2011-08-30 48 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指定一个可以使用的名称(如果它在要尝试的序列中为 ),即使存在具有该名称的缓冲区。