all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* insert gives args out of range
@ 2024-08-16  9:24 Heime
  2024-08-16  9:54 ` Stephen Berman
  0 siblings, 1 reply; 18+ messages in thread
From: Heime @ 2024-08-16  9:24 UTC (permalink / raw)
  To: Heime via Users list for the GNU Emacs text editor

I want to display text starting from "(defun" up to the second blank line that is encountered.

But with this code I get 

insert: Args out of range: #<buffer *Defun Sections*>, 2706, 2804

(defun display-text ()
  "Display the text between a line starting with '(defun' and the second blank line."
  (interactive)
  (let ((output-buffer (get-buffer-create "*Defun Sections*")))
    (with-current-buffer output-buffer
      (erase-buffer)) ;; Clear previous contents
    (save-excursion
      (goto-char (point-min))
      (while (re-search-forward "^(defun" nil t)
        (let ((start (match-beginning 0))
              (blank-lines 0)
              end)
          ;; Move forward to find the second blank line
          (while (and (< blank-lines 2)
                      (re-search-forward "^\\s-*$" nil t))
            (setq blank-lines (1+ blank-lines)))
          (setq end (point))
          ;; Ensure the range is valid before inserting
          (when (> end start)
            (with-current-buffer output-buffer
              (insert (buffer-substring-no-properties start end) "\n"))))))
    ;; Display the output buffer
    (display-buffer output-buffer)))





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

* Re: insert gives args out of range
  2024-08-16  9:24 insert gives args out of range Heime
@ 2024-08-16  9:54 ` Stephen Berman
  2024-08-16 10:20   ` Heime
  0 siblings, 1 reply; 18+ messages in thread
From: Stephen Berman @ 2024-08-16  9:54 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

On Fri, 16 Aug 2024 09:24:23 +0000 Heime <heimeborgia@protonmail.com> wrote:

> I want to display text starting from "(defun" up to the second blank line that is encountered.
>
> But with this code I get
>
> insert: Args out of range: #<buffer *Defun Sections*>, 2706, 2804
>
> (defun display-text ()
>   "Display the text between a line starting with '(defun' and the second blank line."
>   (interactive)
>   (let ((output-buffer (get-buffer-create "*Defun Sections*")))
>     (with-current-buffer output-buffer
>       (erase-buffer)) ;; Clear previous contents
>     (save-excursion
>       (goto-char (point-min))
>       (while (re-search-forward "^(defun" nil t)
>         (let ((start (match-beginning 0))
>               (blank-lines 0)
>               end)
>           ;; Move forward to find the second blank line
>           (while (and (< blank-lines 2)
>                       (re-search-forward "^\\s-*$" nil t))
>             (setq blank-lines (1+ blank-lines)))
>           (setq end (point))
>           ;; Ensure the range is valid before inserting
>           (when (> end start)
>             (with-current-buffer output-buffer
>               (insert (buffer-substring-no-properties start end) "\n"))))))
>     ;; Display the output buffer
>     (display-buffer output-buffer)))

You've set `start' and `end' in a different buffer than output-buffer
but you're invoking buffer-substring-no-properties with these values in
output-buffer, which you've erased, so those values are not in the range
of possible values in output-buffer.  Try let-binding the result of
invoking buffer-substring-no-properties before the second invocation of
with-current-buffer.

Steve Berman



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

* Re: insert gives args out of range
  2024-08-16  9:54 ` Stephen Berman
@ 2024-08-16 10:20   ` Heime
  2024-08-16 10:59     ` Heime
  0 siblings, 1 reply; 18+ messages in thread
From: Heime @ 2024-08-16 10:20 UTC (permalink / raw)
  To: Stephen Berman; +Cc: Heime via Users list for the GNU Emacs text editor


On Friday, August 16th, 2024 at 9:54 PM, Stephen Berman <stephen.berman@gmx.net> wrote:

> On Fri, 16 Aug 2024 09:24:23 +0000 Heime heimeborgia@protonmail.com wrote:
> 
> > I want to display text starting from "(defun" up to the second blank line that is encountered.
> > 
> > But with this code I get
> > 
> > insert: Args out of range: #<buffer Defun Sections>, 2706, 2804
> > 
> > (defun display-text ()
> > "Display the text between a line starting with '(defun' and the second blank line."
> > (interactive)
> > (let ((output-buffer (get-buffer-create "Defun Sections")))
> > (with-current-buffer output-buffer
> > (erase-buffer)) ;; Clear previous contents
> > (save-excursion
> > (goto-char (point-min))
> > (while (re-search-forward "^(defun" nil t)
> > (let ((start (match-beginning 0))
> > (blank-lines 0)
> > end)
> > ;; Move forward to find the second blank line
> > (while (and (< blank-lines 2)
> > (re-search-forward "^\\s-*$" nil t))
> > (setq blank-lines (1+ blank-lines)))
> > (setq end (point))
> > ;; Ensure the range is valid before inserting
> > (when (> end start)
> > (with-current-buffer output-buffer
> > (insert (buffer-substring-no-properties start end) "\n"))))))
> > ;; Display the output buffer
> > (display-buffer output-buffer)))
> 
> 
> You've set `start' and` end' in a different buffer than output-buffer
> but you're invoking buffer-substring-no-properties with these values in
> output-buffer, which you've erased, so those values are not in the range
> of possible values in output-buffer. Try let-binding the result of
> invoking buffer-substring-no-properties before the second invocation of
> with-current-buffer.
> 
> Steve Berman

You're correct



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

* Re: insert gives args out of range
  2024-08-16 10:20   ` Heime
@ 2024-08-16 10:59     ` Heime
  2024-08-16 11:51       ` Stephen Berman
  0 siblings, 1 reply; 18+ messages in thread
From: Heime @ 2024-08-16 10:59 UTC (permalink / raw)
  To: Heime; +Cc: Stephen Berman,
	Heime via Users list for the GNU Emacs text editor

On Friday, August 16th, 2024 at 10:20 PM, Heime <heimeborgia@protonmail.com> wrote:

> On Friday, August 16th, 2024 at 9:54 PM, Stephen Berman stephen.berman@gmx.net wrote:
> 
> > On Fri, 16 Aug 2024 09:24:23 +0000 Heime heimeborgia@protonmail.com wrote:
> > 
> > > I want to display text starting from "(defun" up to the second blank line that is encountered.
> > > 
> > > But with this code I get
> > > 
> > > insert: Args out of range: #<buffer Defun Sections>, 2706, 2804
> > > 
> > > (defun display-text ()
> > > "Display the text between a line starting with '(defun' and the second blank line."
> > > (interactive)
> > > (let ((output-buffer (get-buffer-create "Defun Sections")))
> > > (with-current-buffer output-buffer
> > > (erase-buffer)) ;; Clear previous contents
> > > (save-excursion
> > > (goto-char (point-min))
> > > (while (re-search-forward "^(defun" nil t)
> > > (let ((start (match-beginning 0))
> > > (blank-lines 0)
> > > end)
> > > ;; Move forward to find the second blank line
> > > (while (and (< blank-lines 2)
> > > (re-search-forward "^\\s-*$" nil t))
> > > (setq blank-lines (1+ blank-lines)))
> > > (setq end (point))
> > > ;; Ensure the range is valid before inserting
> > > (when (> end start)
> > > (with-current-buffer output-buffer
> > > (insert (buffer-substring-no-properties start end) "\n"))))))
> > > ;; Display the output buffer
> > > (display-buffer output-buffer)))
> > 
> > You've set `start' and` end' in a different buffer than output-buffer
> > but you're invoking buffer-substring-no-properties with these values in
> > output-buffer, which you've erased, so those values are not in the range
> > of possible values in output-buffer. Try let-binding the result of
> > invoking buffer-substring-no-properties before the second invocation of
> > with-current-buffer.
> > 
> > Steve Berman

I updated as suggested.  Although I get the output, the search stops on the first
blank line rather than on the second blank line.



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

