我已经看到 StackOverflow 上至少有两条建议在编辑LaTeX文档时在句子之间插入换行符.原因是该实践有助于源代码控制,diff协作编辑.
我基本上确信,但我很懒,而且我不想考虑它.
所以我正在寻找一些emacs咒语来为我处理它.可能是次要模式,可能是需要设置的一组变量.
我想我不想要的是
(set long-lines-auto-wrap 't)).这是因为我不想对我的协作者编辑施加要求,有时我会使用其他unix工具来检查这些文件.我想我不想要的是
fill-paragraph填充看起来像是标记句子结尾的换行符.auto-fill-mode将是一个奖励.那是:
聊天聊天.
一个新的句子
包含了需要修复的包装.
嘟m嘟
转变为:
聊天聊天.
一个新的句子包含了需要修复的包装.
嘟m嘟
您的意见和建议表示赞赏.
编辑:JouniK.Seppänen的建议指出了我LaTeX-fill-break-at-separators,这表明emacs几乎知道如何做到这一点.无论如何,我要读一些代码,然后报告回来.再次感谢.
同一问题的更一般版本:编辑摊牌:在句子末尾保持换行符.谢谢,dreeves.
这就是我使用的东西,其中大部分来自Luca de Alfaro:
(defun fill-sentence ()
(interactive)
(save-excursion
(or (eq (point) (point-max)) (forward-char))
(forward-sentence -1)
(indent-relative t)
(let ((beg (point))
(ix (string-match "LaTeX" mode-name)))
(forward-sentence)
(if (and ix (equal "LaTeX" (substring mode-name ix)))
(LaTeX-fill-region-as-paragraph beg (point))
(fill-region-as-paragraph beg (point))))))
Run Code Online (Sandbox Code Playgroud)
我把它绑M-j在一起
(global-set-key (kbd "M-j") 'fill-sentence)
Run Code Online (Sandbox Code Playgroud)
引用"LaTeX"是针对AUCTeX支持的.如果您不使用AUCTeX,let可以简化为
(let (beg (point))
(forward-sentence)
(fill-region-as-paragraph beg (point)))
Run Code Online (Sandbox Code Playgroud)
我一直想永远这样做,最近我发现这篇博文对我来说效果很好。所以这是我使用了几天的(稍微修改的版本)。
(defun auto-fill-by-sentences ()
(if (looking-back (sentence-end))
;; Break at a sentence
(progn
(LaTeX-newline)
t)
;; Fall back to the default
(do-auto-fill)))
(add-hook 'LaTeX-mode-hook (lambda () (setq auto-fill-function 'auto-fill-by-sentences)))
;; Modified from http://pleasefindattached.blogspot.com/2011/12/emacsauctex-sentence-fill-greatly.html
(defadvice LaTeX-fill-region-as-paragraph (around LaTeX-sentence-filling)
"Start each sentence on a new line."
(let ((from (ad-get-arg 0))
(to-marker (set-marker (make-marker) (ad-get-arg 1)))
tmp-end)
(while (< from (marker-position to-marker))
(forward-sentence)
;; might have gone beyond to-marker---use whichever is smaller:
(ad-set-arg 1 (setq tmp-end (min (point) (marker-position to-marker))))
ad-do-it
(ad-set-arg 0 (setq from (point)))
(unless (or (looking-back "^\\s *")
(looking-at "\\s *$"))
(LaTeX-newline)))
(set-marker to-marker nil)))
(ad-activate 'LaTeX-fill-region-as-paragraph)
Run Code Online (Sandbox Code Playgroud)
如果您在每个句子的末尾放置一个注释标记,Emacs 就会知道不要在注释中移动下一行:
chat chat chat.%
A new sentence
with goofed up wrapping that needs to be fixed.%
Mumble mumble%
Run Code Online (Sandbox Code Playgroud)
然后 Mq 分别填充每个句子,至少在 AUCTeX 11.85 中是这样。(如果你在 Emacs 中测试这个,似乎存在一个错误,如果这是缓冲区中的第一段并且你输入 Mq,你会收到一条错误消息。只需在文本前添加一个换行符即可解决该问题。)
如果您不想输入注释字符,您可以使用 LaTeX-fill-paragraph 并对其进行修改,以便行尾的句子结束标点符号的工作方式与注释类似。