GC3: Grid Computing Competence Center

Since July 1st, 2014, GC3 merged into S3IT.
This web site is only kept for historical reasons and may be out-of-date.
Visit the S3IT website for more up-to-date information.

Blog index

GC3 graudates into S3IT
Posted early Tuesday morning, July 1st, 2014
How to create a module that also load a virtualenvironment
Posted mid-morning Friday, March 7th, 2014
Openstack workshop at GC3
Posted mid-morning Saturday, February 22nd, 2014
Moving LVM volumes used by a Cinder storage
Posted Friday evening, February 21st, 2014
How to configure swift glusterfs
Posted Monday night, February 10th, 2014
Fixing LDAP Authentication over TLS/SSL
Posted Monday night, January 6th, 2014
Linker command-line options for Intel MKL
Posted late Saturday evening, January 4th, 2014
A virtue of lazyness
Posted Saturday afternoon, December 21st, 2013
(Almost) readable CFEngine logs
Posted Thursday afternoon, December 19th, 2013
CFEngine error: ExpandAndMapIteratorsFromScalar called with invalid strlen
Posted at lunch time on Wednesday, December 11th, 2013
'Martian source' log messages and the default IP route
Posted Monday afternoon, November 25th, 2013
GC3 takes over maintenance of the Schroedinger cluster
Posted mid-morning Monday, November 4th, 2013
Grid Engine: how to find the set of nodes that ran a job (after it's finished)
Posted terribly early Wednesday morning, October 30th, 2013
Python2 vs Python3
Posted Friday afternoon, September 13th, 2013
GC3Pie 2.1.1 released
Posted at teatime on Friday, September 6th, 2013
Happy SysAdmin day!
Posted early Friday morning, July 26th, 2013
Object-oriented Python training
Posted at lunch time on Thursday, July 25th, 2013
Elasticluster 1.0.0 released
Posted late Thursday evening, July 18th, 2013
Short Autotools tutorial
Posted mid-morning Friday, July 5th, 2013
Patch Emacs' PostScript printing
Posted late Tuesday afternoon, June 11th, 2013
Slides of the Object-oriented Python course now available!
Posted late Tuesday afternoon, June 11th, 2013
Compile an Objective-C application on Ubuntu (Hobbes instance)
Posted late Friday afternoon, May 31st, 2013
Automated deployment of CFEngine keys
Posted Thursday night, May 30th, 2013
blog/Resize_an_image
Posted late Tuesday afternoon, May 14th, 2013
Join us at the Compute Cloud Experience Workshop!
Posted early Monday morning, April 29th, 2013
GC3 Beamer theme released
Posted mid-morning Friday, April 5th, 2013
VM-MAD at the International Supercompting Conference 2013
Posted late Tuesday morning, March 26th, 2013
The GC3 is on GitHub
Posted late Monday morning, March 18th, 2013
How to enable search in IkiWiki
Posted Friday afternoon, March 15th, 2013
GC3Pie Training
Posted Thursday night, March 7th, 2013
Object-oriented Python training
Posted Thursday afternoon, March 7th, 2013
Advance Reservations in GridEngine
Posted mid-morning Thursday, March 7th, 2013
GridEngine accounting queries with PostgreSQL
Posted Wednesday night, March 6th, 2013
Floating IPs not available on Hobbes
Posted Tuesday afternoon, February 26th, 2013
Notes on SWIFT
Posted early Tuesday morning, February 12th, 2013
An online Python code quality analyzer
Posted late Saturday morning, February 9th, 2013
Seminar on cloud infrastructure
Posted Sunday night, February 3rd, 2013
GC3 announce its cloud infrastructure Hobbes
Posted at lunch time on Wednesday, January 30th, 2013
GC3Pie 2.0.2 released
Posted Monday afternoon, January 28th, 2013
Continuous Integration with Jenkins
Posted late Saturday morning, January 26th, 2013
On the importance of testing in a clean environment
Posted early Monday morning, January 21st, 2013
Weirdness with ImageMagick's `convert`
Posted Tuesday afternoon, January 15th, 2013
boto vs libcloud
Posted Tuesday afternoon, January 15th, 2013
Resolve timeout problem when starting many instances at once
Posted late Monday morning, January 7th, 2013
Proceedings of the EGI Community Forum 2012 published
Posted Monday afternoon, December 17th, 2012
SGE Workaround Installation
Posted at noon on Tuesday, December 4th, 2012
How to pass an argument of list type to a CFEngine3 bundle
Posted early Thursday morning, November 22nd, 2012
GC3 at the 'Clouds for Future Internet' workshop
Posted early Wednesday morning, November 21st, 2012
GC3 attends European Commission Cloud Expert Group
Posted early Monday morning, October 29th, 2012
SwiNG - SDCD2012 event
Posted mid-morning Monday, October 22nd, 2012
Large Scale Computing Infrastructures class starts tomorrow!
Posted Tuesday afternoon, September 25th, 2012
From bare metal to cloud at GC3
Posted early Monday morning, September 24th, 2012
GC3 at the EGI Technical Forum 2012
Posted late Thursday evening, September 20th, 2012
Training on GC3Pie and Python
Posted Friday evening, September 7th, 2012
GC3Pie used for research in Computational Quantum Chemistry
Posted Thursday afternoon, September 6th, 2012
``What's so great about MPI or Boost.MPI?''
Posted early Thursday morning, September 6th, 2012
blog/How to generate UML diagram with `pyreverse`
Posted early Thursday morning, August 23rd, 2012
Git's `rebase` command
Posted early Friday morning, June 15th, 2012
AppPot 0.27 released!
Posted mid-morning Thursday, June 14th, 2012
Urban computing - connecting to your server using `mosh`
Posted early Wednesday morning, June 6th, 2012
Whitespace cleanup with Emacs
Posted at lunch time on Tuesday, June 5th, 2012
Translate pages on this site
Posted late Thursday afternoon, May 31st, 2012
Scientific paper citing GC3Pie
Posted at teatime on Wednesday, May 30th, 2012
GC3 attends Nordugrid 2012 conference
Posted mid-morning Wednesday, May 30th, 2012
How the front page image was made
Posted Wednesday evening, May 16th, 2012
GC3 blog launched!
Posted Tuesday evening, May 15th, 2012
New GC3 Wiki now online!
Posted Tuesday afternoon, May 15th, 2012
AppPot paper on arXiv
Posted Tuesday afternoon, May 15th, 2012
GC3 at the EGI Technical Forum 2011
Posted Tuesday afternoon, May 15th, 2012

More on topic...

GNU Emacs has had "PostScript print" functions in the File menu since ages. However, like many things in Emacs, they have to work in a wide variety of environments, for instance TTYs and legacy printing systems. Which, unfortunately, means that we get no "print preview" and no dialog box nor other easy way to select a printer.1

Well, until of course a bit of Emacs LISP comes to the rescue :-).

