|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
;; This file:
;; http://angg.twu.net/elisp/rcd-paps.el.html
;; http://angg.twu.net/elisp/rcd-paps.el
;; (find-angg "elisp/rcd-paps.el")
;; Author: Jean Louis <bugs@gnu.support>
;; Comments: Eduardo Ochs <eduardoochs@gmail.com>
;; Date: 2022dec18
;;
;; To test this, run
;; (eval-buffer)
;; or: (load (buffer-file-name))
;; and then: (rcd-paps-buffer-to-pdf-view)
;; with your favorite variant of `C-e C-x C-e'.
;; When I run this in my default system I get this error in evince:
;;
;; Unable to open document “file:///home/edrx/tmp/Sunday-December-18-2022-19-08-23.pdf”.
;; File type empty document (application/x-zerosize) is not supported.
;;
;; and when I run it in an emacs -Q I get this lisp error:
;;
;; Debugger entered--Lisp error: (wrong-type-argument integer-or-marker-p nil)
;; call-process-region(nil nil "paps" t (t nil) nil "--format" "pdf" "--landscape"
;; "--title" "rcd-paps.el" "--header")
;; apply(call-process-region nil nil "paps" t (t nil) nil ("--format" "pdf" "--landscape"
;; "--title" "rcd-paps.el" "--header"))
'("This is a test block that tests this in an emacs -Q
• (eepitch-shell)
• (eepitch-kill)
• (eepitch-shell)
emacs -fg bisque -bg black -Q -l /home/edrx/elisp/rcd-paps.el &
--")
;; These functions were taken from:
;; https://lists.gnu.org/archive/html/help-gnu-emacs/2022-12/msg00535.html
(defun rcd-command-output-from-input (program input &rest args)
"Return output string from PROGRAM with given INPUT string and optional ARGS."
(let* ((output (with-temp-buffer
(insert input)
(apply #'call-process-region nil nil program t '(t nil) nil args)
(buffer-string))))
output))
(defun rcd-paps-process (text &optional format title &rest args)
(let* ((format (or format "pdf"))
(title (or title (buffer-name)))
(length (length title))
(max 45)
(title (if (> length max) (substring title 0 max) title)))
(apply 'rcd-command-output-from-input
"paps" text
"--format" format
"--landscape"
"--title" title
"--header" args)))
(defun string-to-file-force (string file)
"Prints string into file, matters not if file exists. Return FILE as file name."
(with-temp-file file
(insert string))
file)
(defun rcd-paps-text-to-pdf (text &optional file-name title)
(let* ((output (rcd-paps-process text "pdf" title))
(file-name
(or file-name
(concat (file-name-as-directory (getenv "HOME"))
(read-string "File name without .pdf extension: ")
".pdf"))))
(string-to-file-force output file-name)))
(defun rcd-paps-buffer-to-pdf-view ()
(interactive)
(let ((output (rcd-paps-process-buffer))
(file-name (rcd-temp-file-name nil "pdf")))
(sleep-for 1)
(start-process "evince" nil "evince"
(string-to-file-force output file-name))))
;; These other functions were taken from:
;; https://lists.gnu.org/archive/html/help-gnu-emacs/2022-12/msg00575.html
(defun rcd-paps-process-buffer (&rest args)
(let ((text (buffer-substring-no-properties (point-min) (point-max))))
(apply 'rcd-paps-process text args)))
(defcustom rcd-temp-file-directory "~/tmp/"
"Temporary directory for other temporary files."
:group 'rcd
:type 'string)
(defun rcd-temp-file-name (&optional file-name extension)
"Return temporary file name."
(concat (file-truename (file-name-as-directory rcd-temp-file-directory))
(or file-name (format-time-string "%A-%B-%d-%Y-%H-%M-%S"))
"."
(or extension "txt")))
;; Local Variables:
;; coding: utf-8-unix
;; End: