From: Marcin Borkowski <mbork@mbork.pl>
To: Help Gnu Emacs mailing list <help-gnu-emacs@gnu.org>
Subject: A problem (apparently) connected with window point
Date: Fri, 02 Apr 2021 06:15:14 +0200 [thread overview]
Message-ID: <875z159val.fsf@mbork.pl> (raw)
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
next reply other threads:[~2021-04-02 4:15 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-02 4:15 Marcin Borkowski [this message]
2021-04-02 5:54 ` A problem (apparently) connected with window point Eli Zaretskii
2021-04-02 12:13 ` Stefan Monnier
2021-04-03 3:25 ` Marcin Borkowski
2021-04-03 15:22 ` Stefan Monnier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=875z159val.fsf@mbork.pl \
--to=mbork@mbork.pl \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).