From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.devel Subject: Re: Add file-locals and dir-locals Date: Sun, 19 Jul 2009 21:12:11 +0300 Organization: JURTA Message-ID: <87tz187dac.fsf@mail.jurta.org> References: <20090712144137.GB2524@muc.de> <87hbxhony6.fsf@stupidchicken.com> <20090712180623.GA1009@muc.de> <4A5A2BE1.3050206@harpegolden.net> <87ab34p0o4.fsf_-_@mail.jurta.org> <4A602EF9.9070306@gmx.at> <87d47z3584.fsf@mail.jurta.org> <4A61D293.6020105@gmx.at> <87ocrhk53y.fsf@mail.jurta.org> <4A62F78A.7030201@gmx.at> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1248027299 15659 80.91.229.12 (19 Jul 2009 18:14:59 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 19 Jul 2009 18:14:59 +0000 (UTC) Cc: emacs-devel@gnu.org To: martin rudalics Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Jul 19 20:14:51 2009 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1MSauO-0006PO-FK for ged-emacs-devel@m.gmane.org; Sun, 19 Jul 2009 20:14:49 +0200 Original-Received: from localhost ([127.0.0.1]:33250 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MSauN-0007SE-H2 for ged-emacs-devel@m.gmane.org; Sun, 19 Jul 2009 14:14:47 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MSauI-0007S9-FA for emacs-devel@gnu.org; Sun, 19 Jul 2009 14:14:42 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MSauE-0007Ro-PO for emacs-devel@gnu.org; Sun, 19 Jul 2009 14:14:42 -0400 Original-Received: from [199.232.76.173] (port=51712 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MSauE-0007Rl-Jd for emacs-devel@gnu.org; Sun, 19 Jul 2009 14:14:38 -0400 Original-Received: from smtp-out1.starman.ee ([85.253.0.3]:50142 helo=mx1.starman.ee) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MSauD-0001BH-NX for emacs-devel@gnu.org; Sun, 19 Jul 2009 14:14:38 -0400 X-Virus-Scanned: by Amavisd-New at mx1.starman.ee Original-Received: from mail.starman.ee (82.131.55.64.cable.starman.ee [82.131.55.64]) by mx1.starman.ee (Postfix) with ESMTP id 132C23F411D; Sun, 19 Jul 2009 21:14:31 +0300 (EEST) In-Reply-To: <4A62F78A.7030201@gmx.at> (martin rudalics's message of "Sun, 19 Jul 2009 12:38:02 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (x86_64-pc-linux-gnu) X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:112765 Archived-At: > I don't quite catch the idea behind > > (and (search-backward "Local Variables:" nil t) > (search-forward "Local Variables:" nil t))) > > Do you search the entire buffer here? I had the same doubt that this is sub-ideal. Thanks, fixed. > Also I didn't find a functionality to remove the headers and trailers as > soon as all settings have been removed. I intentionally omitted this functionality because I think it should leave a harmless empty placeholder for the user's convenience. Below is the final patch that I find useful enough to commit. I invite you and other developers to improve this further as you wish. Index: etc/TODO =================================================================== RCS file: /sources/emacs/emacs/etc/TODO,v retrieving revision 1.205 diff -u -r1.205 TODO --- etc/TODO 18 Jul 2009 21:04:15 -0000 1.205 +++ etc/TODO 19 Jul 2009 18:05:50 -0000 @@ -202,9 +202,6 @@ ** Implement something better than the current Refill mode. This probably needs some primitive support. -** Add a command to make a "Local Variables" section in the current buffer - and/or add a variable to the list. - ** Implement primitive and higher-level functions to allow filling properly with variable-pitch faces. Index: etc/NEWS =================================================================== RCS file: /sources/emacs/emacs/etc/NEWS,v retrieving revision 1.2046 diff -c -r1.2046 NEWS *** etc/NEWS 19 Jul 2009 00:29:52 -0000 1.2046 --- etc/NEWS 19 Jul 2009 18:05:32 -0000 *************** *** 58,63 **** --- 58,72 ---- `process-kill-buffer-query-function' from `kill-buffer-query-functions' or setting the appropriate process flag with `set-process-query-on-exit-flag'. + ** New command to add a file-local variable to the "Local Variables" list + in the current buffer is `add-file-local-variable'. New command to remove + a file-local variable from the "Local Variables" list is + `delete-file-local-variable'. New commands to add/remove + a directory-local variable to/from the .dir-locals.el file are + `add-dir-local-variable' and `delete-dir-local-variable'. New commands to + copy file-local variables to directory-local variables and vice versa are + `copy-file-locals-to-dir-locals' and `copy-dir-locals-to-file-locals'. + * Changes in Specialized Modes and Packages in Emacs 23.2 Index: lisp/files.el =================================================================== RCS file: /sources/emacs/emacs/lisp/files.el,v retrieving revision 1.1059 diff -c -r1.1059 files.el *** lisp/files.el 19 Jul 2009 16:55:16 -0000 1.1059 --- lisp/files.el 19 Jul 2009 18:04:44 -0000 *************** *** 3407,3412 **** --- 3407,3699 ---- (hack-local-variables-filter variables dir-name))))))) + ;;; Commands to add/delete file-local/directory-local variables. + + (defun read-file-local-variable (prompt) + "Read file-local variable using PROMPT and completion. + Intended to be used in the `interactive' spec of + `add-file-local-variable', `delete-file-local-variable', + `add-dir-local-variable', `delete-dir-local-variable'." + (let (default variable) + (setq default (variable-at-point)) + (setq default (and (symbolp default) (boundp default) + (symbol-name default))) + (setq variable + (completing-read + (if default + (format "%s (default %s): " prompt default) + (format "%s: " prompt)) + obarray + (lambda (sym) + (or (user-variable-p sym) + (memq sym '(mode eval coding unibyte)))) + nil nil nil default nil)) + (and (stringp variable) (intern variable)))) + + (defun read-file-local-variable-value (variable) + "Read value of file-local VARIABLE using completion. + Intended to be used in the `interactive' spec of + `add-file-local-variable' and `add-dir-local-variable'." + (let (default value) + (cond + ((eq variable 'mode) + (setq default (and (symbolp major-mode) (symbol-name major-mode))) + (setq value + (completing-read + (if default + (format "Add %s with value (default %s): " variable default) + (format "Add %s with value: " variable)) + obarray + (lambda (sym) + (and (string-match-p "-mode\\'" (symbol-name sym)) + (not (string-match-p "-minor-mode\\'" (symbol-name sym))))) + nil nil nil default nil)) + (and (stringp value) + (intern (replace-regexp-in-string "-mode\\'" "" value)))) + ((eq variable 'eval) + (let ((minibuffer-completing-symbol t)) + (read-from-minibuffer (format "Add %s with expression: " variable) + nil read-expression-map t + 'read-expression-history))) + ((eq variable 'coding) + (setq default (and (symbolp buffer-file-coding-system) + (symbol-name buffer-file-coding-system))) + (read-coding-system + (if default + (format "Add %s with value (default %s): " variable default) + (format "Add %s with value: " variable)) + default)) + (t + (read (read-string (format "Add %s with value: " variable) + nil 'set-variable-value-history + (format "%S" (symbol-value variable)))))))) + + (defun read-file-local-variable-mode () + "Read per-directory file-local variable's mode using completion. + Intended to be used in the `interactive' spec of + `add-dir-local-variable', `delete-dir-local-variable'." + (let* ((default (and (symbolp major-mode) (symbol-name major-mode))) + (mode + (completing-read + (if default + (format "Mode or subdirectory (default %s): " default) + (format "Mode or subdirectory: ")) + obarray + (lambda (sym) + (and (string-match-p "-mode\\'" (symbol-name sym)) + (not (string-match-p "-minor-mode\\'" (symbol-name sym))))) + nil nil nil default nil))) + (cond + ((equal mode "nil") nil) + ((and (stringp mode) (fboundp (intern mode))) (intern mode)) + (t mode)))) + + (defun modify-file-local-variable (variable value op) + "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 `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." + (catch 'exit + (let ((startpos (point)) endpos replaced-p) + (unless enable-local-variables + (throw 'exit (message "File-local variables are disabled"))) + + ;; Look for "Local variables:" line in last page. + (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 startpos) + (message "Local Variables not found")))) + + (goto-char (point-max)) + (let ((comment-style 'plain) + (comment-start (or comment-start ";;; "))) + (comment-region + (prog1 (setq startpos (point)) + (insert "\nLocal Variables:\nEnd:\n")) + (point))) + + (unless (let ((case-fold-search t)) + (goto-char startpos) + (search-forward "Local Variables:" nil t)) + (throw 'exit (message "Can't add file-local variables")))) + + ;; suffix is what comes after "local variables:" in its line. + ;; prefix is what comes before "local variables:" in its line. + (let* ((suffix (buffer-substring (point) (line-end-position))) + (prefix (buffer-substring (line-beginning-position) + (match-beginning 0))) + (suffix-re (concat (regexp-quote suffix) "$")) + (prefix-re (concat "^" (regexp-quote prefix)))) + + ;; Find or add missing "End:". + (forward-line 1) + (setq startpos (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 endpos (point-marker))) + + ;; Find and delete all existing variable/value pairs. + (when (member op '(add-or-replace delete)) + (if (and (eq variable 'eval) (eq op 'add-or-replace)) + (goto-char endpos) + (goto-char startpos) + (while (re-search-forward + (format "%s%S:.*%s" prefix-re variable suffix-re) endpos t) + (delete-region (match-beginning 0) (1+ (match-end 0))) + (setq replaced-p t)))) + + ;; 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 startpos)) + ((null replaced-p) (goto-char endpos))) + (insert (format "%s%S: %S%s\n" prefix variable value suffix))))))) + + (defun add-file-local-variable (variable value) + "Add file-local VARIABLE with its VALUE to the Local Variables list. + + This command deletes all existing settings of VARIABLE (except `eval') + and adds a new file-local VARIABLE with VALUE to the Local Variables list. + + If there is no Local Variables list in the current file buffer + then this function adds the first line containing the string + `Local Variables:' and the last line containing the string `End:'." + (interactive + (let ((variable (read-file-local-variable "Add file-local variable"))) + (list variable (read-file-local-variable-value variable)))) + (modify-file-local-variable variable value 'add-or-replace)) + + (defun delete-file-local-variable (variable) + "Delete all settings of file-local VARIABLE from the Local Variables list." + (interactive + (list (read-file-local-variable "Delete file-local variable"))) + (modify-file-local-variable variable nil 'delete)) + + (defun modify-dir-local-variable (mode variable value op) + "Modify directory-local VARIABLE in .dir-locals.el depending on operation OP. + + If OP is `add-or-replace' then delete all existing settings of + VARIABLE (except `eval') and add a new directory-local VARIABLE with VALUE + to the MODE alist where MODE can be a mode name symbol or a subdirectory name. + + If .dir-locals.el was not found and OP is not `delete' then create + this file in the current directory. + + If OP is `delete' then delete all existing settings of VARIABLE + from the the MODE alist ignoring the input argument VALUE." + (catch 'exit + (let (replaced-p) + (unless enable-local-variables + (throw 'exit (message "Directory-local variables are disabled"))) + + (let ((variables-file (or (and (buffer-file-name) + (not (file-remote-p (buffer-file-name))) + (dir-locals-find-file (buffer-file-name))) + dir-locals-file)) + variables) + + ;; Don't create ".dir-locals.el" for the deletion operation. + (when (and (eq op 'delete) + (not (file-exists-p variables-file))) + (throw 'exit (message "File .dir-locals.el not found"))) + + (let ((auto-insert nil)) + (find-file variables-file)) + (goto-char (point-min)) + + ;; Read alist of directory-local variables. + (ignore-errors + (delete-region + (prog1 (point) + (setq variables (let ((read-circle nil)) + (read (current-buffer))))) + (point))) + + ;; Add or replace variable in alist of directory-local variables. + (let ((mode-assoc (assoc mode variables))) + (if mode-assoc + (setq variables + (cons (cons mode + (if (eq op 'delete) + (assq-delete-all variable (cdr mode-assoc)) + (cons + (cons variable value) + (if (eq variable 'eval) + (cdr mode-assoc) + (assq-delete-all variable (cdr mode-assoc)))))) + (assq-delete-all mode variables))) + (setq variables + (cons `(,mode . ((,variable . ,value))) + variables)))) + + ;; Insert modified alist of directory-local variables. + (insert ";;; Directory Local Variables\n") + (insert ";;; See (info \"(emacs) Directory Variables\")\n\n") + (pp (sort variables + (lambda (a b) + (cond + ((null (car a)) t) + ((null (car b)) nil) + ((and (symbolp (car a)) (stringp (car b))) t) + ((and (symbolp (car b)) (stringp (car a))) nil) + (t (string< (car a) (car b)))))) + (current-buffer)))))) + + (defun add-dir-local-variable (mode variable value) + "Add directory-local VARIABLE with its VALUE and MODE to .dir-locals.el." + (interactive + (let (variable) + (list + (read-file-local-variable-mode) + (setq variable (read-file-local-variable "Add directory-local variable")) + (read-file-local-variable-value variable)))) + (modify-dir-local-variable mode variable value 'add-or-replace)) + + (defun delete-dir-local-variable (mode variable) + "Delete all MODE settings of file-local VARIABLE from .dir-locals.el." + (interactive + (list + (read-file-local-variable-mode) + (read-file-local-variable "Delete directory-local variable"))) + (modify-dir-local-variable mode variable nil 'delete)) + + (defun copy-file-locals-to-dir-locals () + "Copy file-local variables to .dir-locals.el." + (interactive) + (dolist (elt file-local-variables-alist) + (unless (assq (car elt) dir-local-variables-alist) + (add-dir-local-variable major-mode (car elt) (cdr elt))))) + + (defun copy-dir-locals-to-file-locals () + "Copy directory-local variables to the Local Variables list." + (interactive) + (dolist (elt dir-local-variables-alist) + (add-file-local-variable (car elt) (cdr elt)))) + + (defcustom change-major-mode-with-file-name t "Non-nil means \\[write-file] should set the major mode from the file name. However, the mode will not be changed if -- Juri Linkov http://www.jurta.org/emacs/