[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [GNU ELPA] I'd like to add switchy.el: a last-recently-used window s
From: |
Tassilo Horn |
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 |
User-agent: |
mu4e 1.11.0; emacs 30.0.50 |
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