9

I would like to change the behavior of fill-paragraph in certain modes (e.g. LaTeX-mode provided by AucTeX).

I could just rebind the key M-q, but I am also using evil-mode whose implementation of evil-fill-and-move uses fill-region. Ideally, my custom fill function to override both the functions fill-paragraph (so it works with M-q) and fill-region (so it works with evil).

Assuming that I have a standalone program format that takes in LaTeX code via stdin and output formatted code on stdout, how would I go about override the above two fill functions to use format?

(Note: this is similar to vim's formatprg option.)

c-o-d
  • 920
  • 9
  • 19
Kevin
  • 576
  • 1
  • 5
  • 16
  • Does rebinding M-q affect evil-fill-and-move in anyway ? I do not use evil hence I am curious. If region is active fill-paragraph calls fill-region anyway. So you might advice or replace fill-region with your function. – Vamsi Sep 24 '14 at 01:02
  • I have no tried it, but I believe not. evil-fill-and-move is bound to the key sequence gq in evil's normal mode. Rebinding M-q should not affect this keybinding.

    In some sense, my question is really two questions:

    1. How to replace the two functions?
    2. How to use an external program?

    The reason for 2) is that I already have an external, non-Elisp solution.

    – Kevin Sep 24 '14 at 01:06
  • 1
    In that case you can solve part of your problem by (add-hook 'LaTeX-mode-hook (lambda () local-set-key (kbd "M-q") 'your-fill-function)) where your-fill-function is your custom elisp defun. This will set that key combo only in Auctex. You could probably use shell-command-on-region with the REPLACE argument to define your-fill-function. – Vamsi Sep 24 '14 at 01:18

1 Answers1

7

It would be better to have 2 functions, although one can be implemented in terms of the other. The reason being that a paragraph is an implicit region, so the input in both cases should be different

(defun my-format-region (beg end)
  (interactive "r")
  (shell-command-on-region beg end "format"))

(defun my-format-paragraph ()
  (interactive)
  (save-excursion
    (mark-paragraph)
    (my-format-region (point) (mark))))

In order to substitute functions to others, regardless of their keybinding, use remap keybindings:

 (define-key LaTeX-mode-map [remap fill-region] 'my-format-region)
 (define-key LaTeX-mode-map [remap fill-paragraph] 'my-format-paragraph)
Sigma
  • 4,520
  • 22
  • 27