emacs-devel
[Top][All Lists]
Advanced

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

Re: buffer segments, uniform and automatic (was: Re: [Elisp: 8 out of 10


From: Emanuel Berg
Subject: Re: buffer segments, uniform and automatic (was: Re: [Elisp: 8 out of 10 problems] I think the last one! (point))
Date: Tue, 13 Aug 2024 19:10:37 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

Ihor Radchenko wrote:

>> -len is (- END BEG)
>> -reg is (BEG END)
>> -beg is BEG
>> -end is END
>> -cur is (buffer-substring-no-properties BEG END) [...]
>
> Check out thingatpt.el: `beginning-of-thing',
> `end-of-thing', `thing-at-point', etc.
>
> "thing" can be anything, including page, paragraph, word,
> link, sexp, etc

You are right, didn't think of that. Let's see later if it
can be reused.

Right now, I'm stuck with this.

It works for these segments:

(pos--def "data" (pos--data))
(pos--def "para" (pos--para))
(pos--def "sntc" (pos--sntc))

But not for this:

(pos--def "word" (pos--word))

It says about `forward-char' in the docstring:

    This function has a ‘byte-compile’ property
    ‘byte-compile-zero-or-one-arg’. See the manual
    for details.

Maybe that is the reason?

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/pos.el

(defun pos--wash-str (str)
  (string-trim
    (replace-regexp-in-string "%" "%%"
      (replace-regexp-in-string "[\n[:blank:]]+" " " str))))

(defmacro pos--def (name spn)
  (let (     (reg (intern (format      "%s-reg" name)))
             (beg (intern (format      "%s-beg" name)))
             (end (intern (format      "%s-end" name)))
             (cur (intern (format      "%s-cur" name)))
             (len (intern (format      "%s-len" name)))
        (goto-beg (intern (format "goto-%s-beg" name)))
        (goto-end (intern (format "goto-%s-end" name))))
    `(progn
       (defun      ,reg ()      (pos--reg ,spn))
       (defun      ,beg ()      (pos--beg ,spn))
       (defun      ,end ()      (pos--end ,spn))
       (defun      ,cur ()      (pos--cur ,spn))
       (defun      ,len ()      (pos--len ,spn))
       (defun ,goto-beg () (pos--goto-beg ,spn))
       (defun ,goto-end () (pos--goto-end ,spn)) )
    ))

(defun pos--reg (spn)
  (pcase-let* (( `(,beg ,end) spn ))
    (list beg end)))

(defun pos--beg (spn)
  (car (pos--reg spn)))

(defun pos--end (spn)
  (cadr (pos--reg spn)))

(defun pos--cur (spn)
  (pcase-let* (( `(,beg ,end) (pos--reg spn) )
               ( cur (pos--wash-str (buffer-substring-no-properties beg end)) ))
    cur))

(defun pos--len (spn)
  (length (pos--cur spn)))

(defun pos--goto-beg (spn)
  (goto-char (pos--beg spn)))

(defun pos--goto-end (spn)
  (goto-char (pos--end spn)))

(defun pos--data ()
  (save-mark-and-excursion
    (let ((min (point-min))
          (max (point-max))
          (beg)
          (end))
      (goto-char min)
      (re-search-forward "." max t)
      (setq beg (match-beginning 0))
      (goto-char max)
      (re-search-backward "." beg t)
      (setq end (match-end 0))
      (when (and beg end (< beg end))
        (list beg end)))))

(defun pos--para ()
  (save-mark-and-excursion
    (let ((beg (progn (start-of-paragraph-text) (point)))
          (end (progn (end-of-paragraph-text)   (point))))
      (list beg end))))

(defun pos--sntc ()
  (save-mark-and-excursion
    (let ((end (forward-sentence))
          (beg (forward-sentence -1)))
      (list beg end))))

(defun pos--word ()
  (save-mark-and-excursion
    (let ((end (forward-word))
          (beg (forward-word -1)))
      (list beg end))))

(pos--def "data" (pos--data))
(pos--def "para" (pos--para))
(pos--def "sntc" (pos--sntc))
(pos--def "word" (pos--word))

(provide 'pos)

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

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