Go to the first, previous, next, last section, table of contents.

4. Customization -- Emacs Lisp and the .emacs file

4.1. How can `.emacs' determine which of the family of Emacsen I am using?

To determine if you are currently running GNU Emacs 18, GNU Emacs 19, XEmacs 19, or Epoch, and use appropriate code, check out the example given in `etc/sample.emacs'. There are other nifty things in there as well! Alternatively, there is a package, `emacs-vers.el', available at an Emacs-Lisp archive near you; try searching

archive.cis.ohio-state.edu:/pub/gnu/emacs/elisp-archive/misc/

4.2. How can I detect a color display?

You can test the return value of the function (device-class), as in:

(if (eq (device-class) 'color)
    (progn 
      (set-face-foreground  'font-lock-comment-face "Grey")
      (set-face-foreground  'font-lock-string-face "Red")
      ....
      ))

4.3. How can I evaluate Emacs-Lisp expressions without switching to the *scratch* buffer?

(put 'eval-expression 'disabled nil)

This sets it so that hitting ESC ESC lets you type a single expression to be evaluated. This line can also be put into your `.emacs'.

4.4. If you put (setq tab-width 6) in your `.emacs' file it does not work! Is there a reason for this. If you do it at the EVAL prompt it works fine!! How strange.

Use setq-default, since tab-width is all-buffer-local.

4.5. How can I add directories to the load-path?

Here are two ways to do that, one that puts your directories at the front of the load-path, the other at the end:

;;; Add things at the beginning of the load-path
(setq load-path (cons "bar" load-path))
(setq load-path (cons "foo" load-path))

;;; Add things at the end
(setq load-path (append load-path '("foo" "bar")))

4.6. How to check if a lisp function is defined or not?

Use the following elisp:

(fboundp 'foo)

It's always a mistake, under all circumstances, to test `emacs-version' or any similar variables, in case they are not bound, unless you do the above.

Instead, use feature-tests, such as featurep or boundp or fboundp, or even simple behavioural tests, eg (defvar foo-old-losing-code? (condition-case nil (progn (losing-code t) nil) (wrong-number-of-arguments t)))

There is an incredible amount of broken code out there which could work much better more often in more places if it did the above instead of trying to divine its environment from the value of one variable.

4.7. Can I force the output of (face-list) to a buffer other than the minibuffer since it is too wide to fit?

Evaluate the expression in the "*scratch*" buffer with point on the rightmost paren and typing C-j.


Go to the first, previous, next, last section, table of contents.