Commit | Line | Data |
---|---|---|
d543cc8e | 1 | ;; Process command line arguments from within a suspended Emacs job |
dc556c64 RS |
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. | |
dc556c64 | 20 | |
d543cc8e RS |
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). | |
dc556c64 RS |
29 | ;; Ideas from Michael DeCorte and other people. |
30 | ||
31 | ;; For csh users, insert the following alias in your .cshrc file | |
d543cc8e | 32 | ;; (after removing the leading double semicolons, of course): |
dc556c64 | 33 | ;; |
d543cc8e | 34 | ;;# The following line could be just EMACS_CMD=emacs, but this depends on |
dc556c64 | 35 | ;;# your site. |
d543cc8e RS |
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' | |
dc556c64 RS |
42 | ;;alias emacs \ |
43 | ;;' \\ | |
d543cc8e RS |
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 | ;;' | |
dc556c64 RS |
53 | ;; |
54 | ;; The alias works as follows: | |
d543cc8e | 55 | ;; 1. If there is a suspended Emacs job that is a child of the |
dc556c64 RS |
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 | |
d543cc8e | 59 | ;; Emacs server is running and attempt to connect to it. If no Emacs |
dc556c64 RS |
60 | ;; server is listening on the socket, this will fail. |
61 | ;; 3. Else if the DISPLAY environment variable is set, presume we are | |
d543cc8e RS |
62 | ;; running under X Windows and start a new GNU Emacs process in the |
63 | ;; background as an X client. | |
dc556c64 | 64 | ;; 4. Else if the WINDOW_PARENT environment variable is set, presume we |
d543cc8e RS |
65 | ;; are running under SunView and start an emacstool process in the |
66 | ;; background. | |
67 | ;; 5. Else start a regular Emacs process. | |
dc556c64 RS |
68 | ;; |
69 | ;; Notes: | |
dc556c64 RS |
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 | |
d543cc8e RS |
73 | ;; command line arguments are placed in a file name ~/.emacs_args. |
74 | ;; The "-nw" switch to Emacs means no windowing system. | |
dc556c64 | 75 | |
d543cc8e RS |
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") | |
dc556c64 RS |
86 | "*This file is where arguments are placed for a suspended emacs job.") |
87 | ||
d543cc8e RS |
88 | (defvar emacs-args-buffer " *Command Line Args*" |
89 | "Buffer that is used by resume-process-args.") | |
dc556c64 RS |
90 | |
91 | (defun resume-process-args () | |
d543cc8e RS |
92 | "This should be called from inside of suspend-resume-hook. This |
93 | grabs the contents of the file whose name is stored in | |
94 | emacs-args-file, and processes these arguments like command line | |
95 | options." | |
96 | (let ((start-buffer (current-buffer)) | |
97 | (args-buffer (get-buffer-create emacs-args-buffer)) | |
98 | length args) | |
dc556c64 RS |
99 | (unwind-protect |
100 | (progn | |
d543cc8e | 101 | (set-buffer args-buffer) |
dc556c64 | 102 | (erase-buffer) |
d543cc8e | 103 | ;; get the contents of emacs-args-file |
dc556c64 | 104 | (condition-case () |
d543cc8e RS |
105 | (let ((result (insert-file-contents emacs-args-file))) |
106 | (setq length (car (cdr result)))) | |
107 | ;; the file doesn't exist, ergo no arguments | |
108 | (file-error | |
109 | (erase-buffer) | |
110 | (setq length 0))) | |
111 | (if (<= length 0) | |
112 | (setq args nil) | |
113 | ;; get the arguments from the buffer | |
114 | (goto-char (point-min)) | |
115 | (while (not (eobp)) | |
116 | (skip-chars-forward " \t\n") | |
117 | (let ((begin (point))) | |
118 | (skip-chars-forward "^ \t\n") | |
119 | (setq args (cons (buffer-substring begin (point)) args))) | |
120 | (skip-chars-forward " \t\n")) | |
121 | ;; arguments are now in reverse order | |
122 | (setq args (nreverse args)) | |
123 | ;; make sure they're not read again | |
124 | (erase-buffer)) | |
125 | (write-buffer-to-file (current-buffer) emacs-args-file) | |
126 | ;; if nothing was in buffer, args will be null | |
127 | (or (null args) | |
128 | (setq default-directory (file-name-as-directory (car args)) | |
129 | args (cdr args))) | |
130 | ;; actually process the arguments | |
131 | (command-line-1 args)) | |
dc556c64 | 132 | ;; If the command line args don't result in a find-file, the |
d543cc8e | 133 | ;; buffer will be left in args-buffer. So we change back to the |
dc556c64 RS |
134 | ;; original buffer. The reason I don't just use |
135 | ;; (let ((default-directory foo)) | |
136 | ;; (command-line-1 args)) | |
137 | ;; in the context of the original buffer is because let does not | |
138 | ;; work properly with buffer-local variables. | |
d543cc8e RS |
139 | (if (eq (current-buffer) args-buffer) |
140 | (set-buffer start-buffer))))) | |
141 | ||
142 | (defun empty-args-file () | |
143 | "This empties the contents of the file whose name is specified by | |
144 | emacs-args-file." | |
145 | (save-excursion | |
146 | (set-buffer (get-buffer-create emacs-args-buffer)) | |
147 | (erase-buffer) | |
148 | (write-buffer-to-file (current-buffer) emacs-args-file))) | |
149 | ||
150 | (defun write-buffer-to-file (buffer file) | |
151 | "Writes the contents of BUFFER into FILE, if permissions allow." | |
152 | (if (not (file-writable-p file)) | |
153 | (error "No permission to write file %s" file)) | |
154 | (save-excursion | |
155 | (set-buffer buffer) | |
156 | (clear-visited-file-modtime) | |
157 | (save-restriction | |
158 | (widen) | |
159 | (write-region (point-min) (point-max) file nil 'quiet)) | |
160 | (set-buffer-modified-p nil))) |