ps-samp.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. ;;; ps-samp.el --- ps-print sample setup code
  2. ;; Copyright (C) 2007-2012 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. ;; See ps-print.el for documentation.
  25. ;;; Code:
  26. (require 'ps-print)
  27. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  28. ;;; Sample Setup Code:
  29. ;; This stuff is for anybody that's brave enough to look this far,
  30. ;; and able to figure out how to use it. It isn't really part of
  31. ;; ps-print, but I'll leave it here in hopes it might be useful:
  32. ;; WARNING!!! The following code is *sample* code only.
  33. ;; Don't use it unless you understand what it does!
  34. ;; The key `f22' should probably be replaced by `print'. --Stef
  35. ;; A hook to bind to `rmail-mode-hook' to locally bind prsc and set the
  36. ;; `ps-left-headers' specially for mail messages.
  37. (defun ps-rmail-mode-hook ()
  38. (local-set-key [(f22)] 'ps-rmail-print-message-from-summary)
  39. (setq ps-header-lines 3
  40. ps-left-header
  41. ;; The left headers will display the message's subject, its
  42. ;; author, and the name of the folder it was in.
  43. '(ps-article-subject ps-article-author buffer-name)))
  44. ;; See `ps-gnus-print-article-from-summary'. This function does the
  45. ;; same thing for rmail.
  46. (defun ps-rmail-print-message-from-summary ()
  47. (interactive)
  48. (ps-print-message-from-summary 'rmail-summary-buffer "RMAIL"))
  49. ;; Used in `ps-rmail-print-article-from-summary',
  50. ;; `ps-gnus-print-article-from-summary' and `ps-vm-print-message-from-summary'.
  51. (defun ps-print-message-from-summary (summary-buffer summary-default)
  52. (let ((ps-buf (or (and (boundp summary-buffer)
  53. (symbol-value summary-buffer))
  54. summary-default)))
  55. (and (get-buffer ps-buf)
  56. (with-current-buffer ps-buf
  57. (ps-spool-buffer-with-faces)))))
  58. ;; Look in an article or mail message for the Subject: line. To be
  59. ;; placed in `ps-left-headers'.
  60. (defun ps-article-subject ()
  61. (save-excursion
  62. (goto-char (point-min))
  63. (if (re-search-forward "^Subject:[ \t]+\\(.*\\)$" nil t)
  64. (buffer-substring (match-beginning 1) (match-end 1))
  65. "Subject ???")))
  66. ;; Look in an article or mail message for the From: line. Sorta-kinda
  67. ;; understands RFC-822 addresses and can pull the real name out where
  68. ;; it's provided. To be placed in `ps-left-headers'.
  69. (defun ps-article-author ()
  70. (save-excursion
  71. (goto-char (point-min))
  72. (if (re-search-forward "^From:[ \t]+\\(.*\\)$" nil t)
  73. (let ((fromstring (buffer-substring (match-beginning 1) (match-end 1))))
  74. (cond
  75. ;; Try first to match addresses that look like
  76. ;; thompson@wg2.waii.com (Jim Thompson)
  77. ((string-match ".*[ \t]+(\\(.*\\))" fromstring)
  78. (substring fromstring (match-beginning 1) (match-end 1)))
  79. ;; Next try to match addresses that look like
  80. ;; Jim Thompson <thompson@wg2.waii.com> or
  81. ;; "Jim Thompson" <thompson@wg2.waii.com>
  82. ((string-match "\\(\"?\\)\\(.*\\)\\1[ \t]+<.*>" fromstring)
  83. (substring fromstring (match-beginning 2) (match-end 2)))
  84. ;; Couldn't find a real name -- show the address instead.
  85. (t fromstring)))
  86. "From ???")))
  87. ;; A hook to bind to `gnus-article-prepare-hook'. This will set the
  88. ;; `ps-left-headers' specially for gnus articles. Unfortunately,
  89. ;; `gnus-article-mode-hook' is called only once, the first time the *Article*
  90. ;; buffer enters that mode, so it would only work for the first time
  91. ;; we ran gnus. The second time, this hook wouldn't get set up. The
  92. ;; only alternative is `gnus-article-prepare-hook'.
  93. (defun ps-gnus-article-prepare-hook ()
  94. (setq ps-header-lines 3
  95. ps-left-header
  96. ;; The left headers will display the article's subject, its
  97. ;; author, and the newsgroup it was in.
  98. '(ps-article-subject ps-article-author gnus-newsgroup-name)))
  99. ;; A hook to bind to `vm-mode-hook' to locally bind prsc and set the
  100. ;; `ps-left-headers' specially for mail messages.
  101. (defun ps-vm-mode-hook ()
  102. (local-set-key [(f22)] 'ps-vm-print-message-from-summary)
  103. (setq ps-header-lines 3
  104. ps-left-header
  105. ;; The left headers will display the message's subject, its
  106. ;; author, and the name of the folder it was in.
  107. '(ps-article-subject ps-article-author buffer-name)))
  108. ;; Every now and then I forget to switch from the *Summary* buffer to
  109. ;; the *Article* before hitting prsc, and a nicely formatted list of
  110. ;; article subjects shows up at the printer. This function, bound to
  111. ;; prsc for the gnus *Summary* buffer means I don't have to switch
  112. ;; buffers first.
  113. ;; sb: Updated for Gnus 5.
  114. (defun ps-gnus-print-article-from-summary ()
  115. (interactive)
  116. (ps-print-message-from-summary 'gnus-article-buffer "*Article*"))
  117. ;; See `ps-gnus-print-article-from-summary'. This function does the
  118. ;; same thing for vm.
  119. (defun ps-vm-print-message-from-summary ()
  120. (interactive)
  121. (ps-print-message-from-summary 'vm-mail-buffer ""))
  122. ;; A hook to bind to bind to `gnus-summary-setup-buffer' to locally bind
  123. ;; prsc.
  124. (defun ps-gnus-summary-setup ()
  125. (local-set-key [(f22)] 'ps-gnus-print-article-from-summary))
  126. ;; Look in an article or mail message for the Subject: line. To be
  127. ;; placed in `ps-left-headers'.
  128. (defun ps-info-file ()
  129. (save-excursion
  130. (goto-char (point-min))
  131. (if (re-search-forward "File:[ \t]+\\([^, \t\n]*\\)" nil t)
  132. (buffer-substring (match-beginning 1) (match-end 1))
  133. "File ???")))
  134. ;; Look in an article or mail message for the Subject: line. To be
  135. ;; placed in `ps-left-headers'.
  136. (defun ps-info-node ()
  137. (save-excursion
  138. (goto-char (point-min))
  139. (if (re-search-forward "Node:[ \t]+\\([^,\t\n]*\\)" nil t)
  140. (buffer-substring (match-beginning 1) (match-end 1))
  141. "Node ???")))
  142. (defun ps-info-mode-hook ()
  143. (setq ps-left-header
  144. ;; The left headers will display the node name and file name.
  145. '(ps-info-node ps-info-file)))
  146. ;; WARNING! The following function is a *sample* only, and is *not*
  147. ;; meant to be used as a whole unless you understand what the effects
  148. ;; will be! (In fact, this is a copy of Jim's setup for ps-print --
  149. ;; I'd be very surprised if it was useful to *anybody*, without
  150. ;; modification.)
  151. (defun ps-jts-ps-setup ()
  152. (global-set-key [(f22)] 'ps-spool-buffer-with-faces) ;f22 is prsc
  153. (global-set-key [(shift f22)] 'ps-spool-region-with-faces)
  154. (global-set-key [(control f22)] 'ps-despool)
  155. (add-hook 'gnus-article-prepare-hook 'ps-gnus-article-prepare-hook)
  156. (add-hook 'gnus-summary-mode-hook 'ps-gnus-summary-setup)
  157. (add-hook 'vm-mode-hook 'ps-vm-mode-hook)
  158. (add-hook 'vm-mode-hooks 'ps-vm-mode-hook)
  159. (add-hook 'Info-mode-hook 'ps-info-mode-hook)
  160. (setq ps-spool-duplex t
  161. ps-print-color-p nil
  162. ps-lpr-command "lpr"
  163. ps-lpr-switches '("-Jjct,duplex_long"))
  164. 'ps-jts-ps-setup)
  165. ;; WARNING! The following function is a *sample* only, and is *not*
  166. ;; meant to be used as a whole unless it corresponds to your needs.
  167. ;; (In fact, this is a copy of Jack's setup for ps-print --
  168. ;; I would not be that surprised if it was useful to *anybody*,
  169. ;; without modification.)
  170. (defun ps-jack-setup ()
  171. (setq ps-print-color-p nil
  172. ps-lpr-command "lpr"
  173. ps-lpr-switches nil
  174. ps-paper-type 'a4
  175. ps-landscape-mode t
  176. ps-number-of-columns 2
  177. ps-left-margin (/ (* 72 1.0) 2.54) ; 1.0 cm
  178. ps-right-margin (/ (* 72 1.0) 2.54) ; 1.0 cm
  179. ps-inter-column (/ (* 72 1.0) 2.54) ; 1.0 cm
  180. ps-bottom-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
  181. ps-top-margin (/ (* 72 1.5) 2.54) ; 1.5 cm
  182. ps-header-offset (/ (* 72 1.0) 2.54) ; 1.0 cm
  183. ps-header-line-pad .15
  184. ps-print-header t
  185. ps-print-header-frame t
  186. ps-header-lines 2
  187. ps-show-n-of-n t
  188. ps-spool-duplex nil
  189. ps-font-family 'Courier
  190. ps-font-size 5.5
  191. ps-header-font-family 'Helvetica
  192. ps-header-font-size 6
  193. ps-header-title-font-size 8)
  194. 'ps-jack-setup)
  195. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  196. ;; If zeroconf is enabled, all CUPS printers can be detected. The
  197. ;; "PostScript printer" menu will be modified dynamically, as printers
  198. ;; are added or removed.
  199. ;; Preconditions:
  200. ;;
  201. ;; * Emacs has D-Bus support enabled. That is, D-Bus is installed on
  202. ;; the system, and Emacs has been configured and built with the
  203. ;; --with-dbus option.
  204. ;;
  205. ;; * The zeroconf daemon avahi-daemon is running.
  206. ;;
  207. ;; * CUPS has enabled the option "Share published printers connected
  208. ;; to this system" (see <http://localhost:631/admin>).
  209. (eval-when-compile
  210. (require 'cl))
  211. (require 'printing)
  212. (require 'zeroconf)
  213. ;; Add a PostScript printer to the "PostScript printer" menu.
  214. (defun ps-add-printer (service)
  215. (let ((name (zeroconf-service-name service))
  216. (text (zeroconf-service-txt service))
  217. (addr (zeroconf-service-address service))
  218. (port (zeroconf-service-port service))
  219. is-ps cups-queue)
  220. ;; `text' is an array of key=value strings like ("Duplex=T" "Copies=T").
  221. (dolist (string text)
  222. (let ((split (split-string string "=" t)))
  223. ;; If it is a PostScript printer, there must be a string like
  224. ;; "pdl=application/postscript,application/vnd.hp-PCL,...".
  225. (when (and (string-equal "pdl" (car split))
  226. (string-match "application/postscript" (cadr split)))
  227. (setq is-ps t))
  228. ;; A CUPS printer queue is coded as "rp=printers/<name>".
  229. (when (and (string-equal "rp" (car split))
  230. (string-match "printers/\\(.+\\)" (cadr split)))
  231. (setq cups-queue (match-string 1 (cadr split))))))
  232. ;; Add the printer.
  233. (when is-ps
  234. (if cups-queue
  235. (add-to-list
  236. 'pr-ps-printer-alist (list (intern name) "lpr" nil "-P" cups-queue))
  237. ;; No CUPS printer, but a network printer.
  238. (add-to-list
  239. 'pr-ps-printer-alist (list (intern name) "cupsdoprint"
  240. '("-P" "default")
  241. "-H" (format "%s:%s" addr port))))
  242. (pr-update-menus t))))
  243. ;; Remove a printer from the "PostScript printer" menu.
  244. (defun ps-remove-printer (service)
  245. (setq pr-ps-printer-alist
  246. (delete (assoc (intern (zeroconf-service-name service))
  247. pr-ps-printer-alist)
  248. pr-ps-printer-alist))
  249. (pr-update-menus t))
  250. ;; Activate the functions in zeroconf.
  251. (defun ps-make-dynamic-printer-menu ()
  252. (when (featurep 'dbusbind)
  253. (zeroconf-init)
  254. (zeroconf-service-add-hook "_ipp._tcp" :new 'ps-add-printer)
  255. (zeroconf-service-add-hook "_ipp._tcp" :removed 'ps-remove-printer)))
  256. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  257. (provide 'ps-samp)
  258. ;;; ps-samp.el ends here