all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Saving "relative point" in a paragraph or line
@ 2013-09-12  8:06 Suvayu Ali
  2013-09-12  8:49 ` Andreas Röhler
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Suvayu Ali @ 2013-09-12  8:06 UTC (permalink / raw)
  To: Emacs help

Hi,

I'm trying to write a few wrappers for transpose functions.  I would
like to restore the point to the relative position of the element I am
trying to transpose.  Are there any standard ways/functions people use
for something like this?

An example (cursor at -!-):

- Lorem ipsum dolor -!- sit amet, consectetur adipiscing elit.
- Vestibulum porttitor metus sed est varius, id dapibus est rhoncus.

                           |
                           v

- Vestibulum porttitor metus sed est varius, id dapibus est rhoncus.
- Lorem ipsum dolor -!- sit amet, consectetur adipiscing elit.

Thanks for any ideas.

Cheers,

-- 
Suvayu

Open source is the future. It sets us free.



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

* Re: Saving "relative point" in a paragraph or line
  2013-09-12  8:06 Saving "relative point" in a paragraph or line Suvayu Ali
@ 2013-09-12  8:49 ` Andreas Röhler
  2013-09-12  9:51   ` Suvayu Ali
  2013-09-12  9:04 ` Jambunathan K
       [not found] ` <mailman.1921.1378976581.10748.help-gnu-emacs@gnu.org>
  2 siblings, 1 reply; 8+ messages in thread
From: Andreas Röhler @ 2013-09-12  8:49 UTC (permalink / raw)
  To: help-gnu-emacs

Am 12.09.2013 10:06, schrieb Suvayu Ali:
> Hi,
>
> I'm trying to write a few wrappers for transpose functions.  I would
> like to restore the point to the relative position of the element I am
> trying to transpose.  Are there any standard ways/functions people use
> for something like this?
>
> An example (cursor at -!-):
>
> - Lorem ipsum dolor -!- sit amet, consectetur adipiscing elit.
> - Vestibulum porttitor metus sed est varius, id dapibus est rhoncus.
>
>                             |
>                             v
>
> - Vestibulum porttitor metus sed est varius, id dapibus est rhoncus.
> - Lorem ipsum dolor -!- sit amet, consectetur adipiscing elit.
>
> Thanks for any ideas.
>
> Cheers,
>

Not sure IIUC, what about storing location with

(setq my-point (copy-marker (point)))

Andreas



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

* Re: Saving "relative point" in a paragraph or line
  2013-09-12  8:06 Saving "relative point" in a paragraph or line Suvayu Ali
  2013-09-12  8:49 ` Andreas Röhler
@ 2013-09-12  9:04 ` Jambunathan K
  2013-09-12 13:31   ` Suvayu Ali
       [not found] ` <mailman.1921.1378976581.10748.help-gnu-emacs@gnu.org>
  2 siblings, 1 reply; 8+ messages in thread
From: Jambunathan K @ 2013-09-12  9:04 UTC (permalink / raw)
  To: Suvayu Ali; +Cc: Emacs help

Suvayu Ali <fatkasuvayu+linux@gmail.com> writes:

> Hi,
>
> I'm trying to write a few wrappers for transpose functions.  I would
> like to restore the point to the relative position of the element I am
> trying to transpose.  Are there any standard ways/functions people use
> for something like this?
>
> An example (cursor at -!-):
>
> - Lorem ipsum dolor -!- sit amet, consectetur adipiscing elit.
> - Vestibulum porttitor metus sed est varius, id dapibus est rhoncus.
>
>                            |
>                            v
>
> - Vestibulum porttitor metus sed est varius, id dapibus est rhoncus.
> - Lorem ipsum dolor -!- sit amet, consectetur adipiscing elit.

This snippet comes with Zero warranty but illustrates how one may
achieve it.

    (defadvice transpose-lines
        (around transpose-lines-preserve-context activate)
      "Transpose lines but preserve the surrounding text context."
      ;; Add a bookmark at current char.
      (add-text-properties (point) (1+ (point)) '(bookmark t))
      ;; Transpose.
      ad-do-it
      ;; Visit the bookmark.  Assumes that the bookmark is at a position
      ;; behind where the cursor is at the end of the transposition.
      (goto-char (1- (previous-single-property-change (point) 'bookmark)))
      ;; Remove it.
      (remove-text-properties (point) (1+ (point)) '(bookmark)))

>
> Thanks for any ideas.
>
> Cheers,



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

* Re: Saving "relative point" in a paragraph or line
       [not found] ` <mailman.1921.1378976581.10748.help-gnu-emacs@gnu.org>
