-
Notifications
You must be signed in to change notification settings - Fork 0
/
wcy-swbuff.el
138 lines (122 loc) · 5.02 KB
/
wcy-swbuff.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
;;; wcy-swbuff.el --- switch buffer quickly
;; Copyright (C) 2004 Free Software Foundation, Inc.
;; Author: ChunYe Wang <[email protected]>
;; Keywords: buffer, convenience
;; Compatibility: Emacs 20.7, Emacs 21, XEmacs
;; This file 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, or (at your option)
;; any later version.
;; This file 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;
;; Place this file somewhere in your `load-path', and add:
;;; (require 'wcy-swbuffer)
;;; (global-set-key (kbd "<C-tab>") 'wcy-switch-buffer-forward)
;;; (global-set-key (kbd "<C-S-kp-tab>") 'wcy-switch-buffer-backward)
;;; then you can use <C-tab> and <C-S-kp-tab> to switch buffer.
(defvar wcy-buffer-exclude-regexps
'("^ .*" "^\\*.*" )
"if buffer name match the regexp, ignore it.")
(defvar wcy-switch-buffer-key
(kbd "<C-tab>")
"default key bind for switch buffer.")
(defvar wcy-switch-buffer-active-buffer-face
'highlight
"default face for active buffer"
)
(defvar wcy-switch-buffer-inactive-buffer-face
'secondary-selection
"default face for inactive buffer"
)
;;cycly the list
;;;###autoload
(defun wcy-cycly-list(l)
(append (cdr l) (list (car l))))
;;cycly the list reverse
;;;###autoload
(defun wcy-cycly-list-reverse(l)
(append (last l) (reverse (cdr (reverse l)))))
;;;###autoload
(defun wcy-buffer-list ()
(if (null wcy-buffer-exclude-regexps)
(buffer-list)
(let ((regexp (mapconcat 'identity wcy-buffer-exclude-regexps "\\|"))
(buffer-list nil))
(dolist (buffer (buffer-list))
(if (not (string-match regexp (buffer-name buffer)))
(setq buffer-list (append buffer-list (list buffer) nil))))
buffer-list)))
;;;###autoload
(defun wcy-display-buffer-list (buffer-list)
"display a buffer list in the echo area."
(let ((other-buffer-name (mapconcat 'buffer-name (cdr buffer-list) "|"))
message-log-max) ;; disable *Message* log
(if (> (length other-buffer-name) (window-width))
(let* ((half-length (- (/ (window-width) 2) 3)))
(setq other-buffer-name
(concat (substring other-buffer-name 0 half-length)
" ... "
(substring other-buffer-name
(- (length other-buffer-name) half-length))))))
(message "%s"
(concat (propertize (buffer-name (car buffer-list))
'face wcy-switch-buffer-active-buffer-face)
"|"
(propertize other-buffer-name
'face wcy-switch-buffer-inactive-buffer-face))))
(switch-to-buffer (car buffer-list) t))
;;;###autoload
(defun wcy-switch-buffer (arg)
"switch buffer with <C-tab> like in windows.
if ARG is negative, switch backword, otherwise forward."
(interactive "p")
(let* ((cycle-function (if (> arg 0) 'wcy-cycly-list ;; cycle forward or backward
'wcy-cycly-list-reverse))
(tmp-buffer-list (funcall cycle-function (wcy-buffer-list))) ;; a list of buffer object
(exitflag nil)
(oldbuffer (current-buffer))
key)
(wcy-display-buffer-list tmp-buffer-list)
(while (null exitflag)
;; read a key
(setq key (read-key-sequence-vector nil))
(let* ((func (key-binding key)))
(setq last-command-event (aref key (1- (length key))))
(cond
((equal func 'wcy-switch-buffer-forward)
(setq tmp-buffer-list (wcy-cycly-list tmp-buffer-list))
(wcy-display-buffer-list tmp-buffer-list))
((equal func 'wcy-switch-buffer-backward)
(setq tmp-buffer-list (wcy-cycly-list-reverse tmp-buffer-list))
(wcy-display-buffer-list tmp-buffer-list))
((equal func 'keyboard-quit)
(setq tmp-buffer-list (cons oldbuffer tmp-buffer-list))
(setq exitflag 1))
(t (setq exitflag t)))))
;; switch to the selected buffer.
(let ((selected-buffer (car tmp-buffer-list)))
(if (buffer-name selected-buffer)
(switch-to-buffer selected-buffer)))
;; execute the last key.
(or (eq 1 exitflag)
(let ((func (key-binding key)))
(if func (call-interactively func))))
nil))
;;;###autoload
(defun wcy-switch-buffer-forward ()
(interactive)
(wcy-switch-buffer 1))
;;;###autoload
(defun wcy-switch-buffer-backward ()
(interactive)
(wcy-switch-buffer -1))
(provide 'wcy-swbuff)
;; wcy-swbuff.el ends here