Skip to content

Commit

Permalink
feat: support :predicate arg in define-default-state
Browse files Browse the repository at this point in the history
  • Loading branch information
DCsunset committed May 31, 2023
1 parent ff0eee4 commit 8681e23
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ Note that the default state must be defined by `modaled-define-state` before you
(modaled-global-mode 1)
```

To enable the default state only in specific modes, use `:predicate` option in `modaled-defined-default-state`
(see more [:predicate usage](https://www.gnu.org/software/emacs/manual/html_node/elisp/Defining-Minor-Modes.html#index-define_002dglobalized_002dminor_002dmode)):

```emacs-lisp
; only in c++-mode
(modaled-define-default-state "normal" :predicate '(c++-mode))
; set default state except c++-mode
(modaled-define-default-state "normal" :predicate '((not c++-mode) t))
(modaled-global-mode 1)
```

To see supported arguments for each function, use `describe-function` (usually bound to `C-h f`) to see the docs.

## License
Expand Down
36 changes: 22 additions & 14 deletions modaled.el
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ This function will generate the definitions for the following items:
2. modaled-STATE-keymap: Keymap for the state.
The following options are supported:
:sparse Use a sparse keymap instead of a full keymap
:suppress Remapping `self-insert-command' to `undefined' in the keymap
:sparse Use a sparse keymap instead of a full keymap.
:suppress Remapping `self-insert-command' to `undefined' in the keymap.
:lighter Text displayed in the mode line when the state is active.
:cursor-type Cursor type for the state."
(let ((mode (modaled--get-state-mode state))
Expand All @@ -121,19 +121,27 @@ The following options are supported:
(setq-local cursor-type ,cursor-type))))))

;;;###autoload
(defmacro modaled-define-default-state (state)
"Define default STATE used in global minor mode."
(let ((mode (modaled--get-state-mode state)))
(defmacro modaled-define-default-state (state &rest body)
"Define default STATE used in global minor mode with options in BODY.
This function accept the following option:
:predicate Specify which major modes the default state should be enabled in.
It is directly passed to `define-globalized-minor-mode'."
(let ((mode (modaled--get-state-mode state))
(pred (if (plist-member body :predicate)
`(:predicate ,(plist-get body :predicate))
'())))
`(progn
(setq modaled-default-state ,state)
(define-globalized-minor-mode modaled-global-mode
,mode
(lambda ()
(unless (minibufferp)
; enable default modaled minor modes
(modaled-set-default-state)))
:require 'modaled
:group 'modaled))))
(setq modaled-default-state ,state)
(define-globalized-minor-mode modaled-global-mode
,mode
(lambda ()
(unless (minibufferp)
; enable default modaled minor modes
(modaled-set-default-state)))
:require 'modaled
:group 'modaled
,@pred))))

(provide 'modaled)

Expand Down

0 comments on commit 8681e23

Please sign in to comment.