annotate lisp/files-x.el @ 111864:cc4e58bd25f1

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