[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: insert gives args out of range
From: |
Stephen Berman |
Subject: |
Re: insert gives args out of range |
Date: |
Fri, 16 Aug 2024 13:51:49 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
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
- insert gives args out of range, Heime, 2024/08/16
- Re: insert gives args out of range, Stephen Berman, 2024/08/16
- Re: insert gives args out of range, Heime, 2024/08/16
- Re: insert gives args out of range, Heime, 2024/08/16
- Re: insert gives args out of range,
Stephen Berman <=
- Re: insert gives args out of range, Heime, 2024/08/16
- Re: insert gives args out of range, Heime, 2024/08/16
- Re: insert gives args out of range, Stephen Berman, 2024/08/16
- Re: insert gives args out of range, Heime, 2024/08/16
- Re: insert gives args out of range, Heime, 2024/08/16
- Re: insert gives args out of range, Stephen Berman, 2024/08/16
- Re: insert gives args out of range, Heime, 2024/08/16
- Re: insert gives args out of range, Stephen Berman, 2024/08/16
- Re: insert gives args out of range, Heime, 2024/08/16
- Re: insert gives args out of range, Stephen Berman, 2024/08/16