@ 2013-09-12  9:14   ` Damien Wyart
  0 siblings, 0 replies; 8+ messages in thread
From: Damien Wyart @ 2013-09-12  9:14 UTC (permalink / raw)
  To: help-gnu-emacs

* Jambunathan K <kjambunathan@gmail.com> in gnu.emacs.help:
>     (defadvice transpose-lines
>         (around transpose-lines-preserve-context activate)
>       "Transpose lines but preserve the surrounding text context."
>       ;; Add a bookmark at current char.
>       (add-text-properties (point) (1+ (point)) '(bookmark t))
>       ;; Transpose.
>       ad-do-it
>       ;; Visit the bookmark.  Assumes that the bookmark is at a position
>       ;; behind where the cursor is at the end of the transposition.
>       (goto-char (1- (previous-single-property-change (point) 'bookmark)))
>       ;; Remove it.
>       (remove-text-properties (point) (1+ (point)) '(bookmark)))

I was thinking of doing it with saving current-column, transposing,
forward-line to go back 2 lines, and move-to-column, but your approach
looks more elegant.

-- 
DW


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

* Re: Saving "relative point" in a paragraph or line
  2013-09-12  8:49 ` Andreas Röhler
@ 2013-09-12  9:51   ` Suvayu Ali
  2013-09-12 10:06     ` Andreas Röhler
  0 siblings, 1 reply; 8+ messages in thread
From: Suvayu Ali @ 2013-09-12  9:51 UTC (permalink / raw)
  To: help-gnu-emacs

On Thu, Sep 12, 2013 at 10:49:37AM +0200, Andreas Röhler wrote:
> Am 12.09.2013 10:06, schrieb Suvayu Ali:
> >Hi,
> >
> >I'm trying to write a few wrappers for transpose functions.  I would
> >like to restore the point to the relative position of the element I am
> >trying to transpose.  Are there any standard ways/functions people use
> >for something like this?
> >
> >An example (cursor at -!-):
> >
> >- Lorem ipsum dolor -!- sit amet, consectetur adipiscing elit.
> >- Vestibulum porttitor metus sed est varius, id dapibus est rhoncus.
> >
> >                            |
> >                            v
> >
> >- Vestibulum porttitor metus sed est varius, id dapibus est rhoncus.
> >- Lorem ipsum dolor -!- sit amet, consectetur adipiscing elit.
> >
> >Thanks for any ideas.
> >
> >Cheers,
> >
> 
> Not sure IIUC, what about storing location with
> 
> (setq my-point (copy-marker (point)))

After the transpose my-point will be somewhere inside the other line.
Hence my use of the terminology "relative point".

:)

-- 
Suvayu

Open source is the future. It sets us free.



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

* Re: Saving "relative point" in a paragraph or line
  2013-09-12  9:51   ` Suvayu Ali
@ 2013-09-12 10:06     ` Andreas Röhler
  2013-09-12 11:26       ` Suvayu Ali
  0 siblings, 1 reply; 8+ messages in thread
From: Andreas Röhler @ 2013-09-12 10:06 UTC (permalink / raw)
  To: help-gnu-emacs

Am 12.09.2013 11:51, schrieb Suvayu Ali:
> On Thu, Sep 12, 2013 at 10:49:37AM +0200, Andreas Röhler wrote:
>> Am 12.09.2013 10:06, schrieb Suvayu Ali:
>>> Hi,
>>>
>>> I'm trying to write a few wrappers for transpose functions.  I would
>>> like to restore the point to the relative position of the element I am
>>> trying to transpose.  Are there any standard ways/functions people use
>>> for something like this?
>>>
>>> An example (cursor at -!-):
>>>
>>> - Lorem ipsum dolor -!- sit amet, consectetur adipiscing elit.
>>> - Vestibulum porttitor metus sed est varius, id dapibus est rhoncus.
>>>
>>>                             |
>>>                             v
>>>
>>> - Vestibulum porttitor metus sed est varius, id dapibus est rhoncus.
>>> - Lorem ipsum dolor -!- sit amet, consectetur adipiscing elit.
>>>
>>> Thanks for any ideas.
>>>
>>> Cheers,
>>>
>>
>> Not sure IIUC, what about storing location with
>>
>> (setq my-point (copy-marker (point)))
>
> After the transpose my-point will be somewhere inside the other line.
> Hence my use of the terminology "relative point".
>
> :)
>

Hmm, you example shows point after "dolor" as in start. That's what copy-marker would provide.
Maybe move the point indicated in example after change, i.e. end-of "porttitor"?

Cheers



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

* Re: Saving "relative point" in a paragraph or line
  2013-09-12 10:06     ` Andreas Röhler
