When working with LaTeX, it is recommended to start each sentence on a new line. The reasons can be found in Axel Brandenburg’s computing tips and this stack overflow page so I won’t repeat them here. However, as an emacs user, I always want emacs do the formatting for me.

The existing solutions are summarized here. The  most promising Emacs Lisp macro is provided by Chris Conway, which was cribbed from Luca de Alfaro. Their method does the job well. However, suppose I have a paragraph like the following (which is taken from this paper)

Once we obtain \citeauthor{Grad1949}'s coefficients, we can use them
to compute the flux terms.  \citeauthor{Grad1949}'s moment method is
linear in the fiducial frame.  The linearity naturally form a class of
closure schemes.  Since we fix the weight $w(\xi)$, the only freedoms
in the closures are the energy scale $\theta$ and the fiducial
reference frame corresponds to $U^\alpha$.

their fill-sentence macro has no effect because no line-break is placed between sentences.

So this morning I finally sat down and worked on the problem. I first needed to understand out how the standard fill-paragraph macro works:

$ gunzip -c /opt/local/share/emacs/23.3/lisp/textmodes/fill.el.gz | less

Note that my GNU Emacs 23.3 was installed by MacPorts, your path may be different. Scanning through the codes, I realized that all those fill-paragraph and fill-region macros go back to fill-region-as-paragraph (line 608 in the source). Hacking this function/macro may provide a good solution.

I copied the whole fill-region-as-paragraph function into my ~/.emacs and started playing around it. The final product is now on my github repository. I highlight the most important changes here

	...

	;; FROM, and point, are now before the text to fill,
	;; but after any fill prefix on the first line.
	(fill-delete-newlines from to justify nosqueeze squeeze-after)

	(if (not newline-after-sentence)
	    (fill-one-line from to justify) ;; original innner loop

	  ;; Insert a line break after each sentence
	  (goto-char from)
	  (while (< (point) to)
	    (forward-sentence)
	    (if (< (point) to) (fill-newline)))
	  ;; This is the actual filling loop.
	  (goto-char from)
	  (let (sentbeg sentend)
	    (while (< (point) to)
	      (setq sentbeg (point))
	      (end-of-line)
	      (setq sentend (point))
	      (fill-one-line sentbeg sentend justify) ;; original innner loop
	      (forward-line)))))

	...

From line 152 to 154, the macros inserts line-breaks after sentences. The loop from line 158 to 163 then fills sentence line-by-line. You can also look at the diff for more details.

Well, I should warn you that this is my first experience on Emacs Lisp. The macros seem to run correctly on GNU Emacs 23.3.1 but they surely contain bugs. Use and test them at your own risk, but please feel free to leave comment or bug report. I really hope this will become something useful for everybody. If you are ready to take the risk, you can append this hack to your ~/.emacs and override the original fill-region-as-paragraph macro:

$ curl https://raw.githubusercontent.com/chanchikwan/fill/master/hack.el >> ~/.emacs

Now, apply fill-paragraph (or simply M-q) in emacs results

Once we obtain \citeauthor{Grad1949}'s coefficients, we can use them
to compute the flux terms.
\citeauthor{Grad1949}'s moment method is linear in the fiducial frame.
The linearity naturally form a class of closure schemes.
Since we fix the weight $w(\xi)$, the only freedoms in the closures
are the energy scale $\theta$ and the fiducial reference frame
corresponds to $U^\alpha$.

which is exactly what I want.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Set your Twitter account name in your settings to use the TwitterBar Section.