Skip to content

Commit

Permalink
fix: update description and use spaces for indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
DCsunset committed May 30, 2023
1 parent 3324c7e commit ff0eee4
Showing 1 changed file with 70 additions and 70 deletions.
140 changes: 70 additions & 70 deletions modaled.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;;; modaled.el --- Fully customizable modal editing -*- lexical-binding: t; -*-
;;; modaled.el --- Build your own minor modes for modal editing -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2023 DCsunset
;;
Expand Down Expand Up @@ -30,110 +30,110 @@
;;; Code:

(defgroup modaled nil
"Fully customizable modal editing for Emacs."
:group 'editing
:tag "Modaled"
:prefix "modaled-"
:link '(url-link :tag "GitHub" "https://github.com/DCsunset/modaled"))
"Build your own minor modes for modal editing."
:group 'editing
:tag "Modaled"
:prefix "modaled-"
:link '(url-link :tag "GitHub" "https://github.com/DCsunset/modaled"))

(defvar modaled-default-state
nil
"Default modaled state.")
nil
"Default modaled state.")

(defvar modaled-state
nil
"Current modaled state.")
nil
"Current modaled state.")

(defun modaled--get-state-mode (state)
"Get the symbol of STATE minor mode."
(intern (format "modaled-%s-mode" state)))
"Get the symbol of STATE minor mode."
(intern (format "modaled-%s-mode" state)))

(defun modaled--get-state-keymap (state)
"Get the symbol of STATE keymap."
(intern (format "modaled-%s-keymap" state)))
"Get the symbol of STATE keymap."
(intern (format "modaled-%s-keymap" state)))

;;;###autoload
(defun modaled-set-state (state)
"Set current modaled STATE."
; disable current mode
(when modaled-state
(funcall (modaled--get-state-mode modaled-state) 0))
(when state
(funcall (modaled--get-state-mode state) 1))
(setq modaled-state state))
"Set current modaled STATE."
; disable current mode
(when modaled-state
(funcall (modaled--get-state-mode modaled-state) 0))
(when state
(funcall (modaled--get-state-mode state) 1))
(setq modaled-state state))

;;;###autoload
(defun modaled-set-default-state ()
"Set current state to default state."
(interactive)
(modaled-set-state modaled-default-state))
"Set current state to default state."
(interactive)
(modaled-set-state modaled-default-state))

;;;###autoload
(defun modaled-define-state-keys (state &rest keybindings)
"Define KEYBINDINGS for the STATE.
"Define KEYBINDINGS for the STATE.
STATE can be a single state or a list of states.
If it's a list, KEYBINDINGS will be applied to all states in list."
(let ((states (if (listp state) state `(,state))))
(dolist (st states)
(let ((keymap (modaled--get-state-keymap st)))
(pcase-dolist (`(,key . ,def) keybindings)
(eval `(define-key ,keymap ,key #',def)))))))
(let ((states (if (listp state) state `(,state))))
(dolist (st states)
(let ((keymap (modaled--get-state-keymap st)))
(pcase-dolist (`(,key . ,def) keybindings)
(eval `(define-key ,keymap ,key #',def)))))))

;;;###autoload
(defun modaled-define-global-keys (&rest keybindings)
"Define KEYBINDINGS globally."
(pcase-dolist (`(,key . ,def) keybindings)
(global-set-key key def)))
"Define KEYBINDINGS globally."
(pcase-dolist (`(,key . ,def) keybindings)
(global-set-key key def)))

;;;###autoload
(defmacro modaled-define-state (state &rest body)
"Define a new STATE minor mode with options in BODY.
"Define a new STATE minor mode with options in BODY.
This function will generate the definitions for the following items:
1. modaled-STATE-mode: Minor mode for the state.
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
: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))
(keymap (modaled--get-state-keymap state))
(keymap-doc (format "Keymap for state %s." state))
(sparse (plist-get body :sparse))
(suppress (plist-get body :suppress))
(lighter (plist-get body :lighter))
(cursor-type (plist-get body :cursor-type))
(doc (format "Modaled minor mode for state %s" state)))
`(progn
(defvar ,keymap
(if ,sparse (make-sparse-keymap) (make-keymap))
,keymap-doc)
(when ,suppress
(suppress-keymap ,keymap))
(define-minor-mode ,mode
,doc
:lighter ,lighter
:keymap ,keymap
(when ,cursor-type
(setq-local cursor-type ,cursor-type))))))
: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))
(keymap (modaled--get-state-keymap state))
(keymap-doc (format "Keymap for state %s." state))
(sparse (plist-get body :sparse))
(suppress (plist-get body :suppress))
(lighter (plist-get body :lighter))
(cursor-type (plist-get body :cursor-type))
(doc (format "Modaled minor mode for state %s" state)))
`(progn
(defvar ,keymap
(if ,sparse (make-sparse-keymap) (make-keymap))
,keymap-doc)
(when ,suppress
(suppress-keymap ,keymap))
(define-minor-mode ,mode
,doc
:lighter ,lighter
:keymap ,keymap
(when ,cursor-type
(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)))
`(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))))
"Define default STATE used in global minor mode."
(let ((mode (modaled--get-state-mode state)))
`(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))))

(provide 'modaled)

Expand Down

0 comments on commit ff0eee4

Please sign in to comment.