1

I have a longish document and I want to number the paragraphs.

So,

Here is the first paragraph. It could span multiple visual lines.

Two paragraphs are separated by an empty line.

converts to :

1 Here is the first paragraph. It could span multiple visual lines.

2 Two paragraphs are separated by an empty line.

How can I do that?

And of course, if I add/ delete paragraphs, there should be some way to adjust the numbers. And it should be possible to remove the numbers.

If it can be done in some other mode like org-mode, etc. that would be fine.

  • 1
    Do you want the numbers to appear as part of the text, as annotations of the text, or as indicators in the margin/fringe? – Basil Jul 15 at 11:55
  • @Basil Any of these will work. But the blank lines should not be numbered. And it would be great if the numbers get printed when I print the document. – deshmukh Jul 15 at 11:58
  • How do you print the buffer? – Basil Jul 15 at 12:06
  • @Basil M-x print-buffer. But what I meant was it would be great if the numbers are part of the document text. I am not able to edit the earlier comment, unfortunately. – deshmukh Jul 15 at 12:54
3

A rather naive solution - there should be no leading numbers of a paragraph with a different --textual-- meaning:

Edited after request: When starting from an already numbered paragraph, take that value counting on.

(defun number-paragraphs (&optional takefirst)
  "Numbers resp. renumber paragraphs.

If starting from already numbered, take that value as offset "
  (interactive "*P")
  (let ((counter 0)
    (last 0))
    (when  (looking-at "\\([0-9]+\\)\. ")
      (setq counter (car (read-from-string  (match-string-no-properties 1))))
      (forward-paragraph))
    (while (and (forward-paragraph) (< last (point)))
      (setq last (copy-marker (point)))
      (backward-paragraph)
      (skip-chars-forward " \t\r\n\f")
      (when (looking-at "[0-9]+\. ")
    (delete-region (match-beginning 0) (match-end 0)))
      (insert (format "%s. " (1+ counter)))
      (setq counter (1+ counter))
      (goto-char last))))

(ert-deftest number-paragraph-test-OFHIl1 ()
  (with-temp-buffer
    (switch-to-buffer (current-buffer))
  (insert "Emacs is the extensible, customizable, self-documenting real-time
display editor.  This manual describes how to edit with Emacs and some
of the ways to customize it; it corresponds to GNU Emacs version 26.2.

   If you are reading this in Emacs, type ‘h’ to read a basic
introduction to the Info documentation system.

   For information on extending Emacs, see *note Emacs Lisp: (elisp)Top.

   This is the ‘GNU Emacs Manual’, updated for Emacs version 26.2.
")
      (goto-char (point-min))
      (number-paragraphs)
      (goto-char (point-min))
      (should (looking-at "1. Emacs"))
      (forward-paragraph)
      (skip-chars-forward " \t\r\n\f")
      (should (looking-at "2. If"))))

(ert-deftest number-paragraph-section-test-OFHIl1 ()
  (with-temp-buffer
    (switch-to-buffer (current-buffer))
    (insert "Emacs is the extensible, customizable, self-documenting real-time
display editor.  This manual describes how to edit with Emacs and some
of the ways to customize it; it corresponds to GNU Emacs version 26.2.

   2. If you are reading this in Emacs, type ‘h’ to read a basic
introduction to the Info documentation system.

   5. For information on extending Emacs, see *note Emacs Lisp: (elisp)Top.

   6. This is the ‘GNU Emacs Manual’, updated for Emacs version 26.2.
")
    (goto-char (point-min))
    (search-forward "If")
    (narrow-to-region (line-beginning-position) (point-max))
    (back-to-indentation)
    (number-paragraphs)
    (widen)
    (goto-char (point-min))
    (search-forward "For")
    (beginning-of-line)
    (should (looking-at " +3. For"))
    (search-forward "This")
    (beginning-of-line)
    (should (looking-at " +4. This"))))
  • Thanks. This adds numbers to the paragraphs. And renumbering is easy after adding/ deleting paragraphs. Would it be possible to restrict the numbering to current selection? I tried replacing point-min with region-beginning and < last (point) with region-end. But that did not work. – deshmukh Jul 17 at 8:29
  • @deshmukh Could make it start with a first number found. Then run from a narrowed buffer(?) – Andreas Röhler Jul 17 at 10:32
  • Thanks for the updated solution. Works just perfectly. And narrowing the buffer had never occurred to me :) – deshmukh Jul 21 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.