2016-12-17 06:58:52 -05:00
|
|
|
;;; winum.el --- Navigate windows and frames using numbers.
|
2016-12-02 19:59:13 -05:00
|
|
|
;;
|
|
|
|
;; Copyright (c) 2006-2015 Nikolaj Schumacher
|
2024-08-26 19:22:29 -04:00
|
|
|
;; Copyright (c) 2016-2019 Thomas Chauvot de Beauchêne
|
|
|
|
;; Copyright (c) 2024 Kiana Sheibani
|
2016-12-02 19:59:13 -05:00
|
|
|
;;
|
|
|
|
;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
;;
|
2024-08-26 19:22:29 -04:00
|
|
|
;; Author: Kiana Sheibani <kiana.a.sheibani@gmail.com>
|
|
|
|
;; Version: 3.0.0
|
2016-12-09 10:19:38 -05:00
|
|
|
;; Keywords: convenience, frames, windows, multi-screen
|
2016-12-02 19:59:13 -05:00
|
|
|
;; URL: http://github.com/deb0ch/winum.el
|
|
|
|
;; Created: 2016
|
|
|
|
;; Compatibility: GNU Emacs 24.x
|
2017-05-28 08:47:33 -04:00
|
|
|
;; Package-requires: ((cl-lib "0.5") (dash "2.13.0"))
|
2016-12-02 19:59:13 -05:00
|
|
|
;;
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
;;
|
|
|
|
;;; Commentary:
|
|
|
|
;;
|
|
|
|
;; Window numbers for Emacs: Navigate your windows and frames using numbers.
|
|
|
|
;;
|
|
|
|
;; This package is an extended and actively maintained version of the
|
|
|
|
;; https://github.com/nschum/window-numbering.el package by Nikolaj Schumacher,
|
|
|
|
;; with some ideas and code taken from https://github.com/abo-abo/ace-window.
|
|
|
|
;;
|
2016-12-17 06:58:52 -05:00
|
|
|
;; This version brings, among other things, support for number sets across multiple
|
|
|
|
;; frames, giving the user a smoother experience of multi-screen Emacs.
|
|
|
|
;;
|
2016-12-02 19:59:13 -05:00
|
|
|
;;; Code:
|
|
|
|
|
2016-12-10 14:16:45 -05:00
|
|
|
(eval-when-compile (require 'cl-lib))
|
2017-05-28 08:47:33 -04:00
|
|
|
(require 'dash)
|
2016-12-10 14:16:45 -05:00
|
|
|
|
2016-12-15 10:50:32 -05:00
|
|
|
;; Configuration variables -----------------------------------------------------
|
|
|
|
|
2016-12-02 19:59:13 -05:00
|
|
|
(defgroup winum nil
|
|
|
|
"Navigate and manage windows using numbers."
|
|
|
|
:group 'convenience)
|
|
|
|
|
|
|
|
(defcustom winum-scope 'global
|
|
|
|
"Frames affected by a number set."
|
|
|
|
:group 'winum
|
|
|
|
:type '(choice
|
|
|
|
(const :tag "frame local" frame-local)
|
|
|
|
(const :tag "visible frames" visible)
|
|
|
|
(const :tag "global" global)))
|
|
|
|
|
|
|
|
(defcustom winum-reverse-frame-list nil
|
|
|
|
"If t, order frames by reverse order of creation.
|
|
|
|
Has effect only when `winum-scope' is not 'frame-local."
|
|
|
|
:group 'winum
|
|
|
|
:type 'boolean)
|
|
|
|
|
2024-08-26 19:04:33 -04:00
|
|
|
(defcustom winum-minibuffer-auto-assign 0
|
|
|
|
"If non-nil, `winum-mode' automatically assigns this index to the minibuffer."
|
2016-12-02 19:59:13 -05:00
|
|
|
:group 'winum
|
2024-08-26 19:04:33 -04:00
|
|
|
:type 'sexp)
|
2016-12-02 19:59:13 -05:00
|
|
|
|
|
|
|
(defcustom winum-assign-func nil
|
|
|
|
"Function called for each window by `winum-mode'.
|
|
|
|
This is called before automatic assignment begins. The function should
|
2024-08-26 19:04:33 -04:00
|
|
|
return an index to have it assigned to the current-window, nil otherwise.
|
2016-12-17 07:59:18 -05:00
|
|
|
|
|
|
|
Example: always assign *Calculator* the number 9 and *NeoTree* the number 0:
|
|
|
|
|
|
|
|
(defun my-winum-assign-func ()
|
|
|
|
(cond
|
|
|
|
((equal (buffer-name) \"*Calculator*\")
|
|
|
|
9)
|
|
|
|
((string-match-p (buffer-name) \".*\\*NeoTree\\*.*\")
|
|
|
|
0)
|
|
|
|
(t
|
|
|
|
nil)))
|
|
|
|
|
|
|
|
(setq winum-assign-func 'my-winum-assign-func)"
|
2016-12-02 19:59:13 -05:00
|
|
|
:group 'winum
|
|
|
|
:type 'function)
|
|
|
|
|
2017-10-28 10:02:40 -04:00
|
|
|
(make-obsolete-variable 'winum-assign-func 'winum-assign-functions "2.0.0")
|
2017-05-28 08:47:33 -04:00
|
|
|
|
|
|
|
(defcustom winum-assign-functions nil
|
|
|
|
"List of functions called for each window by `winum-mode'.
|
|
|
|
|
2024-08-26 19:04:33 -04:00
|
|
|
These functions allow for deterministic assignment of indices to windows. Each
|
|
|
|
function is called for every window. A function should return the index to be
|
|
|
|
assigned to a window or nil. The *first* function to output a value for a
|
|
|
|
given window will determine this window's number.
|
2017-05-28 08:47:33 -04:00
|
|
|
|
|
|
|
If the list is empty or if every functions returns nil for a given window winum
|
2024-08-26 19:04:33 -04:00
|
|
|
will proceed to automatic assignment.
|
2017-05-28 08:47:33 -04:00
|
|
|
|
|
|
|
Since this list is meant to allow custom window assignment for *mutiple*
|
|
|
|
packages at once it should never be directly set, only added to and removed
|
|
|
|
from.
|
|
|
|
|
|
|
|
Example: always assign *Calculator* the number 9, *Flycheck-errors* the number 8
|
|
|
|
and *NeoTree* the number 0:
|
|
|
|
|
|
|
|
(defun winum-assign-9-to-calculator-8-to-flycheck-errors ()
|
|
|
|
(cond
|
|
|
|
((equal (buffer-name) \"*Calculator*\") 9)
|
|
|
|
((equal (buffer-name) \"*Flycheck errors*\") 8)))
|
|
|
|
|
|
|
|
(defun winum-assign-0-to-neotree ()
|
|
|
|
(when (string-match-p (buffer-name) \".*\\*NeoTree\\*.*\") 10))
|
|
|
|
|
|
|
|
(add-to-list
|
|
|
|
'winum-assign-functions #'winum-assign-9-to-calculator-8-to-flycheck-errors)
|
|
|
|
(add-to-list
|
|
|
|
'winum-assign-functions #'winum-assign-0-to-neotree)"
|
|
|
|
:group 'winum
|
2024-08-26 19:04:33 -04:00
|
|
|
:type '(repeat function))
|
|
|
|
|
|
|
|
(defcustom winum-auto-assign-function #'winum--auto-assign
|
|
|
|
"The function called to auto-assign indices to windows.
|
|
|
|
|
|
|
|
This function is called after `winum-assign-functions' to automatically assign
|
|
|
|
indices to the remaining windows. It must take in a list of windows and call
|
2024-11-14 00:27:30 -05:00
|
|
|
the function `winum--assign' to assign an index to each.
|
|
|
|
|
|
|
|
Auto-assign functions must not assign any indices that are `equal' to an index
|
|
|
|
already used (stored in `winum--assigned-indices'). The convenience macro
|
|
|
|
`winum--assign-unique' will handle this automatically.
|
2024-08-26 19:04:33 -04:00
|
|
|
|
|
|
|
The default auto-assign function is `winum--auto-assign', which assigns strictly
|
|
|
|
increasing integers to each window."
|
|
|
|
:group 'winum
|
|
|
|
:type 'function)
|
2017-05-28 08:47:33 -04:00
|
|
|
|
2016-12-04 14:38:42 -05:00
|
|
|
(defcustom winum-auto-setup-mode-line t
|
|
|
|
"When nil, `winum-mode' will not display window numbers in the mode-line.
|
|
|
|
You might want this to be nil if you use a package that already manages window
|
2017-01-11 01:26:04 -05:00
|
|
|
numbers in the mode-line."
|
|
|
|
:group 'winum
|
|
|
|
:type 'boolean)
|
2016-12-04 14:38:42 -05:00
|
|
|
|
2016-12-02 19:59:13 -05:00
|
|
|
(defcustom winum-mode-line-position 1
|
|
|
|
"The position in the mode-line `winum-mode' displays the number."
|
|
|
|
:group 'winum
|
|
|
|
:type 'integer)
|
|
|
|
|
2024-08-26 19:04:33 -04:00
|
|
|
(defcustom winum-mode-line-p #'integerp
|
|
|
|
"A predicate that determines when to display a window index on the mode-line.
|
|
|
|
By default, only integers are displayed."
|
|
|
|
:group 'winum
|
|
|
|
:type 'function)
|
|
|
|
|
2018-11-19 12:05:09 -05:00
|
|
|
(defcustom winum-format " %s "
|
|
|
|
"Format string defining how the window number looks like in the mode-line.
|
2024-08-26 19:04:33 -04:00
|
|
|
This string is passed to the `format' function along with the index."
|
2018-11-19 12:05:09 -05:00
|
|
|
:group 'winum
|
|
|
|
:type 'string)
|
|
|
|
|
2016-12-02 19:59:13 -05:00
|
|
|
(defcustom winum-ignored-buffers '(" *which-key*")
|
2024-08-26 19:04:33 -04:00
|
|
|
"List of buffers to ignore when assigning indices."
|
2016-12-14 19:13:44 -05:00
|
|
|
:group 'winum
|
|
|
|
:type '(repeat string))
|
2016-12-02 19:59:13 -05:00
|
|
|
|
2019-09-11 07:40:18 -04:00
|
|
|
(defcustom winum-ignored-buffers-regexp '()
|
2024-08-26 19:04:33 -04:00
|
|
|
"List of regexps for buffer names to ignore when assigning indices.
|
2019-09-11 07:40:18 -04:00
|
|
|
See Info node `(emacs) Regexps' or Info node `(elisp) Regular Expressions'"
|
2019-07-30 04:57:24 -04:00
|
|
|
:group 'winum
|
|
|
|
:type '(repeat string)
|
|
|
|
:risky t)
|
|
|
|
|
2016-12-15 12:22:20 -05:00
|
|
|
(defface winum-face '()
|
2024-08-26 19:04:33 -04:00
|
|
|
"Face used for the index in the mode-line."
|
2016-12-15 12:22:20 -05:00
|
|
|
:group 'winum)
|
|
|
|
|
2016-12-23 16:55:12 -05:00
|
|
|
(defvar winum-base-map
|
|
|
|
(let ((map (make-sparse-keymap)))
|
2024-08-26 19:04:33 -04:00
|
|
|
(define-key map (kbd "`") 'winum-select-window-by-index)
|
|
|
|
(define-key map (kbd "²") 'winum-select-window-by-index)
|
2016-12-23 16:55:12 -05:00
|
|
|
(define-key map (kbd "0") 'winum-select-window-0-or-10)
|
|
|
|
(define-key map (kbd "1") 'winum-select-window-1)
|
|
|
|
(define-key map (kbd "2") 'winum-select-window-2)
|
|
|
|
(define-key map (kbd "3") 'winum-select-window-3)
|
|
|
|
(define-key map (kbd "4") 'winum-select-window-4)
|
|
|
|
(define-key map (kbd "5") 'winum-select-window-5)
|
|
|
|
(define-key map (kbd "6") 'winum-select-window-6)
|
|
|
|
(define-key map (kbd "7") 'winum-select-window-7)
|
|
|
|
(define-key map (kbd "8") 'winum-select-window-8)
|
|
|
|
(define-key map (kbd "9") 'winum-select-window-9)
|
|
|
|
map)
|
|
|
|
"Keymap to be used under the prefix provided by `winum-keymap-prefix'.")
|
|
|
|
|
2024-08-26 19:04:33 -04:00
|
|
|
(defvar winum-mode-map (let ((map (make-sparse-keymap)))
|
|
|
|
(define-key map (kbd "C-x w") winum-base-map)
|
|
|
|
map)
|
2016-12-14 19:12:51 -05:00
|
|
|
"Keymap used for `winum-mode'.")
|
2016-12-02 19:59:13 -05:00
|
|
|
|
2016-12-15 10:50:32 -05:00
|
|
|
;; Internal variables ----------------------------------------------------------
|
|
|
|
|
|
|
|
(defvar winum--max-frames 16
|
|
|
|
"Maximum number of frames that can be numbered.")
|
|
|
|
|
2024-08-26 19:04:33 -04:00
|
|
|
(defvar winum--assigned-indices nil
|
|
|
|
"List of indices that have already been assigned by `winum-assign-functions'.")
|
2016-12-15 10:50:32 -05:00
|
|
|
|
2024-08-26 19:04:33 -04:00
|
|
|
(defvar winum--window-table nil
|
|
|
|
"Hash table with indices as keys and windows as values.
|
|
|
|
Used internally by winum to get a window provided an index.")
|
2016-12-15 10:50:32 -05:00
|
|
|
|
2024-08-26 19:04:33 -04:00
|
|
|
(defvar winum--index-table nil
|
|
|
|
"Hash table with windows as keys and indices as values.
|
|
|
|
Used internally by winum to get an index provided a window.")
|
2016-12-15 10:50:32 -05:00
|
|
|
|
|
|
|
(defvar winum--frames-table nil
|
2024-08-26 19:04:33 -04:00
|
|
|
"Table linking windows to indices and indices to windows for each frame.
|
2016-12-15 10:50:32 -05:00
|
|
|
|
|
|
|
Used only when `winum-scope' is 'frame-local to keep track of
|
2024-08-26 19:04:33 -04:00
|
|
|
separate window index sets in every frame.
|
2016-12-15 10:50:32 -05:00
|
|
|
|
|
|
|
It is a hash table using Emacs frames as keys and cons of the form
|
2024-08-26 19:04:33 -04:00
|
|
|
\(`winum--window-table' . `winum--index-table')
|
2016-12-15 10:50:32 -05:00
|
|
|
as values.
|
|
|
|
|
2024-08-26 19:04:33 -04:00
|
|
|
To get a window given an index, use the `car' of a value.
|
|
|
|
To get an index given a window, use the `cdr' of a value.
|
2016-12-15 10:50:32 -05:00
|
|
|
|
|
|
|
Such a structure allows for per-frame bidirectional fast access.")
|
|
|
|
|
2018-11-19 12:05:09 -05:00
|
|
|
(defvar winum--mode-line-segment
|
2024-08-26 19:04:33 -04:00
|
|
|
'(:eval (let ((index (winum-get-index)))
|
|
|
|
(when (funcall winum-mode-line-p index)
|
|
|
|
(propertize (format winum-format index) 'face 'winum-face))))
|
2018-11-19 12:05:09 -05:00
|
|
|
"What is pushed into `mode-line-format' when setting it up automatically.")
|
|
|
|
|
2017-04-30 16:07:08 -04:00
|
|
|
(defvar winum--last-used-scope winum-scope
|
|
|
|
"Tracks the last used `winum-scope'.
|
|
|
|
Needed to detect scope changes at runtime.")
|
|
|
|
|
2016-12-15 10:50:32 -05:00
|
|
|
;; Interactive functions -------------------------------------------------------
|
|
|
|
|
2016-12-02 19:59:13 -05:00
|
|
|
;;;###autoload
|
|
|
|
(define-minor-mode winum-mode
|
|
|
|
"A minor mode that allows for managing windows based on window numbers."
|
2024-08-26 19:04:33 -04:00
|
|
|
:lighter nil
|
2016-12-02 19:59:13 -05:00
|
|
|
:global t
|
|
|
|
(if winum-mode
|
|
|
|
(winum--init)
|
|
|
|
(winum--deinit)))
|
|
|
|
|
2016-12-10 19:40:40 -05:00
|
|
|
;;;###autoload
|
2016-12-10 14:20:19 -05:00
|
|
|
(defun winum-select-window-0-or-10 (&optional arg)
|
2024-08-26 19:04:33 -04:00
|
|
|
"Jump to window 0, or window 10 if 0 is not assigned.
|
2016-12-04 03:38:04 -05:00
|
|
|
If prefix ARG is given, delete the window instead of selecting it."
|
|
|
|
(interactive "P")
|
2024-11-14 01:10:08 -05:00
|
|
|
(let ((n (or (and (winum-get-window-by-index 0) 0)
|
|
|
|
(and (winum-get-window-by-index 10) 10)
|
|
|
|
(user-error "No window with index 0 or 10"))))
|
2024-08-26 19:04:33 -04:00
|
|
|
(if arg
|
|
|
|
(winum-delete-window-by-index n)
|
|
|
|
(winum-select-window-by-index n))))
|
2016-12-04 03:38:04 -05:00
|
|
|
|
2016-12-10 19:40:40 -05:00
|
|
|
;;;###autoload
|
2016-12-10 14:20:19 -05:00
|
|
|
(defun winum-select-window-0 (&optional arg)
|
2016-12-05 09:01:25 -05:00
|
|
|
"Jump to window 0.
|
|
|
|
If prefix ARG is given, delete the window instead of selecting it."
|
|
|
|
(interactive "P")
|
2024-08-26 19:04:33 -04:00
|
|
|
(if arg
|
|
|
|
(winum-delete-window-by-index 0)
|
|
|
|
(winum-select-window-by-index 0)))
|
2016-12-05 09:01:25 -05:00
|
|
|
|
2016-12-10 19:40:40 -05:00
|
|
|
;;;###autoload
|
2016-12-10 14:20:19 -05:00
|
|
|
(defun winum-select-window-1 (&optional arg)
|
2016-12-04 03:38:04 -05:00
|
|
|
"Jump to window 1.
|
|
|
|
If prefix ARG is given, delete the window instead of selecting it."
|
|
|
|
(interactive "P")
|
2024-08-26 19:04:33 -04:00
|
|
|
(if arg
|
|
|
|
(winum-delete-window-by-index 1)
|
|
|
|
(winum-select-window-by-index 1)))
|
2016-12-04 03:38:04 -05:00
|
|
|
|
2016-12-10 19:40:40 -05:00
|
|
|
;;;###autoload
|
2016-12-10 14:20:19 -05:00
|
|
|
(defun winum-select-window-2 (&optional arg)
|
2016-12-04 03:38:04 -05:00
|
|
|
"Jump to window 2.
|
|
|
|
If prefix ARG is given, delete the window instead of selecting it."
|
|
|
|
(interactive "P")
|
2024-08-26 19:04:33 -04:00
|
|
|
(if arg
|
|
|
|
(winum-delete-window-by-index 2)
|
|
|
|
(winum-select-window-by-index 2)))
|
2016-12-04 03:38:04 -05:00
|
|
|
|
2016-12-10 19:40:40 -05:00
|
|
|
;;;###autoload
|
2016-12-10 14:20:19 -05:00
|
|
|
(defun winum-select-window-3 (&optional arg)
|
2016-12-04 03:38:04 -05:00
|
|
|
"Jump to window 3.
|
|
|
|
If prefix ARG is given, delete the window instead of selecting it."
|
|
|
|
(interactive "P")
|
2024-08-26 19:04:33 -04:00
|
|
|
(if arg
|
|
|
|
(winum-delete-window-by-index 3)
|
|
|
|
(winum-select-window-by-index 3)))
|
2016-12-04 03:38:04 -05:00
|
|
|
|
2016-12-10 19:40:40 -05:00
|
|
|
;;;###autoload
|
2016-12-10 14:20:19 -05:00
|
|
|
(defun winum-select-window-4 (&optional arg)
|
2016-12-04 03:38:04 -05:00
|
|
|
"Jump to window 4.
|
|
|
|
If prefix ARG is given, delete the window instead of selecting it."
|
|
|
|
(interactive "P")
|
2024-08-26 19:04:33 -04:00
|
|
|
(if arg
|
|
|
|
(winum-delete-window-by-index 4)
|
|
|
|
(winum-select-window-by-index 4)))
|
2016-12-04 03:38:04 -05:00
|
|
|
|
2016-12-10 19:40:40 -05:00
|
|
|
;;;###autoload
|
2016-12-10 14:20:19 -05:00
|
|
|
(defun winum-select-window-5 (&optional arg)
|
2016-12-04 03:38:04 -05:00
|
|
|
"Jump to window 5.
|
|
|
|
If prefix ARG is given, delete the window instead of selecting it."
|
|
|
|
(interactive "P")
|
2024-08-26 19:04:33 -04:00
|
|
|
(if arg
|
|
|
|
(winum-delete-window-by-index 5)
|
|
|
|
(winum-select-window-by-index 5)))
|
2016-12-04 03:38:04 -05:00
|
|
|
|
2016-12-10 19:40:40 -05:00
|
|
|
;;;###autoload
|
2016-12-10 14:20:19 -05:00
|
|
|
(defun winum-select-window-6 (&optional arg)
|
2016-12-04 03:38:04 -05:00
|
|
|
"Jump to window 6.
|
|
|
|
If prefix ARG is given, delete the window instead of selecting it."
|
|
|
|
(interactive "P")
|
2024-08-26 19:04:33 -04:00
|
|
|
(if arg
|
|
|
|
(winum-delete-window-by-index 6)
|
|
|
|
(winum-select-window-by-index 6)))
|
2016-12-04 03:38:04 -05:00
|
|
|
|
2016-12-10 19:40:40 -05:00
|
|
|
;;;###autoload
|
2016-12-10 14:20:19 -05:00
|
|
|
(defun winum-select-window-7 (&optional arg)
|
2016-12-04 03:38:04 -05:00
|
|
|
"Jump to window 7.
|
|
|
|
If prefix ARG is given, delete the window instead of selecting it."
|
|
|
|
(interactive "P")
|
2024-08-26 19:04:33 -04:00
|
|
|
(if arg
|
|
|
|
(winum-delete-window-by-index 7)
|
|
|
|
(winum-select-window-by-index 7)))
|
2016-12-04 03:38:04 -05:00
|
|
|
|
2016-12-10 19:40:40 -05:00
|
|
|
;;;###autoload
|
2016-12-10 14:20:19 -05:00
|
|
|
(defun winum-select-window-8 (&optional arg)
|
2016-12-04 03:38:04 -05:00
|
|
|
"Jump to window 8.
|
|
|
|
If prefix ARG is given, delete the window instead of selecting it."
|
|
|
|
(interactive "P")
|
2024-08-26 19:04:33 -04:00
|
|
|
(if arg
|
|
|
|
(winum-delete-window-by-index 8)
|
|
|
|
(winum-select-window-by-index 8)))
|
2016-12-04 03:38:04 -05:00
|
|
|
|
2016-12-10 19:40:40 -05:00
|
|
|
;;;###autoload
|
2016-12-10 14:20:19 -05:00
|
|
|
(defun winum-select-window-9 (&optional arg)
|
2016-12-04 03:38:04 -05:00
|
|
|
"Jump to window 9.
|
|
|
|
If prefix ARG is given, delete the window instead of selecting it."
|
|
|
|
(interactive "P")
|
2024-08-26 19:04:33 -04:00
|
|
|
(if arg
|
|
|
|
(winum-delete-window-by-index 9)
|
|
|
|
(winum-select-window-by-index 9)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun winum-select-window-by-index (&optional arg)
|
|
|
|
"Select window whose index is specified by ARG.
|
|
|
|
There are several ways to provide the value:
|
|
|
|
- if called from elisp with an argument, use it.
|
|
|
|
- if called from elisp without an argument, use current window.
|
|
|
|
- if called interactively with a numeric prefix argument, use it.
|
|
|
|
- if called interactively with the default prefix argument, use current window.
|
|
|
|
- if called interactively and no argument is provided, read from minibuffer."
|
|
|
|
(interactive "P")
|
|
|
|
(let* ((i (if (called-interactively-p 'any)
|
|
|
|
(cond
|
|
|
|
((integerp arg) arg)
|
|
|
|
(arg (winum-get-index))
|
|
|
|
(t (read--expression "Window: ")))
|
|
|
|
(or arg (winum-get-index))))
|
|
|
|
(w (winum-get-window-by-index i)))
|
|
|
|
(if w
|
|
|
|
(winum--switch-to-window w)
|
|
|
|
(user-error "No window with index %S" i))))
|
2016-12-04 03:34:57 -05:00
|
|
|
|
2016-12-10 19:40:40 -05:00
|
|
|
;;;###autoload
|
2024-08-26 19:04:33 -04:00
|
|
|
(defun winum-delete-window-by-index (&optional arg)
|
|
|
|
"Delete window whose index is specified by ARG.
|
|
|
|
There are several ways to provide the value:
|
2016-12-03 05:33:30 -05:00
|
|
|
- if called from elisp with an argument, use it.
|
2024-08-26 19:04:33 -04:00
|
|
|
- if called from elisp without an argument, use current window.
|
2016-12-03 05:33:30 -05:00
|
|
|
- if called interactively with a numeric prefix argument, use it.
|
2024-08-26 19:04:33 -04:00
|
|
|
- if called interactively with the default prefix argument, use current window.
|
|
|
|
- if called interactively and no argument is provided, read from minibuffer."
|
2016-12-02 19:59:13 -05:00
|
|
|
(interactive "P")
|
2024-08-26 19:04:33 -04:00
|
|
|
(let* ((i (if (called-interactively-p 'any)
|
|
|
|
(cond
|
|
|
|
((integerp arg) arg)
|
|
|
|
(arg (winum-get-index))
|
|
|
|
(t (read--expression "Window: ")))
|
|
|
|
(or arg (winum-get-index))))
|
|
|
|
(w (winum-get-window-by-index i)))
|
2016-12-05 09:00:31 -05:00
|
|
|
(if w
|
2024-08-26 19:04:33 -04:00
|
|
|
(delete-window w)
|
|
|
|
(user-error "No window with index %S" i))))
|
2016-12-02 19:59:13 -05:00
|
|
|
|
2016-12-15 10:50:32 -05:00
|
|
|
;; Public API ------------------------------------------------------------------
|
|
|
|
|
2016-12-23 16:55:12 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun winum-set-keymap-prefix (prefix)
|
|
|
|
"Set key bindings prefix for `winum-keymap' based on `winum-base-map'.
|
|
|
|
This function overrides the value of `winum-keymap', so you
|
|
|
|
should call it before customization of `winum-keymap' and/or
|
|
|
|
after customization of `winum-base-map'.
|
|
|
|
PREFIX must be a key sequence, like the ones returned by `kbd'."
|
|
|
|
(setq winum-keymap (when prefix (let ((map (make-sparse-keymap)))
|
|
|
|
(define-key map prefix winum-base-map)
|
|
|
|
map)))
|
|
|
|
(setcdr (assoc 'winum-mode minor-mode-map-alist)
|
|
|
|
winum-keymap))
|
|
|
|
|
2016-12-02 19:59:13 -05:00
|
|
|
;;;###autoload
|
2024-08-26 19:04:33 -04:00
|
|
|
(defun winum-get-window-by-index (index)
|
|
|
|
"Return window INDEX if exists, nil otherwise."
|
|
|
|
(let ((window-table (winum--get-window-table)))
|
|
|
|
(gethash index window-table)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun winum-get-index (&optional window)
|
|
|
|
"Get the index of WINDOW or the current window."
|
|
|
|
(let ((w (or window (selected-window))))
|
|
|
|
(gethash w (winum--get-index-table))))
|
2016-12-02 19:59:13 -05:00
|
|
|
|
2024-08-26 19:04:33 -04:00
|
|
|
;; For backwards compatibility
|
2016-12-02 19:59:13 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun winum-get-number-string (&optional window)
|
|
|
|
"Get the current or specified window's current number as a propertized string.
|
|
|
|
WINDOW: if specified, the window of which we want to know the number.
|
|
|
|
If not specified, the number of the currently selected window is
|
|
|
|
returned."
|
2024-08-26 19:04:33 -04:00
|
|
|
(let* ((n (winum-get-index window))
|
|
|
|
(s (if (funcall winum-mode-line-p n)
|
|
|
|
(format "%s" n)
|
2017-02-17 04:10:00 -05:00
|
|
|
"")))
|
2016-12-02 19:59:13 -05:00
|
|
|
(propertize s 'face 'winum-face)))
|
|
|
|
|
2016-12-15 10:50:32 -05:00
|
|
|
;; Internal functions ----------------------------------------------------------
|
2016-12-02 19:59:13 -05:00
|
|
|
|
|
|
|
(defun winum--init ()
|
|
|
|
"Initialize winum-mode."
|
|
|
|
(if (eq winum-scope 'frame-local)
|
|
|
|
(setq winum--frames-table (make-hash-table :size winum--max-frames))
|
2024-08-26 19:04:33 -04:00
|
|
|
(setq winum--window-table (make-hash-table :test 'equal)
|
|
|
|
winum--index-table (make-hash-table :test 'equal)))
|
2016-12-04 14:38:42 -05:00
|
|
|
(when winum-auto-setup-mode-line
|
|
|
|
(winum--install-mode-line))
|
2016-12-02 19:59:13 -05:00
|
|
|
(add-hook 'minibuffer-setup-hook 'winum--update)
|
|
|
|
(add-hook 'window-configuration-change-hook 'winum--update)
|
|
|
|
(dolist (frame (frame-list))
|
|
|
|
(select-frame frame)
|
|
|
|
(winum--update)))
|
|
|
|
|
|
|
|
(defun winum--deinit ()
|
|
|
|
"Actions performed when turning off winum-mode."
|
2016-12-04 14:38:42 -05:00
|
|
|
(when winum-auto-setup-mode-line
|
|
|
|
(winum--clear-mode-line))
|
2016-12-02 19:59:13 -05:00
|
|
|
(remove-hook 'minibuffer-setup-hook 'winum--update)
|
|
|
|
(remove-hook 'window-configuration-change-hook 'winum--update)
|
|
|
|
(setq winum--frames-table nil))
|
|
|
|
|
|
|
|
(defun winum--install-mode-line (&optional position)
|
2024-08-26 19:04:33 -04:00
|
|
|
"Install the window index from `winum-mode' to the mode-line.
|
2016-12-02 19:59:13 -05:00
|
|
|
POSITION: position in the mode-line."
|
|
|
|
(let ((mode-line (default-value 'mode-line-format))
|
2018-11-04 13:49:34 -05:00
|
|
|
res)
|
2016-12-02 19:59:13 -05:00
|
|
|
(dotimes (i (min (or position winum-mode-line-position 1)
|
|
|
|
(length mode-line)))
|
2016-12-09 11:04:15 -05:00
|
|
|
(push (pop mode-line) res))
|
2018-11-06 19:40:30 -05:00
|
|
|
(unless (equal (car mode-line) winum--mode-line-segment)
|
|
|
|
(push winum--mode-line-segment res))
|
2016-12-02 19:59:13 -05:00
|
|
|
(while mode-line
|
2016-12-09 11:04:15 -05:00
|
|
|
(push (pop mode-line) res))
|
2018-11-06 19:44:48 -05:00
|
|
|
(let ((nres (nreverse res)))
|
|
|
|
(setq mode-line-format nres)
|
|
|
|
(setq-default mode-line-format nres)))
|
2016-12-02 19:59:13 -05:00
|
|
|
(force-mode-line-update t))
|
|
|
|
|
|
|
|
(defun winum--clear-mode-line ()
|
2024-08-26 19:04:33 -04:00
|
|
|
"Remove the window index of `winum-mode' from the mode-line."
|
2016-12-02 19:59:13 -05:00
|
|
|
(let ((mode-line (default-value 'mode-line-format))
|
2018-11-06 19:47:11 -05:00
|
|
|
res)
|
2016-12-02 19:59:13 -05:00
|
|
|
(while mode-line
|
2018-11-06 19:47:11 -05:00
|
|
|
(let ((item (pop mode-line)))
|
2018-11-06 19:40:30 -05:00
|
|
|
(unless (equal item winum--mode-line-segment)
|
2024-08-26 19:04:33 -04:00
|
|
|
(push item res))))
|
2018-11-06 19:44:48 -05:00
|
|
|
(let ((nres (nreverse res)))
|
|
|
|
(setq mode-line-format nres)
|
|
|
|
(setq-default mode-line-format nres)))
|
2016-12-02 19:59:13 -05:00
|
|
|
(force-mode-line-update t))
|
|
|
|
|
|
|
|
(defun winum--update ()
|
2024-08-26 19:04:33 -04:00
|
|
|
"Update window indices."
|
2016-12-02 19:59:13 -05:00
|
|
|
(let ((windows (winum--window-list)))
|
2024-08-26 19:04:33 -04:00
|
|
|
(setq winum--assigned-indices nil)
|
|
|
|
(clrhash (winum--get-window-table))
|
|
|
|
(clrhash (winum--get-index-table))
|
2017-05-28 08:47:33 -04:00
|
|
|
(when winum-assign-functions
|
2024-08-26 19:04:33 -04:00
|
|
|
(--each (-copy windows) (when (winum--try-to-find-custom-index it)
|
|
|
|
(setq windows (delq it windows)))))
|
|
|
|
(when (and winum-minibuffer-auto-assign
|
2016-12-04 14:41:43 -05:00
|
|
|
(active-minibuffer-window)
|
2024-08-26 19:04:33 -04:00
|
|
|
(not (winum-get-window-by-index winum-minibuffer-auto-assign)))
|
|
|
|
(winum--assign (active-minibuffer-window) winum-minibuffer-auto-assign)
|
|
|
|
(push winum-minibuffer-auto-assign winum--assigned-indices))
|
|
|
|
;; Auto-assign remaining windows
|
|
|
|
(when windows
|
|
|
|
(funcall winum-auto-assign-function windows))))
|
|
|
|
|
2024-11-14 00:27:30 -05:00
|
|
|
(defmacro winum--assign-unique (window var &optional after update pred)
|
|
|
|
"Ensure that the index in VAR is unique, then assign it to WINDOW.
|
|
|
|
|
|
|
|
If AFTER is non-nil, also ensure that VAR is unique after the assignment.
|
|
|
|
|
|
|
|
UPDATE and PRED are quoted function symbols or unquoted sexps. If they
|
|
|
|
are functions, then the value of VAR is passed as input.
|
|
|
|
UPDATE must return a new index to be assigned to VAR, with the guarantee
|
|
|
|
that repeatedly executing it will never return the same index twice. The
|
|
|
|
default updating function is `1+'.
|
|
|
|
PRED must be non-nil if VAR must be updated. By default, PRED checks if
|
|
|
|
the variable's value is in `winum--assigned-indices'."
|
|
|
|
(unless pred
|
|
|
|
(setq pred `(member ,var winum--assigned-indices)))
|
|
|
|
(unless update
|
|
|
|
(setq update '#'1+))
|
|
|
|
(and (= (safe-length pred) 2)
|
|
|
|
(memq (car pred) '(quote function))
|
|
|
|
(setq pred `(funcall ,pred ,var)))
|
|
|
|
(and (= (safe-length update) 2)
|
|
|
|
(memq (car update) '(quote function))
|
|
|
|
(setq update `(funcall ,update ,var)))
|
|
|
|
`(progn
|
|
|
|
(while ,pred (setq ,var ,update))
|
|
|
|
(winum--assign ,window ,var)
|
|
|
|
,@(and after `((setq ,var ,update)))))
|
|
|
|
|
2024-08-26 19:04:33 -04:00
|
|
|
(defun winum--auto-assign (windows)
|
|
|
|
"The default auto-assign function for assigning indices to windows.
|
|
|
|
Takes in the list of windows WINDOWS and uses `winum--assign' to assign
|
|
|
|
increasing integers to it."
|
|
|
|
(let ((index 1))
|
2016-12-02 19:59:13 -05:00
|
|
|
(dolist (w windows)
|
2024-11-14 00:27:30 -05:00
|
|
|
(winum--assign-unique w index t))))
|
2016-12-02 19:59:13 -05:00
|
|
|
|
2024-08-26 19:04:33 -04:00
|
|
|
(defun winum--try-to-find-custom-index (window)
|
|
|
|
"Try to find and assign a custom index for WINDOW.
|
2017-05-28 08:47:33 -04:00
|
|
|
Do so by trying every function in `winum-assign-functions' and assign the
|
2024-08-26 19:04:33 -04:00
|
|
|
*first* non nil value.
|
|
|
|
When multiple functions assign an index to a window log a warning and use the
|
|
|
|
first index anyway."
|
2017-05-28 08:47:33 -04:00
|
|
|
(with-selected-window window
|
|
|
|
(with-current-buffer (window-buffer window)
|
2024-08-26 19:04:33 -04:00
|
|
|
(when-let* ((inds (->> winum-assign-functions
|
|
|
|
(--map (cons it (funcall it)))
|
|
|
|
(--remove (null (cdr it)))))
|
|
|
|
(ind (-> inds (cl-first) (cdr))))
|
|
|
|
(when (> (length inds) 1)
|
|
|
|
(message "Winum conflict - window %s was assigned an index by multiple custom assign functions: '%s'"
|
|
|
|
window (--map (format "%s -> %S" (car it) (cdr it)) inds)))
|
|
|
|
(winum--assign window ind)
|
|
|
|
(push ind winum--assigned-indices)))))
|
|
|
|
|
|
|
|
(defun winum--assign (window index)
|
|
|
|
"Assign to window WINDOW the index INDEX.
|
|
|
|
Returns the assigned index, or nil on error."
|
|
|
|
(if (gethash index (winum--get-window-table))
|
2017-04-15 10:13:11 -04:00
|
|
|
(progn
|
2024-08-26 19:04:33 -04:00
|
|
|
(message "Index %S already assigned to %s, can't assign to %s"
|
|
|
|
index (gethash index (winum--get-window-table)) window)
|
|
|
|
nil)
|
|
|
|
(puthash index window (winum--get-window-table))
|
|
|
|
(puthash window index (winum--get-index-table))
|
|
|
|
index))
|
2017-04-15 10:13:11 -04:00
|
|
|
|
2016-12-02 19:59:13 -05:00
|
|
|
(defun winum--window-list ()
|
|
|
|
"Return a list of interesting windows."
|
|
|
|
(cl-remove-if
|
2017-04-15 09:50:39 -04:00
|
|
|
#'winum--ignore-window-p
|
2016-12-02 19:59:13 -05:00
|
|
|
(cl-case winum-scope
|
|
|
|
(global
|
|
|
|
(cl-mapcan 'winum--list-windows-in-frame
|
|
|
|
(if winum-reverse-frame-list
|
|
|
|
(frame-list)
|
|
|
|
(nreverse (frame-list)))))
|
|
|
|
(visible
|
|
|
|
(cl-mapcan 'winum--list-windows-in-frame
|
|
|
|
(if winum-reverse-frame-list
|
|
|
|
(visible-frame-list)
|
|
|
|
(nreverse (visible-frame-list)))))
|
|
|
|
(frame-local
|
|
|
|
(winum--list-windows-in-frame))
|
|
|
|
(t
|
2024-08-26 19:04:33 -04:00
|
|
|
(user-error "Invalid `winum-scope': %S" winum-scope)))))
|
2016-12-02 19:59:13 -05:00
|
|
|
|
2017-04-15 09:50:39 -04:00
|
|
|
(defun winum--ignore-window-p (window)
|
2024-08-26 19:04:33 -04:00
|
|
|
"Non-nil if WINDOW should be ignored for indexing."
|
2017-04-15 09:50:39 -04:00
|
|
|
(let ((f (window-frame window)))
|
|
|
|
(or (not (and (frame-live-p f)
|
|
|
|
(frame-visible-p f)))
|
|
|
|
(string= "initial_terminal" (terminal-name f))
|
2019-07-30 04:57:24 -04:00
|
|
|
(member (buffer-name (window-buffer window)) winum-ignored-buffers)
|
|
|
|
(cl-some
|
|
|
|
(lambda (regex) (string-match regex (buffer-name (window-buffer window))))
|
2019-09-11 07:40:18 -04:00
|
|
|
winum-ignored-buffers-regexp))))
|
2017-04-15 09:50:39 -04:00
|
|
|
|
2016-12-02 19:59:13 -05:00
|
|
|
(defun winum--list-windows-in-frame (&optional f)
|
|
|
|
"List windows in frame F using natural Emacs ordering."
|
|
|
|
(window-list f 0 (frame-first-window f)))
|
|
|
|
|
2024-08-26 19:04:33 -04:00
|
|
|
(defun winum--get-window-table ()
|
|
|
|
"Return the window table used to get a window given an index.
|
|
|
|
This hashtable is not stored the same way depending on the value of
|
|
|
|
`winum-scope'."
|
2017-04-30 16:07:08 -04:00
|
|
|
(winum--check-for-scope-change)
|
2024-08-26 19:04:33 -04:00
|
|
|
(winum--check-frames-table)
|
2016-12-02 19:59:13 -05:00
|
|
|
(if (eq winum-scope 'frame-local)
|
2016-12-04 14:43:29 -05:00
|
|
|
(car (gethash (selected-frame) winum--frames-table))
|
2024-08-26 19:04:33 -04:00
|
|
|
winum--window-table))
|
2016-12-02 19:59:13 -05:00
|
|
|
|
2024-08-26 19:04:33 -04:00
|
|
|
(defun winum--get-index-table ()
|
|
|
|
"Return the index hashtable used to get an index given a window.
|
2016-12-02 19:59:13 -05:00
|
|
|
This hashtable is not stored the same way depending on the value of
|
2024-08-26 19:04:33 -04:00
|
|
|
`winum-scope'."
|
2017-04-30 16:07:08 -04:00
|
|
|
(winum--check-for-scope-change)
|
2017-09-20 01:34:22 -04:00
|
|
|
(winum--check-frames-table)
|
2016-12-02 19:59:13 -05:00
|
|
|
(if (eq winum-scope 'frame-local)
|
2016-12-04 14:43:29 -05:00
|
|
|
(cdr (gethash (selected-frame) winum--frames-table))
|
2024-08-26 19:04:33 -04:00
|
|
|
winum--index-table))
|
2016-12-02 19:59:13 -05:00
|
|
|
|
2017-09-20 01:34:22 -04:00
|
|
|
(defun winum--check-frames-table ()
|
|
|
|
"Make sure `winum--frames-table' exists and is correctly equipped.
|
|
|
|
Verifies 2 things (when `winum-scope' is frame local):
|
|
|
|
* When `winum-scope' is frame-local for the first time it may be necessary to
|
|
|
|
instantiate `winum--frames-table'.
|
|
|
|
* A table entry for the current frame must be made when the frame has just
|
|
|
|
been created."
|
|
|
|
(when (eq winum-scope 'frame-local)
|
|
|
|
(unless winum--frames-table
|
|
|
|
(setq winum--frames-table (make-hash-table :size winum--max-frames)))
|
|
|
|
(unless (gethash (selected-frame) winum--frames-table)
|
2024-08-26 19:04:33 -04:00
|
|
|
(puthash (selected-frame) (cons
|
|
|
|
(make-hash-table :test 'equal)
|
|
|
|
(make-hash-table :test 'equal))
|
|
|
|
winum--frames-table)
|
2017-09-20 01:34:22 -04:00
|
|
|
(winum--update))))
|
|
|
|
|
2016-12-02 19:59:13 -05:00
|
|
|
(defun winum--switch-to-window (window)
|
|
|
|
"Switch to the window WINDOW and switch input focus if on a different frame."
|
|
|
|
(let ((frame (window-frame window)))
|
|
|
|
(when (and (frame-live-p frame)
|
|
|
|
(not (eq frame (selected-frame))))
|
|
|
|
(select-frame-set-input-focus frame))
|
|
|
|
(if (window-live-p window)
|
|
|
|
(select-window window)
|
|
|
|
(error "Got a dead window %S" window))))
|
|
|
|
|
2017-04-30 16:07:08 -04:00
|
|
|
(defun winum--check-for-scope-change ()
|
|
|
|
"Check whether the `winum-scope' has been changed.
|
|
|
|
If a change is detected run `winum--init' to reinitialize all
|
|
|
|
internal data structures according to the new scope."
|
|
|
|
(unless (eq winum-scope winum--last-used-scope)
|
|
|
|
(setq winum--last-used-scope winum-scope)
|
|
|
|
(winum--init)))
|
|
|
|
|
2017-09-19 13:36:01 -04:00
|
|
|
(defun winum--remove-deleted-frame-from-frames-table (frame)
|
|
|
|
"Remove FRAME from `winum--frames-table' after it was deleted."
|
|
|
|
(when winum--frames-table
|
|
|
|
(remhash frame winum--frames-table)))
|
|
|
|
|
|
|
|
(add-hook 'delete-frame-functions #'winum--remove-deleted-frame-from-frames-table)
|
|
|
|
|
2016-12-02 19:59:13 -05:00
|
|
|
(provide 'winum)
|
|
|
|
|
|
|
|
;;; winum.el ends here
|