Commit | Line | Data |
---|---|---|
752fac18 GM |
1 | ;;; resume.el --- process command line args from within a suspended Emacs job |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 1992, 2001-2011 Free Software Foundation, Inc. |
752fac18 GM |
4 | |
5 | ;; Author: Joe Wells <jbw@bucsf.bu.edu> | |
6 | ;; Adapted-By: ESR | |
7 | ;; Keywords: processes | |
8 | ||
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 | |
13 | ;; the Free Software Foundation, either version 3 of the License, or | |
14 | ;; (at your option) any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
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 | |
22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
23 | ||
24 | ;;; Commentary: | |
25 | ||
26 | ;; This file has been obsolete since Emacs 23.1. | |
27 | ||
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. | |
44 | ||
45 | ;; Stephan Gildea suggested bug fix (gildea@bbn.com). | |
46 | ;; Ideas from Michael DeCorte and other people. | |
47 | ||
48 | ;;; Code: | |
49 | ||
50 | (defvar resume-emacs-args-file (expand-file-name "~/.emacs_args") | |
51 | "*This file is where arguments are placed for a suspended Emacs job.") | |
52 | ||
53 | (defvar resume-emacs-args-buffer " *Command Line Args*" | |
54 | "Buffer that is used by `resume-process-args'.") | |
55 | ||
56 | (defun resume-process-args () | |
57 | "Handler for command line args given when Emacs is resumed." | |
58 | (let ((start-buffer (current-buffer)) | |
59 | (args-buffer (get-buffer-create resume-emacs-args-buffer)) | |
60 | length args | |
61 | (command-line-default-directory default-directory)) | |
62 | (unwind-protect | |
63 | (progn | |
64 | (set-buffer args-buffer) | |
65 | (erase-buffer) | |
66 | ;; get the contents of resume-emacs-args-file | |
67 | (condition-case () | |
68 | (let ((result (insert-file-contents resume-emacs-args-file))) | |
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 | |
87 | (erase-buffer)) | |
88 | (resume-write-buffer-to-file (current-buffer) resume-emacs-args-file) | |
89 | ;; if nothing was in buffer, args will be null | |
90 | (or (null args) | |
91 | (setq command-line-default-directory | |
92 | (file-name-as-directory (car args)) | |
93 | args (cdr args))) | |
94 | ;; actually process the arguments | |
95 | (command-line-1 args)) | |
96 | ;; If the command line args don't result in a find-file, the | |
97 | ;; buffer will be left in args-buffer. So we change back to the | |
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. | |
103 | (if (eq (current-buffer) args-buffer) | |
104 | (set-buffer start-buffer))))) | |
105 | ||
106 | ;;;###autoload | |
107 | (defun resume-suspend-hook () | |
108 | "Clear out the file used for transmitting args when Emacs resumes." | |
9a529312 | 109 | (with-current-buffer (get-buffer-create resume-emacs-args-buffer) |
752fac18 GM |
110 | (erase-buffer) |
111 | (resume-write-buffer-to-file (current-buffer) resume-emacs-args-file))) | |
112 | ||
113 | (defun resume-write-buffer-to-file (buffer file) | |
114 | "Writes the contents of BUFFER into FILE, if permissions allow." | |
115 | (if (not (file-writable-p file)) | |
116 | (error "No permission to write file %s" file)) | |
9a529312 | 117 | (with-current-buffer buffer |
752fac18 GM |
118 | (clear-visited-file-modtime) |
119 | (save-restriction | |
120 | (widen) | |
121 | (write-region (point-min) (point-max) file nil 'quiet)) | |
122 | (set-buffer-modified-p nil))) | |
123 | ||
124 | (provide 'resume) | |
125 | ||
752fac18 | 126 | ;;; resume.el ends here |