Switch to recommended form of GPLv3 permissions notice.
[emacs.git] / lisp / vms-patch.el
blob1cf7b6f62cb7689657f00d8a1aaa7a003644cb04
1 ;; -*- no-byte-compile: t -*-
2 ;;; vms-patch.el --- override parts of files.el for VMS
4 ;; Copyright (C) 1986, 1992, 2001, 2002, 2003, 2004,
5 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
7 ;; Maintainer: FSF
8 ;; Keywords: vms
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Code:
29 (defvar print-region-function)
31 (setq auto-mode-alist (cons '(("\\.com\\'" . dcl-mode)) auto-mode-alist))
33 ;;; Functions that need redefinition
35 ;;; VMS file names are upper case, but buffer names are more
36 ;;; convenient in lower case.
38 (defun create-file-buffer (filename)
39 "Create a suitably named buffer for visiting FILENAME, and return it.
40 FILENAME (sans directory) is used unchanged if that name is free;
41 otherwise a string <2> or <3> or ... is appended to get an unused name."
42 (generate-new-buffer (downcase (file-name-nondirectory filename))))
44 ;;; Given a string FN, return a similar name which is a valid VMS filename.
45 ;;; This is used to avoid invalid auto save file names.
46 (defun make-valid-file-name (fn)
47 (setq fn (copy-sequence fn))
48 (let ((dot nil) (indx 0) (len (length fn)) chr)
49 (while (< indx len)
50 (setq chr (aref fn indx))
51 (cond
52 ((eq chr ?.) (if dot (aset fn indx ?_) (setq dot t)))
53 ((not (or (and (>= chr ?a) (<= chr ?z)) (and (>= chr ?A) (<= chr ?Z))
54 (and (>= chr ?0) (<= chr ?9))
55 (eq chr ?$) (eq chr ?_) (and (eq chr ?-) (> indx 0))))
56 (aset fn indx ?_)))
57 (setq indx (1+ indx))))
58 fn)
60 (define-obsolete-function-alias 'make-legal-file-name 'make-valid-file-name "23.1")
62 ;;; Auto save filesnames start with _$ and end with $.
64 (defun make-auto-save-file-name ()
65 "Return file name to use for auto-saves of current buffer.
66 This function does not consider `auto-save-visited-file-name';
67 the caller should check that before calling this function.
68 This is a separate function so that your `.emacs' file or the site's
69 `site-init.el' can redefine it.
70 See also `auto-save-file-name-p'."
71 (if buffer-file-name
72 (concat (file-name-directory buffer-file-name)
73 "_$"
74 (file-name-nondirectory buffer-file-name)
75 "$")
76 (expand-file-name (concat "_$_" (make-valid-file-name (buffer-name)) "$"))))
78 (defun auto-save-file-name-p (filename)
79 "Return t if FILENAME can be yielded by `make-auto-save-file-name'.
80 FILENAME should lack slashes.
81 This is a separate function so that your `.emacs' file or the site's
82 `site-init.el' can redefine it."
83 (string-match "^_\\$.*\\$" filename))
85 ;;;
86 ;;; This goes along with kepteditor.com which defines these logicals
87 ;;; If EMACS_COMMAND_ARGS is defined, it supersedes EMACS_FILE_NAME,
88 ;;; which is probably set up incorrectly anyway.
89 ;;; The function command-line-again is a kludge, but it does the job.
90 ;;;
91 (defun vms-suspend-resume-hook ()
92 "When resuming suspended Emacs, check for file to be found.
93 If the logical name `EMACS_FILE_NAME' is defined, `find-file' that file."
94 (let ((file (vms-system-info "LOGICAL" "EMACS_FILE_NAME"))
95 (args (vms-system-info "LOGICAL" "EMACS_COMMAND_ARGS"))
96 (line (vms-system-info "LOGICAL" "EMACS_FILE_LINE")))
97 (if (not args)
98 (if file
99 (progn (find-file file)
100 (if line (goto-line (string-to-number line)))))
101 (cd (file-name-directory file))
102 (vms-command-line-again))))
104 (setq suspend-resume-hook 'vms-suspend-resume-hook)
106 (defun vms-suspend-hook ()
107 "Don't allow suspending if logical name `DONT_SUSPEND_EMACS' is defined."
108 (if (vms-system-info "LOGICAL" "DONT_SUSPEND_EMACS")
109 (error "Can't suspend this emacs"))
110 nil)
112 (setq suspend-hook 'vms-suspend-hook)
115 ;;; A kludge that allows reprocessing of the command line. This is mostly
116 ;;; to allow a spawned VMS mail process to do something reasonable when
117 ;;; used in conjunction with the modifications to sysdep.c that allow
118 ;;; Emacs to attach to a "foster" parent.
120 (defun vms-command-line-again ()
121 "Reprocess command line arguments. VMS specific.
122 Command line arguments are initialized from the logical EMACS_COMMAND_ARGS
123 which is defined by kepteditor.com. On VMS this allows attaching to a
124 spawned Emacs and doing things like \"emacs -l myfile.el -f doit\""
125 (let* ((args (downcase (vms-system-info "LOGICAL" "EMACS_COMMAND_ARGS")))
126 (command-line-args (list "emacs"))
127 (beg 0)
128 (end 0)
129 (len (length args))
130 this-char)
131 (if args
132 (progn
133 ;;; replace non-printable stuff with spaces
134 (while (< beg (length args))
135 (if (or (> 33 (setq this-char (aref args beg)))
136 (< 127 this-char))
137 (aset args beg 32))
138 (setq beg (1+ beg)))
139 (setq beg (1- (length args)))
140 (while (= 32 (aref args beg)) (setq beg (1- beg)))
141 (setq args (substring args 0 (1+ beg)))
142 (setq beg 0)
143 ;;; now start parsing args
144 (while (< beg (length args))
145 (while (and (< beg (length args))
146 (or (> 33 (setq this-char (aref args beg)))
147 (< 127 this-char))
148 (setq beg (1+ beg))))
149 (setq end (1+ beg))
150 (while (and (< end (length args))
151 (< 32 (setq this-char (aref args end)))
152 (> 127 this-char))
153 (setq end (1+ end)))
154 (setq command-line-args (append
155 command-line-args
156 (list (substring args beg end))))
157 (setq beg (1+ end)))
158 (command-line)))))
160 (defun vms-read-directory (dirname switches buffer)
161 (save-excursion
162 (set-buffer buffer)
163 (subprocess-command-to-buffer
164 (concat "DIRECTORY " switches " " dirname)
165 buffer)
166 (goto-char (point-min))
167 ;; Remove all the trailing blanks.
168 (while (search-forward " \n")
169 (forward-char -1)
170 (delete-horizontal-space))
171 (goto-char (point-min))))
173 (setq dired-listing-switches
174 "/SIZE/DATE/OWNER/WIDTH=(FILENAME=32,SIZE=5)")
176 (setq print-region-function
177 (lambda (start end command ign1 ign2 ign3 &rest switches)
178 (write-region start end "sys$login:delete-me.txt")
179 (send-command-to-subprocess
181 (concat command
182 " sys$login:delete-me.txt/name=\"GNUprintbuffer\" "
183 (mapconcat 'identity switches " "))
184 nil nil nil)))
187 ;;; Fuctions for using Emacs as a VMS Mail editor
189 (autoload 'vms-pmail-setup "vms-pmail"
190 "Set up file assuming use by VMS Mail utility.
191 The buffer is put into text-mode, auto-save is turned off and the
192 following bindings are established.
194 \\[vms-pmail-save-and-exit] vms-pmail-save-and-exit
195 \\[vms-pmail-abort] vms-pmail-abort
197 All other Emacs commands are still available."
201 ;;; Filename handling in the minibuffer
203 (defun vms-magic-right-square-brace ()
205 Insert a right square brace, but do other things first depending on context.
206 During filename completion, when point is at the end of the line and the
207 character before is not a right square brace, do one of three things before
208 inserting the brace:
209 - If there are already two left square braces preceding, do nothing special.
210 - If there is a previous right-square-brace, convert it to dot.
211 - If the character before is dot, delete it.
212 Additionally, if the preceding chars are right-square-brace followed by
213 either \"-\" or \"..\", strip one level of directory hierarchy."
214 (interactive)
215 (when (and minibuffer-completing-file-name
216 (= (point) (point-max))
217 (not (= 93 (char-before))))
218 (cond
219 ;; Avoid clobbering: user:[one.path][another.path
220 ((search-backward "[" (field-beginning) t 2))
221 ((search-backward "]" (field-beginning) t)
222 (delete-char 1)
223 (insert ".")
224 (goto-char (point-max)))
225 ((= ?. (char-before))
226 (delete-char -1)))
227 (goto-char (point-max))
228 (let ((specs '(".." "-"))
229 (pmax (point-max)))
230 (while specs
231 (let* ((up (car specs))
232 (len (length up))
233 (cut (- (point) len)))
234 (when (and (< (1+ len) pmax)
235 (= ?. (char-before cut))
236 (string= up (buffer-substring cut (point))))
237 (delete-char (- (1+ len)))
238 (while (not (let ((c (char-before)))
239 (or (= ?. c) (= 91 c))))
240 (delete-char -1))
241 (when (= ?. (char-before)) (delete-char -1))
242 (setq specs nil)))
243 (setq specs (cdr specs)))))
244 (insert "]"))
246 (defun vms-magic-colon ()
248 Insert a colon, but do other things first depending on context.
249 During filename completion, when point is at the end of the line
250 and the line contains a right square brace, remove all characters
251 from the beginning of the line up to and including such brace.
252 This enables one to type a new filespec without having to delete
253 the old one."
254 (interactive)
255 (when (and minibuffer-completing-file-name
256 (= (point) (point-max))
257 (search-backward "]" (field-beginning) t))
258 (delete-region (field-beginning) (1+ (point)))
259 (goto-char (point-max)))
260 (insert ":"))
262 (let ((m minibuffer-local-completion-map))
263 (define-key m "]" 'vms-magic-right-square-brace)
264 (define-key m "/" 'vms-magic-right-square-brace)
265 (define-key m ":" 'vms-magic-colon))
267 ;; arch-tag: c178494e-2c37-4d02-99b7-e47e615656cf
268 ;;; vms-patch.el ends here