Emacs:填写除指定区域以外的所有文本

时间:2016-02-05 22:33:14

标签: text emacs elisp fill

在gnu emacs中使用elisp,我希望能够填充缓冲区中的所有文本,除了用特殊标识符指示的文本。标识符可以是任何东西,但是为了这个问题,让我们假设它是在[nofill]和[/ nofill]标记之间的任何文本。

例如,假设我的缓冲区如下所示:

Now is the time
for all good
   men to come to the aid
    of their party. Now is
the time for all good
 men to come to the aid
of their party.

[nofill]
The quick
brown fox
jumped over the
lazy sleeping dog
[/nofill]

When in the course of 
    human events, it becomes 
  it becomes necessary for one
     people to dissolve the
  political bands

[nofill]
    baa-baa
      black sheep,
   have you
    any wool
[/nofill]

在我正在寻找的那种填充之后,我希望缓冲区显示如下:

Now is the time for all good men to come to the aid of their
party. Now is the time for all good me to come to the aid of
their party

[nofill]
The quick
brown fox
jumped over the
lazy sleeping dog
[/nofill]

When in the course of human events, it becomes it becomes
necessary for one people to dissolve the political bands

[nofill]
    baa-baa
      black sheep,
   have you
    any wool
[/nofill]

我知道elisp,我可以写一些这样做的东西。然而,在我尝试“重新发明轮子”之前,我想知道是否有人知道任何可能已经提供此功能的现有elisp模块。

提前谢谢。

2 个答案:

答案 0 :(得分:1)

您可以证明[/nofill][nofill]之间的所有内容(或者可能是缓冲区的开头/结尾)。

(defun fill-special () "fill special"
  (interactive)
  (goto-char (point-min))
  (while (< (point) (point-max))
    (let ((start (point)))
      (if (search-forward "[nofill]" nil 1)
          (forward-line -1))
      (fill-region start (point) 'left)
      (if (search-forward "[/nofill]" nil 1)
          (forward-line 1)))))

答案 1 :(得分:1)

与其他答案相比,这似乎过于复杂,但基本上,我标记当前点,向前搜索标签(可以参数化),并填充该区域。然后,我递归调用fill-region-ignore-tags-helper,使用起始点之后的第一个字符作为区域的开头,然后将下一个[nofill]标记作为区域的结尾。这一直持续到填满整个缓冲区。它似乎适用于一些随机的微不足道的案例,尽管可能存在一些未涵盖的边缘案例。

(defun fill-region-ignore-tags ()
  (interactive)
  (save-excursion
    (fill-region-ignore-tags-helper (point-min) (search-forward "[nofill]"))))

(defun fill-region-ignore-tags-helper (begin end)
  (let ((cur-point begin)
        (next-point end))
    (if (eq next-point nil)
        nil
      (progn
        (fill-region cur-point next-point)
        (fill-region-ignore-tags-helper (progn
                                          (search-forward "[/nofill]")
                                          (re-search-forward "\\S-")
                                          (point))
                                 (progn
                                   (search-forward "[nofill]")
                                   (previous-line)
                                   (point)))))))