From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Dan Nicolaescu Newsgroups: gmane.emacs.bugs Subject: bug#1298: allow 'emacsclient -a "emacs --daemon && emacsclient -c"' Date: Sun, 7 Dec 2008 23:54:33 -0800 (PST) Message-ID: <200812080754.mB87sXSV016391@mothra.ics.uci.edu> References: <200811021818.mA2IIFMq028772@mothra.ics.uci.edu> <200811180822.mAI8MX0n029841@mothra.ics.uci.edu> <200811181817.mAIIHhEf001001@mothra.ics.uci.edu> <200811182219.mAIMJv4G002062@mothra.ics.uci.edu> <200811182327.mAINR4ZT002539@mothra.ics.uci.edu> Reply-To: Dan Nicolaescu , 1298@emacsbugs.donarmstrong.com NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1228723853 1543 80.91.229.12 (8 Dec 2008 08:10:53 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 8 Dec 2008 08:10:53 +0000 (UTC) Cc: 1298@emacsbugs.donarmstrong.com To: Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Dec 08 09:11:58 2008 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1L9bDe-0006ss-CI for geb-bug-gnu-emacs@m.gmane.org; Mon, 08 Dec 2008 09:11:54 +0100 Original-Received: from localhost ([127.0.0.1]:59044 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1L9bCT-00033v-7D for geb-bug-gnu-emacs@m.gmane.org; Mon, 08 Dec 2008 03:10:41 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1L9bCE-0002wB-8g for bug-gnu-emacs@gnu.org; Mon, 08 Dec 2008 03:10:26 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1L9bCD-0002v8-9f for bug-gnu-emacs@gnu.org; Mon, 08 Dec 2008 03:10:25 -0500 Original-Received: from [199.232.76.173] (port=32974 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1L9bCD-0002ut-4e for bug-gnu-emacs@gnu.org; Mon, 08 Dec 2008 03:10:25 -0500 Original-Received: from mx20.gnu.org ([199.232.41.8]:61372) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1L9bCC-00079X-HM for bug-gnu-emacs@gnu.org; Mon, 08 Dec 2008 03:10:24 -0500 Original-Received: from rzlab.ucr.edu ([138.23.92.77]) by mx20.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1L9bCB-0002Ag-IO for bug-gnu-emacs@gnu.org; Mon, 08 Dec 2008 03:10:23 -0500 Original-Received: from rzlab.ucr.edu (rzlab.ucr.edu [127.0.0.1]) by rzlab.ucr.edu (8.13.8/8.13.8/Debian-3) with ESMTP id mB88AEpW008730; Mon, 8 Dec 2008 00:10:14 -0800 Original-Received: (from debbugs@localhost) by rzlab.ucr.edu (8.13.8/8.13.8/Submit) id mB8803Hk004503; Mon, 8 Dec 2008 00:00:03 -0800 X-Loop: don@donarmstrong.com Resent-From: Dan Nicolaescu Resent-To: bug-submit-list@donarmstrong.com Resent-CC: Emacs Bugs Resent-Date: Mon, 08 Dec 2008 08:00:03 +0000 Resent-Message-ID: Resent-Sender: don@donarmstrong.com X-Emacs-PR-Message: report 1298 X-Emacs-PR-Package: emacs X-Emacs-PR-Keywords: Original-Received: via spool by 1298-submit@emacsbugs.donarmstrong.com id=B1298.12287228923005 (code B ref 1298); Mon, 08 Dec 2008 08:00:03 +0000 Original-Received: (at 1298) by emacsbugs.donarmstrong.com; 8 Dec 2008 07:54:52 +0000 Original-Received: from barrelv2.ics.uci.edu (barrelv2.ics.uci.edu [128.195.1.114]) by rzlab.ucr.edu (8.13.8/8.13.8/Debian-3) with ESMTP id mB87sniM002993 for <1298@emacsbugs.donarmstrong.com>; Sun, 7 Dec 2008 23:54:50 -0800 Original-Received: from mothra.ics.uci.edu (mothra.ics.uci.edu [128.195.6.93]) by barrelv2.ics.uci.edu (8.13.7+Sun/8.13.7) with ESMTP id mB87sYZe005582; Sun, 7 Dec 2008 23:54:34 -0800 (PST) Original-Received: (from dann@localhost) by mothra.ics.uci.edu (8.13.8+Sun/8.13.6/Submit) id mB87sXSV016391; Sun, 7 Dec 2008 23:54:33 -0800 (PST) In-Reply-To: (Stefan Monnier's message of "Tue, 18 Nov 2008 21:21:29 -0500") Original-Lines: 188 X-ICS-MailScanner-Information: Please contact the ISP for more information X-ICS-MailScanner-ID: mB87sYZe005582 X-ICS-MailScanner: Found to be clean X-ICS-MailScanner-SpamCheck: not spam, SpamAssassin (score=-1.286, required 5, autolearn=disabled, ALL_TRUSTED -1.44, TW_DL 0.08, TW_RG 0.08) X-ICS-MailScanner-From: dann@mothra.ics.uci.edu X-detected-kernel: by mx20.gnu.org: Linux 2.6 (newer, 3) X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Resent-Date: Mon, 08 Dec 2008 03:10:25 -0500 X-BeenThere: bug-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:23047 Archived-At: Stefan Monnier writes: > > How about something like: > > emacsclient --start-daemon > > You mean "emacsclient --start-daemon-if-needed FILENAME" ? > Yes, probably something like that. It could potentially be folded into > the alternate-editor (e.g. if alternate-editor has some special value > such as the empty string). How does the patch below look like? There's one refinement possible: make --start-daemon-if-needed take an argument --start-daemon-if-needed=ARG and use it like this: emacs --daemon=ARG Index: emacsclient.c =================================================================== RCS file: /cvsroot/emacs/emacs/lib-src/emacsclient.c,v retrieving revision 1.142 diff -u -3 -p -c -r1.142 emacsclient.c cvs diff: conflicting specifications of output style --- emacsclient.c 3 Dec 2008 04:33:44 -0000 1.142 +++ emacsclient.c 8 Dec 2008 07:52:45 -0000 @@ -150,6 +150,10 @@ /* PID of the Emacs server process. */ int emacs_pid = 0; +/* In case connecting to the server fails, try connecting again after + starting the emacs daemon. */ +int start_daemon_if_needed = 0; + void print_help_and_exit () NO_RETURN; struct option longopts[] = @@ -162,6 +166,7 @@ { "nw", no_argument, NULL, 't' }, { "create-frame", no_argument, NULL, 'c' }, { "alternate-editor", required_argument, NULL, 'a' }, + { "start-daemon-if-needed", no_argument, NULL, 'D' }, #ifndef NO_SOCKETS_IN_FILE_SYSTEM { "socket-name", required_argument, NULL, 's' }, #endif @@ -492,7 +497,7 @@ { int opt = getopt_long_only (argc, argv, #ifndef NO_SOCKETS_IN_FILE_SYSTEM - "VHnea:s:f:d:tc", + "VHneDa:s:f:d:tc", #else "VHnea:f:d:tc", #endif @@ -552,6 +557,10 @@ current_frame = 0; break; + case 'D': + start_daemon_if_needed = 1; + break; + case 'H': print_help_and_exit (); break; @@ -563,6 +572,12 @@ } } + if (start_daemon_if_needed && alternate_editor != NULL) + { + message (TRUE, "--alternate-editor cannot be used when using --start_daemon_if_needed\n"); + exit (EXIT_FAILURE); + } + /* If the -c option is used (without -t) and no --display argument is provided, try $DISPLAY. Without the -c option, we used to set `display' to $DISPLAY by @@ -1294,7 +1309,7 @@ #endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */ HSOCKET -set_socket () +set_socket (int no_exit_if_error) { HSOCKET s; @@ -1305,7 +1320,7 @@ if (socket_name) { s = set_local_socket (); - if ((s != INVALID_SOCKET) || alternate_editor) + if ((s != INVALID_SOCKET) || no_exit_if_error) return s; message (TRUE, "%s: error accessing socket \"%s\"\n", progname, socket_name); @@ -1320,7 +1335,7 @@ if (server_file) { s = set_tcp_socket (); - if ((s != INVALID_SOCKET) || alternate_editor) + if ((s != INVALID_SOCKET) || no_exit_if_error) return s; message (TRUE, "%s: error accessing server file \"%s\"\n", @@ -1338,7 +1353,7 @@ /* Implicit server file. */ server_file = "server"; s = set_tcp_socket (); - if ((s != INVALID_SOCKET) || alternate_editor) + if ((s != INVALID_SOCKET) || no_exit_if_error) return s; /* No implicit or explicit socket, and no alternate editor. */ @@ -1353,7 +1368,7 @@ } #ifdef WINDOWSNT -FARPROC set_fg; /* Pointer to AllowSetForegroundWindow. */ +FARPROC se t_fg; /* Pointer to AllowSetForegroundWindow. */ FARPROC get_wc; /* Pointer to RealGetWindowClassA. */ BOOL CALLBACK @@ -1416,6 +1431,8 @@ int i, rl, needlf = 0; char *cwd, *str; char string[BUFSIZ+1]; + int null_socket_name; + int null_server_file; main_argv = argv; progname = argv[0]; @@ -1431,9 +1448,57 @@ exit (EXIT_FAILURE); } - if ((emacs_socket = set_socket ()) == INVALID_SOCKET) - fail (); + if (start_daemon_if_needed) + { + /* set_socket changes the values for socket_name and + server_file, we need to reset them, if they were NULL before + for the second call to set_socket. */ + null_socket_name = (socket_name == NULL); + null_server_file = (server_file == NULL); + } + + if ((emacs_socket = set_socket (alternate_editor || start_daemon_if_needed)) == INVALID_SOCKET) + if (start_daemon_if_needed) + { + pid_t dpid; + int status; + pid_t p; + + dpid = fork (); + if (dpid > 0) + { + p = waitpid (dpid, &status, WUNTRACED | WCONTINUED); + + /* Reset socket_name and server_file if they were NULL + before the set_socket call. */ + if (null_socket_name) + socket_name = NULL; + if (null_server_file) + server_file = NULL; + + /* Try connecting again, the daemon should have started by + now. */ + message (TRUE, "daemon should have started, trying to connect again\n", dpid); + if ((emacs_socket = set_socket (1)) == INVALID_SOCKET) + message (TRUE, "Cannot connect even after starting the daemon\n"); + } + else if (dpid < 0) + { + fprintf (stderr, "Cannot fork!\n"); + exit (1); + } + else + { + char *const d_argv[] = {"emacs", "--daemon", 0 }; + + /* Start the daemon. */ + execvp ("emacs", d_argv); + message (TRUE, "%s: error starting emacs daemon\n", progname); + } + } + else + fail (); cwd = get_current_dir_name (); if (cwd == 0)