1 ;;; files-x.el --- extended file handling commands
3 ;; Copyright (C) 2009 Free Software Foundation, Inc.
5 ;; Author: Juri Linkov <juri@jurta.org>
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;; This file defines additional infrequently used file- and
27 ;; directory-handling commands that should not be in files.el
28 ;; to not make the dumped image bigger.
33 ;;; Commands to add/delete file-local/directory-local variables.
35 (defun read-file-local-variable (prompt)
36 "Read file-local variable using PROMPT and completion.
37 Intended to be used in the `interactive' spec of
38 `add-file-local-variable', `delete-file-local-variable',
39 `add-dir-local-variable', `delete-dir-local-variable'."
40 (let (default variable
)
41 (setq default
(variable-at-point))
42 (setq default
(and (symbolp default
) (boundp default
)
43 (symbol-name default
)))
47 (format "%s (default %s): " prompt default
)
48 (format "%s: " prompt
))
51 (or (user-variable-p sym
)
52 (memq sym
'(mode eval coding unibyte
))))
53 nil nil nil default nil
))
54 (and (stringp variable
) (intern variable
))))
56 (defun read-file-local-variable-value (variable)
57 "Read value of file-local VARIABLE using completion.
58 Intended to be used in the `interactive' spec of
59 `add-file-local-variable' and `add-dir-local-variable'."
63 (setq default
(and (symbolp major-mode
) (symbol-name major-mode
)))
67 (format "Add %s with value (default %s): " variable default
)
68 (format "Add %s with value: " variable
))
71 (and (string-match-p "-mode\\'" (symbol-name sym
))
72 (not (string-match-p "-minor-mode\\'" (symbol-name sym
)))))
73 nil nil nil default nil
))
75 (intern (replace-regexp-in-string "-mode\\'" "" value
))))
77 (let ((minibuffer-completing-symbol t
))
78 (read-from-minibuffer (format "Add %s with expression: " variable
)
79 nil read-expression-map t
80 'read-expression-history
)))
81 ((eq variable
'coding
)
82 (setq default
(and (symbolp buffer-file-coding-system
)
83 (symbol-name buffer-file-coding-system
)))
86 (format "Add %s with value (default %s): " variable default
)
87 (format "Add %s with value: " variable
))
90 (read (read-string (format "Add %s with value: " variable
)
91 nil
'set-variable-value-history
93 (cond ((eq variable
'unibyte
) t
)
94 (t (symbol-value variable
))))))))))
96 (defun read-file-local-variable-mode ()
97 "Read per-directory file-local variable's mode using completion.
98 Intended to be used in the `interactive' spec of
99 `add-dir-local-variable', `delete-dir-local-variable'."
100 (let* ((default (and (symbolp major-mode
) (symbol-name major-mode
)))
104 (format "Mode or subdirectory (default %s): " default
)
105 (format "Mode or subdirectory: "))
108 (and (string-match-p "-mode\\'" (symbol-name sym
))
109 (not (string-match-p "-minor-mode\\'" (symbol-name sym
)))))
110 nil nil nil default nil
)))
112 ((equal mode
"nil") nil
)
113 ((and (stringp mode
) (fboundp (intern mode
))) (intern mode
))
116 (defun modify-file-local-variable (variable value op
)
117 "Modify file-local VARIABLE in Local Variables depending on operation OP.
119 If OP is `add-or-replace' then delete all existing settings of
120 VARIABLE (except `mode' and `eval') and add a new file-local VARIABLE
121 with VALUE to the Local Variables list.
123 If there is no Local Variables list in the current file buffer and OP
124 is not `delete' then this function adds the first line containing the
125 string `Local Variables:' and the last line containing the string `End:'.
127 If OP is `delete' then delete all existing settings of VARIABLE
128 from the Local Variables list ignoring the input argument VALUE."
130 (let ((beg (point)) end replaced-pos
)
131 (unless enable-local-variables
132 (throw 'exit
(message "File-local variables are disabled")))
134 ;; Look for "Local variables:" line in last page.
136 (goto-char (point-max))
137 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move
)
139 ;; Add "Local variables:" list if not found.
140 (unless (let ((case-fold-search t
))
141 (search-forward "Local Variables:" nil t
))
143 ;; Don't add "Local variables:" list for the deletion operation.
144 (when (eq op
'delete
)
145 (throw 'exit
(progn (goto-char beg
)
146 (message "Local Variables not found"))))
148 (goto-char (point-max))
149 (let ((comment-style 'plain
)
150 (comment-start (or comment-start
";;; ")))
152 (prog1 (setq beg
(point))
153 (insert "\nLocal Variables:\nEnd:\n"))
156 (unless (let ((case-fold-search t
))
158 (search-forward "Local Variables:" nil t
))
159 (throw 'exit
(message "Can't add file-local variables"))))
161 ;; prefix is what comes before "local variables:" in its line.
162 ;; suffix is what comes after "local variables:" in its line.
163 (let* ((prefix (buffer-substring (line-beginning-position)
164 (match-beginning 0)))
165 (suffix (buffer-substring (point) (line-end-position)))
166 (prefix-re (concat "^" (regexp-quote prefix
)))
167 (suffix-re (concat (regexp-quote suffix
) "$")))
169 ;; Find or add missing "End:".
173 (unless (let ((case-fold-search t
))
175 (concat prefix-re
"[ \t]*End:[ \t]*" suffix-re
)
178 (insert (format "%sEnd:%s\n" prefix suffix
))))
180 (setq end
(point-marker)))
182 ;; Find and delete all existing variable/value pairs.
183 (when (member op
'(add-or-replace delete
))
184 (if (and (eq op
'add-or-replace
) (memq variable
'(mode eval
)))
187 (while (re-search-forward
188 (format "%s%S:.*%s" prefix-re variable suffix-re
) end t
)
189 (delete-region (match-beginning 0) (1+ (match-end 0)))
190 (setq replaced-pos
(point)))))
192 ;; Add a new variable/value pair. Add `mode' to the start, add new
193 ;; variable to the end, and add a replaced variable to its last location.
194 (when (eq op
'add-or-replace
)
196 ((eq variable
'mode
) (goto-char beg
))
197 ((null replaced-pos
) (goto-char end
))
198 (replaced-pos (goto-char replaced-pos
)))
199 (insert (format "%s%S: %S%s\n" prefix variable value suffix
)))))))
202 (defun add-file-local-variable (variable value
)
203 "Add file-local VARIABLE with its VALUE to the Local Variables list.
205 This command deletes all existing settings of VARIABLE (except `mode'
206 and `eval') and adds a new file-local VARIABLE with VALUE to the
207 Local Variables list.
209 If there is no Local Variables list in the current file buffer
210 then this function adds the first line containing the string
211 `Local Variables:' and the last line containing the string `End:'."
213 (let ((variable (read-file-local-variable "Add file-local variable")))
214 (list variable
(read-file-local-variable-value variable
))))
215 (modify-file-local-variable variable value
'add-or-replace
))
218 (defun delete-file-local-variable (variable)
219 "Delete all settings of file-local VARIABLE from the Local Variables list."
221 (list (read-file-local-variable "Delete file-local variable")))
222 (modify-file-local-variable variable nil
'delete
))
224 (defun modify-file-local-variable-prop-line (variable value op
)
225 "Modify file-local VARIABLE in the -*- line depending on operation OP.
227 If OP is `add-or-replace' then delete all existing settings of
228 VARIABLE (except `mode' and `eval') and add a new file-local VARIABLE
229 with VALUE to the -*- line.
231 If there is no -*- line at the beginning of the current file buffer
232 and OP is not `delete' then this function adds the -*- line.
234 If OP is `delete' then delete all existing settings of VARIABLE
235 from the -*- line ignoring the input argument VALUE."
237 (let ((beg (point)) end replaced-pos
)
238 (unless enable-local-variables
239 (throw 'exit
(message "File-local variables are disabled")))
241 ;; Find the -*- line at the beginning of the current buffer.
243 (goto-char (point-min))
244 (setq end
(set-auto-mode-1))
247 (setq beg
(point-marker) end
(copy-marker end
))
249 ;; Add the -*- line if not found.
250 ;; Don't add the -*- line for the deletion operation.
251 (when (eq op
'delete
)
252 (throw 'exit
(progn (goto-char beg
)
253 (message "The -*- line not found"))))
255 (goto-char (point-min))
257 ;; Skip interpreter magic line "#!"
258 (when (looking-at "^\\(#!\\|'\\\\\"\\)")
261 (let ((comment-style 'plain
)
262 (comment-start (or comment-start
";;; ")))
266 (setq beg
(point-marker))
267 (setq end
(point-marker))
272 ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)")
273 ;; Simple form: "-*- MODENAME -*-".
274 (if (eq variable
'mode
)
275 ;; Replace or delete MODENAME
277 (when (member op
'(add-or-replace delete
))
278 (delete-region (match-beginning 1) (match-end 1)))
279 (when (eq op
'add-or-replace
)
280 (goto-char (match-beginning 1))
281 (insert (format "%S" value
))))
282 ;; Else, turn `MODENAME' into `mode:MODENAME'
283 ;; and add `VARIABLE: VALUE;'
284 (goto-char (match-beginning 2))
285 (insert (format "; %S: %S; " variable value
))
286 (goto-char (match-beginning 1))
290 ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-'
291 ;; Find and delete all existing variable/value pairs.
292 (when (member op
'(add-or-replace delete
))
293 (if (and (eq op
'add-or-replace
) (memq variable
'(mode eval
)))
296 (while (< (point) end
)
297 (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*")
298 (throw 'exit
(message "Malformed -*- line")))
299 (goto-char (match-end 0))
300 (let ((key (intern (match-string 1)))
301 (val (save-restriction
302 (narrow-to-region (point) end
)
303 (let ((read-circle nil
))
304 (read (current-buffer))))))
305 (skip-chars-forward " \t;")
306 (when (eq key variable
)
307 (delete-region (match-beginning 0) (point))
308 (setq replaced-pos
(point)))))))
309 ;; Add a new variable/value pair. Add `mode' to the start, add new
310 ;; variable to the end, and add a replaced variable to its last location.
311 (when (eq op
'add-or-replace
)
313 ((eq variable
'mode
) (goto-char beg
))
314 ((null replaced-pos
) (goto-char end
))
315 (replaced-pos (goto-char replaced-pos
)))
316 (if (and (not (eq (char-before) ?\
;))
317 (not (equal (point) (marker-position beg
))))
319 (unless (eq (char-before) ?\s
) (insert " "))
320 (insert (format "%S: %S;" variable value
))
321 (unless (eq (char-after) ?\s
) (insert " "))))))))
324 (defun add-file-local-variable-prop-line (variable value
)
325 "Add file-local VARIABLE with its VALUE to the -*- line.
327 This command deletes all existing settings of VARIABLE (except `mode'
328 and `eval') and adds a new file-local VARIABLE with VALUE to
331 If there is no -*- line at the beginning of the current file buffer
332 then this function adds it."
334 (let ((variable (read-file-local-variable "Add -*- file-local variable")))
335 (list variable
(read-file-local-variable-value variable
))))
336 (modify-file-local-variable-prop-line variable value
'add-or-replace
))
339 (defun delete-file-local-variable-prop-line (variable)
340 "Delete all settings of file-local VARIABLE from the -*- line."
342 (list (read-file-local-variable "Delete -*- file-local variable")))
343 (modify-file-local-variable-prop-line variable nil
'delete
))
345 (defun modify-dir-local-variable (mode variable value op
)
346 "Modify directory-local VARIABLE in .dir-locals.el depending on operation OP.
348 If OP is `add-or-replace' then delete all existing settings of
349 VARIABLE (except `mode' and `eval') and add a new directory-local VARIABLE
350 with VALUE to the MODE alist where MODE can be a mode name symbol or
353 If .dir-locals.el was not found and OP is not `delete' then create
354 this file in the current directory.
356 If OP is `delete' then delete all existing settings of VARIABLE
357 from the the MODE alist ignoring the input argument VALUE."
359 (unless enable-local-variables
360 (throw 'exit
(message "Directory-local variables are disabled")))
362 (let ((variables-file (or (and (buffer-file-name)
363 (not (file-remote-p (buffer-file-name)))
364 (dir-locals-find-file (buffer-file-name)))
368 ;; Don't create ".dir-locals.el" for the deletion operation.
369 (when (and (eq op
'delete
)
370 (not (file-exists-p variables-file
)))
371 (throw 'exit
(message "File .dir-locals.el not found")))
373 (let ((auto-insert nil
))
374 (find-file variables-file
))
376 (goto-char (point-min))
378 ;; Read alist of directory-local variables.
382 (setq variables
(let ((read-circle nil
))
383 (read (current-buffer)))))
386 ;; Add or replace variable in alist of directory-local variables.
387 (let ((mode-assoc (assoc mode variables
)))
392 (assq-delete-all variable
(cdr mode-assoc
))
394 (cons variable value
)
395 (if (memq variable
'(mode eval
))
397 (assq-delete-all variable
(cdr mode-assoc
))))))
398 (assq-delete-all mode variables
)))
400 (cons `(,mode .
((,variable .
,value
)))
403 ;; Insert modified alist of directory-local variables.
404 (insert ";;; Directory Local Variables\n")
405 (insert ";;; See Info node `(emacs) Directory Variables' for more information.\n\n")
411 ((and (symbolp (car a
)) (stringp (car b
))) t
)
412 ((and (symbolp (car b
)) (stringp (car a
))) nil
)
413 (t (string< (car a
) (car b
))))))
417 (defun add-dir-local-variable (mode variable value
)
418 "Add directory-local VARIABLE with its VALUE and MODE to .dir-locals.el."
422 (read-file-local-variable-mode)
423 (setq variable
(read-file-local-variable "Add directory-local variable"))
424 (read-file-local-variable-value variable
))))
425 (modify-dir-local-variable mode variable value
'add-or-replace
))
428 (defun delete-dir-local-variable (mode variable
)
429 "Delete all MODE settings of file-local VARIABLE from .dir-locals.el."
432 (read-file-local-variable-mode)
433 (read-file-local-variable "Delete directory-local variable")))
434 (modify-dir-local-variable mode variable nil
'delete
))
437 (defun copy-file-locals-to-dir-locals ()
438 "Copy file-local variables to .dir-locals.el."
440 (dolist (elt file-local-variables-alist
)
441 (unless (assq (car elt
) dir-local-variables-alist
)
442 (add-dir-local-variable major-mode
(car elt
) (cdr elt
)))))
445 (defun copy-dir-locals-to-file-locals ()
446 "Copy directory-local variables to the Local Variables list."
448 (dolist (elt dir-local-variables-alist
)
449 (add-file-local-variable (car elt
) (cdr elt
))))
452 (defun copy-dir-locals-to-file-locals-prop-line ()
453 "Copy directory-local variables to the the -*- line."
455 (dolist (elt dir-local-variables-alist
)
456 (add-file-local-variable-prop-line (car elt
) (cdr elt
))))
462 ;; arch-tag: 949d263c-30a8-4b49-af26-cda97c7c5477
463 ;;; files-x.el ends here