emacs-devel
[Top][All Lists]
Advanced

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

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


From: Richard Stallman
Subject: Re: [PATCH] allow function values for `enable-local-eval'
Date: Mon, 1 Jul 2002 08:10:00 -0600 (MDT)

    I edited out the details but here it is:

     eval: (c-add-style "StyleX" (quote ((c-basic-offset . 4) (c-offsets-alist 
(label . *) ...))) t)

This suggests we add a feature where a function can be marked
as "ok to call", provided its arguments are all constant.
That way, this would work with no user customization required.
Similar cases with other functions could be enabled globally,
and local customization would be easy too.

We can add something more completely general if desired,
but I suspect this will handle all actual needs.  Its advantage
is that we can make it DTRT for most of the cases, straight out
of the box.

Please try this code.  It seemed to work for me.


(defun hack-one-local-variable-constantp (exp)
  (or (and (not (symbolp exp)) (not (consp exp)))
      (memq exp '(t nil))
      (keywordp exp)
      (hack-one-local-variable-quotep exp)))

(defun hack-one-local-variable-eval-safep (exp)
  "Return t if it is safe to eval EXP when it is found in a file."
  (and (consp exp)
       (or (and (eq (car exp) 'put)
                (hack-one-local-variable-quotep (nth 1 exp))
                (hack-one-local-variable-quotep (nth 2 exp))
                (memq (nth 1 (nth 2 exp))
                      '(lisp-indent-hook))
                ;; Only allow safe expues of lisp-indent-hook;
                ;; not functions.
                (or (numberp (nth 3 exp))
                    (equal (nth 3 exp) ''defun)))
           (and (symbolp (car exp))
                (get (car exp) 'safe-local-eval-function)
                (let ((ok t))
                  (dolist (arg (cdr exp))
                    (unless (hack-one-local-variable-constantp arg)
                      (setq ok nil)))
                  ok)))))

(defun hack-one-local-variable (var val)
  "\"Set\" one variable in a local variables spec.
A few patterns are specified so that any name which matches one
is considered risky."
  (cond ((eq var 'mode)
         (funcall (intern (concat (downcase (symbol-name val))
                                  "-mode"))))
        ((eq var 'coding)
         ;; We have already handled coding: tag in set-auto-coding.
         nil)
        ((memq var ignored-local-variables)
         nil)
        ;; "Setting" eval means either eval it or do nothing.
        ;; Likewise for setting hook variables.
        ((or (get var 'risky-local-variable)
             (and
              (string-match 
"-hooks?$\\|-functions?$\\|-forms?$\\|-program$\\|-command$\\|-predicate$\\|font-lock-keywords$\\|font-lock-keywords-[0-9]+$\\|font-lock-syntactic-keywords$\\|-frame-alist$\\|-mode-alist$\\|-map$\\|-map-alist$"
                            (symbol-name var))
              (not (get var 'safe-local-variable))))
         ;; Permit evalling a put of a harmless property.
         ;; if the args do nothing tricky.
         (if (or (and (eq var 'eval)
                      (hack-one-local-variable-eval-safep val))
                 ;; Permit eval if not root and user says ok.
                 (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 (eq var 'eval)
                 (save-excursion (eval val))
               (make-local-variable var)
               (set var val))
           (message "Ignoring `eval:' in the local variables list")))
        ;; Ordinary variable, really set it.
        (t (make-local-variable var)
           ;; Make sure the string has no text properties.
           ;; Some text properties can get evaluated in various ways,
           ;; so it is risky to put them on with a local variable list.
           (if (stringp val)
               (set-text-properties 0 (length val) nil val))
           (set var val))))



reply via email to

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