Importing archived version.

This commit is contained in:
Nikolaj Schumacher 2008-04-11 21:24:04 +02:00
parent 9704505170
commit bd4acd1f43

View file

@ -1,6 +1,6 @@
;;; window-numbering --- Emacs window shortcuts ;;; window-numbering --- Emacs window shortcuts
;; Copyright (C) 2006 Nikolaj Schumacher <bugs * nschum , de> ;; Copyright (C) 2006-2007 Nikolaj Schumacher <bugs * nschum , de>
;;; License ;;; License
@ -19,25 +19,23 @@
;;; Usage ;;; Usage
;; Use `deftest' to define a test and `run-elk-test' to run it. ;; Enable window-numbering-mode and use M-1 through M-0 to navigate.
;; Create test bundles with `defsuite' or `build-suite'. ;;
;; Verify your code with `assert-equal', `assert-eq', `assert-eql', ;; If you want to affect the numbers, use window-numbering-before-hook or
;; `assert-nonnil', `assert-t', `assert-nil' and `assert-error' ;; window-numbering-assign-func.
;; to verify your code. ;; For instance, to always assign the calculator window the number 9, add the
;; following to your .emacs:
;;
;; (setq window-numbering-assign-func
;; (lambda () (when (equal (buffer-name) "*Calculator*") 9)))
;;; Version 0.9
(defvar window-numbering-table nil ;;; Changes
"table -> (window vector . number table)")
(defun select-window-by-number (i) ;; 2007-02-18 (1.1)
(interactive) ;; added window-numbering-before-hook, window-numbering-assign-func
(let ((windows (car (gethash (selected-frame) window-numbering-table)))
window) ;;; Configuration
(if (and (>= i 0) (< i 10)
(setq window (aref windows i)))
(select-window window)
(error "No window numbered %s" i))))
(defvar window-numbering-keymap (defvar window-numbering-keymap
(let ((map (make-sparse-keymap))) (let ((map (make-sparse-keymap)))
@ -57,31 +55,96 @@
(defvar window-numbering-auto-assign-0-to-minibuffer t (defvar window-numbering-auto-assign-0-to-minibuffer t
"If non-nil, `window-numbering-mode' assigns 0 to the minibuffer if active.") "If non-nil, `window-numbering-mode' assigns 0 to the minibuffer if active.")
(defun window-numbering-enumerate () (defconst window-numbering-mode-line-position 1
;; (let ((table (gethash (selected-frame) window-numbering-table))) "The position in the mode-line `window-numbering-mode' displays the number.")
;; (unless table
(let ((table (cons (make-vector 10 nil) (make-hash-table :size 10)))) (defvar window-numbering-before-hook nil
(puthash (selected-frame) table window-numbering-table) "Hook called before `window-numbering-mode' starts assigning numbers.
(let ((window-numbering-windows (car table)) The number of windows that will be numbered is passed as a parameter.
(window-numbering-numbers (cdr table)) Use `window-numbering-assign' to manually assign some of them a number.
(minibuffer (when window-numbering-auto-assign-0-to-minibuffer If you want to assign a number to just one buffer, use
(active-minibuffer-window)))) `window-numbering-assign-func' instead.")
(window-numbering-walk (car (window-tree)) 1 (if minibuffer 9 10))
(when minibuffer
(set (aref window-numbering-windows 0) minibuffer)
(puthash minibuffer 0 window-numbering-numbers)))
window-numbering-windows))
;; (setq window-numbering-windows (make-vector 10 nil)) (defvar window-numbering-assign-func nil
;; (setq window-numbering-numbers (make-hash-table :size 10)) "Function called for each window by `window-numbering-mode'.
;; (let* ((minibuffer (when window-numbering-auto-assign-0-to-minibuffer This is called before automatic assignment begins.
;; (active-minibuffer-window)))) The function should return a number to have it assigned to the current-window,
;; (window-numbering-walk (car (window-tree)) 1 (if minibuffer 9 10)) nil otherwise.")
;; (when minibuffer
;; (setf (aref window-numbering-windows 0) minibuffer) ;;; Code
;; (puthash minibuffer 0 window-numbering-numbers)))
;; window-numbering-windows) (require 'cl)
(defvar window-numbering-table nil
"table -> (window vector . number table)")
(defun window-numbering-calculate-left (windows)
(let ((i 9) left)
(while (>= i 0)
(let ((window (aref windows i)))
(unless window
(push (% (1+ i) 10) left)))
(decf i))
left))
(defun window-numbering-assign (window &optional number)
(assert (boundp 'window-numbering-windows))
(assert (boundp 'window-numbering-numbers))
(assert (boundp 'window-numbering-left))
(if number
(if (aref window-numbering-windows number)
(progn (message "Number %s assigned to two buffers (%s and %s)"
number window (aref window-numbering-windows number))
nil)
(setf (aref window-numbering-windows number) window)
(puthash window number window-numbering-numbers)
(setq window-numbering-left (delq number window-numbering-left))
t)
;; else default adding
(when window-numbering-left
(unless (gethash window window-numbering-numbers)
(let ((number (car window-numbering-left)))
(window-numbering-assign window number)
number)))))
(defun select-window-by-number (i)
"Select window given number I by `window-numbering-mode'."
(interactive)
(let ((windows (car (gethash (selected-frame) window-numbering-table)))
window)
(if (and (>= i 0) (< i 10)
(setq window (aref windows i)))
(select-window window)
(error "No window numbered %s" i))))
(defun window-numbering-update ()
"Update the window numbering for the current frame.
Optional parameter PREASSIGNED-WINDOWS is a hashmap already mapping some
windows to numbers."
(let* ((window-numbering-windows (make-vector 10 nil))
(window-numbering-numbers (make-hash-table :size 10))
(window-numbering-left
(window-numbering-calculate-left window-numbering-windows)))
(puthash (selected-frame)
(cons window-numbering-windows window-numbering-numbers)
window-numbering-table)
(when (and window-numbering-auto-assign-0-to-minibuffer
(active-minibuffer-window))
(window-numbering-assign (active-minibuffer-window) 0))
(let ((windows (window-list nil 0 (window-at 0 0))))
(run-hook-with-args 'window-numbering-before-hook windows)
(when window-numbering-assign-func
(mapc `(lambda (window)
(with-selected-window window
(with-current-buffer (window-buffer window)
(let ((num (funcall ,window-numbering-assign-func)))
(when num
(window-numbering-assign window num))))))
windows))
(dolist (window windows)
(window-numbering-assign window))
)))
(defun select-window-0 () (defun select-window-0 ()
(interactive) (interactive)
@ -114,50 +177,40 @@
(interactive) (interactive)
(select-window-by-number 9)) (select-window-by-number 9))
(defun window-numbering-walk (tree i max-window) (defun window-numbering-get-number-string (&optional window)
(if (windowp tree) (int-to-string (window-numbering-get-number window)))
(when (<= i max-window)
(let ((pos (if (= i 10) 0 i)))
(set (aref window-numbering-windows pos) tree)
(puthash tree pos window-numbering-numbers))
(incf i))
(let ((windows (if (windowp tree) `(,tree) (cddr tree))))
(dolist (window windows)
(setq i (window-numbering-walk window i max-window)))))
i)
(defconst window-numbering-mode-line-position 1
"The position in the mode-line `window-numbering-mode' displays the number.")
;; (window-numbering-get-number)
(defun window-numbering-get-number (&optional window) (defun window-numbering-get-number (&optional window)
(int-to-string (gethash (or window (selected-window))
(gethash (or window (selected-window)) window-numbering-numbers))) (cdr (gethash (selected-frame) window-numbering-table))))
(define-minor-mode window-numbering-mode (define-minor-mode window-numbering-mode
"A minor mode that assigns a number to each window." "A minor mode that assigns a number to each window."
nil nil window-numbering-keymap :global t nil nil window-numbering-keymap :global t
(if window-numbering-mode (if window-numbering-mode
(save-excursion (unless window-numbering-table
(setq window-numbering-table (make-hash-table :size 16)) (save-excursion
(window-numbering-install-mode-line) (setq window-numbering-table (make-hash-table :size 16))
(add-hook 'window-configuration-change-hook (window-numbering-install-mode-line)
'window-numbering-enumerate) (add-hook 'window-configuration-change-hook
(dolist (frame (frame-list)) 'window-numbering-update)
(select-frame frame) (dolist (frame (frame-list))
(window-numbering-enumerate))) (select-frame frame)
(window-numbering-update))))
(window-numbering-clear-mode-line) (window-numbering-clear-mode-line)
(remove-hook 'window-configuration-change-hook (remove-hook 'window-configuration-change-hook
'window-numbering-enumerate) 'window-numbering-update)
(setq window-numbering-table nil))) (setq window-numbering-table nil)))
(defun window-numbering-install-mode-line (&optional position) (defun window-numbering-install-mode-line (&optional position)
"Install the window number from `window-numbering-mode' to the mode-line."
(let ((mode-line (default-value 'mode-line-format)) (let ((mode-line (default-value 'mode-line-format))
(res)) (res))
(dotimes (i (min (or position window-numbering-mode-line-position) (dotimes (i (min (or position window-numbering-mode-line-position)
(length mode-line))) (length mode-line)))
(push (car mode-line) res) (push (car mode-line) res)
(pop mode-line)) (pop mode-line))
(push '(:eval (window-numbering-get-number)) res) (push '(:eval (window-numbering-get-number-string)) res)
(while mode-line (while mode-line
(push (car mode-line) res) (push (car mode-line) res)
(pop mode-line)) (pop mode-line))
@ -165,12 +218,12 @@
(force-mode-line-update t)) (force-mode-line-update t))
(defun window-numbering-clear-mode-line () (defun window-numbering-clear-mode-line ()
"Remove the dot installed by `test-runner-install-dot' from the mode-line." "Remove the window number of `window-numbering-mode' from the mode-line."
(let ((mode-line (default-value 'mode-line-format)) (let ((mode-line (default-value 'mode-line-format))
(res)) (res))
(while mode-line (while mode-line
(let ((item (car mode-line))) (let ((item (car mode-line)))
(unless (equal item '(:eval (window-numbering-get-number))) (unless (equal item '(:eval (window-numbering-get-number-string)))
(push item res))) (push item res)))
(pop mode-line)) (pop mode-line))
(setq-default mode-line-format (nreverse res))) (setq-default mode-line-format (nreverse res)))