Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print from Emacs on Win32?

I'm running Emacs 23.0.60.1, downloaded from here, on Windows XP, with a network printer configured as the default printer.

How do I setup Emacs to easily print buffer contents?

The documentation of the patched Emacs version for Win32 mentions "quick and easy" printing, but the "Quick Print" menu entry does not appear and the regular entries ("Print Buffer", "Postscript Print Buffer") don't seem to do anything.

EDIT:
I'm having the same issue with the official windows build of Emacs 22.3. Therefore setup/troubleshooting instructions for any version would be appreciated.

EDIT2:
I went with the PrintFile solution presented by Joe Casadonte below, which works nicely. I'd still be interested in any ideas why the "proper" way is not working, though.

(By the way, is this an appropriate SO question, being only marginally programming related?)

like image 566
Christian Berg Avatar asked Feb 20 '09 14:02

Christian Berg


3 Answers

I will describe everything for Windows 7 with a common USB Printer. Adapt the process where needed to your version. Network can usually be accessed the same way. Just use //NetworkComputerName/SharedPrinterName instead of //MyComputer/MyPrinter and skip steps 1.-6..

  1. Go to Start -> Control Panel -> Hardware and Sound -> Devices and Printers
  2. Rightclick your printer select "Printer Properties"
  3. Go to "Sharing" and check "Share this printer" and "Render print jobs on client computers"
  4. Enter Share name: MyPrinter or something that you can remember and has no spaces.
  5. Click OK to save your changes.
  6. Go to Start -> Computer to check the name of your computer in the left lower corner (e.g MyComputer)
  7. In Emacs evaluate (setq printer-name "//MyComputer/MyPrinter") or put it in your .emacs.el file
  8. Done. You can print your files with M-x print-buffer
like image 78
ayckoster Avatar answered Nov 20 '22 14:11

ayckoster


It's not the "proper" way, but I've been doing it this way for years and it works wonderfully. I use PrintFile, a freeware print program (which can also be used stand-alone). Then I have this in my .emacs:

(defun joc-make-fname-from-buffer-name (buffer-name-in)
  "Returns a valid filename from a given buffer name"
  (interactive "b")
  (save-match-data
    (let* ((start (string-match "[^ \*]" buffer-name-in))
           (end (string-match "[ \*]*$" buffer-name-in (match-end 0)))
           (rc (substring buffer-name-in start end)))
      ;; remove some special characters
      (while (string-match "[:]+" rc)
        (setq rc (replace-match "_" t t rc)))
      rc)))

(when is-win32
    (defun joc-print-buffer-or-region (prefix)
      "Prints buffer or region via PrintFile32.  If a prefix arg is set (via C-u) then
       the current region is printed, otherwise the current buffer is printed."

      (interactive "P")

      ;; ----- set the print directory, fname and args -----
      (let* ((print-dir (expand-file-name "~/emacs/print"))
             (print-fname (joc-make-fname-from-buffer-name (buffer-name)))
             (print-fullpath (concat print-dir "/" print-fname))
             (print-args "/delete")
             ;; ----- set rstart and rend to the current region -----
             (rstart (point-min)) (rend (point-max)))

        ;; ----- if prefix given, set them to region -----
        (if (and prefix)
            (if (and (point) (mark) (/= (point) (mark)))
                (progn (setq rstart (min (point) (mark)))
                       (setq rend (max (point) (mark))))
              (error "No region defined")))

        ;; ----- make the directory -----
        (if (not (file-directory-p print-dir))
            (make-directory print-dir))

        ;; ----- write buffer/region to a temp file, print it, delete directory -----
        (write-region rstart rend print-fullpath)
        (call-process "prfile32" nil t nil print-args print-fullpath)
        (delete-directory print-dir))))

I haven't looked at it in years cause it just works, so I'm sure it could be improved.

like image 38
Joe Casadonte Avatar answered Nov 20 '22 14:11

Joe Casadonte


add the following line to your emacs init file

(setq printer-name "//domain/printer-name")
like image 1
Oleg Pavliv Avatar answered Nov 20 '22 15:11

Oleg Pavliv