-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindow-number.el
169 lines (135 loc) · 5.89 KB
/
window-number.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
;;; window-number.el
;; Copyright (C) 2004 Johann "Myrkraverk" Oskarsson
;; <myrkraverk@users.sourceforge.net>
;; 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 2 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, write to the Free
;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
;; MA 02111-1307 USA
;; Introduction
;; ============
;; Window number mode allows you to select windows by numbers. This
;; edition now works with XEmacs as well as GNU Emacs. The window
;; numbers do not show up in the mode-line in XEmacs yet, instead a
;; -?- is displayed. Hopefully this can be fixed soon, but really
;; depends on XEmacs developers.
;; Installation
;; ============
;; Drop this file into your load path. C-h v load-path RET or F1 v
;; load-path RET will help. Then place the following lines into your
;; .emacs or ~/.xemacs/init.el and uncomment them.
;; ----------------------------------------------------------------------------
;; (autoload 'window-number-mode "window-number"
;; "A global minor mode that enables selection of windows according to
;; numbers with the C-x C-j prefix. Another mode,
;; `window-number-meta-mode' enables the use of the M- prefix."
;; t)
;; (autoload 'window-number-meta-mode "window-number"
;; "A global minor mode that enables use of the M- prefix to select
;; windows, use `window-number-mode' to display the window numbers in
;; the mode-line."
;; t)
;; ----------------------------------------------------------------------------
;; Then you can use M-x window-number-mode RET to turn the mode on, or
;; place (window-number-mode 1) and (window-number-meta-mode 1) into
;; your .emacs or ~/.xemacs/init.el.
;; ----------------------------------------------------------------------------
;; Code starts here.
;; ----------------------------------------------------------------------------
(require 'cl); for set-difference and loop
(defun window-number-list ()
"Returns a list of the windows, in fixed order and the
minibuffer (even if not active) last."
(let* ((walk-windows-start
(car (set-difference
(window-list (selected-frame) t)
(window-list (selected-frame) 1))))
(walk-windows-current walk-windows-start)
list)
(while (progn
(setq walk-windows-current
(next-window walk-windows-current t))
(setq list (cons walk-windows-current list))
(not (eq walk-windows-current walk-windows-start))))
(reverse (cons (car list) (cdr list)))))
(defun window-number-select (number)
"Selects the nth window."
(interactive "P")
(if (integerp number)
(let ((window (nth (1- number) (window-number-list))))
(if (and window (or (not (window-minibuffer-p window))
(minibuffer-window-active-p window)))
(select-window window)
(error "No such window.")))))
(defun window-number ()
"Returns the the number of the current window."
(length
(memq (selected-window)
(nreverse (window-number-list)))))
(defun window-number-string ()
"Returns the string containing the number of the current window"
(propertize
(concat " -" (number-to-string (window-number)) "-")
'face
'window-number-face))
(defvar window-number-mode-map nil
"Keymap for the window number mode.")
(defvar window-number-meta-mode-map nil
"Keymap for the window number meta mode.")
(defmacro window-number-define-keys (mode-map prefix)
`(progn
,@(loop for number from 1 to 10 collect
`(define-key ,mode-map
(kbd ,(concat prefix (number-to-string
(if (>= number 10) 0 number))))
(lambda nil (interactive)
(window-number-select ,number))))))
; define C-x C-j 1 to switch to win 1, etc (C-x C-j 0 = win 10)
(unless window-number-mode-map
(setq window-number-mode-map (make-sparse-keymap))
; space after C-j is important
(window-number-define-keys window-number-mode-map "C-x C-j "))
; define M-1 to switch to win 1, etc (M-0 = win 10)
(unless window-number-meta-mode-map
(setq window-number-meta-mode-map (make-sparse-keymap))
(window-number-define-keys window-number-meta-mode-map "M-"))
(if (featurep 'xemacs)
(define-minor-mode window-number-mode
"A global minor mode that enables selection of windows
according to numbers with the C-x C-j prefix. Another mode,
`window-number-meta-mode' enables the use of the M- prefix."
:global t
:init-value nil
:lighter " -?-")
(define-minor-mode window-number-mode
"A global minor mode that enables selection of windows
according to numbers with the C-x C-j prefix. Another mode,
`window-number-meta-mode' enables the use of the M- prefix."
:global t
:init-value nil
:lighter (:eval (window-number-string))))
(define-minor-mode window-number-meta-mode
"A global minor mode that enables use of the M- prefix to
select windows, use `window-number-mode' to display the window
numbers in the mode-line."
:global t
:init-value nil)
;;(push (cons 'my-window-number-meta-mode my-window-number-mode-map)
;; minor-mode-map-alist)
(defface window-number-face
'((((type tty) (class color))
(:background "red"))
(((type tty) (class mono))
(:inverse-video t))
(((type x w32 mac))
(:foreground "red")))
"The face used for the window number in the mode-line.")
(provide 'window-number)
;;; window-number.el ends here.