如何实现让段落中每句话占一行的功能?
How to make a function that makes each sentence in a paragraph occupy one line?
我使用 emacs 进行创意写作。为了更好地分析我的句子结构,我希望我的段落显示为每行一个句子。所以我需要一个函数,它可以接受一个普通的自动填充段落并执行以下操作:1) 将所有句子拉伸成一行,以及 2) 每行只放一个句子。
想象一下我写了以下段落(Suzanne Vega 的歌词)
My name is Luka. I live on the second floor. I live upstairs from you. Yes I think you've seen me before. If you hear something late at night. Some kind of trouble. Some kind of fight.
使用我希望的功能,段落将显示为:
My name is Luka.
I live on the second floor.
I live upstairs from you.
Yes I think you've seen me before.
If you hear something late at night.
Some kind of trouble.
Some kind of fight.
由于我想在这样显示句子的时候写一些东西,所以该功能除了拉长句子外还应该关闭自动填充模式。
理想情况下,我想要一个功能,可以在自动填充模式(所有句子都换行)和这种自动填充关闭且所有句子都展开的新模式之间切换显示。
提前感谢各种建议或帮助实现这样的功能!
@Drew:这是一段我无法与您的代码分开的文本:
有两种方法可以启用它:第一种是使用 M-x visual-line-mode(对于那些有真实菜单的,显然是 Options->Line Wrapping in this Buffer->Word Wrap),这会给你一个模式行中的次要模式“换行”。如 C-h f visual-line-mode 中所述,此命令的效果之一是巧妙地更改处理“行”的命令的效果:C-a、C-e 不再转到行尾(如 \n ),但转到行尾(如显示行)。 M-a、M-e 仍然正常工作。此外,垂直拆分 windows 保证不会被截断,并在更改宽度时适当调整大小。效果很好,特别是如果你有自由格式的文本,你在版本控制中保存(比如 Latex 中的论文),硬包装不能很好地工作。它还使垂直拆分更有用,尤其是对于巨大的 windows。根据我的经验,它会稍微减慢重绘速度,但这是值得的。
我想这就是您要的东西。
(defun split-para-at-sentence-ends ()
"Split current paragraph into lines with one sentence each.
Then turn off `auto-fill-mode'."
(interactive)
(let ((mode major-mode))
(unwind-protect
(progn (text-mode)
(save-excursion
(let ((emacs-lisp-docstring-fill-column t)
(fill-column (point-max)))
(fill-paragraph))
(let ((bop (copy-marker (progn (backward-paragraph) (point))))
(eop (copy-marker (progn (forward-paragraph) (point)))))
(goto-char bop)
(while (< (point) eop)
(forward-sentence)
(forward-whitespace 1)
(unless (>= (point) eop)
(delete-horizontal-space)
(insert "\n"))))))
(funcall mode)))
(auto-fill-mode -1))
(define-minor-mode split-para-mode
"Toggle between a filled paragraph and one split into sentences."
nil nil nil
(if (not split-para-mode)
(split-para-at-sentence-ends)
(auto-fill-mode 1)
(fill-paragraph)))
(global-set-key "\C-o" 'split-para-mode) ; Or some other key.
我使用 emacs 进行创意写作。为了更好地分析我的句子结构,我希望我的段落显示为每行一个句子。所以我需要一个函数,它可以接受一个普通的自动填充段落并执行以下操作:1) 将所有句子拉伸成一行,以及 2) 每行只放一个句子。
想象一下我写了以下段落(Suzanne Vega 的歌词)
My name is Luka. I live on the second floor. I live upstairs from you. Yes I think you've seen me before. If you hear something late at night. Some kind of trouble. Some kind of fight.
使用我希望的功能,段落将显示为:
My name is Luka.
I live on the second floor.
I live upstairs from you.
Yes I think you've seen me before.
If you hear something late at night.
Some kind of trouble.
Some kind of fight.
由于我想在这样显示句子的时候写一些东西,所以该功能除了拉长句子外还应该关闭自动填充模式。
理想情况下,我想要一个功能,可以在自动填充模式(所有句子都换行)和这种自动填充关闭且所有句子都展开的新模式之间切换显示。
提前感谢各种建议或帮助实现这样的功能!
@Drew:这是一段我无法与您的代码分开的文本:
有两种方法可以启用它:第一种是使用 M-x visual-line-mode(对于那些有真实菜单的,显然是 Options->Line Wrapping in this Buffer->Word Wrap),这会给你一个模式行中的次要模式“换行”。如 C-h f visual-line-mode 中所述,此命令的效果之一是巧妙地更改处理“行”的命令的效果:C-a、C-e 不再转到行尾(如 \n ),但转到行尾(如显示行)。 M-a、M-e 仍然正常工作。此外,垂直拆分 windows 保证不会被截断,并在更改宽度时适当调整大小。效果很好,特别是如果你有自由格式的文本,你在版本控制中保存(比如 Latex 中的论文),硬包装不能很好地工作。它还使垂直拆分更有用,尤其是对于巨大的 windows。根据我的经验,它会稍微减慢重绘速度,但这是值得的。
我想这就是您要的东西。
(defun split-para-at-sentence-ends ()
"Split current paragraph into lines with one sentence each.
Then turn off `auto-fill-mode'."
(interactive)
(let ((mode major-mode))
(unwind-protect
(progn (text-mode)
(save-excursion
(let ((emacs-lisp-docstring-fill-column t)
(fill-column (point-max)))
(fill-paragraph))
(let ((bop (copy-marker (progn (backward-paragraph) (point))))
(eop (copy-marker (progn (forward-paragraph) (point)))))
(goto-char bop)
(while (< (point) eop)
(forward-sentence)
(forward-whitespace 1)
(unless (>= (point) eop)
(delete-horizontal-space)
(insert "\n"))))))
(funcall mode)))
(auto-fill-mode -1))
(define-minor-mode split-para-mode
"Toggle between a filled paragraph and one split into sentences."
nil nil nil
(if (not split-para-mode)
(split-para-at-sentence-ends)
(auto-fill-mode 1)
(fill-paragraph)))
(global-set-key "\C-o" 'split-para-mode) ; Or some other key.