From: Tassilo Horn <tsdh@gnu.org>
Cc: Philip Kaludercic <philipk@posteo.net>, emacs-devel@gnu.org
Subject: Re: [GNU ELPA] I'd like to add switchy.el: a last-recently-used window switcher
Date: Sun, 09 Apr 2023 13:14:31 +0200 [thread overview]
Message-ID: <87cz4dmh7w.fsf@gnu.org> (raw)
In-Reply-To: <b0318f0e-a83e-4151-ba23-156365974f9f@gnu.org>
Tassilo Horn <tsdh@gnu.org> writes:
Hi Philip,
I've added the package to ELPA as switchy-window as suggested by Eli.
>>> ;; Package-Requires: ((emacs "25.1") (compat "29.1.3.4"))
>>
>> If you are to use compat 29.1.0.0 or newer then you also have to
>> require it!
Ah, thanks. I'm new to the compat game but it's a relief not to worry
about compatibility. :-)
>>> ;; SPDX-License-Identifier: GPL-3.0-or-later
>>> ;;
>>> ;; This file is NOT part of GNU Emacs.
>>
>> If added to GNU ELPA, this should be change to "... is part of GNU
>> Emacs", right?
Yes, I've changed that now.
>>> ;; Remove dead windows.
>>> (setq switchy--tick-alist (seq-filter
>>> (lambda (e)
>>> (window-live-p (car e)))
>>> switchy--tick-alist))
>>> ;; Add windows never selected.
>>> (dolist (win (window-list (selected-frame)))
>>> (unless (assq win switchy--tick-alist)
>>> (setf (alist-get win switchy--tick-alist) 0)))
>>
>> The setf is strictly speaking unnecessary here and causes an
>> accidental O(n^2) slowdown, since you traverse the list once to check
>> if it has an entry and then traverse it again to check if you can set
>> 0 to an existing entry. You could also just push a cons-cell to the
>> beginning. Then again, this is all bounded by the maximal number of
>> windows that someone has open so it doesn't matter in practice.
Yup, I've kept that as-is because the theoretical slowdown is not
practical and it's easier to debug when the alist ist free of
duplicates.
>>> (let ((win-entries (seq-filter
>>> (lambda (e)
>>> (let ((win (car e)))
>>> (and (eq (window-frame win) (selected-frame))
>>> (or (minibuffer-window-active-p win)
>>> (not (eq win (minibuffer-window
>>> (selected-frame)))))
>>> (not (memq win switchy--visited-windows)))))
>>> switchy--tick-alist)))
>>> (if win-entries
>>> (when-let ((win (car (seq-reduce (lambda (x e)
>>> (if (and x (funcall (if arg #'<
>>> #'>)
>>> (cdr x) (cdr
>>> e)))
>>> x
>>> e))
>>> win-entries nil))))
>>>
>>> (progn
>>
>> Why the progn if you are using when-let?
A left-over of an earlier version.
>>> ;; Start a new cycle if we're not at the start already, i.e., we
>>> visited
>>> ;; just one (the current) window.
>>> (when (> (length switchy--visited-windows) 1)
>>
>> Or (length> switchy--visited-windows 1)?
Oh, I didn't know that.
>>> (defvar switchy-minor-mode-map (make-sparse-keymap)
>>> "The mode map of `switchy-minor-mode'.
>>> No keys are bound by default. Bind the main command
>>> `switchy-window' to a key of your liking, e.g.,
>>>
>>> ;; That\\='s what I use.
>>> (keymap-set switchy-minor-mode-map \"C-<\" #\\='switchy-window)
>>
>> If you are already making use of keymap-set, you might as well define
>> the map itself using defvar-keymap (Compat provides it).
Yes, thanks!
>>> (define-minor-mode switchy-minor-mode
>>> "Activates recording of window selection ticks.
>>> Those are the timestamps for figuring out the last-recently-used
>>> order of windows.
>>>
>>> The minor-mode provides the keymap `switchy-minor-mode-map',
>>> which see."
>>> :global t
>>> :keymap switchy-minor-mode-map
>>
>> Isn't this the default anyway?
Yes, it is. Removed.
>>> (provide 'switchy)
>>>
>>> (provide 'switchy)
>>
>> Accidentally duplicated?
Just to be on the safe side it is really provided! ;-)
Thanks,
Tassilo
prev parent reply other threads:[~2023-04-09 11:14 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-09 7:36 [GNU ELPA] I'd like to add switchy.el: a last-recently-used window switcher Tassilo Horn
2023-04-09 8:08 ` Eli Zaretskii
2023-04-09 8:56 ` Philip Kaludercic
2023-04-09 9:33 ` Tassilo Horn
2023-04-09 10:30 ` Philip Kaludercic
2023-04-09 11:14 ` Tassilo Horn [this message]
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87cz4dmh7w.fsf@gnu.org \
--to=tsdh@gnu.org \
--cc=emacs-devel@gnu.org \
--cc=philipk@posteo.net \
/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.
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.