Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Emacs to fill sentences, but not paragraphs?

Tags:

emacs

latex

fill

I've seen at least two recommendations on StackOverflow to insert newlines between sentences when editing LaTeX documents. The reason being that the practice facilitates source control, diffing, and collaborative editing.

I'm basically convinced, but I'm lazy, and I don't want to have to think about it.

So I'm searching for some emacs incantation to handle it for me. Could be a minor mode, could be a set of variables that need to be set.

I think what I don't want is

  • Soft wrapping of text (say using the longlines and (set long-lines-auto-wrap 't)). This is because I don't want to impose requirements on my collaborators' editors, and I sometimes use other unix tools to examine these files.

I think what I do want is

  • For fill-paragraph to fill between newlines that look like they mark the end of a sentence.
  • A solution that works with auto-fill-mode would be a bonus.

That is:

chat chat chat.
A new sentence
with goofed up wrapping that needs to be fixed.
Mumble mumble

Transformed to:

chat chat chat.
A new sentence with goofed up wrapping that needs to be fixed.
Mumble mumble

Your comments and suggestions are appreciated.


Edit: The suggestion by Jouni K. Seppänen pointed me at LaTeX-fill-break-at-separators, which suggests that emacs almost knows how to do this already. Anyway, I'm off to read some code, and will report back. Thanks again.


More general version of the same question: Editor showdown: Maintain newlines at the ends of sentences. Thanks, dreeves.

like image 496
dmckee --- ex-moderator kitten Avatar asked Feb 12 '09 04:02

dmckee --- ex-moderator kitten


People also ask

What is the fill column in Emacs?

The variable fill-column illustrates a symbol with a value attached to it: in every GNU Emacs buffer, this symbol is set to some value, usually 72 or 70, but sometimes to some other value. To find the value of this symbol, evaluate it by itself.

What is fill paragraph?

The command M-q ( fill-paragraph ) fills the current paragraph. It redistributes the line breaks within the paragraph, and deletes any excess space and tab characters occurring within the paragraph, in such a way that the lines end up fitting within a certain maximum width.


1 Answers

Here's what I use, which was mostly cribbed from 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)))))) 

I bind this to M-j with

(global-set-key (kbd "M-j") 'fill-sentence) 

The references to "LaTeX" are for AUCTeX support. If you don't use AUCTeX, the let can be simplified to

(let (beg (point))   (forward-sentence)   (fill-region-as-paragraph beg (point))) 
like image 86
Chris Conway Avatar answered Sep 20 '22 14:09

Chris Conway