[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
buffer-crunch.el
From: |
Emanuel Berg |
Subject: |
buffer-crunch.el |
Date: |
Sat, 20 Jul 2024 20:17:10 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
In the thread `buffer-sentences' some 5 days ago I wrote:
Here, one can also think of a general buffer parsing
function, where you could submit what unit you would like it
to be split up into.
Here is an attempt at doing just that!
The examples just below show most of the features.
There is a 'next' function to go forward, an optional explicit
'read' function - that is, not doing that based on 'next'
movements - there is also optional endpoints, and more.
Enjoy, I'll see if I can improve it with your help.
;;; -*- lexical-binding: t -*-
;;
;; this file:
;; https://dataswamp.org/~incal/emacs-init/buffer-crunch.el
;;
;; All features shown as examples:
;;
;; 1. Just a forward function:
;;
;; (buffer-crunch #'forward-line)
;;
;; 2. A forward function and endpoints:
;;
;; (buffer-crunch #'forward-word nil 0 100)
;;
;; 3. A forward function and a custom read function:
;;
;; (buffer-crunch #'forward-word (lambda () (word-at-point t)))
;;
;; 4. A custom forward function and custom read function:
;; (This forward function will overflow the buffer, but
;; that isn't a problem here.)
;;
;; (buffer-crunch (lambda () (forward-char 42)) (lambda () (word-at-point t)))
(require 'cl-lib)
(require 'thingatpt)
(defun strings-make-tidy (strs)
(let ((tst #'string=))
(cl-remove "" (cl-remove-duplicates (sort strs) :test tst) :test tst) ))
(defun wash-part (str &optional end)
(when (numberp str)
(setq str (buffer-substring-no-properties str end)) )
(string-trim (replace-regexp-in-string "[\n\t[:space:]]+" " " str)) )
(defun buffer-crunch (next &optional read beg end)
(or beg (setq beg (point-min)))
(or end (setq end (point-max)))
(save-mark-and-excursion
(goto-char beg)
(cl-loop
with fst
with otr
with strs
with mov = t
while (and (< (point) end) mov) do
(setq fst (point))
(condition-case nil
(apply next nil)
(error (goto-char end)) )
(setq otr (point))
(if (< fst otr)
(push (wash-part (or (and read (apply read nil)) fst) otr) strs)
(setq mov nil) )
finally return (strings-make-tidy strs) )))
(provide 'buffer-crunch)
--
underground experts united
https://dataswamp.org/~incal
- buffer-crunch.el,
Emanuel Berg <=