help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: help with what should have been a very simple defun


From: Pieter van Oostrum
Subject: Re: help with what should have been a very simple defun
Date: Sat, 22 Jan 2022 13:54:22 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (darwin)

Btraven <caudex2@gmail.com> writes:

> Dear Emacs gurus:
>
> I have been able to use the following elisp code to number paragraphs in a 
> plain text file but for some reason it doesn't act like the deterministic 
> machine it should be:
>
> (defun number-pgraphs (start end) ;; alias M-npg
> "insert paragraph-numbers of chapters at paragraphs' start in region. Blank 
> lines have been guaranteed to consist of only a single C-j"
> (interactive "r")
> (save-excursion
> (setq i 1)  ;; current par. number
> (goto-char start)
> (while (< (point) end)
> (forward-line)
> (while (and (bolp) (eolp)) (forward-line)) 
> ;; skip blank line(s) with only linefeed chars
> (if  (not (= (char-after (point)) 42)) ;; not * character, start of new 
> chapter
>  (progn
>       (insert (concat (number-to-string i) "." " "))
>       (setq i (1+ i)))
>  )))
> ) ;; defun
>
> Chapters are *1, *2, *3 at bol, etc. to very many chapters and
> all paragraphs are unfilled (one long line) and separated by minimal blank 
> line (no white space).  Paragraphs are numbered correctly at least 95% of the 
> time (now and then they're all numbered but usually I have to add a few 
> paragraph numbers at ends of chapters manually ) .
> I was hoping to figure this out before manually editing the whole thing and 
> maybe add an outer loop for chapters in order  to process the whole file at 
> once, but no such luck. After making many versions of the above defun, I have 
> decided to escalate the problem.
>
> my emacs is w64 of ver. 24-3-1

Basically, I think your algorithm is correct. Maybe something fails if your 
paragraphs don't conform to your specification.

But there can be some improvements:
- The variable name 'i' is too generic; use a more descriptive one, like 
'par-number'. Also the variable should be made local with '(let ...)', 
otherwise it may interact with another one of the same name.
- Put the '(forward-line)' at the end of the loop, so that you also look at the 
first line.
- Reset the paragraph number at the beginning of each chapter (I assume that 
paragraph numbers start at 1 at each chapter).
- Remove existing paragraph numbers before inserting a new one. In this way you 
can just rerun the numbering if something went wrong without having to undo the 
previous operation.
- Use ?* instead of 42, it is more clear this way.

Here is an improved version. Please try it, and if it still gives problems, 
give some indication of what the input looks like where it fails.

By the way I am using Emacs 27.2. It is still possible that your Emacs has a 
bug.

(defun number-pgraphs (start end) ;; alias M-npg
  "insert paragraph-numbers of chapters at paragraphs' start in region. Blank 
lines have been guaranteed to consist of only a single C-j"
  (interactive "r")
  (let ((par-number 1))
    (save-excursion
      (goto-char start)
      (while (< (point) end)
        (while (and (bolp) (eolp)) (forward-line)) 
        ;; skip blank line(s) with only linefeed chars
        (if (= (following-char) ?*)
            ;; * character, start of new chapter - reset paragraph number
            (setq par-number 1)
          ;; Check if there is already a paragraph number, remove it first
          (if (looking-at "[0-9]+\. ")
              (replace-match ""))
          (insert (concat (number-to-string par-number) "." " "))
          (setq par-number (1+ par-number)))
        (forward-line))))
  ) ;; defun
-- 
Pieter van Oostrum <pieter@vanoostrum.org>
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]

reply via email to

[Prev in Thread] Current Thread [Next in Thread]