Skip to content

Commit

Permalink
Make regional mode checking more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
GuiltyDolphin committed Aug 9, 2021
1 parent bdd070d commit 0eb4f8e
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 43 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.org
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
** Fixed

+ keybindings will now display in their respective keymaps
+ regional checking for =org-evil= minor modes is more robust

** Removed

- dependency on =monitor=

* 0.5.0 - 2018-06-20

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ build :

test :
@cask build \
&& cask emacs -q -batch -L . -l tests/org-evil-tests.el -f ert-run-tests-batch-and-exit
&& cask emacs -q -batch -L . -l tests/org-evil-tests.el -f ert-run-tests-batch-and-exit 2>&1 | grep -v -e '^-- INSERT --$$'
1 change: 0 additions & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ dependencies installed:

+ [[https://github.com/magnars/dash.el][dash]]
+ [[https://github.com/emacs-evil/evil][evil]]
+ [[https://github.com/GuiltyDolphin/monitor][monitor]]

Once you have set up these dependencies appropriately
(and they are in your ~load-path~) you can add:
Expand Down
54 changes: 14 additions & 40 deletions org-evil-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
;;;
;;; Code:

(require 'monitor)
(require 'org)

(defgroup org-evil nil
Expand All @@ -44,24 +43,11 @@
"Perform additional initialisation for `org-evil-mode'."
;; enable default minor modes
(mapc 'funcall org-evil--default-minor-modes)
(monitor-enable 'org-evil-hook-monitor)
(add-hook 'buffer-list-update-hook
'org-evil--buffer-list-update-hook-fn)
;; need to perform an initial check to make sure the
;; correct regional modes activate, as point won't
;; have changed yet
(org-evil--init-hooks)
;; need to perform an initial check to make sure the correct
;; regional modes are active when the mode loads
(org-evil--check-region))

;; NOTE: Until monitor supports buffer-local monitors, we need
;; to handle ensuring the monitor doesn't become out-of-date.
(defun org-evil--buffer-list-update-hook-fn ()
"Ensure state of org-evil is correct after a change of buffer."
(if org-evil-mode
(and (monitor--disabled-p 'org-evil-hook-monitor)
(monitor-enable 'org-evil-hook-monitor))
(and (monitor--enabled-p 'org-evil-hook-monitor)
(monitor-disable 'org-evil-hook-monitor))))

(defun org-evil--org-mode-hook-fn ()
"Ensure `org-evil-mode' is kept up-to-date with `org-mode'."
(if (eq major-mode 'org-mode)
Expand All @@ -74,8 +60,7 @@

(defun org-evil--mode-disable-internal ()
"Clean up after org-evil."
(org-evil--disable-all-org-evil-minor-modes)
(monitor-disable 'org-evil-hook-monitor))
(org-evil--disable-all-org-evil-minor-modes))

(defvar org-evil--minor-modes nil
"Minor modes for org-evil.")
Expand Down Expand Up @@ -125,27 +110,6 @@ ARGS should be the same as in `define-minor-mode' (bar MODE and DOC)."
(add-to-list 'org-evil--regional-checkers ',check-fn))))
(put 'org-evil--define-regional-minor-mode 'lisp-indent-function 'defun)

(defvar org-evil--hook-ivar nil)

(define-monitor 'org-evil-hook-monitor 'hook
"Org-evil monitor for hooks."
:hook-ivar 'org-evil--hook-ivar)

(defvar org-evil--post-command-instance
(monitor 'org-evil-hook-monitor
:hook 'post-command-hook
:trigger 'org-evil--check-point))

(defvar org-evil--point-check-instance
(monitor 'expression-value
:expr '(point)
:pred '/=
:trigger 'org-evil--check-region))

(defun org-evil--check-point ()
"Check the current point for region change."
(monitor-run-monitor-option 'expression-value :check org-evil--point-check-instance))

(defun org-evil--check-region ()
"Check the current region with `org-evil--regional-checkers'."
(-each org-evil--regional-checkers 'funcall))
Expand All @@ -165,5 +129,15 @@ See also `evil-define-key' and `evil-define-minor-mode-key'."
(apply 'evil-define-minor-mode-key state mode key def bindings)
(apply 'evil-define-key* state (symbol-value (intern (format "%s-map" mode))) key def bindings))

(defun org-evil--post-command ()
"Perform regional checking after running commands with `org-evil-mode' enabled."
(if (not (derived-mode-p 'org-mode))
(org-evil-mode -1)
(org-evil--check-region)))

(defun org-evil--init-hooks ()
"Initialize hooks for `org-evil-mode'."
(add-hook 'post-command-hook #'org-evil--post-command nil t))

(provide 'org-evil-core)
;;; org-evil-core.el ends here
2 changes: 1 addition & 1 deletion org-evil.el
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
;; Created: 2016-08-21
;; Version: 0.5.0
;; Keywords: convenience, evil, org
;; Package-Requires: ((dash "2.19.0") (evil "0") (monitor "0") (org "9.4.4"))
;; Package-Requires: ((dash "2.19.0") (evil "0") (org "9.4.4"))

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
Expand Down
98 changes: 98 additions & 0 deletions tests/org-evil-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ Switching to org-mode when %s should cause
(let ((evil-auto-indent nil)
(org-startup-folded nil))
(insert ,text)
(switch-to-buffer (current-buffer))
;; Need to enable org-mode so some (org-mode) local variables get set
(evil-mode)
(org-mode)
,@body)))

Expand All @@ -65,6 +67,14 @@ EXPECTED is the text that should be in the buffer after running BODY with the bu
,@body (should (equal ,expected (buffer-string)))))
(put 'org-evil--test-with-expected-buffer-text 'lisp-indent-function 2)

(ert-deftest org-evil-test-setup-teardown ()
"Test that `org-evil-mode' correctly sets up and cleans up when enabled/disabled."
(with-temp-buffer
(org-mode)
(should (memq #'org-evil--post-command post-command-hook))
(fundamental-mode)
(should-not (memq #'org-evil--post-command post-command-hook))))

(ert-deftest org-evil-list-test-open-item-or-insert-above ()
"Tests for `org-evil-list-open-item-or-insert-above'."
:tags '(org-evil org-evil-list)
Expand Down Expand Up @@ -121,6 +131,94 @@ EXPECTED is the text that should be in the buffer after running BODY with the bu
(goto-char (point-min))
(org-evil-heading-open-sibling-or-insert-below t)))

(ert-deftest org-evil-heading-test-mode ()
"Tests for `org-evil-heading-mode'."
:tags '(org-evil org-evil-heading)

(org-evil--test-with-buffer-text "* X"
;; starting in heading is in heading mode
(should org-evil-heading-mode)
;; disabling org mode disables the heading mode too
(fundamental-mode)
(should-not org-evil-heading-mode))

(org-evil--test-with-buffer-text "* X"
(should org-evil-heading-mode)
;; moving to different buffer
(org-evil--test-with-buffer-text ""
(should-not org-evil-heading-mode)))

;; moving out of heading
(org-evil--test-with-buffer-text "Test\n* X"
(should org-evil-heading-mode)
(execute-kbd-macro "gg")
(should-not org-evil-heading-mode))

;; moving into heading
(org-evil--test-with-buffer-text "* X\nTest"
(should-not org-evil-heading-mode)
(execute-kbd-macro "gg")
(should org-evil-heading-mode)))

(ert-deftest org-evil-table-test-mode ()
"Tests for `org-evil-table-mode'."
:tags '(org-evil org-evil-table)

;; starting in table is in table mode
(org-evil--test-with-buffer-text "| Test |"
(should org-evil-table-mode)

;; disabling org mode when in table
(fundamental-mode)
(should-not org-evil-table-mode))

;; moving out of table
(org-evil--test-with-buffer-text "Test\n| Table |"
(should org-evil-table-mode)
(execute-kbd-macro "gg")
(should-not org-evil-table-mode))

;; moving into table
(org-evil--test-with-buffer-text "| Table |\nTest"
(should-not org-evil-table-mode)
(execute-kbd-macro "gg")
(should org-evil-table-mode)))

(ert-deftest org-evil-block-test-mode ()
"Tests for `org-evil-block-mode'."
:tags '(org-evil org-evil-block)

(org-evil--test-with-buffer-text "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE"
;; inside block
(execute-kbd-macro "k")
(should org-evil-block-mode)

;; end of block
(execute-kbd-macro "j$")
(should org-evil-block-mode)

;; at start of block
(execute-kbd-macro "gg")
(should org-evil-block-mode))

;; disabling org mode when in block
(org-evil--test-with-buffer-text "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE"
(execute-kbd-macro "k")
(should org-evil-block-mode)
(fundamental-mode)
(should-not org-evil-block-mode))

;; moving out of block
(org-evil--test-with-buffer-text "Test\n#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE"
(execute-kbd-macro "gg")
(should-not org-evil-block-mode))

;; moving into block
(org-evil--test-with-buffer-text "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE\nTest"
(should-not org-evil-block-mode)
(execute-kbd-macro "gg")
(should org-evil-block-mode)))

(ert-deftest org-evil-table-test-number-of-columns ()
"Tests for `org-evil-table-number-of-columns'."
:tags '(org-evil org-evil-table)
Expand Down

0 comments on commit 0eb4f8e

Please sign in to comment.