all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* A problem (apparently) connected with window point
@ 2021-04-02  4:15 Marcin Borkowski
  2021-04-02  5:54 ` Eli Zaretskii
  2021-04-02 12:13 ` Stefan Monnier
  0 siblings, 2 replies; 5+ messages in thread
From: Marcin Borkowski @ 2021-04-02  4:15 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hello everyone,

I'm writing a small utility to solve a problem of reordering a sentence
(more general than `transpose-words').  I encountered a problem - which
is kind of embarassing, since my goal with that utility is to /teach
Elisp/, and now I'm seeking help myself;-).  (In case anyone wonders -
after a few years I came back to my idea of an intermediate book about
Elisp.)  But hey, we all learn all the time (and btw, teaching something
is often a great way to learn it oneself).

Here's my code (not very useful at this point (pun intended) yet):

--8<---------------cut here---------------start------------->8---
(defvar-local reorder-sentence--begin nil
  "The beginning of the sentence to be reordered.")

(defvar-local reorder-sentence--end nil
  "The end of the sentence to be reordered.")

(defvar-local reorder-sentence--buffer nil
  "The buffer used to construct a reordered sentence.")

(defun reorder-sentence (beg end)
  "Reorder the words in the active region."
  (interactive "*r")
  (setq reorder-sentence--begin beg
       reorder-sentence--end end
       reorder-sentence--buffer
       (or reorder-sentence--buffer
           (generate-new-buffer
            (format "*Reorder sentence %s*"
     	       (buffer-name)))))
  (display-buffer reorder-sentence--buffer)
  (setq deactivate-mark t)
  (with-current-buffer reorder-sentence--buffer
    (erase-buffer)))

(defun reorder-sentence-copy-word-at-point ()
  "Copy the word at point to the sentence reordering buffer."
  (interactive)
  (let ((word (current-word)))
    (with-current-buffer reorder-sentence--buffer
     (when (and (not (bobp))
     	   (save-excursion (zerop (skip-syntax-backward "-"))))
       (insert " "))
     (insert word))))
--8<---------------cut here---------------end--------------->8---

The idea is that I select a region, call `reorder-sentence' (which then
prepares a buffer to construct the reordered sentence), and then call
`reorder-sentence-copy-word-at-point' with the point at words in the
order I want them in the reordered sentence.  (This function does not
deal with a convenient UI, punctuation, capitalization, and - last but
not least - putting the reordered sentence back where the original
sentence was.  All that will come later.)

The problem is that `reorder-sentence-copy-word-at-point' inserts every
word at the beginning of the buffer.  (Try it, following the steps
above.)  I very much suspect that this is because of the window point:
if I do not /display/ that buffer, it works correctly (i.e., puts
subsequent words one after another), and if I change the code to use
`with-selected-window' instead of `with-current-buffer', it also works
correctly.

Now I tried to add `(window-point-insertion-type t)' to the `let'
clause, but to no avail.  I studied the relevant section of the manual
a few times:
(info "(elisp) Window Point")
but found nothing of relevance: while the code above /displays/ a window
with the "temporary" buffer, it never /selects/ it.

I would prefer to avoid changing the code above to use
`with-selected-window', for two reasons.

1. Having `reorder-sentence--buffer' buffer-local enables the user to
use this feature independently (simultaneously, even) in more than one
buffer.  I am not sure how a corresponding `reorder-sentence--window'
would behave in that respect (though it might work).

2. I can imagine a power user not needing/wanting to display the buffer
where the reordered sentence is constructed (maybe enabling such
behavior via a prefix argument to `reorder-sentence'), so using
a /buffer/ and not necessarily a /window/ seems more general.  (IOW,
actually /displaying/ that buffer seems not to be an essential
feature, and yet it changes the behavior).

Any ideas why the code above doesn't work, and how to fix it in an
elegant way?

TIA,

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: A problem (apparently) connected with window point
  2021-04-02  4:15 A problem (apparently) connected with window point Marcin Borkowski
@ 2021-04-02  5:54 ` Eli Zaretskii
  2021-04-02 12:13 ` Stefan Monnier
  1 sibling, 0 replies; 5+ messages in thread
From: Eli Zaretskii @ 2021-04-02  5:54 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Marcin Borkowski <mbork@mbork.pl>
> Date: Fri, 02 Apr 2021 06:15:14 +0200
> 
> The idea is that I select a region, call `reorder-sentence' (which then
> prepares a buffer to construct the reordered sentence), and then call
> `reorder-sentence-copy-word-at-point' with the point at words in the
> order I want them in the reordered sentence.

I'm probably missing something, because the description above doesn't
seem to be covered by the code you presented.  Specifically, the "with
the point at words in the order I want them in the reordered sentence"
part: where is it?  Since position of point is the main issue here, I
think showing that is important.

> The problem is that `reorder-sentence-copy-word-at-point' inserts every
> word at the beginning of the buffer.  (Try it, following the steps
> above.)  I very much suspect that this is because of the window point:
> if I do not /display/ that buffer, it works correctly (i.e., puts
> subsequent words one after another), and if I change the code to use
> `with-selected-window' instead of `with-current-buffer', it also works
> correctly.

