├── .gitignore ├── Cask ├── README.md ├── custom-setup.png ├── default-setup.png ├── nswbuff.el └── test ├── nswbuff-test.el └── test-helper.el /.gitignore: -------------------------------------------------------------------------------- 1 | *.elc 2 | swbuff* 3 | /.cask/ 4 | -------------------------------------------------------------------------------- /Cask: -------------------------------------------------------------------------------- 1 | (source gnu) 2 | (source melpa) 3 | 4 | (package-file "nswbuff.el") 5 | 6 | (development 7 | (depends-on "ert-runner")) 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # `nswbuff` # 2 | 3 | `nswbuff` is an Emacs library for quick buffer switching based on [`swbuff.el`](https://www.emacswiki.org/emacs/SwBuff), incorporating the changes from [`swbuff-x.el`](https://www.emacswiki.org/emacs/swbuff-x.el) and a few new features not present in either. 4 | 5 | ## Installation ## 6 | 7 | `nswbuff` can be installed through [Melpa](https://melpa.org/#/nswbuff). 8 | 9 | 10 | ## Primary commands ## 11 | 12 | The primary commands are `nswbuff-switch-to-previous-buffer` and `nswbuff-switch-to-next-buffer`, which should be bound to keys of your liking, e.g., `` and `` (i.e., `Ctrl-TAB` and `Shift-Ctrl-TAB`). 13 | 14 | In a default setup, executing `nswbuff-switch-to-next-buffer` once has the following effect: 15 | 16 | ![nswbuff screen shot 1](default-setup.png) 17 | 18 | This screen shot shows a buffer switch from `nswbuff.el` to `README.md`. The bottom of the frame shows the list of buffers. Special buffers (those whose names start with `*`) are in red. The bold red, underlined buffer will become active when buffer switching ends. The first buffer in the list is the one you're switching from, which is still visible in the main window. Executing `nswbuff-switch-to-next-buffer` again will highlight the next buffer in the list (`*ielm*` in the screen shot). 19 | 20 | Buffer switching ends automatically after a short delay, or when you press any key (other than the keys bound to `nswbuff-switch-to-next-buffer` and `nswbuff-switch-to-previous-buffer`, obviously). By default, during buffer switching, the initial buffer remains visible until buffer switching ends. If you wish to make the intermediate buffers visible during cycling, you can set the option `nswbuff-display-intermediate-buffers` to `t`: 21 | 22 | ![nswbuff screen shot 2](custom-setup.png) 23 | 24 | This screen shot shows the same buffer switch, from `nswbuff.el` to `README.md`, but this time the `README.md` buffer is shown during switching. Executing `nswbuff-switch-to-next-buffer` again would show the `*eshell*` buffer. Note that the screen shot also shows that the face and colours used for displaying the buffer names are fully customisable. 25 | 26 | 27 | ## Customizing the buffer list ## 28 | 29 | The buffers that `nswbuff` offers for cycling are essentially the buffers returned by the function `buffer-list`. There are a few options to customize this list, however. First, you can exclude buffers with `nswbuff-exclude-buffer-regexps`. Any buffer whose name matches one of the regexps in this list is not switchable. The default setting excludes buffers with a name that begins with a blank character. To exclude all the internal buffers (that is `*scratch*`, `*Message*`, etc...) you could use the following regexps `'("^ .*" "^\\*.*\\*")`. 30 | 31 | Buffers can also be excluded based on their major mode through the option `nswbuff-exclude-mode-regexps`. These regexps are matched against the `symbol-name` of the buffer's major mode. 32 | 33 | Buffers that match one of these options but should be included anyway can be matched using the option `nswbuff-include-buffer-regexps`. Any buffer matching one of the regexps in this list is included, regardless of whether they also match an exclude regexp. Obviously, the same effect can also be obtained by using more specialized exclude regexps, but using an include regexp is usually easier to understand and less error-prone. 34 | 35 | Another option for customizing the list of switchable buffers is `nswbuff-buffer-list-function`. This can be set to a function that returns a list of buffers, making only those buffers switchable instead of all buffers returned by `buffer-list`. If the function in `nswbuff-buffer-list-function` returns `nil`, `nswbuff` falls back to the buffers returned by `buffer-list`. 36 | 37 | Note that the list of buffers returned by the function in `nswbuff-buffer-list-function` is still matched against `nswbuff-exclude-buffer-regexps`, `nswbuff-exclude-mode-regexps` and `nswbuff-include-buffer-regexps`, so set (or keep) these to `nil` if you don't want this. 38 | 39 | Lastly, buffers can be excluded through a buffer-local variable `nswbuff-exclude`. This variable can be set to `t` in a file-local variable block or in a `.dir-locals.el` file to exclude specific files from switching that cannot be easily captured in another way. For example, if you want to exclude certain Org files (e.g., the agenda files) from the buffer list without excluding all Org files, you could use this method. 40 | 41 | 42 | ## Further Customization ## 43 | 44 | Quite a few aspects of `nswbuff` can be customized in addition to the ones already mentioned. See the customization group `nswbuff` for details. One option worth pointing out here is `nswbuff-status-window-layout`, which controls how the window displaying the list of buffers during switching is laid out. Normally, the buffer shows the list of buffers on a single line, but if the number of buffers grows too large, you can either choose to scroll the buffer list horizontally (so that not all buffers names are visible in the `nswbuff` status window), or you can decide to adjust the status window's height. The default value is a combination of these two options: if there is only one window in the frame, the status window's height is adjusted, otherwise the buffer list is scrolled. 45 | 46 | Alternatively, you can set `nswbuff-status-window-layout` to `minibuffer`, in which case the minibuffer will be used to display the buffer list. 47 | 48 | ## Projects ## 49 | 50 | `nswbuff` provides some very simple but nifty integration with the project-management packages `project.el` (built-in) and [`projectile`](http://batsov.com/projectile). If you set `nswbuff-buffer-list-function` to `nswbuff-project-buffer-list` or `nswbuff-projectile-buffer-list`, only the buffers belonging to the current project are offered for switching. This makes for a very light-weight (but in my opinion effective) alternative to such modes as [`perspective.el`](https://github.com/nex3/perspective-el) or [`persp-mode`](https://github.com/Bad-ptr/persp-mode.el). Switching buffers with `nswbuff` keeps you in the project, any other buffer-switching method (`switch-to-buffer`, but also 3rd-party options such as ivy or helm) is not affected and can thus be used to move out of the project. 51 | 52 | 53 | ## `golden-ratio` / `zoom` ## 54 | 55 | Packages such as [zoom](https://github.com/cyrus-and/zoom) and [golden-ratio](https://github.com/roman/golden-ratio.el) automatically resize windows to ensure that the active window always has a certain minimum size. There is no point in resizing the buffer switching window, however, so in order to prevent that from happening, you should add the name of the `nswbuff` status buffer to `golden-ratio-exclude-buffer-names` or `zoom-ignored-buffer-names`. This name defaults to `" *nswbuff*"` (without the double quotes; note the initial space). 56 | 57 | -------------------------------------------------------------------------------- /custom-setup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joostkremers/nswbuff/dfea30e33ddb212a0d537bc927b4bcdf3ebe2cd1/custom-setup.png -------------------------------------------------------------------------------- /default-setup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joostkremers/nswbuff/dfea30e33ddb212a0d537bc927b4bcdf3ebe2cd1/default-setup.png -------------------------------------------------------------------------------- /nswbuff.el: -------------------------------------------------------------------------------- 1 | ;;; nswbuff.el --- Quick switching between buffers. -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 1998, 2000, 2001, 2003, 2004 by David Ponce 4 | ;; Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. 5 | ;; Copyright (C) 2017-2022 Joost Kremers 6 | 7 | ;; Author: David Ponce 8 | ;; Kahlil (Kal) HODGSON 9 | ;; Joost Kremers 10 | ;; Maintainer: Joost Kremers 11 | ;; Created: 18 May 2017 12 | ;; Keywords: extensions convenience 13 | ;; Package-Version: 1.3 14 | ;; Package-Requires: ((emacs "25.1")) 15 | ;; URL: https://github.com/joostkremers/nswbuff 16 | 17 | ;; This file is not part of Emacs 18 | 19 | ;; This program is free software; you can redistribute it and/or 20 | ;; modify it under the terms of the GNU General Public License as 21 | ;; published by the Free Software Foundation; either version 2, or (at 22 | ;; your option) any later version. 23 | 24 | ;; This program is distributed in the hope that it will be useful, but 25 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 26 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 27 | ;; General Public License for more details. 28 | 29 | ;; You should have received a copy of the GNU General Public License 30 | ;; along with this program; see the file COPYING. If not, write to 31 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 32 | ;; Boston, MA 02111-1307, USA. 33 | 34 | ;;; Commentary: 35 | ;; 36 | ;; `nswbuff' is an Emacs library for quick buffer switching based on swbuff.el, 37 | ;; incorporating the changes from swbuff-x.el and a few new features not present 38 | ;; in either. 39 | ;; 40 | ;; This package provides two commands: `nswbuff-switch-to-next-buffer' and 41 | ;; `nswbuff-switch-to-previous-buffer' to switch to the next or previous buffer 42 | ;; in the buffer list, respectively. During buffer switching, the list of 43 | ;; switchable buffers is visible at the bottom of the frame. 44 | ;; 45 | ;; Switching buffers pops-up a status window at the bottom of the selected 46 | ;; window. The status window shows the list of switchable buffers where the 47 | ;; switched one is highlighted using `nswbuff-current-buffer-face'. This window 48 | ;; is automatically discarded after any command is executed or after the delay 49 | ;; specified by `nswbuff-clear-delay'. 50 | ;; 51 | ;; The bufferlist is sorted by how recently the buffers were used. If you 52 | ;; prefer a fixed (cyclic) order set `nswbuff-recent-buffers-first' to nil. 53 | ;; 54 | ;; There are several options to manipulate the list of switchable buffers. 55 | ;; The option `nswbuff-exclude-buffer-regexps' defines a list of regular 56 | ;; expressions for excluded buffers. The default setting excludes 57 | ;; buffers whose name begin with a blank character. To exclude all the 58 | ;; internal buffers (that is *scratch*, *Message*, etc...) you could 59 | ;; use the following regexps '("^ .*" "^\\*.*\\*"). 60 | ;; 61 | ;; Buffers can also be excluded by major mode using the option 62 | ;; `nswbuff-exclude-mode-regexp'. 63 | ;; 64 | ;; The option `nswbuff-include-buffer-regexps' defines a list of regular 65 | ;; expressions of buffers that must be included, even if they already match a 66 | ;; regexp in `nswbuff-exclude-buffer-regexps'. (The same could be done by using 67 | ;; more sophisticated exclude regexps, but this option keeps the regexps cleaner 68 | ;; and easier to understand.) 69 | ;; 70 | ;; You can further customize the list of switchable buffers by setting the 71 | ;; option `nswbuff-buffer-list-function' to a function that returns a list of 72 | ;; buffers. Only the buffers returned by this function will be offered for 73 | ;; switching. If this function returns nil, the buffers returned by 74 | ;; `buffer-list' is used instead. Note that this list is still checked against 75 | ;; `nswbuff-exclude-buffer-regexps', `nswbuff-exclude-mode-regexp' and 76 | ;; `nswbuff-include-buffer-regexps'. 77 | ;; 78 | ;; Two functions already provided that makes use of this option are 79 | ;; `nswbuff-project-buffer-list' and `nswbuff-projectile-buffer-list', which 80 | ;; return the buffers of the current project according to the built-in 81 | ;; `project.el' or [Projectile](http://batsov.com/projectile/), 82 | ;; respectively. 83 | 84 | ;;; History: 85 | ;; 86 | 87 | ;;; Code: 88 | (require 'timer) 89 | (require 'seq) 90 | (require 'subr-x) 91 | 92 | (declare-function projectile-project-buffers "ext:projectile.el" (&optional project)) 93 | 94 | ;;; Options 95 | 96 | (defgroup nswbuff nil 97 | "Quick switching between Emacs buffers." 98 | :group 'extensions 99 | :group 'convenience 100 | :prefix "nswbuff-") 101 | 102 | (defcustom nswbuff-status-window-layout nil 103 | "Method used to ensure the switched buffer is always visible. 104 | This occurs when the buffer list is larger than the status window 105 | width. The possible choices are: 106 | 107 | - - 'Default' If there is only one window in the frame (ignoring the 108 | minibuffer one and the status window itself) the status 109 | window height is adjusted. 110 | Otherwise horizontal scrolling is used. 111 | - - 'Scroll' Horizontal scrolling is always used. 112 | - - 'Adjust' Only adjust the window height. 113 | - - 'Minibuffer' Use the minibuffer." 114 | :group 'nswbuff 115 | :type '(choice (const :tag "Default" nil) 116 | (const :tag "Scroll" scroll) 117 | (const :tag "Adjust" adjust) 118 | (const :tag "Minibuffer" minibuffer))) 119 | 120 | (defcustom nswbuff-clear-delay 3 121 | "Time in seconds to delay before discarding the status window." 122 | :group 'nswbuff 123 | :type '(number :tag "Seconds")) 124 | 125 | (defcustom nswbuff-recent-buffers-first t 126 | "Show recent buffers first. 127 | If non-nil the buffer list is sorted by how recently the buffers were 128 | used. If nil, it is as a cyclic list with fixed order. Note that 129 | other commands (switch-to-buffer) still change the order." 130 | :group 'nswbuff 131 | :type 'boolean) 132 | 133 | (defcustom nswbuff-separator " | " 134 | "String used to separate buffer names in the status line. 135 | It is possible to include a newline character in order to obtain 136 | a vertical buffer display." 137 | :group 'nswbuff 138 | :type 'string) 139 | 140 | (defcustom nswbuff-header "" 141 | "Status line header string." 142 | :group 'nswbuff 143 | :type 'string) 144 | 145 | (defcustom nswbuff-trailer "" 146 | "Status line trailer string." 147 | :group 'nswbuff 148 | :type 'string) 149 | 150 | (defcustom nswbuff-status-window-at-top nil 151 | "If set, put the status window at the top of the current window." 152 | :group 'nswbuff 153 | :type 'boolean) 154 | 155 | (define-obsolete-variable-alias 'nswbuff-window-min-text-height 'nswbuff-status-window-min-text-height "1.1") 156 | 157 | (defcustom nswbuff-status-window-min-text-height 1 158 | "Minimum text height of the status window." 159 | :group 'nswbuff 160 | :type 'integer) 161 | 162 | (defface nswbuff-default-face '((t (:inherit highlight))) 163 | "Default face used for buffer names." 164 | :group 'nswbuff) 165 | 166 | (defface nswbuff-current-buffer-face '((t (:inherit font-lock-keyword-face))) 167 | "Face used to highlight the current buffer name." 168 | :group 'nswbuff) 169 | 170 | (defface nswbuff-separator-face '((t (:inherit font-lock-comment-face))) 171 | "Face used for separators." 172 | :group 'nswbuff) 173 | 174 | (defcustom nswbuff-exclude-buffer-regexps '("^ ") 175 | "List of regular expressions for excluded buffers. 176 | The default setting excludes buffers whose name begin with a 177 | blank character. To exclude all the internal buffers (that is 178 | *scratch*, *Message*, etc...) use the following regexps: 179 | (\"^ \" \"^\\*.*\\*\")." 180 | :group 'nswbuff 181 | :type '(repeat (regexp :format "%v"))) 182 | 183 | (defcustom nswbuff-this-frame-only t 184 | "If non-nil, skip buffers displayed in other visble or iconified frames. 185 | This is a convient way of temporarily excluding a particluar 186 | buffer from the cycle list." 187 | :group 'nswbuff 188 | :type 'boolean) 189 | 190 | (defcustom nswbuff-exclude-mode-regexp "" 191 | "Regular expression matching major modes to skip when cycling." 192 | :group 'nswbuff 193 | :type '(string :tag "Regexp")) 194 | 195 | (defcustom nswbuff-include-buffer-regexps nil 196 | "List of buffer names to always be included." 197 | :group 'nswbuff 198 | :type '(repeat (regexp :format "%v"))) 199 | 200 | (defcustom nswbuff-buffer-list-function nil 201 | "Function to obtain a list of switchable buffers. 202 | The list of buffers returned by this function is further filtered 203 | according to the options `nswbuff-exclude-buffer-regexps', 204 | `nswbuff-exclude-mode-regexp' and 205 | `nswbuff-include-buffer-regexps'. 206 | 207 | The function should return a list of buffers or nil if no 208 | eligible buffers exist. When nil is returned, buffer switching 209 | defaults to the standard `buffer-list' function. 210 | 211 | One predefined function is `nswbuff-projectile-buffer-list', 212 | which returns the buffers in the current projectile project plus 213 | any buffers from the standard buffer list that match 214 | `nswbuff-include-buffer-regexps'." 215 | :group 'nswbuff 216 | :type '(choice (const :tag "Use Default Buffer List" :value nil) 217 | (const :tag "Use Project Buffer List" :value nswbuff-project-buffer-list) 218 | (const :tag "Use Projectile Buffer List" :value nswbuff-projectile-buffer-list) 219 | (function :tag "Use Custom Function"))) 220 | 221 | (declare-function project-current "ext:project") 222 | (declare-function project-root "ext:project") 223 | 224 | (defun nswbuff-project-buffer-list () 225 | "Return the buffers of the current project.el project. 226 | Added to the list are buffers that are not part of the current 227 | project but that match `nswbuff-include-buffer-regexps'. If the 228 | current buffer is not part of a project, return nil." 229 | (when-let ((curr-proj (project-current))) 230 | (let ((conn (file-remote-p (project-root curr-proj)))) 231 | (seq-filter (lambda (buf) 232 | ;; Most of this defun is copied from upstream 233 | ;; project.el. There, it is assumed that a project 234 | ;; resides entirely on one host, and it is noted 235 | ;; that they may relax that in the future. 236 | ;; https://github.com/emacs-straight/project/blob/4072f35d85bf0a1c669329d66633e4819f497c1c/project.el#L1126-L1128 237 | (and (equal conn 238 | (file-remote-p (buffer-local-value 'default-directory buf))) 239 | (or 240 | (equal curr-proj 241 | (with-current-buffer buf 242 | (project-current))) 243 | (nswbuff-include-p (buffer-name buf))))) 244 | (buffer-list))))) 245 | 246 | (defun nswbuff-projectile-buffer-list () 247 | "Return the buffers of the current Projectile project. 248 | Added to the list are buffers that are not part of the current 249 | project but that match `nswbuff-include-buffer-regexps'. If the 250 | current buffer is not part of a project, return nil." 251 | (if (and (fboundp 'projectile-project-p) 252 | (projectile-project-p default-directory)) 253 | (let ((projectile-buffers (projectile-project-buffers))) 254 | (dolist (buf (buffer-list) projectile-buffers) 255 | (if (and (nswbuff-include-p (buffer-name buf)) 256 | (not (memq buf projectile-buffers))) 257 | (setq projectile-buffers (append projectile-buffers (list buf)))))))) 258 | 259 | (defcustom nswbuff-pre-switch-hook nil 260 | "Standard hook containing functions to be called before a switch. 261 | This option can be made buffer-local. This may be useful for 262 | handling modes that use more than one window for display. For 263 | example, VM uses one (small) window for its Summary buffer and 264 | the remaining frame for the Presentation buffer. Switching 265 | buffers and retaining the window configuration doesn't make sense 266 | in this context, so by setting the following hooks, these extra 267 | windows can be deleted before switching: 268 | 269 | \(defun my-vm-mode-hook () 270 | \"Delete other windows before a switch.\" 271 | (make-local-hook \\='swbuff-pre-switch-hook) 272 | (add-hook \\='swbuff-pre-switch-hook #\\='delete-other-windows t t)) 273 | 274 | \(add-hook \\='vm-mode-hook #\\='my-vm-mode-hook) 275 | \(add-hook \\='vm-summary-mode-hook #\\='my-vm-mode-hook) 276 | \(add-hook \\='vm-presentation-mode-hook #\\='my-vm-mode-hook)" 277 | :group 'nswbuff 278 | :type 'hook) 279 | 280 | (defcustom nswbuff-start-with-current-centered nil 281 | "If t, center the current buffer in the buffer list." 282 | :group 'nswbuff 283 | :type 'boolean) 284 | 285 | (defcustom nswbuff-delay-switch nil 286 | "If t, just show the buffer list upon first call. 287 | When set, the functions `nswbuff-next-buffer' and 288 | `nswbuff-previous-buffer' simply display the buffer list when 289 | they are first called rather than switching buffers immediately. 290 | Only a second call to either of these functions actually switches 291 | the buffer." 292 | :group 'nswbuff 293 | :type 'boolean) 294 | 295 | (defcustom nswbuff-display-intermediate-buffers nil 296 | "If t, show intermediate buffers while switching. 297 | When set, each call to `nswbuff-next-buffer' or 298 | `nswbuff-previous-buffer' in a sequence causes a new buffer to be 299 | displayed. If nil only the last buffer in the sequence is 300 | actually displayed." 301 | :group 'nswbuff 302 | :type 'boolean) 303 | 304 | (defcustom nswbuff-left "" 305 | "String placed immediately before a buffer name in the status line. 306 | For example, try \"(\"." 307 | :group 'nswbuff 308 | :type 'string) 309 | 310 | (defcustom nswbuff-right "" 311 | "String placed immediately after a buffer name in the status line. 312 | For example, try \")\"." 313 | :group 'nswbuff 314 | :type 'string) 315 | 316 | (defcustom nswbuff-special-buffers-re "^\\*" 317 | "Regular expression matching special buffers. 318 | Buffers matching this regular expression are highlighted with 319 | `nswbuff-special-buffers-face'." 320 | :group 'nswbuff 321 | :type 'string) 322 | 323 | (defface nswbuff-special-buffers-face '((t (:inherit warning))) 324 | "Face for highlighting special buffers in the buffer list." 325 | :group 'nswbuff) 326 | 327 | (defcustom nswbuff-mode-line-format nil 328 | "Mode line format of the nswbuff status window. 329 | If set to nil, no mode line is displayed. See `mode-line-format' 330 | for a detailed format description." 331 | :group 'nswbuff 332 | :type 'sexp) 333 | 334 | ;;; Internals 335 | ;; 336 | (defvar nswbuff-buffer-list nil "List of currently switchable buffers.") 337 | 338 | ;; Store the initial buffer-list, buffer, window, and frame at the 339 | ;; time the switch sequence was called. 340 | (defvar nswbuff-initial-buffer-list nil "Initial buffer list when switching is initiated.") 341 | (defvar nswbuff-initial-buffer nil "Initial buffer when switching is initiated.") 342 | (defvar nswbuff-initial-window nil "Initial window when switching is initiated.") 343 | (defvar nswbuff-initial-frame nil "Initial frame when switching is initiated.") 344 | 345 | (defvar nswbuff-current-buffer nil "Current buffer being displayed during switching.") 346 | 347 | (defvar-local nswbuff-exclude nil "Buffer-local variable that can be set to exclude a buffer from the buffer list.") 348 | (put 'nswbuff-exclude 'safe-local-variable 'booleanp) 349 | 350 | (defvar nswbuff-status-window nil 351 | "The status buffer window. 352 | This window is saved in case any external code that runs on a 353 | timer changes the current window.") 354 | 355 | (defvar nswbuff-status-buffer nil 356 | "The status buffer.") 357 | 358 | (defvar nswbuff-display-timer nil "The timer used to remove the status window after `nswbuff-clear-delay'.") 359 | 360 | (defvar nswbuff-override-map 361 | (let ((map (make-sparse-keymap))) 362 | map) 363 | "Override map for nswbuff. 364 | This map becomes active whenever ‘nswbuff-switch-to-next-buffer’ or 365 | ‘nswbuff-switch-to-previous-buffer’ is invoked. It can be used to 366 | bind functions for buffer handling which then become available 367 | during buffer switching.") 368 | 369 | ;; Make sure status buffer is reset when window layout is changed. 370 | (add-variable-watcher 371 | 'nswbuff-status-window-layout 372 | (lambda (_oldval _newval _operation _where) 373 | (setq nswbuff-status-buffer nil))) 374 | 375 | (defun nswbuff-status-buffer-name () 376 | "Name of the working buffer used by nswbuff to display the buffer list." 377 | (if (eq nswbuff-status-window-layout 'minibuffer) " *Minibuf-0*" " *nswbuff*")) 378 | 379 | (defun nswbuff-get-status-buffer () 380 | "Create or return the nswbuff status buffer." 381 | (if (buffer-live-p nswbuff-status-buffer) 382 | nswbuff-status-buffer 383 | (let ((buffer (get-buffer-create (nswbuff-status-buffer-name)))) 384 | (with-current-buffer buffer 385 | (set (make-local-variable 'face-remapping-alist) 386 | '((default nswbuff-default-face))) 387 | (setq cursor-type nil) 388 | (setq mode-line-format nswbuff-mode-line-format) 389 | (setq nswbuff-status-buffer (current-buffer)))))) 390 | 391 | (defun nswbuff-initialize () 392 | "Initialize nswbuff variables prior to a switch sequence." 393 | (setq nswbuff-buffer-list (nswbuff-buffer-list) 394 | nswbuff-initial-buffer-list nswbuff-buffer-list 395 | nswbuff-initial-buffer (car nswbuff-initial-buffer-list) 396 | nswbuff-initial-window (selected-window) 397 | nswbuff-initial-frame (selected-frame))) 398 | 399 | (defun nswbuff-kill-this-buffer () 400 | "Kill the current buffer but retain the status window. 401 | This function can be bound to a key in `nswbuff-override-map' to kill 402 | the current buffer without ending the buffer switching sequence." 403 | (interactive) 404 | (let ((dead-buffer nswbuff-current-buffer)) 405 | (if (condition-case nil (kill-buffer dead-buffer)) 406 | (progn 407 | (if nswbuff-initial-buffer 408 | (setq nswbuff-buffer-list 409 | (delq dead-buffer nswbuff-buffer-list) 410 | nswbuff-initial-buffer-list 411 | (delq dead-buffer nswbuff-initial-buffer-list)) 412 | (nswbuff-initialize)) 413 | ;; Update status info based on remaining buffer list 414 | (cond 415 | ;; Two or more buffers left 416 | ((cadr nswbuff-buffer-list) 417 | (nswbuff-previous-buffer) 418 | (nswbuff-show-status-window)) 419 | ;; Only one buffer let 420 | ((car nswbuff-buffer-list) 421 | (nswbuff-previous-buffer) 422 | (nswbuff-discard-status-window)) 423 | ;; No buffer left 424 | (t (nswbuff-discard-status-window)))) 425 | (nswbuff-discard-status-window)))) 426 | 427 | (defun nswbuff-buffer-list () 428 | "Return the list of switchable buffers. 429 | Buffers whose name matches `nswbuff-exclude-buffer-regexps' are 430 | excluded, unless they match one of the regular expressions in 431 | `include-buffer-regexps'. If `nswbuff-this-frame-only' is 432 | non-nil, buffers that are currently displayed in other visible or 433 | iconified frames are also excluded." 434 | (let ((blist (seq-filter (lambda (buf) 435 | (and (not (buffer-local-value 'nswbuff-exclude buf)) 436 | (not (nswbuff-exclude-mode-p buf)) 437 | (or (nswbuff-include-p (buffer-name buf)) 438 | (not (nswbuff-exclude-p (buffer-name buf)))) 439 | (not (and nswbuff-this-frame-only 440 | (nswbuff-in-other-frame-p buf))))) 441 | (or (and nswbuff-buffer-list-function 442 | (funcall nswbuff-buffer-list-function)) 443 | (buffer-list))))) 444 | (when blist 445 | ;; add the current buffer if it would normally be skipped 446 | (unless (memq (current-buffer) blist) 447 | (push (current-buffer) blist))) 448 | blist)) 449 | 450 | (defun nswbuff-window-lines () 451 | "Return the number of lines in the current buffer. 452 | This number may be greater than the number of actual lines in the 453 | buffer if any wrap on the display due to their length." 454 | (count-lines (point-min) (point-max))) 455 | 456 | (defun nswbuff-adjust-window (&optional text-height) 457 | "Adjust window height to fit its buffer contents. 458 | If optional TEXT-HEIGHT is non-nil adjust window height to this 459 | value." 460 | (setq text-height (max nswbuff-status-window-min-text-height 461 | (or text-height 462 | (nswbuff-window-lines)))) 463 | (if (fboundp 'set-window-text-height) 464 | (set-window-text-height nil text-height) 465 | (let ((height (window-height)) 466 | (lines (+ 2 text-height))) 467 | (enlarge-window (- lines height)))) 468 | (goto-char (point-min))) 469 | 470 | ;; Used to prevent discarding the status window on some mouse event. 471 | (defalias 'nswbuff-ignore 'ignore) 472 | 473 | (defun nswbuff-scroll-window (position) 474 | "Adjust horizontal scrolling to ensure that POSITION is visible." 475 | (setq truncate-lines t) 476 | (let ((auto-hscroll-mode t)) 477 | (goto-char position))) 478 | 479 | ;; Use mouse-1, mouse-3 on mode line buffer identification to 480 | ;; respectively switch to previous or next buffer. And mouse-2 to 481 | ;; kill the current buffer. 482 | (let ((map mode-line-buffer-identification-keymap)) 483 | (define-key map [mode-line mouse-1] 'nswbuff-switch-to-previous-buffer) 484 | (define-key map [mode-line drag-mouse-1] 'nswbuff-ignore) 485 | (define-key map [mode-line down-mouse-1] 'nswbuff-ignore) 486 | (define-key map [mode-line mouse-2] 'nswbuff-kill-this-buffer) 487 | (define-key map [mode-line mouse-3] 'nswbuff-switch-to-next-buffer)) 488 | 489 | (defun nswbuff-one-window-p (window) 490 | "Return non-nil if there is only one window in this frame. 491 | Ignore WINDOW and the minibuffer window." 492 | (let ((count 0)) 493 | (walk-windows #'(lambda (w) 494 | (or (eq w window) (setq count (1+ count))))) 495 | (= count 1))) 496 | 497 | (defvar nswbuff-buffer-list-holder nil 498 | "Hold the current displayed buffer list.") 499 | 500 | (defun nswbuff-layout-status-line (window bcurr) 501 | "Layout a status line in WINDOW current buffer. 502 | BCURR is the buffer name to highlight." 503 | (let* ((blist nswbuff-initial-buffer-list) 504 | (head (or nswbuff-header "" )) 505 | (separ (or nswbuff-separator " ")) 506 | (trail (or nswbuff-trailer "" )) 507 | (left (or nswbuff-left "" )) 508 | (right (or nswbuff-right "" )) 509 | (width (window-width window)) 510 | (lines 0) 511 | (adjust (or (eq nswbuff-status-window-layout 'adjust) 512 | (nswbuff-one-window-p window))) 513 | ;; Okay, it's crazy logic but it works. :-) 514 | (half-way (1- (/ 515 | (if (= (% (length blist) 2) 0) ;; If even ... 516 | (length blist) 517 | (1+ (length blist))) ;; make it even. 518 | 2))) 519 | start end buffer bname fillr) 520 | (when nswbuff-start-with-current-centered 521 | ;; Rearrange blist so that the first elt is in the middle 522 | (setq blist (append (last blist half-way) ;; last half 523 | (butlast blist half-way)))) ;; first half 524 | (save-selected-window 525 | (select-window window) 526 | (erase-buffer) 527 | (setq start (point)) 528 | (insert head) 529 | (if (> (point) start) 530 | (set-text-properties 531 | start (point) '(face nswbuff-separator-face))) 532 | (while blist 533 | (setq buffer (car blist) 534 | blist (cdr blist)) 535 | (when (buffer-live-p buffer) 536 | (setq bname (buffer-name buffer) 537 | start (point) 538 | fillr (if blist separ trail)) 539 | ;; Add a newline if we will run out of space. 540 | (when (and adjust 541 | (> (- (+ start (length bname) 542 | (length (concat left fillr right))) 543 | (* lines width)) 544 | width)) 545 | (newline) 546 | (setq start (point) 547 | lines (1+ lines))) 548 | (insert left) 549 | (if (> (point) start) 550 | (set-text-properties 551 | start (point) '(face nswbuff-separator-face))) 552 | (setq start (point)) 553 | (insert bname) 554 | ;; Highlight it if it is the current one. 555 | (cond 556 | ((string-equal bname bcurr) 557 | (setq end (point)) 558 | (set-text-properties 559 | start end '(face nswbuff-current-buffer-face))) 560 | ((and (not (string= nswbuff-special-buffers-re "")) 561 | (string-match-p nswbuff-special-buffers-re bname)) 562 | (set-text-properties 563 | start (point) '(face nswbuff-special-buffers-face))) 564 | (t 565 | (set-text-properties 566 | start (point) '(face nswbuff-default-face)))) 567 | (setq start (point)) 568 | (insert right) 569 | (if (> (point) start) 570 | (set-text-properties 571 | start (point) '(face nswbuff-separator-face))) 572 | (setq start (point)) 573 | (insert fillr) 574 | (if (> (point) start) 575 | (set-text-properties 576 | start (point) '(face nswbuff-separator-face))))) 577 | (if adjust 578 | (nswbuff-adjust-window) 579 | (nswbuff-adjust-window 1) 580 | (nswbuff-scroll-window end))))) 581 | 582 | (defun nswbuff-show-status-window () 583 | "Pop-up the nswbuff status window at the bottom of the selected window. 584 | The status window shows the list of switchable buffers where the 585 | switched one is highlighted using `nswbuff-current-buffer-face'. 586 | It is automatically discarded after any command is executed or 587 | after the delay specified by `nswbuff-clear-delay'." 588 | (if nswbuff-initial-buffer-list 589 | (let ((buffer-name (buffer-name nswbuff-current-buffer)) 590 | (window-min-height 1) 591 | (cursor-in-non-selected-windows nil)) 592 | (with-current-buffer (nswbuff-get-status-buffer) 593 | (let ((window (or (and (eq nswbuff-status-window-layout 'minibuffer) (minibuffer-window)) 594 | (get-buffer-window (nswbuff-status-buffer-name)) 595 | (if nswbuff-status-window-at-top 596 | (split-window nil (- nswbuff-status-window-min-text-height) 'above) 597 | (split-window-vertically (- nswbuff-status-window-min-text-height)))))) 598 | ;; If we forget this we may end up with multiple status 599 | ;; windows (kal). 600 | (setq nswbuff-status-window window) 601 | (set-window-buffer window (current-buffer)) 602 | (nswbuff-layout-status-line window buffer-name) 603 | (set-transient-map nswbuff-override-map nil #'nswbuff-maybe-discard-status-window) 604 | (if (timerp nswbuff-display-timer) 605 | (cancel-timer nswbuff-display-timer)) 606 | (setq nswbuff-display-timer 607 | (run-with-timer nswbuff-clear-delay nil 608 | #'nswbuff-discard-status-window))))) 609 | (nswbuff-discard-status-window) 610 | (message "No buffers eligible for switching."))) 611 | 612 | (defun nswbuff-in-other-frame-p (buffer) 613 | "Return non-nil if BUFFER is being displayed in another visible frame." 614 | (let ((found-in-other-frame nil) 615 | (window nil) 616 | (window-list (get-buffer-window-list buffer nil 0))) 617 | (while (and (setq window (car window-list)) 618 | (not found-in-other-frame)) 619 | (unless (eq (window-frame window) nswbuff-initial-frame) 620 | (setq found-in-other-frame t)) 621 | (pop window-list)) 622 | found-in-other-frame)) 623 | 624 | (defun nswbuff-exclude-mode-p (buffer) 625 | "Return non-nil if BUFFER should be excluded from the buffer list. 626 | This is the case if BUFFER's major mode matches one of the 627 | regexps in `nswbuff-exclude-mode-regexps'." 628 | (unless (string-equal "" nswbuff-exclude-mode-regexp) 629 | (with-current-buffer buffer 630 | (string-match-p nswbuff-exclude-mode-regexp 631 | (symbol-name major-mode))))) 632 | 633 | (defun nswbuff-exclude-p (buffer) 634 | "Return non-nil if BUFFER should be excluded from the buffer list. 635 | BUFFER should be a buffer name. It is tested against the regular expressions in 636 | `nswbuff-exclude-buffer-regexps', and if one matches, BUFFER is excluded." 637 | (let ((rl (cons (regexp-quote (nswbuff-status-buffer-name)) 638 | (delete "" nswbuff-exclude-buffer-regexps)))) 639 | (while (and rl (car rl) (not (string-match-p (car rl) buffer))) 640 | (setq rl (cdr rl))) 641 | (not (null rl)))) 642 | 643 | (defun nswbuff-include-p (name) 644 | "Return non-nil if buffer NAME can be included in the buffer list. 645 | BUFFER should be a buffer name. It is tested against the regular expressions in 646 | `nswbuff-include-buffer-regexps', and if one matches, BUFFER is included." 647 | (let ((rl (delete "" nswbuff-include-buffer-regexps))) 648 | (while (and rl (car rl) (not (string-match-p (car rl) name))) 649 | (setq rl (cdr rl))) 650 | (not (null rl)))) 651 | 652 | (defun nswbuff-maybe-discard-status-window () 653 | "Discard the status window conditionally." 654 | (when (eq (selected-frame) nswbuff-initial-frame) 655 | (if (timerp nswbuff-display-timer) 656 | (cancel-timer nswbuff-display-timer)) 657 | (setq nswbuff-display-timer nil) 658 | (cond 659 | ;; If this-command is a command bound in `nswbuff-override-map', we renew the 660 | ;; timer and the map. 661 | ((where-is-internal this-command (list nswbuff-override-map)) 662 | (setq nswbuff-display-timer 663 | (run-with-timer nswbuff-clear-delay nil 664 | #'nswbuff-discard-status-window)) 665 | (set-transient-map nswbuff-override-map nil #'nswbuff-maybe-discard-status-window)) 666 | ;; If this-command is a buffer-switching command, we do nothing. 667 | ((memq this-command '(nswbuff-switch-to-previous-buffer 668 | nswbuff-switch-to-next-buffer 669 | nswbuff-ignore)) 670 | t) 671 | ;; If this-command is anything else, we discard the status window. 672 | (t (nswbuff-discard-status-window))))) 673 | 674 | (defun nswbuff-discard-status-window () 675 | "Discard the status window. 676 | This function is called directly by the nswbuff timer." 677 | (let ((buffer (get-buffer (nswbuff-status-buffer-name))) 678 | (buffer-list (nreverse nswbuff-buffer-list))) 679 | ;; Cleanup status window and status buffer 680 | (if (eq nswbuff-status-window-layout 'minibuffer) 681 | (with-current-buffer buffer (erase-buffer)) 682 | (when (window-live-p nswbuff-status-window) 683 | (delete-window nswbuff-status-window)) 684 | (when buffer 685 | (kill-buffer buffer))) 686 | (unwind-protect 687 | (when (and nswbuff-initial-buffer nswbuff-current-buffer) 688 | (save-window-excursion 689 | ;; Because this may be called from a timer we have to be really 690 | ;; careful that we are in the right frame, window and buffer at that 691 | ;; time --- other timers (e.g., those called by speedbar) may put us 692 | ;; elsewhere. :-) 693 | (select-frame nswbuff-initial-frame) 694 | (select-window nswbuff-initial-window) 695 | ;; Reset visit order to what it was before the sequence began. 696 | (while (setq buffer (car buffer-list)) 697 | (switch-to-buffer buffer) 698 | (setq buffer-list (cdr buffer-list)))) 699 | ;; Then switch between the first and last buffers in the sequence. 700 | (and nswbuff-initial-buffer 701 | (switch-to-buffer nswbuff-initial-buffer)) 702 | (and nswbuff-current-buffer 703 | (switch-to-buffer nswbuff-current-buffer))) 704 | ;; Protect forms. 705 | (setq nswbuff-initial-buffer nil 706 | nswbuff-initial-buffer-list nil 707 | nswbuff-current-buffer nil 708 | nswbuff-initial-frame nil 709 | nswbuff-initial-window nil 710 | nswbuff-status-window nil)))) 711 | 712 | (defun nswbuff-previous-buffer () 713 | "Display and activate the buffer at the end of the buffer list." 714 | (let ((buf (car (last nswbuff-buffer-list)))) 715 | (when buf 716 | (when nswbuff-display-intermediate-buffers 717 | (switch-to-buffer buf t)) 718 | (setq nswbuff-current-buffer buf) 719 | (setq nswbuff-buffer-list (butlast nswbuff-buffer-list)) 720 | (setq nswbuff-buffer-list (cons buf nswbuff-buffer-list))))) 721 | 722 | (defun nswbuff-next-buffer () 723 | "Display and activate the next buffer in the buffer list." 724 | (let ((buf (car nswbuff-buffer-list))) 725 | (when buf 726 | (setq nswbuff-buffer-list (cdr nswbuff-buffer-list)) 727 | (setq nswbuff-buffer-list (append nswbuff-buffer-list (list buf))) 728 | (setq nswbuff-current-buffer (car nswbuff-buffer-list)) 729 | (when nswbuff-display-intermediate-buffers 730 | (switch-to-buffer (car nswbuff-buffer-list) t))))) ;; no record 731 | 732 | ;;; Commands 733 | 734 | ;;;###autoload 735 | (defun nswbuff-switch-to-previous-buffer () 736 | "Switch to the previous buffer in the buffer list." 737 | (interactive) 738 | (run-hooks 'nswbuff-pre-switch-hook) 739 | (if nswbuff-initial-buffer 740 | (and nswbuff-delay-switch (nswbuff-previous-buffer)) 741 | (nswbuff-initialize)) 742 | (or nswbuff-delay-switch (nswbuff-previous-buffer)) 743 | (nswbuff-show-status-window)) 744 | 745 | ;;;###autoload 746 | (defun nswbuff-switch-to-next-buffer () 747 | "Switch to the next buffer in the buffer list." 748 | (interactive) 749 | (run-hooks 'nswbuff-pre-switch-hook) 750 | (if nswbuff-initial-buffer 751 | (nswbuff-next-buffer) 752 | ;; First call in the sequence. 753 | (nswbuff-initialize) 754 | (unless nswbuff-delay-switch 755 | (nswbuff-next-buffer))) 756 | (nswbuff-show-status-window)) 757 | 758 | (provide 'nswbuff) 759 | 760 | ;;; nswbuff.el ends here 761 | -------------------------------------------------------------------------------- /test/nswbuff-test.el: -------------------------------------------------------------------------------- 1 | ;;; nswbuff-test.el --- Tests for nswbuff 2 | 3 | ;;; nswbuff-test.el ends here 4 | -------------------------------------------------------------------------------- /test/test-helper.el: -------------------------------------------------------------------------------- 1 | ;;; test-helper.el --- Helpers for nswbuff-test.el 2 | 3 | ;;; test-helper.el ends here 4 | --------------------------------------------------------------------------------