Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a function (e.g. server-running-p) is available under Emacs?

Tags:

emacs

elisp

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.

like image 328
Christian Hudon Avatar asked Apr 03 '12 18:04

Christian Hudon


3 Answers

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))
like image 122
ryuslash Avatar answered Nov 18 '22 04:11

ryuslash


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))
like image 30
Justin Zamora Avatar answered Nov 18 '22 03:11

Justin Zamora


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.

like image 3
Keith Wygant Avatar answered Nov 18 '22 05:11

Keith Wygant