files-x.el 17 KB

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