emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: streams are cool, you could stream virtually anything!


From: Michael Heerdegen
Subject: Re: streams are cool, you could stream virtually anything!
Date: Thu, 05 Nov 2015 16:15:42 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

Nicolas Petton <address@hidden> writes:

> > I don't know if this is a problem or not, which is why I am asking.
>
> No, it's not, unless you do things like (`seq-do' is eager):

Let's say it depends on what you want to achieve with this function.

The current implementation is simple but has two downsides:

  - It pollutes the searched buffer with markers, unnecessarily, because
  it doesn't allow to change the buffer, so positions (numbers) would
  suffice

  - It has no state.  If you want to use it like an iterator (aka
  `stream-pop'), you currently can't pause, do something else (move
  point, edit the buffer), and continue.

Here is a version that tries to solve these problems:

--8<---------------cut here---------------start------------->8---
;; -*- lexical-binding: t -*-
(defun stream-regexp (buffer regexp)
  (let ((match nil))
    (letrec ((builder
              (lambda (buffer regexp)
                (stream-make
                 (save-match-data
                   (save-excursion
                     (goto-char (or match (point-min)))
                     (with-current-buffer buffer
                       (setq match (re-search-forward regexp nil t)))
                     (when match
                       (setq match (copy-marker match))
                       (cons (cons (match-beginning 0)
                                   (match-end 0))
                             (funcall builder buffer regexp)))))))))
      (funcall builder buffer regexp))))
--8<---------------cut here---------------end--------------->8---



Regards,

Michael.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]