comparison lisp/resume.el @ 17:1e1bc0842996

Initial revision
author Richard M. Stallman <rms@gnu.org>
date Sat, 08 Oct 1988 12:24:34 +0000
parents
children 0bc7e3b14f85
comparison
equal deleted inserted replaced
16:b539fba0f13f 17:1e1bc0842996
1 ;; process command line arguments from within a suspended emacs job
2 ;; Copyright (C) 1988 Free Software Foundation, Inc.
3
4 ;; This file is not yet part of GNU Emacs, but soon will be.
5
6 ;; GNU Emacs is distributed in the hope that it will be useful,
7 ;; but WITHOUT ANY WARRANTY. No author or distributor
8 ;; accepts responsibility to anyone for the consequences of using it
9 ;; or for whether it serves any particular purpose or works at all,
10 ;; unless he says so in writing. Refer to the GNU Emacs General Public
11 ;; License for full details.
12
13 ;; Everyone is granted permission to copy, modify and redistribute
14 ;; GNU Emacs, but only under the conditions described in the
15 ;; GNU Emacs General Public License. A copy of this license is
16 ;; supposed to have been given to you along with GNU Emacs so you
17 ;; can know your rights and responsibilities. It should be in a
18 ;; file named COPYING. Among other things, the copyright notice
19 ;; and this notice must be preserved on all copies.
20 ;;
21 ;; by Joe Wells
22 ;; jbw@bucsf.bu.edu
23 ;; joew@uswat.uswest.com (maybe, ... the mailer there sucks)
24
25 ;; Stephan Gildea suggested bug fix (gildea@bbn.com).
26 ;; Ideas from Michael DeCorte and other people.
27
28 ;; For csh users, insert the following alias in your .cshrc file
29 ;; (after removing the leading double semicolons):
30 ;;
31 ;;# The following line could be just EMACS=emacs, but this depends on
32 ;;# your site.
33 ;;set EMACS=emacs
34 ;;set EMACS_PATTERN="^\[[0-9]\] . Stopped ............ $EMACS"
35 ;;alias emacs \
36 ;;' \\
37 ;; jobs >! /tmp/jobs$$ \\
38 ;; && grep "$EMACS_PATTERN" /tmp/jobs$$ >& /dev/null \\
39 ;; && echo `pwd` \!* >! ~/.emacs_args && eval "%$EMACS" \\
40 ;;|| test -S ~/.emacs_server && emacsclient \!* \\
41 ;;|| test "$?DISPLAY" = 1 && eval "\$EMACS -i \!* &" \\
42 ;;|| test "$?WINDOW_PARENT" = 1 && eval "emacstool -f emacstool-init \!* &" \\
43 ;;|| eval "\$EMACS -nw \!*"'
44 ;;
45 ;; The alias works as follows:
46 ;; 1. If there is a suspended emacs jobs that is a child of the
47 ;; current shell, place its arguments in the ~/.emacs_args file and
48 ;; resume it.
49 ;; 2. Else if the ~/.emacs_server socket has been created, presume an
50 ;; emacs server is running and attempt to connect to it. If no emacs
51 ;; server is listening on the socket, this will fail.
52 ;; 3. Else if the DISPLAY environment variable is set, presume we are
53 ;; running under X Windows and start a new X Gnu Emacs process in the
54 ;; background.
55 ;; 4. Else if the WINDOW_PARENT environment variable is set, presume we
56 ;; are running under Sunview and Suntools and start an emacstool
57 ;; process in the background.
58 ;; 5. Else start a regular emacs process.
59 ;;
60 ;; Notes:
61 ;; "test -S" checks if a unix domain socket by that name exists.
62 ;; The output of the "jobs" command is not piped directly into "grep"
63 ;; because that would run the "jobs" command in a subshell.
64 ;; Before resuming a suspended emacs, the current directory and all
65 ;; command line arguments are placed in a file.
66 ;; The command to run emacs is always preceded by a \ to prevent
67 ;; possible alias loops.
68 ;; The "-nw" switch in the last line is is undocumented, and it means
69 ;; no windowing system.
70
71 (setq suspend-resume-hook 'resume-process-args)
72 (setq suspend-hook 'resume-preparation)
73
74 (defvar emacs-args-file "~/.emacs_args"
75 "*This file is where arguments are placed for a suspended emacs job.")
76
77 (defun resume-preparation ()
78 (condition-case ()
79 (delete-file emacs-args-file)
80 (error nil)))
81
82 (defun resume-process-args ()
83 "This should be called from inside of suspend-resume-hook.
84 Grabs the contents of the file whose name is stored in
85 emacs-args-file, and processes these arguments like command line options."
86 (let ((resume-start-buffer (current-buffer))
87 (resume-args-buffer (get-buffer-create " *Command Line Args*"))
88 resume-args)
89 (unwind-protect
90 (progn
91 (set-buffer resume-args-buffer)
92 (erase-buffer)
93 ;; Get the contents of emacs-args-file, then delete the file.
94 (condition-case ()
95 (progn
96 (insert-file-contents emacs-args-file)
97 (delete-file emacs-args-file))
98 ;; The file doesn't exist or we can't delete it, ergo no arguments.
99 ;; (If we can't delete it now, we probably couldn't delete it
100 ;; before suspending, and that implies it may be vestigial.)
101 (file-error (erase-buffer)))
102 ;; Get the arguments from the buffer.
103 (goto-char (point-min))
104 (while (progn (skip-chars-forward " \t\n") (not (eobp)))
105 (setq resume-args
106 (cons (buffer-substring (point)
107 (progn
108 (skip-chars-forward "^ \t\n")
109 (point)))
110 resume-args)))
111 (cond (resume-args
112 ;; Arguments are now in reverse order.
113 (setq resume-args (nreverse resume-args))
114 ;; The "first argument" is really a default directory to use
115 ;; while processing the rest of the arguments.
116 (setq default-directory (concat (car resume-args) "/"))
117 ;; Actually process the arguments.
118 (command-line-1 (cdr resume-args)))))
119 ;; If the command line args don't result in a find-file, the
120 ;; buffer will be left in resume-args-buffer. So we change back to the
121 ;; original buffer. The reason I don't just use
122 ;; (let ((default-directory foo))
123 ;; (command-line-1 args))
124 ;; in the context of the original buffer is because let does not
125 ;; work properly with buffer-local variables.
126 (if (eq (current-buffer) resume-args-buffer)
127 (set-buffer resume-start-buffer)))))