8

Emacs 24.4 made an incompatible change to interpreter-mode-alist, treating its members now as regular expressions and no longer as simple strings (http://git.savannah.gnu.org/cgit/emacs.git/commit/?id=1af4c2203ce7954c089133234ba80e6272ce9458). I want to replace:

(add-to-list 'interpreter-mode-alist
             ;; Match php, php-3, php5, php7, php5.5, php-7.0.1, etc.
             (cons "php\\(?:-?[3457]\\(?:\\.[0-9]+\\)*\\)?" 'php-mode))

(which works fine with Emacs 24.4 and newer) with compatibility code that, depending on the version of Emacs, adds either a regular expression or strings.

What is the preferred way to test if the Emacs version in use is 24.4 or newer? (if (version< emacs-version "24.4") …)?

Drew
  • 75,699
  • 9
  • 109
  • 225
Tim Landscheidt
  • 467
  • 3
  • 8
  • Using `emacs-version` with the `version*` functions is sane, yes. – phils Sep 07 '17 at 02:33
  • You may also be interested in playing with `version-list-...` and `version-to-list`; e.g., `(unless (version-list-<= (version-to-list emacs-version) '(25 2 50 1)) ...)` – lawlist Sep 07 '17 at 05:51
  • @phils: Could you please post your answer as an answer? You can use my code at https://github.com/scfc/php-mode/commit/a0b927b4988903f18909ec8eddc157b6de9cbe53 to mirror the code in the question. – Tim Landscheidt Sep 08 '17 at 04:17
  • @lawlist: Thanks, that's interesting. For my use (simple) case, I'll stick with `version<`. – Tim Landscheidt Sep 08 '17 at 04:18
  • @TimLandscheidt, sure; done. – phils Sep 09 '17 at 04:02

1 Answers1

11

Using emacs-version with the various version* functions is sane, yes.

  • version=
  • version<
  • version<=

(These are convenience wrappers which use the version-list functionality behind the scenes.)

By way of example, the following is the code Tim ended up using:

(if (version< emacs-version "24.4")
    (dolist (i '("php" "php3" "php5" "php7" "php-5" "php-5.5" "php7.0.1"))
      (add-to-list 'interpreter-mode-alist (cons i 'php-mode)))
  (add-to-list 'interpreter-mode-alist
               ;; Match php, php-3, php5, php7, php5.5, php-7.0.1, etc.
               (cons "php\\(?:-?[3457]\\(?:\\.[0-9]+\\)*\\)?" 'php-mode)))
phils
  • 48,657
  • 3
  • 76
  • 115
  • 1
    n.b. The variables `emacs-major-version` and `emacs-minor-version` were also mentioned in another answer (which has unfortunately been deleted), and these might be useful alternatives in some situations. – phils Sep 09 '17 at 04:07