如何在 OSX 中从 emacs 打印

如何在 OSX 中从 emacs 打印

我已经在 Mac 上使用 emacs 几年了,但一直在使用其他应用程序进行打印。我终于开始深入研究 emacs 打印。我决定设置 postscript 打印。我打印到文件,然后使用预览进行实际打印。这是我的设置。还有人有更好的吗?

;; Postscript printing
;;
;; defaults
(setq ps-font-size 9)
(setq ps-header-font-size 8)
(setq ps-header-title-font-size 8)
;;(setq ps-header-lines 1) ;; only use the filename and page number

(setq ps-header-offset 18) ;; margin between header and text in 72nds of an inch
(setq ps-left-margin 36)   ;; left margin in 72nds
(setq ps-right-margin 36)  ;; right margin in 72nds

;; Simple print buffer command
;;
(defun sprint-buffer ()
  "Output a postscript file using the defaults."
  (interactive)
  (ps-print-buffer "~/temp.ps")
  (shell-command "open /Applications/Preview.app ~/temp.ps"))

;; Simple print region command
;;
(defun sprint-region ()
  "Output a postscript file using the defaults."
  (interactive)
  (ps-print-region (mark) (point) "~/temp.ps")
  (shell-command "open /Applications/Preview.app ~/temp.ps"))

;; Setup the postscript settings
;;
(defun sprint-setup (use-headers use-line-numbers use-landscape)
  (interactive
   (list (y-or-n-p "Headers?")
         (y-or-n-p "Line numbers?")
         (y-or-n-p "Landscape?")))
  (setq ps-print-headers use-headers)
  (setq ps-line-number use-line-numbers)
  (setq ps-landscape-mode use-landscape))

答案1

emacs 24正在使用htmlize-buffer命令。这将生成一个.html文件(具有所有正确的语法突出显示),然后我可以打印或转换为.pdf

答案2

您将需要较旧的代码longlines.el(当前版本的 visual-line-mode 不能代替 longlines-mode):  http://www.emacswiki.org/emacs/download/longlines.el

https://stackoverflow.com/questions/17077434/adding-a-file-exists-p-condition-inside-an-interactive-list-of-a-function

我正在使用 courier,因为它比 times 更好地处理所有大写字母。

我在其中提供了一个是/否选项,作为 ns-read-file-name 错误的解决方法,但 Emacs 团队几周前刚刚在 Trunk 中修复了该错误。因此,如果您构建了 Emacs Trunk 开发人员版本--with-ns,则不再需要是/否选项。

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; PRINT TO PDF ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'ps-print)

;; Add Consolas
(setq ps-font-info-database
      (append
       '((Consolas
          (fonts (normal      . "Consolas")
                 (bold        . "Consolas-Bold")
                 (italic      . "Consolas-Italic")
                 (bold-italic . "Consolas-Bold-Italic"))
          (size           . 13.0)
          (line-height    . 14.5)
          (space-width    . 6.15)
          (avg-char-width . 6.05)))
       ps-font-info-database))
(setq ps-font-family 'Courier) ;; no difference between Courier and Courier Bold
(setq ps-font-size 13)
;; Print in color
;; (setq-default ps-print-color-p t)
;; Page layout: Header [file-name     2011-12-05]
;;              Footer [                     n/m]
(setq ps-left-margin 60)
(setq ps-right-margin 0) ;; fill-column in print-to-pdf trumps this
;; Header
(setq ps-top-margin 0)
(setq ps-header-offset 0)
(setq ps-header-line-pad 2.6)
(setq ps-header-lines 1)
(setq ps-header-font-size 13)
(setq ps-header-title-font-size 13)
(setq ps-header-font-family 'Consolas)
(setq ps-left-header nil)
(setq ps-right-header nil)
(setq ps-print-header-frame nil)
;; Footer
(setq ps-bottom-margin 0)
(setq ps-footer-lines 1)
(setq ps-footer-font-size 13)
(setq ps-footer-font-family 'Consolas)
(setq ps-print-footer t)
(setq ps-left-footer nil)
(setq ps-right-footer (list "/pagenumberstring load"))
(setq ps-footer-offset 1.0)
(setq ps-footer-line-pad 2.5)
(setq ps-print-footer-frame nil)


(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)
  (ps-spool-buffer-with-faces) )

(defun print-to-pdf (pdf-file-name)
  "Print the current file to the given file."
  (interactive (list (ns-read-file-name "Write PDF file: " "~/.0.data/" nil ".pdf")))
  (when (or (not (file-exists-p pdf-file-name))
            (yes-or-no-p (format "%s  File already exists.  Do you want to overwrite it?" pdf-file-name)))
    (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 63)
    (require 'longlines)
    (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))))

相关内容