home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / misc / hack-locals.el < prev    next >
Encoding:
Text File  |  1991-07-07  |  4.6 KB  |  121 lines

  1. ; Newsgroups: dg.ml.emacs.vm
  2. ; From: mike-w@cs.aukuni.ac.nz
  3. ; Subject: Re: inhibit-local-variables on mailboxes again
  4. ; Organization: University of Auckland, New Zealand.
  5. ; Date: 24 Jun 91 10:10:07
  6. ;     Here's a modified version of Carl Witty's modified version of
  7. ;   hack-local-variables (Dan suggested I pass it on).  It supports two
  8. ;   additional values for inhibit-local-variables:
  9. ;       'always    means never obey "Local Variables" section, and don't
  10. ;                 bother querying the user.  (I thought that setting this
  11. ;                 value in vm-mode-hooks would solve Dan's problem, but
  12. ;                 unfortunately vm explicitly let-binds
  13. ;                 inhibit-local-variables to t.)
  14. ;       'query-other
  15. ;                 means only bother querying the user if the file is owned by
  16. ;                 someone else (helps prevent Trojan Horses without getting
  17. ;                 in the way too much).
  18. ; --- CUT HERE --------------------------------------------------------------
  19. ;; LCD Archive Entry:
  20. ;; hack-locals|Carl Witty,Mike Williams
  21. ;; |cwitty@csli.stanford.edu,mike-w@cs14b.cs.aukuni.ac.nz
  22. ;; |Show local variables when present, more options for inhibit-local-variables
  23. ;; |91-04-26||~/misc/hack-locals.el.Z
  24.  
  25. ;; Modified by cwitty@csli.Stanford.EDU (Carl Witty) on 12/4/88
  26. ;; to show "Local Variables" section of file on screen before
  27. ;; asking whether to use them (when inhibit-local-variables set).
  28.  
  29. ;; Mike Williams (mike-w@cs14b.cs.aukuni.ac.nz) - Fri Apr 26 15:08:12 1991 
  30. ;; inhibit-local-variables can have the values
  31. ;;    nil         ... always set
  32. ;;    'query, t        ... always query
  33. ;;    'query-other    ... query if not file's owner
  34. ;;    'always        ... always inhibit
  35.  
  36. (provide 'hack-locals)
  37.  
  38. (defun hack-local-variables (&optional force)
  39.   "Parse, and bind or evaluate as appropriate, any local variables
  40. for current buffer."
  41.   ;; Look for "Local variables:" line in last page.
  42.   (save-excursion
  43.     (goto-char (point-max))
  44.     (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
  45.     (if (and 
  46.      (let ((case-fold-search t)) (search-forward "Local Variables:" nil t))
  47.      (or force (not inhibit-local-variables)
  48.          (and (eq inhibit-local-variables 'query-other) 
  49.           buffer-file-name
  50.           (eq (nth 2 (file-attributes (buffer-file-name))) 
  51.               (user-uid)))
  52.          (if (eq inhibit-local-variables 'always) nil
  53.            (save-window-excursion
  54.          (switch-to-buffer (current-buffer) t)
  55.          (recenter 0)
  56.          (y-or-n-p 
  57.           (format 
  58.            "Set local variables as specified at end of %s? "
  59.            (file-name-nondirectory buffer-file-name)))))))
  60.     (let ((continue t)
  61.           prefix prefixlen suffix beg)
  62.       ;; The prefix is what comes before "local variables:" in its line.
  63.       ;; The suffix is what comes after "local variables:" in its line.
  64.       (skip-chars-forward " \t")
  65.       (or (eolp)
  66.           (setq suffix (buffer-substring (point)
  67.                          (progn (end-of-line) (point)))))
  68.       (goto-char (match-beginning 0))
  69.       (or (bolp)
  70.           (setq prefix
  71.             (buffer-substring (point)
  72.                       (progn (beginning-of-line) (point)))))
  73.       (if prefix (setq prefixlen (length prefix)
  74.                prefix (regexp-quote prefix)))
  75.       (if suffix (setq suffix (regexp-quote suffix)))
  76.       (while continue
  77.         ;; Look at next local variable spec.
  78.         (if selective-display (re-search-forward "[\n\C-m]")
  79.           (forward-line 1))
  80.         ;; Skip the prefix, if any.
  81.         (if prefix
  82.         (if (looking-at prefix)
  83.             (forward-char prefixlen)
  84.           (error "Local variables entry is missing the prefix")))
  85.         ;; Find the variable name; strip whitespace.
  86.         (skip-chars-forward " \t")
  87.         (setq beg (point))
  88.         (skip-chars-forward "^:\n")
  89.         (if (eolp) (error "Missing colon in local variables entry"))
  90.         (skip-chars-backward " \t")
  91.         (let* ((str (buffer-substring beg (point)))
  92.            (var (read str))
  93.           val)
  94.           ;; Setting variable named "end" means end of list.
  95.           (if (string-equal (downcase str) "end")
  96.           (setq continue nil)
  97.         ;; Otherwise read the variable value.
  98.         (skip-chars-forward "^:")
  99.         (forward-char 1)
  100.         (setq val (read (current-buffer)))
  101.         (skip-chars-backward "\n")
  102.         (skip-chars-forward " \t")
  103.         (or (if suffix (looking-at suffix) (eolp))
  104.             (error "Local variables entry is terminated incorrectly"))
  105.         ;; Set the variable.  "Variables" mode and eval are funny.
  106.         (cond ((eq var 'mode)
  107.                (funcall (intern (concat (downcase (symbol-name val))
  108.                         "-mode"))))
  109.               ((eq var 'eval)
  110.                (if (string= (user-login-name) "root")
  111.                (message "Ignoring `eval:' in file's local variables")
  112.              (eval val)))
  113.               (t (make-local-variable var)
  114.              (set var val))))))))))
  115.  
  116. ;;=== END of hack-locals.el ===============================================
  117.