#+title: EXPLANATION

* Basic Concepts
In this section we walk through a list of basic concepts in design.

** Selection

To get a better understanding on movement commands in NORMAL state, it's important to know what a *selection* is.
We call it selection to distinguish from region.

A selection is a *region* with the *direction* (forward/backward), the *type* (char/word/line/etc...) and an *expandable flag*.

For example, we can say we have a selection which is
- from point 7 to 12
- with forward direction
- with the type *word*
- and is expandable.

And there are 3 rules that decide the behavior of a movement
- *RULE 1* commands except for char/word/symbol have no opinion on direction, they will follow the current direction when meet same selection type.
- *RULE 2* a selection will be expanded if the movement and current selection have the same type, and the current selection is expandable.
- *RULE 3* these commands create an expandable selection:
  - ~meow-*-expand~
  - ~meow-mark-word~
  - ~meow-mark-symbol~
  - ~meow-line~

It sounds complicated at first. But it will be seamless once you get used to it.

A few cases to help you understand.

*CASE 1* To select 3 lines, do ~meow-line~ 3 times. ~meow-line~ will create an expandable line selection.

*CASE 2* To reverse select 3 lines, do ~negative-argument~, then ~meow-line~ 3 times. ~line~ will follow the direction.

*CASE 3* To select the previous, current and next words, do ~meow-mark-word~, ~meow-back-word~ and ~meow-next-word~.
Back and next will expand, because ~meow-mark-word~ creates an *expandable word* selection.

*CASE 4* To select the next word, do ~meow-next-word~ twice.
The current word won't be selected because ~meow-next-word~ will create a *nonexpandable word* selection.

** Secondary Selection

Emacs has built-in secondary selection support. By default, you create a secondary selection by dragging with Alt + left mouse button.
It can be a range or just a single point.

Since the secondary selection has no relation to the cursor, there are a lot features built on top of it.

*FEATURE 1* For text swap. See command ~meow-swap-grab~ and ~meow-sync-grab~.

*FEATURE 2* To represent a range for kmacro application. See Meow BEACON state.

*FEATURE 3* To mark a text to be inserted in the minibuffer prompt later. See ~meow-grab-fill-commands~.

** Multiedit

*** Ways to multiedit

There are many ways to multiedit in Emacs. Following are the parts I know:

**** Evil's n.n.n.n. (Evil built-in)
- pros: supports simple transformation, no extra keys
- cons: not for complex transformation, hard to undo

counter example:
#+begin_example
1 2 3
=>
[| "1" |] [| "2" |] [| "3" |]
#+end_example
You can do it, but it's not as simple as it should be.

**** Evil's :exe (Evil built-in)
- pros: flexible, great compatibility
- cons: verbose, bad visual feedback

Same counter case as with kmacro

**** kmacro (built-in)
- pros: no lag on recording, great compatibility, flexible, arbitrary operation, macros can be used later
- cons: verbose, can only be applied to a single position, or each line in a region, no visual feedback, hard to undo

counter example:
#+begin_example
x-y-foo-bar-baz
=>
x_y_foo_bar_baz
#+end_example
More keys (record, finish, call) make it useless in a very simple case.

**** query-replace (built-in)
- pros: simple, interactive application (y/n/!), fastest, good visual feedback (with anzu package)
- cons: not flexible, have to type more keys

counter example:
#+begin_example
foo-1-bar
foo-2-bar
foo-3-bar
=>
bar-1-foo
bar-2-foo
bar-3-foo
#+end_example

You can do it with query-replace, but typing regexp ~foo-\([0-9]\)-bar~ and ~bar-\1-foo~ requires more keys.

**** rectangle-mark-mode (built-in)
- pros: easy to use for insertion
- cons: not flexible

counter example:
#+begin_example
foo bar foo
bar foo bar
=>
foo baz foo
baz foo baz
#+end_example
Just can't do things like this

**** iedit / evil-multiedit (package)
- pros: less keys, easy to specify affected range, good visual feedback
- cons: no arbitrary transformation, only for same occurs

counter example:
#+begin_example
1 2 3
=>
"1" "2" "3"
#+end_example

**** multiple-cursors / evil-mc (package)
- pros: flexible, good visual feedback, intuitive
- cons: lag for many cursors, operation not re-useable, bad compatibility

counter example: whenever number of cursors > 100

After each time you type, multiple cursors has to run hooks, backup/restore variables for all cursors

*** The answer from Meow

Meow embraces kmacro, and tries to improve the experience by collapsing undo boundary and introducing BEACON state.

(text-mode is used here, no additional setup required, assuming meow-setup for Qwerty is used)

**** case 1

#+begin_example
1 2 3
=>
[| "1" |] [| "2" |] [| "3" |]
#+end_example

