emacs-devel
[Top][All Lists]
Advanced

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

Re: Possibility for a stream editor (sed) inside emacs ?


From: Ted Zlatanov
Subject: Re: Possibility for a stream editor (sed) inside emacs ?
Date: Wed, 25 May 2011 08:11:44 -0500
User-agent: Gnus/5.110018 (No Gnus v0.18) Emacs/24.0.50 (gnu/linux)

On Wed, 25 May 2011 02:08:05 +0300 Alin Soare <address@hidden> wrote: 

AS> My purpose was to write the machine that makes the computations, and not to
AS> write a complete sed. Its parser that generates tokens for example does not
AS> jump over the spaces. It's just a toy sed.
...
AS> Do you consider, an internal stream editor would be good for emacs?

Emacs already has good text editing functionality.  Do you like sed's
brevity?  I think most of it can be emulated with an inline DSL based on
macros instead of a full VM and parser.

I do think it's worthwhile to explore a "apply a function to every line
or block of a file" wrapper.  The key utility would be that large files
will not need to be loaded entirely into a buffer.  It's trivial with
data blocks and a little harder with line-oriented processing, but still
not too bad.  Then any stream processing functionality, including
something like sed, can be passed to the wrapper as a lambda.

Something like this line-oriented file processor (found in my .emacs but
IIRC originally from several sources, and not well tested) is what I'm
thinking of:

#+begin_src lisp
(defun map-file-lines (file func &optional startline count bufsize)
  (let ((filepos 0)
        (linenum 0)
        (bufsize (or bufsize (* 128 1024))))
    (with-temp-buffer
      (while
          (let*
              ((inserted (insert-file-contents
                          file nil
                          filepos (+ filepos bufsize)
                          t))
               (numlines (count-lines (point-min) (point-max)))
               (read (nth 1 inserted))
               (done (< 1 read))
               result line-end)
            (while (not (zerop (decf numlines)))
                (goto-char (point-min))
                (setq line-end (line-end-position)
                      result (if (and startline (< linenum startline))
                                 ()
                               (if (and
                                    count
                                    (>= (- linenum startline) count))
                                   (return)
                                 (funcall func
                                          (buffer-substring
                                           (line-beginning-position)
                                           line-end)
                                          linenum)))
                      done (and done result))
                (incf filepos line-end)
                (forward-line)
                (incf linenum))
            done)))
    linenum))
#+end_src

Ted




reply via email to

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