all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* etc/TODO: command to make a "Local Variables" section
@ 2005-10-17 16:49 Reiner Steib
  2005-10-18  8:02 ` Juri Linkov
  0 siblings, 1 reply; 2+ messages in thread
From: Reiner Steib @ 2005-10-17 16:49 UTC (permalink / raw)


Hi,

in etc/TODO we have:

,----[ etc/TODO ]
| * Important features:
| 
| [...]
| 
| ** Add a command to make a "Local Variables" section in the current buffer
|   and/or add a variable to the list.
`----

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.

--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)))
		 (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")
      (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))))
--8<---------------cut here---------------end--------------->8---

Bye, Reiner.
\f
-- 
       ,,,
      (o o)
---ooO-(_)-Ooo---  |  PGP key available  |  http://rsteib.home.pages.de/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: etc/TODO: command to make a "Local Variables" section
  2005-10-17 16:49 etc/TODO: command to make a "Local Variables" section Reiner Steib
@ 2005-10-18  8:02 ` Juri Linkov
  0 siblings, 0 replies; 2+ messages in thread
From: Juri Linkov @ 2005-10-18  8:02 UTC (permalink / raw)


> 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/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-10-18  8:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-10-17 16:49 etc/TODO: command to make a "Local Variables" section Reiner Steib
2005-10-18  8:02 ` Juri Linkov

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.