replace hard-coded values by variables: max frames and max windows

This commit is contained in:
deb0ch 2016-11-30 01:59:52 +01:00
parent ed2e4722a3
commit 6078468da6

View file

@ -86,6 +86,10 @@ return a number to have it assigned to the current-window, nil otherwise."
:group 'window-numbering :group 'window-numbering
:type 'integer) :type 'integer)
(defcustom window-numbering--window-number-max 10
"Max number of windows that can be numbered."
:group 'window-numbering
:type 'integer)
(defface window-numbering-face '() (defface window-numbering-face '()
"Face used for the number in the mode-line." "Face used for the number in the mode-line."
@ -117,7 +121,7 @@ return a number to have it assigned to the current-window, nil otherwise."
(window-numbering--deinit))) (window-numbering--deinit)))
;; define interactive functions window-numbering-select-window-[0..n] ;; define interactive functions window-numbering-select-window-[0..n]
(dotimes (i 10) (dotimes (i window-numbering--window-number-max)
(eval `(defun ,(intern (format "select-window-%s" i)) (&optional arg) (eval `(defun ,(intern (format "select-window-%s" i)) (&optional arg)
,(format "Jump to window %d.\nIf prefix ARG is given, delete the\ ,(format "Jump to window %d.\nIf prefix ARG is given, delete the\
window instead of selecting it." i) window instead of selecting it." i)
@ -142,7 +146,7 @@ If prefix ARG is given, delete the window instead of selecting it."
window-numbering--frames-table)) window-numbering--frames-table))
window-numbering--window-vector)) window-numbering--window-vector))
window) window)
(if (and (>= i 0) (< i 10) (if (and (>= i 0) (< i window-numbering--window-number-max)
(setq window (aref windows i))) (setq window (aref windows i)))
window window
(error "No window numbered %s" i)))) (error "No window numbered %s" i))))
@ -204,11 +208,7 @@ WINDOW: if specified, the window of which we want to know the number.
;; Internal variables ;; Internal variables
(defvar window-numbering--window-number-max 10 ;; TODO replace hard-coded values (defvar window-numbering--max-frames 16
;; by this var
"Max number of windows that can be numbered.")
(defvar window-numbering--max-frames-count 16
"Maximum number of frames that can be numbered.") "Maximum number of frames that can be numbered.")
(defvar window-numbering--window-vector nil (defvar window-numbering--window-vector nil
@ -239,35 +239,32 @@ Such a structure allows for per-frame bidirectional fast access.")
(defun window-numbering--init () (defun window-numbering--init ()
"Initialize window-numbering-mode." "Initialize window-numbering-mode."
(unless window-numbering--frames-table
(save-excursion ;; TODO is this really needed ?
(if (eq window-numbering-scope 'frame-local) (if (eq window-numbering-scope 'frame-local)
(setq window-numbering--frames-table (make-hash-table :size 16)) (setq window-numbering--frames-table (make-hash-table :size window-numbering--max-frames))
(setq window-numbering--numbers-table (make-hash-table :size 10))) (setq window-numbering--numbers-table (make-hash-table :size window-numbering--window-number-max)))
(window-numbering-install-mode-line) (window-numbering-install-mode-line)
(add-hook 'minibuffer-setup-hook 'window-numbering--update) (add-hook 'minibuffer-setup-hook 'window-numbering--update)
(add-hook 'window-configuration-change-hook (add-hook 'window-configuration-change-hook 'window-numbering--update)
'window-numbering--update)
(dolist (frame (frame-list)) (dolist (frame (frame-list))
(select-frame frame) (select-frame frame)
(window-numbering--update))))) (window-numbering--update)))
(defun window-numbering--deinit () (defun window-numbering--deinit ()
"Actions performed when turning off window-numbering-mode." "Actions performed when turning off window-numbering-mode."
(window-numbering-clear-mode-line) (window-numbering-clear-mode-line)
(remove-hook 'minibuffer-setup-hook 'window-numbering--update) (remove-hook 'minibuffer-setup-hook 'window-numbering--update)
(remove-hook 'window-configuration-change-hook (remove-hook 'window-configuration-change-hook 'window-numbering--update)
'window-numbering--update)
(setq window-numbering--frames-table nil)) (setq window-numbering--frames-table nil))
(defun window-numbering--update () (defun window-numbering--update ()
"Update window numbers." "Update window numbers."
(setq window-numbering--remaining (window-numbering--get-available-numbers)) (setq window-numbering--remaining (window-numbering--available-numbers))
(if (eq window-numbering-scope 'frame-local) (if (eq window-numbering-scope 'frame-local)
(puthash (selected-frame) (puthash (selected-frame)
(cons (make-vector 10 nil) (make-hash-table :size 10)) (cons (make-vector window-numbering--window-number-max nil)
(make-hash-table :size window-numbering--window-number-max))
window-numbering--frames-table) window-numbering--frames-table)
(setq window-numbering--window-vector (make-vector 10 nil)) (setq window-numbering--window-vector (make-vector window-numbering--window-number-max nil))
(clrhash window-numbering--numbers-table)) (clrhash window-numbering--numbers-table))
(when (and window-numbering-auto-assign-0-to-minibuffer (when (and window-numbering-auto-assign-0-to-minibuffer
(active-minibuffer-window)) (active-minibuffer-window))
@ -353,19 +350,13 @@ This hashtable is not stored the same way depending on the value of
window-numbering--frames-table)) window-numbering--frames-table))
window-numbering--numbers-table)) window-numbering--numbers-table))
(defun window-numbering--get-available-numbers (&optional windows) (defun window-numbering--available-numbers ()
"Return a list of numbers currently available for assignment. "Return a list of numbers from 1 to `window-numbering--window-number-max'.
WINDOWS: a vector of currently assigned windows." 0 is the last element of the list."
;; TODO simplify, the WINDOWS argument is not needed in the current (let ((numbers))
;; implementation. (dotimes (i window-numbering--window-number-max)
(let ((i 9) (push (% (1+ i) window-numbering--window-number-max) numbers))
left) (nreverse numbers)))
(while (>= i 0)
(let ((window (when windows (aref windows i))))
(unless window
(push (% (1+ i) 10) left)))
(decf i))
left))
(defun window-numbering--switch-to-window (window) (defun window-numbering--switch-to-window (window)
"Switch to the window WINDOW and switch input focus if on a different frame." "Switch to the window WINDOW and switch input focus if on a different frame."