views:

522

answers:

2

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?)

+2  A: 

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.

Joe Casadonte
+1  A: 

add the following line to your emacs init file

(setq printer-name "//domain/printer-name")
Oleg Pavliv
This didn't have any effect, either. Maybe my printer settings are strange...
Christian Berg