help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Advice on writing predicates


From: Anselm Helbig
Subject: Re: Advice on writing predicates
Date: Fri, 26 Jun 2009 01:23:56 +0200

At Thu, 25 Jun 2009 15:27:19 -0700,
Sarir Khamsi <sarir.khamsi@raytheon.com> wrote:
> 
> I wrote a simple predicate that returns t if the Emacs major version
> number is greater than 22 and would like some advice/comments:
> 
> The following
> 
> (defun sk-emacs-version-greater-than23-p ()
>   "Return non-nil if current Emacs version is greater than 22."
>   (interactive)
>   (setq version-string (replace-regexp-in-string 
> ".*?\\([0-9]+\\)\.\\([0-9]+\\)\.\\([0-9]+\\)[()-.a-zA-Z0-9 \n]+" "\\1 \\2 
> \\3" (emacs-version)))
>   (setq version-num (mapcar 'string-to-number (split-string version-string)))
>   (if (> 22 (car version-num))
>       t
>     nil))

I hope you're not too disappointed, but there's a constant named
emacs-major-version: 

  (> emacs-major-version 22)

This is also extracted from the string in emacs-version, this is how
the emacs devs did it:

  (defconst emacs-major-version
    (progn (string-match "^[0-9]+" emacs-version)
     (string-to-number (match-string 0 emacs-version)))
    "Major version number of this version of Emacs.
  This variable first existed in version 19.23.")

If you want to parse the version number yourself, this is how I would
do it: 

  (mapcar 'string-to-number (split-string emacs-version "\\."))

Note that the emacs-version variable just holds the number. This makes
things a lot easier. 

Generally you shouldnt rely on the version number to test if a feature
is available - just test directly for the feature you need. 

Regards, 

Anselm


-- 
Anselm Helbig 
mailto:anselm.helbig+news2009@googlemail.com


reply via email to

[Prev in Thread] Current Thread [Next in Thread]