使用`ns-write-file-using-panel`或类似选项将缓冲区保存为* .pdf

时间:2013-05-27 20:08:07

标签: emacs

Rupert Swarbrick编写的现有代码,后来由Rory Yorke修改,仍然需要使用另存为函数指定文件位置(例如在OSX上,这将是ns-write-file-using-panel)。请问有没有人建议添加类似ns-write-file-using-panel的选项和/或修改脚本中写的/ tmp目录选项?

Word wrap for Emacs print buffer to PDF

Formatting a header in an Emacs function to print a buffer to PDF w/ line wrapping

(defun harden-newlines ()
  (interactive)
  "Make all the newlines in the buffer hard."
  (save-excursion
    (goto-char (point-min))
    (while (search-forward "\n" nil t)
      (backward-char)
      (put-text-property (point) (1+ (point)) 'hard t)
      (forward-char))))

;;    (defun spool-buffer-given-name (name)
;;      (load "ps-print")
;;      (let ((tmp ps-left-header))
;;        (unwind-protect
;;            (progn
;;              (setq ps-left-header
;;                    (list (lambda () name) 'ps-header-dirpart))
;;              (ps-spool-buffer-with-faces))
;;          (setf ps-left-header tmp))))

(defun spool-buffer-given-name (name)
  (let ((ps-left-header (list (format "(%s)" name))))
    (ps-spool-buffer-with-faces)))

(defun print-to-pdf ()
  "Print the current file to /tmp/print.pdf"
  (interactive)
  (let ((wbuf (generate-new-buffer "*Wrapped*"))
        (sbuf (current-buffer)))
    (jit-lock-fontify-now)
    (save-current-buffer
      (set-buffer wbuf)
      (insert-buffer sbuf)
;;      (longlines-mode t)
      (visual-line-mode t)
      (harden-newlines)
      (spool-buffer-given-name (buffer-name sbuf))
      (kill-buffer wbuf)
      (switch-to-buffer "*PostScript*")
      (write-file "/tmp/print.ps")
      (kill-buffer (current-buffer)))
    (call-process "ps2pdf14" nil nil nil
                  "/tmp/print.ps" "/tmp/print.pdf")
    (delete-file "/tmp/print.ps")
    (message "PDF saved to /tmp/print.pdf")))

1 个答案:

答案 0 :(得分:2)

您可以修改最后一个函数,将文件名作为参数:

(defun print-to-pdf (pdf-file-name)
  "Print the current file to the given file."
  (interactive "FWrite PDF file: ")
  (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)
      (setq fill-column 95)
      (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 t)
      (kill-buffer (current-buffer)))
    (call-process "ps2pdf14" nil nil nil ps-file-name pdf-file-name)
    (delete-file ps-file-name)
    (message "PDF saved to %s" pdf-file-name)))

您可能希望添加一些代码来测试PDF文件是否已存在,以避免覆盖任何内容。