Emacs: printing with line numbers

Does anyone know how to print with line numbers of code in a field? I can display the line number, cannot have this in the printout. Thanks!

+4
source share
3 answers

You can add line numbers with temporary overlays and convert the buffer to HTML using the htmlize package, after which you can save the HTML and print using lpr or a browser.

 (defun htmlize-with-line-numbers () (interactive) (goto-char (point-min)) (let ((n 1)) (while (not (eobp)) (htmlize-make-tmp-overlay (point) (point) `(before-string ,(format "%4d " n))) (setq n (1+ n)) (forward-line 1))) (switch-to-buffer (htmlize-buffer))) 

This will require the latest version of htmlize .

+3
source

A simple but hacky way, of course, would be to temporarily paste line numbers directly into the buffer

 C-< CM-% ^ RET \,(1+ \#) SPC RET 

then print it

 Mx print-buffer 

and then cancel the line numbers again:

 C-/ Cu C-SPC 

The result is not very beautiful, but useful. There are three main problems:

  • You are making changes to the buffer. In particular, this means that the buffer should not be read-only.
  • line numbers are left-justified, which means that you get different indentation depending on the number of digits of the line number.
  • your main mode will turn off line numbers and you will lose syntax highlighting. If you are printing on a black and white printer, this is not a problem.

You can fix the second point using a more complex replacement string:

 \,(format "%4d " (1+ \#)) 

but then you should know what is the maximum line number, so you can specify the correct number of digits between % and d . Of course, you can just quickly jump to the end of the buffer to check the maximum line number. But more importantly, it becomes painful to print all this every time you want to print line numbers.

+3
source

Sorry for the decision on such an old post. I used ps-print-buffer , not print-buffer , as the results are much nicer. Anyway, for some reason it is not documented in the manual, but if you look at the source for ps-print.el , you will find the ps-line-number variable, which you can set non-nil to include line numbers.

 Mx set-variable RET ps-line-number RET t 

This should install it temporarily so that you can print. You can install it forever in init.el

You can also print using the Mx pr-interface command, which calls the buffer of all print options. A.

+2
source

All Articles