Skip to content

Commit

Permalink
improve(major-mode): click toggles fundamental-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Walheimat committed Apr 21, 2024
1 parent 9b9922a commit 828f060
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
50 changes: 43 additions & 7 deletions test/whale-line-segments-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -280,18 +280,54 @@

;;;; Major mode

(ert-deftest major-mode--icon ()
(bydi ((:mock whale-line-segments--decorate :return "?")
(:mock format-mode-line :return "echo"))
(ert-deftest major-mode--toggle-fundamental ()
:tags '(segments major-mode)

(should (equal '((:propertize "?" help-echo "echo" display (raise -0.135)))
(whale-line-segments--major-mode--decorated)))))
(bydi ((:always text-mode)
(:watch whale-line-segments--major-mode--before-fundamental))

(ert-with-test-buffer (:name "fundamental")

(setq major-mode 'text-mode)

(whale-line-segments--major-mode--toggle-fundamental)

(bydi-was-set-to whale-line-segments--major-mode--before-fundamental 'text-mode)

(whale-line-segments--major-mode--toggle-fundamental)

(bydi-was-called text-mode)

(bydi-was-set-to whale-line-segments--major-mode--before-fundamental nil))))

(ert-deftest major-mode--decorated ()
:tags '(segments major-mode)

(let ((whale-line-segments--major-mode--map nil))

(bydi ((:mock whale-line-segments--decorate :return "?")
(:mock format-mode-line :return "echo"))

(should (equal '((:propertize "?"
help-echo "echo"
display (raise -0.135)
mouse-face whale-line-highlight
local-map nil))
(whale-line-segments--major-mode--decorated))))))

(ert-deftest major-mode--text ()
(should (equal '((:propertize (" " mode-name " ") face whale-line-highlight))
(whale-line-segments--major-mode--text))))
:tags '(segments major-mode)

(let ((whale-line-segments--major-mode--map nil))

(should (equal '((:propertize (" " mode-name " ")
face whale-line-highlight
mouse-face whale-line-highlight
local-map nil))
(whale-line-segments--major-mode--text)))))

(ert-deftest major-mode ()
:tags '(segments major-mode)
(bydi ((:sometimes whale-line-segments--major-mode--decorated)
(:always whale-line-segments--major-mode--text))

Expand Down
34 changes: 32 additions & 2 deletions whale-line-segments.el
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,31 @@ Returns nil if not checking or if no errors were found."

;;;;; Major mode

(defvar whale-line-segments--major-mode--map
(let ((map (make-sparse-keymap)))

(define-key map [mode-line mouse-1] 'whale-line-segments--major-mode--toggle-fundamental)

map))

(defvar-local whale-line-segments--major-mode--before-fundamental nil
"The mode in this buffer before `fundamental-mode' was activated.")

(defun whale-line-segments--major-mode--toggle-fundamental ()
"Toggle `fundamental-mode' on or off."
(interactive)

(let ((current major-mode))

(if-let ((before whale-line-segments--major-mode--before-fundamental))

(progn
(funcall before)
(setq whale-line-segments--major-mode--before-fundamental nil))

(fundamental-mode)
(setq whale-line-segments--major-mode--before-fundamental current))))

(defun whale-line-segments--major-mode--decorated ()
"Get the decoration for the `major-mode'."
(and-let* ((decorated (whale-line-segments--decorate 'major-mode))
Expand All @@ -477,11 +502,16 @@ Returns nil if not checking or if no errors were found."

`((:propertize ,decorated
help-echo ,(format "%s" (format-mode-line mode-name))
display (raise -0.135)))))
display (raise -0.135)
mouse-face whale-line-highlight
local-map ,whale-line-segments--major-mode--map))))

(defun whale-line-segments--major-mode--text ()
"Get the text for the `major-mode'."
`((:propertize (" " mode-name " ") face whale-line-highlight)))
`((:propertize (" " mode-name " ")
face whale-line-highlight
mouse-face whale-line-highlight
local-map ,whale-line-segments--major-mode--map)))

(defun whale-line-segments--major-mode ()
"Get the `major-mode' segment."
Expand Down

0 comments on commit 828f060

Please sign in to comment.