unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* byte compile condioning
@ 2004-05-22 22:00 Miguel Frasson
  2004-05-22 22:23 ` Lawrence Mitchell
  0 siblings, 1 reply; 2+ messages in thread
From: Miguel Frasson @ 2004-05-22 22:00 UTC (permalink / raw)



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.)

Thanks for any advise.

Miguel.

 

-- 
Miguel Vinicius Santini Frasson
http://www.math.leidenuniv.nl/~frasson

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: byte compile condioning
  2004-05-22 22:00 byte compile condioning Miguel Frasson
@ 2004-05-22 22:23 ` Lawrence Mitchell
  0 siblings, 0 replies; 2+ messages in thread
From: Lawrence Mitchell @ 2004-05-22 22:23 UTC (permalink / raw)


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 <wence@gmx.li>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-05-22 22:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-05-22 22:00 byte compile condioning Miguel Frasson
2004-05-22 22:23 ` Lawrence Mitchell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).