Function: hack-local-variables

hack-local-variables is a byte-compiled function defined in files.el.gz.

Signature

(hack-local-variables &optional HANDLE-MODE INHIBIT-LOCALS)

Documentation

Parse and put into effect this buffer's local variables spec.

Also puts into effect directory-local variables. For buffers not visiting files, apply the directory-local variables that would be applicable to files in default-directory.

Uses hack-local-variables-apply and hack-dir-local-variables to apply the variables.

If enable-local-variables or local-enable-local-variables is nil, or INHIBIT-LOCALS is non-nil, this function disregards all normal local variables. If inhibit-local-variables-regexps applies to the file in question, the file is not scanned for local variables, but directory-local variables may still be applied.

Variables present in permanently-enabled-local-variables will still be evaluated, even if local variables are otherwise inhibited.

If HANDLE-MODE is t, the function only checks whether a "mode:" is specified, and returns the corresponding mode symbol, or nil. In this case, try to ignore minor-modes, and return only a major-mode. If HANDLE-MODE is nil, the function gathers all the specified local variables. If HANDLE-MODE is neither nil nor t, the function gathers all the specified local variables, but ignores any settings of "mode:".

View in manual

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun hack-local-variables (&optional handle-mode inhibit-locals)
  "Parse and put into effect this buffer's local variables spec.
Also puts into effect directory-local variables.
For buffers not visiting files, apply the directory-local variables that
would be applicable to files in `default-directory'.

Uses `hack-local-variables-apply' and `hack-dir-local-variables'
to apply the variables.

If `enable-local-variables' or `local-enable-local-variables' is
nil, or INHIBIT-LOCALS is non-nil, this function disregards all
normal local variables.  If `inhibit-local-variables-regexps'
applies to the file in question, the file is not scanned for
local variables, but directory-local variables may still be
applied.

Variables present in `permanently-enabled-local-variables' will
still be evaluated, even if local variables are otherwise
inhibited.

If HANDLE-MODE is t, the function only checks whether a \"mode:\"
is specified, and returns the corresponding mode symbol, or nil.
In this case, try to ignore minor-modes, and return only a major-mode.
If HANDLE-MODE is nil, the function gathers all the specified local
variables.  If HANDLE-MODE is neither nil nor t, the function gathers
all the specified local variables, but ignores any settings of \"mode:\"."
  ;; We don't let inhibit-local-variables-p influence the value of
  ;; enable-local-variables, because then it would affect dir-local
  ;; variables.  We don't want to search eg tar files for file local
  ;; variable sections, but there is no reason dir-locals cannot apply
  ;; to them.  The real meaning of inhibit-local-variables-p is "do
  ;; not scan this file for local variables".
  (let ((enable-local-variables
	 (and (not inhibit-locals)
              local-enable-local-variables enable-local-variables)))
    (if (eq handle-mode t)
        ;; We're looking just for the major mode setting.
        (and enable-local-variables
             (not (inhibit-local-variables-p))
	     ;; If HANDLE-MODE is t, and the prop line specifies a
	     ;; mode, then we're done, and have no need to scan further.
             (or (hack-local-variables-prop-line t)
                 ;; Look for the mode elsewhere in the buffer.
                 (hack-local-variables--find-variables t)))
      ;; Normal handling of local variables.
      (setq file-local-variables-alist nil)
      (when (and (file-remote-p default-directory)
                 (fboundp 'hack-connection-local-variables)
                 (fboundp 'connection-local-criteria-for-default-directory))
        (with-demoted-errors "Connection-local variables error: %s"
	  ;; Note this is a no-op if enable-local-variables is nil.
	  (hack-connection-local-variables
           (connection-local-criteria-for-default-directory))))
      (with-demoted-errors "Directory-local variables error: %s"
	;; Note this is a no-op if enable-local-variables is nil.
	(hack-dir-local-variables))
      (let ((result (append (hack-local-variables--find-variables handle-mode)
                            (hack-local-variables-prop-line handle-mode))))
        (if (and enable-local-variables
                 (not (inhibit-local-variables-p)))
            (progn
	      ;; Set the variables.
	      (hack-local-variables-filter result nil)
	      (hack-local-variables-apply))
          ;; Handle `lexical-binding' and other special local
          ;; variables.
          (dolist (variable permanently-enabled-local-variables)
            (when-let* ((elem (assq variable result)))
              (push elem file-local-variables-alist)))
          (hack-local-variables-apply))))))