help.txt major-modes.txt                                              salza.dk


                            Major modes


==============================================================================
Structure

Modes have the form

    {:insert {}
     :normal {}
     :visual {}
     :syntax {}
     :init (fn [])}

Modes are "saved" in the editor state, like this:

    (editor/add-mode :my-mode mode)

or

    (swap! editor/state update ::editor/modes assoc :my-mode mode)

Major modes are associated with buffers through the ::buffer/major-modes
keyword in the buffer. It contains a list of major modes:

    ::buffer/major-modes (list :mode1 :mode2)

It defines a priority of modes to choose from when resolving a key press.

If for example the mode is :normal and a key like "f3" is pressed, the editor
will first look for a function in :normal in :mode1 defined for "f3". If there
is no hit, it will look in :mode2 etc.

Syntax highlighting will choose from the first mode in the list containing a
:syntax keyword. This allows one to add a mode only redefining parts of the
mode, like keybindings and not necessarily syntax highlighting and visa versa.

See syntax-highlighting.txt for more details around syntax highlighting.

See keybindings.txt for more details around keybindings.

==============================================================================
Simple mode for override keybindings

Executing the code below will create a very simple mode :my-mode, which only
contains changes to some keys. Escape will remove the mode from the current
buffer again. This is useful for temporary overriding keybindings.

    (def my-mode
      {:normal
        {"esc" #(editor/apply-to-buffer
                  (fn [buf] (buffer/remove-major-mode buf :my-mode)))
         "a" #(editor/message (str "You pressed a " (rand-int 10)))
         "b" #(editor/message (str "You pressed a " (rand-int 10)))}})


    (editor/add-mode :my-mode #'my-mode)
    (editor/apply-to-buffer #(buffer/add-major-mode % :my-mode))