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

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

Re: byte compile condioning


From: Lawrence Mitchell
Subject: Re: byte compile condioning
Date: Sat, 22 May 2004 23:23:13 +0100
User-agent: Gnus/5.110003 (No Gnus v0.3) Emacs/21.3.50

Miguel Frasson wrote:

> Hi.

> Suppose that I want to make a program to byte-compile in both GNU Emacs and
> Xemacs.

> Is it possible to define a function in two different ways, one for each
> emacs? (before somebody asks, I have a reason to do that, because I am
> dealling with toolbars in both emacs.)

> Suppose I want to define a function foo like

> ;; for xemacs
> (defun foo (a b) (function-only-defined-in-xemacs a b))

> ;; for gnu emacs
> (defun foo (a b) (function-only-defined-in-gnu-emacs a b))

> I thought about put functions in different files and `require' then with
> `eval-when-compile'using a regexp on `emacs-version', but I don't know it
> the result is good. Also, I don't like to split a program in files if not
> strictly necessary (program is not so big to do so.)

There are few ways of doing this.  Basically, they all test the
presence of 'xemacs in features.

One is to wrap the function calls in an if statement:

(defun foo (a b)
  (if (featurep 'xemacs)
      (xemacs-function ...)
    (gnu-emacs-function ...)))

If you're worried about the runtime conditional, you could do
something like:

(defun foo (a b)
  (funcall (eval-when-compile
             (if (featurep 'xemacs)
                 'xemacs-function
               'gnu-emacs-function))
           a b ...))

This has a performance penalty when run interpreted, but removes
the conditional when byte-compiled.


Alternately, you can, at the cost of a tiny bit of indirection,
introduce a variable to hold the function symbol:

(defvar my-function (if (featurep 'xemacs) ...))

(defun foo (a b)
  (funcall my-function a b))

A third option, often used if the two functions do similar
things, but have, for example, a different API, is to wrap things
up in a compatibility layer:

(if (featurep 'xemacs)
    (defun my-toolbar-thingy (...)
      (do-stuff-for-xemacs-toolbars)
      ...)
  (defun my-toolbar-thingy (...)
    (do-stuff-for-emacs-toolbars)
    ...))

then

(defun foo (a b)
  (my-toolbar-thingy a b))


-- 
Lawrence Mitchell <address@hidden>


reply via email to

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