1 ;;; files-x.el --- extended file handling commands
3 ;; Copyright (C) 2009-2015 Free Software Foundation, Inc.
5 ;; Author: Juri Linkov <juri@jurta.org>
6 ;; Maintainer: emacs-devel@gnu.org
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/>.
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.
34 ;;; Commands to add/delete file-local/directory-local variables.
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-at-point))
42 (default (and (symbolp default
) (boundp default
)
43 (symbol-name default
)))
47 (format "%s (default %s): " prompt default
)
48 (format "%s: " prompt
))
51 (or (custom-variable-p sym
)
52 (get sym
'safe-local-variable
)
53 (memq sym
'(mode eval coding unibyte
))))
54 nil nil nil default nil
)))
55 (and (stringp variable
) (intern variable
))))
57 (defun read-file-local-variable-value (variable)
58 "Read value of file-local VARIABLE using completion.
59 Intended to be used in the `interactive' spec of
60 `add-file-local-variable' and `add-dir-local-variable'."
63 (let* ((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 (string-match-p "-mode\\'" (symbol-name sym
)))
72 nil nil nil default nil
)))
74 (intern (replace-regexp-in-string "-mode\\'" "" value
)))))
76 (read--expression (format "Add %s with expression: " variable
)))
77 ((eq variable
'coding
)
78 (let ((default (and (symbolp buffer-file-coding-system
)
79 (symbol-name buffer-file-coding-system
))))
82 (format "Add %s with value (default %s): " variable default
)
83 (format "Add %s with value: " variable
))
86 (let ((default (format "%S"
87 (cond ((eq variable
'unibyte
) t
)
89 (symbol-value variable
)))))
90 (minibuffer-completing-symbol t
))
91 (read-from-minibuffer (format "Add %s with value: " variable
)
92 nil read-expression-map t
93 'set-variable-value-history
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 (or (memq sym minor-mode-list
)
110 (string-match-p "-minor-mode\\'"
111 (symbol-name sym
))))))
112 nil nil nil default nil
)))
114 ((equal mode
"nil") nil
)
115 ((and (stringp mode
) (fboundp (intern mode
))) (intern mode
))
118 (defun modify-file-local-variable-message (variable value op
)
119 (let* ((not-value (make-symbol ""))
120 (old-value (cond ((eq variable
'mode
)
122 ((eq variable
'coding
)
123 buffer-file-coding-system
)
124 (t (if (and (symbolp variable
)
126 (symbol-value variable
)
128 (new-value (if (eq op
'delete
)
129 (cond ((eq variable
'mode
)
130 (default-value 'major-mode
))
131 ((eq variable
'coding
)
132 (default-value 'buffer-file-coding-system
))
133 (t (if (and (symbolp variable
)
134 (default-boundp variable
))
135 (default-value variable
)
137 (cond ((eq variable
'mode
)
138 (let ((string (format "%S" value
)))
139 (if (string-match-p "-mode\\'" string
)
141 (intern (concat string
"-mode")))))
143 (when (or (eq old-value not-value
)
144 (eq new-value not-value
)
145 (not (equal old-value new-value
)))
146 (message "%s" (substitute-command-keys
147 "For this change to take effect revisit file using \\[revert-buffer]")))))
149 (defun modify-file-local-variable (variable value op
&optional interactive
)
150 "Modify file-local VARIABLE in Local Variables depending on operation OP.
152 If OP is `add-or-replace' then delete all existing settings of
153 VARIABLE (except `mode' and `eval') and add a new file-local VARIABLE
154 with VALUE to the Local Variables list.
156 If there is no Local Variables list in the current file buffer and OP
157 is not `delete' then this function adds the first line containing the
158 string `Local Variables:' and the last line containing the string `End:'.
160 If OP is `delete' then delete all existing settings of VARIABLE
161 from the Local Variables list ignoring the input argument VALUE."
163 (let ((beg (point)) end replaced-pos
)
164 (unless enable-local-variables
165 (throw 'exit
(message "File-local variables are disabled")))
167 ;; Look for "Local variables:" line in last page.
169 (goto-char (point-max))
170 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move
)
172 ;; Add "Local variables:" list if not found.
173 (unless (let ((case-fold-search t
))
174 (search-forward "Local Variables:" nil t
))
176 ;; Don't add "Local variables:" list for the deletion operation.
177 (when (eq op
'delete
)
178 (throw 'exit
(progn (goto-char beg
)
179 (message "Local Variables not found"))))
181 (goto-char (point-max))
182 (let ((comment-style 'plain
)
183 (comment-start (or comment-start
";; ")))
185 (prog1 (setq beg
(point))
186 (insert "\nLocal Variables:\nEnd:\n"))
189 (unless (let ((case-fold-search t
))
191 (search-forward "Local Variables:" nil t
))
192 (throw 'exit
(message "Can’t add file-local variables"))))
194 ;; prefix is what comes before "local variables:" in its line.
195 ;; suffix is what comes after "local variables:" in its line.
196 (let* ((prefix (buffer-substring (line-beginning-position)
197 (match-beginning 0)))
198 (suffix (buffer-substring (point) (line-end-position)))
199 (prefix-re (concat "^" (regexp-quote prefix
)))
200 (suffix-re (concat (regexp-quote suffix
) "$")))
202 ;; Find or add missing "End:".
206 (unless (let ((case-fold-search t
))
208 (concat prefix-re
"[ \t]*End:[ \t]*" suffix-re
)
211 (insert (format "%sEnd:%s\n" prefix suffix
))))
213 (setq end
(point-marker)))
215 ;; Find and delete all existing variable/value pairs.
216 (when (member op
'(add-or-replace delete
))
217 (if (and (eq op
'add-or-replace
) (memq variable
'(mode eval
)))
220 (while (re-search-forward
221 (format "%s%S:.*%s" prefix-re variable suffix-re
) end t
)
222 (delete-region (match-beginning 0) (1+ (match-end 0)))
223 (setq replaced-pos
(point)))))
225 ;; Add a new variable/value pair. Add `mode' to the start, add new
226 ;; variable to the end, and add a replaced variable to its last location.
227 (when (eq op
'add-or-replace
)
229 ((eq variable
'mode
) (goto-char beg
))
230 ((null replaced-pos
) (goto-char end
))
231 (replaced-pos (goto-char replaced-pos
)))
232 (insert (format "%s%S: %S%s\n" prefix variable value suffix
))))
235 (modify-file-local-variable-message variable value op
)))))
238 (defun add-file-local-variable (variable value
&optional interactive
)
239 "Add file-local VARIABLE with its VALUE to the Local Variables list.
241 This command deletes all existing settings of VARIABLE (except `mode'
242 and `eval') and adds a new file-local VARIABLE with VALUE to the
243 Local Variables list.
245 If there is no Local Variables list in the current file buffer
246 then this function adds the first line containing the string
247 `Local Variables:' and the last line containing the string `End:'."
249 (let ((variable (read-file-local-variable "Add file-local variable")))
250 ;; Error before reading value.
251 (if (equal variable
'lexical-binding
)
252 (user-error "The `%s' variable must be set at the start of the file"
254 (list variable
(read-file-local-variable-value variable
) t
)))
255 (if (equal variable
'lexical-binding
)
256 (user-error "The `%s' variable must be set at the start of the file"
258 (modify-file-local-variable variable value
'add-or-replace interactive
))
261 (defun delete-file-local-variable (variable &optional interactive
)
262 "Delete all settings of file-local VARIABLE from the Local Variables list."
264 (list (read-file-local-variable "Delete file-local variable") t
))
265 (modify-file-local-variable variable nil
'delete interactive
))
267 (defun modify-file-local-variable-prop-line (variable value op
&optional interactive
)
268 "Modify file-local VARIABLE in the -*- line depending on operation OP.
270 If OP is `add-or-replace' then delete all existing settings of
271 VARIABLE (except `mode' and `eval') and add a new file-local VARIABLE
272 with VALUE to the -*- line.
274 If there is no -*- line at the beginning of the current file buffer
275 and OP is not `delete' then this function adds the -*- line.
277 If OP is `delete' then delete all existing settings of VARIABLE
278 from the -*- line ignoring the input argument VALUE."
280 (let ((beg (point)) end replaced-pos
)
281 (unless enable-local-variables
282 (throw 'exit
(message "File-local variables are disabled")))
284 ;; Find the -*- line at the beginning of the current buffer.
286 (goto-char (point-min))
287 (setq end
(set-auto-mode-1))
290 (setq beg
(point-marker) end
(copy-marker end
))
292 ;; Add the -*- line if not found.
293 ;; Don't add the -*- line for the deletion operation.
294 (when (eq op
'delete
)
295 (throw 'exit
(progn (goto-char beg
)
296 (message "The -*- line not found"))))
298 (goto-char (point-min))
300 ;; Skip interpreter magic line "#!" or XML declaration.
301 (when (or (looking-at file-auto-mode-skip
)
302 (looking-at "<\\?xml[^>\n]*>$"))
305 (let ((comment-style 'plain
)
306 (comment-start (or comment-start
";;; "))
307 (line-beg (line-beginning-position))
309 (comment-normalize-vars)
310 ;; If the first line contains a comment.
312 (and (looking-at comment-start-skip
)
313 (goto-char (match-end 0))
314 (re-search-forward comment-end-skip
)
315 (goto-char (match-beginning 0))
316 ;; Still on the same line?
317 (equal line-beg
(line-beginning-position))
319 ;; Add local variables to the end of the existing comment.
323 (setq beg
(point-marker))
324 (setq end
(point-marker))
326 ;; Otherwise, add a new comment before the first line.
330 (setq beg
(point-marker))
331 (setq end
(point-marker))
336 ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)")
337 ;; Simple form: "-*- MODENAME -*-".
338 (if (eq variable
'mode
)
339 ;; Replace or delete MODENAME
341 (when (member op
'(add-or-replace delete
))
342 (delete-region (match-beginning 1) (match-end 1)))
343 (when (eq op
'add-or-replace
)
344 (goto-char (match-beginning 1))
345 (insert (format "%S" value
))))
346 ;; Else, turn `MODENAME' into `mode:MODENAME'
347 ;; and add `VARIABLE: VALUE;'
348 (goto-char (match-beginning 2))
349 (insert (format "; %S: %S; " variable value
))
350 (goto-char (match-beginning 1))
354 ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-'
355 ;; Find and delete all existing variable/value pairs.
356 (when (member op
'(add-or-replace delete
))
357 (if (and (eq op
'add-or-replace
) (memq variable
'(mode eval
)))
360 (while (< (point) end
)
361 (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*")
362 (throw 'exit
(message "Malformed -*- line")))
363 (goto-char (match-end 0))
364 (let ((key (intern (match-string 1))))
366 (narrow-to-region (point) end
)
367 (let ((read-circle nil
))
368 (read (current-buffer))))
369 (skip-chars-forward " \t;")
370 (when (eq key variable
)
371 (delete-region (match-beginning 0) (point))
372 (setq replaced-pos
(point)))))))
373 ;; Add a new variable/value pair. Add `mode' to the start, add new
374 ;; variable to the end, and add a replaced variable to its last location.
375 (when (eq op
'add-or-replace
)
377 ((eq variable
'mode
) (goto-char beg
))
378 ((null replaced-pos
) (goto-char end
))
379 (replaced-pos (goto-char replaced-pos
)))
380 (if (and (not (eq (char-before) ?\
;))
381 (not (equal (point) (marker-position beg
)))
382 ;; When existing `-*- -*-' is empty, beg > end.
383 (not (> (marker-position beg
) (marker-position end
))))
385 (unless (eq (char-before) ?\s
) (insert " "))
386 (insert (format "%S: %S;" variable value
))
387 (unless (eq (char-after) ?\s
) (insert " ")))))
390 (modify-file-local-variable-message variable value op
)))))
393 (defun add-file-local-variable-prop-line (variable value
&optional interactive
)
394 "Add file-local VARIABLE with its VALUE to the -*- line.
396 This command deletes all existing settings of VARIABLE (except `mode'
397 and `eval') and adds a new file-local VARIABLE with VALUE to
400 If there is no -*- line at the beginning of the current file buffer
401 then this function adds it."
403 (let ((variable (read-file-local-variable "Add -*- file-local variable")))
404 (list variable
(read-file-local-variable-value variable
) t
)))
405 (modify-file-local-variable-prop-line variable value
'add-or-replace interactive
))
408 (defun delete-file-local-variable-prop-line (variable &optional interactive
)
409 "Delete all settings of file-local VARIABLE from the -*- line."
411 (list (read-file-local-variable "Delete -*- file-local variable") t
))
412 (modify-file-local-variable-prop-line variable nil
'delete interactive
))
414 (defvar auto-insert
) ; from autoinsert.el
416 (defun modify-dir-local-variable (mode variable value op
)
417 "Modify directory-local VARIABLE in .dir-locals.el depending on operation OP.
419 If OP is `add-or-replace' then delete all existing settings of
420 VARIABLE (except `mode' and `eval') and add a new directory-local VARIABLE
421 with VALUE to the MODE alist where MODE can be a mode name symbol or
424 If .dir-locals.el was not found and OP is not `delete' then create
425 this file in the current directory.
427 If OP is `delete' then delete all existing settings of VARIABLE
428 from the MODE alist ignoring the input argument VALUE."
430 (unless enable-local-variables
431 (throw 'exit
(message "Directory-local variables are disabled")))
432 (let ((variables-file (or (and (buffer-file-name)
433 (not (file-remote-p (buffer-file-name)))
434 (dir-locals-find-file (buffer-file-name)))
437 (if (consp variables-file
) ; result from cache
438 ;; If cache element has an mtime, assume it came from a file.
439 ;; Otherwise, assume it was set directly.
440 (setq variables-file
(if (nth 2 variables-file
)
441 (expand-file-name dir-locals-file
442 (car variables-file
))
443 (cadr variables-file
))))
444 ;; I can't be bothered to handle this case right now.
445 ;; Dir locals were set directly from a class. You need to
446 ;; directly modify the class in dir-locals-class-alist.
447 (and variables-file
(not (stringp variables-file
))
448 (throw 'exit
(message "Directory locals were not set from a file")))
449 ;; Don't create ".dir-locals.el" for the deletion operation.
451 (or (not variables-file
)
452 (not (file-exists-p variables-file
)))
453 (throw 'exit
(message "No .dir-locals.el file was found")))
454 (let ((auto-insert nil
))
455 (find-file variables-file
))
457 (goto-char (point-min))
459 ;; Read alist of directory-local variables.
463 (setq variables
(let ((read-circle nil
))
464 (read (current-buffer)))))
467 ;; Add or replace variable in alist of directory-local variables.
468 (let ((mode-assoc (assoc mode variables
)))
473 (assq-delete-all variable
(cdr mode-assoc
))
475 (cons variable value
)
476 (if (memq variable
'(mode eval
))
478 (assq-delete-all variable
(cdr mode-assoc
))))))
479 (assq-delete-all mode variables
)))
481 (cons `(,mode .
((,variable .
,value
)))
484 ;; Insert modified alist of directory-local variables.
485 (insert ";;; Directory Local Variables\n")
486 (insert ";;; For more information see (info \"(emacs) Directory Variables\")\n\n")
492 ((and (symbolp (car a
)) (stringp (car b
))) t
)
493 ((and (symbolp (car b
)) (stringp (car a
))) nil
)
494 (t (string< (car a
) (car b
))))))
498 (defun add-dir-local-variable (mode variable value
)
499 "Add directory-local VARIABLE with its VALUE and MODE to .dir-locals.el."
503 (read-file-local-variable-mode)
504 (setq variable
(read-file-local-variable "Add directory-local variable"))
505 (read-file-local-variable-value variable
))))
506 (modify-dir-local-variable mode variable value
'add-or-replace
))
509 (defun delete-dir-local-variable (mode variable
)
510 "Delete all MODE settings of file-local VARIABLE from .dir-locals.el."
513 (read-file-local-variable-mode)
514 (read-file-local-variable "Delete directory-local variable")))
515 (modify-dir-local-variable mode variable nil
'delete
))
518 (defun copy-file-locals-to-dir-locals ()
519 "Copy file-local variables to .dir-locals.el."
521 (dolist (elt file-local-variables-alist
)
522 (unless (assq (car elt
) dir-local-variables-alist
)
523 (add-dir-local-variable major-mode
(car elt
) (cdr elt
)))))
526 (defun copy-dir-locals-to-file-locals ()
527 "Copy directory-local variables to the Local Variables list."
529 (dolist (elt dir-local-variables-alist
)
530 (add-file-local-variable (car elt
) (cdr elt
))))
533 (defun copy-dir-locals-to-file-locals-prop-line ()
534 "Copy directory-local variables to the -*- line."
536 (dolist (elt dir-local-variables-alist
)
537 (add-file-local-variable-prop-line (car elt
) (cdr elt
))))
543 ;;; files-x.el ends here