comparison lisp/packages/resume.el @ 0:376386a54a3c r19-14

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