@ 2013-09-12 11:26       ` Suvayu Ali
  0 siblings, 0 replies; 8+ messages in thread
From: Suvayu Ali @ 2013-09-12 11:26 UTC (permalink / raw)
  To: help-gnu-emacs

On Thu, Sep 12, 2013 at 12:06:55PM +0200, Andreas Röhler wrote:
> Am 12.09.2013 11:51, schrieb Suvayu Ali:
> >On Thu, Sep 12, 2013 at 10:49:37AM +0200, Andreas Röhler wrote:
> >>Am 12.09.2013 10:06, schrieb Suvayu Ali:
> >>>Hi,
> >>>
> >>>I'm trying to write a few wrappers for transpose functions.  I would
> >>>like to restore the point to the relative position of the element I am
> >>>trying to transpose.  Are there any standard ways/functions people use
> >>>for something like this?
> >>>
> >>>An example (cursor at -!-):
> >>>
> >>>- Lorem ipsum dolor -!- sit amet, consectetur adipiscing elit.
> >>>- Vestibulum porttitor metus sed est varius, id dapibus est rhoncus.
> >>>
> >>>                            |
> >>>                            v
> >>>
> >>>- Vestibulum porttitor metus sed est varius, id dapibus est rhoncus.
> >>>- Lorem ipsum dolor -!- sit amet, consectetur adipiscing elit.
> >>
> >>(setq my-point (copy-marker (point)))
> >
> >After the transpose my-point will be somewhere inside the other line.
> >Hence my use of the terminology "relative point".
> 
> Hmm, you example shows point after "dolor" as in start. That's what copy-marker would provide.
> Maybe move the point indicated in example after change, i.e. end-of "porttitor"?

Ah, I think I misunderstood what copy-marker does.  Thank you.  I'll
test both your and Jambu's suggestion later in the day.

Cheers,

-- 
Suvayu

Open source is the future. It sets us free.



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

* Re: Saving "relative point" in a paragraph or line
  2013-09-12  9:04 ` Jambunathan K
@ 2013-09-12 13:31   ` Suvayu Ali
  0 siblings, 0 replies; 8+ messages in thread
From: Suvayu Ali @ 2013-09-12 13:31 UTC (permalink / raw)
  To: Jambunathan K; +Cc: Emacs help

On Thu, Sep 12, 2013 at 02:34:56PM +0530, Jambunathan K wrote:
> 
>     (defadvice transpose-lines
>         (around transpose-lines-preserve-context activate)
>       "Transpose lines but preserve the surrounding text context."
>       ;; Add a bookmark at current char.
>       (add-text-properties (point) (1+ (point)) '(bookmark t))
>       ;; Transpose.
>       ad-do-it
>       ;; Visit the bookmark.  Assumes that the bookmark is at a position
>       ;; behind where the cursor is at the end of the transposition.
>       (goto-char (1- (previous-single-property-change (point) 'bookmark)))
>       ;; Remove it.
>       (remove-text-properties (point) (1+ (point)) '(bookmark)))

I adapted this to my needs; works great!

Thank you :)

-- 
Suvayu

Open source is the future. It sets us free.



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

end of thread, other threads:[~2013-09-12 13:31 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-09-12  8:06 Saving "relative point" in a paragraph or line Suvayu Ali
2013-09-12  8:49 ` Andreas Röhler
2013-09-12  9:51   ` Suvayu Ali
2013-09-12 10:06     ` Andreas Röhler
2013-09-12 11:26       ` Suvayu Ali
2013-09-12  9:04 ` Jambunathan K
2013-09-12 13:31   ` Suvayu Ali
     [not found] ` <mailman.1921.1378976581.10748.help-gnu-emacs@gnu.org>
2013-09-12  9:14   ` Damien Wyart

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.