files-x.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. ;;; files-x.el --- extended file handling commands
  2. ;; Copyright (C) 2009-2017 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* ((dir-or-cache (and (buffer-file-name)
  378. (not (file-remote-p (buffer-file-name)))
  379. (dir-locals-find-file (buffer-file-name))))
  380. (variables-file
  381. ;; If there are several .dir-locals, the user probably
  382. ;; wants to edit the last one (the highest priority).
  383. (cond ((stringp dir-or-cache)
  384. (car (last (dir-locals--all-files dir-or-cache))))
  385. ((consp dir-or-cache) ; result from cache
  386. ;; If cache element has an mtime, assume it came
  387. ;; from a file. Otherwise, assume it was set
  388. ;; directly.
  389. (if (nth 2 dir-or-cache)
  390. (car (last (dir-locals--all-files (car dir-or-cache))))
  391. (cadr dir-or-cache)))
  392. ;; Try to make a proper file-name.
  393. (t (expand-file-name dir-locals-file))))
  394. variables)
  395. ;; I can't be bothered to handle this case right now.
  396. ;; Dir locals were set directly from a class. You need to
  397. ;; directly modify the class in dir-locals-class-alist.
  398. (and variables-file (not (stringp variables-file))
  399. (throw 'exit (message "Directory locals were not set from a file")))
  400. ;; Don't create ".dir-locals.el" for the deletion operation.
  401. (and (eq op 'delete)
  402. (or (not variables-file)
  403. (not (file-exists-p variables-file)))
  404. (throw 'exit (message "No .dir-locals.el file was found")))
  405. (let ((auto-insert nil))
  406. (find-file variables-file))
  407. (widen)
  408. (goto-char (point-min))
  409. ;; Read alist of directory-local variables.
  410. (ignore-errors
  411. (delete-region
  412. (prog1 (point)
  413. (setq variables (let ((read-circle nil))
  414. (read (current-buffer)))))
  415. (point)))
  416. ;; Add or replace variable in alist of directory-local variables.
  417. (let ((mode-assoc (assoc mode variables)))
  418. (if mode-assoc
  419. (setq variables
  420. (cons (cons mode
  421. (if (eq op 'delete)
  422. (assq-delete-all variable (cdr mode-assoc))
  423. (cons
  424. (cons variable value)
  425. (if (memq variable '(mode eval))
  426. (cdr mode-assoc)
  427. (assq-delete-all variable (cdr mode-assoc))))))
  428. (assq-delete-all mode variables)))
  429. (setq variables
  430. (cons `(,mode . ((,variable . ,value)))
  431. variables))))
  432. ;; Insert modified alist of directory-local variables.
  433. (insert ";;; Directory Local Variables\n")
  434. (insert ";;; For more information see (info \"(emacs) Directory Variables\")\n\n")
  435. (pp (sort variables
  436. (lambda (a b)
  437. (cond
  438. ((null (car a)) t)
  439. ((null (car b)) nil)
  440. ((and (symbolp (car a)) (stringp (car b))) t)
  441. ((and (symbolp (car b)) (stringp (car a))) nil)
  442. (t (string< (car a) (car b))))))
  443. (current-buffer)))))
  444. ;;;###autoload
  445. (defun add-dir-local-variable (mode variable value)
  446. "Add directory-local VARIABLE with its VALUE and MODE to .dir-locals.el."
  447. (interactive
  448. (let (variable)
  449. (list
  450. (read-file-local-variable-mode)
  451. (setq variable (read-file-local-variable "Add directory-local variable"))
  452. (read-file-local-variable-value variable))))
  453. (modify-dir-local-variable mode variable value 'add-or-replace))
  454. ;;;###autoload
  455. (defun delete-dir-local-variable (mode variable)
  456. "Delete all MODE settings of file-local VARIABLE from .dir-locals.el."
  457. (interactive
  458. (list
  459. (read-file-local-variable-mode)
  460. (read-file-local-variable "Delete directory-local variable")))
  461. (modify-dir-local-variable mode variable nil 'delete))
  462. ;;;###autoload
  463. (defun copy-file-locals-to-dir-locals ()
  464. "Copy file-local variables to .dir-locals.el."
  465. (interactive)
  466. (dolist (elt file-local-variables-alist)
  467. (unless (assq (car elt) dir-local-variables-alist)
  468. (add-dir-local-variable major-mode (car elt) (cdr elt)))))
  469. ;;;###autoload
  470. (defun copy-dir-locals-to-file-locals ()
  471. "Copy directory-local variables to the Local Variables list."
  472. (interactive)
  473. (dolist (elt dir-local-variables-alist)
  474. (add-file-local-variable (car elt) (cdr elt))))
  475. ;;;###autoload
  476. (defun copy-dir-locals-to-file-locals-prop-line ()
  477. "Copy directory-local variables to the -*- line."
  478. (interactive)
  479. (dolist (elt dir-local-variables-alist)
  480. (add-file-local-variable-prop-line (car elt) (cdr elt))))
  481. ;;; connection-local variables.
  482. ;;;###autoload
  483. (defvar enable-connection-local-variables t
  484. "Non-nil means enable use of connection-local variables.")
  485. (defvar connection-local-variables-alist nil
  486. "Alist of connection-local variable settings in the current buffer.
  487. Each element in this list has the form (VAR . VALUE), where VAR
  488. is a connection-local variable (a symbol) and VALUE is its value.
  489. The actual value in the buffer may differ from VALUE, if it is
  490. changed by the user.")
  491. (make-variable-buffer-local 'connection-local-variables-alist)
  492. (setq ignored-local-variables
  493. (cons 'connection-local-variables-alist ignored-local-variables))
  494. (defvar connection-local-profile-alist '()
  495. "Alist mapping connection profiles to variable lists.
  496. Each element in this list has the form (PROFILE VARIABLES).
  497. PROFILE is the name of a connection profile (a symbol).
  498. VARIABLES is a list that declares connection-local variables for
  499. PROFILE. An element in VARIABLES is an alist whose elements are
  500. of the form (VAR . VALUE).")
  501. (defvar connection-local-criteria-alist '()
  502. "Alist mapping connection criteria to connection profiles.
  503. Each element in this list has the form (CRITERIA PROFILES).
  504. CRITERIA is a plist identifying a connection and the application
  505. using this connection. Property names might be `:application',
  506. `:protocol', `:user' and `:machine'. The property value of
  507. `:application' is a symbol, all other property values are
  508. strings. All properties are optional; if CRITERIA is nil, it
  509. always applies.
  510. PROFILES is a list of connection profiles (symbols).")
  511. (defsubst connection-local-normalize-criteria (criteria &rest properties)
  512. "Normalize plist CRITERIA according to PROPERTIES.
  513. Return a new ordered plist list containing only property names from PROPERTIES."
  514. (delq
  515. nil
  516. (mapcar
  517. (lambda (property)
  518. (when (and (plist-member criteria property) (plist-get criteria property))
  519. (list property (plist-get criteria property))))
  520. properties)))
  521. (defsubst connection-local-get-profiles (criteria)
  522. "Return the connection profiles list for CRITERIA.
  523. CRITERIA is a plist identifying a connection and the application
  524. using this connection, see `connection-local-criteria-alist'."
  525. (or (cdr
  526. (assoc
  527. (connection-local-normalize-criteria
  528. criteria :application :protocol :user :machine)
  529. connection-local-criteria-alist))
  530. ;; Try it without :application.
  531. (cdr
  532. (assoc
  533. (connection-local-normalize-criteria criteria :protocol :user :machine)
  534. connection-local-criteria-alist))))
  535. ;;;###autoload
  536. (defun connection-local-set-profiles (criteria &rest profiles)
  537. "Add PROFILES for CRITERIA.
  538. CRITERIA is a plist identifying a connection and the application
  539. using this connection, see `connection-local-criteria-alist'.
  540. PROFILES are the names of connection profiles (a symbol).
  541. When a connection to a remote server is opened and CRITERIA
  542. matches to that server, the connection-local variables from
  543. PROFILES are applied to the corresponding process buffer. The
  544. variables for a connection profile are defined using
  545. `connection-local-set-profile-variables'."
  546. (unless (listp criteria)
  547. (error "Wrong criteria `%s'" criteria))
  548. (dolist (profile profiles)
  549. (unless (assq profile connection-local-profile-alist)
  550. (error "No such connection profile `%s'" (symbol-name profile))))
  551. (let* ((criteria (connection-local-normalize-criteria
  552. criteria :application :protocol :user :machine))
  553. (slot (assoc criteria connection-local-criteria-alist)))
  554. (if slot
  555. (setcdr slot (delete-dups (append (cdr slot) profiles)))
  556. (setq connection-local-criteria-alist
  557. (cons (cons criteria (delete-dups profiles))
  558. connection-local-criteria-alist)))))
  559. (defsubst connection-local-get-profile-variables (profile)
  560. "Return the connection-local variable list for PROFILE."
  561. (cdr (assq profile connection-local-profile-alist)))
  562. ;;;###autoload
  563. (defun connection-local-set-profile-variables (profile variables)
  564. "Map the symbol PROFILE to a list of variable settings.
  565. VARIABLES is a list that declares connection-local variables for
  566. the connection profile. An element in VARIABLES is an alist
  567. whose elements are of the form (VAR . VALUE).
  568. When a connection to a remote server is opened, the server's
  569. connection profiles are found. A server may be assigned a
  570. connection profile using `connection-local-set-profile'. Then
  571. variables are set in the server's process buffer according to the
  572. VARIABLES list of the connection profile. The list is processed
  573. in order."
  574. (setf (alist-get profile connection-local-profile-alist) variables))
  575. (defun hack-connection-local-variables (criteria)
  576. "Read connection-local variables according to CRITERIA.
  577. Store the connection-local variables in buffer local
  578. variable`connection-local-variables-alist'.
  579. This does nothing if `enable-connection-local-variables' is nil."
  580. (when enable-connection-local-variables
  581. ;; Filter connection profiles.
  582. (dolist (profile (connection-local-get-profiles criteria))
  583. ;; Loop over variables.
  584. (dolist (variable (connection-local-get-profile-variables profile))
  585. (unless (assq (car variable) connection-local-variables-alist)
  586. (push variable connection-local-variables-alist))))))
  587. ;;;###autoload
  588. (defun hack-connection-local-variables-apply (criteria)
  589. "Apply connection-local variables identified by CRITERIA.
  590. Other local variables, like file-local and dir-local variables,
  591. will not be changed."
  592. (hack-connection-local-variables criteria)
  593. (let ((file-local-variables-alist
  594. (copy-tree connection-local-variables-alist)))
  595. (hack-local-variables-apply)))
  596. ;;;###autoload
  597. (defmacro with-connection-local-profiles (profiles &rest body)
  598. "Apply connection-local variables according to PROFILES in current buffer.
  599. Execute BODY, and unwind connection-local variables."
  600. (declare (indent 1) (debug t))
  601. `(let ((enable-connection-local-variables t)
  602. (old-buffer-local-variables (buffer-local-variables))
  603. connection-local-variables-alist connection-local-criteria-alist)
  604. (apply 'connection-local-set-profiles nil ,profiles)
  605. (hack-connection-local-variables-apply nil)
  606. (unwind-protect
  607. (progn ,@body)
  608. ;; Cleanup.
  609. (dolist (variable connection-local-variables-alist)
  610. (let ((elt (assq (car variable) old-buffer-local-variables)))
  611. (if elt
  612. (set (make-local-variable (car elt)) (cdr elt))
  613. (kill-local-variable (car variable))))))))
  614. (provide 'files-x)
  615. ;;; files-x.el ends here