我正在尝试一些来自Elisp Cookbook的代码,我最初认为这段代码:
(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)))))
Run Code Online (Sandbox Code Playgroud)
将在我的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"))))
Run Code Online (Sandbox Code Playgroud)
这是这样做的吗?
由于这是一个名为"无标题"的缓冲区,因此我只能在会话中使用其中一个缓冲区.有没有什么我可以用来拥有多个,而不是诉诸随机数?
生成唯一缓冲区名称的 elisp 方法是使用generate-new-buffer-name. 文档是:
(generate-new-buffer-name NAME &optional IGNORE)
Run Code Online (Sandbox Code Playgroud)
返回一个字符串,该字符串是基于 NAME 的不存在的缓冲区的名称。如果没有名为 NAME 的实时缓冲区,则返回 NAME。否则,通过附加 '' 来修改名称,递增 NUMBER(从 2 开始)直到找到未使用的名称,然后返回该名称。可选的第二个参数 IGNORE 指定可以使用的名称(如果它位于要尝试的序列中),即使存在具有该名称的缓冲区也是如此。