annotate lisp/packages/resume.el @ 100:4be1180a9e89 r20-1b2

Import from CVS: tag r20-1b2
author cvs
date Mon, 13 Aug 2007 09:15:11 +0200
parents ac2d302a0011
children
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
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
2 ;; Copyright (C) 1988, 1992 Free Software Foundation, Inc.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
3
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
4 ;; Author: Joe Wells <jbw@bucsf.bu.edu>
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
5 ;; Adapted-By: ESR
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; Keywords: processes
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
8 ;; This file is part of XEmacs.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
10 ;; XEmacs is free software; you can redistribute it and/or modify it
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
11 ;; under the terms of the GNU General Public License as published by
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
13 ;; any later version.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
15 ;; XEmacs is distributed in the hope that it will be useful, but
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
18 ;; General Public License for more details.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
19
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
20 ;; You should have received a copy of the GNU General Public License
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
21 ;; along with XEmacs; see the file COPYING. If not, write to the
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
23 ;; Boston, MA 02111-1307, USA.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
24
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
25 ;;; Synched up with: 19.34
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
27 ;;; Commentary:
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
28
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
29 ;; The purpose of this library is to handle command line arguments
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
30 ;; when you resume an existing Emacs job.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
31
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
32 ;; In order to use it, you must put this code in your .emacs file.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
34 ;; (add-hook 'suspend-hook 'resume-suspend-hook)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
35 ;; (add-hook 'suspend-resume-hook 'resume-process-args)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
36
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
37 ;; You can't get the benefit of this library by using the `emacs' command,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
38 ;; since that always starts a new Emacs job. Instead you must use a
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
39 ;; command called `edit' which knows how to resume an existing Emacs job
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
40 ;; if you have one, or start a new Emacs job if you don't have one.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
41
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
42 ;; To define the `edit' command, run the script etc/emacs.csh (if you use CSH),
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
43 ;; or etc/emacs.bash if you use BASH. You would normally do this in your
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
44 ;; login script.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 ;; Stephan Gildea suggested bug fix (gildea@bbn.com).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 ;; Ideas from Michael DeCorte and other people.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
49 ;;; Code:
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
51 (defvar resume-emacs-args-file (expand-file-name "~/.emacs_args")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 "*This file is where arguments are placed for a suspended emacs job.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
54 (defvar resume-emacs-args-buffer " *Command Line Args*"
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
55 "Buffer that is used by resume-process-args.")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 (defun resume-process-args ()
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
58 "Handler for command line args given when Emacs is resumed."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
59 (let ((start-buffer (current-buffer))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
60 (args-buffer (get-buffer-create resume-emacs-args-buffer))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
61 length args
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
62 (command-line-default-directory default-directory))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 (progn
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
65 (set-buffer args-buffer)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 (erase-buffer)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
67 ;; get the contents of resume-emacs-args-file
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (condition-case ()
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
69 (let ((result (insert-file-contents resume-emacs-args-file)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
70 (setq length (car (cdr result))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
71 ;; the file doesn't exist, ergo no arguments
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
72 (file-error
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
73 (erase-buffer)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
74 (setq length 0)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
75 (if (<= length 0)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
76 (setq args nil)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
77 ;; get the arguments from the buffer
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
78 (goto-char (point-min))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
79 (while (not (eobp))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
80 (skip-chars-forward " \t\n")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
81 (let ((begin (point)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
82 (skip-chars-forward "^ \t\n")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
83 (setq args (cons (buffer-substring begin (point)) args)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
84 (skip-chars-forward " \t\n"))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
85 ;; arguments are now in reverse order
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
86 (setq args (nreverse args))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
87 ;; make sure they're not read again
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
88 (erase-buffer))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
89 (resume-write-buffer-to-file (current-buffer) resume-emacs-args-file)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
90 ;; if nothing was in buffer, args will be null
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
91 (or (null args)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
92 (setq command-line-default-directory
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
93 (file-name-as-directory (car args))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
94 args (cdr args)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
95 ;; actually process the arguments
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
96 ;; XEmacs: command-line-1 doesn't take a parameter
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
97 (let ((command-line-args-left args))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
98 (command-line-1)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 ;; If the command line args don't result in a find-file, the
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
100 ;; buffer will be left in args-buffer. So we change back to the
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 ;; original buffer. The reason I don't just use
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 ;; (let ((default-directory foo))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 ;; (command-line-1 args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 ;; in the context of the original buffer is because let does not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 ;; work properly with buffer-local variables.
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
106 (if (eq (current-buffer) args-buffer)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
107 (set-buffer start-buffer)))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
108
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
109 ;;;###autoload
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
110 (defun resume-suspend-hook ()
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
111 "Clear out the file used for transmitting args when Emacs resumes."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
112 (save-excursion
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
113 (set-buffer (get-buffer-create resume-emacs-args-buffer))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
114 (erase-buffer)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
115 (resume-write-buffer-to-file (current-buffer) resume-emacs-args-file)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
116
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
117 (defun resume-write-buffer-to-file (buffer file)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
118 "Writes the contents of BUFFER into FILE, if permissions allow."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
119 (if (not (file-writable-p file))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
120 (error "No permission to write file %s" file))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
121 (save-excursion
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
122 (set-buffer buffer)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
123 (clear-visited-file-modtime)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
124 (save-restriction
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
125 (widen)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
126 (write-region (point-min) (point-max) file nil 'quiet))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
127 (set-buffer-modified-p nil)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
128
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
129 (provide 'resume)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
130
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
131 ;;; resume.el ends here