feat(wpcarro/emacs): Package cycle.el

This will likely break a few things since I've changed the names of a few
functions to reflect their mutative APIs.

Change-Id: If6279999fba50813b68e66d7713c12afd209eb90
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6004
Reviewed-by: wpcarro <wpcarro@gmail.com>
Autosubmit: wpcarro <wpcarro@gmail.com>
Tested-by: BuildkiteCI
This commit is contained in:
William Carroll 2022-07-29 21:12:59 -07:00 committed by clbot
parent 65fb82097b
commit d1ab0c7cbc
10 changed files with 188 additions and 95 deletions

View file

@ -143,12 +143,12 @@ Return a reference to that buffer."
(defun buffer-cycle-next ()
"Cycle forward through the `buffer-source-code-buffers'."
(interactive)
(buffer-cycle #'cycle-next))
(buffer-cycle #'cycle-next!))
(defun buffer-cycle-prev ()
"Cycle backward through the `buffer-source-code-buffers'."
(interactive)
(buffer-cycle #'cycle-prev))
(buffer-cycle #'cycle-prev!))
(defun buffer-ivy-source-code ()
"Use `ivy-read' to choose among all open source code buffers."

View file

@ -51,7 +51,7 @@ There is no hook that I'm aware of to handle this more elegantly."
(defun colorscheme-whitelist-set (colorscheme)
"Focus the COLORSCHEME in the `colorscheme-whitelist' cycle."
(cycle-focus (lambda (x) (equal x colorscheme)) colorscheme-whitelist)
(cycle-focus! (lambda (x) (equal x colorscheme)) colorscheme-whitelist)
(colorscheme-set (colorscheme-current)))
(defun colorscheme-ivy-select ()
@ -66,8 +66,8 @@ There is no hook that I'm aware of to handle this more elegantly."
Cycle prev otherwise."
(disable-theme (cycle-current colorscheme-whitelist))
(let ((theme (if forward?
(cycle-next colorscheme-whitelist)
(cycle-prev colorscheme-whitelist))))
(cycle-next! colorscheme-whitelist)
(cycle-prev! colorscheme-whitelist))))
(colorscheme-set theme)
(message (s-concat "Active theme: " (symbol-to-string theme)))))

View file

@ -1,224 +0,0 @@
;;; cycle.el --- Simple module for working with cycles -*- lexical-binding: t -*-
;; Author: William Carroll <wpcarro@gmail.com>
;; Version: 0.0.1
;; Package-Requires: ((emacs "24.3"))
;;; Commentary:
;; Something like this may already exist, but I'm having trouble finding it, and
;; I think writing my own is a nice exercise for learning more Elisp.
;;; Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Dependencies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'prelude)
(require 'math)
(require 'maybe)
(require 'struct)
(require 'cl-lib)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Wish list
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; - TODO: Provide immutable variant.
;; - TODO: Replace mutable consumption with immutable variant.
;; - TODO: Replace indexing with (math-mod current cycle).
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Library
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; `current-index' tracks the current index
;; `xs' is the original list
(cl-defstruct cycle current-index previous-index xs)
(defconst cycle-enable-tests? t
"When t, run the tests defined herein.")
(defun cycle-from-list (xs)
"Create a cycle from a list of `XS'."
(if (= 0 (length xs))
(make-cycle :current-index nil
:previous-index nil
:xs xs)
(make-cycle :current-index 0
:previous-index nil
:xs xs)))
(defun cycle-new (&rest xs)
"Create a cycle with XS as the values."
(cycle-from-list xs))
(defun cycle-to-list (xs)
"Return the list representation of a cycle, XS."
(cycle-xs xs))
(defun cycle--next-index<- (lo hi x)
"Return the next index in a cycle when moving downwards.
- `LO' is the lower bound.
- `HI' is the upper bound.
- `X' is the current index."
(if (< (- x 1) lo)
(- hi 1)
(- x 1)))
(defun cycle--next-index-> (lo hi x)
"Return the next index in a cycle when moving upwards.
- `LO' is the lower bound.
- `HI' is the upper bound.
- `X' is the current index."
(if (>= (+ 1 x) hi)
lo
(+ 1 x)))
(defun cycle-previous-focus (cycle)
"Return the previously focused entry in CYCLE."
(let ((i (cycle-previous-index cycle)))
(if (maybe-some? i)
(nth i (cycle-xs cycle))
nil)))
;; TODO: Consider adding "!" to the function name herein since many of them
;; mutate the collection, and the APIs are beginning to confuse me.
(defun cycle-focus-previous! (xs)
"Jump to the item in XS that was most recently focused; return the cycle.
This will error when previous-index is nil. This function mutates the
underlying struct."
(let ((i (cycle-previous-index xs)))
(if (maybe-some? i)
(progn
(cycle-jump i xs)
(cycle-current xs))
(error "Cannot focus the previous element since cycle-previous-index is nil"))))
(defun cycle-next (xs)
"Return the next value in `XS' and update `current-index'."
(let* ((current-index (cycle-current-index xs))
(next-index (cycle--next-index-> 0 (cycle-count xs) current-index)))
(struct-set! cycle previous-index current-index xs)
(struct-set! cycle current-index next-index xs)
(nth next-index (cycle-xs xs))))
(defun cycle-prev (xs)
"Return the previous value in `XS' and update `current-index'."
(let* ((current-index (cycle-current-index xs))
(next-index (cycle--next-index<- 0 (cycle-count xs) current-index)))
(struct-set! cycle previous-index current-index xs)
(struct-set! cycle current-index next-index xs)
(nth next-index (cycle-xs xs))))
(defun cycle-current (cycle)
"Return the current value in `CYCLE'."
(nth (cycle-current-index cycle) (cycle-xs cycle)))
(defun cycle-count (cycle)
"Return the length of `xs' in `CYCLE'."
(length (cycle-xs cycle)))
(defun cycle-jump (i xs)
"Jump to the I index of XS."
(let ((current-index (cycle-current-index xs))
(next-index (math-mod i (cycle-count xs))))
(struct-set! cycle previous-index current-index xs)
(struct-set! cycle current-index next-index xs))
xs)
(defun cycle-focus (p cycle)
"Focus the element in CYCLE for which predicate, P, is t."
(let ((i (->> cycle
cycle-xs
(-find-index p))))
(if i
(cycle-jump i cycle)
(error "No element in cycle matches predicate"))))
(defun cycle-focus-item (x xs)
"Focus item, X, in cycle XS.
ITEM is the first item in XS that t for `equal'."
(cycle-focus (lambda (y) (equal x y)) xs))
(defun cycle-contains? (x xs)
"Return t if cycle, XS, has member X."
(->> xs
cycle-xs
(list-contains? x)))
(defun cycle-empty? (xs)
"Return t if cycle XS has no elements."
(= 0 (length (cycle-xs xs))))
(defun cycle-focused? (xs)
"Return t if cycle XS has a non-nil value for current-index."
(maybe-some? (cycle-current-index xs)))
(defun cycle-append (x xs)
"Add X to the left of the focused element in XS.
If there is no currently focused item, add X to the beginning of XS."
(if (cycle-empty? xs)
(progn
(struct-set! cycle xs (list x) xs)
(struct-set! cycle current-index 0 xs)
(struct-set! cycle previous-index nil xs))
(let ((curr-i (cycle-current-index xs))
(prev-i (cycle-previous-index xs)))
(if curr-i
(progn
(struct-set! cycle xs (-insert-at curr-i x (cycle-xs xs)) xs)
(when (and prev-i (>= prev-i curr-i))
(struct-set! cycle previous-index (1+ prev-i) xs))
(when curr-i (struct-set! cycle current-index (1+ curr-i) xs)))
(progn
(struct-set! cycle xs (cons x (cycle-xs xs)) xs)
(when prev-i (struct-set! cycle previous-index (1+ prev-i) xs))))
xs)))
(defun cycle-remove (x xs)
"Attempt to remove X from XS.
X is found using `equal'.
If X is the currently focused value, after it's deleted, current-index will be
nil. If X is the previously value, after it's deleted, previous-index will be
nil."
(let ((curr-i (cycle-current-index xs))
(prev-i (cycle-previous-index xs))
(rm-i (-elem-index x (cycle-xs xs))))
(struct-set! cycle xs (-remove-at rm-i (cycle-xs xs)) xs)
(when prev-i
(when (> prev-i rm-i) (struct-set! cycle previous-index (1- prev-i) xs))
(when (= prev-i rm-i) (struct-set! cycle previous-index nil xs)))
(when curr-i
(when (> curr-i rm-i) (struct-set! cycle current-index (1- curr-i) xs))
(when (= curr-i rm-i) (struct-set! cycle current-index nil xs)))
xs))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(when cycle-enable-tests?
(let ((xs (cycle-new 1 2 3)))
(prelude-assert (maybe-nil? (cycle-previous-focus xs)))
(prelude-assert (= 1 (cycle-current xs)))
(prelude-assert (= 2 (cycle-next xs)))
(prelude-assert (= 1 (cycle-previous-focus xs)))
(prelude-assert (= 1 (->> xs (cycle-jump 0) cycle-current)))
(prelude-assert (= 2 (->> xs (cycle-jump 1) cycle-current)))
(prelude-assert (= 3 (->> xs (cycle-jump 2) cycle-current)))
(prelude-assert (= 2 (cycle-previous-focus xs)))
(prelude-assert (= 2 (cycle-focus-previous! xs)))
(prelude-assert (equal '(1 4 2 3) (cycle-xs (cycle-append 4 xs))))
(prelude-assert (equal '(1 2 3) (cycle-xs (cycle-remove 4 xs))))
(progn
(cycle-focus-item 3 xs)
(cycle-focus-item 2 xs)
(cycle-remove 1 xs)
(prelude-assert (= 2 (cycle-current xs)))
(prelude-assert (= 3 (cycle-previous-focus xs))))))
(provide 'cycle)
;;; cycle.el ends here

View file

@ -68,8 +68,8 @@
(cl-defun fonts-cycle (&key forward?)
"Cycle forwards when `FORWARD?' non-nil."
(let ((font (if forward?
(cycle-next fonts-whitelist)
(cycle-prev fonts-whitelist))))
(cycle-next! fonts-whitelist)
(cycle-prev! fonts-whitelist))))
(message (s-concat "Active font: " font))
(fonts-set font)))
@ -93,7 +93,7 @@
"Focuses the FONT in the `fonts-whitelist' cycle.
The size of the font is determined by `fonts-size'."
(prelude-assert (cycle-contains? font fonts-whitelist))
(cycle-focus (lambda (x) (equal x font)) fonts-whitelist)
(cycle-focus! (lambda (x) (equal x font)) fonts-whitelist)
(fonts-set (fonts-current) fonts-size))
(defun fonts-ivy-select ()

View file

@ -135,7 +135,7 @@
(with-current-buffer (current-buffer)
(let ((cycle (irc-channel->cycle irc-server->channels (buffer-name))))
(erc-join-channel
(cycle-next cycle))
(cycle-next! cycle))
(irc-message
(string-format "Current IRC channel: %s" (cycle-current cycle))))))
@ -145,7 +145,7 @@
(with-current-buffer (current-buffer)
(let ((cycle (irc-channel->cycle irc-server->channels (buffer-name))))
(erc-join-channel
(cycle-prev cycle))
(cycle-prev! cycle))
(irc-message
(string-format "Current IRC channel: %s" (cycle-current cycle))))))

View file

@ -55,8 +55,8 @@ This function should be called from a buffer running vterm."
(interactive)
(vterm-mgt--assert-vterm-buffer)
(vterm-mgt-reconcile-state)
(cycle-focus-item (current-buffer) vterm-mgt--instances)
(switch-to-buffer (cycle-next vterm-mgt--instances))
(cycle-focus-item! (current-buffer) vterm-mgt--instances)
(switch-to-buffer (cycle-next! vterm-mgt--instances))
(when vterm-mgt-scroll-on-focus (end-of-buffer)))
(defun vterm-mgt-prev ()
@ -65,8 +65,8 @@ This function should be called from a buffer running vterm."
(interactive)
(vterm-mgt--assert-vterm-buffer)
(vterm-mgt-reconcile-state)
(cycle-focus-item (current-buffer) vterm-mgt--instances)
(switch-to-buffer (cycle-prev vterm-mgt--instances))
(cycle-focus-item! (current-buffer) vterm-mgt--instances)
(switch-to-buffer (cycle-prev! vterm-mgt--instances))
(when vterm-mgt-scroll-on-focus (end-of-buffer)))
(defun vterm-mgt-instantiate ()
@ -81,8 +81,8 @@ If however you must call `vterm', if you'd like to cycle through vterm
(interactive)
(vterm-mgt-reconcile-state)
(let ((buffer (vterm t)))
(cycle-append buffer vterm-mgt--instances)
(cycle-focus-item buffer vterm-mgt--instances)))
(cycle-append! buffer vterm-mgt--instances)
(cycle-focus-item! buffer vterm-mgt--instances)))
(defun vterm-mgt-kill ()
"Kill the current buffer and remove it from `vterm-mgt--instances'.
@ -106,7 +106,7 @@ instance."
(if (cycle-focused? vterm-mgt--instances)
(switch-to-buffer (cycle-current vterm-mgt--instances))
(progn
(cycle-jump 0 vterm-mgt--instances)
(cycle-jump! 0 vterm-mgt--instances)
(switch-to-buffer (cycle-current vterm-mgt--instances))))))
(defun vterm-mgt-rename-buffer (name)

View file

@ -97,12 +97,12 @@
(defun window-manager-next-workspace ()
"Cycle forwards to the next workspace."
(interactive)
(window-manager--change-workspace (cycle-next window-manager--workspaces)))
(window-manager--change-workspace (cycle-next! window-manager--workspaces)))
(defun window-manager-prev-workspace ()
"Cycle backwards to the previous workspace."
(interactive)
(window-manager--change-workspace (cycle-prev window-manager--workspaces)))
(window-manager--change-workspace (cycle-prev! window-manager--workspaces)))
;; Here is the code required to toggle EXWM's modes.
(defun window-manager--line-mode ()
@ -120,7 +120,7 @@
(interactive)
(with-current-buffer (window-buffer)
(when (eq major-mode 'exwm-mode)
(funcall (cycle-next window-manager--modes)))))
(funcall (cycle-next! window-manager--modes)))))
(defun window-manager--label->index (label workspaces)
"Return the index of the workspace in WORKSPACES named LABEL."
@ -152,10 +152,10 @@ Currently using super- as the prefix for switching workspaces."
(defun window-manager--switch (label)
"Switch to a named workspaces using LABEL."
(cycle-focus (lambda (x)
(equal label
(window-manager-named-workspace-label x)))
window-manager--workspaces)
(cycle-focus! (lambda (x)
(equal label
(window-manager-named-workspace-label x)))
window-manager--workspaces)
(window-manager--change-workspace (cycle-current window-manager--workspaces)))
(defun window-manager-toggle-previous ()