A stub in the dark: did you try setting
switch-to-buffer-preserve-window-point to the nil value?



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

* Re: A problem (apparently) connected with window point
  2021-04-02  4:15 A problem (apparently) connected with window point Marcin Borkowski
  2021-04-02  5:54 ` Eli Zaretskii
@ 2021-04-02 12:13 ` Stefan Monnier
  2021-04-03  3:25   ` Marcin Borkowski
  1 sibling, 1 reply; 5+ messages in thread
From: Stefan Monnier @ 2021-04-02 12:13 UTC (permalink / raw)
  To: help-gnu-emacs

> The problem is that `reorder-sentence-copy-word-at-point' inserts every
> word at the beginning of the buffer.  (Try it, following the steps
> above.)  I very much suspect that this is because of the window point:

That's right.  To avoid the problem you have to understand that
`(with-current-buffer reorder-sentence--buffer ...)` will select the
right buffer but will place point at a location that depends on what was
the last use of that buffer (and redisplay *is* a use of that buffer,
which is why having the buffer displayed makes a difference).
In your case I think you have two options:

1- Take it for granted that the buffer is displayed and make
   `reorder-sentence-copy-word-at-point` move the corresponding
   window-point (either by selecting that window around the `insert` or
   by explicitly using `set-window-point`) and use that window-point as
   the insertion point (so the user can move the cursor in that window
   in order to insert word either at the end, beginning or elsewhere in
   the temp buffer).

2- Use your own notion of "point", e.g. by calling (goto-char <my-point>)
   before the `insert` and updating <my-point> as you see fit.
   In your example, I suspect using (point-max) for <my-point> might do
   the trick.


-- Stefan




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

* Re: A problem (apparently) connected with window point
  2021-04-02 12:13 ` Stefan Monnier
@ 2021-04-03  3:25   ` Marcin Borkowski
  2021-04-03 15:22     ` Stefan Monnier
  0 siblings, 1 reply; 5+ messages in thread
From: Marcin Borkowski @ 2021-04-03  3:25 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


On 2021-04-02, at 14:13, Stefan Monnier <monnier@iro.umontreal.ca> wrote:

>> The problem is that `reorder-sentence-copy-word-at-point' inserts every
>> word at the beginning of the buffer.  (Try it, following the steps
>> above.)  I very much suspect that this is because of the window point:
>
> That's right.  To avoid the problem you have to understand that
> `(with-current-buffer reorder-sentence--buffer ...)` will select the
> right buffer but will place point at a location that depends on what was
> the last use of that buffer (and redisplay *is* a use of that buffer,
> which is why having the buffer displayed makes a difference).
> In your case I think you have two options:

Thanks!  I have to admit that I still don't get _why_ this is
happening.  Why would `with-current-buffer' do that -- it just uses
`set-buffer' and `save-current-buffer' under the hood, and their
docstrings don't mention anything like that.  Isn't it a bug in the
docs, then?

> 1- Take it for granted that the buffer is displayed and make
>    `reorder-sentence-copy-word-at-point` move the corresponding
>    window-point (either by selecting that window around the `insert` or
>    by explicitly using `set-window-point`) and use that window-point as
>    the insertion point (so the user can move the cursor in that window
>    in order to insert word either at the end, beginning or elsewhere in
>    the temp buffer).
>
> 2- Use your own notion of "point", e.g. by calling (goto-char <my-point>)
>    before the `insert` and updating <my-point> as you see fit.
>    In your example, I suspect using (point-max) for <my-point> might do
>    the trick.

I think both vaiants make sense - 1 is slightly better (and I guess
doable via `get-buffer-window'), 2 is slightly simpler (and should cover
99.99% of the needs anyway).

Thanks,

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: A problem (apparently) connected with window point
  2021-04-03  3:25   ` Marcin Borkowski
@ 2021-04-03 15:22     ` Stefan Monnier
  0 siblings, 0 replies; 5+ messages in thread
From: Stefan Monnier @ 2021-04-03 15:22 UTC (permalink / raw)
  To: help-gnu-emacs

>> That's right.  To avoid the problem you have to understand that
>> `(with-current-buffer reorder-sentence--buffer ...)` will select the
>> right buffer but will place point at a location that depends on what was
>> the last use of that buffer (and redisplay *is* a use of that buffer,
>> which is why having the buffer displayed makes a difference).
>> In your case I think you have two options:
>
> Thanks!  I have to admit that I still don't get _why_ this is
> happening.

Where do you expect `with-current-buffer` (or `set-buffer` for that
matter) to put the position of `point`?  Remember that between execution
of two commands, there can be a *lot* of other code that will be running
(via redisplay, jit-lock, post/pre-command-hook, you name it).

So while you want to go back to where *you* were, Emacs doesn't really
know what you mean by that.


        Stefan




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

end of thread, other threads:[~2021-04-03 15:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-04-02  4:15 A problem (apparently) connected with window point Marcin Borkowski
2021-04-02  5:54 ` Eli Zaretskii
2021-04-02 12:13 ` Stefan Monnier
2021-04-03  3:25   ` Marcin Borkowski
2021-04-03 15:22     ` Stefan Monnier

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.