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