files-x.el 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. ;; Error before reading value.
  219. (if (equal variable 'lexical-binding)
  220. (user-error "The `%s' variable must be set at the start of the file"
  221. variable))
  222. (list variable (read-file-local-variable-value variable) t)))
  223. (if (equal variable 'lexical-binding)
  224. (user-error "The `%s' variable must be set at the start of the file"
  225. variable))
  226. (modify-file-local-variable variable value 'add-or-replace interactive))
  227. ;;;###autoload
  228. (defun delete-file-local-variable (variable &optional interactive)
  229. "Delete all settings of file-local VARIABLE from the Local Variables list."
  230. (interactive
  231. (list (read-file-local-variable "Delete file-local variable") t))
  232. (modify-file-local-variable variable nil 'delete interactive))
  233. (defun modify-file-local-variable-prop-line (variable value op &optional interactive)
  234. "Modify file-local VARIABLE in the -*- line depending on operation OP.
  235. If OP is `add-or-replace' then delete all existing settings of
  236. VARIABLE (except `mode' and `eval') and add a new file-local VARIABLE
  237. with VALUE to the -*- line.
  238. If there is no -*- line at the beginning of the current file buffer
  239. and OP is not `delete' then this function adds the -*- line.
  240. If OP is `delete' then delete all existing settings of VARIABLE
  241. from the -*- line ignoring the input argument VALUE."
  242. (catch 'exit
  243. (let ((beg (point)) end replaced-pos)
  244. (unless enable-local-variables
  245. (throw 'exit (message "File-local variables are disabled")))
  246. ;; Find the -*- line at the beginning of the current buffer.
  247. (widen)
  248. (goto-char (point-min))
  249. (setq end (set-auto-mode-1))
  250. (if end
  251. (setq beg (point-marker) end (copy-marker end))
  252. ;; Add the -*- line if not found.
  253. ;; Don't add the -*- line for the deletion operation.
  254. (when (eq op 'delete)
  255. (throw 'exit (progn (goto-char beg)
  256. (message "The -*- line not found"))))
  257. (goto-char (point-min))
  258. ;; Skip interpreter magic line "#!" or XML declaration.
  259. (when (or (looking-at file-auto-mode-skip)
  260. (looking-at "<\\?xml[^>\n]*>$"))
  261. (forward-line 1))
  262. (let ((comment-style 'plain)
  263. (comment-start (or comment-start ";;; "))
  264. (line-beg (line-beginning-position))
  265. (ce nil))
  266. (comment-normalize-vars)
  267. ;; If the first line contains a comment.
  268. (if (save-excursion
  269. (and (looking-at comment-start-skip)
  270. (goto-char (match-end 0))
  271. (re-search-forward comment-end-skip)
  272. (goto-char (match-beginning 0))
  273. ;; Still on the same line?
  274. (equal line-beg (line-beginning-position))
  275. (setq ce (point))))
  276. ;; Add local variables to the end of the existing comment.
  277. (progn
  278. (goto-char ce)
  279. (insert " -*-")
  280. (setq beg (point-marker))
  281. (setq end (point-marker))
  282. (insert "-*-"))
  283. ;; Otherwise, add a new comment before the first line.
  284. (comment-region
  285. (prog1 (point)
  286. (insert "-*-")
  287. (setq beg (point-marker))
  288. (setq end (point-marker))
  289. (insert "-*-\n"))
  290. (point)))))
  291. (cond
  292. ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)")
  293. ;; Simple form: "-*- MODENAME -*-".
  294. (if (eq variable 'mode)
  295. ;; Replace or delete MODENAME
  296. (progn
  297. (when (member op '(add-or-replace delete))
  298. (delete-region (match-beginning 1) (match-end 1)))
  299. (when (eq op 'add-or-replace)
  300. (goto-char (match-beginning 1))
  301. (insert (format "%S" value))))
  302. ;; Else, turn `MODENAME' into `mode:MODENAME'
  303. ;; and add `VARIABLE: VALUE;'
  304. (goto-char (match-beginning 2))
  305. (insert (format "; %S: %S; " variable value))
  306. (goto-char (match-beginning 1))
  307. (insert " mode: ")))
  308. (t
  309. ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-'
  310. ;; Find and delete all existing variable/value pairs.
  311. (when (member op '(add-or-replace delete))
  312. (if (and (eq op 'add-or-replace) (memq variable '(mode eval)))
  313. (goto-char end)
  314. (goto-char beg)
  315. (while (< (point) end)
  316. (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*")
  317. (throw 'exit (message "Malformed -*- line")))
  318. (goto-char (match-end 0))
  319. (let ((key (intern (match-string 1))))
  320. (save-restriction
  321. (narrow-to-region (point) end)
  322. (let ((read-circle nil))
  323. (read (current-buffer))))
  324. (skip-chars-forward " \t;")
  325. (when (eq key variable)
  326. (delete-region (match-beginning 0) (point))
  327. (setq replaced-pos (point)))))))
  328. ;; Add a new variable/value pair. Add `mode' to the start, add new
  329. ;; variable to the end, and add a replaced variable to its last location.
  330. (when (eq op 'add-or-replace)
  331. (cond
  332. ((eq variable 'mode) (goto-char beg))
  333. ((null replaced-pos) (goto-char end))
  334. (replaced-pos (goto-char replaced-pos)))
  335. (if (and (not (eq (char-before) ?\;))
  336. (not (equal (point) (marker-position beg)))
  337. ;; When existing `-*- -*-' is empty, beg > end.
  338. (not (> (marker-position beg) (marker-position end))))
  339. (insert ";"))
  340. (unless (eq (char-before) ?\s) (insert " "))
  341. (insert (format "%S: %S;" variable value))
  342. (unless (eq (char-after) ?\s) (insert " ")))))
  343. (when interactive
  344. (modify-file-local-variable-message variable value op)))))
  345. ;;;###autoload
  346. (defun add-file-local-variable-prop-line (variable value &optional interactive)
  347. "Add file-local VARIABLE with its VALUE to the -*- line.
  348. This command deletes all existing settings of VARIABLE (except `mode'
  349. and `eval') and adds a new file-local VARIABLE with VALUE to
  350. the -*- line.
  351. If there is no -*- line at the beginning of the current file buffer
  352. then this function adds it."
  353. (interactive
  354. (let ((variable (read-file-local-variable "Add -*- file-local variable")))
  355. (list variable (read-file-local-variable-value variable) t)))
  356. (modify-file-local-variable-prop-line variable value 'add-or-replace interactive))
  357. ;;;###autoload
  358. (defun delete-file-local-variable-prop-line (variable &optional interactive)
  359. "Delete all settings of file-local VARIABLE from the -*- line."
  360. (interactive
  361. (list (read-file-local-variable "Delete -*- file-local variable") t))
  362. (modify-file-local-variable-prop-line variable nil 'delete interactive))
  363. (defvar auto-insert) ; from autoinsert.el
  364. (defun modify-dir-local-variable (mode variable value op)
  365. "Modify directory-local VARIABLE in .dir-locals.el depending on operation OP.
  366. If OP is `add-or-replace' then delete all existing settings of
  367. VARIABLE (except `mode' and `eval') and add a new directory-local VARIABLE
  368. with VALUE to the MODE alist where MODE can be a mode name symbol or
  369. a subdirectory name.
  370. If .dir-locals.el was not found and OP is not `delete' then create
  371. this file in the current directory.
  372. If OP is `delete' then delete all existing settings of VARIABLE
  373. from the MODE alist ignoring the input argument VALUE."
  374. (catch 'exit
  375. (unless enable-local-variables
  376. (throw 'exit (message "Directory-local variables are disabled")))
  377. (let ((variables-file (or (and (buffer-file-name)
  378. (not (file-remote-p (buffer-file-name)))
  379. (dir-locals-find-file (buffer-file-name)))
  380. dir-locals-file))
  381. variables)
  382. (if (consp variables-file) ; result from cache
  383. ;; If cache element has an mtime, assume it came from a file.
  384. ;; Otherwise, assume it was set directly.
  385. (setq variables-file (if (nth 2 variables-file)
  386. (expand-file-name dir-locals-file
  387. (car variables-file))
  388. (cadr variables-file))))
  389. ;; I can't be bothered to handle this case right now.
  390. ;; Dir locals were set directly from a class. You need to
  391. ;; directly modify the class in dir-locals-class-alist.
  392. (and variables-file (not (stringp variables-file))
  393. (throw 'exit (message "Directory locals were not set from a file")))
  394. ;; Don't create ".dir-locals.el" for the deletion operation.
  395. (and (eq op 'delete)
  396. (or (not variables-file)
  397. (not (file-exists-p variables-file)))
  398. (throw 'exit (message "No .dir-locals.el file was found")))
  399. (let ((auto-insert nil))
  400. (find-file variables-file))
  401. (widen)
  402. (goto-char (point-min))
  403. ;; Read alist of directory-local variables.
  404. (ignore-errors
  405. (delete-region
  406. (prog1 (point)
  407. (setq variables (let ((read-circle nil))
  408. (read (current-buffer)))))
  409. (point)))
  410. ;; Add or replace variable in alist of directory-local variables.
  411. (let ((mode-assoc (assoc mode variables)))
  412. (if mode-assoc
  413. (setq variables
  414. (cons (cons mode
  415. (if (eq op 'delete)
  416. (assq-delete-all variable (cdr mode-assoc))
  417. (cons
  418. (cons variable value)
  419. (if (memq variable '(mode eval))
  420. (cdr mode-assoc)
  421. (assq-delete-all variable (cdr mode-assoc))))))
  422. (assq-delete-all mode variables)))
  423. (setq variables
  424. (cons `(,mode . ((,variable . ,value)))
  425. variables))))
  426. ;; Insert modified alist of directory-local variables.
  427. (insert ";;; Directory Local Variables\n")
  428. (insert ";;; For more information see (info \"(emacs) Directory Variables\")\n\n")
  429. (pp (sort variables
  430. (lambda (a b)
  431. (cond
  432. ((null (car a)) t)
  433. ((null (car b)) nil)
  434. ((and (symbolp (car a)) (stringp (car b))) t)
  435. ((and (symbolp (car b)) (stringp (car a))) nil)
  436. (t (string< (car a) (car b))))))
  437. (current-buffer)))))
  438. ;;;###autoload
  439. (defun add-dir-local-variable (mode variable value)
  440. "Add directory-local VARIABLE with its VALUE and MODE to .dir-locals.el."
  441. (interactive
  442. (let (variable)
  443. (list
  444. (read-file-local-variable-mode)
  445. (setq variable (read-file-local-variable "Add directory-local variable"))
  446. (read-file-local-variable-value variable))))
  447. (modify-dir-local-variable mode variable value 'add-or-replace))
  448. ;;;###autoload
  449. (defun delete-dir-local-variable (mode variable)
  450. "Delete all MODE settings of file-local VARIABLE from .dir-locals.el."
  451. (interactive
  452. (list
  453. (read-file-local-variable-mode)
  454. (read-file-local-variable "Delete directory-local variable")))
  455. (modify-dir-local-variable mode variable nil 'delete))
  456. ;;;###autoload
  457. (defun copy-file-locals-to-dir-locals ()
  458. "Copy file-local variables to .dir-locals.el."
  459. (interactive)
  460. (dolist (elt file-local-variables-alist)
  461. (unless (assq (car elt) dir-local-variables-alist)
  462. (add-dir-local-variable major-mode (car elt) (cdr elt)))))
  463. ;;;###autoload
  464. (defun copy-dir-locals-to-file-locals ()
  465. "Copy directory-local variables to the Local Variables list."
  466. (interactive)
  467. (dolist (elt dir-local-variables-alist)
  468. (add-file-local-variable (car elt) (cdr elt))))
  469. ;;;###autoload
  470. (defun copy-dir-locals-to-file-locals-prop-line ()
  471. "Copy directory-local variables to the -*- line."
  472. (interactive)
  473. (dolist (elt dir-local-variables-alist)
  474. (add-file-local-variable-prop-line (car elt) (cdr elt))))
  475. (provide 'files-x)
  476. ;;; files-x.el ends here