Mercurial > emacs
annotate lisp/files-x.el @ 111428:043aac1cda3c
Minor idlwave change.
* lisp/progmodes/idlwave.el (idlwave-routine-entry-compare-twins):
(idlwave-study-twins): Prefix dynamic local `class'.
(idlwave-routine-twin-compare): Update for above name change.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Sat, 06 Nov 2010 12:55:44 -0700 |
parents | 280c8ae2476d |
children | 417b1e4d63cd |
rev | line source |
---|---|
104236 | 1 ;;; files-x.el --- extended file handling commands |
2 | |
106815 | 3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc. |
104236 | 4 |
5 ;; Author: Juri Linkov <juri@jurta.org> | |
6 ;; Maintainer: FSF | |
7 ;; Keywords: files | |
110015
280c8ae2476d
Add "Package:" file headers to denote built-in packages.
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
8 ;; Package: emacs |
104236 | 9 |
10 ;; This file is part of GNU Emacs. | |
11 | |
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. | |
16 | |
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. | |
21 | |
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/>. | |
24 | |
25 ;;; Commentary: | |
26 | |
27 ;; This file defines additional infrequently used file- and | |
28 ;; directory-handling commands that should not be in files.el | |
29 ;; to not make the dumped image bigger. | |
30 | |
31 ;;; Code: | |
32 | |
33 | |
34 ;;; Commands to add/delete file-local/directory-local variables. | |
35 | |
36 (defun read-file-local-variable (prompt) | |
37 "Read file-local variable using PROMPT and completion. | |
38 Intended to be used in the `interactive' spec of | |
39 `add-file-local-variable', `delete-file-local-variable', | |
40 `add-dir-local-variable', `delete-dir-local-variable'." | |
41 (let (default variable) | |
42 (setq default (variable-at-point)) | |
43 (setq default (and (symbolp default) (boundp default) | |
44 (symbol-name default))) | |
45 (setq variable | |
46 (completing-read | |
47 (if default | |
48 (format "%s (default %s): " prompt default) | |
49 (format "%s: " prompt)) | |
50 obarray | |
51 (lambda (sym) | |
52 (or (user-variable-p sym) | |
105513
1bfde4a3bd05
(read-file-local-variable): Include some
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
105423
diff
changeset
|
53 (get sym 'safe-local-variable) |
104236 | 54 (memq sym '(mode eval coding unibyte)))) |
55 nil nil nil default nil)) | |
56 (and (stringp variable) (intern variable)))) | |
57 | |
58 (defun read-file-local-variable-value (variable) | |
59 "Read value of file-local VARIABLE using completion. | |
60 Intended to be used in the `interactive' spec of | |
61 `add-file-local-variable' and `add-dir-local-variable'." | |
62 (let (default value) | |
63 (cond | |
64 ((eq variable 'mode) | |
65 (setq default (and (symbolp major-mode) (symbol-name major-mode))) | |
66 (setq value | |
67 (completing-read | |
68 (if default | |
69 (format "Add %s with value (default %s): " variable default) | |
70 (format "Add %s with value: " variable)) | |
71 obarray | |
72 (lambda (sym) | |
105588
2586fdd484bd
(read-file-local-variable-value): Don't filter out
Juri Linkov <juri@jurta.org>
parents:
105555
diff
changeset
|
73 (string-match-p "-mode\\'" (symbol-name sym))) |
104236 | 74 nil nil nil default nil)) |
75 (and (stringp value) | |
76 (intern (replace-regexp-in-string "-mode\\'" "" value)))) | |
77 ((eq variable 'eval) | |
78 (let ((minibuffer-completing-symbol t)) | |
79 (read-from-minibuffer (format "Add %s with expression: " variable) | |
80 nil read-expression-map t | |
81 'read-expression-history))) | |
82 ((eq variable 'coding) | |
83 (setq default (and (symbolp buffer-file-coding-system) | |
84 (symbol-name buffer-file-coding-system))) | |
85 (read-coding-system | |
86 (if default | |
87 (format "Add %s with value (default %s): " variable default) | |
88 (format "Add %s with value: " variable)) | |
89 default)) | |
90 (t | |
91 (read (read-string (format "Add %s with value: " variable) | |
92 nil 'set-variable-value-history | |
93 (format "%S" | |
94 (cond ((eq variable 'unibyte) t) | |
105555
627cf97fdea2
(read-file-local-variable-value):
Juri Linkov <juri@jurta.org>
parents:
105513
diff
changeset
|
95 ((boundp variable) |
627cf97fdea2
(read-file-local-variable-value):
Juri Linkov <juri@jurta.org>
parents:
105513
diff
changeset
|
96 (symbol-value variable)))))))))) |
104236 | 97 |
98 (defun read-file-local-variable-mode () | |
99 "Read per-directory file-local variable's mode using completion. | |
100 Intended to be used in the `interactive' spec of | |
101 `add-dir-local-variable', `delete-dir-local-variable'." | |
102 (let* ((default (and (symbolp major-mode) (symbol-name major-mode))) | |
103 (mode | |
104 (completing-read | |
105 (if default | |
106 (format "Mode or subdirectory (default %s): " default) | |
107 (format "Mode or subdirectory: ")) | |
108 obarray | |
109 (lambda (sym) | |
110 (and (string-match-p "-mode\\'" (symbol-name sym)) | |
111 (not (string-match-p "-minor-mode\\'" (symbol-name sym))))) | |
112 nil nil nil default nil))) | |
113 (cond | |
114 ((equal mode "nil") nil) | |
115 ((and (stringp mode) (fboundp (intern mode))) (intern mode)) | |
116 (t mode)))) | |
117 | |
118 (defun modify-file-local-variable (variable value op) | |
119 "Modify file-local VARIABLE in Local Variables depending on operation OP. | |
120 | |
121 If OP is `add-or-replace' then delete all existing settings of | |
122 VARIABLE (except `mode' and `eval') and add a new file-local VARIABLE | |
123 with VALUE to the Local Variables list. | |
124 | |
125 If there is no Local Variables list in the current file buffer and OP | |
126 is not `delete' then this function adds the first line containing the | |
127 string `Local Variables:' and the last line containing the string `End:'. | |
128 | |
129 If OP is `delete' then delete all existing settings of VARIABLE | |
130 from the Local Variables list ignoring the input argument VALUE." | |
131 (catch 'exit | |
132 (let ((beg (point)) end replaced-pos) | |
133 (unless enable-local-variables | |
134 (throw 'exit (message "File-local variables are disabled"))) | |
135 | |
136 ;; Look for "Local variables:" line in last page. | |
137 (widen) | |
138 (goto-char (point-max)) | |
139 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move) | |
140 | |
141 ;; Add "Local variables:" list if not found. | |
142 (unless (let ((case-fold-search t)) | |
143 (search-forward "Local Variables:" nil t)) | |
144 | |
145 ;; Don't add "Local variables:" list for the deletion operation. | |
146 (when (eq op 'delete) | |
147 (throw 'exit (progn (goto-char beg) | |
148 (message "Local Variables not found")))) | |
149 | |
150 (goto-char (point-max)) | |
151 (let ((comment-style 'plain) | |
152 (comment-start (or comment-start ";;; "))) | |
153 (comment-region | |
154 (prog1 (setq beg (point)) | |
155 (insert "\nLocal Variables:\nEnd:\n")) | |
156 (point))) | |
157 | |
158 (unless (let ((case-fold-search t)) | |
159 (goto-char beg) | |
160 (search-forward "Local Variables:" nil t)) | |
161 (throw 'exit (message "Can't add file-local variables")))) | |
162 | |
163 ;; prefix is what comes before "local variables:" in its line. | |
164 ;; suffix is what comes after "local variables:" in its line. | |
165 (let* ((prefix (buffer-substring (line-beginning-position) | |
166 (match-beginning 0))) | |
167 (suffix (buffer-substring (point) (line-end-position))) | |
168 (prefix-re (concat "^" (regexp-quote prefix))) | |
169 (suffix-re (concat (regexp-quote suffix) "$"))) | |
170 | |
171 ;; Find or add missing "End:". | |
172 (forward-line 1) | |
173 (setq beg (point)) | |
174 (save-excursion | |
175 (unless (let ((case-fold-search t)) | |
176 (re-search-forward | |
177 (concat prefix-re "[ \t]*End:[ \t]*" suffix-re) | |
178 nil t)) | |
179 (save-excursion | |
180 (insert (format "%sEnd:%s\n" prefix suffix)))) | |
181 (beginning-of-line) | |
182 (setq end (point-marker))) | |
183 | |
184 ;; Find and delete all existing variable/value pairs. | |
185 (when (member op '(add-or-replace delete)) | |
186 (if (and (eq op 'add-or-replace) (memq variable '(mode eval))) | |
187 (goto-char end) | |
188 (goto-char beg) | |
189 (while (re-search-forward | |
190 (format "%s%S:.*%s" prefix-re variable suffix-re) end t) | |
191 (delete-region (match-beginning 0) (1+ (match-end 0))) | |
192 (setq replaced-pos (point))))) | |
193 | |
194 ;; Add a new variable/value pair. Add `mode' to the start, add new | |
195 ;; variable to the end, and add a replaced variable to its last location. | |
196 (when (eq op 'add-or-replace) | |
197 (cond | |
198 ((eq variable 'mode) (goto-char beg)) | |
199 ((null replaced-pos) (goto-char end)) | |
200 (replaced-pos (goto-char replaced-pos))) | |
201 (insert (format "%s%S: %S%s\n" prefix variable value suffix))))))) | |
202 | |
203 ;;;###autoload | |
204 (defun add-file-local-variable (variable value) | |
205 "Add file-local VARIABLE with its VALUE to the Local Variables list. | |
206 | |
207 This command deletes all existing settings of VARIABLE (except `mode' | |
208 and `eval') and adds a new file-local VARIABLE with VALUE to the | |
209 Local Variables list. | |
210 | |
211 If there is no Local Variables list in the current file buffer | |
212 then this function adds the first line containing the string | |
213 `Local Variables:' and the last line containing the string `End:'." | |
214 (interactive | |
215 (let ((variable (read-file-local-variable "Add file-local variable"))) | |
216 (list variable (read-file-local-variable-value variable)))) | |
217 (modify-file-local-variable variable value 'add-or-replace)) | |
218 | |
219 ;;;###autoload | |
220 (defun delete-file-local-variable (variable) | |
221 "Delete all settings of file-local VARIABLE from the Local Variables list." | |
222 (interactive | |
223 (list (read-file-local-variable "Delete file-local variable"))) | |
224 (modify-file-local-variable variable nil 'delete)) | |
225 | |
226 (defun modify-file-local-variable-prop-line (variable value op) | |
227 "Modify file-local VARIABLE in the -*- line depending on operation OP. | |
228 | |
229 If OP is `add-or-replace' then delete all existing settings of | |
230 VARIABLE (except `mode' and `eval') and add a new file-local VARIABLE | |
231 with VALUE to the -*- line. | |
232 | |
233 If there is no -*- line at the beginning of the current file buffer | |
234 and OP is not `delete' then this function adds the -*- line. | |
235 | |
236 If OP is `delete' then delete all existing settings of VARIABLE | |
237 from the -*- line ignoring the input argument VALUE." | |
238 (catch 'exit | |
239 (let ((beg (point)) end replaced-pos) | |
240 (unless enable-local-variables | |
241 (throw 'exit (message "File-local variables are disabled"))) | |
242 | |
243 ;; Find the -*- line at the beginning of the current buffer. | |
244 (widen) | |
245 (goto-char (point-min)) | |
246 (setq end (set-auto-mode-1)) | |
247 | |
248 (if end | |
249 (setq beg (point-marker) end (copy-marker end)) | |
250 | |
251 ;; Add the -*- line if not found. | |
252 ;; Don't add the -*- line for the deletion operation. | |
253 (when (eq op 'delete) | |
254 (throw 'exit (progn (goto-char beg) | |
255 (message "The -*- line not found")))) | |
256 | |
257 (goto-char (point-min)) | |
258 | |
259 ;; Skip interpreter magic line "#!" | |
260 (when (looking-at "^\\(#!\\|'\\\\\"\\)") | |
261 (forward-line 1)) | |
262 | |
263 (let ((comment-style 'plain) | |
264 (comment-start (or comment-start ";;; "))) | |
265 (comment-region | |
266 (prog1 (point) | |
267 (insert "-*-") | |
268 (setq beg (point-marker)) | |
269 (setq end (point-marker)) | |
270 (insert "-*-\n")) | |
271 (point)))) | |
272 | |
273 (cond | |
274 ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)") | |
275 ;; Simple form: "-*- MODENAME -*-". | |
276 (if (eq variable 'mode) | |
277 ;; Replace or delete MODENAME | |
278 (progn | |
279 (when (member op '(add-or-replace delete)) | |
280 (delete-region (match-beginning 1) (match-end 1))) | |
281 (when (eq op 'add-or-replace) | |
282 (goto-char (match-beginning 1)) | |
283 (insert (format "%S" value)))) | |
284 ;; Else, turn `MODENAME' into `mode:MODENAME' | |
285 ;; and add `VARIABLE: VALUE;' | |
286 (goto-char (match-beginning 2)) | |
287 (insert (format "; %S: %S; " variable value)) | |
288 (goto-char (match-beginning 1)) | |
289 (insert " mode: "))) | |
290 | |
291 (t | |
292 ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-' | |
293 ;; Find and delete all existing variable/value pairs. | |
294 (when (member op '(add-or-replace delete)) | |
295 (if (and (eq op 'add-or-replace) (memq variable '(mode eval))) | |
296 (goto-char end) | |
297 (goto-char beg) | |
298 (while (< (point) end) | |
299 (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*") | |
300 (throw 'exit (message "Malformed -*- line"))) | |
301 (goto-char (match-end 0)) | |
302 (let ((key (intern (match-string 1))) | |
303 (val (save-restriction | |
304 (narrow-to-region (point) end) | |
305 (let ((read-circle nil)) | |
306 (read (current-buffer)))))) | |
307 (skip-chars-forward " \t;") | |
308 (when (eq key variable) | |
309 (delete-region (match-beginning 0) (point)) | |
310 (setq replaced-pos (point))))))) | |
311 ;; Add a new variable/value pair. Add `mode' to the start, add new | |
312 ;; variable to the end, and add a replaced variable to its last location. | |
313 (when (eq op 'add-or-replace) | |
314 (cond | |
315 ((eq variable 'mode) (goto-char beg)) | |
316 ((null replaced-pos) (goto-char end)) | |
317 (replaced-pos (goto-char replaced-pos))) | |
318 (if (and (not (eq (char-before) ?\;)) | |
319 (not (equal (point) (marker-position beg)))) | |
320 (insert ";")) | |
321 (unless (eq (char-before) ?\s) (insert " ")) | |
322 (insert (format "%S: %S;" variable value)) | |
323 (unless (eq (char-after) ?\s) (insert " ")))))))) | |
324 | |
325 ;;;###autoload | |
326 (defun add-file-local-variable-prop-line (variable value) | |
327 "Add file-local VARIABLE with its VALUE to the -*- line. | |
328 | |
329 This command deletes all existing settings of VARIABLE (except `mode' | |
330 and `eval') and adds a new file-local VARIABLE with VALUE to | |
331 the -*- line. | |
332 | |
333 If there is no -*- line at the beginning of the current file buffer | |
334 then this function adds it." | |
335 (interactive | |
336 (let ((variable (read-file-local-variable "Add -*- file-local variable"))) | |
337 (list variable (read-file-local-variable-value variable)))) | |
338 (modify-file-local-variable-prop-line variable value 'add-or-replace)) | |
339 | |
340 ;;;###autoload | |
341 (defun delete-file-local-variable-prop-line (variable) | |
342 "Delete all settings of file-local VARIABLE from the -*- line." | |
343 (interactive | |
344 (list (read-file-local-variable "Delete -*- file-local variable"))) | |
345 (modify-file-local-variable-prop-line variable nil 'delete)) | |
346 | |
347 (defun modify-dir-local-variable (mode variable value op) | |
348 "Modify directory-local VARIABLE in .dir-locals.el depending on operation OP. | |
349 | |
350 If OP is `add-or-replace' then delete all existing settings of | |
351 VARIABLE (except `mode' and `eval') and add a new directory-local VARIABLE | |
352 with VALUE to the MODE alist where MODE can be a mode name symbol or | |
353 a subdirectory name. | |
354 | |
355 If .dir-locals.el was not found and OP is not `delete' then create | |
356 this file in the current directory. | |
357 | |
358 If OP is `delete' then delete all existing settings of VARIABLE | |
105423
309bc750556d
* files-x.el (modify-dir-local-variable)
Juanma Barranquero <lekktu@gmail.com>
parents:
104264
diff
changeset
|
359 from the MODE alist ignoring the input argument VALUE." |
104236 | 360 (catch 'exit |
361 (unless enable-local-variables | |
362 (throw 'exit (message "Directory-local variables are disabled"))) | |
363 | |
364 (let ((variables-file (or (and (buffer-file-name) | |
365 (not (file-remote-p (buffer-file-name))) | |
366 (dir-locals-find-file (buffer-file-name))) | |
367 dir-locals-file)) | |
368 variables) | |
369 | |
370 ;; Don't create ".dir-locals.el" for the deletion operation. | |
371 (when (and (eq op 'delete) | |
372 (not (file-exists-p variables-file))) | |
373 (throw 'exit (message "File .dir-locals.el not found"))) | |
374 | |
375 (let ((auto-insert nil)) | |
376 (find-file variables-file)) | |
377 (widen) | |
378 (goto-char (point-min)) | |
379 | |
380 ;; Read alist of directory-local variables. | |
381 (ignore-errors | |
382 (delete-region | |
383 (prog1 (point) | |
384 (setq variables (let ((read-circle nil)) | |
385 (read (current-buffer))))) | |
386 (point))) | |
387 | |
388 ;; Add or replace variable in alist of directory-local variables. | |
389 (let ((mode-assoc (assoc mode variables))) | |
390 (if mode-assoc | |
391 (setq variables | |
392 (cons (cons mode | |
393 (if (eq op 'delete) | |
394 (assq-delete-all variable (cdr mode-assoc)) | |
395 (cons | |
396 (cons variable value) | |
397 (if (memq variable '(mode eval)) | |
398 (cdr mode-assoc) | |
399 (assq-delete-all variable (cdr mode-assoc)))))) | |
400 (assq-delete-all mode variables))) | |
401 (setq variables | |
402 (cons `(,mode . ((,variable . ,value))) | |
403 variables)))) | |
404 | |
405 ;; Insert modified alist of directory-local variables. | |
406 (insert ";;; Directory Local Variables\n") | |
407 (insert ";;; See Info node `(emacs) Directory Variables' for more information.\n\n") | |
408 (pp (sort variables | |
409 (lambda (a b) | |
410 (cond | |
411 ((null (car a)) t) | |
412 ((null (car b)) nil) | |
413 ((and (symbolp (car a)) (stringp (car b))) t) | |
414 ((and (symbolp (car b)) (stringp (car a))) nil) | |
415 (t (string< (car a) (car b)))))) | |
416 (current-buffer))))) | |
417 | |
418 ;;;###autoload | |
419 (defun add-dir-local-variable (mode variable value) | |
420 "Add directory-local VARIABLE with its VALUE and MODE to .dir-locals.el." | |
421 (interactive | |
422 (let (variable) | |
423 (list | |
424 (read-file-local-variable-mode) | |
425 (setq variable (read-file-local-variable "Add directory-local variable")) | |
426 (read-file-local-variable-value variable)))) | |
427 (modify-dir-local-variable mode variable value 'add-or-replace)) | |
428 | |
429 ;;;###autoload | |
430 (defun delete-dir-local-variable (mode variable) | |
431 "Delete all MODE settings of file-local VARIABLE from .dir-locals.el." | |
432 (interactive | |
433 (list | |
434 (read-file-local-variable-mode) | |
435 (read-file-local-variable "Delete directory-local variable"))) | |
436 (modify-dir-local-variable mode variable nil 'delete)) | |
437 | |
438 ;;;###autoload | |
439 (defun copy-file-locals-to-dir-locals () | |
440 "Copy file-local variables to .dir-locals.el." | |
441 (interactive) | |
442 (dolist (elt file-local-variables-alist) | |
443 (unless (assq (car elt) dir-local-variables-alist) | |
444 (add-dir-local-variable major-mode (car elt) (cdr elt))))) | |
445 | |
446 ;;;###autoload | |
447 (defun copy-dir-locals-to-file-locals () | |
448 "Copy directory-local variables to the Local Variables list." | |
449 (interactive) | |
450 (dolist (elt dir-local-variables-alist) | |
451 (add-file-local-variable (car elt) (cdr elt)))) | |
452 | |
453 ;;;###autoload | |
454 (defun copy-dir-locals-to-file-locals-prop-line () | |
105423
309bc750556d
* files-x.el (modify-dir-local-variable)
Juanma Barranquero <lekktu@gmail.com>
parents:
104264
diff
changeset
|
455 "Copy directory-local variables to the -*- line." |
104236 | 456 (interactive) |
457 (dolist (elt dir-local-variables-alist) | |
458 (add-file-local-variable-prop-line (car elt) (cdr elt)))) | |
459 | |
460 | |
461 | |
462 (provide 'files-x) | |
104264 | 463 |
464 ;; arch-tag: 949d263c-30a8-4b49-af26-cda97c7c5477 | |
104236 | 465 ;;; files-x.el ends here |