Refactor MRU EXWM workspace using cycle/focus-previous!

Instead of consuming `cycle/previous-focus`, define a function
`cycle/focus-previous!` that "focuses" the element at `previous-index` and
returns that element.

This function greatly simplified the code in window-manager.el and eliminated
the unnecessary `exwm/previous-workspace` variable that was managing the state.
This commit is contained in:
William Carroll 2020-01-09 14:01:49 +00:00
parent 1399eae319
commit 71c24104eb
2 changed files with 35 additions and 22 deletions

View file

@ -75,6 +75,19 @@
(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))
@ -135,7 +148,8 @@
(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/previous-focus xs)))
(prelude/assert (= 2 (cycle/focus-previous! xs)))))
(provide 'cycle)
;;; cycle.el ends here

View file

@ -249,22 +249,12 @@
(defun exwm/next-workspace ()
"Cycle forwards to the next workspace."
(interactive)
(exwm-workspace-switch
(exwm/named-workspace-index (cycle/next exwm/workspaces)))
(window-manager/alert
(string/concat
"Current workspace: "
(exwm/named-workspace-label (cycle/current exwm/workspaces)))))
(exwm/change-workspace (cycle/next exwm/workspaces)))
(defun exwm/prev-workspace ()
"Cycle backwards to the previous workspace."
(interactive)
(exwm-workspace-switch
(exwm/named-workspace-index (cycle/prev exwm/workspaces)))
(window-manager/alert
(string/concat
"Current workspace: "
(exwm/named-workspace-label (cycle/current exwm/workspaces)))))
(exwm/change-workspace (cycle/prev exwm/workspaces)))
;; TODO: Create friendlier API for working with EXWM.
@ -534,17 +524,26 @@ Currently using super- as the prefix for switching workspaces."
(kbd/for 'workspace (s-capitalize key))
handler)))
(defun exwm/change-workspace (workspace)
"Switch EXWM workspaces to the WORKSPACE struct."
(exwm-workspace-switch (exwm/named-workspace-index workspace))
(window-manager/alert
(string/format "Switched to: %s" (exwm/named-workspace-label workspace))))
(defun exwm/switch (label)
"Switch to a named workspaces using LABEL."
(cycle/focus
(lambda (x)
(equal label
(exwm/named-workspace-label x)))
exwm/workspaces)
(exwm-workspace-switch
(exwm/named-workspace-index (cycle/current exwm/workspaces)))
(window-manager/alert
(string/concat "Switched to: " label)))
(cycle/focus (lambda (x)
(equal label
(exwm/named-workspace-label x)))
exwm/workspaces)
(exwm/change-workspace (cycle/current exwm/workspaces)))
;; TODO: Assign an easy-to-remember keybinding to this.
(exwm-input-set-key (kbd "C-S-f") #'exwm/toggle-previous)
(defun exwm/toggle-previous ()
"Focus the previously active EXWM workspace."
(interactive)
(exwm/change-workspace (cycle/focus-previous! exwm/workspaces)))
(defun exwm/ivy-switch ()
"Use ivy to switched between named workspaces."