* Re: help with what should have been a very simple defun
[not found] <e941a882-e952-432a-852b-63fd368b23c4n@googlegroups.com>
@ 2022-01-22 12:54 ` Pieter van Oostrum
2022-01-22 13:39 ` Emanuel Berg via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 2+ messages in thread
From: Pieter van Oostrum @ 2022-01-22 12:54 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 2523 bytes --]
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.
[-- Attachment #2: Type: text/plain, Size: 851 bytes --]
(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
[-- Attachment #3: Type: text/plain, Size: 111 bytes --]
--
Pieter van Oostrum <pieter@vanoostrum.org>
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
^ permalink raw reply [flat|nested] 2+ messages in thread