├── screenshots ├── tab-notes.png └── tab-emacsd.png ├── README.org ├── LICENSE └── tabspaces.el /screenshots/tab-notes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mclear-tools/tabspaces/HEAD/screenshots/tab-notes.png -------------------------------------------------------------------------------- /screenshots/tab-emacsd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mclear-tools/tabspaces/HEAD/screenshots/tab-emacsd.png -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | #+title: Tabspaces 2 | #+author: Colin McLear 3 | #+language: en 4 | #+export_file_name: tabspaces.texi 5 | #+texinfo_filename: tabspaces.info 6 | #+texinfo_dir_category: Emacs 7 | #+texinfo_dir_title: Tabspaces: (tabspaces). 8 | #+texinfo_dir_desc: Tabbed workspaces using tab-bar and project.el 9 | 10 | #+html: GNU Emacs 11 | #+html: MELPA 12 | #+html: Buy Me A Coffee 13 | 14 | 15 | Tabspaces leverages [[https://github.com/emacs-mirror/emacs/blob/master/lisp/tab-bar.el][tab-bar.el]] and [[https://github.com/emacs-mirror/emacs/blob/master/lisp/progmodes/project.el][project.el]] (both built into emacs 27+) to 16 | create buffer-isolated workspaces (or "tabspaces") that also integrate with your 17 | version-controlled projects. It should work with emacs 27+. It is tested to work 18 | with a single frame workflow, but should work with multiple frames as well. 19 | 20 | While other great packages exist for managing workspaces, such as [[https://github.com/alphapapa/bufler.el][bufler]], 21 | [[https://github.com/nex3/perspective-el][perspective]] and [[https://github.com/Bad-ptr/persp-mode.el][persp-mode]], this package is less complex than those alternatives, and works 22 | entirely based on the built-in (to emacs 27+) tab-bar and project packages. If 23 | you like simple, this may be the workspace package for you. That said, bufler, 24 | perspective or persp-mode, etc. may better fit your needs. 25 | 26 | *NOTE*: version 1.2 renames several functions and streamlines tab and project 27 | creation. Apologies if this breaks your workflow. Please update your configuration accordingly. 28 | 29 | ** Basic Usage 30 | 31 | Calling the minor-mode =tabspaces-mode= sets up newly created tabs as 32 | buffer-isolated workspaces using =tab.el= in the background. Calling 33 | =tabspaces-mode= does not itself create a new tabbed workspace. 34 | 35 | Switch or create workspace via =tabspaces-switch-or-create-workspace=. Close a 36 | workspace by invoking =tabspaces-close-workspace=. Note that these two functions 37 | are simply wrappers around native =tab-bar= commands. You can close a workspace 38 | and /kill/ all buffers associated with it using 39 | =tabspaces-kill-buffers-close-workspace=. 40 | 41 | Open an existing version-controlled project in its own workspace using 42 | =tabspaces-open-or-create-project-and-workspace=. If no such project exists it 43 | will then create one in its own workspace for you. 44 | 45 | See workspace buffers using =tabspaces-switch-to-buffer= (for =consult= integration see 46 | below), which will only show buffers in the workspace (but list-buffers, 47 | ibuffer, etc. will show all buffers). Setting 48 | =tabspaces-use-filtered-buffers-as-default= to =t= remaps =switch-to-buffer= to 49 | =tabspaces-switch-to-buffer=. 50 | 51 | Adding buffers to a workspace is as simple as opening the buffer in 52 | the workspace. Delete buffers from a workspace either by killing them or using 53 | one of either =tabspaces-remove-selected-buffer= or 54 | =tabspaces-remove-current-buffer=. Removed buffers are still available from the 55 | default tabspace unless the variable =tabspaces-remove-to-default= is set to =nil=. 56 | 57 | *NOTE* that other than tabbed buffer isolation for all created window tabs this 58 | package does not modify =tab-bar=, =tab-line=, or =project= in any way. It simply adds 59 | convenience functions for use with those packages. So it is still up to the user 60 | to configure tabs, etc., however they like. 61 | 62 | Here are some screenshots of tabspaces (with my [[https://github.com/Lambda-Emacs/lambda-themes][lambda-themes]]) and using =consult-buffer= (see below for instructions on that setup). You can see the workspace isolated buffers in each and the tabs at top: 63 | 64 | #+ATTR_HTML: :width 85% 65 | [[file:screenshots/tab-notes.png]] 66 | #+ATTR_HTML: :width 85% 67 | [[file:screenshots/tab-emacsd.png]] 68 | 69 | ** Installation 70 | 71 | You may install this package either from Melpa (=M-x package-install tabspaces 72 | RET=) or by cloning this repo and adding it to your load-path. 73 | 74 | ** Setup 75 | 76 | Here's one possible way of setting up the package using [[https://github.com/jwiegley/use-package][use-package]] (and 77 | [[https://github.com/raxod502/straight.el][straight]], if you use that). 78 | 79 | #+begin_src emacs-lisp 80 | (use-package tabspaces 81 | ;; use this next line only if you also use straight, otherwise ignore it. 82 | :straight (:type git :host github :repo "mclear-tools/tabspaces") 83 | :hook (after-init . tabspaces-mode) ;; use this only if you want the minor-mode loaded at startup. 84 | :commands (tabspaces-switch-or-create-workspace 85 | tabspaces-open-or-create-project-and-workspace) 86 | :custom 87 | (tabspaces-use-filtered-buffers-as-default t) 88 | (tabspaces-default-tab "Default") 89 | (tabspaces-remove-to-default t) 90 | (tabspaces-include-buffers '("*scratch*")) 91 | (tabspaces-initialize-project-with-todo t) 92 | (tabspaces-todo-file-name "project-todo.org") 93 | ;; sessions 94 | (tabspaces-session t) 95 | (tabspaces-session-auto-restore t) 96 | ;; additional options 97 | (tabspaces-fully-resolve-paths t) ; Resolve relative project paths to absolute 98 | (tabspaces-exclude-buffers '("*Messages*" "*Compile-Log*")) ; Additional buffers to exclude 99 | (tab-bar-new-tab-choice "*scratch*")) 100 | #+end_src 101 | 102 | Note the inclusion of the `tab-bar` setting, which is built-in to Emacs and allows a number of different options for what buffer to set for a newly created tab. 103 | 104 | *** Keybindings 105 | Workspace Keybindings are defined in the following variable: 106 | 107 | #+begin_src emacs-lisp 108 | (defvar tabspaces-command-map 109 | (let ((map (make-sparse-keymap))) 110 | (define-key map (kbd "C") 'tabspaces-clear-buffers) 111 | (define-key map (kbd "b") 'tabspaces-switch-to-buffer) 112 | (define-key map (kbd "d") 'tabspaces-close-workspace) 113 | (define-key map (kbd "k") 'tabspaces-kill-buffers-close-workspace) 114 | (define-key map (kbd "o") 'tabspaces-open-or-create-project-and-workspace) 115 | (define-key map (kbd "r") 'tabspaces-remove-current-buffer) 116 | (define-key map (kbd "R") 'tabspaces-remove-selected-buffer) 117 | (define-key map (kbd "s") 'tabspaces-switch-or-create-workspace) 118 | (define-key map (kbd "t") 'tabspaces-switch-buffer-and-tab) 119 | (define-key map (kbd "w") 'tabspaces-show-workspaces) 120 | (define-key map (kbd "T") 'tabspaces-toggle-echo-area-display) 121 | map) 122 | "Keymap for tabspace/workspace commands after `tabspaces-keymap-prefix'.") 123 | #+end_src 124 | 125 | The variable =tabspaces-keymap-prefix= sets a key prefix (default is =C-c TAB=) for 126 | the keymap, but this can be changed to anything the user prefers. Set it to =nil= 127 | to disable automatic keymap binding entirely. 128 | 129 | *Note on key conflicts:* If you want to use =C-x TAB= as the prefix, be aware that 130 | in terminal Emacs, =TAB= and =C-i= are indistinguishable. However, in GUI Emacs, 131 | =C-x TAB= and =C-x C-i= are separate keybindings, so you can use =C-x TAB= for 132 | tabspaces while keeping =C-x C-i= for =indent-rigidly=. 133 | 134 | *** Buffer Filtering 135 | 136 | When =tabspaces-mode= is enabled use =tabspaces-switch-to-buffer= to choose from a 137 | filtered list of only those buffers in the current tab/workspace. Though =nil= by 138 | default, when =tabspaces-use-filtered-buffers-as-default= is set to =t= and 139 | =tabspaces-mode= is enabled, =switch-to-buffer= is globally remapped to 140 | =tabspaces-switch-to-buffer=, and thus only shows those buffers in the current 141 | workspace. For use with =consult-buffer=, see below. 142 | 143 | *** Switch Tabs via Buffer 144 | 145 | Sometimes the user may wish to switch to some open buffer in a tabspace and switch to that tab as well. Use =(=tabspaces-switch-buffer-and-tab=) to achieve this. If the buffer is open in more than one tabspace the user will be prompted to choose which tab to switch to. If there is no such buffer user will be prompted on whether to create it in a new tabspace or the current one. 146 | 147 | *** Tabs & Projects 148 | 149 | The =tabspaces-open-or-create-project-and-workspace= function provides a 150 | versatile way to manage projects and their associated workspaces in 151 | Emacs. Here's what you can do with it: 152 | 153 | 1. *Open Existing Projects*: Open an existing version-controlled project 154 | in its own workspace. The function will switch to the project's tab 155 | if it already exists. 156 | 157 | 2. *Create New Projects*: If no such project exists at the specified 158 | path, it will create one in its own workspace for you, initializing 159 | version control (git or other VCS) in the process. 160 | 161 | 3. *Descriptive Tab Naming*: 162 | 163 | - Tabs are named descriptively based on the project structure. 164 | - In case of naming conflicts, it intelligently renames tabs to avoid 165 | confusion. 166 | 167 | 4. *Multiple Tabs for the Same Project*: 168 | 169 | - By using a universal argument (C-u) before calling the function, 170 | you can force the creation of a new tab even for already open project tabs. 171 | - The first tab will have the original project name. 172 | - Subsequent tabs will be automatically named with incrementing 173 | numbers (e.g., "ProjectName<2>", "ProjectName<3>"). 174 | - This is useful when you want to work on different aspects of the 175 | same project in separate workspaces. 176 | 177 | 178 | *** Persistent Tabspaces 179 | 180 | Tabspaces provides basic functionality to save and restore both global (all 181 | tabspaces) and project-specific tabspace sessions. These sessions store: 182 | 183 | - Open file-visiting buffers in each tab 184 | - Window configurations (splits, sizes, buffer positions) 185 | 186 | **** Configuration 187 | 188 | By default, project sessions are stored in their respective project root 189 | directories as hidden files (e.g. =.{project-basename}-tabspaces-session.el=). So for a project at =/home/user/myproject/=, the session file would be =.myproject-tabspaces-session.el=. 190 | You can configure where project sessions are stored using 191 | =tabspaces-session-project-session-store=: 192 | 193 | #+begin_src elisp 194 | ;; Store in project directories (default) 195 | (setq tabspaces-session-project-session-store 'project) 196 | 197 | ;; Store all project sessions in a specific directory 198 | (setq tabspaces-session-project-session-store "~/.emacs.d/tabspaces-sessions/") 199 | 200 | ;; Use a custom function to determine location 201 | (setq tabspaces-session-project-session-store 202 | (lambda (project-root) 203 | (expand-file-name 204 | (concat "sessions/" (file-name-nondirectory project-root) "-tabspaces-session.el") 205 | project-root))) 206 | #+end_src 207 | 208 | The /global/ session file location is controlled by 209 | =tabspaces-session-file= (defaults to =~/.emacs.d/tabsession.el=). 210 | 211 | **** Usage 212 | :PROPERTIES: 213 | :CUSTOM_ID: usage 214 | :END: 215 | ***** Global Sessions 216 | :PROPERTIES: 217 | :CUSTOM_ID: global-sessions 218 | :END: 219 | Save all tabs and their configurations: 220 | 221 | #+begin_src elisp 222 | M-x tabspaces-save-session 223 | #+end_src 224 | 225 | Restore saved global session: 226 | 227 | #+begin_src elisp 228 | M-x tabspaces-restore-session 229 | #+end_src 230 | 231 | ***** Project Sessions 232 | :PROPERTIES: 233 | :CUSTOM_ID: project-sessions 234 | :END: 235 | Save current project tab and its configuration: 236 | 237 | #+begin_src elisp 238 | M-x tabspaces-save-current-project-session 239 | #+end_src 240 | 241 | Restore sessions contextually: 242 | 243 | #+begin_src elisp 244 | ;; When in a project tab (with per-project storage enabled): 245 | M-x tabspaces-restore-session ; Restores current project's session 246 | 247 | ;; When in a non-project tab: 248 | M-x tabspaces-restore-session ; Restores global session 249 | 250 | ;; Explicitly restore a specific project: 251 | (tabspaces-restore-session "/path/to/project") 252 | #+end_src 253 | 254 | ***** Automatic Session Handling 255 | :PROPERTIES: 256 | :CUSTOM_ID: automatic-session-handling 257 | :END: 258 | Enable automatic session saving on Emacs exit: 259 | 260 | #+begin_src elisp 261 | (setq tabspaces-session t) ; Save sessions automatically 262 | #+end_src 263 | 264 | Control automatic session restoration: 265 | 266 | #+begin_src elisp 267 | ;; Auto-restore sessions on startup and when opening projects 268 | (setq tabspaces-session-auto-restore t) 269 | 270 | ;; Disable auto-restore (sessions must be manually restored) 271 | (setq tabspaces-session-auto-restore nil) 272 | #+end_src 273 | 274 | When =tabspaces-session-auto-restore= is =t=: 275 | - Global sessions are restored on Emacs startup 276 | - Project sessions are restored when opening projects (if per-project storage is enabled) 277 | 278 | When =tabspaces-session-auto-restore= is =nil=: 279 | - No automatic restoration occurs 280 | - Use =M-x tabspaces-restore-session= to manually restore sessions 281 | - The command is context-aware: restores project session when in a project tab, global session otherwise 282 | 283 | 284 | Session support for saving tabspaces across Emacs sessions has been implemented. Setting =tabspaces-session= to =t= ensures that all open tabspaces and file-visiting buffers are saved. Sessions can be restored interactively via =(tabspaces-restore-session)=, which is context-aware and will restore the current project's session when called from a project tab, or the global session otherwise. Automatic restoration is controlled by =tabspaces-session-auto-restore=: when set to =t=, sessions are restored on startup and when opening projects; when =nil=, all restoration is manual. Project sessions can be saved individually via =(tabspaces-save-current-project-session)= and are stored either in project directories or a central location based on =tabspaces-session-project-session-store=. 285 | 286 | ***** Advanced Session Management 287 | 288 | For more granular control over session management, additional functions are available: 289 | 290 | #+begin_src elisp 291 | ;; Save all project tabs to their individual session files 292 | (tabspaces-save-all-project-sessions) 293 | 294 | ;; Save only non-project tabs to the global session file 295 | (tabspaces-save-non-project-tabs) 296 | #+end_src 297 | 298 | These functions are particularly useful when using per-project session storage mode, 299 | allowing you to selectively save different types of workspaces. 300 | 301 | *** Additional Customization 302 | 303 | **** Echo Area Display 304 | 305 | Tabspaces can optionally display tabs in the echo area (bottom of the frame) instead of the top tab-bar. This feature provides a less visually prominent way to show workspace information. 306 | 307 | #+begin_src emacs-lisp 308 | ;; Enable echo area tab display 309 | (setq tabspaces-echo-area-enable t) 310 | 311 | ;; Customize idle delay (default is 1.0 seconds) 312 | (setq tabspaces-echo-area-idle-delay 5.0) 313 | 314 | ;; Customize the format function (advanced users) 315 | (setq tabspaces-echo-area-format-function #'my-custom-tab-formatter) 316 | 317 | ;; Toggle echo area display (bound to C-c TAB T by default) 318 | (tabspaces-toggle-echo-area-display) 319 | 320 | ;; Troubleshooting functions 321 | (tabspaces-restart-idle-timer) ; Restart the idle timer if display stops working 322 | (tabspaces-echo-area-timer-status) ; Check timer status for debugging 323 | #+end_src 324 | 325 | When enabled, this feature: 326 | - Hides the visual tab-bar at the top of the frame 327 | - Shows formatted tabs in the echo area after the configured idle time 328 | - Does not displace other messages or minibuffer content 329 | - Filters duplicate messages from the =*Messages*= buffer to reduce clutter 330 | - Maintains compatibility with existing tab-bar formatters and themes 331 | - Respects your existing tab-bar format configuration 332 | 333 | The echo area display respects your existing tab formatting configuration and works seamlessly with features like SF Symbols for tab numbering. 334 | 335 | You can also display workspaces in the echo area on demand using: 336 | #+begin_src emacs-lisp 337 | ;; Show workspaces immediately (bound to C-c TAB w by default) 338 | (tabspaces-show-workspaces) 339 | #+end_src 340 | 341 | This command shows all workspaces in the echo area without waiting for idle time or enabling the automatic display feature. 342 | 343 | **** Buffer Management Integration 344 | 345 | Tabspaces provides a minimal API for integrating with buffer management and completion frameworks. Rather than implementing tool-specific features, the package exposes workspace primitives that work with any completion system or buffer management tool. 346 | 347 | *Integration API:* 348 | 349 | The following functions form the public integration points: 350 | 351 | - =(tabspaces--list-tabspaces)= - Returns list of all workspace names 352 | - =(tabspaces--buffer-list &optional frame tabnum)= - Returns buffers for a workspace 353 | - With no arguments: buffers in current workspace 354 | - With =tabnum=: buffers in specific workspace by index 355 | - =(tabspaces--current-tab-name)= - Returns current workspace name 356 | - =(tabspaces--local-buffer-p buffer)= - Predicate testing if buffer belongs to current workspace 357 | 358 | These functions use the =--= prefix (typically indicating internal functions) but are autoloaded and form the stable integration API. Below are integration examples for popular frameworks. 359 | 360 | ***** Consult 361 | 362 | If you have [[https://github.com/minad/consult][consult]] installed you can implement the following to have workspace buffers in =consult-buffer=: 363 | 364 | #+begin_src emacs-lisp 365 | ;; Filter Buffers for Consult-Buffer 366 | 367 | (with-eval-after-load 'consult 368 | ;; hide full buffer list (still available with "b" prefix) 369 | (consult-customize consult--source-buffer :hidden t :default nil) 370 | ;; set consult-workspace buffer list 371 | (defvar consult--source-workspace 372 | (list :name "Workspace Buffers" 373 | :narrow ?w 374 | :history 'buffer-name-history 375 | :category 'buffer 376 | :state #'consult--buffer-state 377 | :default t 378 | :items (lambda () (consult--buffer-query 379 | :predicate #'tabspaces--local-buffer-p 380 | :sort 'visibility 381 | :as #'buffer-name))) 382 | 383 | "Set workspace buffer list for consult-buffer.") 384 | (add-to-list 'consult-buffer-sources 'consult--source-workspace)) 385 | #+end_src 386 | 387 | This seamlessly integrates workspace buffers into =consult-buffer=, displaying workspace buffers by default and all buffers when narrowing using "b". Note that you can also see all project related buffers and files just by narrowing with "p" in [[https://github.com/minad/consult#configuration][a default consult setup]]. 388 | 389 | *NOTE*: If you typically toggle between having =tabspaces-mode= active and inactive, you may want to include a hook function to turn off the =consult--source-workspace= and modify the visibility of =consult--source-buffer=: 390 | 391 | #+begin_src emacs-lisp 392 | (defun my--consult-tabspaces () 393 | "Deactivate isolated buffers when not using tabspaces." 394 | (require 'consult) 395 | (cond (tabspaces-mode 396 | ;; hide full buffer list (still available with "b") 397 | (consult-customize consult--source-buffer :hidden t :default nil) 398 | (add-to-list 'consult-buffer-sources 'consult--source-workspace)) 399 | (t 400 | ;; reset consult-buffer to show all buffers 401 | (consult-customize consult--source-buffer :hidden nil :default t) 402 | (setq consult-buffer-sources (remove #'consult--source-workspace consult-buffer-sources))))) 403 | 404 | (add-hook 'tabspaces-mode-hook #'my--consult-tabspaces) 405 | #+end_src 406 | 407 | ***** Ivy 408 | 409 | If you use ivy you can use this function to limit your buffer search to only those in the tabspace: 410 | 411 | #+begin_src emacs-lisp 412 | (defun tabspaces-ivy-switch-buffer (buffer) 413 | "Display the local buffer BUFFER in the selected window. 414 | This is the frame/tab-local equivilant to `switch-to-buffer'." 415 | (interactive 416 | (list 417 | (let ((blst (mapcar #'buffer-name (tabspaces--buffer-list)))) 418 | (read-buffer 419 | "Switch to local buffer: " blst nil 420 | (lambda (b) (member (if (stringp b) b (car b)) blst)))))) 421 | (ivy-switch-buffer buffer)) 422 | #+end_src 423 | 424 | Alternatively, you may use the following function, which is basically a clone of =ivy-switch-buffer= (and thus uses ivy's own implementation framework), but with an additional predicate that only allows showing buffers from the current tabspace: 425 | 426 | #+begin_src emacs-lisp 427 | (defun tabspaces-ivy-switch-buffer () 428 | "Switch to another buffer in the current tabspace." 429 | (interactive) 430 | (ivy-read "Switch to buffer: " #'internal-complete-buffer 431 | :predicate (when (tabspaces--current-tab-name) 432 | (let ((local-buffers (tabspaces--buffer-list))) 433 | (lambda (name-and-buffer) 434 | (member (cdr name-and-buffer) local-buffers)))) 435 | :keymap ivy-switch-buffer-map 436 | :preselect (buffer-name (other-buffer (current-buffer))) 437 | :action #'ivy--switch-buffer-action 438 | :matcher #'ivy--switch-buffer-matcher 439 | :caller 'ivy-switch-buffer)) 440 | #+end_src 441 | 442 | ***** ibuffer 443 | 444 | To integrate with ibuffer, use hooks to create workspace-based filter groups: 445 | 446 | #+begin_src emacs-lisp 447 | (defun my-tabspaces-ibuffer-group () 448 | "Group ibuffer entries by tabspace." 449 | (setq ibuffer-filter-groups 450 | (mapcar (lambda (tab) 451 | (let ((tab-index (tab-bar--tab-index-by-name tab))) 452 | (cons tab 453 | `((predicate . (member (buffer-name) 454 | (mapcar #'buffer-name 455 | (tabspaces--buffer-list nil ,tab-index)))))))) 456 | (tabspaces--list-tabspaces)))) 457 | 458 | (add-hook 'ibuffer-hook #'my-tabspaces-ibuffer-group) 459 | #+end_src 460 | 461 | This automatically organizes ibuffer by workspace whenever you open it. You can customize the grouping logic by modifying the filter predicate. 462 | 463 | **** Included Buffers 464 | 465 | By default the =*scratch*= buffer is included in all workspaces. You can modify 466 | which buffers are included by default by changing the value of 467 | =tabspaces-include-buffers=. 468 | 469 | If you want emacs to startup with a set of initial buffers in a workspace 470 | (something I find works well) you could do something like the following: 471 | 472 | #+begin_src emacs-lisp 473 | (defun my--tabspace-setup () 474 | "Set up tabspace at startup." 475 | ;; Add *Messages* and *splash* to Tab \`Home\' 476 | (tabspaces-mode 1) 477 | (progn 478 | (tab-bar-rename-tab "Home") 479 | (when (get-buffer "*Messages*") 480 | (set-frame-parameter nil 481 | 'buffer-list 482 | (cons (get-buffer "*Messages*") 483 | (frame-parameter nil 'buffer-list)))) 484 | (when (get-buffer "*splash*") 485 | (set-frame-parameter nil 486 | 'buffer-list 487 | (cons (get-buffer "*splash*") 488 | (frame-parameter nil 'buffer-list)))))) 489 | 490 | (add-hook 'after-init-hook #'my--tabspace-setup) 491 | #+end_src 492 | 493 | **** File Per Project 494 | 495 | By default Tabspaces will create a =project-todo.org= file at the root of the project 496 | when creating a new workspace using =tabspaces-open-or-create-project-and-workspace=. 497 | 498 | Use =tabspaces-todo-file-name= to change the name of that file, or =tabspaces-initialize-project-with-todo= 499 | to disable this feature completely. 500 | 501 | 502 | ** Acknowledgments 503 | Code for this package is derived from, or inspired by, a variety of sources. 504 | These include: 505 | 506 | - The original buffer filter function 507 | + https://www.rousette.org.uk/archives/using-the-tab-bar-in-emacs/ 508 | + https://github.com/wamei/elscreen-separate-buffer-list/issues/8 509 | + https://github.com/kaz-yos/emacs 510 | - Buffer filtering and removal 511 | + https://github.com/florommel/bufferlo 512 | - Consult integration 513 | + https://github.com/minad/consult#multiple-sources 514 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /tabspaces.el: -------------------------------------------------------------------------------- 1 | ;;; tabspaces.el --- Leverage tab-bar and project for buffer-isolated workspaces -*- lexical-binding: t -*- 2 | 3 | ;; Author: Colin McLear 4 | ;; Maintainer: Colin McLear 5 | ;; Version: 1.7 6 | ;; Package-Requires: ((emacs "27.1") (project "0.8.1")) 7 | ;; Keywords: convenience, frames 8 | ;; Homepage: https://github.com/mclear-tools/tabspaces 9 | 10 | ;; Copyright (C) 2022 Colin McLear 11 | 12 | ;; This file is not part of GNU Emacs 13 | 14 | ;; This program is free software: you can redistribute it and/or modify 15 | ;; it under the terms of the GNU General Public License as published by 16 | ;; the Free Software Foundation, either version 3 of the License, or 17 | ;; (at your option) any later version. 18 | 19 | ;; This program is distributed in the hope that it will be useful, 20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 | ;; GNU General Public License for more details. 23 | 24 | ;; You should have received a copy of the GNU General Public License 25 | ;; along with this program. If not, see . 26 | 27 | ;;; Commentary: 28 | 29 | ;; This package provides several functions to facilitate a frame-based 30 | ;; tab workflow with one workspace per tab, integration with project.el (for 31 | ;; project-based workspaces) and buffer isolation per tab (i.e. a "tabspace" 32 | ;; workspace). The package assumes project.el and tab-bar.el are both present 33 | ;; (they are built-in to Emacs 27.1+). 34 | 35 | ;; This file is not part of GNU Emacs. 36 | 37 | ;;; Acknowledgements 38 | ;; Much of the package code is inspired by: 39 | 40 | ;; - https://github.com/kaz-yos/emacs 41 | ;; - https://github.com/wamei/elscreen-separate-buffer-list/issues/8 42 | ;; - https://www.rousette.org.uk/archives/using-the-tab-bar-in-emacs/ 43 | ;; - https://github.com/minad/consult#multiple-sources 44 | ;; - https://github.com/florommel/bufferlo 45 | 46 | ;;; Code: 47 | 48 | ;;;; Requirements 49 | 50 | (require 'tab-bar) 51 | (require 'project) 52 | (require 'vc) 53 | (require 'seq) 54 | (require 'cl-lib) 55 | (require 'dired-x) 56 | 57 | (declare-function magit-init "magit-status") 58 | (declare-function magit-status-setup-buffer "magit-status") 59 | 60 | ;;;; Variables 61 | 62 | (defgroup tabspaces nil 63 | "Manage tab/workspace buffers." 64 | :group 'convenience) 65 | 66 | (defcustom tabspaces-default-tab "Default" 67 | "Specify a default tab by name TAB." 68 | :group 'tabspaces 69 | :type 'string) 70 | 71 | (defcustom tabspaces-remove-to-default t 72 | "Add buffer to default tabspace when removed from current tabspace." 73 | :group 'tabspaces 74 | :type 'boolean) 75 | 76 | (defcustom tabspaces-include-buffers '("*scratch*") 77 | "Buffers that should always get included in a new tab or frame. 78 | This is a list of regular expressions that match buffer names, 79 | which overrides buffers excluded by `tabspaces-exclude-buffers'." 80 | :group 'tabspaces 81 | :type '(repeat string)) 82 | 83 | (defcustom tabspaces-exclude-buffers nil 84 | "Buffers that should always get excluded in a new tab or frame. 85 | This is a list of regular expressions that match buffer names, 86 | which does not override buffers inside `tabspaces-include-buffers'." 87 | :group 'tabspaces 88 | :type '(repeat string)) 89 | 90 | (defcustom tabspaces-use-filtered-buffers-as-default nil 91 | "When t, remap `switch-to-buffer' to `tabspaces-switch-to-buffer'." 92 | :group 'tabspaces 93 | :type 'boolean) 94 | 95 | (defcustom tabspaces-keymap-prefix "C-c TAB" 96 | "Key prefix for the tabspaces-prefix-map keymap. 97 | Set to nil to disable automatic keymap binding." 98 | :group 'tabspaces 99 | :type '(choice (const :tag "Disabled" nil) 100 | string)) 101 | 102 | (defcustom tabspaces-initialize-project-with-todo t 103 | "When Non-nil create a `tabspaces-todo-file-name' file in the project 104 | when creating a workspace for it." 105 | :group 'tabspaces 106 | :type 'boolean) 107 | 108 | (defcustom tabspaces-todo-file-name "project-todo.org" 109 | "The name of the TODO file to create if non-existing for new workspaces." 110 | :group 'tabspaces 111 | :type 'string) 112 | 113 | (defcustom tabspaces-project-switch-commands project-switch-commands 114 | "Available commands when switch between projects. 115 | Change this value if you wish to run a specific command, such as 116 | `find-file' on project switch. Otherwise this will default to 117 | the value of `project-switch-commands'." 118 | :group 'tabspaces 119 | :type 'sexp) 120 | 121 | (defcustom tabspaces-fully-resolve-paths nil 122 | "Resolve \".\", \"..\", etc. in project paths." 123 | :group 'tabspaces 124 | :type 'boolean) 125 | 126 | (defcustom tabspaces-echo-area-enable nil 127 | "Display tabs in echo area instead of tab-bar when enabled." 128 | :group 'tabspaces 129 | :type 'boolean) 130 | 131 | (defcustom tabspaces-echo-area-format-function #'tabspaces--echo-area-format-tabs 132 | "Function to format tabs for echo area display." 133 | :group 'tabspaces 134 | :type 'function) 135 | 136 | (defcustom tabspaces-echo-area-idle-delay 1.0 137 | "Number of seconds to wait before showing tabs when idle." 138 | :group 'tabspaces 139 | :type 'number 140 | :set (lambda (symbol value) 141 | (set-default symbol value) 142 | ;; Restart timer with new delay if echo area is enabled 143 | (when (and (boundp 'tabspaces-echo-area-enable) 144 | tabspaces-echo-area-enable 145 | (boundp 'tabspaces--idle-timer) 146 | tabspaces--idle-timer) 147 | (tabspaces--setup-idle-timer)))) 148 | 149 | ;;;; Echo Area Display 150 | 151 | (defvar tabspaces--tabs-visible nil 152 | "Non-nil when tabs are currently displayed in the echo area.") 153 | 154 | (defvar tabspaces--idle-timer nil 155 | "Timer object for displaying tabs after idle time.") 156 | 157 | (defvar tabspaces--original-tab-bar-show nil 158 | "Original value of `tab-bar-show' before echo area display is enabled.") 159 | 160 | (defvar tabspaces--last-echo-display nil 161 | "Last tabs display string to avoid duplicate messages.") 162 | 163 | (defun tabspaces--filter-messages-buffer () 164 | "Remove tab display messages from *Messages* buffer." 165 | (when (and (get-buffer "*Messages*") 166 | tabspaces--last-echo-display) 167 | (with-current-buffer "*Messages*" 168 | (save-excursion 169 | (let ((inhibit-read-only t)) 170 | (goto-char (point-max)) 171 | (forward-line -1) 172 | (when (looking-at-p (regexp-quote tabspaces--last-echo-display)) 173 | (delete-region (line-beginning-position) 174 | (min (1+ (line-end-position)) (point-max))))))))) 175 | 176 | (defun tabspaces--echo-area-format-tabs () 177 | "Format all tabs for echo area display using the configured tab-bar formatter. 178 | Returns a formatted string containing all tabs, or nil if only one tab exists." 179 | (when (> (length (tab-bar-tabs)) 1) 180 | (let* ((tabs (tab-bar-tabs)) 181 | (current-tab (tab-bar--current-tab-find tabs)) 182 | (tab-strings '())) 183 | (dotimes (i (length tabs)) 184 | (let* ((tab (nth i tabs)) 185 | (current-p (eq tab current-tab)) 186 | ;; Convert 0-based tab index to 1-based for display 187 | (display-index (if (< i 9) (1+ i) 0)) 188 | (formatted-name (funcall tab-bar-tab-name-format-function tab display-index))) 189 | (push formatted-name tab-strings))) 190 | (mapconcat #'identity (reverse tab-strings) "")))) 191 | 192 | (defun tabspaces--echo-area-display (&rest _) 193 | "Display formatted tabs in the echo area without logging to *Messages*. 194 | Sets the visibility flag to indicate tabs are currently shown. 195 | Optional ARGS are ignored, allowing use as advice." 196 | (when tabspaces-echo-area-enable 197 | (let ((tabs-display (funcall tabspaces-echo-area-format-function))) 198 | (when tabs-display 199 | (setq tabspaces--tabs-visible t) 200 | ;; Mark this as a tab display message for filtering 201 | (setq tabspaces--last-echo-display tabs-display) 202 | ;; Display the message normally 203 | (message "%s" tabs-display) 204 | ;; Then remove it from *Messages* buffer 205 | (run-with-timer 0.01 nil #'tabspaces--filter-messages-buffer))))) 206 | 207 | (defun tabspaces--idle-display () 208 | "Display tabs in echo area after idle period. 209 | Only displays if echo area feature is enabled, multiple tabs exist, 210 | and the minibuffer is not active." 211 | (when (and tabspaces-echo-area-enable 212 | (> (length (tab-bar-tabs)) 1) 213 | (not (minibufferp (current-buffer)))) 214 | (tabspaces--echo-area-display))) 215 | 216 | (defun tabspaces--setup-idle-timer () 217 | "Initialize idle timer to display tabs after inactivity. 218 | Cancels any existing timer before creating a new one." 219 | (when tabspaces--idle-timer 220 | (cancel-timer tabspaces--idle-timer)) 221 | (setq tabspaces--idle-timer 222 | (run-with-idle-timer tabspaces-echo-area-idle-delay t #'tabspaces--idle-display))) 223 | 224 | (defun tabspaces--cancel-idle-timer () 225 | "Cancel and clear the idle display timer." 226 | (when tabspaces--idle-timer 227 | (cancel-timer tabspaces--idle-timer) 228 | (setq tabspaces--idle-timer nil))) 229 | 230 | (defun tabspaces-restart-idle-timer () 231 | "Restart the echo area idle timer with current delay settings. 232 | Useful for troubleshooting or after changing the delay value." 233 | (interactive) 234 | (when tabspaces-echo-area-enable 235 | (tabspaces--setup-idle-timer) 236 | (message "Idle timer restarted with delay: %.1f seconds" tabspaces-echo-area-idle-delay))) 237 | 238 | (defun tabspaces-echo-area-timer-status () 239 | "Display current status of the echo area idle timer. 240 | Shows if timer is active, the delay setting, and other relevant info." 241 | (interactive) 242 | (let ((status-parts '())) 243 | (push (format "Echo area enabled: %s" (if tabspaces-echo-area-enable "yes" "no")) status-parts) 244 | (push (format "Idle delay: %.1f seconds" tabspaces-echo-area-idle-delay) status-parts) 245 | (push (format "Timer active: %s" (if tabspaces--idle-timer "yes" "no")) status-parts) 246 | (when tabspaces--idle-timer 247 | (push (format "Timer object: %s" tabspaces--idle-timer) status-parts)) 248 | (push (format "Number of tabs: %d" (length (tab-bar-tabs))) status-parts) 249 | (message (mapconcat #'identity status-parts ", ")))) 250 | 251 | (defun tabspaces--echo-area-setup () 252 | "Initialize echo area tab display when enabled. 253 | Hides the visual tab-bar and sets up idle timer for tab display." 254 | (when tabspaces-echo-area-enable 255 | ;; Ensure tab-bar-mode is enabled for tab functionality 256 | (unless tab-bar-mode (tab-bar-mode 1)) 257 | ;; Store original setting and hide visual tab-bar 258 | (setq tabspaces--original-tab-bar-show tab-bar-show) 259 | (setq tab-bar-show nil) 260 | ;; Force tab-bar update after brief delay to override other configurations 261 | (run-with-timer 0.1 nil 262 | (lambda () 263 | (setq tab-bar-show nil) 264 | (when (fboundp 'tab-bar--update-tab-bar-lines) 265 | (tab-bar--update-tab-bar-lines)))) 266 | ;; Configure automatic display via idle timer only 267 | (tabspaces--setup-idle-timer))) 268 | 269 | (defun tabspaces--echo-area-cleanup () 270 | "Clean up echo area tab display configuration. 271 | Restores original tab-bar visibility and removes timer." 272 | ;; Restore original tab-bar visibility setting 273 | (when (boundp 'tabspaces--original-tab-bar-show) 274 | (setq tab-bar-show tabspaces--original-tab-bar-show)) 275 | ;; Clean up timer 276 | (tabspaces--cancel-idle-timer) 277 | ;; Reset state variables 278 | (setq tabspaces--tabs-visible nil 279 | tabspaces--last-echo-display nil)) 280 | 281 | (defun tabspaces-show-workspaces () 282 | "Display current workspaces in the echo area on command." 283 | (interactive) 284 | (let ((tabs-display (funcall tabspaces-echo-area-format-function))) 285 | (if tabs-display 286 | (message "%s" tabs-display) 287 | (message "Only one workspace active")))) 288 | 289 | (defun tabspaces-toggle-echo-area-display () 290 | "Toggle echo area tab display feature on or off. 291 | When enabled, tabs will appear in the echo area after idle time and 292 | during tab operations. When disabled, tabs are only shown in the tab-bar." 293 | (interactive) 294 | (if (or tabspaces--tabs-visible tabspaces-echo-area-enable) 295 | ;; Turn off: disable feature and clear current display 296 | (progn 297 | (setq tabspaces-echo-area-enable nil) 298 | (setq tabspaces--tabs-visible nil) 299 | (message nil) 300 | (tabspaces--echo-area-cleanup) 301 | (run-with-timer 0.5 nil (lambda () (message "Echo area tabs disabled")))) 302 | ;; Turn on: enable feature and show tabs 303 | (progn 304 | (setq tabspaces-echo-area-enable t) 305 | (tabspaces--echo-area-setup) 306 | (tabspaces--echo-area-display) 307 | (run-with-timer 0.5 nil (lambda () (message "Echo area tabs enabled")))))) 308 | 309 | ;;;; Create Buffer Workspace 310 | 311 | (defun tabspaces-reset-buffer-list () 312 | "Reset the current tab's `buffer-list'. 313 | Only the current window buffers and buffers in 314 | `tabspaces-include-buffers' are kept in the `buffer-list' and 315 | `buried-buffer-list'." 316 | (interactive) 317 | ;; https://www.gnu.org/software/emacs/manual/html_node/elisp/Current-Buffer.html 318 | ;; The current-tab uses `buffer-list' and `buried-buffer-list'. 319 | ;; A hidden tab keeps these as `wc-bl' and `wc-bbl'. 320 | (set-frame-parameter nil 321 | 'buffer-list 322 | (let ((window-buffers (mapcar #'window-buffer (window-list)))) 323 | (seq-filter (lambda (buffer) 324 | (or (member buffer window-buffers) 325 | (and (member (buffer-name buffer) 326 | tabspaces-include-buffers) 327 | (not (member (buffer-name buffer) 328 | tabspaces-exclude-buffers))))) 329 | (frame-parameter nil 'buffer-list)))) 330 | (set-frame-parameter nil 331 | 'buried-buffer-list 332 | (seq-filter (lambda (buffer) 333 | (and (member (buffer-name buffer) 334 | tabspaces-include-buffers) 335 | (not (member (buffer-name buffer) 336 | tabspaces-exclude-buffers)))) 337 | (frame-parameter nil 'buried-buffer-list)))) 338 | 339 | (defun tabspaces--tab-post-open-function (_tab) 340 | "Reset buffer list on new tab creation." 341 | (tabspaces-reset-buffer-list)) 342 | 343 | ;;;; Filter Workspace Buffers 344 | 345 | (defun tabspaces--local-buffer-p (buffer) 346 | "Return whether BUFFER is in the list of local buffers." 347 | (or (member (buffer-name buffer) tabspaces-include-buffers) 348 | (memq buffer (frame-parameter nil 'buffer-list)))) 349 | 350 | (defun tabspaces--set-buffer-predicate (frame) 351 | "Set the buffer predicate of FRAME to `tabspaces--local-buffer-p'." 352 | (set-frame-parameter frame 'buffer-predicate #'tabspaces--local-buffer-p)) 353 | 354 | (defun tabspaces--reset-buffer-predicate (frame) 355 | "Reset the buffer predicate of FRAME if it is `tabspaces--local-buffer-p'." 356 | (when (eq (frame-parameter frame 'buffer-predicate) #'tabspaces--local-buffer-p) 357 | (set-frame-parameter frame 'buffer-predicate nil))) 358 | 359 | (defun tabspaces--buffer-list (&optional frame tabnum) 360 | "Return a list of all live buffers associated with the current frame and tab. 361 | A non-nil value of FRAME selects a specific frame instead of the 362 | current one. If TABNUM is nil, the current tab is used. If it is 363 | non-nil, then specify a tab index in the given frame." 364 | (let ((list 365 | (if tabnum 366 | (let ((tab (nth tabnum (frame-parameter frame 'tabs)))) 367 | (if (eq 'current-tab (car tab)) 368 | (frame-parameter frame 'buffer-list) 369 | (or 370 | (cdr (assq 'wc-bl tab)) 371 | (mapcar 'get-buffer 372 | (car (cdr (assq #'tabspaces--buffer-list (assq 'ws tab)))))))) 373 | (frame-parameter frame 'buffer-list)))) 374 | (seq-filter #'buffer-live-p list))) 375 | 376 | ;;;; Project Workspace Helper Functions 377 | 378 | ;;;###autoload 379 | (defun tabspaces--current-tab-name () 380 | "Get name of current tab." 381 | (cdr (assq 'name (tab-bar--current-tab)))) 382 | 383 | ;;;###autoload 384 | (defun tabspaces--list-tabspaces () 385 | "Return a list of `tab-bar' tabs/workspaces." 386 | (mapcar (lambda (tab) (alist-get 'name tab)) (tab-bar-tabs))) 387 | 388 | ;;;###autoload 389 | (defun tabspaces--project-name () 390 | "Get name for project from vc. 391 | If not in a project return buffer filename, or `-' if not visiting a file." 392 | (let ((buf (buffer-file-name))) 393 | (cond ((and buf (vc-registered buf)) 394 | (file-name-nondirectory (directory-file-name (vc-root-dir)))) 395 | (t "-")))) 396 | 397 | ;;;###autoload 398 | (defun tabspaces--name-tab-by-project-or-default () 399 | "Return project name if in a project, or default tab-bar name if not. 400 | The default tab-bar name uses the buffer name along with a counter." 401 | (let ((project-name (tabspaces--project-name)) 402 | (tab (tab-bar-tab-name-current))) 403 | (cond ((string= tab project-name) 404 | (tab-bar-switch-to-tab tab)) 405 | ((string= "-" project-name) 406 | (tab-bar-tab-name-current-with-count)) 407 | (t (tabspaces--project-name))))) 408 | 409 | ;;;###autoload 410 | (defun tabspaces--add-to-default-tabspace (buffer) 411 | "Add BUFFER to default tabspace buffer list." 412 | (let ((tab-names (mapcar 413 | (lambda (tab) (alist-get 'name tab)) 414 | (funcall tab-bar-tabs-function)))) 415 | (when (and tabspaces-remove-to-default 416 | (member tabspaces-default-tab tab-names)) 417 | ;; add buffer to default tabspace 418 | (tab-bar-select-tab-by-name tabspaces-default-tab) 419 | (display-buffer buffer) 420 | (switch-to-buffer buffer t nil) 421 | (if (one-window-p t) 422 | (previous-buffer) 423 | (delete-window)) 424 | (tab-bar-switch-to-recent-tab)))) 425 | 426 | ;;;; Interactive Functions 427 | 428 | ;;;;; Open Project & File 429 | (defun tabspaces-project-switch-project-open-file (dir) 430 | "Switch to another project by running an Emacs command. 431 | Open file using `project-find-file'. NOTE: this function does *not* 432 | open or switch to a new workspace. Rather it switches to a new 433 | project and opens a file via `completing-read'. If you prefer to 434 | use the project.el command-menu, then use 435 | `project-switch-project' 436 | 437 | When called, this function will use the project corresponding 438 | to the selected directory DIR." 439 | (interactive (list (project-prompt-project-dir))) 440 | (let ((project-switch-commands tabspaces-project-switch-commands)) 441 | (project-switch-project dir))) 442 | 443 | ;;;;; Buffer Functions 444 | 445 | (defun tabspaces-remove-buffer (&optional buffer) 446 | "Bury and remove BUFFER from current tabspace. 447 | If BUFFER is nil, remove current buffer. If 448 | `tabspaces-remove-to-default' is t then add the buffer to the 449 | default tabspace after remove, unless we're already in the default tabspace, in which case remove from the default as well." 450 | (let* ((buffer (get-buffer (or buffer (current-buffer)))) 451 | (buffer-list (frame-parameter nil 'buffer-list)) 452 | (in-default-tab (string= (tabspaces--current-tab-name) 453 | tabspaces-default-tab))) 454 | ;; delete window of buffer 455 | (cond 456 | ((eq buffer (window-buffer (selected-window))) 457 | (if (one-window-p t) 458 | (bury-buffer) 459 | (delete-window))) 460 | ((get-buffer-window buffer) 461 | (select-window (get-buffer-window buffer) t) 462 | (if (one-window-p t) 463 | (bury-buffer) 464 | (delete-window))) 465 | (t 466 | (message (format "Buffer `%s' removed from `%s' tabspace." 467 | buffer (tabspaces--current-tab-name))))) 468 | (bury-buffer buffer) 469 | ;; Delete buffer from tabspace buffer list 470 | (delete buffer buffer-list) 471 | ;; If specified AND we're not in default tab, add buffer to default tabspace 472 | (when (and tabspaces-remove-to-default (not in-default-tab)) 473 | (tabspaces--add-to-default-tabspace buffer)))) 474 | 475 | (defun tabspaces-remove-current-buffer () 476 | "Bury and remove current buffer from current tabspace." 477 | (interactive) 478 | (tabspaces-remove-buffer)) 479 | 480 | (defun tabspaces-remove-selected-buffer (buffer) 481 | "Remove selected BUFFER from the frame's buffer list. 482 | If `tabspaces-remove-to-default' is t then add the buffer to the 483 | default tabspace." 484 | (interactive 485 | (list 486 | (let ((blst (mapcar (lambda (b) (buffer-name b)) 487 | (tabspaces--buffer-list)))) 488 | ;; select buffer 489 | (read-buffer (format "Remove buffer from `%s' tabspace: " 490 | (tabspaces--current-tab-name)) 491 | nil t 492 | (lambda (b) (member (car b) blst)))))) 493 | (tabspaces-remove-buffer buffer)) 494 | 495 | (defun tabspaces-switch-to-buffer (buffer &optional norecord force-same-window) 496 | "Display the local buffer BUFFER in the selected window. 497 | This is the frame/tab-local equivalent to `switch-to-buffer'. 498 | The arguments NORECORD and FORCE-SAME-WINDOW are passed to `switch-to-buffer'." 499 | (interactive 500 | (list 501 | (let ((blst (cl-remove (buffer-name) (mapcar #'buffer-name (tabspaces--buffer-list))))) 502 | (read-buffer 503 | "Switch to local buffer: " blst nil 504 | (lambda (b) (member (if (stringp b) b (car b)) blst)))))) 505 | (switch-to-buffer buffer norecord force-same-window)) 506 | 507 | ;; See https://emacs.stackexchange.com/a/53016/11934 508 | (defun tabspaces--report-dupes (xs) 509 | (let ((ys ())) 510 | (while xs 511 | (unless (member (car xs) ys) ; Don't check it if already known to be a dup. 512 | (when (member (car xs) (cdr xs)) (push (car xs) ys))) 513 | (setq xs (cdr xs))) 514 | ys)) 515 | 516 | (defun tabspaces-switch-buffer-and-tab (buffer &optional norecord force-same-window) 517 | "Switch to the tab of chosen buffer, or create buffer. 518 | If buffer does not exist in buffer-list user can either create a 519 | new tab with the new buffer or open a new buffer in the current 520 | tab." 521 | (interactive 522 | (list 523 | (let ((blst (cl-remove (buffer-name) (mapcar #'buffer-name (buffer-list))))) 524 | (read-buffer 525 | "Switch to tab for buffer: " blst nil 526 | (lambda (b) (member (if (stringp b) b (car b)) blst)))))) 527 | 528 | ;; Action on buffer 529 | (let* ((tabcand nil) 530 | (buflst nil) 531 | ;; Provide flat list of all buffers in all tabs (and print dupe buffers). 532 | ;; This is the list of all buffers to search through. 533 | (bufflst (flatten-tree (dolist (tab (tabspaces--list-tabspaces) buflst) 534 | (push (mapcar #'buffer-name (tabspaces--buffer-list nil (tab-bar--tab-index-by-name tab))) buflst)))) 535 | (dupe (member buffer (tabspaces--report-dupes bufflst)))) 536 | ;; Run through conditions: 537 | (cond 538 | ;; 1. Buffer exists and is not open in more than one tabspace. 539 | ((and (get-buffer buffer) 540 | (not dupe)) 541 | (dolist (tab (tabspaces--list-tabspaces)) 542 | (when (member buffer (mapcar #'buffer-name (tabspaces--buffer-list nil (tab-bar--tab-index-by-name tab)))) 543 | (progn (tab-bar-switch-to-tab tab) 544 | (tabspaces-switch-to-buffer buffer))))) 545 | ;; 2. Buffer exists and is open in more than one tabspace. 546 | ((and (get-buffer buffer) 547 | dupe) 548 | (dolist (tab (tabspaces--list-tabspaces) tabcand) 549 | (when (member buffer (mapcar #'buffer-name (tabspaces--buffer-list nil (tab-bar--tab-index-by-name tab)))) 550 | (push tab tabcand))) 551 | (progn 552 | (tab-bar-switch-to-tab (completing-read "Select tab: " tabcand)) 553 | (tabspaces-switch-to-buffer buffer))) 554 | ;; 3. Buffer does not exist. 555 | ((yes-or-no-p "Buffer not found -- create a new workspace with buffer?") 556 | (switch-to-buffer-other-tab buffer)) 557 | ;; 4. Default -- create buffer in current tabspace. 558 | (t 559 | (switch-to-buffer buffer norecord force-same-window))))) 560 | 561 | (defun tabspaces-clear-buffers (&optional frame) 562 | "Clear the tabspace's buffer list, except for the current buffer. 563 | If FRAME is nil, use the current frame." 564 | (interactive) 565 | (set-frame-parameter frame 'buffer-list 566 | (list (if frame 567 | (with-selected-frame frame 568 | (current-buffer)) 569 | (current-buffer))))) 570 | 571 | ;;;;; Switch or Create Workspace 572 | ;; Some convenience functions for opening/closing workspaces and buffers. 573 | ;; Some of these are just wrappers around built-in functions. 574 | ;;;###autoload 575 | (defun tabspaces-switch-or-create-workspace (&optional workspace) 576 | "Switch to tab if it exists, otherwise create a new tabbed workspace." 577 | (interactive 578 | (let ((tabs (tabspaces--list-tabspaces))) 579 | (cond ((eq tabs nil) 580 | (tab-new) 581 | (tab-rename (completing-read "Workspace name: " tabs))) 582 | (t 583 | (list 584 | (completing-read "Select or create tab: " tabs nil nil)))))) 585 | (cond ((member workspace (tabspaces--list-tabspaces)) 586 | (tab-bar-switch-to-tab workspace)) 587 | (t 588 | (tab-new) 589 | (tab-rename workspace)))) 590 | 591 | ;;;;; Close Workspace 592 | (defalias 'tabspaces-close-workspace #'tab-bar-close-tab) 593 | 594 | ;;;;; Close Workspace & Kill Buffers 595 | (defun tabspaces-kill-buffers-close-workspace () 596 | "Kill all buffers in the workspace and then close the workspace itself." 597 | (interactive) 598 | (let ((buf (tabspaces--buffer-list))) 599 | (unwind-protect 600 | (cl-loop for b in buf 601 | do (kill-buffer b)) 602 | (tab-bar-close-tab)))) 603 | 604 | ;;;;; Open or Create Project in Workspace 605 | 606 | (defvar tabspaces-project-tab-map '() 607 | "Alist mapping full project paths to their respective tab names.") 608 | 609 | (defun tabspaces--get-project-for-tab (tab-name) 610 | "Get project root path for TAB-NAME, or nil if not a project tab. 611 | Handles numbered tabs like \"ProjectName<2>\" by checking both exact 612 | match and base name without suffix." 613 | (or 614 | ;; First try exact match 615 | (car (rassoc tab-name tabspaces-project-tab-map)) 616 | ;; Then try stripping numbered suffix like "<2>" 617 | (when (string-match "\\`\\(.+\\)<[0-9]+>\\'" tab-name) 618 | (let ((base-name (match-string 1 tab-name))) 619 | (car (rassoc base-name tabspaces-project-tab-map)))))) 620 | 621 | (defun tabspaces-rename-existing-tab (old-name new-name) 622 | "Rename an existing tab from OLD-NAME to NEW-NAME." 623 | (let ((tabs (tab-bar-tabs))) 624 | (dolist (tab tabs) 625 | (when (equal (alist-get 'name tab) old-name) 626 | (tab-bar-rename-tab-by-name old-name new-name))))) 627 | 628 | (defun tabspaces-generate-descriptive-tab-name (project-path existing-tab-names) 629 | "Generate a unique tab name from the PROJECT-PATH checking against EXISTING-TAB-NAMES." 630 | (let* ((parts (reverse (split-string (directory-file-name project-path) "/"))) 631 | (base-name (car parts)) 632 | (parent-dir (nth 1 parts)) 633 | (grandparent-dir (nth 2 parts)) 634 | (simple-tab-name base-name) 635 | (complex-tab-name (if parent-dir 636 | (format "%s (%s/%s)" base-name (or grandparent-dir "") parent-dir) 637 | base-name))) 638 | (if (member simple-tab-name existing-tab-names) 639 | (let ((existing-path (rassoc simple-tab-name tabspaces-project-tab-map))) 640 | (when existing-path 641 | ;; Generate a new complex name for the existing conflict 642 | (let ((new-name-for-existing (tabspaces-generate-complex-name (car existing-path)))) 643 | ;; Rename the existing tab 644 | (tabspaces-rename-existing-tab simple-tab-name new-name-for-existing) 645 | ;; Update the map with the new name for the existing path 646 | (setcdr existing-path new-name-for-existing))) 647 | ;; Use the complex name for the new tab to avoid future conflicts 648 | complex-tab-name) 649 | ;; No conflict, add to map and use the simple name 650 | (progn 651 | (add-to-list 'tabspaces-project-tab-map (cons project-path simple-tab-name)) 652 | simple-tab-name)))) 653 | 654 | (defun tabspaces-generate-complex-name (project-path) 655 | "Generate a complex name based on the grandparent and parent directory names." 656 | (let* ((parts (reverse (split-string (directory-file-name project-path) "/"))) 657 | (base-name (car parts)) 658 | (parent-dir (nth 1 parts)) 659 | (grandparent-dir (nth 2 parts))) 660 | (format "%s (%s/%s)" base-name (or grandparent-dir "") parent-dir))) 661 | 662 | ;; Function to generate a unique numbered tab name 663 | (defun generate-unique-numbered-tab-name (base-name existing-names) 664 | (let ((counter 2) 665 | (new-name base-name)) 666 | (while (member new-name existing-names) 667 | (setq new-name (format "%s<%d>" base-name counter) 668 | counter (1+ counter))) 669 | new-name)) 670 | 671 | ;; Replace read-directory-name so that we can create new projects when necessary 672 | (defun tabspaces--read-directory-name (prompt &optional dir default mustmatch) 673 | "Read a directory name, and create it if it does not exist." 674 | (let ((dir-name (read-directory-name prompt dir default mustmatch))) 675 | (unless (file-directory-p dir-name) 676 | (when (y-or-n-p (format "Directory %s does not exist. Create it?" dir-name)) 677 | (make-directory dir-name t))) 678 | dir-name)) 679 | 680 | 681 | ;; Replace project-prompt-project-dir for project creation 682 | (defun tabspaces-prompt-project-dir () 683 | "Prompt the user for a directory that is one of the known project roots. 684 | The project is chosen among projects known from the project list, 685 | see `project-list-file'. 686 | It's also possible to enter an arbitrary directory not in the list." 687 | (project--ensure-read-project-list) 688 | (let* ((dir-choice "... (choose a dir)") 689 | (choices 690 | ;; XXX: Just using this for the category (for the substring 691 | ;; completion style). 692 | (project--file-completion-table 693 | (append project--list `(,dir-choice)))) 694 | (pr-dir "")) 695 | (while (equal pr-dir "") 696 | ;; If the user simply pressed RET, do this again until they don't. 697 | (setq pr-dir (completing-read "Select project: " choices nil t))) 698 | (if (equal pr-dir dir-choice) 699 | (tabspaces--read-directory-name "Select directory: " nil nil nil) 700 | pr-dir))) 701 | 702 | ;;;###autoload 703 | (defun tabspaces-open-or-create-project-and-workspace (&optional project prefix) 704 | "Open or create a project and its workspace with a descriptive tab name. 705 | With universal argument PREFIX, always create a new tab for the project." 706 | (interactive 707 | (list (tabspaces-prompt-project-dir) current-prefix-arg)) 708 | (let* ((project-switch-commands tabspaces-project-switch-commands) 709 | (project (if tabspaces-fully-resolve-paths 710 | (expand-file-name project) ; Resolve relative paths 711 | project)) 712 | (existing-tab-names (tabspaces--list-tabspaces)) 713 | (original-tab-name (or (cdr (assoc project tabspaces-project-tab-map)) 714 | (tabspaces-generate-descriptive-tab-name project existing-tab-names))) 715 | (tab-name original-tab-name) 716 | (session (concat project "." (file-name-nondirectory (directory-file-name project)) "-tabspaces-session.el")) 717 | (project-directory project) ; Use the full path as the project directory 718 | (project-exists (member (list project) project--list)) 719 | (create-new-tab (or prefix (not (member tab-name existing-tab-names))))) 720 | 721 | (message "Tabspaces: Project directory: %s" project-directory) 722 | 723 | ;; Now manage the workspace based on the project state: 724 | (cond 725 | ;; If there is no tab nor project, create both 726 | ((not project-exists) 727 | (message "Tabspaces - Creating new project and tab") 728 | (tab-bar-new-tab) 729 | (tab-bar-rename-tab tab-name) 730 | (let ((default-directory project-directory)) 731 | (message "Tabspaces: default directory set to %s" default-directory) 732 | (if (fboundp 'magit-init) 733 | (magit-init project-directory) 734 | (call-interactively #'vc-create-repo)) 735 | (delete-other-windows) 736 | (when (and tabspaces-initialize-project-with-todo 737 | (not (file-exists-p (expand-file-name tabspaces-todo-file-name project-directory)))) 738 | (with-temp-buffer 739 | (write-file (expand-file-name tabspaces-todo-file-name project-directory)))) 740 | (if (fboundp 'magit-status-setup-buffer) 741 | (magit-status-setup-buffer project-directory) 742 | (project-vc-dir)) 743 | (dired-jump-other-window)) 744 | ;; Remember new project 745 | (let ((pr (project--find-in-directory default-directory))) 746 | (project-remember-project pr))) 747 | 748 | ;; If project and tab exist, but we want a new tab 749 | ((and project-exists 750 | (member tab-name existing-tab-names) 751 | create-new-tab) 752 | (message "Tabspaces - Creating new tab for existing project and tab") 753 | (let ((new-tab-name (generate-unique-numbered-tab-name tab-name existing-tab-names))) 754 | (tab-bar-new-tab) 755 | (tab-bar-rename-tab new-tab-name) 756 | (setq tab-name new-tab-name)) 757 | (project-switch-project project)) 758 | 759 | ;; If project and tab exist, switch to it 760 | ((and project-exists 761 | (member tab-name existing-tab-names)) 762 | (message "Tabspaces - Switching to existing tab") 763 | (tab-bar-switch-to-tab tab-name)) 764 | 765 | ;; If project exists, but no corresponding tab, open a new tab 766 | (project-exists 767 | (message "Tabspaces - Creating new tab for existing project") 768 | (tab-bar-new-tab) 769 | (tab-bar-rename-tab tab-name) 770 | (if (and tabspaces-session-auto-restore 771 | (file-exists-p session)) 772 | (tabspaces-restore-session session) 773 | (project-switch-project project))) 774 | 775 | (t 776 | (message "Tabspaces - No project found or created.") 777 | nil)) 778 | 779 | (message "Tabspaces: Conditional execution completed") 780 | 781 | ;; Update tabspaces-project-tab-map (only for the main tab, not numbered duplicates) 782 | (unless (string-match-p "<[0-9]+>$" tab-name) 783 | (setq tabspaces-project-tab-map 784 | (cons (cons project-directory tab-name) 785 | (assq-delete-all project-directory tabspaces-project-tab-map)))))) 786 | 787 | ;;;; Tabspace Sessions 788 | (defconst tabspaces-session-header 789 | ";; ------------------------------------------------------------------------- 790 | ;; Tabspaces Session File for Emacs 791 | ;; ------------------------------------------------------------------------- 792 | " "Header to place in Tabspaces session file.") 793 | 794 | (defcustom tabspaces-session t 795 | "Whether to save tabspaces across sessions." 796 | :group 'tabspaces 797 | :type 'boolean) 798 | 799 | (defcustom tabspaces-session-auto-restore nil 800 | "Whether to restore tabspaces on session startup." 801 | :group 'tabspaces 802 | :type 'boolean) 803 | 804 | (defcustom tabspaces-session-file (concat user-emacs-directory "tabsession.el") 805 | "File for saving tabspaces session." 806 | :group 'tabspaces 807 | :type 'string) 808 | 809 | (defcustom tabspaces-session-project-session-store 'project 810 | "Determines where project session files are stored. 811 | Can be one of: 812 | - 'project (default) - Store in the project root directory 813 | - a string path - Store all project sessions in this directory 814 | - a function - Called with project root path to determine session file location" 815 | :group 'tabspaces 816 | :type '(choice 817 | (const :tag "In project directory" project) 818 | (directory :tag "In specific directory") 819 | (function :tag "Custom function"))) 820 | 821 | (defvar tabspaces--session-list nil 822 | "Store `tabspaces' session tabs and buffers.") 823 | 824 | ;; Helper functions 825 | (defun tabspaces--buffile (b) 826 | "Get filename for buffers." 827 | (cl-remove-if nil (buffer-file-name b))) 828 | 829 | (defun tabspaces--store-buffers (bufs) 830 | "Make list of filenames." 831 | (flatten-tree (mapcar #'tabspaces--buffile bufs))) 832 | 833 | ;; Save global session 834 | ;;;###autoload 835 | (defun tabspaces-save-session () 836 | "Save all tabspaces with their buffers and window configurations." 837 | (interactive) 838 | ;; Start from an empty list. 839 | (setq tabspaces--session-list nil) 840 | (let ((curr (tab-bar--current-tab-index))) 841 | ;; loop over tabs 842 | (cl-loop for tab in (tabspaces--list-tabspaces) 843 | do (progn 844 | (tab-bar-select-tab-by-name tab) 845 | (setq tabspaces--session-list 846 | (append tabspaces--session-list 847 | (list (list 848 | (tabspaces--store-buffers (tabspaces--buffer-list)) 849 | tab 850 | (window-state-get nil t))))))) 851 | ;; As tab-bar-select-tab starts counting from 1, we need to add 1 to the index. 852 | (tab-bar-select-tab (+ curr 1))) 853 | ;; Write to file 854 | (with-temp-file tabspaces-session-file 855 | (point-min) 856 | (insert ";; -*- mode: emacs-lisp; lexical-binding:t; coding: utf-8-emacs; -*-\n" 857 | tabspaces-session-header 858 | ";; Created " (current-time-string) "\n\n" 859 | ";; Project to tab name mapping:\n") 860 | (insert "(setq tabspaces-project-tab-map '" 861 | (format "%S" tabspaces-project-tab-map) ")\n\n" 862 | ";; Tabs and buffers:\n") 863 | (insert "(setq tabspaces--session-list '" 864 | (format "%S" tabspaces--session-list) ")")) 865 | (message "Global tabspaces session file \'%s\' saved" tabspaces-session-file)) 866 | 867 | ;; Save current project session 868 | (defun tabspaces-save-current-project-session (&optional session-file) 869 | "Save tabspace name, buffers, and window config for current tab & project. 870 | Optional SESSION-FILE parameter specifies where to save the session file. 871 | If not provided, uses the location specified by 872 | `tabspaces-session-project-session-store'." 873 | (interactive) 874 | (unless (vc-root-dir) 875 | (error "Not in a version controlled project")) 876 | (let ((tabspaces--session-list nil) ;; Start from an empty list. 877 | (ctab (tabspaces--current-tab-name)) 878 | (current-session (or session-file 879 | (tabspaces--get-project-session-file)))) 880 | ;; Ensure directory exists 881 | (make-directory (file-name-directory current-session) t) 882 | ;; Get buffers and window state 883 | (add-to-list 'tabspaces--session-list 884 | (list (tabspaces--store-buffers (tabspaces--buffer-list)) 885 | ctab 886 | (window-state-get nil t))) ;; t means include buffer names 887 | ;; Write to file 888 | (with-temp-file current-session 889 | (point-min) 890 | (insert ";; -*- mode: emacs-lisp; lexical-binding:t; coding: utf-8-emacs; -*-\n" 891 | tabspaces-session-header 892 | ";; Created " (current-time-string) "\n\n" 893 | ";; Project to tab name mapping:\n") 894 | (insert "(setq tabspaces-project-tab-map '" 895 | (format "%S" tabspaces-project-tab-map) ")\n\n" 896 | ";; Tab and buffers:\n") 897 | (insert "(setq tabspaces--session-list '" 898 | (format "%S" tabspaces--session-list) ")")) 899 | (message "Current project tabspaces session file \'%s\' saved" current-session))) 900 | 901 | ;; Save all project sessions 902 | (defun tabspaces-save-all-project-sessions () 903 | "Save each project tab to its own session file. 904 | Iterates through all tabs, identifies which are associated with projects 905 | via `tabspaces-project-tab-map', and saves each project tab's session 906 | to its respective project directory based on 907 | `tabspaces-session-project-session-store'." 908 | (let ((curr (tab-bar--current-tab-index)) 909 | (saved-projects '())) 910 | (condition-case err 911 | (progn 912 | (dolist (tab-name (tabspaces--list-tabspaces)) 913 | (let ((project-root (tabspaces--get-project-for-tab tab-name))) 914 | (when project-root 915 | ;; Switch to the project tab 916 | (tab-bar-select-tab-by-name tab-name) 917 | ;; Get session file path for this project 918 | (let* ((session-file (tabspaces--get-project-session-file-for-restore project-root)) 919 | (tabspaces--session-list nil) 920 | (ctab tab-name)) 921 | ;; Ensure directory exists 922 | (make-directory (file-name-directory session-file) t) 923 | ;; Store buffers and window state 924 | (add-to-list 'tabspaces--session-list 925 | (list (tabspaces--store-buffers (tabspaces--buffer-list)) 926 | ctab 927 | (window-state-get nil t))) 928 | ;; Write to file 929 | (with-temp-file session-file 930 | (point-min) 931 | (insert ";; -*- mode: emacs-lisp; lexical-binding:t; coding: utf-8-emacs; -*-\n" 932 | tabspaces-session-header 933 | ";; Created " (current-time-string) "\n\n" 934 | ";; Project to tab name mapping:\n") 935 | (insert "(setq tabspaces-project-tab-map '" 936 | (format "%S" tabspaces-project-tab-map) ")\n\n" 937 | ";; Tab and buffers:\n") 938 | (insert "(setq tabspaces--session-list '" 939 | (format "%S" tabspaces--session-list) ")")) 940 | (push project-root saved-projects))))) 941 | ;; Restore original tab 942 | (tab-bar-select-tab (+ curr 1)) 943 | (when saved-projects 944 | (message "Saved %d project session(s)" (length saved-projects)))) 945 | (error 946 | (message "Error saving project sessions: %s" (error-message-string err)) 947 | ;; Try to restore original tab even on error 948 | (ignore-errors (tab-bar-select-tab (+ curr 1))))))) 949 | 950 | ;; Save non-project tabs to global session 951 | (defun tabspaces-save-non-project-tabs () 952 | "Save tabs not associated with projects to the global session file. 953 | This preserves non-project workspaces when using per-project session mode." 954 | (let ((curr (tab-bar--current-tab-index)) 955 | (non-project-session-list nil)) 956 | (condition-case err 957 | (progn 958 | (dolist (tab-name (tabspaces--list-tabspaces)) 959 | (unless (tabspaces--get-project-for-tab tab-name) 960 | ;; This is a non-project tab 961 | (tab-bar-select-tab-by-name tab-name) 962 | (setq non-project-session-list 963 | (append non-project-session-list 964 | (list (list 965 | (tabspaces--store-buffers (tabspaces--buffer-list)) 966 | tab-name 967 | (window-state-get nil t))))))) 968 | ;; Restore original tab 969 | (tab-bar-select-tab (+ curr 1)) 970 | ;; Only write if there are non-project tabs 971 | (when non-project-session-list 972 | (with-temp-file tabspaces-session-file 973 | (point-min) 974 | (insert ";; -*- mode: emacs-lisp; lexical-binding:t; coding: utf-8-emacs; -*-\n" 975 | tabspaces-session-header 976 | ";; Created " (current-time-string) "\n\n" 977 | ";; Non-project tabs only (project tabs saved separately)\n\n" 978 | ";; Project to tab name mapping:\n") 979 | (insert "(setq tabspaces-project-tab-map '" 980 | (format "%S" tabspaces-project-tab-map) ")\n\n" 981 | ";; Tabs and buffers:\n") 982 | (insert "(setq tabspaces--session-list '" 983 | (format "%S" non-project-session-list) ")")) 984 | (message "Saved %d non-project tab(s) to global session" (length non-project-session-list)))) 985 | (error 986 | (message "Error saving non-project tabs: %s" (error-message-string err)) 987 | (ignore-errors (tab-bar-select-tab (+ curr 1))))))) 988 | 989 | ;; Smart session saver - dispatches based on configuration 990 | (defun tabspaces--save-session-smart () 991 | "Save sessions intelligently based on configuration. 992 | If `tabspaces-session-project-session-store' is set, saves each project 993 | tab to its own file and non-project tabs to the global file. 994 | Otherwise, saves everything to the global session file (traditional behavior)." 995 | (cond 996 | ;; Per-project saving enabled 997 | ((and tabspaces-session 998 | tabspaces-session-project-session-store) 999 | (tabspaces-save-all-project-sessions) 1000 | (tabspaces-save-non-project-tabs)) 1001 | 1002 | ;; Traditional global saving 1003 | (tabspaces-session 1004 | (tabspaces-save-session)))) 1005 | 1006 | ;; Restore session functions 1007 | (defun tabspaces--get-project-session-file () 1008 | "Get the session file path based on configuration." 1009 | (let* ((project-root (or (vc-root-dir) 1010 | (error "Not in a version controlled project"))) 1011 | (project-name (file-name-nondirectory (directory-file-name project-root))) 1012 | (session-name (concat "." project-name "-tabspaces-session.el"))) 1013 | (cond 1014 | ((eq tabspaces-session-project-session-store 'project) 1015 | (expand-file-name session-name project-root)) 1016 | 1017 | ((stringp tabspaces-session-project-session-store) 1018 | (expand-file-name session-name tabspaces-session-project-session-store)) 1019 | 1020 | ((functionp tabspaces-session-project-session-store) 1021 | (funcall tabspaces-session-project-session-store project-root)) 1022 | 1023 | (t (expand-file-name session-name project-root))))) 1024 | 1025 | (defun tabspaces--get-project-session-file-for-restore (project) 1026 | "Get the session file path for PROJECT based on configuration." 1027 | (let* ((project-name (file-name-nondirectory (directory-file-name project))) 1028 | (session-name (concat "." project-name "-tabspaces-session.el"))) 1029 | (cond 1030 | ((eq tabspaces-session-project-session-store 'project) 1031 | (expand-file-name session-name project)) 1032 | 1033 | ((stringp tabspaces-session-project-session-store) 1034 | (expand-file-name session-name tabspaces-session-project-session-store)) 1035 | 1036 | ((functionp tabspaces-session-project-session-store) 1037 | (funcall tabspaces-session-project-session-store project)) 1038 | 1039 | (t (expand-file-name session-name project))))) 1040 | 1041 | 1042 | ;;;###autoload 1043 | (defun tabspaces-restore-session (&optional project-or-session-file) 1044 | "Restore tabspaces session. 1045 | If PROJECT-OR-SESSION-FILE is: 1046 | - nil: if in a project tab and per-project storage is enabled, restore current project's session; 1047 | otherwise restore the global session from `tabspaces-session-file' 1048 | - a file path: restore that specific session file 1049 | - a project path: restore that project's session based on `tabspaces-session-project-session-store'" 1050 | (interactive) 1051 | (let ((session-file 1052 | (cond 1053 | ;; No argument - check if we're in a project tab with per-project storage 1054 | ((null project-or-session-file) 1055 | (if (and tabspaces-session-project-session-store 1056 | (project-current)) 1057 | ;; We're in a project - restore this project's session 1058 | (let* ((project-root (project-root (project-current))) 1059 | (project-session (tabspaces--get-project-session-file-for-restore project-root))) 1060 | (if (file-exists-p project-session) 1061 | project-session 1062 | ;; Project session doesn't exist, fall back to global 1063 | tabspaces-session-file)) 1064 | ;; Not in a project or per-project storage disabled - use global 1065 | tabspaces-session-file)) 1066 | ;; File path - use directly 1067 | ((file-exists-p project-or-session-file) 1068 | project-or-session-file) 1069 | ;; Project path - get session file location 1070 | (t 1071 | (tabspaces--get-project-session-file-for-restore project-or-session-file))))) 1072 | 1073 | (if (file-exists-p session-file) 1074 | (progn 1075 | (load-file session-file) 1076 | ;; Use placeholder buffer to avoid pollution 1077 | (cl-loop for elm in tabspaces--session-list do 1078 | (switch-to-buffer "*tabspaces--placeholder*") 1079 | (tabspaces-switch-or-create-workspace (cadr elm)) 1080 | (mapc #'find-file (car elm)) 1081 | (when (caddr elm) ; If window state exists 1082 | (window-state-put (caddr elm) nil 'safe))) 1083 | ;; Clean up placeholder buffer 1084 | (cl-loop for elm in tabspaces--session-list do 1085 | (tabspaces-switch-or-create-workspace (cadr elm)) 1086 | (tabspaces-remove-selected-buffer "*tabspaces--placeholder*")) 1087 | (kill-buffer "*tabspaces--placeholder*") 1088 | (message "Restored session from %s" session-file)) 1089 | (message "No session file found at %s" session-file)))) 1090 | 1091 | ;; Make sure session file exists 1092 | (defun tabspaces--create-session-file () 1093 | "Create the tabspaces session file if it does not exist." 1094 | (unless (file-exists-p tabspaces-session-file) 1095 | (with-temp-buffer 1096 | (write-file tabspaces-session-file)) 1097 | (message "Created tabspaces session file: %s" tabspaces-session-file))) 1098 | 1099 | ;; Restore session used for startup 1100 | (defun tabspaces--restore-session-on-startup () 1101 | "Restore tabspaces session on startup. 1102 | Unlike the interactive restore, this function does more clean up to remove 1103 | unnecessary tab." 1104 | (message "Restoring tabspaces session on startup.") 1105 | (tabspaces--create-session-file) 1106 | (tabspaces-restore-session)) 1107 | 1108 | ;;;; Define Keymaps 1109 | (defvar tabspaces-command-map 1110 | (let ((map (make-sparse-keymap))) 1111 | (define-key map (kbd "C") 'tabspaces-clear-buffers) 1112 | (define-key map (kbd "b") 'tabspaces-switch-to-buffer) 1113 | (define-key map (kbd "d") 'tabspaces-close-workspace) 1114 | (define-key map (kbd "k") 'tabspaces-kill-buffers-close-workspace) 1115 | (define-key map (kbd "o") 'tabspaces-open-or-create-project-and-workspace) 1116 | (define-key map (kbd "r") 'tabspaces-remove-current-buffer) 1117 | (define-key map (kbd "R") 'tabspaces-remove-selected-buffer) 1118 | (define-key map (kbd "s") 'tabspaces-switch-or-create-workspace) 1119 | (define-key map (kbd "t") 'tabspaces-switch-buffer-and-tab) 1120 | (define-key map (kbd "w") 'tabspaces-show-workspaces) 1121 | (define-key map (kbd "T") 'tabspaces-toggle-echo-area-display) 1122 | map) 1123 | "Keymap for tabspace/workspace commands after `tabspaces-keymap-prefix'.") 1124 | (fset 'tabspaces-command-map tabspaces-command-map) 1125 | 1126 | (defvar tabspaces-mode-map 1127 | (let ((map (make-sparse-keymap))) 1128 | (when tabspaces-keymap-prefix 1129 | (define-key map (kbd tabspaces-keymap-prefix) 'tabspaces-command-map)) 1130 | map) 1131 | "Keymap for Tabspaces mode.") 1132 | 1133 | ;;;; Define Minor Mode 1134 | ;;;###autoload 1135 | (define-minor-mode tabspaces-mode 1136 | "Create a global minor mode for `tabspaces', or buffer-isolated workspaces. 1137 | This uses Emacs `tab-bar' and `project.el'." 1138 | :lighter "" 1139 | :keymap tabspaces-mode-map 1140 | :global t 1141 | (cond (tabspaces-mode 1142 | ;; Set up tabspace isolated buffers 1143 | (dolist (frame (frame-list)) 1144 | (tabspaces--set-buffer-predicate frame) 1145 | (add-hook 'after-make-frame-functions #'tabspaces--set-buffer-predicate) 1146 | (add-to-list 'tab-bar-tab-post-open-functions #'tabspaces--tab-post-open-function) 1147 | ;; Option to always use filtered buffers when minor mode is enabled. 1148 | (when tabspaces-use-filtered-buffers-as-default 1149 | ;; Remap switch-to-buffer 1150 | (define-key (current-global-map) [remap switch-to-buffer] #'tabspaces-switch-to-buffer))) 1151 | (when tabspaces-session 1152 | (add-hook 'kill-emacs-hook #'tabspaces--save-session-smart)) 1153 | (when tabspaces-session-auto-restore 1154 | (tabspaces--restore-session-on-startup)) 1155 | ;; Setup echo area display if enabled 1156 | (tabspaces--echo-area-setup)) 1157 | (t 1158 | ;; Remove all modifications 1159 | (dolist (frame (frame-list)) 1160 | (tabspaces--reset-buffer-predicate frame)) 1161 | (when tabspaces-use-filtered-buffers-as-default 1162 | (define-key (current-global-map) [remap switch-to-buffer] nil)) 1163 | (setq tab-bar-tab-post-open-functions (remove #'tabspaces--tab-post-open-function tab-bar-tab-post-open-functions)) 1164 | (remove-hook 'after-make-frame-functions #'tabspaces--set-buffer-predicate) 1165 | (remove-hook 'kill-emacs-hook #'tabspaces--save-session-smart) 1166 | (remove-hook 'emacs-startup-hook #'tabspaces-restore-session) 1167 | ;; Cleanup echo area display 1168 | (tabspaces--echo-area-cleanup)))) 1169 | 1170 | ;;; Provide 1171 | (provide 'tabspaces) 1172 | ;;; tabspaces.el ends here 1173 | --------------------------------------------------------------------------------