annotate lisp/files-x.el @ 109460:597339bd6bef

SQL Mode V2.2 - Added sql-connect and sql-connection-alist
author Michael Mauger <mmaug@yahoo.com>
date Sun, 18 Jul 2010 14:44:32 -0400
parents 1d1d5d9bd884
children 280c8ae2476d 376148b31b5e
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
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)
105513
1bfde4a3bd05 (read-file-local-variable): Include some
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 105423
diff changeset
52 (get sym 'safe-local-variable)
104236
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
53 (memq sym '(mode eval coding unibyte))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
54 nil nil nil default nil))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
55 (and (stringp variable) (intern variable))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
56
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
57 (defun read-file-local-variable-value (variable)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
58 "Read value of file-local VARIABLE using completion.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
59 Intended to be used in the `interactive' spec of
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
60 `add-file-local-variable' and `add-dir-local-variable'."
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
61 (let (default value)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
62 (cond
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
63 ((eq variable 'mode)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
64 (setq default (and (symbolp major-mode) (symbol-name major-mode)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
65 (setq value
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
66 (completing-read
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
67 (if default
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
68 (format "Add %s with value (default %s): " variable default)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
69 (format "Add %s with value: " variable))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
70 obarray
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
71 (lambda (sym)
105588
2586fdd484bd (read-file-local-variable-value): Don't filter out
Juri Linkov <juri@jurta.org>
parents: 105555
diff changeset
72 (string-match-p "-mode\\'" (symbol-name sym)))
104236
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)
105555
627cf97fdea2 (read-file-local-variable-value):
Juri Linkov <juri@jurta.org>
parents: 105513
diff changeset
94 ((boundp variable)
627cf97fdea2 (read-file-local-variable-value):
Juri Linkov <juri@jurta.org>
parents: 105513
diff changeset
95 (symbol-value variable))))))))))
104236
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
96
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
97 (defun read-file-local-variable-mode ()
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
98 "Read per-directory file-local variable's mode using completion.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
99 Intended to be used in the `interactive' spec of
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
100 `add-dir-local-variable', `delete-dir-local-variable'."
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
101 (let* ((default (and (symbolp major-mode) (symbol-name major-mode)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
102 (mode
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
103 (completing-read
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
104 (if default
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
105 (format "Mode or subdirectory (default %s): " default)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
106 (format "Mode or subdirectory: "))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
107 obarray
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
108 (lambda (sym)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
109 (and (string-match-p "-mode\\'" (symbol-name sym))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
110 (not (string-match-p "-minor-mode\\'" (symbol-name sym)))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
111 nil nil nil default nil)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
112 (cond
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
113 ((equal mode "nil") nil)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
114 ((and (stringp mode) (fboundp (intern mode))) (intern mode))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
115 (t mode))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
116
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
117 (defun modify-file-local-variable (variable value op)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
118 "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
119
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
120 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
121 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
122 with VALUE to the Local Variables list.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
123
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
124 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
125 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
126 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
127
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
128 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
129 from the Local Variables list ignoring the input argument VALUE."
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
130 (catch 'exit
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
131 (let ((beg (point)) end replaced-pos)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
132 (unless enable-local-variables
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
133 (throw 'exit (message "File-local variables are disabled")))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
134
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
135 ;; Look for "Local variables:" line in last page.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
136 (widen)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
137 (goto-char (point-max))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
138 (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
139
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
140 ;; Add "Local variables:" list if not found.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
141 (unless (let ((case-fold-search t))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
142 (search-forward "Local Variables:" nil t))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
143
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
144 ;; Don't add "Local variables:" list for the deletion operation.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
145 (when (eq op 'delete)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
146 (throw 'exit (progn (goto-char beg)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
147 (message "Local Variables not found"))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
148
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
149 (goto-char (point-max))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
150 (let ((comment-style 'plain)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
151 (comment-start (or comment-start ";;; ")))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
152 (comment-region
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
153 (prog1 (setq beg (point))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
154 (insert "\nLocal Variables:\nEnd:\n"))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
155 (point)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
156
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
157 (unless (let ((case-fold-search t))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
158 (goto-char beg)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
159 (search-forward "Local Variables:" nil t))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
160 (throw 'exit (message "Can't add file-local variables"))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
161
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
162 ;; prefix is what comes before "local variables:" in its line.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
163 ;; suffix is what comes after "local variables:" in its line.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
164 (let* ((prefix (buffer-substring (line-beginning-position)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
165 (match-beginning 0)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
166 (suffix (buffer-substring (point) (line-end-position)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
167 (prefix-re (concat "^" (regexp-quote prefix)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
168 (suffix-re (concat (regexp-quote suffix) "$")))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
169
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
170 ;; Find or add missing "End:".
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
171 (forward-line 1)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
172 (setq beg (point))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
173 (save-excursion
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
174 (unless (let ((case-fold-search t))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
175 (re-search-forward
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
176 (concat prefix-re "[ \t]*End:[ \t]*" suffix-re)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
177 nil t))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
178 (save-excursion
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
179 (insert (format "%sEnd:%s\n" prefix suffix))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
180 (beginning-of-line)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
181 (setq end (point-marker)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
182
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
183 ;; Find and delete all existing variable/value pairs.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
184 (when (member op '(add-or-replace delete))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
185 (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
186 (goto-char end)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
187 (goto-char beg)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
188 (while (re-search-forward
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
189 (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
190 (delete-region (match-beginning 0) (1+ (match-end 0)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
191 (setq replaced-pos (point)))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
192
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
193 ;; 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
194 ;; 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
195 (when (eq op 'add-or-replace)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
196 (cond
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
197 ((eq variable 'mode) (goto-char beg))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
198 ((null replaced-pos) (goto-char end))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
199 (replaced-pos (goto-char replaced-pos)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
200 (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
201
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
202 ;;;###autoload
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
203 (defun add-file-local-variable (variable value)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
204 "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
205
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
206 This command deletes all existing settings of VARIABLE (except `mode'
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
207 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
208 Local Variables list.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
209
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
210 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
211 then this function adds the first line containing the string
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
212 `Local Variables:' and the last line containing the string `End:'."
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
213 (interactive
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
214 (let ((variable (read-file-local-variable "Add file-local variable")))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
215 (list variable (read-file-local-variable-value variable))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
216 (modify-file-local-variable variable value 'add-or-replace))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
217
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
218 ;;;###autoload
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
219 (defun delete-file-local-variable (variable)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
220 "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
221 (interactive
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
222 (list (read-file-local-variable "Delete file-local variable")))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
223 (modify-file-local-variable variable nil 'delete))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
224
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
225 (defun modify-file-local-variable-prop-line (variable value op)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
226 "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
227
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
228 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
229 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
230 with VALUE to the -*- line.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
231
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
232 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
233 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
234
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
235 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
236 from the -*- line ignoring the input argument VALUE."
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
237 (catch 'exit
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
238 (let ((beg (point)) end replaced-pos)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
239 (unless enable-local-variables
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
240 (throw 'exit (message "File-local variables are disabled")))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
241
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
242 ;; Find the -*- line at the beginning of the current buffer.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
243 (widen)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
244 (goto-char (point-min))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
245 (setq end (set-auto-mode-1))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
246
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
247 (if end
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
248 (setq beg (point-marker) end (copy-marker end))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
249
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
250 ;; Add the -*- line if not found.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
251 ;; Don't add the -*- line for the deletion operation.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
252 (when (eq op 'delete)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
253 (throw 'exit (progn (goto-char beg)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
254 (message "The -*- line not found"))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
255
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
256 (goto-char (point-min))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
257
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
258 ;; Skip interpreter magic line "#!"
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
259 (when (looking-at "^\\(#!\\|'\\\\\"\\)")
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
260 (forward-line 1))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
261
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
262 (let ((comment-style 'plain)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
263 (comment-start (or comment-start ";;; ")))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
264 (comment-region
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
265 (prog1 (point)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
266 (insert "-*-")
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
267 (setq beg (point-marker))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
268 (setq end (point-marker))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
269 (insert "-*-\n"))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
270 (point))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
271
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
272 (cond
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
273 ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)")
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
274 ;; Simple form: "-*- MODENAME -*-".
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
275 (if (eq variable 'mode)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
276 ;; Replace or delete MODENAME
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
277 (progn
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
278 (when (member op '(add-or-replace delete))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
279 (delete-region (match-beginning 1) (match-end 1)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
280 (when (eq op 'add-or-replace)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
281 (goto-char (match-beginning 1))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
282 (insert (format "%S" value))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
283 ;; Else, turn `MODENAME' into `mode:MODENAME'
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
284 ;; and add `VARIABLE: VALUE;'
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
285 (goto-char (match-beginning 2))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
286 (insert (format "; %S: %S; " variable value))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
287 (goto-char (match-beginning 1))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
288 (insert " mode: ")))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
289
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
290 (t
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
291 ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-'
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
292 ;; Find and delete all existing variable/value pairs.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
293 (when (member op '(add-or-replace delete))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
294 (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
295 (goto-char end)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
296 (goto-char beg)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
297 (while (< (point) end)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
298 (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*")
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
299 (throw 'exit (message "Malformed -*- line")))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
300 (goto-char (match-end 0))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
301 (let ((key (intern (match-string 1)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
302 (val (save-restriction
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
303 (narrow-to-region (point) end)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
304 (let ((read-circle nil))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
305 (read (current-buffer))))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
306 (skip-chars-forward " \t;")
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
307 (when (eq key variable)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
308 (delete-region (match-beginning 0) (point))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
309 (setq replaced-pos (point)))))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
310 ;; 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
311 ;; 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
312 (when (eq op 'add-or-replace)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
313 (cond
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
314 ((eq variable 'mode) (goto-char beg))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
315 ((null replaced-pos) (goto-char end))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
316 (replaced-pos (goto-char replaced-pos)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
317 (if (and (not (eq (char-before) ?\;))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
318 (not (equal (point) (marker-position beg))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
319 (insert ";"))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
320 (unless (eq (char-before) ?\s) (insert " "))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
321 (insert (format "%S: %S;" variable value))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
322 (unless (eq (char-after) ?\s) (insert " "))))))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
323
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
324 ;;;###autoload
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
325 (defun add-file-local-variable-prop-line (variable value)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
326 "Add file-local VARIABLE with its VALUE to the -*- line.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
327
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
328 This command deletes all existing settings of VARIABLE (except `mode'
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
329 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
330 the -*- line.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
331
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
332 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
333 then this function adds it."
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
334 (interactive
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
335 (let ((variable (read-file-local-variable "Add -*- file-local variable")))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
336 (list variable (read-file-local-variable-value variable))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
337 (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
338
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
339 ;;;###autoload
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
340 (defun delete-file-local-variable-prop-line (variable)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
341 "Delete all settings of file-local VARIABLE from the -*- line."
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
342 (interactive
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
343 (list (read-file-local-variable "Delete -*- file-local variable")))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
344 (modify-file-local-variable-prop-line variable nil 'delete))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
345
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
346 (defun modify-dir-local-variable (mode variable value op)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
347 "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
348
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
349 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
350 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
351 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
352 a subdirectory name.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
353
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
354 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
355 this file in the current directory.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
356
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
357 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
358 from the MODE alist ignoring the input argument VALUE."
104236
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
359 (catch 'exit
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
360 (unless enable-local-variables
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
361 (throw 'exit (message "Directory-local variables are disabled")))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
362
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
363 (let ((variables-file (or (and (buffer-file-name)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
364 (not (file-remote-p (buffer-file-name)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
365 (dir-locals-find-file (buffer-file-name)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
366 dir-locals-file))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
367 variables)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
368
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
369 ;; Don't create ".dir-locals.el" for the deletion operation.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
370 (when (and (eq op 'delete)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
371 (not (file-exists-p variables-file)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
372 (throw 'exit (message "File .dir-locals.el not found")))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
373
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
374 (let ((auto-insert nil))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
375 (find-file variables-file))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
376 (widen)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
377 (goto-char (point-min))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
378
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
379 ;; Read alist of directory-local variables.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
380 (ignore-errors
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
381 (delete-region
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
382 (prog1 (point)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
383 (setq variables (let ((read-circle nil))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
384 (read (current-buffer)))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
385 (point)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
386
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
387 ;; Add or replace variable in alist of directory-local variables.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
388 (let ((mode-assoc (assoc mode variables)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
389 (if mode-assoc
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
390 (setq variables
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
391 (cons (cons mode
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
392 (if (eq op 'delete)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
393 (assq-delete-all variable (cdr mode-assoc))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
394 (cons
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
395 (cons variable value)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
396 (if (memq variable '(mode eval))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
397 (cdr mode-assoc)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
398 (assq-delete-all variable (cdr mode-assoc))))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
399 (assq-delete-all mode variables)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
400 (setq variables
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
401 (cons `(,mode . ((,variable . ,value)))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
402 variables))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
403
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
404 ;; Insert modified alist of directory-local variables.
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
405 (insert ";;; Directory Local Variables\n")
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
406 (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
407 (pp (sort variables
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
408 (lambda (a b)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
409 (cond
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
410 ((null (car a)) t)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
411 ((null (car b)) nil)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
412 ((and (symbolp (car a)) (stringp (car b))) t)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
413 ((and (symbolp (car b)) (stringp (car a))) nil)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
414 (t (string< (car a) (car b))))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
415 (current-buffer)))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
416
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
417 ;;;###autoload
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
418 (defun add-dir-local-variable (mode variable value)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
419 "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
420 (interactive
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
421 (let (variable)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
422 (list
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
423 (read-file-local-variable-mode)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
424 (setq variable (read-file-local-variable "Add directory-local variable"))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
425 (read-file-local-variable-value variable))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
426 (modify-dir-local-variable mode variable value 'add-or-replace))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
427
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
428 ;;;###autoload
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
429 (defun delete-dir-local-variable (mode variable)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
430 "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
431 (interactive
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
432 (list
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
433 (read-file-local-variable-mode)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
434 (read-file-local-variable "Delete directory-local variable")))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
435 (modify-dir-local-variable mode variable nil 'delete))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
436
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
437 ;;;###autoload
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
438 (defun copy-file-locals-to-dir-locals ()
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
439 "Copy file-local variables to .dir-locals.el."
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
440 (interactive)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
441 (dolist (elt file-local-variables-alist)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
442 (unless (assq (car elt) dir-local-variables-alist)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
443 (add-dir-local-variable major-mode (car elt) (cdr elt)))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
444
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
445 ;;;###autoload
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
446 (defun copy-dir-locals-to-file-locals ()
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
447 "Copy directory-local variables to the Local Variables list."
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
448 (interactive)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
449 (dolist (elt dir-local-variables-alist)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
450 (add-file-local-variable (car elt) (cdr elt))))
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
451
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
452 ;;;###autoload
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
453 (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
454 "Copy directory-local variables to the -*- line."
104236
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
455 (interactive)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
456 (dolist (elt dir-local-variables-alist)
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
457 (add-file-local-variable-prop-line (car elt) (cdr elt))))
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
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
461 (provide 'files-x)
104264
be5412b66c92 Add arch tagline
Miles Bader <miles@gnu.org>
parents: 104236
diff changeset
462
be5412b66c92 Add arch tagline
Miles Bader <miles@gnu.org>
parents: 104236
diff changeset
463 ;; arch-tag: 949d263c-30a8-4b49-af26-cda97c7c5477
104236
22de0794e0bd * files-x.el: New file.
Juri Linkov <juri@jurta.org>
parents:
diff changeset
464 ;;; files-x.el ends here