all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Proposing new function for basic editing
@ 2024-02-06  7:05 Pedro Andres Aranda Gutierrez
  2024-02-06  8:14 ` Philip Kaludercic
  2024-02-06  8:25 ` Adam Porter
  0 siblings, 2 replies; 6+ messages in thread
From: Pedro Andres Aranda Gutierrez @ 2024-02-06  7:05 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1521 bytes --]

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.

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
    (progn
      (insert before)
      (if put-after
          (insert after) ; put point after both strings
        (save-excursion (insert after)))))) ; put point between strings
--- cut here ---

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

Best, /PA

-- 
Fragen sind nicht da, um beantwortet zu werden,
Fragen sind da um gestellt zu werden
Georg Kreisler

Headaches with a Juju log:
unit-basic-16: 09:17:36 WARNING juju.worker.uniter.operation we should run
a leader-deposed hook here, but we can't yet

[-- Attachment #2: Type: text/html, Size: 2139 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Proposing new function for basic editing
  2024-02-06  7:05 Proposing new function for basic editing Pedro Andres Aranda Gutierrez
@ 2024-02-06  8:14 ` Philip Kaludercic
  2024-02-06 10:34   ` Pedro Andres Aranda Gutierrez
  2024-02-06  8:25 ` Adam Porter
  1 sibling, 1 reply; 6+ messages in thread
From: Philip Kaludercic @ 2024-02-06  8:14 UTC (permalink / raw)
  To: Pedro Andres Aranda Gutierrez; +Cc: emacs-devel

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



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Proposing new function for basic editing
  2024-02-06  7:05 Proposing new function for basic editing Pedro Andres Aranda Gutierrez
  2024-02-06  8:14 ` Philip Kaludercic
@ 2024-02-06  8:25 ` Adam Porter
  2024-02-06  9:16   ` Pedro Andres Aranda Gutierrez
  1 sibling, 1 reply; 6+ messages in thread
From: Adam Porter @ 2024-02-06  8:25 UTC (permalink / raw)
  To: paaguti; +Cc: emacs-devel

FWIW, electric-pair-mode seems to handle this.  If the region is active 
and you type a quotation mark, paren, etc, it will be wrapped.  So a 
common key sequence for me is:

C-M-SPC ... "

to wrap a region of text in quotes.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Proposing new function for basic editing
  2024-02-06  8:25 ` Adam Porter
@ 2024-02-06  9:16   ` Pedro Andres Aranda Gutierrez
  2024-02-06 17:29     ` Juri Linkov
  0 siblings, 1 reply; 6+ messages in thread
From: Pedro Andres Aranda Gutierrez @ 2024-02-06  9:16 UTC (permalink / raw)
  To: Adam Porter; +Cc: emacs-devel

I know that, but I normally inset arbitrary strings. I’ll look at electric-pair-mode though

/PA
Enviado desde mi iPhone

> El 6 feb 2024, a las 9:25, Adam Porter <adam@alphapapa.net> escribió:
> 
> FWIW, electric-pair-mode seems to handle this.  If the region is active and you type a quotation mark, paren, etc, it will be wrapped.  So a common key sequence for me is:
> 
> C-M-SPC ... "
> 
> to wrap a region of text in quotes.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Proposing new function for basic editing
  2024-02-06  8:14 ` Philip Kaludercic
@ 2024-02-06 10:34   ` Pedro Andres Aranda Gutierrez
  0 siblings, 0 replies; 6+ messages in thread
From: Pedro Andres Aranda Gutierrez @ 2024-02-06 10:34 UTC (permalink / raw)
  To: Philip Kaludercic; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 3114 bytes --]

Hi Philip,

thanks for the comments. I'll look at wrapping anything approach when time
permits...
Meanwhile, following your comment, I'd propose something like

--- 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"
  ;; insert the first string before the mark or region-beginning
  (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 before))
  ;; Insert the second string after the point or mark
  (if put-after
      (insert after) ; put point after second string
    (save-excursion (insert after)))) ; put point before second string
--- cut here ---

Wouldn't it serve a couple of scenarios?

Best, /PA


On Tue, 6 Feb 2024 at 09:14, Philip Kaludercic <philipk@posteo.net> wrote:

> 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
>


-- 
Fragen sind nicht da, um beantwortet zu werden,
Fragen sind da um gestellt zu werden
Georg Kreisler

Headaches with a Juju log:
unit-basic-16: 09:17:36 WARNING juju.worker.uniter.operation we should run
a leader-deposed hook here, but we can't yet

[-- Attachment #2: Type: text/html, Size: 4367 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Proposing new function for basic editing
  2024-02-06  9:16   ` Pedro Andres Aranda Gutierrez
@ 2024-02-06 17:29     ` Juri Linkov
  0 siblings, 0 replies; 6+ messages in thread
From: Juri Linkov @ 2024-02-06 17:29 UTC (permalink / raw)
  To: Pedro Andres Aranda Gutierrez; +Cc: Adam Porter, emacs-devel

> I’ll look at electric-pair-mode though

Also there is a simpler insert-pair.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-02-06 17:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-06  7:05 Proposing new function for basic editing Pedro Andres Aranda Gutierrez
2024-02-06  8:14 ` Philip Kaludercic
2024-02-06 10:34   ` Pedro Andres Aranda Gutierrez
2024-02-06  8:25 ` Adam Porter
2024-02-06  9:16   ` Pedro Andres Aranda Gutierrez
2024-02-06 17:29     ` Juri Linkov

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.