3

我在尝试在file-exists-p此 print-to-pdf 函数的交互式列表中添加 if 条件时遇到了困难。错误信息是: file-name-sans-extension: Wrong type argument: stringp, t。我已经注释掉了有效的部分,但是它覆盖了现有文件而不提示是或否。我试过用 替换hello-worldpdf-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))
)
4

1 回答 1

4

为什么要首先添加它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))))
于 2013-06-13T01:12:56.837 回答