【问题标题】:Emacs: fill all text except indicated regionsEmacs:填充除指定区域外的所有文本
【发布时间】:2016-02-06 13:50:50
【问题描述】:

在 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 模块可能已经提供了此功能。

提前谢谢你。

【问题讨论】:

    标签: text emacs elisp fill


    【解决方案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)))))
    

    【讨论】:

    • 嗯,这比我想象的要容易得多。谢谢你的回答!
    【解决方案2】:

    与其他答案相比,这似乎过于复杂,但基本上,我标记当前点,向前搜索标签(可以参数化),然后填充该区域。然后,我递归调用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)))))))
    

    【讨论】:

    • 是的,也适用于我。可以使用 "(when next-point ..." 作为比 "(if (eq next-point nil) nil (progn ..." 更短的形式
    猜你喜欢
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多