all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Pieter van Oostrum <pieter-l@vanoostrum.org>
To: help-gnu-emacs@gnu.org
Subject: Re: help with what should have been a very simple defun
Date: Sat, 22 Jan 2022 13:54:22 +0100	[thread overview]
Message-ID: <m24k5w3p8h.fsf@vanoostrum.org> (raw)
In-Reply-To: <e941a882-e952-432a-852b-63fd368b23c4n@googlegroups.com> (Btraven's message of "Mon, 17 Jan 2022 09:45:04 -0800 (PST)")

[-- 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]

       reply	other threads:[~2022-01-22 12:54 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <e941a882-e952-432a-852b-63fd368b23c4n@googlegroups.com>
2022-01-22 12:54 ` Pieter van Oostrum [this message]
2022-01-22 13:39   ` help with what should have been a very simple defun Emanuel Berg via Users list for the GNU Emacs text editor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m24k5w3p8h.fsf@vanoostrum.org \
    --to=pieter-l@vanoostrum.org \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.