Elips语言初学时的问题

我是在这个网站大概了解一下elips的:https://learnxinyminutes.com/docs/elisp/ 有一个问题解决不了

(ps:这些代码都是那个网页里的。我并没有买一本elips的书系统地学它,不知道有没有必要?)

先是有这两段代码:

(setq list-of-names '("Sarah" "Chloe" "Mathilde"))(push "Stephanie" list-of-names),构造了一个由四个人的名字组成的列表,然后定义并运行了函数greeting:

(defun greeting ()
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    (mapcar 'hello list-of-names)
    (other-window 1))

(greeting)运行结果是在名叫test的buffer中打印了以下内容并使光标返回名为scratch的buffer:

Hello Stephanie
Hello Sarah
Hello Chloe
Hello Bonjour

又定义并运行了另一个函数,用于把test这个buffer中的Hello全换为Bonjour,全换完后再把光标移回scratch的buffer:

(defun replace-hello-by-bonjour ()
    (switch-to-buffer-other-window "*test*")
    (goto-char (point-min))
    (while (search-forward "Hello")
      (replace-match "Bonjour"))
    (other-window 1))

我的问题在于在我运行这些代码时,最后光标并没有回到scratch中,而是停在了test中的最后一个Bonjour的后面,而且在minibuffer中还显示了search failed: “Hello”,如图:

请问如何解决这个问题?产生这个问题的原因是什么?怎么解决和避免?

注1:我的emacs版本是24.5,该网站用的是24.3

注2:我是大一学生,只学过C语言,纯属新手,请多指教,请多见谅。

我要是大一的时候就开始玩lisp和emacs那现在岂不是比现在厉害多了。。
我也跑过这个例子,我记得search failed是正常的,它找Hello并且全部替换直到全部被替换之后就找不到了,最后一次search就fail了。
我看了下other-window的文档,参数为1就表示跳到下一个window,你看看最后执行完是不是scratchtest各一个window?
另外按照例子里把test写成*test*吧,看*scratch**Messages*这些大概可以猜到,命名的约定是:准备存文件的buffer就正常写,不准备存在文件里的,就前后加星号。(至于首字母为什么一会儿大写一会儿小写我就不知道了)

谢谢你的热心回答。但是情况是两个window都还在,却没有切换。是不是while一但fail就不往下执行了?要想知道原因并解决问题,我有没有必要买本elisp的书系统地学一下lisp语言呢? ps:我学习lisp是因为视频里提到要想搞emacs至少要把这个网页里的lisp掌握,如果能不用拿大把的时间学lisp的话,我还是不太想去看lisp的书的

你需要多阅读文档,使用 C-h f search-forward,我们可以看到这个函数接受的第 3 个参数可以让 search failed 的时候不出错。

search-forward is an interactive built-in function in ‘C source code’.

It is bound to <find>.

(search-forward STRING &optional BOUND NOERROR COUNT)

Search forward from point for STRING.
Set point to the end of the occurrence found, and return point.
An optional second argument bounds the search; it is a buffer position.
  The match found must not end after that position.  A value of nil
  means search to the end of the accessible portion of the buffer.
Optional third argument, if t, means if fail just return nil (no error).
  If not nil and not t, move to limit of search and return nil.
Optional fourth argument COUNT, if a positive number, means to search
  for COUNT successive occurrences.  If COUNT is negative, search
  backward, instead of forward, for -COUNT occurrences.  A value of
  nil means the same as 1.
With COUNT positive, the match found is the COUNTth one (or first,
  if COUNT is 1 or nil) in the buffer located entirely after the
  origin of the search; correspondingly with COUNT negative.

Search case-sensitivity is determined by the value of the variable
‘case-fold-search’, which see.

See also the functions ‘match-beginning’, ‘match-end’ and ‘replace-match’.

[back]

啊,真是不好意思!:sweat_smile:碰到问题我就没接着往下读,那个网站在那段代码的下文有解释,真是麻烦你和大家了,谢谢你,以后提问时我会更慎重的

谢谢您的帮助

没关系,下次提问记得把格式弄好一点,我刚刚帮你整理了,你可以点开编辑看看我是怎么做的。

对啊那就把learnxinyminutes的看完就好了。

前者是an introduction,后者是menu,前者在序言里说它是为非程序员准备的。再多我也不知道了,两个我都看了个开头。话说gnu这些十几年前写的网页的链接跳进跳出的组织方式真是让人发疯。。

我也是新手,感觉不想去看的话暂时肯定不用,反正抄别人配置也是直接拷代码的,改改按键、改改函数名不需要对lisp有多少理解,等能自己写个三五十行的函数了再考虑要不要读手册这个问题吧。

我提过比你更菜的问题。这毕竟是个闲聊也可以的论坛,对内容质量没有太苛刻的要求,有问题大可以来提。

我在stackoverflow经常被换行狂魔整理文字,我心里还不服气:代码quote起来就好了,为什么非要两句话一段,段与段空一行?不是都一样看么:joy:

段落之间空一行显得更加可读,另外代码与代码也是有关联关系的,并不是简单的 quote 了事。

还有,quote 并没有正确的语法高亮,需要在 ```后面加上 language name 才有高亮,比如本论坛在贴 lisp 代码的时候,最好写成:

```lisp your code goes here. ```

1 个赞

原来是这个意思!我以后会按格式发的,谢谢你的矫正与帮助!

高中生在此!哈哈哈哈

显然你没有认真看,那个简短教程在你帖的代码附近已经提了这个问题并在下一例子中给了解决方案的。不过非常感谢你发这个链接,让我也简短学习了一下elisp。