all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Faking an active region
@ 2011-09-03 16:09 Deniz Dogan
  2011-09-03 16:19 ` Eli Zaretskii
  2011-09-03 16:40 ` Drew Adams
  0 siblings, 2 replies; 4+ messages in thread
From: Deniz Dogan @ 2011-09-03 16:09 UTC (permalink / raw)
  To: help-gnu-emacs

I am writing a minor mode in which I want to remap `undo' to ALWAYS act 
as if a specific region was active and transient-mark-mode was on.

So how would I go about "faking" this active region in Emacs Lisp?  This 
is what I have so far:

(define-key map [remap undo] 'nima-undo-undo)

(defun nima-undo-undo ()
   (interactive)
   (let ((transient-mark-mode t))
     (push-mark nima-prompt-end t t) ;; a marker
     (goto-char (point-max))
     (undo)))

This doesn't work, and I'm not surprised because I feel like I'm just 
throwing everything I can find at this problem right now.

Thanks in advance,
Deniz



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

* Re: Faking an active region
  2011-09-03 16:09 Faking an active region Deniz Dogan
@ 2011-09-03 16:19 ` Eli Zaretskii
  2011-09-03 16:40 ` Drew Adams
  1 sibling, 0 replies; 4+ messages in thread
From: Eli Zaretskii @ 2011-09-03 16:19 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Sat, 03 Sep 2011 18:09:46 +0200
> From: Deniz Dogan <deniz@dogan.se>
> 
> So how would I go about "faking" this active region in Emacs Lisp?  This 
> is what I have so far:
> 
> (define-key map [remap undo] 'nima-undo-undo)
> 
> (defun nima-undo-undo ()
>    (interactive)
>    (let ((transient-mark-mode t))
>      (push-mark nima-prompt-end t t) ;; a marker
>      (goto-char (point-max))
>      (undo)))
> 
> This doesn't work, and I'm not surprised because I feel like I'm just 
> throwing everything I can find at this problem right now.

See region-active-p and push-mark.



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

* RE: Faking an active region
  2011-09-03 16:09 Faking an active region Deniz Dogan
  2011-09-03 16:19 ` Eli Zaretskii
@ 2011-09-03 16:40 ` Drew Adams
  2011-09-03 17:30   ` Deniz Dogan
  1 sibling, 1 reply; 4+ messages in thread
From: Drew Adams @ 2011-09-03 16:40 UTC (permalink / raw)
  To: 'Deniz Dogan', help-gnu-emacs

> I am writing a minor mode in which I want to remap
> `undo' to ALWAYS act as if a specific region was active
> and transient-mark-mode was on.

Your mention of a "specific" region and your code attempt suggest that it is
always the same region, or at least that the region start is always the same
(the end is always eob, apparently).

> So how would I go about "faking" this active region in Emacs 
> Lisp?

Eli> See region-active-p and push-mark.

I doubt that will help much.

This is I think something like what Deniz requested:

(defun reg-undo ()
  "..."
  (interactive)
  (save-excursion
    (save-restriction
      (narrow-to-region nima-prompt-end (point-max))
      (setq this-command  'undo)
      (condition-case nil (undo) (error nil)))))

You must set `this-command' to `undo'.

To work on a region, which might not be active, just use `narrow-to-region' (and
`save-restriction').  A `save-excursion' seems to be needed at least for the
case where changes (which won't be undone) were made outside the region.
Likewise, the `condition-case' (or `ignore-errors', if you prefer).

You might need to tweak this a bit - test with various scenarios (redo etc.).




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

* Re: Faking an active region
  2011-09-03 16:40 ` Drew Adams
@ 2011-09-03 17:30   ` Deniz Dogan
  0 siblings, 0 replies; 4+ messages in thread
From: Deniz Dogan @ 2011-09-03 17:30 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

On 2011-09-03 18:40, Drew Adams wrote:
>> I am writing a minor mode in which I want to remap
>> `undo' to ALWAYS act as if a specific region was active
>> and transient-mark-mode was on.
>
> Your mention of a "specific" region and your code attempt suggest that it is
> always the same region, or at least that the region start is always the same
> (the end is always eob, apparently).
>
>> So how would I go about "faking" this active region in Emacs
>> Lisp?
>
> Eli>  See region-active-p and push-mark.
>
> I doubt that will help much.
>
> This is I think something like what Deniz requested:
>
> (defun reg-undo ()
>    "..."
>    (interactive)
>    (save-excursion
>      (save-restriction
>        (narrow-to-region nima-prompt-end (point-max))
>        (setq this-command  'undo)
>        (condition-case nil (undo) (error nil)))))
>
> You must set `this-command' to `undo'.
>
> To work on a region, which might not be active, just use `narrow-to-region' (and
> `save-restriction').  A `save-excursion' seems to be needed at least for the
> case where changes (which won't be undone) were made outside the region.
> Likewise, the `condition-case' (or `ignore-errors', if you prefer).
>
> You might need to tweak this a bit - test with various scenarios (redo etc.).
>

Thanks a lot, that does _almost_ what I had in mind.  It seems to get 
stuck in some sort of infinite undo/redo loop though -- it never says 
"No further undo information".

Here is the real scenario:

I'm writing an IRC client where nima-prompt-end is the marker at the end 
of the input prompt.  When the user wants to "undo", I don't want any 
changes in the chat buffer to be undone (as they only represent a visual 
"history" that cannot be undone).

rcirc accomplishes this by disabling and then immediately enabling undo 
information for the buffer (if I understand it correctly).  However, I 
believe if only I could "fake" an active region appropriately, this 
wouldn't be necessary.



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

end of thread, other threads:[~2011-09-03 17:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-09-03 16:09 Faking an active region Deniz Dogan
2011-09-03 16:19 ` Eli Zaretskii
2011-09-03 16:40 ` Drew Adams
2011-09-03 17:30   ` Deniz Dogan

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.