gpt4 book ai didi

elisp 从文件内容创建未命名的缓冲区

转载 作者:行者123 更新时间:2023-12-04 20:53:44 25 4
gpt4 key购买 nike

我正在尝试来自 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)))))

将在我的 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"))))

这是这样做的方法吗?

由于这是一个名为“untitled”的缓冲区,我只能在一个 session 中使用其中之一。有没有什么我可以使用的东西不止一个,而无需求助于随机数?

最佳答案

生成唯一缓冲区名称的 elisp 方法是使用 generate-new-buffer-name .文档是:

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

Return a string that is the name of no existing buffer based on NAME. If there is no live buffer named NAME, then return NAME. Otherwise modify name by appending `', incrementing NUMBER (starting at 2) until an unused name is found, and then return that name. Optional second argument IGNORE specifies a name that is okay to use (if it is in the sequence to be tried) even if a buffer with that name exists.

关于elisp 从文件内容创建未命名的缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7246212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com