无法在Emacs中添加功能(Cannot add function in Emacs)

我最近开始使用Emacs,我正在尝试添加一个函数,将输入的句子拆分为单词列表(拆分一个空格):

(defun split-by-one-space (string) ; from: http://cl-cookbook.sourceforge.net/strings.html "Returns a list of substrings of string divided by ONE space each. Note: Two consecutive spaces will be seen as if there were an empty string between them." (loop for i = 0 then (1+ j) as j = (position #\Space string :start i) collect (subseq string i j) while j)) (defun myfn (ss) "documentation of fn: to be added" (interactive "s\Enter some words: ") (message "Read: %s" (split-by-one-space ss)))

编辑:

使用命令“ emacs --debug-init ”启动Emacs会显示以下错误:

(invalid-read-syntax "#")

Edit:

Starting Emacs with command "emacs --debug-init" shows that following error:

(invalid-read-syntax "#")

Apparently \#Space not recognized in Emacs Lisp. I replaced it with '?\ ' and this error went away. The function myfn is now available for executing. However, on executing it, following error is shown:

Symbol's function definition is void: loop

Apparently the loop part needs to be rewritten in Emacs Lisp.

最满意答案

#\Space是无效的Elisp并且会导致读取错误,因此大多数Emacs此时将停止处理您的文件。 您想要编写?\s或?\ ,这是Elisp中用于字符文字的语法。

之后,您将看到Emacs不知道该loop 。 你想使用cl-loop代替,这是由cl-lib包提供的,所以你不仅需要将loop重命名为cl-loop而且还需要添加

(require 'cl-lib)

早。 类似地,子subseq和positions不为Elisp所知,您需要使用cl-subseq和cl-position (也由cl-lib )。

#\Space is not valid Elisp and will cause a read-error, so most Emacs will stop processing your file at this point. You want to write ?\s or ?\ instead, which is the syntax used in Elisp for character literals.

After that, you'll see that loop is not known to Emacs. You want to use cl-loop instead, which is provided by the cl-lib package, so not only you need to rename loop to cl-loop but you additionally need to add

(require 'cl-lib)

earlier. Similarly subseq and positions are not known to Elisp and you'll need to use cl-subseq and cl-position instead (also provided by cl-lib).

更多推荐