我正在定义一个主要模式,该模式适用于以下性质的段落:
: Identifier
1. some text
2. ...
3. some more text
: New Identifier
: Another Identifier
some text
我想写一个名为get-paragraphs
的defun
,它将返回一个列表,看起来像:
( ("Identifier", ("1. some text", "2. ...", "3. some more text")),
("New Identifier", ()),
("Another Identifier", ("some text"))
)
如何在Emacs Lisp:中剪切这样的文本
有没有一个函数可以迭代它们(然后根据我的喜好将它们切碎)?我应该使用正则表达式吗?有没有更简单的方法?
您应该遍历缓冲区并收集文本(未经测试):
(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))))
这里,取这个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)))