gpt4 book ai didi

emacs - 在函数的交互式列表中添加 file-exists-p 条件

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

我在尝试在此 print-to-pdf 功能的交互列表中添加 if file-exists-p 条件时遇到困难。错误消息是:file-name-sans-extension: Wrong type argument: stringp, t。我已经注释掉了有效的部分,但它仍然覆盖了现有文件而不提示是或否。我已尝试将 hello-world 替换为 pdf-file-name,但这并没有解决错误。

Emacs Trunk 开发人员在 OSX 上构建 --with-ns,使用 ns-read-file-name,喜欢在没有提示的情况下覆盖文件,除非有额外的 file-exists-p包含在函数中。

(defun print-to-pdf (pdf-file-name)
"Print the current file to the given file."

;; (interactive (list (ns-read-file-name "Write PDF file: " "~/" nil ".pdf")

(interactive (list

(let ((hello-world (ns-read-file-name "Write PDF file: " "~/" nil ".pdf")))
(if (file-exists-p hello-world)
(or (yes-or-no-p (format "File %s exists. Save anyway? " hello-world))
(error ""))))

))


(let (
(ps-file-name (concat (file-name-sans-extension pdf-file-name) ".ps"))
(wbuf (generate-new-buffer "*Wrapped*"))
(sbuf (current-buffer)))
(jit-lock-fontify-now)
(save-current-buffer
(set-buffer wbuf)
(insert-buffer sbuf)
(longlines-mode t)
(harden-newlines)
(message (buffer-name sbuf))
(spool-buffer-given-name (buffer-name sbuf))
(kill-buffer wbuf)
(switch-to-buffer "*PostScript*")
(write-file ps-file-name)
(kill-buffer (current-buffer)))
(call-process "/usr/local/bin/ps2pdf14" nil nil nil ps-file-name pdf-file-name)
(delete-file ps-file-name)
(message "PDF saved to %s" pdf-file-name))
)

最佳答案

为什么首先要将它添加到 interactive 中?这样的事情不适合你吗?

(defun print-to-pdf (pdf-file-name)
"Print the current buffer to the given file as PDF."
(interactive (list (ns-read-file-name "Write PDF file: " "~/" nil ".pdf")))
(when (and pdf-file-name
(or (not (file-exists-p pdf-file-name))
(yes-or-no-p "File exists. Overwrite? ")))
(let ((ps-file-name (concat (file-name-sans-extension pdf-file-name) ".ps"))
(wbuf (generate-new-buffer "*Wrapped*"))
(sbuf (current-buffer)))
(jit-lock-fontify-now)
(save-current-buffer
(set-buffer wbuf)
(insert-buffer sbuf)
(longlines-mode t)
(harden-newlines)
(message (buffer-name sbuf))
(spool-buffer-given-name (buffer-name sbuf))
(kill-buffer wbuf)
(switch-to-buffer "*PostScript*")
(write-file ps-file-name)
(kill-buffer (current-buffer)))
(call-process "/usr/local/bin/ps2pdf14" nil nil nil ps-file-name pdf-file-name)
(delete-file ps-file-name)
(message "PDF saved to %s" pdf-file-name))))

关于emacs - 在函数的交互式列表中添加 file-exists-p 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17077434/

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