Idea: redefine the PS printing functions

Documentation for the "PostScript print buffer" function shows that it takes a single argument FILENAME: if this is non-nil then ps-print-buffer saves the PS output to that file instead of sending it to the printer.

So we could just redefine the "PostScript print buffer" function to:

  1. Always save the PS output to a file,
  2. Invoke Evince on that file, which gives us a nice preview of what we are going to print, and a GTK print dialog to select the output printer and other options.

Now show me the code

ELisp includes a defadvice function that lets you wrap arbitrary code around an already-defined function in a simple and clean way2.

defadvice allows code to be run before or after the normal invocation of a function, or around it, namely, the added code is responsible for calling the advised function.3

The syntax for defining an around-advice is the following:

    ;; redefine `ps-print-buffer' to save to file and invoke the viewer on it
    (defadvice ps-print-buffer (around my/ps-print-buffer activate)

defadvice takes the function name to be advised as first argument, then a list of options:

  • when to run this piece of advice (before, after, around);
  • a unique name for the piece of advice (here, my/ps-print-buffer);
  • a list of optional flags: here we use activate to make this piece of advice immediately available (otherwise, the ad-activate function should be used to activate it).

Other options allow to re-define the list of argument taken by the function, or to set the priority of the piece of advice being defined.

Next comes an ELisp form defining the piece of advice. Since this is an around advice, we are effectively replacing the function definition. The actual call to the advised function is done in the ad-do-it line (which is expanded by defadvice with the original function definition):

      (interactive "P")
      (if filename
        ;; do std Emacs print-to-file
        ad-do-it
        ;; else print to temp file and display that
        (let ((outfile (make-temp-file "emacs-ps-print-" nil ".ps")))
          (setq filename outfile)
          ad-do-it
          (start-process "printing" " *Printing*" "evince" outfile))))

Note that there are two occurrences of ad-do-it in the code; one can call ad-do-it many times over, or embed it in a loop, even.

Also note that ad-do-it does not contain any reference to the argument list passed to the function, so how do we modify the actual parameters? Since our advice code is called in a body equivalent to the function being advised, we can just setq filename to alter the value of the filename argument as seen by the original ps-print-buffer function.

Finally, note that we need to preserve the interactive declaration of the original function, otherwise we remove its possibility to be used as an interactive command, i.e., from keystrokes and menus.

Patching many functions

The File menu on my Emacs 24 lists 4 variants of "PostScript" printing: "PostScript print buffer", "PostScript print region" and the "(B+W)" variants. These correspond to ELisp functions ps-print-buffer-with-faces, ps-print-region-with-faces, and two analogous functions with the -with-faces suffix omitted. In addition, the -buffer and -region variants have different interactive declarations so a slight modification of the code is needed for the two cases.

A macro is needed to save typing and keep code compact:

    (defmacro my/advice-ps-print-functions (fname argspec)
      "Redefine function FNAME to save the PS output to a file and invoke a viewer on it."
      (let ((ad-name    (intern (concat "my/" (symbol-name fname))))
            (fileprefix (concat "emacs-" (symbol-name fname) "-")))
        `(defadvice ,fname (around ,ad-name activate)
           (interactive ,argspec)
           (if filename
             ;; std Emacs print-to-file
             ad-do-it
             ;; print to temp file and use PDF viewer to print
             (let ((ps-outfile (make-temp-file fileprefix nil ".ps"))
                   (pdf-outfile (make-temp-file fileprefix nil ".pdf")))
               ;; generate PS printout
               (setq filename ps-outfile)
               ad-do-it
               ;; convert it to PDF
               (shell-command (concat "ps2pdf" " " ps-outfile " " pdf-outfile))
               ;; view it
               (start-process "printing" " *Printing*" "evince" pdf-outfile))))))

    (my/advice-ps-print-functions ps-print-buffer "P")
    (my/advice-ps-print-functions ps-print-buffer-with-faces "P")
    (my/advice-ps-print-functions ps-print-region "rP")
    (my/advice-ps-print-functions ps-print-region-with-faces "rP")

The macro body is just a slight modification of the code previously seen: the entire defadvice form is quasi-quoted with the following substitutions happening thanks to the ,-expansion:

  • fname, the function symbol being redefined;
  • ad-name, the name of the corresponding piece of advice (my/ + function name);
  • argspec, the argument list for the interactive declaration;
  • fileprefix, a prefix for the temporary file to be used, which depends on the function being advised.

Note also that the printout is converted to PDF before calling Evince (my Evince version 3.6.1 has troubles showing PostScript output).

As a final touch, we can also configure Emacs so that all printing goes through a PostScript printer:

    ;; all printing goes through PostScript
    (defalias 'print-buffer #'ps-print-buffer)

The easy modification of the code to allow customizing the PS to PDF converter and the PDF viewer is left as an exercise to the willing reader.

--RM


  1. You can set the default printer in the ELisp variables printer-name and ps-printer-name (look them up with C-h v), which work rather well if you ever only use one and only one printer... ↩

  2. defadvice also allows independent and concurrent modifications of the same function without one change overwriting the other, and has a priority system to determine the order in which the different "advises" should be run. "Advising" in this sense has a long LISP tradition, and is part of the more general Aspects-oriented programming. ↩

  3. This is similar to what Python decorators do. ↩

top