*** empty log message ***
[bpt/emacs.git] / lisp / resume.el
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 ;; Created by: Joe Wells, jbw@bucsf.bu.edu
22 ;; Created on: 1988?
23 ;; Last modified by: Joe Wells, jbw@dodge
24 ;; Last modified on: Thu Jun 14 15:20:41 1990
25 ;; Filename: resume.el
26 ;; Purpose: handle command line arguments when resuming suspended job
27
28 ;; Stephen Gildea suggested bug fix (gildea@bbn.com).
29 ;; Ideas from Michael DeCorte and other people.
30
31 ;; For csh users, insert the following alias in your .cshrc file
32 ;; (after removing the leading double semicolons, of course):
33 ;;
34 ;;# The following line could be just EMACS_CMD=emacs, but this depends on
35 ;;# your site.
36 ;;if (! $?EMACS_CMD) set EMACS_CMD=emacs
37 ;;set JOBS_FILE=/tmp/jobs.$USER.$$
38 ;;set ARGS_FILE=~/.emacs_args
39 ;;set STOP_PATT='^\[[0-9]*\] *[ +-] Stopped ............ '
40 ;;set SUNVIEW_CMD='emacstool -nw -f emacstool-init -f server-start'
41 ;;set X_CMD=\'\''$EMACS_CMD -i -f server-start'
42 ;;alias emacs \
43 ;;' \\
44 ;; jobs >! "$JOBS_FILE" \\
45 ;; && grep "$STOP_PATT$EMACS_CMD" "$JOBS_FILE" >& /dev/null \\
46 ;; && echo `pwd` \!* >! "$ARGS_FILE" && ""fg %$EMACS_CMD \\
47 ;;|| if (! -e ~/.emacs_server || -f ~/.emacs_server) set status=1 \\
48 ;; && emacsclient \!* \\
49 ;;|| @ status=1 - $?DISPLAY && eval "$X_CMD -i \!* &" \\
50 ;;|| @ status=1 - $?WINDOW_PARENT && eval "$SUNVIEW_CMD \!* &" \\
51 ;;|| ""$EMACS_CMD -nw \!* \\
52 ;;'
53 ;;
54 ;; The alias works as follows:
55 ;; 1. If there is a suspended Emacs job that is a child of the
56 ;; current shell, place its arguments in the ~/.emacs_args file and
57 ;; resume it.
58 ;; 2. Else if the ~/.emacs_server socket has been created, presume an
59 ;; Emacs server is running and attempt to connect to it. If no Emacs
60 ;; server is listening on the socket, this will fail.
61 ;; 3. Else if the DISPLAY environment variable is set, presume we are
62 ;; running under X Windows and start a new GNU Emacs process in the
63 ;; background as an X client.
64 ;; 4. Else if the WINDOW_PARENT environment variable is set, presume we
65 ;; are running under SunView and start an emacstool process in the
66 ;; background.
67 ;; 5. Else start a regular Emacs process.
68 ;;
69 ;; Notes:
70 ;; The output of the "jobs" command is not piped directly into "grep"
71 ;; because that would run the "jobs" command in a subshell.
72 ;; Before resuming a suspended emacs, the current directory and all
73 ;; command line arguments are placed in a file name ~/.emacs_args.
74 ;; The "-nw" switch to Emacs means no windowing system.
75
76 ;; Insert this in your .emacs file:
77 ;;(setq suspend-resume-hook 'resume-process-args)
78 ;;(setq suspend-hook 'empty-args-file)
79 ;;(autoload 'empty-args-file "resume")
80 ;;(autoload 'resume-process-args "resume")
81
82 ;; Finally, put the rest in a file named "resume.el" in a lisp library
83 ;; directory.
84
85 (defvar emacs-args-file (expand-file-name "~/.emacs_args")
86 "*This file is where arguments are placed for a suspended emacs job.")
87
88 (defvar emacs-args-buffer " *Command Line Args*"
89 "Buffer that is used by resume-process-args.")
90
91 (defun resume-process-args ()
92 "This should be called from inside of `suspend-resume-hook'.
93 This grabs the contents of the file whose name is stored in `emacs-args-file',
94 and processes these arguments like command line options."
95 (let ((start-buffer (current-buffer))
96 (args-buffer (get-buffer-create emacs-args-buffer))
97 length args)
98 (unwind-protect
99 (progn
100 (set-buffer args-buffer)
101 (erase-buffer)
102 ;; get the contents of emacs-args-file
103 (condition-case ()
104 (let ((result (insert-file-contents emacs-args-file)))
105 (setq length (car (cdr result))))
106 ;; the file doesn't exist, ergo no arguments
107 (file-error
108 (erase-buffer)
109 (setq length 0)))
110 (if (<= length 0)
111 (setq args nil)
112 ;; get the arguments from the buffer
113 (goto-char (point-min))
114 (while (not (eobp))
115 (skip-chars-forward " \t\n")
116 (let ((begin (point)))
117 (skip-chars-forward "^ \t\n")
118 (setq args (cons (buffer-substring begin (point)) args)))
119 (skip-chars-forward " \t\n"))
120 ;; arguments are now in reverse order
121 (setq args (nreverse args))
122 ;; make sure they're not read again
123 (erase-buffer))
124 (write-buffer-to-file (current-buffer) emacs-args-file)
125 ;; if nothing was in buffer, args will be null
126 (or (null args)
127 (setq default-directory (file-name-as-directory (car args))
128 args (cdr args)))
129 ;; actually process the arguments
130 (command-line-1 args))
131 ;; If the command line args don't result in a find-file, the
132 ;; buffer will be left in args-buffer. So we change back to the
133 ;; original buffer. The reason I don't just use
134 ;; (let ((default-directory foo))
135 ;; (command-line-1 args))
136 ;; in the context of the original buffer is because let does not
137 ;; work properly with buffer-local variables.
138 (if (eq (current-buffer) args-buffer)
139 (set-buffer start-buffer)))))
140
141 (defun empty-args-file ()
142 "This empties the contents of the file whose name is specified by
143 `emacs-args-file'."
144 (save-excursion
145 (set-buffer (get-buffer-create emacs-args-buffer))
146 (erase-buffer)
147 (write-buffer-to-file (current-buffer) emacs-args-file)))
148
149 (defun write-buffer-to-file (buffer file)
150 "Writes the contents of BUFFER into FILE, if permissions allow."
151 (if (not (file-writable-p file))
152 (error "No permission to write file %s" file))
153 (save-excursion
154 (set-buffer buffer)
155 (clear-visited-file-modtime)
156 (save-restriction
157 (widen)
158 (write-region (point-min) (point-max) file nil 'quiet))
159 (set-buffer-modified-p nil)))