ps-samp.el 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. ;;; ps-samp.el --- ps-print sample setup code
  2. ;; Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. ;; Author: Jim Thompson (was <thompson@wg2.waii.com>)
  4. ;; Jacques Duthen (was <duthen@cegelec-red.fr>)
  5. ;; Vinicius Jose Latorre <viniciusjl@ig.com.br>
  6. ;; Kenichi Handa <handa@m17n.org> (multi-byte characters)
  7. ;; Maintainer: Kenichi Handa <handa@m17n.org> (multi-byte characters)
  8. ;; Vinicius Jose Latorre <viniciusjl@ig.com.br>
  9. ;; Keywords: wp, print, PostScript
  10. ;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre
  11. ;; Package: ps-print
  12. ;; This file is part of GNU Emacs.
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;; Commentary:
  24. ;; Some example hacks for ps-print.el.
  25. ;; This stuff is for anybody that's brave enough to look this far,
  26. ;; and able to figure out how to use it. It isn't really part of
  27. ;; ps-print, but I'll leave it here in hopes it might be useful:
  28. ;; WARNING!!! The following code is *sample* code only.
  29. ;; Don't use it unless you understand what it does!
  30. ;;; Code:
  31. (require 'ps-print)
  32. ;; A hook to bind to `rmail-mode-hook' to locally bind prsc and set
  33. ;; `ps-left-header' specially for mail messages.
  34. (defun ps-rmail-mode-hook ()
  35. (local-set-key [print] 'ps-rmail-print-message-from-summary)
  36. (setq-local ps-header-lines 3)
  37. ;; The left header will display the message's subject, its
  38. ;; author, and the name of the folder it was in.
  39. (setq-local ps-left-header
  40. '(ps-article-subject ps-article-author buffer-name)))
  41. ;; Like `ps-gnus-print-article-from-summary', but for rmail.
  42. (defun ps-rmail-print-message-from-summary ()
  43. (interactive)
  44. (ps-print-message-from-summary 'rmail-summary-buffer "RMAIL"))
  45. ;; Used in `ps-rmail-print-article-from-summary',
  46. ;; `ps-gnus-print-article-from-summary' and `ps-vm-print-message-from-summary'.
  47. (defun ps-print-message-from-summary (summary-buffer summary-default)
  48. (let ((ps-buf (or (and (boundp summary-buffer)
  49. (symbol-value summary-buffer))
  50. summary-default)))
  51. (and (get-buffer ps-buf)
  52. (with-current-buffer ps-buf
  53. (ps-spool-buffer-with-faces)))))
  54. ;; Look in an article or mail message for the Subject: line.
  55. (defun ps-article-subject ()
  56. (save-excursion
  57. (save-restriction
  58. (narrow-to-region (point-min) (progn (rfc822-goto-eoh) (point)))
  59. (concat "Subject: " (or (mail-fetch-field "Subject") "???")))))
  60. ;; Look in an article or mail message for the From: line. Sorta-kinda
  61. ;; understands RFC-822 addresses and can pull the real name out where
  62. ;; it's provided.
  63. (defun ps-article-author ()
  64. (save-excursion
  65. (save-restriction
  66. (narrow-to-region (point-min) (progn (rfc822-goto-eoh) (point)))
  67. (let ((fromstring (mail-fetch-field "From")))
  68. (cond
  69. ;; Try first to match addresses that look like
  70. ;; thompson@wg2.waii.com (Jim Thompson)
  71. ((and fromstring (string-match ".*[ \t]+(\\(.*\\))" fromstring))
  72. (match-string 1 fromstring))
  73. ;; Next try to match addresses that look like
  74. ;; Jim Thompson <thompson@wg2.waii.com> or
  75. ;; "Jim Thompson" <thompson@wg2.waii.com>
  76. ((and fromstring
  77. (string-match "\\(\"?\\)\\(.*\\)\\1[ \t]+<.*>" fromstring))
  78. (match-string 2 fromstring))
  79. ;; Couldn't find a real name -- show the address instead.
  80. (fromstring)
  81. (t "From ???"))))))
  82. ;; A hook to bind to `gnus-article-prepare-hook'. This will set
  83. ;; `ps-left-header' specially for gnus articles. Unfortunately,
  84. ;; `gnus-article-mode-hook' is called only once, the first time the *Article*
  85. ;; buffer enters that mode, so it would only work for the first time
  86. ;; we ran gnus. The second time, this hook wouldn't get set up. The
  87. ;; only alternative is `gnus-article-prepare-hook'.
  88. (defun ps-gnus-article-prepare-hook ()
  89. (setq-local ps-header-lines 3)
  90. ;; The left headers will display the article's subject, its
  91. ;; author, and the newsgroup it was in.
  92. (setq-local ps-left-header
  93. '(ps-article-subject ps-article-author gnus-newsgroup-name)))
  94. ;; A hook to bind to `vm-mode-hook' to locally bind prsc and set
  95. ;; `ps-left-header' specially for mail messages.
  96. (defun ps-vm-mode-hook ()
  97. (local-set-key [print] 'ps-vm-print-message-from-summary)
  98. (setq-local ps-header-lines 3)
  99. ;; The left headers will display the message's subject, its
  100. ;; author, and the name of the folder it was in.
  101. (setq-local ps-left-header
  102. '(ps-article-subject ps-article-author buffer-name)))
  103. ;; Every now and then I forget to switch from the *Summary* buffer to
  104. ;; the *Article* before hitting prsc, and a nicely formatted list of
  105. ;; article subjects shows up at the printer. This function, bound to
  106. ;; prsc for the gnus *Summary* buffer means I don't have to switch
  107. ;; buffers first.
  108. (defun ps-gnus-print-article-from-summary ()
  109. (interactive)
  110. (ps-print-message-from-summary 'gnus-article-buffer "*Article*"))
  111. ;; Like `ps-gnus-print-article-from-summary', but for vm.
  112. (defun ps-vm-print-message-from-summary ()
  113. (interactive)
  114. (ps-print-message-from-summary 'vm-mail-buffer ""))
  115. ;; A hook to bind to `gnus-summary-setup-buffer' to locally bind prsc.
  116. (defun ps-gnus-summary-setup ()
  117. (local-set-key [print] 'ps-gnus-print-article-from-summary))
  118. (defun ps-info-file ()
  119. (save-excursion
  120. (goto-char (point-min))
  121. (if (re-search-forward "File:[ \t]+\\([^, \t\n]*\\)" nil t)
  122. (match-string 1)
  123. "File ???")))
  124. (defun ps-info-node ()
  125. (save-excursion
  126. (goto-char (point-min))
  127. (if (re-search-forward "Node:[ \t]+\\([^,\t\n]*\\)" nil t)
  128. (match-string 1)
  129. "Node ???")))
  130. (defun ps-info-mode-hook ()
  131. ;; The left headers will display the node name and file name.
  132. (setq-local ps-left-header '(ps-info-node ps-info-file)))
  133. ;; WARNING! The following function is a *sample* only, and is *not* meant
  134. ;; to be used as a whole unless you understand what the effects will be!
  135. (defun ps-samp-ps-setup ()
  136. (global-set-key [print] 'ps-spool-buffer-with-faces)
  137. (global-set-key [S-print] 'ps-spool-region-with-faces)
  138. (global-set-key [C-print] 'ps-despool)
  139. (add-hook 'gnus-article-prepare-hook 'ps-gnus-article-prepare-hook)
  140. (add-hook 'gnus-summary-mode-hook 'ps-gnus-summary-setup)
  141. (add-hook 'vm-mode-hook 'ps-vm-mode-hook)
  142. (add-hook 'vm-mode-hooks 'ps-vm-mode-hook)
  143. (add-hook 'Info-mode-hook 'ps-info-mode-hook)
  144. (setq ps-spool-duplex t
  145. ps-print-color-p nil
  146. ps-lpr-command "lpr"
  147. ps-lpr-switches '("-Jjct,duplex_long")
  148. ps-paper-type 'a4
  149. ps-landscape-mode t
  150. ps-number-of-columns 2
  151. ps-left-margin (/ (* 72 1.0) 2.54) ; 1.0 cm
  152. ps-right-margin (/ (* 72 1.0) 2.54) ; 1.0 cm
  153. ps-inter-column (/ (* 72 1.0) 2.54) ; 1.0 cm
  154. ps-bottom-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
  155. ps-top-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
  156. ps-header-offset (/ (* 72 1.0) 2.54) ; 1.0 cm
  157. ps-header-line-pad .15
  158. ps-print-header t
  159. ps-print-header-frame t
  160. ps-header-lines 2
  161. ps-show-n-of-n t
  162. ps-spool-duplex nil
  163. ps-font-family 'Courier
  164. ps-font-size 5.5
  165. ps-header-font-family 'Helvetica
  166. ps-header-font-size 6
  167. ps-header-title-font-size 8))
  168. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  169. ;; If zeroconf is enabled, all CUPS printers can be detected. The
  170. ;; "PostScript printer" menu will be modified dynamically, as printers
  171. ;; are added or removed.
  172. ;; Preconditions:
  173. ;;
  174. ;; * Emacs has D-Bus support enabled. That is, D-Bus is installed on
  175. ;; the system, and Emacs has been configured and built with the
  176. ;; --with-dbus option.
  177. ;;
  178. ;; * The zeroconf daemon avahi-daemon is running.
  179. ;;
  180. ;; * CUPS has enabled the option "Share published printers connected
  181. ;; to this system" (see <http://localhost:631/admin>).
  182. (require 'printing)
  183. (require 'zeroconf)
  184. ;; Add a PostScript printer to the "PostScript printer" menu.
  185. (defun ps-add-printer (service)
  186. (let ((name (zeroconf-service-name service))
  187. (text (zeroconf-service-txt service))
  188. (addr (zeroconf-service-address service))
  189. (port (zeroconf-service-port service))
  190. is-ps cups-queue)
  191. ;; `text' is an array of key=value strings like ("Duplex=T" "Copies=T").
  192. (dolist (string text)
  193. (let ((split (split-string string "=" t)))
  194. ;; If it is a PostScript printer, there must be a string like
  195. ;; "pdl=application/postscript,application/vnd.hp-PCL,...".
  196. (when (and (string-equal "pdl" (car split))
  197. (string-match "application/postscript" (cadr split)))
  198. (setq is-ps t))
  199. ;; A CUPS printer queue is coded as "rp=printers/<name>".
  200. (when (and (string-equal "rp" (car split))
  201. (string-match "printers/\\(.+\\)" (cadr split)))
  202. (setq cups-queue (match-string 1 (cadr split))))))
  203. ;; Add the printer.
  204. (when is-ps
  205. (if cups-queue
  206. (add-to-list
  207. 'pr-ps-printer-alist (list (intern name) "lpr" nil "-P" cups-queue))
  208. ;; No CUPS printer, but a network printer.
  209. (add-to-list
  210. 'pr-ps-printer-alist (list (intern name) "cupsdoprint"
  211. '("-P" "default")
  212. "-H" (format "%s:%s" addr port))))
  213. (pr-update-menus t))))
  214. ;; Remove a printer from the "PostScript printer" menu.
  215. (defun ps-remove-printer (service)
  216. (setq pr-ps-printer-alist
  217. (delete (assoc (intern (zeroconf-service-name service))
  218. pr-ps-printer-alist)
  219. pr-ps-printer-alist))
  220. (pr-update-menus t))
  221. ;; Activate the functions in zeroconf.
  222. (defun ps-make-dynamic-printer-menu ()
  223. (when (featurep 'dbusbind)
  224. (zeroconf-init)
  225. (zeroconf-service-add-hook "_ipp._tcp" :new 'ps-add-printer)
  226. (zeroconf-service-add-hook "_ipp._tcp" :removed 'ps-remove-printer)))
  227. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  228. (provide 'ps-samp)
  229. ;;; ps-samp.el ends here