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