emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: etc/TODO: command to make a "Local Variables" section


From: Juri Linkov
Subject: Re: etc/TODO: command to make a "Local Variables" section
Date: Tue, 18 Oct 2005 11:02:04 +0300
User-agent: Gnus/5.110004 (No Gnus v0.4) Emacs/22.0.50 (gnu/linux)

> How about the following?
>
> Upto now, it only adds a new section, but extending the function to
> add a variable to an existing list can be done by using parts of
> `hack-local-variables', AFAICS.

I think this is a good starting point.

As for its top-level interactive usage, I think that instead of
`insert-local-variables' there should be two commands
`insert-local-variable' (inserting only one given variable) and
`insert-local-variables' (inserting all useful local variables).

`insert-local-variables' could look like below:

(defun insert-local-variables ()
  (interactive)
  (insert-local-variable (cons 'mode major-mode))
  (insert-local-variable (cons 'coding buffer-file-coding-system))
  (dolist (variable insert-local-variables-list)
    (if (local-variable-p variable)
        (insert-local-variable variable))))

where `insert-local-variables-list' is just a copy from
`desktop-locals-to-save' which contains all useful local variables:

(defcustom insert-local-variables-list
  '(truncate-lines
    case-fold-search
    case-replace
    fill-column
    overwrite-mode
    change-log-default-name
    line-number-mode
    column-number-mode
    size-indication-mode
    buffer-file-coding-system
    indent-tabs-mode
    indicate-buffer-boundaries
    indicate-empty-lines
    show-trailing-whitespace)
  "List of local variables to save in the local variables section.
The variables are saved only when they really are local."
  :type '(repeat symbol))

Please see also more comments below in regard to your code:

> --8<---------------cut here---------------start------------->8---
> (defun insert-local-variables (&optional formfeed mode coding variables)
>   "Add a \"Local Variables\" section in the current buffer.
>
> If FORMFEED, insert ^L before the \"Local Variables\" section.
> If MODE, insert \"mode:\", if CODING, add \"coding:\".  VARIABLES
> is a list of variables.  For all variables, the values in the
> current buffer are used."
>   (interactive
>    (list (y-or-n-p "Insert page marker (formfeed)? ")
>        (y-or-n-p "Insert mode? ")
>        (y-or-n-p "Insert coding? ")
>        (let ((var t) (lst nil))
>          (while var
>            (if (string=
>                 ""
>                 (setq var
>                       (completing-read "Variable: " obarray 'boundp
>                                        t nil nil nil)))

In `insert-local-variable' (inserting one variable) this interactive
specification could be replaced with just

    (interactive "vLocal Variable: ")

>                (setq var nil)
>              (add-to-list 'lst (intern var))))
>          lst)))
>   (save-excursion
>     (goto-char (point-max))
>     (if (search-backward
>        "Local Variables:"
>        (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) t)
>        t)
>       (error "Buffer already has a \"Local Variables:\" list")

When a buffer already has a "Local Variables:" then it's better to
add a variable to the existing "Local Variables:" or to replace
its value if the same variable already exists in the existing "Local
Variables:" section.

>       (goto-char (point-max)))
>     (when formfeed (insert "\n\f\n"))
>     (set-mark (point))
>     (insert "Local Variables:\n")
>     (when mode
>       (insert (format "mode: %s\n"
>                     (replace-regexp-in-string
>                      "-mode$" ""
>                      (symbol-name major-mode)))))
>     (when coding
>       (insert (format "coding: %s\n"
>                     (symbol-name buffer-file-coding-system))))
>     (dolist (v variables)
>       (insert (format "%s: %S\n" (symbol-name v) (symbol-value v))))
>     (insert "End:\n")
>     (comment-region (mark) (point))))

As Stefan already pointed out recently, comment-region should be
wrapped with an appropriate let binding of comment-style.

> --8<---------------cut here---------------end--------------->8---

-- 
Juri Linkov
http://www.jurta.org/emacs/





reply via email to

[Prev in Thread] Current Thread [Next in Thread]