Subject: Re: bug#58904: [PATCH] Handle buffer streams that operate on lines, pages, etc.

From: Stefan Kangas <[email-masked]>
Date: Thu, 24 Nov 2022 11:40:02 -0800

Philip Kaludercic <[email-masked]> writes:

> Tags: patch
>
> The commentary for stream.el mentions that the following streams are
> supported:
>
>> ;; - buffers (by character)
>> ;; - buffers (by line)
>> ;; - buffers (by page)
>
> But I couldn't find anything beyond charachter-streams.  The following
> patch would implement that and more, by re-using thingatpt to define any
> kind of stream one would want.

Copying in Nicolas Petton, in case he has any comments.

> In GNU Emacs 29.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
>  3.24.30, cairo version 1.16.0) of 2022-10-30 built on heron
> Repository revision: 2a4f37fe520b4f18295cff6671f289a47c1578df
> Repository branch: feature/package+vc
> System Description: Guix System
>
> Configured using:
>  'configure --with-pgtk --with-imagemagick
>  PKG_CONFIG_PATH=/gnu/store/ssg343s6ldqdwh30136pnawhbgd0cb6i-profile/lib/pkgconfig:/gnu/store/ssg343s6ldqdwh30136pnawhbgd0cb6i-profile/share/pkgconfig'
>
>>>From 1593ba1da6a5caad7f74603ce9f8d5bf1e55de77 Mon Sep 17 00:00:00 2001
> From: Philip Kaludercic <[email-masked]>
> Date: Sun, 30 Oct 2022 22:28:25 +0100
> Subject: [PATCH] Handle buffer streams that operate on lines, pages, etc.
>
> * stream.el (stream): Add optional argument THING.
>
> This is promised by the package commentary but apparently wasn't
> actually implemented.
> ---
>  stream.el | 48 ++++++++++++++++++++++++++++++++++--------------
>  1 file changed, 34 insertions(+), 14 deletions(-)
>
> diff --git a/stream.el b/stream.el
> index eb81b14220..d8d4bfc3c7 100644
> --- a/stream.el
> +++ b/stream.el
> @@ -1,6 +1,6 @@
>  ;;; stream.el --- Implementation of streams  -*- lexical-binding: t -*-
>
> -;; Copyright (C) 2016-2020 Free Software Foundation, Inc.
> +;; Copyright (C) 2016-2020, 2022 Free Software Foundation, Inc.
>
>  ;; Author: Nicolas Petton <[email-masked]>
>  ;; Keywords: stream, laziness, sequences
> @@ -120,22 +120,42 @@ SEQ can be a list, vector or string."
>       (car list)
>       (stream (cdr list)))))
>
> -(cl-defmethod stream ((buffer buffer) &optional pos)
> +(cl-defmethod stream ((buffer buffer) &optional pos thing)
>    "Return a stream of the characters of the buffer BUFFER.
> -BUFFER may be a buffer or a string (buffer name).
> -The sequence starts at POS if non-nil, `point-min' otherwise."
> +BUFFER may be a buffer or a string (buffer name).  The sequence
> +starts at POS if non-nil, `point-min' otherwise.  By default the
> +stream will consist of characters.  If THING is non-nil, it must
> +be a symbol supported by `thing-at-point' and `forward-thing'
> +that will be used to extract and navigate through things.
> +Examples include \\='line, \\='page, or \\='defun."
>    (with-current-buffer buffer
>      (unless pos (setq pos (point-min)))
> -    (if (>= pos (point-max))
> -        (stream-empty))
> -    (stream-cons
> -     (with-current-buffer buffer
> -       (save-excursion
> -         (save-restriction
> -           (widen)
> -           (goto-char pos)
> -           (char-after (point)))))
> -     (stream buffer (1+ pos)))))
> +    (cond
> +     ((>= pos (point-max))
> +      (stream-empty))
> +     ((not (null thing))
> +      (with-current-buffer buffer
> +        (let ((this (save-excursion
> +                      (save-restriction
> +                        (widen)
> +                        (goto-char pos)
> +                        (thing-at-point thing))))
> +              (next (save-excursion
> +                      (save-restriction
> +                        (widen)
> +                        (goto-char pos)
> +                        (forward-thing thing)
> +                        (point)))))
> +          (stream-cons this (stream buffer next thing)))))
> +     ((null thing)
> +      (stream-cons
> +       (with-current-buffer buffer
> +         (save-excursion
> +           (save-restriction
> +             (widen)
> +             (goto-char pos)
> +             (char-after (point)))))
> +       (stream buffer (1+ pos)))))))
>
>  (declare-function iter-next "generator")