help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A problem (apparently) connected with window point


From: Marcin Borkowski
Subject: A problem (apparently) connected with window point
Date: Fri, 02 Apr 2021 06:15:14 +0200
User-agent: mu4e 1.1.0; emacs 28.0.50

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]