all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* 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

* Re: help with what should have been a very simple defun
  2022-01-22 12:54 ` help with what should have been a very simple defun Pieter van Oostrum
@ 2022-01-22 13:39   ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 2+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-01-22 13:39 UTC (permalink / raw)
  To: help-gnu-emacs

Pieter van Oostrum wrote:

>> (defun number-pgraphs (start end) ;; alias M-npg
>> "insert paragraph-numbers of chapters at paragraphs'

Well ... there is an obvious problem with this that you should
be aware of, and it is that it changes the material
it analyzes ...

That makes for difficult implementation.

But worse, often the idea itself isn't good, either - better
keep them apart ...

What about a "flash mode", when you fire it off it shows the
digits for x seconds and then the data shows as it was before.
Because it never changed, and only what was displayed to the
user did!

It's the "MVC". Or as I like to think about it, just keep
everything apart that don't belong naturally, like vodka and
cranberry, perhaps.

> (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

You can clean that up a lot ...

Byte-compile and do (checkdoc-current-buffer t) ...

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-22 13:39 UTC | newest]

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

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.