From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.help Subject: A problem (apparently) connected with window point Date: Fri, 02 Apr 2021 06:15:14 +0200 Message-ID: <875z159val.fsf@mbork.pl> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="30119"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.1.0; emacs 28.0.50 To: Help Gnu Emacs mailing list Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Fri Apr 02 06:16:07 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lSBE7-0007lA-Kg for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 02 Apr 2021 06:16:07 +0200 Original-Received: from localhost ([::1]:49100 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lSBE6-00073t-Ir for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 02 Apr 2021 00:16:06 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:57246) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lSBDW-00072Y-P4 for help-gnu-emacs@gnu.org; Fri, 02 Apr 2021 00:15:30 -0400 Original-Received: from mail.mojserwer.eu ([195.110.48.8]:45438) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lSBDS-0002GG-2M for help-gnu-emacs@gnu.org; Fri, 02 Apr 2021 00:15:30 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id E727FE66FC for ; Fri, 2 Apr 2021 06:15:22 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mail.mojserwer.eu Original-Received: from mail.mojserwer.eu ([127.0.0.1]) by localhost (mail.mojserwer.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 9fNdirRtOuWB for ; Fri, 2 Apr 2021 06:15:18 +0200 (CEST) Original-Received: from localhost (178235147175.dynamic-3-poz-k-0-1-0.vectranet.pl [178.235.147.175]) by mail.mojserwer.eu (Postfix) with ESMTPSA id 5543DE6250 for ; Fri, 2 Apr 2021 06:15:18 +0200 (CEST) Received-SPF: pass client-ip=195.110.48.8; envelope-from=mbork@mbork.pl; helo=mail.mojserwer.eu X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:128801 Archived-At: 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