请教一下elisp里面,带冒号的符号的作用。


#1

看到别人的package代码里面都有用到冒号,这个带冒号的:symbol跟常见的(set 'aaa 5)有什么区别。

(defgroup everything nil
  "Bridge to the Windows desktop search-engine everything. See http://www.voidtools.com."
  :group 'external
  :prefix "everything-")


(setq h-data '((:num 1 :key "apple")
               (:num 9 :key "berry")
               (:num 2 :key "cactus")
               (:num 5 :key "dog")
               (:num 4 :key "frog")))

#2

argument list


#3

这个argument 怎么读取的。


#4
ELISP> (keywordp :a)
t
ELISP> (symbolp :a)
t
ELISP> (eq ':a :a)
t
ELISP> (set :a 1)
*** Eval error ***  Attempt to set a constant symbol: :a 

package自己解析的


#5

搜索了一下,看了一下别人的代码,大概有点理解了,可以通过plist-get等函数读取。

(defun print-name (&rest args)
  (let ((first (plist-get args :first))
        (last (or (plist-get args :last) "?")))
    (princ last)
    (when first
      (princ ", ")
      (princ first))))


ELISP> (print-name :last "aaaa")
aaaa
ELISP> (print-name :first "aaaa")
?, aaaa
ELISP> (plist-get '(:first 1 :last 2) :last)
2 (#o2, #x2, ?\C-b)

#6

刚好看到了这个:

(info "(elisp) Symbol Type")

A symbol whose name starts with a colon (‘:’) is called a “keyword symbol”. These symbols automatically act as constants, and are normally used only by comparing an unknown symbol with a few specific alternatives. *Note Constant Variables::.

(info "(elisp) Constant Variables")

In Emacs Lisp, certain symbols normally evaluate to themselves. These include ‘nil’ and ‘t’, as well as any symbol whose name starts with ‘:’ (these are called “keywords”). These symbols cannot be rebound, nor can their values be changed. Any attempt to set or bind ‘nil’ or ‘t’ signals a ‘setting-constant’ error. The same is true for a keyword (a symbol whose name starts with ‘:’), if it is interned in the standard obarray, except that setting such a symbol to itself is not an error.

就是说带冒号的 symbol (符号) 是一种特殊的 symbol。在 Emacs 中 会 evaluate 他们自己 (就是不会被改变么?)。


#7

evaluate to themselves 就是对其求值会得到他们自己,也有叫self quoted的(因为quote的作用就是让eval一个(quote sym)的form可以返回sym自己。


#8

动手试试:

(set ':aaa 5)
error-> Attempt to set a constant symbol: :aaa

所以说 :aaaaaa 的区别在于前者不能赋值,类似于 nilt 也不能赋值。原因前面帖子有解释。


#9

学会了查文档 不错欧