Emacs 如何正确解析elisp中的缓冲区?

Emacs 如何正确解析elisp中的缓冲区?,emacs,elisp,Emacs,Elisp,解析缓冲区以存储其内容并重用它的正确方法是什么 假设我得到了这个缓冲区: always|five|words|by|line not|always|the|same|words sometimes|no|lines|at|all but|only|five|no|more/less 从行中找到的符号构造列表的最佳方法是什么(如果没有找到,则会很好地出错) 缓冲区在那里,我可以访问它,像这样获取它的内容 (message "Buffer content : %s" (buffer-substri

解析缓冲区以存储其内容并重用它的正确方法是什么

假设我得到了这个缓冲区:

always|five|words|by|line
not|always|the|same|words
sometimes|no|lines|at|all
but|only|five|no|more/less
从行中找到的符号构造列表的最佳方法是什么(如果没有找到,则会很好地出错)

缓冲区在那里,我可以访问它,像这样获取它的内容

(message "Buffer content : %s" (buffer-substring (point-min) (point-max)))
在我干净利落地杀死它之后,但不知何故,我在构建对象(列表“单词”的列表“行”)时失败了,这将允许我这样做:

(list-length lines)
    ==> 4

(car (nthcdr 3 lines))
    ==> sometimes

一个志同道合的灵魂能指引我走向光明吗?感谢您的耐心等待,Lisp elders。

这里有一个简单的基于regexp的解析器,它可能是实现您想要的东西的开始:

(let (lines)
  (beginning-of-line)  
  (while (not (eobp))
    (push
     (if (looking-at "\\([^|\n]+\\)|\\([^|\n]+\\)|\\([^|\n]+\\)|\\([^|\n]+\\)|\\([^|\n]+\\)")
         (list (match-string-no-properties 1)
               (match-string-no-properties 2)
               (match-string-no-properties 3)
               (match-string-no-properties 4)
               (match-string-no-properties 5))    
       'no-match)
     lines)
    (forward-line 1))

  (setq lines (nreverse lines))

  (print lines))

您还可以使用内置的
split string
函数,类似于Perl和其他语言中的
split

(defun buffer-to-list-of-lists (buf)
  (with-current-buffer buf
    (save-excursion
      (goto-char (point-min))
      (let ((lines '()))
        (while (not (eobp))
          (push (split-string
                 (buffer-substring (point) (point-at-eol)) "|")
                lines)
          (beginning-of-line 2))
        (nreverse lines)))))
然后,使用名为
temp
的缓冲区中的示例文本,
(缓冲区到列表“temp”)
返回值

(("always" "five" "words" "by" "line") 
 ("not" "always" "the" "same" "words")
 ("sometimes" "no" "lines" "at" "all")
 ("but" "only" "five" "no" "more/less"))
这将适用于包含任意数量的
|
-分隔字的行,这对您的应用程序来说可能更好,也可能不更好。如果不希望列表列表中的字符串包含字体信息和原始缓冲区中的其他属性,请将
buffer substring
更改为
buffer substring no properties


一旦您想让它正常工作,您还需要将示例用法
(列表长度(行))
更改为
(列表长度行)
。在其当前形式中,您需要一个常量单元素列表的长度,该列表仅包含符号

让我们假设变量
文本
包含缓冲区的内容作为字符串,根据。然后使用列表API和API函数:

(--map (s-split "|" it) (s-lines text))

-map
的一个函数,它公开了一个临时变量
it
,因此您不必传递匿名函数。是一个围绕
拆分字符串
的简单包装器,将字符串除以换行符。

我想我找到了一种计算行数的方法。但是,至于如何将这些单词以易于检索的形式存储起来,我运气不好。我不得不戴上太阳镜阅读你的“sur-mesure”答案。它充满了有用的信息,非常感谢你,老兄,你是一位绅士和学者:)@PhilippeCM。。。我不得不翻开一本法语词典来找出“sur mesure”是什么意思;-)很高兴这有帮助!