[英]How do I get all paragraphs in Emacs Lisp?
I am defining a major mode that works on paragraphs of the following nature: 我正在定义一个主要模式,适用于以下性质的段落:
: Identifier
1. some text
2. ...
3. some more text
: New Identifier
: Another Identifier
some text
I want to write a defun
called get-paragraphs
that will return a list that looks like: 我想写一个名为get-paragraphs
的defun
,它将返回一个如下所示的列表:
( ("Identifier", ("1. some text", "2. ...", "3. some more text")),
("New Identifier", ()),
("Another Identifier", ("some text"))
)
How do I go about cutting up the text like this in Emacs Lisp: 我如何在Emacs Lisp中切割这样的文本:
Is there a function to iterate through them (and subsequently chop them up to my liking)? 是否有一个函数来迭代它们(然后根据自己的喜好将它们切掉)? Should I use regular expressions? 我应该使用正则表达式吗? Is there an easier way? 有更容易的方法吗?
You should iterate over the buffer and collect your text (untested): 您应该遍历缓冲区并收集文本(未经测试):
(defun get-paragraphs ()
(save-excursion
(goto-char (point-min))
(let ((ret '()))
(while (search-forward-regexp "^: " nil t)
(let ((header (buffer-substring-no-properties (point) (line-end-position)))
(body '()))
(forward-line)
(while (not (looking-at "^$"))
(push (buffer-substring-no-properties (point) (line-end-position)) body)
(forward-line))
(push (cons header (list (reverse body))) ret)))
(nreverse ret))))
Here, take this Lisp code: 在这里,拿这个Lisp代码:
(defun chopchop ()
(mapcar
(lambda (x)
(destructuring-bind (head &rest tail)
(split-string x "\n" t)
(list head tail)))
(split-string (buffer-substring-no-properties
(point-min)
(point-max)) "\n?: *" t)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.