files-x.el 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. ;;; files-x.el --- extended file handling commands
  2. ;; Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. ;; Author: Juri Linkov <juri@jurta.org>
  4. ;; Maintainer: emacs-devel@gnu.org
  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-at-point))
  30. (default (and (symbolp default) (boundp default)
  31. (symbol-name default)))
  32. (variable
  33. (completing-read
  34. (if default
  35. (format "%s (default %s): " prompt default)
  36. (format "%s: " prompt))
  37. obarray
  38. (lambda (sym)
  39. (or (custom-variable-p sym)
  40. (get sym 'safe-local-variable)
  41. (memq sym '(mode eval coding unibyte))))
  42. nil nil nil default nil)))
  43. (and (stringp variable) (intern variable))))
  44. (defun read-file-local-variable-value (variable)
  45. "Read value of file-local VARIABLE using completion.
  46. Intended to be used in the `interactive' spec of
  47. `add-file-local-variable' and `add-dir-local-variable'."
  48. (cond
  49. ((eq variable 'mode)
  50. (let* ((default (and (symbolp major-mode) (symbol-name major-mode)))
  51. (value
  52. (completing-read
  53. (if default
  54. (format "Add %s with value (default %s): " variable default)
  55. (format "Add %s with value: " variable))
  56. obarray
  57. (lambda (sym)
  58. (string-match-p "-mode\\'" (symbol-name sym)))
  59. nil nil nil default nil)))
  60. (and (stringp value)
  61. (intern (replace-regexp-in-string "-mode\\'" "" value)))))
  62. ((eq variable 'eval)
  63. (read--expression (format "Add %s with expression: " variable)))
  64. ((eq variable 'coding)
  65. (let ((default (and (symbolp buffer-file-coding-system)
  66. (symbol-name buffer-file-coding-system))))
  67. (read-coding-system
  68. (if default
  69. (format "Add %s with value (default %s): " variable default)
  70. (format "Add %s with value: " variable))
  71. default)))
  72. (t
  73. (let ((default (format "%S"
  74. (cond ((eq variable 'unibyte) t)
  75. ((boundp variable)
  76. (symbol-value variable)))))
  77. (minibuffer-completing-symbol t))
  78. (read-from-minibuffer (format "Add %s with value: " variable)
  79. nil read-expression-map t
  80. 'set-variable-value-history
  81. default)))))
  82. (defun read-file-local-variable-mode ()
  83. "Read per-directory file-local variable's mode using completion.
  84. Intended to be used in the `interactive' spec of
  85. `add-dir-local-variable', `delete-dir-local-variable'."
  86. (let* ((default (and (symbolp major-mode) (symbol-name major-mode)))
  87. (mode
  88. (completing-read
  89. (if default
  90. (format "Mode or subdirectory (default %s): " default)
  91. (format "Mode or subdirectory: "))
  92. obarray
  93. (lambda (sym)
  94. (and (string-match-p "-mode\\'" (symbol-name sym))
  95. (not (or (memq sym minor-mode-list)
  96. (string-match-p "-minor-mode\\'"
  97. (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-message (variable value op)
  104. (let* ((not-value (make-symbol ""))
  105. (old-value (cond ((eq variable 'mode)
  106. major-mode)
  107. ((eq variable 'coding)
  108. buffer-file-coding-system)
  109. (t (if (and (symbolp variable)
  110. (boundp variable))
  111. (symbol-value variable)
  112. not-value))))
  113. (new-value (if (eq op 'delete)
  114. (cond ((eq variable 'mode)
  115. (default-value 'major-mode))
  116. ((eq variable 'coding)
  117. (default-value 'buffer-file-coding-system))
  118. (t (if (and (symbolp variable)
  119. (default-boundp variable))
  120. (default-value variable)
  121. not-value)))
  122. (cond ((eq variable 'mode)
  123. (let ((string (format "%S" value)))
  124. (if (string-match-p "-mode\\'" string)
  125. value
  126. (intern (concat string "-mode")))))
  127. (t value)))))
  128. (when (or (eq old-value not-value)
  129. (eq new-value not-value)
  130. (not (equal old-value new-value)))
  131. (message "%s" (substitute-command-keys
  132. "For this change to take effect revisit file using \\[revert-buffer]")))))
  133. (defun modify-file-local-variable (variable value op &optional interactive)
  134. "Modify file-local VARIABLE in Local Variables depending on operation OP.
  135. If OP is `add-or-replace' then delete all existing settings of
  136. VARIABLE (except `mode' and `eval') and add a new file-local VARIABLE
  137. with VALUE to the Local Variables list.
  138. If there is no Local Variables list in the current file buffer and OP
  139. is not `delete' then this function adds the first line containing the
  140. string `Local Variables:' and the last line containing the string `End:'.
  141. If OP is `delete' then delete all existing settings of VARIABLE
  142. from the Local Variables list ignoring the input argument VALUE."
  143. (catch 'exit
  144. (let ((beg (point)) end replaced-pos)
  145. (unless enable-local-variables
  146. (throw 'exit (message "File-local variables are disabled")))
  147. ;; Look for "Local variables:" line in last page.
  148. (widen)
  149. (goto-char (point-max))
  150. (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
  151. ;; Add "Local variables:" list if not found.
  152. (unless (let ((case-fold-search t))
  153. (search-forward "Local Variables:" nil t))
  154. ;; Don't add "Local variables:" list for the deletion operation.
  155. (when (eq op 'delete)
  156. (throw 'exit (progn (goto-char beg)
  157. (message "Local Variables not found"))))
  158. (goto-char (point-max))
  159. (let ((comment-style 'plain)
  160. (comment-start (or comment-start ";; ")))
  161. (comment-region
  162. (prog1 (setq beg (point))
  163. (insert "\nLocal Variables:\nEnd:\n"))
  164. (point)))
  165. (unless (let ((case-fold-search t))
  166. (goto-char beg)
  167. (search-forward "Local Variables:" nil t))
  168. (throw 'exit (message "Can't add file-local variables"))))
  169. ;; prefix is what comes before "local variables:" in its line.
  170. ;; suffix is what comes after "local variables:" in its line.
  171. (let* ((prefix (buffer-substring (line-beginning-position)
  172. (match-beginning 0)))
  173. (suffix (buffer-substring (point) (line-end-position)))
  174. (prefix-re (concat "^" (regexp-quote prefix)))
  175. (suffix-re (concat (regexp-quote suffix) "$")))
  176. ;; Find or add missing "End:".
  177. (forward-line 1)
  178. (setq beg (point))
  179. (save-excursion
  180. (unless (let ((case-fold-search t))
  181. (re-search-forward
  182. (concat prefix-re "[ \t]*End:[ \t]*" suffix-re)
  183. nil t))
  184. (save-excursion
  185. (insert (format "%sEnd:%s\n" prefix suffix))))
  186. (beginning-of-line)
  187. (setq end (point-marker)))
  188. ;; Find and delete all existing variable/value pairs.
  189. (when (member op '(add-or-replace delete))
  190. (if (and (eq op 'add-or-replace) (memq variable '(mode eval)))
  191. (goto-char end)
  192. (goto-char beg)
  193. (while (re-search-forward
  194. (format "%s%S:.*%s" prefix-re variable suffix-re) end t)
  195. (delete-region (match-beginning 0) (1+ (match-end 0)))
  196. (setq replaced-pos (point)))))
  197. ;; Add a new variable/value pair. Add `mode' to the start, add new
  198. ;; variable to the end, and add a replaced variable to its last location.
  199. (when (eq op 'add-or-replace)
  200. (cond
  201. ((eq variable 'mode) (goto-char beg))
  202. ((null replaced-pos) (goto-char end))
  203. (replaced-pos (goto-char replaced-pos)))
  204. (insert (format "%s%S: %S%s\n" prefix variable value suffix))))
  205. (when interactive
  206. (modify-file-local-variable-message variable value op)))))
  207. ;;;###autoload
  208. (defun add-file-local-variable (variable value &optional interactive)
  209. "Add file-local VARIABLE with its VALUE to the Local Variables list.
  210. This command deletes all existing settings of VARIABLE (except `mode'
  211. and `eval') and adds a new file-local VARIABLE with VALUE to the
  212. Local Variables list.
  213. If there is no Local Variables list in the current file buffer
  214. then this function adds the first line containing the string
  215. `Local Variables:' and the last line containing the string `End:'."
  216. (interactive
  217. (let ((variable (read-file-local-variable "Add file-local variable")))
  218. (list variable (read-file-local-variable-value variable) t)))
  219. (modify-file-local-variable variable value 'add-or-replace interactive))
  220. ;;;###autoload
  221. (defun delete-file-local-variable (variable &optional interactive)
  222. "Delete all settings of file-local VARIABLE from the Local Variables list."
  223. (interactive
  224. (list (read-file-local-variable "Delete file-local variable") t))
  225. (modify-file-local-variable variable nil 'delete interactive))
  226. (defun modify-file-local-variable-prop-line (variable value op &optional interactive)
  227. "Modify file-local VARIABLE in the -*- line depending on operation OP.
  228. If OP is `add-or-replace' then delete all existing settings of
  229. VARIABLE (except `mode' and `eval') and add a new file-local VARIABLE
  230. with VALUE to the -*- line.
  231. If there is no -*- line at the beginning of the current file buffer
  232. and OP is not `delete' then this function adds the -*- line.
  233. If OP is `delete' then delete all existing settings of VARIABLE
  234. from the -*- line ignoring the input argument VALUE."
  235. (catch 'exit
  236. (let ((beg (point)) end replaced-pos)
  237. (unless enable-local-variables
  238. (throw 'exit (message "File-local variables are disabled")))
  239. ;; Find the -*- line at the beginning of the current buffer.
  240. (widen)
  241. (goto-char (point-min))
  242. (setq end (set-auto-mode-1))
  243. (if end
  244. (setq beg (point-marker) end (copy-marker end))
  245. ;; Add the -*- line if not found.
  246. ;; Don't add the -*- line for the deletion operation.
  247. (when (eq op 'delete)
  248. (throw 'exit (progn (goto-char beg)
  249. (message "The -*- line not found"))))
  250. (goto-char (point-min))
  251. ;; Skip interpreter magic line "#!" or XML declaration.
  252. (when (or (looking-at file-auto-mode-skip)
  253. (looking-at "<\\?xml[^>\n]*>$"))
  254. (forward-line 1))
  255. (let ((comment-style 'plain)
  256. (comment-start (or comment-start ";;; "))
  257. (line-beg (line-beginning-position))
  258. (ce nil))
  259. (comment-normalize-vars)
  260. ;; If the first line contains a comment.
  261. (if (save-excursion
  262. (and (looking-at comment-start-skip)
  263. (goto-char (match-end 0))
  264. (re-search-forward comment-end-skip)
  265. (goto-char (match-beginning 0))
  266. ;; Still on the same line?
  267. (equal line-beg (line-beginning-position))
  268. (setq ce (point))))
  269. ;; Add local variables to the end of the existing comment.
  270. (progn
  271. (goto-char ce)
  272. (insert " -*-")
  273. (setq beg (point-marker))
  274. (setq end (point-marker))
  275. (insert "-*-"))
  276. ;; Otherwise, add a new comment before the first line.
  277. (comment-region
  278. (prog1 (point)
  279. (insert "-*-")
  280. (setq beg (point-marker))
  281. (setq end (point-marker))
  282. (insert "-*-\n"))
  283. (point)))))
  284. (cond
  285. ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)")
  286. ;; Simple form: "-*- MODENAME -*-".
  287. (if (eq variable 'mode)
  288. ;; Replace or delete MODENAME
  289. (progn
  290. (when (member op '(add-or-replace delete))
  291. (delete-region (match-beginning 1) (match-end 1)))
  292. (when (eq op 'add-or-replace)
  293. (goto-char (match-beginning 1))
  294. (insert (format "%S" value))))
  295. ;; Else, turn `MODENAME' into `mode:MODENAME'
  296. ;; and add `VARIABLE: VALUE;'
  297. (goto-char (match-beginning 2))
  298. (insert (format "; %S: %S; " variable value))
  299. (goto-char (match-beginning 1))
  300. (insert " mode: ")))
  301. (t
  302. ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-'
  303. ;; Find and delete all existing variable/value pairs.
  304. (when (member op '(add-or-replace delete))
  305. (if (and (eq op 'add-or-replace) (memq variable '(mode eval)))
  306. (goto-char end)
  307. (goto-char beg)
  308. (while (< (point) end)
  309. (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*")
  310. (throw 'exit (message "Malformed -*- line")))
  311. (goto-char (match-end 0))
  312. (let ((key (intern (match-string 1))))
  313. (save-restriction
  314. (narrow-to-region (point) end)
  315. (let ((read-circle nil))
  316. (read (current-buffer))))
  317. (skip-chars-forward " \t;")
  318. (when (eq key variable)
  319. (delete-region (match-beginning 0) (point))
  320. (setq replaced-pos (point)))))))
  321. ;; Add a new variable/value pair. Add `mode' to the start, add new
  322. ;; variable to the end, and add a replaced variable to its last location.
  323. (when (eq op 'add-or-replace)
  324. (cond
  325. ((eq variable 'mode) (goto-char beg))
  326. ((null replaced-pos) (goto-char end))
  327. (replaced-pos (goto-char replaced-pos)))
  328. (if (and (not (eq (char-before) ?\;))
  329. (not (equal (point) (marker-position beg)))
  330. ;; When existing `-*- -*-' is empty, beg > end.
  331. (not (> (marker-position beg) (marker-position end))))
  332. (insert ";"))
  333. (unless (eq (char-before) ?\s) (insert " "))
  334. (insert (format "%S: %S;" variable value))
  335. (unless (eq (char-after) ?\s) (insert " ")))))
  336. (when interactive
  337. (modify-file-local-variable-message variable value op)))))
  338. ;;;###autoload
  339. (defun add-file-local-variable-prop-line (variable value &optional interactive)
  340. "Add file-local VARIABLE with its VALUE to the -*- line.
  341. This command deletes all existing settings of VARIABLE (except `mode'
  342. and `eval') and adds a new file-local VARIABLE with VALUE to
  343. the -*- line.
  344. If there is no -*- line at the beginning of the current file buffer
  345. then this function adds it."
  346. (interactive
  347. (let ((variable (read-file-local-variable "Add -*- file-local variable")))
  348. (list variable (read-file-local-variable-value variable) t)))
  349. (modify-file-local-variable-prop-line variable value 'add-or-replace interactive))
  350. ;;;###autoload
  351. (defun delete-file-local-variable-prop-line (variable &optional interactive)
  352. "Delete all settings of file-local VARIABLE from the -*- line."
  353. (interactive
  354. (list (read-file-local-variable "Delete -*- file-local variable") t))
  355. (modify-file-local-variable-prop-line variable nil 'delete interactive))
  356. (defvar auto-insert) ; from autoinsert.el
  357. (defun modify-dir-local-variable (mode variable value op)
  358. "Modify directory-local VARIABLE in .dir-locals.el depending on operation OP.
  359. If OP is `add-or-replace' then delete all existing settings of
  360. VARIABLE (except `mode' and `eval') and add a new directory-local VARIABLE
  361. with VALUE to the MODE alist where MODE can be a mode name symbol or
  362. a subdirectory name.
  363. If .dir-locals.el was not found and OP is not `delete' then create
  364. this file in the current directory.
  365. If OP is `delete' then delete all existing settings of VARIABLE
  366. from the MODE alist ignoring the input argument VALUE."
  367. (catch 'exit
  368. (unless enable-local-variables
  369. (throw 'exit (message "Directory-local variables are disabled")))
  370. (let ((variables-file (or (and (buffer-file-name)
  371. (not (file-remote-p (buffer-file-name)))
  372. (dir-locals-find-file (buffer-file-name)))
  373. dir-locals-file))
  374. variables)
  375. (if (consp variables-file) ; result from cache
  376. ;; If cache element has an mtime, assume it came from a file.
  377. ;; Otherwise, assume it was set directly.
  378. (setq variables-file (if (nth 2 variables-file)
  379. (expand-file-name dir-locals-file
  380. (car variables-file))
  381. (cadr variables-file))))
  382. ;; I can't be bothered to handle this case right now.
  383. ;; Dir locals were set directly from a class. You need to
  384. ;; directly modify the class in dir-locals-class-alist.
  385. (and variables-file (not (stringp variables-file))
  386. (throw 'exit (message "Directory locals were not set from a file")))
  387. ;; Don't create ".dir-locals.el" for the deletion operation.
  388. (and (eq op 'delete)
  389. (or (not variables-file)
  390. (not (file-exists-p variables-file)))
  391. (throw 'exit (message "No .dir-locals.el file was found")))
  392. (let ((auto-insert nil))
  393. (find-file variables-file))
  394. (widen)
  395. (goto-char (point-min))
  396. ;; Read alist of directory-local variables.
  397. (ignore-errors
  398. (delete-region
  399. (prog1 (point)
  400. (setq variables (let ((read-circle nil))
  401. (read (current-buffer)))))
  402. (point)))
  403. ;; Add or replace variable in alist of directory-local variables.
  404. (let ((mode-assoc (assoc mode variables)))
  405. (if mode-assoc
  406. (setq variables
  407. (cons (cons mode
  408. (if (eq op 'delete)
  409. (assq-delete-all variable (cdr mode-assoc))
  410. (cons
  411. (cons variable value)
  412. (if (memq variable '(mode eval))
  413. (cdr mode-assoc)
  414. (assq-delete-all variable (cdr mode-assoc))))))
  415. (assq-delete-all mode variables)))
  416. (setq variables
  417. (cons `(,mode . ((,variable . ,value)))
  418. variables))))
  419. ;; Insert modified alist of directory-local variables.
  420. (insert ";;; Directory Local Variables\n")
  421. (insert ";;; For more information see (info \"(emacs) Directory Variables\")\n\n")
  422. (pp (sort variables
  423. (lambda (a b)
  424. (cond
  425. ((null (car a)) t)
  426. ((null (car b)) nil)
  427. ((and (symbolp (car a)) (stringp (car b))) t)
  428. ((and (symbolp (car b)) (stringp (car a))) nil)
  429. (t (string< (car a) (car b))))))
  430. (current-buffer)))))
  431. ;;;###autoload
  432. (defun add-dir-local-variable (mode variable value)
  433. "Add directory-local VARIABLE with its VALUE and MODE to .dir-locals.el."
  434. (interactive
  435. (let (variable)
  436. (list
  437. (read-file-local-variable-mode)
  438. (setq variable (read-file-local-variable "Add directory-local variable"))
  439. (read-file-local-variable-value variable))))
  440. (modify-dir-local-variable mode variable value 'add-or-replace))
  441. ;;;###autoload
  442. (defun delete-dir-local-variable (mode variable)
  443. "Delete all MODE settings of file-local VARIABLE from .dir-locals.el."
  444. (interactive
  445. (list
  446. (read-file-local-variable-mode)
  447. (read-file-local-variable "Delete directory-local variable")))
  448. (modify-dir-local-variable mode variable nil 'delete))
  449. ;;;###autoload
  450. (defun copy-file-locals-to-dir-locals ()
  451. "Copy file-local variables to .dir-locals.el."
  452. (interactive)
  453. (dolist (elt file-local-variables-alist)
  454. (unless (assq (car elt) dir-local-variables-alist)
  455. (add-dir-local-variable major-mode (car elt) (cdr elt)))))
  456. ;;;###autoload
  457. (defun copy-dir-locals-to-file-locals ()
  458. "Copy directory-local variables to the Local Variables list."
  459. (interactive)
  460. (dolist (elt dir-local-variables-alist)
  461. (add-file-local-variable (car elt) (cdr elt))))
  462. ;;;###autoload
  463. (defun copy-dir-locals-to-file-locals-prop-line ()
  464. "Copy directory-local variables to the -*- line."
  465. (interactive)
  466. (dolist (elt dir-local-variables-alist)
  467. (add-file-local-variable-prop-line (car elt) (cdr elt))))
  468. (provide 'files-x)
  469. ;;; files-x.el ends here