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

霸气de小男生 提交于 2019-11-29 16:57:02

问题


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.


回答1:


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



回答2:


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



回答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.



来源:https://stackoverflow.com/questions/9999320/how-to-check-if-a-function-e-g-server-running-p-is-available-under-emacs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!