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

发布于 2024-12-04 15:37:16 字数 1629 浏览 1 评论 0原文

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

Rupert Swarbrick came up with the following three functions to print an Emacs buffer to pdf in which lines are wrapped (this is not normally the case when using ps-print* functions).

The problem is this function is that to achieve this line wrapping, a copy of the current buffer has to be made. This effectively breaks the capability of ps-print-buffer-with-faces to display a correct header on top of each page in the resulting PDF file.

As part of his solution, Rupert Swarbrick wrote a function that preserves this information. The resulting header, however, is always HeaderLinesLeft

Who can spot the mistake?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

記柔刀 2024-12-11 15:37:16

试试这个:

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

请参阅变量 ps-left-header 的文档,这就是我了解上述内容的原因。

这是一个很巧妙的能力;我肯定会用它代替 ps-print。让它更像 Cu Mx ps-print-buffer-with-faces 会很好;两个缺失的功能是标题中的目录名称,以及提示用户输入文件名。两者都不太困难。

Try this:

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

See documentation for variable ps-left-header, which is what clued me in to the above.

This is a neat capability; I'll definitely use it instead of ps-print. It would be good to make it act more like C-u M-x ps-print-buffer-with-faces; two missing features are directory name in the header, and prompting the user for a filename. Neither is too difficult.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文