Function: modify-file-local-variable
modify-file-local-variable is a byte-compiled function defined in
files-x.el.gz.
Signature
(modify-file-local-variable VARIABLE VALUE OP &optional INTERACTIVE)
Documentation
Modify file-local VARIABLE in Local Variables depending on operation OP.
If OP is add-or-replace then delete all existing settings of
VARIABLE (except mode and eval) and add a new file-local VARIABLE
with VALUE to the Local Variables list.
If there is no Local Variables list in the current file buffer and OP
is not delete then this function adds the first line containing the
string Local Variables: and the last line containing the string End:.
If OP is delete then delete all existing settings of VARIABLE
from the Local Variables list ignoring the input argument VALUE.
If optional variable INTERACTIVE is non-nil, display a message telling the user how to make the new value take effect.
Source Code
;; Defined in /usr/src/emacs/lisp/files-x.el.gz
(defun modify-file-local-variable (variable value op &optional interactive)
"Modify file-local VARIABLE in Local Variables depending on operation OP.
If OP is `add-or-replace' then delete all existing settings of
VARIABLE (except `mode' and `eval') and add a new file-local VARIABLE
with VALUE to the Local Variables list.
If there is no Local Variables list in the current file buffer and OP
is not `delete' then this function adds the first line containing the
string `Local Variables:' and the last line containing the string `End:'.
If OP is `delete' then delete all existing settings of VARIABLE
from the Local Variables list ignoring the input argument VALUE.
If optional variable INTERACTIVE is non-nil, display a message telling
the user how to make the new value take effect."
(catch 'exit
(let ((beg (point)) end replaced-pos)
(unless enable-local-variables
(throw 'exit (message "File-local variables are disabled")))
;; Look for "Local variables:" line in last page.
(widen)
(goto-char (point-max))
(search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
;; Add "Local variables:" list if not found.
(unless (let ((case-fold-search t))
(search-forward "Local Variables:" nil t))
;; Don't add "Local variables:" list for the deletion operation.
(when (eq op 'delete)
(throw 'exit (progn (goto-char beg)
(message "Local Variables not found"))))
(goto-char (point-max))
(let ((comment-style 'plain)
(comment-start (or comment-start ";; ")))
(comment-region
(prog1 (setq beg (point))
(insert "\nLocal Variables:\nEnd:\n"))
(point)))
(unless (let ((case-fold-search t))
(goto-char beg)
(search-forward "Local Variables:" nil t))
(throw 'exit (message "Can't add file-local variables"))))
;; prefix is what comes before "local variables:" in its line.
;; suffix is what comes after "local variables:" in its line.
(let* ((prefix (buffer-substring (line-beginning-position)
(match-beginning 0)))
(suffix (buffer-substring (point) (line-end-position)))
(prefix-re (concat "^" (regexp-quote prefix)))
(suffix-re (concat (regexp-quote suffix) "$")))
;; Find or add missing "End:".
(forward-line 1)
(setq beg (point))
(save-excursion
(unless (let ((case-fold-search t))
(re-search-forward
(concat prefix-re "[ \t]*End:[ \t]*" suffix-re)
nil t))
(save-excursion
(insert (format "%sEnd:%s\n" prefix suffix))))
(beginning-of-line)
(setq end (point-marker)))
;; Find and delete all existing variable/value pairs.
(when (member op '(add-or-replace delete))
(if (and (eq op 'add-or-replace) (memq variable '(mode eval)))
(goto-char end)
(goto-char beg)
(while (re-search-forward
(format "%s%S:.*%s" prefix-re variable suffix-re) end t)
(delete-region (match-beginning 0) (1+ (match-end 0)))
(setq replaced-pos (point)))))
;; Add a new variable/value pair. Add `mode' to the start, add new
;; variable to the end, and add a replaced variable to its last location.
(when (eq op 'add-or-replace)
(cond
((eq variable 'mode) (goto-char beg))
((null replaced-pos) (goto-char end))
(replaced-pos (goto-char replaced-pos)))
(insert (format "%s%S: %S%s\n" prefix variable value suffix))))
(when interactive
(modify-file-local-variable-message variable value op)))))