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

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.