格式化Emacs函数中的标题以将缓冲区打印到带有换行的PDF

Rupert Swarbrick提出了以下三个函数来将Emacs缓冲区打印到包含行的pdf(使用ps-print *函数时通常不会这样).

问题是这个功能是为了实现这种换行,必须制作当前缓冲区的副本.这有效地破坏了ps-print-buffer-with-faces在生成的PDF文件中每页顶部显示正确标题的能力.

作为他的解决方案的一部分,Rupert Swarbrick写了一个保留这些信息的函数.但是,生成的标头始终是HeaderLinesLeft

谁能发现错误?

(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 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)
      (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 "/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")))

最佳答案 试试这个:

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

请参阅变量ps-left-header的文档,这是我上面提到的内容.

这是一个很好的能力;我肯定会使用它而不是ps-print.最好让它更像C-u M-x ps-print-buffer-with-faces;两个缺少的功能是标题中的目录名称,并提示用户输入文件名.两者都不太难.

点赞