Archive for August, 2007

Today I wrote an extension for emacs that lets you view pdf, ps or dvi documents in emacs. It’s named doc-view.el and I posted it to the emacs-sources mailing list (here it is). But I recommend that you get the latest version by cloning my Git repository.

A description of doc-view.el and how to get it can be found on my homepage.

UPDATE: doc-view is official part of Emacs and the default viewer for DVI and PDF files.

Directory local variables

Posted: August 14, 2007 in Emacs

You probably know file local variables. If not read (info "(emacs)File Variables"). They’re quite handy if you work on projects that use different coding conventions, e.g. some indent with tabs and others don’t. The problem with this approach is that it requires you to modify the files.

To escape this restriction I came up with this:

(defvar th-dir-local-variables-alist
  '(("~/repos/gnus/" . ((indent-tabs-mode . t)
                        (tab-width . 8))))
  "An alist with (PATH . LIST) pairs.  PATH is a path and LIST is
a list of variables to set locally for files below that path.  It
has elements of the form (VAR . VAL) where VAR is a symbol and
VAL is its value.")

(defun th-set-dir-local-variables ()
  "Locally set the variables defined in
`th-dir-local-variables-alist' for the current buffer."
  (interactive)
  (let ((file (buffer-file-name (current-buffer))))
    (when file
      (dolist (pair th-dir-local-variables-alist)
        (when (string-match (concat "^" (regexp-quote (expand-file-name (car pair))))
                            file)
          (dolist (var (cdr pair))
            (if (local-variable-if-set-p (car var))
                (set (car var) (cdr var))
              (set (make-local-variable (car var)) (cdr var)))))))))

(add-hook 'find-file-hook
          'th-set-dir-local-variables)