lpr.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. ;;; lpr.el --- print Emacs buffer on line printer
  2. ;; Copyright (C) 1985, 1988, 1992, 1994, 2001-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Maintainer: FSF
  5. ;; Keywords: unix
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it 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. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Commands to send the region or a buffer to your printer. Entry points
  19. ;; are `lpr-buffer', `print-buffer', `lpr-region', or `print-region'; option
  20. ;; variables include `printer-name', `lpr-switches' and `lpr-command'.
  21. ;;; Code:
  22. (eval-when-compile (require 'cl))
  23. ;;;###autoload
  24. (defvar lpr-windows-system
  25. (memq system-type '(ms-dos windows-nt))
  26. "Non-nil if running on MS-DOS or MS Windows.")
  27. ;;;###autoload
  28. (defvar lpr-lp-system
  29. (memq system-type '(usg-unix-v hpux irix))
  30. "Non-nil if running on a system type that uses the \"lp\" command.")
  31. (defgroup lpr nil
  32. "Print Emacs buffer on line printer."
  33. :group 'wp)
  34. ;;;###autoload
  35. (defcustom printer-name
  36. (and (eq system-type 'ms-dos) "PRN")
  37. "The name of a local printer to which data is sent for printing.
  38. \(Note that PostScript files are sent to `ps-printer-name', which see.\)
  39. On Unix-like systems, a string value should be a name understood by
  40. lpr's -P option; otherwise the value should be nil.
  41. On MS-DOS and MS-Windows systems, a string value is taken as the name of
  42. a printer device or port, provided `lpr-command' is set to \"\".
  43. Typical non-default settings would be \"LPT1\" to \"LPT3\" for parallel
  44. printers, or \"COM1\" to \"COM4\" or \"AUX\" for serial printers, or
  45. \"//hostname/printer\" for a shared network printer. You can also set
  46. it to the name of a file, in which case the output gets appended to that
  47. file. If you want to discard the printed output, set this to \"NUL\"."
  48. :type '(choice :menu-tag "Printer Name"
  49. :tag "Printer Name"
  50. (const :tag "Default" nil)
  51. ;; could use string but then we lose completion for files.
  52. (file :tag "Name"))
  53. :group 'lpr)
  54. ;;;###autoload
  55. (defcustom lpr-switches nil
  56. "List of strings to pass as extra options for the printer program.
  57. It is recommended to set `printer-name' instead of including an explicit
  58. switch on this list.
  59. See `lpr-command'."
  60. :type '(repeat (string :tag "Argument"))
  61. :group 'lpr)
  62. (defcustom lpr-add-switches (memq system-type '(berkeley-unix gnu/linux))
  63. "Non-nil means construct `-T' and `-J' options for the printer program.
  64. These are made assuming that the program is `lpr';
  65. if you are using some other incompatible printer program,
  66. this variable should be nil."
  67. :type 'boolean
  68. :group 'lpr)
  69. (defcustom lpr-printer-switch
  70. (if lpr-lp-system
  71. "-d "
  72. "-P")
  73. "Printer switch, that is, something like \"-P\", \"-d \", \"/D:\", etc.
  74. This switch is used in conjunction with `printer-name'."
  75. :type '(choice :menu-tag "Printer Name Switch"
  76. :tag "Printer Name Switch"
  77. (const :tag "None" nil)
  78. (string :tag "Printer Switch"))
  79. :group 'lpr)
  80. ;;;###autoload
  81. (defcustom lpr-command
  82. (purecopy
  83. (cond
  84. (lpr-windows-system
  85. "")
  86. (lpr-lp-system
  87. "lp")
  88. (t
  89. "lpr")))
  90. "Name of program for printing a file.
  91. On MS-DOS and MS-Windows systems, if the value is an empty string then
  92. Emacs will write directly to the printer port named by `printer-name'.
  93. The programs `print' and `nprint' (the standard print programs on
  94. Windows NT and Novell Netware respectively) are handled specially, using
  95. `printer-name' as the destination for output; any other program is
  96. treated like `lpr' except that an explicit filename is given as the last
  97. argument."
  98. :type 'string
  99. :group 'lpr)
  100. ;; Default is nil, because that enables us to use pr -f
  101. ;; which is more reliable than pr with no args, which is what lpr -p does.
  102. (defcustom lpr-headers-switches nil
  103. "List of strings of options to request page headings in the printer program.
  104. If nil, we run `lpr-page-header-program' to make page headings
  105. and print the result."
  106. :type '(repeat (string :tag "Argument"))
  107. :group 'lpr)
  108. (defcustom print-region-function nil
  109. "Function to call to print the region on a printer.
  110. See definition of `print-region-1' for calling conventions."
  111. :type '(choice (const nil) function)
  112. :group 'lpr)
  113. (defcustom lpr-page-header-program "pr"
  114. "Name of program for adding page headers to a file."
  115. :type 'string
  116. :group 'lpr)
  117. ;; Berkeley systems support -F, and GNU pr supports both -f and -F,
  118. ;; So it looks like -F is a better default.
  119. (defcustom lpr-page-header-switches '("-h" "%s" "-F")
  120. "List of strings to use as options for the page-header-generating program.
  121. If `%s' appears in any of the strings, it is substituted by the page title.
  122. Note that for correct quoting, `%s' should normally be a separate element.
  123. The variable `lpr-page-header-program' specifies the program to use."
  124. :type '(repeat string)
  125. :group 'lpr)
  126. ;;;###autoload
  127. (defun lpr-buffer ()
  128. "Print buffer contents without pagination or page headers.
  129. See the variables `lpr-switches' and `lpr-command'
  130. for customization of the printer command."
  131. (interactive
  132. (unless (y-or-n-p "Send current buffer to default printer? ")
  133. (error "Cancelled")))
  134. (print-region-1 (point-min) (point-max) lpr-switches nil))
  135. ;;;###autoload
  136. (defun print-buffer ()
  137. "Paginate and print buffer contents.
  138. The variable `lpr-headers-switches' controls how to paginate.
  139. If it is nil (the default), we run the `pr' program (or whatever program
  140. `lpr-page-header-program' specifies) to paginate.
  141. `lpr-page-header-switches' specifies the switches for that program.
  142. Otherwise, the switches in `lpr-headers-switches' are used
  143. in the print command itself; we expect them to request pagination.
  144. See the variables `lpr-switches' and `lpr-command'
  145. for further customization of the printer command."
  146. (interactive
  147. (unless (y-or-n-p "Send current buffer to default printer? ")
  148. (error "Cancelled")))
  149. (print-region-1 (point-min) (point-max) lpr-switches t))
  150. ;;;###autoload
  151. (defun lpr-region (start end)
  152. "Print region contents without pagination or page headers.
  153. See the variables `lpr-switches' and `lpr-command'
  154. for customization of the printer command."
  155. (interactive
  156. (if (y-or-n-p "Send selected text to default printer? ")
  157. (list (region-beginning) (region-end))
  158. (error "Cancelled")))
  159. (print-region-1 start end lpr-switches nil))
  160. ;;;###autoload
  161. (defun print-region (start end)
  162. "Paginate and print the region contents.
  163. The variable `lpr-headers-switches' controls how to paginate.
  164. If it is nil (the default), we run the `pr' program (or whatever program
  165. `lpr-page-header-program' specifies) to paginate.
  166. `lpr-page-header-switches' specifies the switches for that program.
  167. Otherwise, the switches in `lpr-headers-switches' are used
  168. in the print command itself; we expect them to request pagination.
  169. See the variables `lpr-switches' and `lpr-command'
  170. for further customization of the printer command."
  171. (interactive
  172. (if (y-or-n-p "Send selected text to default printer? ")
  173. (list (region-beginning) (region-end))
  174. (error "Cancelled")))
  175. (print-region-1 start end lpr-switches t))
  176. (defun print-region-1 (start end switches page-headers)
  177. ;; On some MIPS system, having a space in the job name
  178. ;; crashes the printer demon. But using dashes looks ugly
  179. ;; and it seems to annoying to do for that MIPS system.
  180. (let ((name (concat (buffer-name) " Emacs buffer"))
  181. (title (concat (buffer-name) " Emacs buffer"))
  182. ;; Make pipes use the same coding system as
  183. ;; writing the buffer to a file would.
  184. (coding-system-for-write (or coding-system-for-write
  185. buffer-file-coding-system))
  186. (coding-system-for-read (or coding-system-for-read
  187. buffer-file-coding-system))
  188. (width tab-width)
  189. nswitches
  190. switch-string)
  191. (save-excursion
  192. (and page-headers lpr-headers-switches
  193. ;; It's possible to use an lpr option to get page headers.
  194. (setq switches (append (if (stringp lpr-headers-switches)
  195. (list lpr-headers-switches)
  196. lpr-headers-switches)
  197. switches)))
  198. (setq nswitches (lpr-flatten-list
  199. (mapcar 'lpr-eval-switch ; Dynamic evaluation
  200. switches))
  201. switch-string (if switches
  202. (concat " with options "
  203. (mapconcat 'identity switches " "))
  204. ""))
  205. (message "Spooling%s..." switch-string)
  206. (if (/= tab-width 8)
  207. (let ((new-coords (print-region-new-buffer start end)))
  208. (setq start (car new-coords)
  209. end (cdr new-coords)
  210. tab-width width)
  211. (save-excursion
  212. (goto-char end)
  213. (setq end (point-marker)))
  214. (untabify (point-min) (point-max))))
  215. (if page-headers
  216. (if lpr-headers-switches
  217. ;; We handled this above by modifying SWITCHES.
  218. nil
  219. ;; Run a separate program to get page headers.
  220. (let ((new-coords (print-region-new-buffer start end)))
  221. (apply 'call-process-region (car new-coords) (cdr new-coords)
  222. lpr-page-header-program t t nil
  223. (mapcar (lambda (e) (format e title))
  224. lpr-page-header-switches)))
  225. (setq start (point-min)
  226. end (point-max))))
  227. (let ((buf (current-buffer)))
  228. (with-temp-buffer
  229. (let ((tempbuf (current-buffer)))
  230. (with-current-buffer buf
  231. (apply (or print-region-function 'call-process-region)
  232. (nconc (list start end lpr-command
  233. nil tempbuf nil)
  234. (and lpr-add-switches
  235. (list "-J" name))
  236. ;; These belong in pr if we are using that.
  237. (and lpr-add-switches lpr-headers-switches
  238. (list "-T" title))
  239. (and (stringp printer-name)
  240. (list (concat lpr-printer-switch
  241. printer-name)))
  242. nswitches))))
  243. (if (markerp end)
  244. (set-marker end nil))
  245. (message "Spooling%s...done%s%s" switch-string
  246. (case (count-lines (point-min) (point-max))
  247. (0 "")
  248. (1 ": ")
  249. (t ":\n"))
  250. (buffer-string)))))))
  251. ;; This function copies the text between start and end
  252. ;; into a new buffer, makes that buffer current.
  253. ;; It returns the new range to print from the new current buffer
  254. ;; as (START . END).
  255. (defun print-region-new-buffer (ostart oend)
  256. (if (string= (buffer-name) " *spool temp*")
  257. (cons ostart oend)
  258. (let ((oldbuf (current-buffer)))
  259. (set-buffer (get-buffer-create " *spool temp*"))
  260. (widen)
  261. (erase-buffer)
  262. (insert-buffer-substring oldbuf ostart oend)
  263. (cons (point-min) (point-max)))))
  264. (defun printify-region (begin end)
  265. "Replace nonprinting characters in region with printable representations.
  266. The printable representations use ^ (for ASCII control characters) or hex.
  267. The characters tab, linefeed, space, return and formfeed are not affected."
  268. (interactive "r")
  269. (save-excursion
  270. (save-restriction
  271. (narrow-to-region begin end)
  272. (goto-char (point-min))
  273. (let (c)
  274. (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" nil t)
  275. (setq c (preceding-char))
  276. (delete-char -1)
  277. (insert (if (< c ?\s)
  278. (format "\\^%c" (+ c ?@))
  279. (format "\\%02x" c))))))))
  280. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  281. ;; Functions hacked from `ps-print' package.
  282. ;; Dynamic evaluation
  283. (defun lpr-eval-switch (arg)
  284. (cond ((stringp arg) arg)
  285. ((functionp arg) (apply arg nil))
  286. ((symbolp arg) (symbol-value arg))
  287. ((consp arg) (apply (car arg) (cdr arg)))
  288. (t nil)))
  289. ;; `lpr-flatten-list' is defined here (copied from "message.el" and
  290. ;; enhanced to handle dotted pairs as well) until we can get some
  291. ;; sensible autoloads, or `flatten-list' gets put somewhere decent.
  292. ;; (lpr-flatten-list '((a . b) c (d . e) (f g h) i . j))
  293. ;; => (a b c d e f g h i j)
  294. (defun lpr-flatten-list (&rest list)
  295. (lpr-flatten-list-1 list))
  296. (defun lpr-flatten-list-1 (list)
  297. (cond
  298. ((null list) (list))
  299. ((consp list)
  300. (append (lpr-flatten-list-1 (car list))
  301. (lpr-flatten-list-1 (cdr list))))
  302. (t (list list))))
  303. (provide 'lpr)
  304. ;;; lpr.el ends here