emacs-devel
[Top][All Lists]
Advanced

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

[PATCH] allow function values for `enable-local-eval'


From: Karl Fogel
Subject: [PATCH] allow function values for `enable-local-eval'
Date: Wed, 26 Jun 2002 18:11:46 -0500

This patch allows users to set `enable-local-eval' to a function.  If
the function returns non-nil, the eval happens, otherwise it does not.

Example motivation, taken from real life:

We have a project where virtually every source file has some "eval:"
forms.  The constant y-or-n-p querying gets to be a real pain.
Especially when running (say) tags-query-replace, because the meaning
of "y"/"n" swings unpredictably between "Do or don't eval local
variables" and "Do or don't perform this replacement."  The user must
stay preternaturally alert to avoid making mistakes under such
circumstances.  And setting enable-local-eval to `t' is too dangerous
for the security-conscious, of course.

I believe this patch is backwards compatible in practice.  True, if
anyone has been setting `enable-local-eval' to a function before now,
then Emacs' behavior would change for them.  But surely no one has
been doing that, because it would have been pointless.

Two minor issues:

   - I'm not sure whether the change to forms.el (the only other
     non-trivial user of `enable-local-eval' in the Lisp code) is
     truly called for, although I felt it more consistent to make the
     change than not to.  Thoughts?

   - Not sure what, if anything, should be done about the
     customization type for `enable-local-eval' with this patch.

Comments welcome, thanks.

-Karl

2002-06-26  Karl Fogel  <address@hidden>

        * files.el (enable-local-eval): Document new behavior if value is
        a function.  Offer a tentative customization type for function,
        but keep it commented out, as not sure whether it's useful given
        that one still has to write the function.
        (hack-one-local-variable): Handle case where enable-local-eval is
        a function.

        * forms.el (forms-mode): Handle case where enable-local-eval is a
        function.

--- files.el.~1.586.~   Wed Jun 26 11:03:51 2002
+++ files.el    Wed Jun 26 18:03:21 2002
@@ -423,15 +423,27 @@
 
 (defcustom enable-local-eval 'maybe
   "*Control processing of the \"variable\" `eval' in a file's local variables.
-The value can be t, nil or something else.
-A value of t means obey `eval' variables;
-nil means ignore them; anything else means query.
+The value can be t, nil, a function, or something else.
+
+A value of t means obey `eval' variables; nil means ignore them.
+
+If the value is a function, invoke the function in the file's buffer
+once for each expression to be evaluated, passing the expression as
+the only argument.  If the function returns non-nil, evaluate that
+expression, else ignore it.  (If the function errors, or an evaluation
+errors, then no further processing is done for `eval' variables in
+this buffer.)
+
+Any other kind of value means query.
 
 The command \\[normal-mode] always obeys local-variables lists
 and ignores this variable."
-  :type '(choice (const :tag "Obey" t)
-                (const :tag "Ignore" nil)
-                (other :tag "Query" other))
+  :type '(choice (const    :tag "Obey" t)
+                (const    :tag "Ignore" nil)
+                 ;; Any point adding a customization type for the
+                 ;; function possibility?
+                 ;; (function :tag "Function" nil)
+                (other    :tag "Query" other))
   :group 'find-file)
 
 ;; Avoid losing in versions where CLASH_DETECTION is disabled.
@@ -2073,16 +2085,21 @@
                 (and (not (zerop (user-uid)))
                      (or (eq enable-local-eval t)
                          (and enable-local-eval
-                              (save-window-excursion
-                                (switch-to-buffer (current-buffer))
-                                (save-excursion
-                                  (beginning-of-line)
-                                  (set-window-start (selected-window) (point)))
-                                (setq enable-local-eval
-                                      (y-or-n-p (format "Process `eval' or 
hook local variables in %s? "
-                                                        (if buffer-file-name
-                                                            (concat "file " 
(file-name-nondirectory buffer-file-name))
-                                                          (concat "buffer " 
(buffer-name)))))))))))
+                               (if (functionp enable-local-eval)
+                                   ;; Return whatever the function returns...
+                                   (funcall enable-local-eval val)
+                                 ;; ...else *set* enable-local-eval
+                                 ;; to the result of an interactive query:
+                                 (save-window-excursion
+                                   (switch-to-buffer (current-buffer))
+                                   (save-excursion
+                                     (beginning-of-line)
+                                     (set-window-start (selected-window) 
(point)))
+                                   (setq enable-local-eval
+                                         (y-or-n-p (format "Process `eval' or 
hook local variables in %s? "
+                                                           (if buffer-file-name
+                                                               (concat "file " 
(file-name-nondirectory buffer-file-name))
+                                                             (concat "buffer " 
(buffer-name))))))))))))
             (if (eq var 'eval)
                 (save-excursion (eval val))
               (make-local-variable var)


--- forms.el.~2.43.~    Thu Jun  6 19:24:24 2002
+++ forms.el    Wed Jun 26 17:06:23 2002
@@ -519,11 +519,12 @@
 
        ;; eval the buffer, should set variables
        ;;(message "forms: processing control file...")
-       ;; If enable-local-eval is not set to t the user is asked first.
        (if (or (eq enable-local-eval t)
-               (yes-or-no-p 
-                (concat "Evaluate lisp code in buffer "
-                        (buffer-name) " to display forms ")))
+                (if (functionp enable-local-eval)
+                    (funcall enable-local-eval val)
+                  (yes-or-no-p 
+                   (concat "Evaluate lisp code in buffer "
+                           (buffer-name) " to display forms "))))
            (eval-current-buffer)
          (error "`enable-local-eval' inhibits buffer evaluation"))
 



reply via email to

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