]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-print.el
6653d97782c4c6a046679f8da99ceec73611989b
[notmuch] / emacs / notmuch-print.el
1 ;; notmuch-print.el --- printing messages from notmuch.
2 ;;
3 ;; Copyright © David Edmondson
4 ;;
5 ;; This file is part of Notmuch.
6 ;;
7 ;; Notmuch is free software: you can redistribute it and/or modify it
8 ;; under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
11 ;;
12 ;; Notmuch is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 ;; General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.
19 ;;
20 ;; Authors: David Edmondson <dme@dme.org>
21
22 (require 'notmuch-lib)
23
24 (declare-function notmuch-show-get-prop "notmuch-show" (prop &optional props))
25
26 (defcustom notmuch-print-mechanism 'notmuch-print-lpr
27   "How should printing be done?"
28   :group 'notmuch
29   :type '(choice
30           (function :tag "Use lpr" notmuch-print-lpr)
31           (function :tag "Use ps-print" notmuch-print-ps-print)
32           (function :tag "Use ps-print then evince" notmuch-print-ps-print/evince)
33           (function :tag "Use muttprint" notmuch-print-muttprint)
34           (function :tag "Use muttprint then evince" notmuch-print-muttprint/evince)
35           (function :tag "Using a custom function")))
36
37 ;; Utility functions:
38
39 (defun notmuch-print-run-evince (file)
40   "View FILE using 'evince'."
41   (start-process "evince" nil "evince" file))
42
43 (defun notmuch-print-run-muttprint (&optional output)
44   "Pass the contents of the current buffer to 'muttprint'.
45
46 Optional OUTPUT allows passing a list of flags to muttprint."
47   (apply #'call-process-region (point-min) (point-max)
48          ;; Reads from stdin.
49          "muttprint"
50          nil nil nil
51          ;; Show the tags.
52          "--printed-headers" "Date_To_From_CC_Newsgroups_*Subject*_/Tags/"
53          output))
54
55 ;; User-visible functions:
56
57 (defun notmuch-print-lpr (msg)
58   "Print a message buffer using lpr."
59   (lpr-buffer))
60
61 (defun notmuch-print-ps-print (msg)
62   "Print a message buffer using the ps-print package."
63   (let ((subject (notmuch-prettify-subject
64                   (plist-get (notmuch-show-get-prop :headers msg) :Subject))))
65     (rename-buffer subject t)
66     (ps-print-buffer)))
67
68 (defun notmuch-print-ps-print/evince (msg)
69   "Preview a message buffer using ps-print and evince."
70   (let ((ps-file (make-temp-file "notmuch"))
71         (subject (notmuch-prettify-subject
72                   (plist-get (notmuch-show-get-prop :headers msg) :Subject))))
73     (rename-buffer subject t)
74     (ps-print-buffer ps-file)
75     (notmuch-print-run-evince ps-file)))
76
77 (defun notmuch-print-muttprint (msg)
78   "Print a message using muttprint."
79   (notmuch-print-run-muttprint))
80
81 (defun notmuch-print-muttprint/evince (msg)
82   "Preview a message buffer using muttprint and evince."
83   (let ((ps-file (make-temp-file "notmuch")))
84     (notmuch-print-run-muttprint (list "--printer" (concat "TO_FILE:" ps-file)))
85     (notmuch-print-run-evince ps-file)))
86
87 (defun notmuch-print-message (msg)
88   "Print a message using the user-selected mechanism."
89   (set-buffer-modified-p nil)
90   (funcall notmuch-print-mechanism msg))
91
92 (provide 'notmuch-print)