[[file:https://user-images.githubusercontent.com/11796018/144555848-2ec72117-231a-4e5e-a954-a5ae59638b06.gif]]

- select the whole content, then activate a secondary selection with ~G(meow-grab)~
- ~b(back-word)~ to create fake cursors at each word beginning
- ~F3~ to start macro recording
- type
- ~F4~ to end macro recording and apply to all fake cursors

**** case 2

#+begin_example
x-y-foo-bar-baz
=>
x_y_foo_bar_baz
#+end_example

[[file:https://user-images.githubusercontent.com/11796018/144555855-78d98a7d-fc1a-4399-8dee-467516ffbae3.gif]]

- select the whole content, then activate a secondary selection with ~G(meow-grab)~
- ~-(negative-argument) f(meow-find) -~ to search backwards for the character ~-~, which will create a fake cursor at each ~-~
- quick start recording and switch to insert state with ~c(meow-change)~ (the character under the current cursor is deleted)
- type ~_~
- ~ESC~ to go back to NORMAL, then the macro will be applied to all fake cursors.

**** case 3

#+begin_example
foo-1-bar
foo-2-bar
foo-3-bar
=>
bar-1-foo
bar-2-foo
bar-3-foo
#+end_example

[[file:https://user-images.githubusercontent.com/11796018/144555869-3ad3f571-3762-4805-8778-26c3bc45151a.gif]]

- select the whole content, then activate a secondary selection with ~G(meow-grab)~
- ~x(meow-line)~ to create fake regions at each line
- ~F3~ to start macro recording (default fake cursors are in the same column)
- select bar with ~w(mark-word)~, then activate a secondary selection with ~G(meow-grab)~
- select foo, swap with the secondary selection with ~R(meow-swap-grab)~.
- ~F4~ to end macro recording and apply to all fake cursors

**** case 4
#+begin_example
foo bar foo
bar foo bar
=>
foo baz foo
baz foo baz
#+end_example

[[file:https://user-images.githubusercontent.com/11796018/144555875-1e59ada8-0f17-43aa-bf9d-f5c0883859ff.gif]]

- select the whole content, then activate a secondary selection with ~G(meow-grab)~
- move to bar, select it with ~w(mark-word)~ , create fake regions at each bar
- quick start recording and switch to insert state with ~c(meow-change)~ (current bar is deleted)
- type baz
- ~ESC~ to go back to NORMAL, then the macro will be applied to all fake regions.

* Reason

Why another modal editing package in Emacs?

Emacs is the one editor with the most modal editing schemes in the world. Before I started working on Meow, there were a few options (listed at the end).

Unfortunately, none of them satisfy me. I want a modal editing with the following features.

- Customizable command layout
- Using existing keymap (both buit-in and third party) without modifier keys
- A set of efficient commands
- Lightweight, fast startup time

|              | customizable command layout | using existing keymap | efficient command set | Lightweight |
|--------------+-----------------------------+-----------------------+-----------------------+-------------+
| evil         | no                          | no                    | yes                   | no          |
| xah-fly-keys | no                          | no                    | yes                   | yes         |
| boon         | no                          | yes                   | yes                   | yes         |
| god-mode     | yes                         | yes                   | no                    | yes         |
| modalka      | yes                         | possible              | no                    | yes         |
| ryo-modal    | yes                         | possible              | no                    | yes         |
| kakoune.el   | no                          | no                    | yes                   | yes         |

* Why not use an existing package?

** Evil

[[https://github.com/emacs-evil/evil][Evil]]

A complete Vim emulator in Emacs. Before Emacs, I was using Vim. So my Emacs journey started with Evil.
However, Evil has a few problems.

1. high cost on integration with other packages. Basically an editing-related package won't play well with Evil if it doesn't know Evil.
So there's [[https://github.com/emacs-evil/evil-collection][evil-collection]] and other evil-* packages.

2. Communities like spacemacs and doom emacs prefer to organize keybindings with evil-leader. The result is easy to use, but it takes time to maintain.
Introducing another keybinding system usually results in a complex configuration.

3. Vim is designed for the Qwerty keyboard layout. H/J/K/L is meaningless on other layouts.

4. Vim is old, and there are some modern alternatives, like Kakoune. We are on Emacs and we have more choices.

5. Evil is heavy, its startup time is 10X longer than other modal editing packages.

** God Mode

[[https://github.com/emacsorphanage/god-mode][God Mode]]

God mode is small, simple and easy to start with. If "no modifier keys" is all you want, god-mode is a good choice.

The only problem: it's not a complete modal editing solution.
God mode lacks a set of commands which is necessary for maximizing the benefits of single-key commands.

** Modalka

[[https://github.com/mrkkrp/modalka][Modalka]]

Modalka allows the user to define their own command layout. It's more flexible than god-mode and requires more configuration.
It has the same problem as with God Mode, it's not a complete modal editing solution.

** ryo-modal

[[https://github.com/Kungsgeten/ryo-modal][ryo-modal]]

The same problem as with modalka and God Mode.

** Xah-fly-keys

[[http://xahlee.info/emacs/misc/ergoemacs_vi_mode.html][xah-fly-keys]]

Xah-fly-keys is declared to be more efficient than vim, or any keybinding set in history.
Personally I don't like DWIM (Do What I Mean) style commands.
Of course DWIM can reduce the number of commands, but I'd rather make commands more orthogonal.

xah-fly-keys has a pre-defined leader keymap, I don't think it's enough for my case.

** boon

[[https://github.com/jyp/boon][boon]]

Boon has an efficient command layout, a good approach to executing commands without modifier keys.
It's very close to what I wanted before I started with Meow.

Boon integrates with expand-region, multiple-cursors, avy, etc. I think we can have a better way for these purposes.

** Kakoune.el

[[https://github.com/jmorag/kakoune.el][kakoune.el]]

Kakoune.el is a package trying to bring kakoune's commands to Emacs.

I like how kakoune deals with selections, but it uses both alt & shift a lot.
Since I am going to use modal editing, I prefer to avoid modifier as much as possible.