* Re: insert gives args out of range
  2024-08-16 10:59     ` Heime
@ 2024-08-16 11:51       ` Stephen Berman
  2024-08-16 12:03         ` Heime
  0 siblings, 1 reply; 18+ messages in thread
From: Stephen Berman @ 2024-08-16 11:51 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

On Fri, 16 Aug 2024 10:59:09 +0000 Heime <heimeborgia@protonmail.com> wrote:

> On Friday, August 16th, 2024 at 10:20 PM, Heime <heimeborgia@protonmail.com> wrote:
>
>> On Friday, August 16th, 2024 at 9:54 PM, Stephen Berman
>> stephen.berman@gmx.net wrote:
>>
>> > On Fri, 16 Aug 2024 09:24:23 +0000 Heime heimeborgia@protonmail.com wrote:
>> >
>> > > I want to display text starting from "(defun" up to the second blank
>> > > line that is encountered.
>> > >
>> > > But with this code I get
>> > >
>> > > insert: Args out of range: #<buffer Defun Sections>, 2706, 2804
>> > >
>> > > (defun display-text ()
>> > > "Display the text between a line starting with '(defun' and the second
>> > > blank line."
>> > > (interactive)
>> > > (let ((output-buffer (get-buffer-create "Defun Sections")))
>> > > (with-current-buffer output-buffer
>> > > (erase-buffer)) ;; Clear previous contents
>> > > (save-excursion
>> > > (goto-char (point-min))
>> > > (while (re-search-forward "^(defun" nil t)
>> > > (let ((start (match-beginning 0))
>> > > (blank-lines 0)
>> > > end)
>> > > ;; Move forward to find the second blank line
>> > > (while (and (< blank-lines 2)
>> > > (re-search-forward "^\\s-*$" nil t))
>> > > (setq blank-lines (1+ blank-lines)))
>> > > (setq end (point))
>> > > ;; Ensure the range is valid before inserting
>> > > (when (> end start)
>> > > (with-current-buffer output-buffer
>> > > (insert (buffer-substring-no-properties start end) "\n"))))))
>> > > ;; Display the output buffer
>> > > (display-buffer output-buffer)))
>> >
>> > You've set `start' and` end' in a different buffer than output-buffer
>> > but you're invoking buffer-substring-no-properties with these values in
>> > output-buffer, which you've erased, so those values are not in the range
>> > of possible values in output-buffer. Try let-binding the result of
>> > invoking buffer-substring-no-properties before the second invocation of
>> > with-current-buffer.
>> >
>> > Steve Berman
>
> I updated as suggested.  Although I get the output, the search stops on the first
> blank line rather than on the second blank line.

Consider a buffer "*test*" with the following content (two blank lines
between the defuns):

;;;;;;;;;;;;;

(defun f1 ()
  (ignore))


(defun f2 ()
  (ignore))
;;;;;;;;;;;;;

 When you call `display-text' in "*test*", the first evaluation of the
sexp (re-search-forward "^\\s-*$" nil t) moves point to the first blank
line below the first defun.  Where is point after the second evaluation
of that sexp?

Steve Berman



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

* Re: insert gives args out of range
  2024-08-16 11:51       ` Stephen Berman
@ 2024-08-16 12:03         ` Heime
  2024-08-16 12:10           ` Heime
  0 siblings, 1 reply; 18+ messages in thread
From: Heime @ 2024-08-16 12:03 UTC (permalink / raw)
  To: Stephen Berman; +Cc: Heime via Users list for the GNU Emacs text editor

On Friday, August 16th, 2024 at 11:51 PM, Stephen Berman <stephen.berman@gmx.net> wrote:

> On Fri, 16 Aug 2024 10:59:09 +0000 Heime heimeborgia@protonmail.com wrote:
> 
> > On Friday, August 16th, 2024 at 10:20 PM, Heime heimeborgia@protonmail.com wrote:
> > 
> > > On Friday, August 16th, 2024 at 9:54 PM, Stephen Berman
> > > stephen.berman@gmx.net wrote:
> > > 
> > > > On Fri, 16 Aug 2024 09:24:23 +0000 Heime heimeborgia@protonmail.com wrote:
> > > > 
> > > > > I want to display text starting from "(defun" up to the second blank
> > > > > line that is encountered.
> > > > > 
> > > > > But with this code I get
> > > > > 
> > > > > insert: Args out of range: #<buffer Defun Sections>, 2706, 2804
> > > > > 
> > > > > (defun display-text ()
> > > > > "Display the text between a line starting with '(defun' and the second
> > > > > blank line."
> > > > > (interactive)
> > > > > (let ((output-buffer (get-buffer-create "Defun Sections")))
> > > > > (with-current-buffer output-buffer
> > > > > (erase-buffer)) ;; Clear previous contents
> > > > > (save-excursion
> > > > > (goto-char (point-min))
> > > > > (while (re-search-forward "^(defun" nil t)
> > > > > (let ((start (match-beginning 0))
> > > > > (blank-lines 0)
> > > > > end)
> > > > > ;; Move forward to find the second blank line
> > > > > (while (and (< blank-lines 2)
> > > > > (re-search-forward "^\\s-*$" nil t))
> > > > > (setq blank-lines (1+ blank-lines)))
> > > > > (setq end (point))
> > > > > ;; Ensure the range is valid before inserting
> > > > > (when (> end start)
> > > > > (with-current-buffer output-buffer
> > > > > (insert (buffer-substring-no-properties start end) "\n"))))))
> > > > > ;; Display the output buffer
> > > > > (display-buffer output-buffer)))
> > > > 
> > > > You've set `start' and` end' in a different buffer than output-buffer
> > > > but you're invoking buffer-substring-no-properties with these values in
> > > > output-buffer, which you've erased, so those values are not in the range
> > > > of possible values in output-buffer. Try let-binding the result of
> > > > invoking buffer-substring-no-properties before the second invocation of
> > > > with-current-buffer.
> > > > 
> > > > Steve Berman
> > 
> > I updated as suggested. Although I get the output, the search stops on the first
> > blank line rather than on the second blank line.
> 
> 
> Consider a buffer "test" with the following content (two blank lines
> between the defuns):
> 
> ;;;;;;;;;;;;;
> 
> (defun f1 ()
> (ignore))
> 
> 
> (defun f2 ()
> (ignore))
> ;;;;;;;;;;;;;
> 
> When you call `display-text' in "test", the first evaluation of the
> sexp (re-search-forward "^\\s-*$" nil t) moves point to the first blank
> line below the first defun. Where is point after the second evaluation
> of that sexp? - Steve Berman

It is still at the beginning of the first blank line.



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

* Re: insert gives args out of range
  2024-08-16 12:03         ` Heime
@ 2024-08-16 12:10           ` Heime
  2024-08-16 12:26             ` Stephen Berman
  0 siblings, 1 reply; 18+ messages in thread
From: Heime @ 2024-08-16 12:10 UTC (permalink / raw)
  To: Stephen Berman; +Cc: Heime via Users list for the GNU Emacs text editor


On Saturday, August 17th, 2024 at 12:03 AM, Heime <heimeborgia@protonmail.com> wrote:

> On Friday, August 16th, 2024 at 11:51 PM, Stephen Berman stephen.berman@gmx.net wrote:
> 
> > On Fri, 16 Aug 2024 10:59:09 +0000 Heime heimeborgia@protonmail.com wrote:
> > 
> > > On Friday, August 16th, 2024 at 10:20 PM, Heime heimeborgia@protonmail.com wrote:
> > > 
> > > > On Friday, August 16th, 2024 at 9:54 PM, Stephen Berman
> > > > stephen.berman@gmx.net wrote:
> > > > 
> > > > > On Fri, 16 Aug 2024 09:24:23 +0000 Heime heimeborgia@protonmail.com wrote:
> > > > > 
> > > > > > I want to display text starting from "(defun" up to the second blank
> > > > > > line that is encountered.
> > > > > > 
> > > > > > But with this code I get
> > > > > > 
> > > > > > insert: Args out of range: #<buffer Defun Sections>, 2706, 2804
> > > > > > 
> > > > > > (defun display-text ()
> > > > > > "Display the text between a line starting with '(defun' and the second
> > > > > > blank line."
> > > > > > (interactive)
> > > > > > (let ((output-buffer (get-buffer-create "Defun Sections")))
> > > > > > (with-current-buffer output-buffer
> > > > > > (erase-buffer)) ;; Clear previous contents
> > > > > > (save-excursion
> > > > > > (goto-char (point-min))
> > > > > > (while (re-search-forward "^(defun" nil t)
> > > > > > (let ((start (match-beginning 0))
> > > > > > (blank-lines 0)
> > > > > > end)
> > > > > > ;; Move forward to find the second blank line
> > > > > > (while (and (< blank-lines 2)
> > > > > > (re-search-forward "^\\s-*$" nil t))
> > > > > > (setq blank-lines (1+ blank-lines)))
> > > > > > (setq end (point))
> > > > > > ;; Ensure the range is valid before inserting
> > > > > > (when (> end start)
> > > > > > (with-current-buffer output-buffer
> > > > > > (insert (buffer-substring-no-properties start end) "\n"))))))
> > > > > > ;; Display the output buffer
> > > > > > (display-buffer output-buffer)))
> > > > > 
> > > > > You've set `start' and` end' in a different buffer than output-buffer
> > > > > but you're invoking buffer-substring-no-properties with these values in
> > > > > output-buffer, which you've erased, so those values are not in the range
> > > > > of possible values in output-buffer. Try let-binding the result of
> > > > > invoking buffer-substring-no-properties before the second invocation of
> > > > > with-current-buffer.
> > > > > 
> > > > > Steve Berman
> > > 
> > > I updated as suggested. Although I get the output, the search stops on the first
> > > blank line rather than on the second blank line.
> > 
> > Consider a buffer "test" with the following content (two blank lines
> > between the defuns):
> > 
> > ;;;;;;;;;;;;;
> > 
> > (defun f1 ()
> > (ignore))
> > 
> > (defun f2 ()
> > (ignore))
> > ;;;;;;;;;;;;;
> > 
> > When you call `display-text' in "test", the first evaluation of the
> > sexp (re-search-forward "^\\s-*$" nil t) moves point to the first blank
> > line below the first defun. Where is point after the second evaluation
> > of that sexp? - Steve Berman
> 
> 
> It is still at the beginning of the first blank line.

I do not understand why this happens.  Because (point) after re-search-forward should
give me the position at the beginning of the second blank line that is encountered.




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

* Re: insert gives args out of range
  2024-08-16 12:10           ` Heime
@ 2024-08-16 12:26             ` Stephen Berman
  2024-08-16 15:01               ` Heime
  0 siblings, 1 reply; 18+ messages in thread
From: Stephen Berman @ 2024-08-16 12:26 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

On Fri, 16 Aug 2024 12:10:48 +0000 Heime <heimeborgia@protonmail.com> wrote:

> On Saturday, August 17th, 2024 at 12:03 AM, Heime <heimeborgia@protonmail.com> wrote:
>
>> On Friday, August 16th, 2024 at 11:51 PM, Stephen Berman
>> stephen.berman@gmx.net wrote:
>>
>> > On Fri, 16 Aug 2024 10:59:09 +0000 Heime heimeborgia@protonmail.com wrote:
>> >
>> > > On Friday, August 16th, 2024 at 10:20 PM, Heime
>> > > heimeborgia@protonmail.com wrote:
>> > >
>> > > > On Friday, August 16th, 2024 at 9:54 PM, Stephen Berman
>> > > > stephen.berman@gmx.net wrote:
>> > > >
>> > > > > On Fri, 16 Aug 2024 09:24:23 +0000 Heime heimeborgia@protonmail.com wrote:
>> > > > >
>> > > > > > I want to display text starting from "(defun" up to the second blank
>> > > > > > line that is encountered.
>> > > > > >
>> > > > > > But with this code I get
>> > > > > >
>> > > > > > insert: Args out of range: #<buffer Defun Sections>, 2706, 2804
>> > > > > >
>> > > > > > (defun display-text ()
>> > > > > > "Display the text between a line starting with '(defun' and the second
>> > > > > > blank line."
>> > > > > > (interactive)
>> > > > > > (let ((output-buffer (get-buffer-create "Defun Sections")))
>> > > > > > (with-current-buffer output-buffer
>> > > > > > (erase-buffer)) ;; Clear previous contents
>> > > > > > (save-excursion
>> > > > > > (goto-char (point-min))
>> > > > > > (while (re-search-forward "^(defun" nil t)
>> > > > > > (let ((start (match-beginning 0))
>> > > > > > (blank-lines 0)
>> > > > > > end)
>> > > > > > ;; Move forward to find the second blank line
>> > > > > > (while (and (< blank-lines 2)
>> > > > > > (re-search-forward "^\\s-*$" nil t))
>> > > > > > (setq blank-lines (1+ blank-lines)))
>> > > > > > (setq end (point))
>> > > > > > ;; Ensure the range is valid before inserting
>> > > > > > (when (> end start)
>> > > > > > (with-current-buffer output-buffer
>> > > > > > (insert (buffer-substring-no-properties start end) "\n"))))))
>> > > > > > ;; Display the output buffer
>> > > > > > (display-buffer output-buffer)))
>> > > > >
>> > > > > You've set `start' and` end' in a different buffer than output-buffer
>> > > > > but you're invoking buffer-substring-no-properties with these values in
>> > > > > output-buffer, which you've erased, so those values are not in the range
>> > > > > of possible values in output-buffer. Try let-binding the result of
>> > > > > invoking buffer-substring-no-properties before the second invocation of
>> > > > > with-current-buffer.
>> > > > >
>> > > > > Steve Berman
>> > >
>> > > I updated as suggested. Although I get the output, the search stops on
>> > > the first
>> > > blank line rather than on the second blank line.
>> >
>> > Consider a buffer "test" with the following content (two blank lines
>> > between the defuns):
>> >
>> > ;;;;;;;;;;;;;
>> >
>> > (defun f1 ()
>> > (ignore))
>> >
>> > (defun f2 ()
>> > (ignore))
>> > ;;;;;;;;;;;;;
>> >
>> > When you call `display-text' in "test", the first evaluation of the
>> > sexp (re-search-forward "^\\s-*$" nil t) moves point to the first blank
>> > line below the first defun. Where is point after the second evaluation
>> > of that sexp? - Steve Berman
>>
>>
>> It is still at the beginning of the first blank line.
>
> I do not understand why this happens.  Because (point) after
> re-search-forward should give me the position at the beginning of the
> second blank line that is encountered.

The sexp (re-search-forward "^\\s-*$" nil t) succeeds if it find a line
containing only zero or more whitespace characters and moves point to
the end of the match.  If point is on a blank line, the search succeeds,
but since the beginning and end of the match coincide, point does not
move.  If you want point to move, your code needs to accommodate this
case.

Steve Berman




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

* Re: insert gives args out of range
  2024-08-16 12:26             ` Stephen Berman
@ 2024-08-16 15:01               ` Heime
  2024-08-16 15:09                 ` Heime
  0 siblings, 1 reply; 18+ messages in thread
From: Heime @ 2024-08-16 15:01 UTC (permalink / raw)
  To: Stephen Berman; +Cc: Heime via Users list for the GNU Emacs text editor

On Saturday, August 17th, 2024 at 12:26 AM, Stephen Berman <stephen.berman@gmx.net> wrote:

> On Fri, 16 Aug 2024 12:10:48 +0000 Heime heimeborgia@protonmail.com wrote:
> 
> > On Saturday, August 17th, 2024 at 12:03 AM, Heime heimeborgia@protonmail.com wrote:
> > 
> > > On Friday, August 16th, 2024 at 11:51 PM, Stephen Berman
> > > stephen.berman@gmx.net wrote:
> > > 
> > > > On Fri, 16 Aug 2024 10:59:09 +0000 Heime heimeborgia@protonmail.com wrote:
> > > > 
> > > > > On Friday, August 16th, 2024 at 10:20 PM, Heime
> > > > > heimeborgia@protonmail.com wrote:
> > > > > 
> > > > > > On Friday, August 16th, 2024 at 9:54 PM, Stephen Berman
> > > > > > stephen.berman@gmx.net wrote:
> > > > > > 
> > > > > > > On Fri, 16 Aug 2024 09:24:23 +0000 Heime heimeborgia@protonmail.com wrote:
> > > > > > > 
> > > > > > > > I want to display text starting from "(defun" up to the second blank
> > > > > > > > line that is encountered.
> > > > > > > > 
> > > > > > > > But with this code I get
> > > > > > > > 
> > > > > > > > insert: Args out of range: #<buffer Defun Sections>, 2706, 2804
> > > > > > > > 
> > > > > > > > (defun display-text ()
> > > > > > > > "Display the text between a line starting with '(defun' and the second
> > > > > > > > blank line."
> > > > > > > > (interactive)
> > > > > > > > (let ((output-buffer (get-buffer-create "Defun Sections")))
> > > > > > > > (with-current-buffer output-buffer
> > > > > > > > (erase-buffer)) ;; Clear previous contents
> > > > > > > > (save-excursion
> > > > > > > > (goto-char (point-min))
> > > > > > > > (while (re-search-forward "^(defun" nil t)
> > > > > > > > (let ((start (match-beginning 0))
> > > > > > > > (blank-lines 0)
> > > > > > > > end)
> > > > > > > > ;; Move forward to find the second blank line
> > > > > > > > (while (and (< blank-lines 2)
> > > > > > > > (re-search-forward "^\\s-*$" nil t))
> > > > > > > > (setq blank-lines (1+ blank-lines)))
> > > > > > > > (setq end (point))
> > > > > > > > ;; Ensure the range is valid before inserting
> > > > > > > > (when (> end start)
> > > > > > > > (with-current-buffer output-buffer
> > > > > > > > (insert (buffer-substring-no-properties start end) "\n"))))))
> > > > > > > > ;; Display the output buffer
> > > > > > > > (display-buffer output-buffer)))
> > > > > > > 
> > > > > > > You've set `start' and` end' in a different buffer than output-buffer
> > > > > > > but you're invoking buffer-substring-no-properties with these values in
> > > > > > > output-buffer, which you've erased, so those values are not in the range
> > > > > > > of possible values in output-buffer. Try let-binding the result of
> > > > > > > invoking buffer-substring-no-properties before the second invocation of
> > > > > > > with-current-buffer.
> > > > > > > 
> > > > > > > Steve Berman
> > > > > 
> > > > > I updated as suggested. Although I get the output, the search stops on
> > > > > the first
> > > > > blank line rather than on the second blank line.
> > > > 
> > > > Consider a buffer "test" with the following content (two blank lines
> > > > between the defuns):
> > > > 
> > > > ;;;;;;;;;;;;;
> > > > 
> > > > (defun f1 ()
> > > > (ignore))
> > > > 
> > > > (defun f2 ()
> > > > (ignore))
> > > > ;;;;;;;;;;;;;
> > > > 
> > > > When you call `display-text' in "test", the first evaluation of the
> > > > sexp (re-search-forward "^\\s-*$" nil t) moves point to the first blank
> > > > line below the first defun. Where is point after the second evaluation
> > > > of that sexp? - Steve Berman
> > > 
> > > It is still at the beginning of the first blank line.
> > 
> > I do not understand why this happens. Because (point) after
> > re-search-forward should give me the position at the beginning of the
> > second blank line that is encountered.
> 
> 
> The sexp (re-search-forward "^\\s-*$" nil t) succeeds if it find a line
> containing only zero or more whitespace characters and moves point to
> the end of the match. If point is on a blank line, the search succeeds,
> but since the beginning and end of the match coincide, point does not
> move. If you want point to move, your code needs to accommodate this
> case. - Steve Berman

Have printed start and end just after (setq end (point))

(message "Start: %d, End: %d" start end)

And the values for start and end are different.




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

* Re: insert gives args out of range
  2024-08-16 15:01               ` Heime
@ 2024-08-16 15:09                 ` Heime
  2024-08-16 15:16                   ` Stephen Berman
  0 siblings, 1 reply; 18+ messages in thread
From: Heime @ 2024-08-16 15:09 UTC (permalink / raw)
  To: Heime; +Cc: Stephen Berman,
	Heime via Users list for the GNU Emacs text editor

On Saturday, August 17th, 2024 at 3:01 AM, Heime <heimeborgia@protonmail.com> wrote:

> On Saturday, August 17th, 2024 at 12:26 AM, Stephen Berman stephen.berman@gmx.net wrote:
> 
> > On Fri, 16 Aug 2024 12:10:48 +0000 Heime heimeborgia@protonmail.com wrote:
> > 
> > > On Saturday, August 17th, 2024 at 12:03 AM, Heime heimeborgia@protonmail.com wrote:
> > > 
> > > > On Friday, August 16th, 2024 at 11:51 PM, Stephen Berman
> > > > stephen.berman@gmx.net wrote:
> > > > 
> > > > > On Fri, 16 Aug 2024 10:59:09 +0000 Heime heimeborgia@protonmail.com wrote:
> > > > > 
> > > > > > On Friday, August 16th, 2024 at 10:20 PM, Heime
> > > > > > heimeborgia@protonmail.com wrote:
> > > > > > 
> > > > > > > On Friday, August 16th, 2024 at 9:54 PM, Stephen Berman
> > > > > > > stephen.berman@gmx.net wrote:
> > > > > > > 
> > > > > > > > On Fri, 16 Aug 2024 09:24:23 +0000 Heime heimeborgia@protonmail.com wrote:
> > > > > > > > 
> > > > > > > > > I want to display text starting from "(defun" up to the second blank
> > > > > > > > > line that is encountered.
> > > > > > > > > 
> > > > > > > > > But with this code I get
> > > > > > > > > 
> > > > > > > > > insert: Args out of range: #<buffer Defun Sections>, 2706, 2804
> > > > > > > > > 
> > > > > > > > > (defun display-text ()
> > > > > > > > > "Display the text between a line starting with '(defun' and the second
> > > > > > > > > blank line."
> > > > > > > > > (interactive)
> > > > > > > > > (let ((output-buffer (get-buffer-create "Defun Sections")))
> > > > > > > > > (with-current-buffer output-buffer
> > > > > > > > > (erase-buffer)) ;; Clear previous contents
> > > > > > > > > (save-excursion
> > > > > > > > > (goto-char (point-min))
> > > > > > > > > (while (re-search-forward "^(defun" nil t)
> > > > > > > > > (let ((start (match-beginning 0))
> > > > > > > > > (blank-lines 0)
> > > > > > > > > end)
> > > > > > > > > ;; Move forward to find the second blank line
> > > > > > > > > (while (and (< blank-lines 2)
> > > > > > > > > (re-search-forward "^\\s-*$" nil t))
> > > > > > > > > (setq blank-lines (1+ blank-lines)))
> > > > > > > > > (setq end (point))
> > > > > > > > > ;; Ensure the range is valid before inserting
> > > > > > > > > (when (> end start)
> > > > > > > > > (with-current-buffer output-buffer
> > > > > > > > > (insert (buffer-substring-no-properties start end) "\n"))))))
> > > > > > > > > ;; Display the output buffer
> > > > > > > > > (display-buffer output-buffer)))
> > > > > > > > 
> > > > > > > > You've set `start' and` end' in a different buffer than output-buffer
> > > > > > > > but you're invoking buffer-substring-no-properties with these values in
> > > > > > > > output-buffer, which you've erased, so those values are not in the range
> > > > > > > > of possible values in output-buffer. Try let-binding the result of
> > > > > > > > invoking buffer-substring-no-properties before the second invocation of
> > > > > > > > with-current-buffer.
> > > > > > > > 
> > > > > > > > Steve Berman
> > > > > > 
> > > > > > I updated as suggested. Although I get the output, the search stops on
> > > > > > the first
> > > > > > blank line rather than on the second blank line.
> > > > > 
> > > > > Consider a buffer "test" with the following content (two blank lines
> > > > > between the defuns):
> > > > > 
> > > > > ;;;;;;;;;;;;;
> > > > > 
> > > > > (defun f1 ()
> > > > > (ignore))
> > > > > 
> > > > > (defun f2 ()
> > > > > (ignore))
> > > > > ;;;;;;;;;;;;;
> > > > > 
> > > > > When you call `display-text' in "test", the first evaluation of the
> > > > > sexp (re-search-forward "^\\s-*$" nil t) moves point to the first blank
> > > > > line below the first defun. Where is point after the second evaluation
> > > > > of that sexp? - Steve Berman
> > > > 
> > > > It is still at the beginning of the first blank line.
> > > 
> > > I do not understand why this happens. Because (point) after
> > > re-search-forward should give me the position at the beginning of the
> > > second blank line that is encountered.
> > 
> > The sexp (re-search-forward "^\\s-*$" nil t) succeeds if it find a line
> > containing only zero or more whitespace characters and moves point to
> > the end of the match. If point is on a blank line, the search succeeds,
> > but since the beginning and end of the match coincide, point does not
> > move. If you want point to move, your code needs to accommodate this
> > case. - Steve Berman
> 
> 
> Have printed start and end just after (setq end (point))
> 
> (message "Start: %d, End: %d" start end)
> 
> And the values for start and end are different.

When the first blank is reached, point moves to that blank, the next search
keeps point at the same place rather than moving to the next blank line.



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

* Re: insert gives args out of range
  2024-08-16 15:09                 ` Heime
@ 2024-08-16 15:16                   ` Stephen Berman
  2024-08-16 15:21                     ` Heime
  0 siblings, 1 reply; 18+ messages in thread
From: Stephen Berman @ 2024-08-16 15:16 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

On Fri, 16 Aug 2024 15:09:55 +0000 Heime <heimeborgia@protonmail.com> wrote:

> On Saturday, August 17th, 2024 at 3:01 AM, Heime <heimeborgia@protonmail.com> wrote:
>
>> On Saturday, August 17th, 2024 at 12:26 AM, Stephen Berman
>> stephen.berman@gmx.net wrote:
>>
>> > On Fri, 16 Aug 2024 12:10:48 +0000 Heime heimeborgia@protonmail.com wrote:
>> >
>> > > On Saturday, August 17th, 2024 at 12:03 AM, Heime
>> > > heimeborgia@protonmail.com wrote:
>> > >
>> > > > On Friday, August 16th, 2024 at 11:51 PM, Stephen Berman
>> > > > stephen.berman@gmx.net wrote:
>> > > >
>> > > > > On Fri, 16 Aug 2024 10:59:09 +0000 Heime heimeborgia@protonmail.com wrote:
>> > > > >
>> > > > > > On Friday, August 16th, 2024 at 10:20 PM, Heime
>> > > > > > heimeborgia@protonmail.com wrote:
>> > > > > >
>> > > > > > > On Friday, August 16th, 2024 at 9:54 PM, Stephen Berman
>> > > > > > > stephen.berman@gmx.net wrote:
>> > > > > > >
>> > > > > > > > On Fri, 16 Aug 2024 09:24:23 +0000 Heime
>> > > > > > > > heimeborgia@protonmail.com wrote:
>> > > > > > > >
>> > > > > > > > > I want to display text starting from "(defun" up to the
>> > > > > > > > > second blank
>> > > > > > > > > line that is encountered.
>> > > > > > > > >
>> > > > > > > > > But with this code I get
>> > > > > > > > >
>> > > > > > > > > insert: Args out of range: #<buffer Defun Sections>, 2706, 2804
>> > > > > > > > >
>> > > > > > > > > (defun display-text ()
>> > > > > > > > > "Display the text between a line starting with '(defun' and
>> > > > > > > > > the second
>> > > > > > > > > blank line."
>> > > > > > > > > (interactive)
>> > > > > > > > > (let ((output-buffer (get-buffer-create "Defun Sections")))
>> > > > > > > > > (with-current-buffer output-buffer
>> > > > > > > > > (erase-buffer)) ;; Clear previous contents
>> > > > > > > > > (save-excursion
>> > > > > > > > > (goto-char (point-min))
>> > > > > > > > > (while (re-search-forward "^(defun" nil t)
>> > > > > > > > > (let ((start (match-beginning 0))
>> > > > > > > > > (blank-lines 0)
>> > > > > > > > > end)
>> > > > > > > > > ;; Move forward to find the second blank line
>> > > > > > > > > (while (and (< blank-lines 2)
>> > > > > > > > > (re-search-forward "^\\s-*$" nil t))
>> > > > > > > > > (setq blank-lines (1+ blank-lines)))
>> > > > > > > > > (setq end (point))
>> > > > > > > > > ;; Ensure the range is valid before inserting
>> > > > > > > > > (when (> end start)
>> > > > > > > > > (with-current-buffer output-buffer
>> > > > > > > > > (insert (buffer-substring-no-properties start end) "\n"))))))
>> > > > > > > > > ;; Display the output buffer
>> > > > > > > > > (display-buffer output-buffer)))
>> > > > > > > >
>> > > > > > > > You've set `start' and` end' in a different buffer than output-buffer
>> > > > > > > > but you're invoking buffer-substring-no-properties with these
>> > > > > > > > values in
>> > > > > > > > output-buffer, which you've erased, so those values are not in
>> > > > > > > > the range
>> > > > > > > > of possible values in output-buffer. Try let-binding the result of
>> > > > > > > > invoking buffer-substring-no-properties before the second
>> > > > > > > > invocation of
>> > > > > > > > with-current-buffer.
>> > > > > > > >
>> > > > > > > > Steve Berman
>> > > > > >
>> > > > > > I updated as suggested. Although I get the output, the search stops on
>> > > > > > the first
>> > > > > > blank line rather than on the second blank line.
>> > > > >
>> > > > > Consider a buffer "test" with the following content (two blank lines
>> > > > > between the defuns):
>> > > > >
>> > > > > ;;;;;;;;;;;;;
>> > > > >
>> > > > > (defun f1 ()
>> > > > > (ignore))
>> > > > >
>> > > > > (defun f2 ()
>> > > > > (ignore))
>> > > > > ;;;;;;;;;;;;;
>> > > > >
>> > > > > When you call `display-text' in "test", the first evaluation of the
>> > > > > sexp (re-search-forward "^\\s-*$" nil t) moves point to the first blank
>> > > > > line below the first defun. Where is point after the second evaluation
>> > > > > of that sexp? - Steve Berman
>> > > >
>> > > > It is still at the beginning of the first blank line.
>> > >
>> > > I do not understand why this happens. Because (point) after
>> > > re-search-forward should give me the position at the beginning of the
>> > > second blank line that is encountered.
>> >
>> > The sexp (re-search-forward "^\\s-*$" nil t) succeeds if it find a line
>> > containing only zero or more whitespace characters and moves point to
>> > the end of the match. If point is on a blank line, the search succeeds,
>> > but since the beginning and end of the match coincide, point does not
>> > move. If you want point to move, your code needs to accommodate this
>> > case. - Steve Berman
>>
>>
>> Have printed start and end just after (setq end (point))
>>
>> (message "Start: %d, End: %d" start end)
>>
>> And the values for start and end are different.

`start' holds the value of the beginning of the match for "^(defun", but
your problem arises when searching for "^\\s-*$"; you'll see it if you
change the message to this:

(message "Start: %d, End: %d" (match-beginning 0) (match-end 0))

> When the first blank is reached, point moves to that blank, the next search
> keeps point at the same place rather than moving to the next blank line.

For the reason I explained above.

Steve Berman



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

* Re: insert gives args out of range
  2024-08-16 15:16                   ` Stephen Berman
@ 2024-08-16 15:21                     ` Heime
  2024-08-16 15:34                       ` Stephen Berman
  0 siblings, 1 reply; 18+ messages in thread
From: Heime @ 2024-08-16 15:21 UTC (permalink / raw)
  To: Stephen Berman; +Cc: Heime via Users list for the GNU Emacs text editor

On Saturday, August 17th, 2024 at 3:16 AM, Stephen Berman <stephen.berman@gmx.net> wrote:

> On Fri, 16 Aug 2024 15:09:55 +0000 Heime heimeborgia@protonmail.com wrote:
> 
> > On Saturday, August 17th, 2024 at 3:01 AM, Heime heimeborgia@protonmail.com wrote:
> > 
> > > On Saturday, August 17th, 2024 at 12:26 AM, Stephen Berman
> > > stephen.berman@gmx.net wrote:
> > > 
> > > > On Fri, 16 Aug 2024 12:10:48 +0000 Heime heimeborgia@protonmail.com wrote:
> > > > 
> > > > > On Saturday, August 17th, 2024 at 12:03 AM, Heime
> > > > > heimeborgia@protonmail.com wrote:
> > > > > 
> > > > > > On Friday, August 16th, 2024 at 11:51 PM, Stephen Berman
> > > > > > stephen.berman@gmx.net wrote:
> > > > > > 
> > > > > > > On Fri, 16 Aug 2024 10:59:09 +0000 Heime heimeborgia@protonmail.com wrote:
> > > > > > > 
> > > > > > > > On Friday, August 16th, 2024 at 10:20 PM, Heime
> > > > > > > > heimeborgia@protonmail.com wrote:
> > > > > > > > 
> > > > > > > > > On Friday, August 16th, 2024 at 9:54 PM, Stephen Berman
> > > > > > > > > stephen.berman@gmx.net wrote:
> > > > > > > > > 
> > > > > > > > > > On Fri, 16 Aug 2024 09:24:23 +0000 Heime
> > > > > > > > > > heimeborgia@protonmail.com wrote:
> > > > > > > > > > 
> > > > > > > > > > > I want to display text starting from "(defun" up to the
> > > > > > > > > > > second blank
> > > > > > > > > > > line that is encountered.
> > > > > > > > > > > 
> > > > > > > > > > > But with this code I get
> > > > > > > > > > > 
> > > > > > > > > > > insert: Args out of range: #<buffer Defun Sections>, 2706, 2804
> > > > > > > > > > > 
> > > > > > > > > > > (defun display-text ()
> > > > > > > > > > > "Display the text between a line starting with '(defun' and
> > > > > > > > > > > the second
> > > > > > > > > > > blank line."
> > > > > > > > > > > (interactive)
> > > > > > > > > > > (let ((output-buffer (get-buffer-create "Defun Sections")))
> > > > > > > > > > > (with-current-buffer output-buffer
> > > > > > > > > > > (erase-buffer)) ;; Clear previous contents
> > > > > > > > > > > (save-excursion
> > > > > > > > > > > (goto-char (point-min))
> > > > > > > > > > > (while (re-search-forward "^(defun" nil t)
> > > > > > > > > > > (let ((start (match-beginning 0))
> > > > > > > > > > > (blank-lines 0)
> > > > > > > > > > > end)
> > > > > > > > > > > ;; Move forward to find the second blank line
> > > > > > > > > > > (while (and (< blank-lines 2)
> > > > > > > > > > > (re-search-forward "^\\s-*$" nil t))
> > > > > > > > > > > (setq blank-lines (1+ blank-lines)))
> > > > > > > > > > > (setq end (point))
> > > > > > > > > > > ;; Ensure the range is valid before inserting
> > > > > > > > > > > (when (> end start)
> > > > > > > > > > > (with-current-buffer output-buffer
> > > > > > > > > > > (insert (buffer-substring-no-properties start end) "\n"))))))
> > > > > > > > > > > ;; Display the output buffer
> > > > > > > > > > > (display-buffer output-buffer)))
> > > > > > > > > > 
> > > > > > > > > > You've set `start' and` end' in a different buffer than output-buffer
> > > > > > > > > > but you're invoking buffer-substring-no-properties with these
> > > > > > > > > > values in
> > > > > > > > > > output-buffer, which you've erased, so those values are not in
> > > > > > > > > > the range
> > > > > > > > > > of possible values in output-buffer. Try let-binding the result of
> > > > > > > > > > invoking buffer-substring-no-properties before the second
> > > > > > > > > > invocation of
> > > > > > > > > > with-current-buffer.
> > > > > > > > > > 
> > > > > > > > > > Steve Berman
> > > > > > > > 
> > > > > > > > I updated as suggested. Although I get the output, the search stops on
> > > > > > > > the first
> > > > > > > > blank line rather than on the second blank line.
> > > > > > > 
> > > > > > > Consider a buffer "test" with the following content (two blank lines
> > > > > > > between the defuns):
> > > > > > > 
> > > > > > > ;;;;;;;;;;;;;
> > > > > > > 
> > > > > > > (defun f1 ()
> > > > > > > (ignore))
> > > > > > > 
> > > > > > > (defun f2 ()
> > > > > > > (ignore))
> > > > > > > ;;;;;;;;;;;;;
> > > > > > > 
> > > > > > > When you call `display-text' in "test", the first evaluation of the
> > > > > > > sexp (re-search-forward "^\\s-*$" nil t) moves point to the first blank
> > > > > > > line below the first defun. Where is point after the second evaluation
> > > > > > > of that sexp? - Steve Berman
> > > > > > 
> > > > > > It is still at the beginning of the first blank line.
> > > > > 
> > > > > I do not understand why this happens. Because (point) after
> > > > > re-search-forward should give me the position at the beginning of the
> > > > > second blank line that is encountered.
> > > > 
> > > > The sexp (re-search-forward "^\\s-*$" nil t) succeeds if it find a line
> > > > containing only zero or more whitespace characters and moves point to
> > > > the end of the match. If point is on a blank line, the search succeeds,
> > > > but since the beginning and end of the match coincide, point does not
> > > > move. If you want point to move, your code needs to accommodate this
> > > > case. - Steve Berman
> > > 
> > > Have printed start and end just after (setq end (point))
> > > 
> > > (message "Start: %d, End: %d" start end)
> > > 
> > > And the values for start and end are different.
> 
> 
> `start' holds the value of the beginning of the match for "^(defun", but
> your problem arises when searching for "^\\s-*$"; you'll see it if you
> change the message to this:
> 
> (message "Start: %d, End: %d" (match-beginning 0) (match-end 0))
> 
> > When the first blank is reached, point moves to that blank, the next search
> > keeps point at the same place rather than moving to the next blank line.
> 
> 
> For the reason I explained above. - Steve Berman

I have solved the problem by adding (forward-line 1) after setting blank-lines

          (while (and (< blank-lines 2)
                      (re-search-forward "^\\s-*$" nil t))
            (setq blank-lines (1+ blank-lines))
            ;; Move point forward to avoid re-matching the same blank line
            (forward-line 1))

Could this fail on some circumstances ?






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

* Re: insert gives args out of range
  2024-08-16 15:21                     ` Heime
@ 2024-08-16 15:34                       ` Stephen Berman
  2024-08-16 17:32                         ` Heime
  0 siblings, 1 reply; 18+ messages in thread
From: Stephen Berman @ 2024-08-16 15:34 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

On Fri, 16 Aug 2024 15:21:12 +0000 Heime <heimeborgia@protonmail.com> wrote:

> On Saturday, August 17th, 2024 at 3:16 AM, Stephen Berman
> <stephen.berman@gmx.net> wrote:
>
>> On Fri, 16 Aug 2024 15:09:55 +0000 Heime heimeborgia@protonmail.com wrote:
>>
>> > On Saturday, August 17th, 2024 at 3:01 AM, Heime
>> > heimeborgia@protonmail.com wrote:
>> >
>> > > On Saturday, August 17th, 2024 at 12:26 AM, Stephen Berman
>> > > stephen.berman@gmx.net wrote:
>> > >
>> > > > On Fri, 16 Aug 2024 12:10:48 +0000 Heime heimeborgia@protonmail.com wrote:
>> > > >
>> > > > > On Saturday, August 17th, 2024 at 12:03 AM, Heime
>> > > > > heimeborgia@protonmail.com wrote:
>> > > > >
>> > > > > > On Friday, August 16th, 2024 at 11:51 PM, Stephen Berman
>> > > > > > stephen.berman@gmx.net wrote:
>> > > > > >
>> > > > > > > On Fri, 16 Aug 2024 10:59:09 +0000 Heime
>> > > > > > > heimeborgia@protonmail.com wrote:
>> > > > > > >
>> > > > > > > > On Friday, August 16th, 2024 at 10:20 PM, Heime
>> > > > > > > > heimeborgia@protonmail.com wrote:
>> > > > > > > >
>> > > > > > > > > On Friday, August 16th, 2024 at 9:54 PM, Stephen Berman
>> > > > > > > > > stephen.berman@gmx.net wrote:
>> > > > > > > > >
>> > > > > > > > > > On Fri, 16 Aug 2024 09:24:23 +0000 Heime
>> > > > > > > > > > heimeborgia@protonmail.com wrote:
>> > > > > > > > > >
>> > > > > > > > > > > I want to display text starting from "(defun" up to the
>> > > > > > > > > > > second blank
>> > > > > > > > > > > line that is encountered.
>> > > > > > > > > > >
>> > > > > > > > > > > But with this code I get
>> > > > > > > > > > >
>> > > > > > > > > > > insert: Args out of range: #<buffer Defun Sections>, 2706, 2804
>> > > > > > > > > > >
>> > > > > > > > > > > (defun display-text ()
>> > > > > > > > > > > "Display the text between a line starting with '(defun' and
>> > > > > > > > > > > the second
>> > > > > > > > > > > blank line."
>> > > > > > > > > > > (interactive)
>> > > > > > > > > > > (let ((output-buffer (get-buffer-create "Defun Sections")))
>> > > > > > > > > > > (with-current-buffer output-buffer
>> > > > > > > > > > > (erase-buffer)) ;; Clear previous contents
>> > > > > > > > > > > (save-excursion
>> > > > > > > > > > > (goto-char (point-min))
>> > > > > > > > > > > (while (re-search-forward "^(defun" nil t)
>> > > > > > > > > > > (let ((start (match-beginning 0))
>> > > > > > > > > > > (blank-lines 0)
>> > > > > > > > > > > end)
>> > > > > > > > > > > ;; Move forward to find the second blank line
>> > > > > > > > > > > (while (and (< blank-lines 2)
>> > > > > > > > > > > (re-search-forward "^\\s-*$" nil t))
>> > > > > > > > > > > (setq blank-lines (1+ blank-lines)))
>> > > > > > > > > > > (setq end (point))
>> > > > > > > > > > > ;; Ensure the range is valid before inserting
>> > > > > > > > > > > (when (> end start)
>> > > > > > > > > > > (with-current-buffer output-buffer
>> > > > > > > > > > > (insert (buffer-substring-no-properties start end) "\n"))))))
>> > > > > > > > > > > ;; Display the output buffer
>> > > > > > > > > > > (display-buffer output-buffer)))
>> > > > > > > > > >
>> > > > > > > > > > You've set `start' and` end' in a different buffer than
>> > > > > > > > > > output-buffer
>> > > > > > > > > > but you're invoking buffer-substring-no-properties with these
>> > > > > > > > > > values in
>> > > > > > > > > > output-buffer, which you've erased, so those values are not in
>> > > > > > > > > > the range
>> > > > > > > > > > of possible values in output-buffer. Try let-binding the
>> > > > > > > > > > result of
>> > > > > > > > > > invoking buffer-substring-no-properties before the second
>> > > > > > > > > > invocation of
>> > > > > > > > > > with-current-buffer.
>> > > > > > > > > >
>> > > > > > > > > > Steve Berman
>> > > > > > > >
>> > > > > > > > I updated as suggested. Although I get the output, the search
>> > > > > > > > stops on
>> > > > > > > > the first
>> > > > > > > > blank line rather than on the second blank line.
>> > > > > > >
>> > > > > > > Consider a buffer "test" with the following content (two blank lines
>> > > > > > > between the defuns):
>> > > > > > >
>> > > > > > > ;;;;;;;;;;;;;
>> > > > > > >
>> > > > > > > (defun f1 ()
>> > > > > > > (ignore))
>> > > > > > >
>> > > > > > > (defun f2 ()
>> > > > > > > (ignore))
>> > > > > > > ;;;;;;;;;;;;;
>> > > > > > >
>> > > > > > > When you call `display-text' in "test", the first evaluation of the
>> > > > > > > sexp (re-search-forward "^\\s-*$" nil t) moves point to the first blank
>> > > > > > > line below the first defun. Where is point after the second evaluation
>> > > > > > > of that sexp? - Steve Berman
>> > > > > >
>> > > > > > It is still at the beginning of the first blank line.
>> > > > >
>> > > > > I do not understand why this happens. Because (point) after
>> > > > > re-search-forward should give me the position at the beginning of the
>> > > > > second blank line that is encountered.
>> > > >
>> > > > The sexp (re-search-forward "^\\s-*$" nil t) succeeds if it find a line
>> > > > containing only zero or more whitespace characters and moves point to
>> > > > the end of the match. If point is on a blank line, the search succeeds,
>> > > > but since the beginning and end of the match coincide, point does not
>> > > > move. If you want point to move, your code needs to accommodate this
>> > > > case. - Steve Berman
>> > >
>> > > Have printed start and end just after (setq end (point))
>> > >
>> > > (message "Start: %d, End: %d" start end)
>> > >
>> > > And the values for start and end are different.
>>
>>
>> `start' holds the value of the beginning of the match for "^(defun", but
>> your problem arises when searching for "^\\s-*$"; you'll see it if you
>> change the message to this:
>>
>> (message "Start: %d, End: %d" (match-beginning 0) (match-end 0))
>>
>> > When the first blank is reached, point moves to that blank, the next search
>> > keeps point at the same place rather than moving to the next blank line.
>>
>>
>> For the reason I explained above. - Steve Berman
>
> I have solved the problem by adding (forward-line 1) after setting blank-lines
>
>           (while (and (< blank-lines 2)
>                       (re-search-forward "^\\s-*$" nil t))
>             (setq blank-lines (1+ blank-lines))
>             ;; Move point forward to avoid re-matching the same blank line
>             (forward-line 1))
>
> Could this fail on some circumstances ?

