如何在Emacs Lisp中获取所有段落?

时间:2013-08-05 17:19:16

标签: emacs elisp major-mode

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

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

: New Identifier

: Another Identifier
some text

我想写一个名为defun的{​​{1}},它会返回一个如下所示的列表:

get-paragraphs

如何在Emacs Lisp中删除这样的文本:

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

2 个答案:

答案 0 :(得分: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))))

答案 1 :(得分: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)))