annotate lisp/files-x.el @ 105476:e3bdbd8a2169

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