From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Pieter van Oostrum Newsgroups: gmane.emacs.help Subject: Re: help with what should have been a very simple defun Date: Sat, 22 Jan 2022 13:54:22 +0100 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="14039"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (darwin) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sat Jan 22 14:09:36 2022 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nBG9A-0003U6-NO for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 22 Jan 2022 14:09:36 +0100 Original-Received: from localhost ([::1]:58218 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nBG99-0005RE-QZ for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 22 Jan 2022 08:09:35 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:36862) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nBFum-0005n8-CN for help-gnu-emacs@gnu.org; Sat, 22 Jan 2022 07:54:45 -0500 Original-Received: from smarthost-a.hosting2go.nl ([185.135.240.50]:39482) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nBFuX-0002Wh-HI for help-gnu-emacs@gnu.org; Sat, 22 Jan 2022 07:54:41 -0500 X-ASG-Debug-ID: 1642856063-0273707f3351c3a10001-2yy5ZX Original-Received: from server24.hosting2go.nl (server24.hosting2go.nl [185.135.241.24]) by smarthost-a.hosting2go.nl with ESMTP id tM3DmIKQ1GZPws6L (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO) for ; Sat, 22 Jan 2022 13:54:23 +0100 (CET) X-Barracuda-Envelope-From: pieter-l@vanoostrum.org X-Barracuda-Effective-Source-IP: server24.hosting2go.nl[185.135.241.24] X-Barracuda-Apparent-Source-IP: 185.135.241.24 Original-Received: (qmail 28561 invoked from network); 22 Jan 2022 12:54:23 -0000 Original-Received: from 82-168-23-214.fixed.kpn.net (HELO Cochabamba.local) (82.168.23.214) by server24.hosting2go.nl with ESMTPS (TLS_AES_256_GCM_SHA384 encrypted); 22 Jan 2022 12:54:23 -0000 Received-SPF: unknown (server24.hosting2go.nl: domain at 185.135.240.40 does not designate permitted sender hosts) X-ASG-Orig-Subj: Re: help with what should have been a very simple defun In-Reply-To: (Btraven's message of "Mon, 17 Jan 2022 09:45:04 -0800 (PST)") X-Barracuda-Connect: server24.hosting2go.nl[185.135.241.24] X-Barracuda-Start-Time: 1642856063 X-Barracuda-Encrypted: TLS_AES_256_GCM_SHA384 X-Barracuda-URL: https://185.135.240.50:443/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at hosting2go.nl X-Barracuda-Scan-Msg-Size: 3551 X-Barracuda-BRTS-Status: 1 X-Barracuda-Spam-Score: 0.50 X-Barracuda-Spam-Status: No, SCORE=0.50 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=4.5 tests=BSF_SC7_SG0129 A-X-Hosting2GO-Smarthost: Clean X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.95498 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- 0.50 BSF_SC7_SG0129 Custom rule SG0129 Received-SPF: none client-ip=185.135.240.50; envelope-from=pieter-l@vanoostrum.org; helo=smarthost-a.hosting2go.nl X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:135502 Archived-At: --=-=-= Content-Type: text/plain Btraven 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. --=-=-= Content-Type: text/plain Content-Disposition: inline (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 --=-=-= Content-Type: text/plain -- Pieter van Oostrum www: http://pieter.vanoostrum.org/ PGP key: [8DAE142BE17999C4] --=-=-=--