all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Emanuel Berg <incal@dataswamp.org>
To: emacs-devel@gnu.org
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	[thread overview]
Message-ID: <87frr8fiwi.fsf@dataswamp.org> (raw)
In-Reply-To: 875xs4ikdk.fsf@localhost

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	other threads:[~2024-08-13 17:10 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-11  0:14 [Elisp: 8 out of 10 problems] I think the last one! (point) Emanuel Berg
2024-08-11  4:46 ` Emanuel Berg
2024-08-11  5:43   ` Eli Zaretskii
2024-08-11  5:22 ` Eli Zaretskii
2024-08-11  7:44 ` Yuri Khan
2024-08-11  9:06   ` Emanuel Berg
2024-08-11 12:16   ` Emanuel Berg
2024-08-13 13:13     ` buffer segments, uniform and automatic (was: Re: [Elisp: 8 out of 10 problems] I think the last one! (point)) Emanuel Berg
2024-08-13 14:10       ` Ihor Radchenko
2024-08-13 17:10         ` Emanuel Berg [this message]
2024-08-13 18:07           ` Emanuel Berg
2024-08-14 20:02             ` Emanuel Berg
2024-08-15  5:39               ` Emanuel Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87frr8fiwi.fsf@dataswamp.org \
    --to=incal@dataswamp.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.