2020-09-01 11:17:43 +02:00
|
|
|
;;; buffer.el --- Working with buffers -*- lexical-binding: t -*-
|
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
;; Author: William Carroll <wpcarro@gmail.com>
|
2020-09-01 11:17:43 +02:00
|
|
|
;; Version: 0.0.1
|
|
|
|
;; URL: https://git.wpcarro.dev/wpcarro/briefcase
|
|
|
|
;; Package-Requires: ((emacs "24.3"))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;; Utilities for CRUDing buffers in Emacs.
|
|
|
|
;;
|
|
|
|
;; Many of these functions may seem unnecessary especially when you consider
|
|
|
|
;; there implementations. In general I believe that Elisp suffers from a
|
|
|
|
;; library disorganization problem. Providing simple wrapper functions that
|
|
|
|
;; rename functions or reorder parameters is worth the effort in my opinion if
|
|
|
|
;; it improves discoverability (via intuition) and improve composability.
|
2020-01-08 16:21:37 +01:00
|
|
|
;;
|
|
|
|
;; I support three ways for switching between what I'm calling "source code
|
|
|
|
;; buffers":
|
|
|
|
;; 1. Toggling previous: <SPC><SPC>
|
|
|
|
;; 2. Using `ivy-read': <SPC>b
|
2020-09-01 14:44:18 +02:00
|
|
|
;; TODO: These obscure evil KBDs. Maybe a hydra definition would be best?
|
2020-01-08 16:21:37 +01:00
|
|
|
;; 3. Cycling (forwards/backwards): C-f, C-b
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Dependencies
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-01-08 16:21:37 +01:00
|
|
|
(require 'prelude)
|
2019-10-09 13:13:56 +02:00
|
|
|
(require 'maybe)
|
2020-01-08 16:21:37 +01:00
|
|
|
(require 'set)
|
|
|
|
(require 'cycle)
|
2020-01-09 14:38:51 +01:00
|
|
|
(require 'struct)
|
2020-09-02 15:52:17 +02:00
|
|
|
(require 'ts)
|
2020-09-02 16:01:43 +02:00
|
|
|
(require 'general)
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Library
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defconst buffer-enable-tests? t
|
2020-01-08 16:21:37 +01:00
|
|
|
"When t, run the test suite.")
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defconst buffer-install-kbds? t
|
2020-01-08 16:21:37 +01:00
|
|
|
"When t, install the keybindings defined herein.")
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defconst buffer-source-code-blacklist
|
|
|
|
(set-new 'dired-mode
|
2020-01-09 15:06:42 +01:00
|
|
|
'erc-mode
|
2020-02-02 14:19:54 +01:00
|
|
|
'vterm-mode
|
2020-01-09 15:06:42 +01:00
|
|
|
'magit-status-mode
|
|
|
|
'magit-process-mode
|
2020-01-10 21:28:17 +01:00
|
|
|
'magit-log-mode
|
2020-02-02 14:19:54 +01:00
|
|
|
'magit-diff-mode
|
2020-01-10 21:28:17 +01:00
|
|
|
'org-mode
|
|
|
|
'fundamental-mode)
|
2020-01-08 16:21:37 +01:00
|
|
|
"A blacklist of major-modes to ignore for listing source code buffers.")
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defconst buffer-source-code-timeout 2
|
2020-01-08 16:21:37 +01:00
|
|
|
"Number of seconds to wait before invalidating the cycle.")
|
|
|
|
|
|
|
|
(cl-defstruct source-code-cycle cycle last-called)
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun buffer-emacs-generated? (name)
|
2020-01-08 16:21:37 +01:00
|
|
|
"Return t if buffer, NAME, is an Emacs-generated buffer.
|
|
|
|
Some buffers are Emacs-generated but are surrounded by whitespace."
|
|
|
|
(let ((trimmed (s-trim name)))
|
|
|
|
(and (s-starts-with? "*" trimmed))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun buffer-find (buffer-or-name)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Find a buffer by its BUFFER-OR-NAME."
|
|
|
|
(get-buffer buffer-or-name))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun buffer-major-mode (name)
|
2020-01-08 16:21:37 +01:00
|
|
|
"Return the active `major-mode' in buffer, NAME."
|
2020-09-01 11:17:43 +02:00
|
|
|
(with-current-buffer (buffer-find name)
|
2020-01-08 16:21:37 +01:00
|
|
|
major-mode))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun buffer-source-code-buffers ()
|
2020-01-08 16:21:37 +01:00
|
|
|
"Return a list of source code buffers.
|
|
|
|
This will ignore Emacs-generated buffers, like *Messages*. It will also ignore
|
2020-09-01 11:17:43 +02:00
|
|
|
any buffer whose major mode is defined in `buffer-source-code-blacklist'."
|
2020-01-08 16:21:37 +01:00
|
|
|
(->> (buffer-list)
|
2020-09-01 11:17:43 +02:00
|
|
|
(list-map #'buffer-name)
|
|
|
|
(list-reject #'buffer-emacs-generated?)
|
|
|
|
(list-reject (lambda (name)
|
|
|
|
(set-contains? (buffer-major-mode name)
|
|
|
|
buffer-source-code-blacklist)))))
|
2020-01-08 16:21:37 +01:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defvar buffer-source-code-cycle-state
|
2020-01-08 16:21:37 +01:00
|
|
|
(make-source-code-cycle
|
2020-09-01 11:17:43 +02:00
|
|
|
:cycle (cycle-from-list (buffer-source-code-buffers))
|
2020-01-08 16:21:37 +01:00
|
|
|
:last-called (ts-now))
|
|
|
|
"State used to manage cycling between source code buffers.")
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun buffer-exists? (name)
|
2020-01-08 16:21:37 +01:00
|
|
|
"Return t if buffer, NAME, exists."
|
2020-09-01 11:17:43 +02:00
|
|
|
(maybe-some? (buffer-find name)))
|
2020-01-08 16:21:37 +01:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun buffer-new (name)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return a newly created buffer NAME."
|
|
|
|
(generate-new-buffer name))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun buffer-find-or-create (name)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Find or create buffer, NAME.
|
|
|
|
Return a reference to that buffer."
|
2020-09-01 11:17:43 +02:00
|
|
|
(let ((x (buffer-find name)))
|
2020-08-31 15:59:48 +02:00
|
|
|
(if (maybe-some? x)
|
2019-10-09 13:13:56 +02:00
|
|
|
x
|
2020-09-01 11:17:43 +02:00
|
|
|
(buffer-new name))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;; TODO: Should this consume: `display-buffer' or `switch-to-buffer'?
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun buffer-show (buffer-or-name)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Display the BUFFER-OR-NAME, which is either a buffer reference or its name."
|
|
|
|
(display-buffer buffer-or-name))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
;; TODO: Move this and `buffer-cycle-prev' into a separate module that
|
2020-01-08 16:21:37 +01:00
|
|
|
;; encapsulates all of this behavior.
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun buffer-cycle (cycle-fn)
|
2020-09-01 14:44:18 +02:00
|
|
|
"Using CYCLE-FN, move through `buffer-source-code-buffers'."
|
2020-01-08 16:21:37 +01:00
|
|
|
(let ((last-called (source-code-cycle-last-called
|
2020-09-01 11:17:43 +02:00
|
|
|
buffer-source-code-cycle-state))
|
2020-01-08 16:21:37 +01:00
|
|
|
(cycle (source-code-cycle-cycle
|
2020-09-01 11:17:43 +02:00
|
|
|
buffer-source-code-cycle-state)))
|
2020-01-08 16:21:37 +01:00
|
|
|
(if (> (ts-diff (ts-now) last-called)
|
2020-09-01 11:17:43 +02:00
|
|
|
buffer-source-code-timeout)
|
2020-01-08 16:21:37 +01:00
|
|
|
(progn
|
2020-09-01 00:28:47 +02:00
|
|
|
(struct-set! source-code-cycle
|
2020-01-09 14:38:51 +01:00
|
|
|
cycle
|
2020-09-01 11:17:43 +02:00
|
|
|
(cycle-from-list (buffer-source-code-buffers))
|
|
|
|
buffer-source-code-cycle-state)
|
2020-01-08 16:21:37 +01:00
|
|
|
(let ((cycle (source-code-cycle-cycle
|
2020-09-01 11:17:43 +02:00
|
|
|
buffer-source-code-cycle-state)))
|
2020-01-08 16:21:37 +01:00
|
|
|
(funcall cycle-fn cycle)
|
2020-09-01 11:17:43 +02:00
|
|
|
(switch-to-buffer (cycle-current cycle)))
|
2020-09-01 00:28:47 +02:00
|
|
|
(struct-set! source-code-cycle
|
2020-01-09 14:38:51 +01:00
|
|
|
last-called
|
|
|
|
(ts-now)
|
2020-09-01 11:17:43 +02:00
|
|
|
buffer-source-code-cycle-state))
|
2020-01-08 16:21:37 +01:00
|
|
|
(progn
|
|
|
|
(funcall cycle-fn cycle)
|
2020-09-01 11:17:43 +02:00
|
|
|
(switch-to-buffer (cycle-current cycle))))))
|
2020-01-08 16:21:37 +01:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun buffer-cycle-next ()
|
|
|
|
"Cycle forward through the `buffer-source-code-buffers'."
|
2020-01-08 16:21:37 +01:00
|
|
|
(interactive)
|
2020-09-01 11:17:43 +02:00
|
|
|
(buffer-cycle #'cycle-next))
|
2020-01-08 16:21:37 +01:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun buffer-cycle-prev ()
|
|
|
|
"Cycle backward through the `buffer-source-code-buffers'."
|
2020-01-08 16:21:37 +01:00
|
|
|
(interactive)
|
2020-09-01 11:17:43 +02:00
|
|
|
(buffer-cycle #'cycle-prev))
|
2020-01-08 16:21:37 +01:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun buffer-ivy-source-code ()
|
2020-01-08 16:21:37 +01:00
|
|
|
"Use `ivy-read' to choose among all open source code buffers."
|
|
|
|
(interactive)
|
|
|
|
(ivy-read "Source code buffer: "
|
2020-09-01 11:17:43 +02:00
|
|
|
(-drop 1 (buffer-source-code-buffers))
|
2020-01-08 16:21:37 +01:00
|
|
|
:sort nil
|
|
|
|
:action #'switch-to-buffer))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun buffer-show-previous ()
|
2020-01-08 16:21:37 +01:00
|
|
|
"Call `switch-to-buffer' on the previously visited buffer.
|
|
|
|
This function ignores Emacs-generated buffers, i.e. the ones that look like
|
|
|
|
this: *Buffer*. It also ignores buffers that are `dired-mode' or `erc-mode'.
|
|
|
|
This blacklist can easily be changed."
|
|
|
|
(interactive)
|
2020-09-01 11:17:43 +02:00
|
|
|
(let* ((xs (buffer-source-code-buffers))
|
|
|
|
(candidate (list-get 1 xs)))
|
2020-08-31 18:05:31 +02:00
|
|
|
(prelude-assert (maybe-some? candidate))
|
2020-01-08 16:21:37 +01:00
|
|
|
(switch-to-buffer candidate)))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(when buffer-install-kbds?
|
2020-01-08 16:21:37 +01:00
|
|
|
(general-define-key
|
|
|
|
:states '(normal)
|
2020-09-01 11:17:43 +02:00
|
|
|
"C-f" #'buffer-cycle-next
|
|
|
|
"C-b" #'buffer-cycle-prev)
|
2020-01-18 17:59:46 +01:00
|
|
|
(general-define-key
|
|
|
|
:prefix "<SPC>"
|
|
|
|
:states '(normal)
|
2020-09-01 11:17:43 +02:00
|
|
|
"b" #'buffer-ivy-source-code
|
|
|
|
"<SPC>" #'buffer-show-previous
|
2020-01-18 17:59:46 +01:00
|
|
|
"k" #'kill-buffer))
|
2020-01-08 16:21:37 +01:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Tests
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(when buffer-enable-tests?
|
2020-08-31 18:05:31 +02:00
|
|
|
(prelude-assert
|
2020-09-01 11:17:43 +02:00
|
|
|
(list-all? #'buffer-emacs-generated?
|
2020-01-08 16:21:37 +01:00
|
|
|
'("*scratch*"
|
|
|
|
"*Messages*"
|
|
|
|
"*shell*"
|
|
|
|
"*Shell Command Output*"
|
|
|
|
"*Occur*"
|
|
|
|
"*Warnings*"
|
|
|
|
"*Help*"
|
|
|
|
"*Completions*"
|
|
|
|
"*Apropos*"
|
|
|
|
"*info*"))))
|
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
(provide 'buffer)
|
|
|
|
;;; buffer.el ends here
|