Depends on what you consider failure: with the buffer "*test*" I gave as
an example above, output-buffer contains not just two but three empty
lines between the two defuns, and not just one but two empty lines at
the end; is that what you want?

Steve Berman



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

* Re: insert gives args out of range
  2024-08-16 15:34                       ` Stephen Berman
@ 2024-08-16 17:32                         ` Heime
  2024-08-16 21:29                           ` Stephen Berman
  0 siblings, 1 reply; 18+ messages in thread
From: Heime @ 2024-08-16 17:32 UTC (permalink / raw)
  To: Stephen Berman; +Cc: Heime via Users list for the GNU Emacs text editor

On Saturday, August 17th, 2024 at 3:34 AM, Stephen Berman <stephen.berman@gmx.net> wrote:

> On Fri, 16 Aug 2024 15:21:12 +0000 Heime heimeborgia@protonmail.com wrote:
> 
> > On Saturday, August 17th, 2024 at 3:16 AM, Stephen Berman
> > stephen.berman@gmx.net wrote:
> > 
> > > On Fri, 16 Aug 2024 15:09:55 +0000 Heime heimeborgia@protonmail.com wrote:
> > > 
> > > > On Saturday, August 17th, 2024 at 3:01 AM, Heime
> > > > heimeborgia@protonmail.com wrote:
> > > > 
> > > > > On Saturday, August 17th, 2024 at 12:26 AM, Stephen Berman
> > > > > stephen.berman@gmx.net wrote:
> > > > > 
> > > > > > On Fri, 16 Aug 2024 12:10:48 +0000 Heime heimeborgia@protonmail.com wrote:
> > > > > > 
> > > > > > > On Saturday, August 17th, 2024 at 12:03 AM, Heime
> > > > > > > heimeborgia@protonmail.com wrote:
> > > > > > > 
> > > > > > > > On Friday, August 16th, 2024 at 11:51 PM, Stephen Berman
> > > > > > > > stephen.berman@gmx.net wrote:
> > > > > > > > 
> > > > > > > > > On Fri, 16 Aug 2024 10:59:09 +0000 Heime
> > > > > > > > > heimeborgia@protonmail.com wrote:
> > > > > > > > > 
> > > > > > > > > > On Friday, August 16th, 2024 at 10:20 PM, Heime
> > > > > > > > > > heimeborgia@protonmail.com wrote:
> > > > > > > > > > 
> > > > > > > > > > > On Friday, August 16th, 2024 at 9:54 PM, Stephen Berman
> > > > > > > > > > > stephen.berman@gmx.net wrote:
> > > > > > > > > > > 
> > > > > > > > > > > > On Fri, 16 Aug 2024 09:24:23 +0000 Heime
> > > > > > > > > > > > heimeborgia@protonmail.com wrote:
> > > > > > > > > > > > 
> > > > > > > > > > > > > I want to display text starting from "(defun" up to the
> > > > > > > > > > > > > second blank
> > > > > > > > > > > > > line that is encountered.
> > > > > > > > > > > > > 
> > > > > > > > > > > > > But with this code I get
> > > > > > > > > > > > > 
> > > > > > > > > > > > > insert: Args out of range: #<buffer Defun Sections>, 2706, 2804
> > > > > > > > > > > > > 
> > > > > > > > > > > > > (defun display-text ()
> > > > > > > > > > > > > "Display the text between a line starting with '(defun' and
> > > > > > > > > > > > > the second
> > > > > > > > > > > > > blank line."
> > > > > > > > > > > > > (interactive)
> > > > > > > > > > > > > (let ((output-buffer (get-buffer-create "Defun Sections")))
> > > > > > > > > > > > > (with-current-buffer output-buffer
> > > > > > > > > > > > > (erase-buffer)) ;; Clear previous contents
> > > > > > > > > > > > > (save-excursion
> > > > > > > > > > > > > (goto-char (point-min))
> > > > > > > > > > > > > (while (re-search-forward "^(defun" nil t)
> > > > > > > > > > > > > (let ((start (match-beginning 0))
> > > > > > > > > > > > > (blank-lines 0)
> > > > > > > > > > > > > end)
> > > > > > > > > > > > > ;; Move forward to find the second blank line
> > > > > > > > > > > > > (while (and (< blank-lines 2)
> > > > > > > > > > > > > (re-search-forward "^\\s-*$" nil t))
> > > > > > > > > > > > > (setq blank-lines (1+ blank-lines)))
> > > > > > > > > > > > > (setq end (point))
> > > > > > > > > > > > > ;; Ensure the range is valid before inserting
> > > > > > > > > > > > > (when (> end start)
> > > > > > > > > > > > > (with-current-buffer output-buffer
> > > > > > > > > > > > > (insert (buffer-substring-no-properties start end) "\n"))))))
> > > > > > > > > > > > > ;; Display the output buffer
> > > > > > > > > > > > > (display-buffer output-buffer)))
> > > > > > > > > > > > 
> > > > > > > > > > > > You've set `start' and` end' in a different buffer than
> > > > > > > > > > > > output-buffer
> > > > > > > > > > > > but you're invoking buffer-substring-no-properties with these
> > > > > > > > > > > > values in
> > > > > > > > > > > > output-buffer, which you've erased, so those values are not in
> > > > > > > > > > > > the range
> > > > > > > > > > > > of possible values in output-buffer. Try let-binding the
> > > > > > > > > > > > result of
> > > > > > > > > > > > invoking buffer-substring-no-properties before the second
> > > > > > > > > > > > invocation of
> > > > > > > > > > > > with-current-buffer.
> > > > > > > > > > > > 
> > > > > > > > > > > > Steve Berman
> > > > > > > > > > 
> > > > > > > > > > I updated as suggested. Although I get the output, the search
> > > > > > > > > > stops on
> > > > > > > > > > the first
> > > > > > > > > > blank line rather than on the second blank line.
> > > > > > > > > 
> > > > > > > > > Consider a buffer "test" with the following content (two blank lines
> > > > > > > > > between the defuns):
> > > > > > > > > 
> > > > > > > > > ;;;;;;;;;;;;;
> > > > > > > > > 
> > > > > > > > > (defun f1 ()
> > > > > > > > > (ignore))
> > > > > > > > > 
> > > > > > > > > (defun f2 ()
> > > > > > > > > (ignore))
> > > > > > > > > ;;;;;;;;;;;;;
> > > > > > > > > 
> > > > > > > > > When you call `display-text' in "test", the first evaluation of the
> > > > > > > > > sexp (re-search-forward "^\\s-*$" nil t) moves point to the first blank
> > > > > > > > > line below the first defun. Where is point after the second evaluation
> > > > > > > > > of that sexp? - Steve Berman
> > > > > > > > 
> > > > > > > > It is still at the beginning of the first blank line.
> > > > > > > 
> > > > > > > I do not understand why this happens. Because (point) after
> > > > > > > re-search-forward should give me the position at the beginning of the
> > > > > > > second blank line that is encountered.
> > > > > > 
> > > > > > The sexp (re-search-forward "^\\s-*$" nil t) succeeds if it find a line
> > > > > > containing only zero or more whitespace characters and moves point to
> > > > > > the end of the match. If point is on a blank line, the search succeeds,
> > > > > > but since the beginning and end of the match coincide, point does not
> > > > > > move. If you want point to move, your code needs to accommodate this
> > > > > > case. - Steve Berman
> > > > > 
> > > > > Have printed start and end just after (setq end (point))
> > > > > 
> > > > > (message "Start: %d, End: %d" start end)
> > > > > 
> > > > > And the values for start and end are different.
> > > 
> > > `start' holds the value of the beginning of the match for "^(defun", but
> > > your problem arises when searching for "^\\s-*$"; you'll see it if you
> > > change the message to this:
> > > 
> > > (message "Start: %d, End: %d" (match-beginning 0) (match-end 0))
> > > 
> > > > When the first blank is reached, point moves to that blank, the next search
> > > > keeps point at the same place rather than moving to the next blank line.
> > > 
> > > For the reason I explained above. - Steve Berman
> > 
> > I have solved the problem by adding (forward-line 1) after setting blank-lines
> > 
> > (while (and (< blank-lines 2)
> > (re-search-forward "^\\s-*$" nil t))
> > (setq blank-lines (1+ blank-lines))
> > ;; Move point forward to avoid re-matching the same blank line
> > (forward-line 1))
> > 
> > Could this fail on some circumstances ?
> 
> 
> Depends on what you consider failure: with the buffer "test" I gave as
> an example above, output-buffer contains not just two but three empty
> lines between the two defuns, and not just one but two empty lines at
> the end; is that what you want?
> 
> Steve Berman

By failure, I mean this: Could Emacs fire an error buffer based on some unforeseen
structure of the file when re-search-forward executes ?




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

