Emacs:填充除指定区域之外的所有文本
Emacs: fill all text except indicated regions
在 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,我可以写一些东西来做这个。然而,在我尝试 "reinvent the wheel" 之前,我想知道是否有人知道任何现有的 elisp 模块可能已经提供了这个功能。
提前致谢。
您可以证明 [/nofill]
和 [nofill]
之间的所有内容(或者可能 beginning/end 缓冲区)。
(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)))))
与其他答案相比,这似乎过于复杂,但基本上,我标记当前点,向前搜索标签(可以参数化),并填充该区域。然后,我递归调用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)))))))
在 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,我可以写一些东西来做这个。然而,在我尝试 "reinvent the wheel" 之前,我想知道是否有人知道任何现有的 elisp 模块可能已经提供了这个功能。
提前致谢。
您可以证明 [/nofill]
和 [nofill]
之间的所有内容(或者可能 beginning/end 缓冲区)。
(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)))))
与其他答案相比,这似乎过于复杂,但基本上,我标记当前点,向前搜索标签(可以参数化),并填充该区域。然后,我递归调用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)))))))