Emacs打印缓冲区到PDF的自动换行

4

我使用这个函数将缓冲区的内容打印到PDF中

(来自我的.emacs文件:)

(defun print-to-pdf ()
  (interactive)
  (ps-spool-buffer-with-faces)
  (switch-to-buffer "*PostScript*")
  (write-file "/tmp/tmp.ps")
  (kill-buffer "tmp.ps")
  (setq cmd (concat "ps2pdf14 /tmp/tmp.ps /home/user/" (buffer-name) ".pdf"))
  (shell-command cmd)
  (shell-command "rm /tmp/tmp.ps")
  (message (concat "Saved to:  /home/user/" (buffer-name) ".pdf"))  
  )

然而,我找不到一种方法在将PostScript缓冲区写入磁盘之前启用或应用可视化行小模式,以便在输出中启用自动换行。


将缓冲区复制到临时缓冲区,并在发送到 spool 之前启用 visual-line minor mode 如何? - kindahero
在我运行的缓冲区中,vl-mode 已经启用了。 - Werner
1个回答

5
获取视觉行模式受到的问题在于它插入了“软换行”(这些被PS渲染器忽略)。解决方案是用硬换行来替换它们。下面的代码可以实现您想要的效果。请注意,我们在临时缓冲区中调用harden-newlines,以免破坏当前文档。此外,我已经更改了输出目标,使其始终落在/tmp/print.pdf中。没有任何警告地覆盖您/home中的文档似乎是不明智的!您可以随时移动PDF文件。无论如何,这是您想要的吗?
(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)
      (longlines-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")))

同时,longlines 限制一行的字符数为 80。将 (longlines-mode t) 替换为 (visual-line-mode t) 可以消除自动换行(因为断点不同?)。 - Werner
是的,这很有道理(关于longlines-mode)。但我不知道字体锁定出了什么问题... :-( - Rupert Swarbrick
嗯,这真的很奇怪。对于我刚测试的文本文件,字体锁定在第三页中途消失了... - Rupert Swarbrick
解决了!问题出在“jit”字体锁定上(为了速度只对您可以看到的缓冲区位进行字体化)。您只需要添加一个调用(jit-lock-fontify-now)即可:我马上会编辑我的答案。 - Rupert Swarbrick
我只是想创建一个带有两个相关帖子的评论:第一个跟进链接:https://dev59.com/pFvUa4cB1Zd3GeqPsVXy,第二个跟进链接:http://stackoverflow.com/questions/16779882/save-buffer-as-a-pdf-with-ns-write-file-using-panel-or-similar-option。我仍然需要实现打开面板,但它几乎做到了我想要的。 - lawlist
显示剩余8条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接