From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: "Stefan Monnier" Newsgroups: gmane.emacs.devel Subject: Re: Windows port: using Windows' default printer Date: 15 Sep 2003 10:52:23 -0400 Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: References: <3E54D5AA.4000805@runestig.com> <3E55CCC0.5050408@runestig.com> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1063637640 23496 80.91.224.253 (15 Sep 2003 14:54:00 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 15 Sep 2003 14:54:00 +0000 (UTC) Cc: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Mon Sep 15 16:53:57 2003 Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 19yujl-0006hw-00 for ; Mon, 15 Sep 2003 16:53:57 +0200 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.35 #1 (Debian)) id 19yumR-0001L1-00 for ; Mon, 15 Sep 2003 16:56:43 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.22) id 19yuji-0008Q4-6P for emacs-devel@quimby.gnus.org; Mon, 15 Sep 2003 10:53:54 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.22) id 19yuia-0008FA-Lr for emacs-devel@gnu.org; Mon, 15 Sep 2003 10:52:44 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.22) id 19yuiW-0008Ej-Sq for emacs-devel@gnu.org; Mon, 15 Sep 2003 10:52:43 -0400 Original-Received: from [132.204.24.67] (helo=mercure.iro.umontreal.ca) by monty-python.gnu.org with esmtp (Exim 4.22) id 19yuiM-0008E6-BY for emacs-devel@gnu.org; Mon, 15 Sep 2003 10:52:30 -0400 Original-Received: from vor.iro.umontreal.ca (vor.iro.umontreal.ca [132.204.24.42]) by mercure.iro.umontreal.ca (8.12.9/8.12.9) with ESMTP id h8FEqOHD002935; Mon, 15 Sep 2003 10:52:24 -0400 Original-Received: by vor.iro.umontreal.ca (Postfix, from userid 20848) id E75FF3C526; Mon, 15 Sep 2003 10:52:23 -0400 (EDT) Original-To: "Peter 'Luna' Runestig" In-Reply-To: Original-Lines: 290 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50 X-MailScanner-DIRO: Found to be clean X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: Emacs development discussions. List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:16402 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:16402 > This is a reworked version of the `w32-default-printer-device' variable > patch I posted in March 2002. After a private mail exchange with Jason > Rumney, I followed his ideas, and instead implemented a > `default-printer-name' function, that is to be used if `printer-name' > is nil. I quote Jason's description of the idea: I think the C part of the code is fine and the use of `printer-name' is fine as well, but I wish that the use of default-printer-name could be confined to a single spot (in Unix, it's in `lpr' so Emacs doesn't know about it). We probably need to create a function that either calls `lpr' or calls windows' printing code (using default-printer-name if necessary) and then have both lpr.el and ps-print.el use this function. In the mean time, your patch looks like a step in the right direction. Stefan >>>>>>> > [...] Rather, you should expose a function to lisp (using the DEFUN C > macro) which returns the default printer name, then modify the code that > uses printer-name (and ps-printer-name) to call this function if > printer-name is nil and the function is available. > If possible, avoid w32- specific variables, function names and tests, > then if MacOS or other systems need to do a similar thing, they just > need to implement a function of the same name. This is much cleaner than > having branches all over the place to handle specific platforms. > An example of the Lisp required to do what I'm thinking of would be: > (let ((printer-name (or printer-name > (and (fboundp 'default-printer-name) > (default-printer-name))))) > ;; Code that uses printer-name here > ) > <<<<<< > diff -bur emacs-cvs-20030904.orig/lisp/ChangeLog emacs-cvs-20030904/lisp/ChangeLog > --- emacs-cvs-20030904.orig/lisp/ChangeLog 2003-09-03 10:14:52.000000000 +0200 > +++ emacs-cvs-20030904/lisp/ChangeLog 2003-09-08 09:01:53.000000000 +0200 > @@ -1,3 +1,8 @@ > +2003-09-05 Peter Runestig > + > + * dos-w32.el, lpr.el, ps-print.el, play/handwrite.el: Added support for > + the `default-printer-name' function. > + > 2003-09-02 Jason Rumney > * international/titdic-cnv.el (tsang-quick-converter): Fix broken > diff -bur emacs-cvs-20030904.orig/lisp/dos-w32.el emacs-cvs-20030904/lisp/dos-w32.el > --- emacs-cvs-20030904.orig/lisp/dos-w32.el 2003-09-03 10:14:55.000000000 +0200 > +++ emacs-cvs-20030904/lisp/dos-w32.el 2003-09-05 22:50:41.000000000 +0200 > @@ -378,7 +378,9 @@ > (printer (or (and (boundp 'dos-printer) > (stringp (symbol-value 'dos-printer)) > (symbol-value 'dos-printer)) > - printer-name))) > + printer-name > + (and (fboundp 'default-printer-name) > + (default-printer-name))))) > (or (eq coding-system-for-write 'no-conversion) > (setq coding-system-for-write > (aref eol-type 1))) ; force conversion to DOS EOLs > @@ -411,7 +413,9 @@ > (let ((printer (or (and (boundp 'dos-ps-printer) > (stringp (symbol-value 'dos-ps-printer)) > (symbol-value 'dos-ps-printer)) > - ps-printer-name))) > + ps-printer-name > + (and (fboundp 'default-printer-name) > + (default-printer-name))))) > (direct-print-region-helper printer start end lpr-prog > delete-text buf display rest))) > diff -bur emacs-cvs-20030904.orig/lisp/lpr.el emacs-cvs-20030904/lisp/lpr.el > --- emacs-cvs-20030904.orig/lisp/lpr.el 2003-09-03 10:14:58.000000000 +0200 > +++ emacs-cvs-20030904/lisp/lpr.el 2003-09-05 22:50:41.000000000 +0200 > @@ -255,9 +255,12 @@ > ;; These belong in pr if we are using that. > (and lpr-add-switches lpr-headers-switches > (list "-T" title)) > - (and (stringp printer-name) > + (or (and (stringp printer-name) > (list (concat lpr-printer-switch > printer-name))) > + (and (fboundp 'default-printer-name) > + (list (concat lpr-printer-switch > + (default-printer-name))))) > nswitches)) > (if (markerp end) > (set-marker end nil)) > diff -bur emacs-cvs-20030904.orig/lisp/play/handwrite.el emacs-cvs-20030904/lisp/play/handwrite.el > --- emacs-cvs-20030904.orig/lisp/play/handwrite.el 2003-09-03 10:15:15.000000000 +0200 > +++ emacs-cvs-20030904/lisp/play/handwrite.el 2003-09-05 22:50:41.000000000 +0200 > @@ -335,7 +335,9 @@ > (let* ((coding-system-for-write 'raw-text-unix) > (ps-printer-name (or ps-printer-name > (and (boundp 'printer-name) > - printer-name))) > + printer-name) > + (and (fboundp 'default-printer-name) > + (default-printer-name)))) > (ps-lpr-switches > (if (stringp ps-printer-name) > (list (concat "-P" ps-printer-name))))) > diff -bur emacs-cvs-20030904.orig/lisp/ps-print.el emacs-cvs-20030904/lisp/ps-print.el > --- emacs-cvs-20030904.orig/lisp/ps-print.el 2003-09-03 10:15:00.000000000 +0200 > +++ emacs-cvs-20030904/lisp/ps-print.el 2003-09-05 22:50:41.000000000 +0200 > @@ -6563,7 +6563,9 @@ > (let* ((coding-system-for-write 'raw-text-unix) > (ps-printer-name (or ps-printer-name > (and (boundp 'printer-name) > - (symbol-value 'printer-name)))) > + (symbol-value 'printer-name)) > + (and (fboundp 'default-printer-name) > + (default-printer-name)))) > (ps-lpr-switches > (append ps-lpr-switches > (and (stringp ps-printer-name) > diff -bur emacs-cvs-20030904.orig/nt/ChangeLog emacs-cvs-20030904/nt/ChangeLog > --- emacs-cvs-20030904.orig/nt/ChangeLog 2003-09-04 09:19:35.000000000 +0200 > +++ emacs-cvs-20030904/nt/ChangeLog 2003-09-08 09:04:42.000000000 +0200 > @@ -1,3 +1,7 @@ > +2003-09-05 Peter Runestig > + > + * gmake.defs, nmake.defs: Add linking to ``winspool.lib''. > + > 2003-09-03 Peter Runestig > * configure.bat: Create ``makefile'' in directories man, lispref > diff -bur emacs-cvs-20030904.orig/nt/gmake.defs emacs-cvs-20030904/nt/gmake.defs > --- emacs-cvs-20030904.orig/nt/gmake.defs 2003-09-03 10:15:31.000000000 +0200 > +++ emacs-cvs-20030904/nt/gmake.defs 2003-09-05 22:50:41.000000000 +0200 > @@ -177,6 +177,7 @@ > USER32 = -luser32 > WSOCK32 = -lwsock32 > WINMM = -lwinmm > +WINSPOOL = -lwinspool > ifdef NOOPT > DEBUG_CFLAGS = -DEMACSDEBUG > diff -bur emacs-cvs-20030904.orig/nt/nmake.defs emacs-cvs-20030904/nt/nmake.defs > --- emacs-cvs-20030904.orig/nt/nmake.defs 2003-09-03 10:15:31.000000000 +0200 > +++ emacs-cvs-20030904/nt/nmake.defs 2003-09-05 22:50:41.000000000 +0200 > @@ -124,6 +124,7 @@ > USER32 = user32.lib > WSOCK32 = wsock32.lib > WINMM = winmm.lib > +WINSPOOL = winspool.lib > !ifdef NOOPT > DEBUG_CFLAGS = -DEMACSDEBUG > diff -bur emacs-cvs-20030904.orig/src/ChangeLog emacs-cvs-20030904/src/ChangeLog > --- emacs-cvs-20030904.orig/src/ChangeLog 2003-09-03 10:15:33.000000000 +0200 > +++ emacs-cvs-20030904/src/ChangeLog 2003-09-08 09:06:20.000000000 +0200 > @@ -1,3 +1,7 @@ > +2003-09-05 Peter Runestig > + > + * makefile.w32-in, w32fns.c: Add `default-printer-name' function. > + > 2003-09-03 Kim F. Storm > * xdisp.c (get_window_cursor_type): Partially undo 2002-03-01 > Only in emacs-cvs-20030904/src: ChangeLog.~1.3360.~ > diff -bur emacs-cvs-20030904.orig/src/makefile.w32-in emacs-cvs-20030904/src/makefile.w32-in > --- emacs-cvs-20030904.orig/src/makefile.w32-in 2003-09-03 10:15:39.000000000 +0200 > +++ emacs-cvs-20030904/src/makefile.w32-in 2003-09-06 01:50:36.000000000 +0200 > @@ -141,6 +141,7 @@ > $(USER32) \ > $(MPR) \ > $(SHELL32) \ > + $(WINSPOOL) \ > $(libc) > # > diff -bur emacs-cvs-20030904.orig/src/w32fns.c emacs-cvs-20030904/src/w32fns.c > --- emacs-cvs-20030904.orig/src/w32fns.c 2003-09-03 10:15:41.000000000 +0200 > +++ emacs-cvs-20030904/src/w32fns.c 2003-09-05 23:41:01.000000000 +0200 > @@ -51,6 +51,7 @@ > #include > #include > #include > +#include > #include > #define FILE_NAME_TEXT_FIELD edt1 > @@ -13925,6 +13926,76 @@ > return value; > } > > +DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name, > + 0, 0, 0, doc: /* Return the name of Windows default printer device. */) > + () > +{ > + static char pname_buf[256]; > + int err; > + HANDLE hPrn; > + PRINTER_INFO_2 *ppi2 = NULL; > + DWORD dwNeeded = 0, dwReturned = 0; > + > + /* Retrieve the default string from Win.ini (the registry). > + * String will be in form "printername,drivername,portname". > + * This is the most portable way to get the default printer. */ > + if (GetProfileString ("windows", "device", ",,", pname_buf, sizeof (pname_buf)) <= 0) > + return Qnil; > + /* printername precedes first "," character */ > + strtok (pname_buf, ","); > + /* We want to know more than the printer name */ > + if (!OpenPrinter (pname_buf, &hPrn, NULL)) > + return Qnil; > + GetPrinter (hPrn, 2, NULL, 0, &dwNeeded); > + if (dwNeeded == 0) > + { > + ClosePrinter (hPrn); > + return Qnil; > + } > + /* Allocate memory for the PRINTER_INFO_2 struct */ > + ppi2 = (PRINTER_INFO_2 *) xmalloc (dwNeeded); > + if (!ppi2) > + { > + ClosePrinter (hPrn); > + return Qnil; > + } > + /* Call GetPrinter() again with big enouth memory block */ > + err = GetPrinter (hPrn, 2, (LPBYTE)ppi2, dwNeeded, &dwReturned); > + ClosePrinter (hPrn); > + if (!err) > + { > + xfree(ppi2); > + return Qnil; > + } > + > + if (ppi2) > + { > + if (ppi2->Attributes & PRINTER_ATTRIBUTE_SHARED && ppi2->pServerName) > + { > + /* a remote printer */ > + if (*ppi2->pServerName == '\\') > + _snprintf(pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName, > + ppi2->pShareName); > + else > + _snprintf(pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName, > + ppi2->pShareName); > + pname_buf[sizeof (pname_buf) - 1] = '\0'; > + } > + else > + { > + /* a local printer */ > + strncpy(pname_buf, ppi2->pPortName, sizeof (pname_buf)); > + pname_buf[sizeof (pname_buf) - 1] = '\0'; > + /* `pPortName' can include several ports, delimited by ','. > + * we only use the first one. */ > + strtok(pname_buf, ","); > + } > + xfree(ppi2); > + } > + > + return build_string (pname_buf); > +} > + > /*********************************************************************** > Initialization > ***********************************************************************/ > @@ -14377,6 +14448,7 @@ > defsubr (&Sw32_find_bdf_fonts); > defsubr (&Sfile_system_info); > + defsubr (&Sdefault_printer_name); > /* Setting callback functions for fontset handler. */ > get_font_info_func = w32_get_font_info; > -- > Peter 'Luna' Runestig (fd. Altberg), Sweden > PGP Key ID: 0xD07BBE13 > Fingerprint: 7B5C 1F48 2997 C061 DE4B 42EA CB99 A35C D07B BE13 > AOL Instant Messenger Screen name: PRunestig > Yahoo! Messenger profile name: altberg > _______________________________________________ > Emacs-devel mailing list > Emacs-devel@gnu.org > http://mail.gnu.org/mailman/listinfo/emacs-devel