20

I'm trying to check if server-running-p is available in my .emacs file before calling it. I already have the following:

(if (not (server-running-p))
    (server-start))

But on some computers where I use Emacs, calling (server-running-p) gives an error because said call is not available. So I want to check if server-running-p is available before calling it. I thought boundp would do the try, but calling (boundp 'server-running-p) return nil even though the (server-running-p) call succeeds. What's the right way to check that calling server-running-p won't fail... or at least to suppress the error if said call fails. (And what kind of weird object is server-running-p anyway that boundp returns nil, but calling it succeeds?)

This is on Emacs 23.2.1, if it makes any difference.


Actually found the answer. You have to use fboundp for this instead of boundp, for some reason.

Christian Hudon
  • 1,881
  • 1
  • 21
  • 42
  • 3
    The reason is that you can have a function and a variable with the same name (e.g. `font-lock-mode`). Hence you need different functions to ask "Is this a variable?" and "Is this a function?" – cjm Apr 03 '12 at 19:30
  • Thanks. I had completely forgotten about that particularity of some Lisp languages. Makes more sense now. – Christian Hudon Apr 04 '12 at 20:41

3 Answers3

26

boundp checks to see if a variable is bound. Since server-running-p is a function you'll want to use fboundp. Like so:

(if (and (fboundp 'server-running-p) 
         (not (server-running-p)))
   (server-start))
ryuslash
  • 1,232
  • 11
  • 11
17

A simpler way is to use "require" to make sure the server code is loaded. Here's what I use:

(require 'server)
(unless (server-running-p)
    (server-start))
Justin Zamora
  • 191
  • 1
  • 4
  • 4
    I am also trying to keep my .emacs compatible with both Emacs and XEmacs. In that case, using `fbounpd` seems safer to me, no? – Christian Hudon Jul 04 '12 at 21:32
  • @ChristianHudon, this is not just about compatibility. This version and the other one do two different things. This one loads the `server` package if it wasn't already loaded. The other version only starts the server if the `server` package was already loaded elsewhere. So I think this version is more self-contained. I don't know about XEmacs, but a more compatible alternative might be to use `(load "server")` instead (instead of `(require 'server)`, that is). – harpo Sep 01 '16 at 16:17
3

ryuslash’s suggestion was really helpful, but I modified it for my .emacs:

(unless (and (fboundp 'server-running-p)
             (server-running-p))
  (server-start))

This gets the server running even if server.el hasn't been loaded—server-running-p is only defined when server.el is loaded, and server-start is autoloaded.