* Re: insert gives args out of range
  2024-08-16 17:32                         ` Heime
@ 2024-08-16 21:29                           ` Stephen Berman
  2024-08-16 21:47                             ` Heime
  0 siblings, 1 reply; 18+ messages in thread
From: Stephen Berman @ 2024-08-16 21:29 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

On Fri, 16 Aug 2024 17:32:49 +0000 Heime <heimeborgia@protonmail.com> wrote:

> On Saturday, August 17th, 2024 at 3:34 AM, Stephen Berman
> <stephen.berman@gmx.net> wrote:
>
>> On Fri, 16 Aug 2024 15:21:12 +0000 Heime heimeborgia@protonmail.com wrote:
[...]
>> > I have solved the problem by adding (forward-line 1) after setting blank-lines
>> >
>> > (while (and (< blank-lines 2)
>> > (re-search-forward "^\\s-*$" nil t))
>> > (setq blank-lines (1+ blank-lines))
>> > ;; Move point forward to avoid re-matching the same blank line
>> > (forward-line 1))
>> >
>> > Could this fail on some circumstances ?
>>
>>
>> Depends on what you consider failure: with the buffer "test" I gave as
>> an example above, output-buffer contains not just two but three empty
>> lines between the two defuns, and not just one but two empty lines at
>> the end; is that what you want?
>>
>> Steve Berman
>
> By failure, I mean this: Could Emacs fire an error buffer based on some unforeseen
> structure of the file when re-search-forward executes ?

Yes.

Steve Berman



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

* Re: insert gives args out of range
  2024-08-16 21:29                           ` Stephen Berman
@ 2024-08-16 21:47                             ` Heime
  2024-08-16 22:26                               ` Stephen Berman
  0 siblings, 1 reply; 18+ messages in thread
From: Heime @ 2024-08-16 21:47 UTC (permalink / raw)
  To: Stephen Berman; +Cc: Heime via Users list for the GNU Emacs text editor

On Saturday, August 17th, 2024 at 9:29 AM, Stephen Berman <stephen.berman@gmx.net> wrote:

> On Fri, 16 Aug 2024 17:32:49 +0000 Heime heimeborgia@protonmail.com wrote:
> 
> > On Saturday, August 17th, 2024 at 3:34 AM, Stephen Berman
> > stephen.berman@gmx.net wrote:
> > 
> > > On Fri, 16 Aug 2024 15:21:12 +0000 Heime heimeborgia@protonmail.com wrote:
> 
> [...]
> 
> > > > I have solved the problem by adding (forward-line 1) after setting blank-lines
> > > > 
> > > > (while (and (< blank-lines 2)
> > > > (re-search-forward "^\\s-*$" nil t))
> > > > (setq blank-lines (1+ blank-lines))
> > > > ;; Move point forward to avoid re-matching the same blank line
> > > > (forward-line 1))
> > > > 
> > > > Could this fail on some circumstances ?
> > > 
> > > Depends on what you consider failure: with the buffer "test" I gave as
> > > an example above, output-buffer contains not just two but three empty
> > > lines between the two defuns, and not just one but two empty lines at
> > > the end; is that what you want?
> > > 
> > > Steve Berman
> > 
> > By failure, I mean this: Could Emacs fire an error buffer based on some unforeseen
> > structure of the file when re-search-forward executes ?
> 
> 
> Yes. Steve Berman

Perhaps you can help me write an improved, more robust implementation, if the problem is
evident.



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

* Re: insert gives args out of range
  2024-08-16 21:47                             ` Heime
@ 2024-08-16 22:26                               ` Stephen Berman
  2024-08-17  9:47                                 ` Heime
  0 siblings, 1 reply; 18+ messages in thread
From: Stephen Berman @ 2024-08-16 22:26 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

On Fri, 16 Aug 2024 21:47:07 +0000 Heime <heimeborgia@protonmail.com> wrote:

> On Saturday, August 17th, 2024 at 9:29 AM, Stephen Berman
> <stephen.berman@gmx.net> wrote:
>
>> On Fri, 16 Aug 2024 17:32:49 +0000 Heime heimeborgia@protonmail.com wrote:
>>
>> > On Saturday, August 17th, 2024 at 3:34 AM, Stephen Berman
>> > stephen.berman@gmx.net wrote:
>> >
>> > > On Fri, 16 Aug 2024 15:21:12 +0000 Heime heimeborgia@protonmail.com wrote:
>>
>> [...]
>>
>> > > > I have solved the problem by adding (forward-line 1) after setting
>> > > > blank-lines
>> > > >
>> > > > (while (and (< blank-lines 2)
>> > > > (re-search-forward "^\\s-*$" nil t))
>> > > > (setq blank-lines (1+ blank-lines))
>> > > > ;; Move point forward to avoid re-matching the same blank line
>> > > > (forward-line 1))
>> > > >
>> > > > Could this fail on some circumstances ?
>> > >
>> > > Depends on what you consider failure: with the buffer "test" I gave as
>> > > an example above, output-buffer contains not just two but three empty
>> > > lines between the two defuns, and not just one but two empty lines at
>> > > the end; is that what you want?
>> > >
>> > > Steve Berman
>> >
>> > By failure, I mean this: Could Emacs fire an error buffer based on
>> > some unforeseen structure of the file when re-search-forward
>> > executes ?
>>
>>
>> Yes. Steve Berman
>
> Perhaps you can help me write an improved, more robust implementation,
> if the problem is evident.

If you post code and I see problems with it I can point them out and may
be able to suggest improvements, but I can make no useful response to
vague questions like whether the code could fail in "some circumstances"
or "based on some unforeseen structure of the file".

Steve Berman



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

* Re: insert gives args out of range
  2024-08-16 22:26                               ` Stephen Berman
@ 2024-08-17  9:47                                 ` Heime
  0 siblings, 0 replies; 18+ messages in thread
From: Heime @ 2024-08-17  9:47 UTC (permalink / raw)
  To: Stephen Berman; +Cc: Heime via Users list for the GNU Emacs text editor






Sent with Proton Mail secure email.

On Saturday, August 17th, 2024 at 10:26 AM, Stephen Berman <stephen.berman@gmx.net> wrote:

> On Fri, 16 Aug 2024 21:47:07 +0000 Heime heimeborgia@protonmail.com wrote:
> 
> > On Saturday, August 17th, 2024 at 9:29 AM, Stephen Berman
> > stephen.berman@gmx.net wrote:
> > 
> > > On Fri, 16 Aug 2024 17:32:49 +0000 Heime heimeborgia@protonmail.com wrote:
> > > 
> > > > On Saturday, August 17th, 2024 at 3:34 AM, Stephen Berman
> > > > stephen.berman@gmx.net wrote:
> > > > 
> > > > > On Fri, 16 Aug 2024 15:21:12 +0000 Heime heimeborgia@protonmail.com wrote:
> > > 
> > > [...]
> > > 
> > > > > > I have solved the problem by adding (forward-line 1) after setting
> > > > > > blank-lines
> > > > > > 
> > > > > > (while (and (< blank-lines 2)
> > > > > > (re-search-forward "^\\s-*$" nil t))
> > > > > > (setq blank-lines (1+ blank-lines))
> > > > > > ;; Move point forward to avoid re-matching the same blank line
> > > > > > (forward-line 1))
> > > > > > 
> > > > > > Could this fail on some circumstances ?
> > > > > 
> > > > > Depends on what you consider failure: with the buffer "test" I gave as
> > > > > an example above, output-buffer contains not just two but three empty
> > > > > lines between the two defuns, and not just one but two empty lines at
> > > > > the end; is that what you want?
> > > > > 
> > > > > Steve Berman
> > > > 
> > > > By failure, I mean this: Could Emacs fire an error buffer based on
> > > > some unforeseen structure of the file when re-search-forward
> > > > executes ?
> > > 
> > > Yes. Steve Berman
> > 
> > Perhaps you can help me write an improved, more robust implementation,
> > if the problem is evident.
> 
> 
> If you post code and I see problems with it I can point them out and may
> be able to suggest improvements, but I can make no useful response to
> vague questions like whether the code could fail in "some circumstances"
> or "based on some unforeseen structure of the file".
> 
> Steve Berman

Good, then we are happy with the latest implementation, of inserting 
(forward-line 1), yes ?



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

end of thread, other threads:[~2024-08-17  9:47 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-16  9:24 insert gives args out of range Heime
2024-08-16  9:54 ` Stephen Berman
2024-08-16 10:20   ` Heime
2024-08-16 10:59     ` Heime
2024-08-16 11:51       ` Stephen Berman
2024-08-16 12:03         ` Heime
2024-08-16 12:10           ` Heime
2024-08-16 12:26             ` Stephen Berman
2024-08-16 15:01               ` Heime
2024-08-16 15:09                 ` Heime
2024-08-16 15:16                   ` Stephen Berman
2024-08-16 15:21                     ` Heime
2024-08-16 15:34                       ` Stephen Berman
2024-08-16 17:32                         ` Heime
2024-08-16 21:29                           ` Stephen Berman
2024-08-16 21:47                             ` Heime
2024-08-16 22:26                               ` Stephen Berman
2024-08-17  9:47                                 ` Heime

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.