emacs-devel
[Top][All Lists]
Advanced

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

Re: Proposing new function for basic editing


From: Philip Kaludercic
Subject: Re: Proposing new function for basic editing
Date: Tue, 06 Feb 2024 08:14:28 +0000

Pedro Andres Aranda Gutierrez <paaguti@gmail.com> writes:

> Hi,
> I really don't know if this is useful for more people or it's just me. In
> the elisp I write, I have ended up more than once wrapping the point or the
> active region with two strings.

There are certainly some cases where less generic code like this was
written, e.g. in rcirc we have `rcirc-format'.

> I normally use a yasnippet for this, so I really don't use elisp ;-)
>
> But when I need this for a package, I end up replicating the following
> function where I need it:
>
> --- cut here ---
> (defun surround-region-or-point (before after &optional put-after)
>   "Insert `before' before mark or region,
> `after' after mark or region and
> put mark after `after' if `put-after' is not nil"
>   (if (use-region-p)
>       (with-restriction (region-beginning) (region-end)
>         ;; Insert `before' before region
>         (goto-char (point-min))
>         (insert before)
>         (goto-char (point-max))
>         ;; Insert `after' after region
>         (if put-after
>             (insert after) ; put point after inserted string
>           (save-excursion (insert after)))) ; put point after region

Can't you pull this code out of the if

>     (progn

... and then drop the progn?

>       (insert before)
>       (if put-after
>           (insert after) ; put point after both strings
>         (save-excursion (insert after)))))) ; put point between strings
> --- cut here ---

Also interesting, if you want to be generic, why not use an approach
like `isearch-forward-thing-at-point' and allow wrapping anything that
defines `bounds-of-thing-at-point'?

> I was wondering if it would make sense to have this in the emacs lisp
> library once and for good.
>
> Best, /PA



reply via email to

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