1

我正在定义一种适用于以下性质段落的主要模式:

: Identifier
1. some text
2. ...
3. some more text

: New Identifier

: Another Identifier
some text

我想编写一个defun调用get-paragraphs,它将返回一个如下所示的列表:

(   ("Identifier", ("1. some text", "2. ...", "3. some more text")), 
    ("New Identifier", ()),
    ("Another Identifier", ("some text"))
)

如何在 Emacs Lisp 中像这样剪切文本:

是否有一个函数可以遍历它们(然后根据我的喜好将它们切碎)?我应该使用正则表达式吗?有没有更简单的方法?

4

2 回答 2

2

您应该遍历缓冲区并收集您的文本(未经测试):

(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))))
于 2013-08-05T18:27:53.913 回答
1

在这里,使用这个 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)))
于 2013-08-05T20:07:38.813 回答