[ESS] Printing *R* help pages within Emacs using doc-view

Stephen Eglen S.J.Eglen at damtp.cam.ac.uk
Thu Jul 26 11:13:31 CEST 2012

Dear all,

Here's some proof of concept code to solve the following problem for
me.  If you'd like to see it in ESS, let me know, and if I get
sufficient feedback, I'll tidy it up and put it in.

Unix only for now.

If you are viewing a R help page in ESS, e.g. "rnorm", then if you hit
the P key, a few seconds later, you should see /tmp/rnorm.pdf opened,
which should render nicely thanks to doc-view.  Then within doc-view,
hit ! to send the PDF to a printer.  This is the only line you'll need
to configure/edit:

(setq sje-doc-view-pdf-lpr-command "lpr -Pg0 %s")  ;; docstring below

You'll need a unix command to print a pdf; our local printers can print
a pdf using a command like:

$ lpr -Pg0 /tmp/rnorm.pdf

If your printer doesn't accept pdf directly, you'll need to change it to
something else.  "g0" is the name of my local printer.  It won't print
automatically, when you hit ! you get the chance to edit the command

I've put the pdf in /tmp as I don't like them accummulating in the *R*
current working directory.

Feedback welcome!


p.s. the code for printing pdf from doc-view is independent of ESS.

(defun ess-r-help-pdf ()
      ((cmd (format "help(%s, help_type='pdf')\n" ess-help-object))
       (pdf (format "%s.pdf" ess-help-object))
       (newpdf (format "/tmp/%s.pdf" ess-help-object))
    (ess-command cmd)
    (rename-file pdf newpdf 'overwrite)
    (find-file newpdf)))

(add-hook 'ess-help-mode-hook 
 	  '(lambda ()   (local-set-key "P" 'ess-r-help-pdf)))

;;; Now set up doc-view for printing.
(defun sje-doc-view-pdf-lpr ()
  "Send current PDF to the default printer.
The default printer command is set by `sje-doc-view-pdf-lpr-command'."
  (let ((cmd (format sje-doc-view-pdf-lpr-command (buffer-file-name))))
     (read-string (format "Print command (default %s): " cmd) nil nil cmd))))

;; g0 is my default printer.  
(defvar sje-doc-view-pdf-lpr-command "lpr -Pg0 %s"
  "Print command to use to send a pdf to the printer.  %s will
get expanded to the name of the file associated with the current
(add-hook 'doc-view-mode-hook 
 	  '(lambda ()   (local-set-key "!" 'sje-doc-view-pdf-lpr)))

More information about the ESS-help mailing list