Endless Parentheses

Ramblings on productivity and technical subjects.

profile for Malabarba on Stack Exchange

Meta Binds Part 2: A peeve with paragraphs

Emacs is an intelligent editor in more ways than one. Instead of hardcoding its movement functions (such as end-of-defun or forward-paragraph), they're implemented in terms of delimiters which are configured by each major-mode. It works beautifully for functions, but it feels like a stone in my shoe for paragraphs.

Say what you will, but a paragraph delimiter is a blank line. Whichever mode I'm in, that's where I expect to go when I hit M-e and it annoys me to no end when some major-mode changes that.

(global-set-key (kbd "M-a") 'endless/backward-paragraph)
(global-set-key (kbd "M-e") 'endless/forward-paragraph)

(defun endless/forward-paragraph (&optional n)
  "Advance just past next blank line."
  (interactive "p")
  (let ((para-commands
         '(endless/forward-paragraph
           endless/backward-paragraph)))
    ;; Only push mark if it's not active and we're not
    ;; repeating.
    (or (use-region-p)
        (not (member this-command para-commands))
        (member last-command para-commands)
        (push-mark))
    ;; The actual movement.
    (dotimes (_ (abs n))
      (if (> n 0)
          (skip-chars-forward "\n[:blank:]")
        (skip-chars-backward "\n[:blank:]"))
      (if (search-forward-regexp
           "\n[[:blank:]]*\n[[:blank:]]*"
           nil t (cl-signum n))
          (goto-char (match-end 0))
        (goto-char
         (if (> n 0) (point-max) (point-min)))))))

(defun endless/backward-paragraph (&optional n)
  "Go back up to previous blank line."
  (interactive "p")
  (endless/forward-paragraph (- n)))

By no means am I the first to be bothered by this. Both Xah Lee and Magnar Sveen, for instance, also complain about this in html-mode (I'm more bothered by LaTeX-mode). Had I the bravery to delve into the mailing lists, I'd no doubt find posts on this that are older than me.

Does paragraph navigation ever get on your nerves?

comments powered by Disqus