Adding a file-exists-p condition inside an interactive function list

I'm having difficulty trying to add an if file-exists-p condition to the interactive list of this print-to-pdf function. Error message: file-name-sans-extension: Wrong type argument: stringp, t . I commented on a section that works, but which nonetheless overwrites an existing file without asking yes or no. I tried replacing hello-world with pdf-file-name , but this did not fix the error.

Emacs Trunk build --with-ns on OSX, using ns-read-file-name , likes to overwrite files without asking if the function does not include additional 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)) ) 
+3
source share
1 answer

Why do you want to add it to interactive in the first place? Is something like this job for you?

 (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)))) 
+4
source

All Articles