Question

I want to write a .emacs that uses as much of the mainline emacs functionality as possible, falling back gracefully when run under previous versions. I've found through trial and error some functions that didn't exist, for example, in emacs 22 but now do in emacs 23 on the rare occasion that I've ended up running my dotfiles under emacs 22. However, I'd like to take a more proactive approach to this, and have subsets of my dotfiles that only take effect when version >= <some-threshold> (for example). The function I'm focusing on right now is scroll-bar-mode but I'd like a general solution.

I have not seen a consistent source for this info; I've checked the gnu.org online docs, the function code itself, and so far nothing. How can I determine this, without keeping every version of emacs I want to support kicking around?

Was it helpful?

Solution

I cannot answer your question directly, but one technique I use is to check the functionp function that tells me if a function exists.

e.g.

(if (load "completion" t)
  (progn
    (initialize-completions)
    (if (functionp 'dynamic-completion-mode)
      (dynamic-completion-mode) ; if exists
      (completion-mode)         ; otherwise use old version
    )
  ) ; progn
) ; if

update: adding version specific macros

In addition to using functionp I also have some version specific macros:

(defmacro GNU_EMACS_21 (&rest stuff)
   (list 'if (string-match "GNU Emacs 21" (emacs-version)) (cons 'progn stuff)))
(defmacro GNU_EMACS_20 (&rest stuff)
  (list 'if (string-match "GNU Emacs 20" (emacs-version)) (cons 'progn stuff)))
(defmacro GNU_EMACS_19 (&rest stuff)
  (list 'if (string-match "GNU Emacs 19" (emacs-version)) (cons 'progn stuff))) 
(defmacro WINSYS_X (&rest stuff)
  (list 'if (eq window-system 'x) (cons 'progn stuff)))
(defmacro WINSYS_W32 (&rest stuff)
  (list 'if (eq window-system 'w32) (cons 'progn stuff)))
(defmacro WINSYS_NIL (&rest stuff)
  (list 'if (eq window-system nil) (cons 'progn stuff)))
(defmacro SYSTYPE_LINUX (&rest stuff)
  (list 'if (string-match "linux" (symbol-name system-type)) (cons 'progn stuff)))

I can then use these:

(GNU_EMACS_21
  (if (load "cua" t)
    (CUA-mode t)
  )
)
(WINSYS_NIL ; when running in text mode
  (push (cons 'foreground-color "white") default-frame-alist)
  (push (cons 'background-color "black") default-frame-alist)
  (push (cons 'cursor-color "cyan") default-frame-alist)
  (push (cons 'minibuffer t) default-frame-alist)
)

I'm guessing you already know this, however; and questions like "when did CUA mode get included with Emacs" are difficult to answer..

OTHER TIPS

The "NEWS" files (accessible via C-h N) may give hints as to when functions were introduced.

It's usually better practice to test the existence of the function or variable you want to use, rather than test the Emacs version. Use fboundp and boundp, for example. Occasionally it makes sense to check featurep, but there again it is usually better to use fboundp or boundp.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top