網站地圖 最近更新 News ElispArea 教导

DirectoryVariables

See also: LocalVariables, FileLocalVariables

From the manual, Directory Variables

If you put a file with a special name .dir-locals.el in a directory, Emacs will read it when it visits any file in that directory or any of its subdirectories, and apply the settings it specifies to the file’s buffer. Emacs searches for `.dir-locals.el’ starting in the directory of the visited file, and moving up the directory tree.

The `.dir-locals.el’ file should hold a specially-constructed list. This list maps Emacs mode names (symbols) to alists; each alist specifies values for variables to use when the respective mode is turned on. The special mode name ‘nil’ means that its alist applies to any mode. Instead of a mode name, you can specify a string that is a name of a subdirectory of the project’s directory; then the corresponding alist applies to all the files in that subdirectory.

Here’s an example of a `.dir-locals.el’ file:

     ((nil . ((indent-tabs-mode . t)
              (tab-width . 4)
              (fill-column . 80)))
      ;; Warn about spaces used for indentation:
      (haskell-mode . ((eval . (highlight-regexp "^ *"))))
      (c-mode . ((c-file-style . "BSD")))
      (java-mode . ((c-file-style . "BSD")))
      ("src/imported"
       . ((nil . ((change-log-default-name . "ChangeLog.local"))))))

Emacs (23.2.1) will not attempt to apply directory local variables to buffers visiting remote files (via tramp).

As of Emacs 24.3, you can set the variable ‘enable-remote-dir-locals’ to apply such values on remote files. If you’re using an earlier version of Emacs, read on:

We can advise ‘hack-dir-local-variables’ to work around this, if you are willing to incur the associated performance cost (which can be relatively minimal in some situations).

     ;; Enable directory local variables with remote files. This facilitates both
     ;; the (dir-locals-set-class-variables ...)(dir-locals-set-directory-class ...)
     ;; and the dir-locals.el approaches.
     (defadvice hack-dir-local-variables (around my-remote-dir-local-variables)
       "Allow directory local variables with remote files, by temporarily redefining
     `file-remote-p' to return nil unconditionally."
       (flet ((file-remote-p (&rest) nil))
         ad-do-it))
     (ad-activate 'hack-dir-local-variables)

And an example of using this without dir-locals.el:

     (dir-locals-set-class-variables
      'plone-core
      '((nil . ((buffer-read-only . t)))))
     (dir-locals-set-class-variables
      'plone-instance
      '((nil . ((indent-tabs-mode . nil)
                (fill-column . 80)))
        ;; (python-mode . (()))
        ;; (nxhtml-mode . (()))
        ))
     (dir-locals-set-directory-class
      "/scpc:(user)@(host):/home/(user)/Plone/" 'plone-core)
     (dir-locals-set-directory-class
      "/scpc:(user)@(host):/home/(user)/Plone/zinstance/" 'plone-instance)

As of Emacs 24: