Created
December 2, 2021 01:03
-
-
Save jsilve24/2708ad0c02cd9d9db23840bd7838fc19 to your computer and use it in GitHub Desktop.
Simple print helper, its a thin wrapper around pdf-tools' pdf-misc-print-document that lets you select a printer registered with CUPS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; see cups help page for lots on how to print with lpr and lp | |
;; the below functions assume these options are set | |
(setq pdf-misc-print-program-executable "/usr/bin/lpr" | |
pdf-misc-print-program-args (list "-o sides=two-sided-long-edge")) | |
;;; print-helper -- not enough to make a stand-alone package | |
(require 'dash) | |
;;;###autoload | |
(defun ph--get-list-of-priters () | |
"Return list of printer with default in position 1." | |
(let* ((printers (shell-command-to-string "lpstat -p -d")) | |
(printers (split-string printers "\n")) | |
(default (-filter (lambda (x) (string-match "default destination" x)) | |
printers)) | |
(printers (-filter (lambda (x) (string-match "printer " x)) | |
printers)) | |
(default (last (split-string (car default) " "))) | |
(printers (-map (lambda (x) (nth 1 (split-string x " "))) | |
printers)) | |
;; move to front of list | |
(printers (cons (car default) (remove (car default) printers)))) | |
printers)) | |
;;;###autoload | |
(defun ph-pdf-misc-print-document (&optional arg) | |
"Wrapper around pdf-misc-print-document that allows you to | |
select printer (pulling priter list and default from lpstat -p | |
-d command) using completing-read. Also doesn't ask what | |
printer to use instead assumes that | |
`pdf-misc-print-program-executable' is already set." | |
(interactive "P") | |
(if arg | |
(let ((printer (completing-read "Choose a printer:" (ph--get-list-of-priters))) | |
(pdf-misc-print-program-args | |
;; should this not have a space after P? | |
(cons (concat "-P " printer) pdf-misc-print-program-args))) | |
(pdf-misc-print-document | |
(pdf-view-buffer-file-name) | |
;; dont' prompt for program to print with (nil) | |
nil)) | |
;; no prefix -- just use default printer | |
(pdf-misc-print-document | |
(pdf-view-buffer-file-name) | |
;; dont' prompt for program to print with (nil) | |
nil))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment