mh-print.el 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. ;;; mh-print.el --- MH-E printing support
  2. ;; Copyright (C) 2003-2012 Free Software Foundation, Inc.
  3. ;; Author: Jeffrey C Honig <jch@honig.net>
  4. ;; Maintainer: Bill Wohler <wohler@newt.com>
  5. ;; Keywords: mail
  6. ;; See: mh-e.el
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;; Change Log:
  20. ;;; Code:
  21. (require 'mh-e)
  22. (require 'mh-scan)
  23. (require 'ps-print)
  24. (defvar mh-ps-print-color-option ps-print-color-p
  25. "Specify how buffer's text color is printed.
  26. Valid values are:
  27. nil - Do not print colors.
  28. t - Print colors.
  29. black-white - Print colors on black/white printer.
  30. See also `ps-black-white-faces'.
  31. Any other value is treated as t. This variable is initialized
  32. from `ps-print-color-p'.")
  33. (defvar mh-ps-print-func 'ps-spool-buffer-with-faces
  34. "Function to use to spool a buffer.
  35. Sensible choices are the functions `ps-spool-buffer' and
  36. `ps-spool-buffer-with-faces'.")
  37. ;;;###mh-autoload
  38. (defun mh-ps-print-msg (range)
  39. "Print RANGE\\<mh-folder-mode-map>.
  40. Check the documentation of `mh-interactive-range' to see how RANGE is
  41. read in interactive use.
  42. This command will print inline text attachments but will not decrypt
  43. messages. However, when a message is displayed in an MH-Show buffer,
  44. then that buffer is used verbatim for printing with the caveat that
  45. only text attachments, if opened inline, are printed. Therefore,
  46. encrypted messages can be printed by showing and decrypting them
  47. first.
  48. MH-E uses the \"ps-print\" package to do the printing, so you can
  49. customize the printing further by going to the `ps-print'
  50. customization group. This command does not use the options
  51. `mh-lpr-command-format' or `mh-print-background-flag'. See also the
  52. commands \\[mh-ps-print-toggle-color] and
  53. \\[mh-ps-print-toggle-faces]."
  54. (interactive (list (mh-interactive-range "Print")))
  55. (mh-ps-print-range range nil))
  56. (defun mh-ps-print-range (range file)
  57. "Print RANGE to FILE.
  58. This is the function that actually does the work.
  59. If FILE is nil, then the messages are spooled to the printer."
  60. (mh-iterate-on-range msg range
  61. (unwind-protect
  62. (mh-ps-spool-msg msg))
  63. (mh-notate msg mh-note-printed mh-cmd-note))
  64. (ps-despool file))
  65. (defun mh-ps-spool-msg (msg)
  66. "Spool MSG."
  67. (let* ((folder mh-current-folder)
  68. (buffer (mh-in-show-buffer (mh-show-buffer)
  69. (if (not (equal (mh-msg-filename msg folder)
  70. buffer-file-name))
  71. (get-buffer-create mh-temp-buffer)))))
  72. (unwind-protect
  73. (save-excursion
  74. (if buffer
  75. (let ((mh-show-buffer buffer))
  76. (mh-display-msg msg folder)))
  77. (mh-ps-spool-buffer (if buffer buffer mh-show-buffer)))
  78. (if buffer
  79. (kill-buffer buffer)))))
  80. (defun mh-ps-spool-buffer (buffer)
  81. "Spool BUFFER."
  82. (with-current-buffer buffer
  83. (let ((ps-print-color-p mh-ps-print-color-option)
  84. (ps-left-header
  85. (list
  86. (concat "(" (mh-get-header-field "Subject:") ")")
  87. (concat "(" (mh-get-header-field "From:") ")")))
  88. (ps-right-header
  89. (list
  90. "/pagenumberstring load"
  91. (concat "(" (mh-get-header-field "Date:") ")"))))
  92. (funcall mh-ps-print-func))))
  93. ;;;###mh-autoload
  94. (defun mh-ps-print-msg-file (range file)
  95. "Print RANGE to FILE\\<mh-folder-mode-map>.
  96. Check the documentation of `mh-interactive-range' to see how RANGE is
  97. read in interactive use.
  98. This command will print inline text attachments but will not decrypt
  99. messages. However, when a message is displayed in an MH-Show buffer,
  100. then that buffer is used verbatim for printing with the caveat that
  101. only text attachments, if opened inline, are printed. Therefore,
  102. encrypted messages can be printed by showing and decrypting them
  103. first.
  104. MH-E uses the \"ps-print\" package to do the printing, so you can
  105. customize the printing further by going to the `ps-print'
  106. customization group. This command does not use the options
  107. `mh-lpr-command-format' or `mh-print-background-flag'. See also the
  108. commands \\[mh-ps-print-toggle-color] and
  109. \\[mh-ps-print-toggle-faces]."
  110. (interactive (list (mh-interactive-range "Print") (mh-ps-print-preprint 1)))
  111. (mh-ps-print-range range file))
  112. (defun mh-ps-print-preprint (prefix-arg)
  113. "Provide a better default file name for `ps-print-preprint'.
  114. Pass along the PREFIX-ARG to it."
  115. (let ((buffer-file-name (format "mh-%s" (substring (buffer-name) 1))))
  116. (ps-print-preprint prefix-arg)))
  117. ;;;###mh-autoload
  118. (defun mh-ps-print-toggle-faces ()
  119. "Toggle whether printing is done with faces or not.
  120. When faces are enabled, the printed message will look very
  121. similar to the message in the MH-Show buffer."
  122. (interactive)
  123. (if (eq mh-ps-print-func 'ps-spool-buffer-with-faces)
  124. (progn
  125. (setq mh-ps-print-func 'ps-spool-buffer)
  126. (message "Printing without faces"))
  127. (setq mh-ps-print-func 'ps-spool-buffer-with-faces)
  128. (message "Printing with faces")))
  129. ;;;###mh-autoload
  130. (defun mh-ps-print-toggle-color ()
  131. "Toggle whether color is used in printing messages.
  132. Colors are emulated on black-and-white printers with shades of
  133. gray. This might produce illegible output, even if your screen
  134. colors only use shades of gray. If this is the case, try using
  135. this command to toggle between color, no color, and a black and
  136. white representation of the colors and see which works best. You
  137. change this setting permanently by customizing the option
  138. `ps-print-color-p'."
  139. (interactive)
  140. (if (eq mh-ps-print-color-option nil)
  141. (progn
  142. (setq mh-ps-print-color-option 'black-white)
  143. (message "Colors will be printed as black & white"))
  144. (if (eq mh-ps-print-color-option 'black-white)
  145. (progn
  146. (setq mh-ps-print-color-option t)
  147. (message "Colors will be printed"))
  148. (setq mh-ps-print-color-option nil)
  149. (message "Colors will not be printed"))))
  150. ;; Old non-PS based printing
  151. ;;;###mh-autoload
  152. (defun mh-print-msg (range)
  153. "Print RANGE the old fashioned way\\<mh-folder-mode-map>.
  154. The message is formatted with \"mhl\" (see option
  155. `mh-mhl-format-file') and printed with the \"lpr\" command (see
  156. option `mh-lpr-command-format').
  157. Check the documentation of `mh-interactive-range' to see how
  158. RANGE is read in interactive use.
  159. Consider using \\[mh-ps-print-msg] instead."
  160. (interactive (list (mh-interactive-range "Print")))
  161. (message "Printing...")
  162. (let (msgs)
  163. ;; Gather message numbers and add them to "printed" sequence.
  164. (mh-iterate-on-range msg range
  165. (mh-add-msgs-to-seq msg 'printed t)
  166. (mh-notate nil mh-note-printed mh-cmd-note)
  167. (push msg msgs))
  168. (setq msgs (nreverse msgs))
  169. ;; Print scan listing if we have more than one message.
  170. (if (> (length msgs) 1)
  171. (let* ((msgs-string
  172. (mapconcat 'identity (mh-list-to-string
  173. (mh-coalesce-msg-list msgs)) " "))
  174. (lpr-command
  175. (format mh-lpr-command-format
  176. (cond ((listp range)
  177. (format "Folder: %s, Messages: %s"
  178. mh-current-folder msgs-string))
  179. ((symbolp range)
  180. (format "Folder: %s, Sequence: %s"
  181. mh-current-folder range)))))
  182. (scan-command
  183. (format "scan %s | %s" msgs-string lpr-command)))
  184. (if mh-print-background-flag
  185. (mh-exec-cmd-daemon shell-file-name nil "-c" scan-command)
  186. (call-process shell-file-name nil nil nil "-c" scan-command))))
  187. ;; Print the messages
  188. (dolist (msg msgs)
  189. (let* ((mhl-command (format "%s %s %s"
  190. (expand-file-name "mhl" mh-lib-progs)
  191. (if mh-mhl-format-file
  192. (format " -form %s" mh-mhl-format-file)
  193. "")
  194. (mh-msg-filename msg)))
  195. (lpr-command
  196. (format mh-lpr-command-format
  197. (format "%s/%s" mh-current-folder msg)))
  198. (print-command
  199. (format "%s | %s" mhl-command lpr-command)))
  200. (if mh-print-background-flag
  201. (mh-exec-cmd-daemon shell-file-name nil "-c" print-command)
  202. (call-process shell-file-name nil nil nil "-c" print-command)))))
  203. (message "Printing...done"))
  204. (provide 'mh-print)
  205. ;; Local Variables:
  206. ;; indent-tabs-mode: nil
  207. ;; sentence-end-double-space: nil
  208. ;; End:
  209. ;;; mh-print.el ends here