Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
Merge branch 'modes-in-keymap'
Browse files Browse the repository at this point in the history
  • Loading branch information
cldwalker committed Jan 31, 2016
2 parents 7529d58 + 5d7e5a7 commit 74b3bbe
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 122 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,12 @@
## 0.2.1

* Normal and visual keys can be defined in .keymap instead of .behaviors
* Use :editor.keys.vim.normal.cm tag in keymap instead of deprecated lt.plugins.vim/map-keys behavior
For example, `[:app :lt.plugins.vim/map-keys {"j" "gj" "k" "gk"}]` in a behaviors file,
is `[:editor.keys.vim.normal.cm "j" "gj"] [:editor.keys.vim.normal.cm "k" "gk"]` in a keymap file.
* Use :editor.keys.vim.visual.cm tag in keymap instead of deprecated lt.plugins.vim/map-keys-visual behavior
* Add :qa ex command

## 0.2.0

* Plugin activated by default. :lt.plugins.vim/activate-vim behavior in
Expand Down
40 changes: 35 additions & 5 deletions README.md
Expand Up @@ -11,17 +11,47 @@ Vim keybindings only take effect on new files. Existing files will need to be re

## Mapping Keys

To map keys to normal mode, add a `:lt.plugins.vim/map-keys` behavior:
To map keys to normal mode, place them in a keymap under a
`:editor.keys.vim.normal.cm` or `:editor.keys.vim.normal` tag. These tags differ
in what key combos are available, how keys are referred to and what they can map
to. `:editor.keys.vim.normal` is for standard LightTable keybindings that support
standard modifier-key combos e.g. ctrl-Y and map to LightTable command(s). Do
_not_ use this tag to map individual keys, remap keys to other keys or bind to
non-standard key combos e.g. `<Space>ob`.

`:editor.keys.vim.normal.cm` is a tag that bypasses LightTable's normal keyhandling and
interacts directly with the [Vim CodeMirror plugin](https://github.com/LightTable/Vim/blob/master/vim.js).
This allows for the following more powerful but non-standard behavior:

* Key combinations are not limited to modifier keys e.g. `<Space>ob` or `jkl`
* Keys can be mapped to a string of keys or the standard LightTable command(s)
* An individual key can be a keybinding
* Special keys and modifier keys are referred to differently than in standard LightTable keybindings.
For example, `down` is `<down>` and `Ctrl-x` is `<C-x>`. See the [Vim CodeMirror keymap](https://github.com/LightTable/Vim/blob/master/vim.js#L72-L353)
for more examples.

Some examples of these two tags:

```clojurescript
[:app :lt.plugins.vim/map-keys {"j" "gj" "k" "gk"}]
;; Map a standard key combo to a command
[:editor.keys.vim.normal "alt-q" :lt.plugins.reflow/reflow]
;; Map an individual key to a LightTable command
[:editor.keys.vim.normal.cm ":" :vim.ex]
;; Remap a key to a sequence of keys
[:editor.keys.vim.normal.cm "j" "gj"]
```

For a thorough example, [see this config](https://github.com/cldwalker/ltfiles/blob/bf5ce36188219622796b794f7dcf7be4d255dd36/settings/user.behaviors#L9-L120)
For a thorough example of `:editor.keys.vim.normal.cm`, [see this config](https://github.com/cldwalker/ltfiles/blob/36dbb844a8e1b84fcc3bea64776f7b4e58138353/user.keymap#L81-L195).

To map keys to visual mode, add a `:lt.plugins.vim/map-keys-visual` behavior under your `:app` section like above.
To map keys to visual mode, place them in a keymap under a
`:editor.keys.vim.visual.cm` or `:editor.keys.vim.visual` tag. These have the
same behavior as their respective normal tags: `:editor.keys.vim.visual.cm`
supports powerful but non-standard keybindings while `:editor.keys.vim.visual`
supports standard LightTable keybindings.

CodeMirror does not support mapping to other modes at this time.
Our current vim mode version does not support mapping to other modes at this time.


## For Committers
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
@@ -1,7 +1,7 @@
{
"name": "Vim",
"author": "Kodowa",
"version": "0.2.0",
"version": "0.2.1",
"source": "https://github.com/LightTable/Vim",
"desc": "Vim keybindings for Light Table",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion project.clj
@@ -1,3 +1,3 @@
(defproject com.lighttable/vim "0.2.0"
(defproject com.lighttable/vim "0.2.1"
:description "Vim keybindings for Light Table"
:dependencies [[org.clojure/clojure "1.5.1"]])
39 changes: 39 additions & 0 deletions src/lt/plugins/vim.cljs
@@ -1,11 +1,13 @@
(ns lt.plugins.vim
(:require [lt.object :as object]
[lt.objs.context :as ctx]
[lt.objs.keyboard :as kb]
[lt.util.load :as load]
[lt.objs.editor.pool :as pool]
[lt.objs.sidebar.command :as scmd]
[lt.objs.command :as cmd :refer [command]]
[lt.objs.editor :as editor]
[lt.objs.console :as console]
[lt.objs.notifos :as notifos])
(:require-macros [lt.macros :refer [behavior]]))

Expand Down Expand Up @@ -58,6 +60,7 @@
:type :clj}]
:type :user
:reaction (fn [this ks]
(console/error (str ::map-keys " is deprecated and will be removed in 0.3.0. Instead define these keys in a keymap under the :editor.keys.vim.normal.cm tag"))
(doseq [[k v] ks]
(js/CodeMirror.Vim.map k v "normal"))))

Expand All @@ -80,6 +83,7 @@
:type :clj}]
:type :user
:reaction (fn [this ks]
(console/error (str ::map-keys-visual " is deprecated and will be removed in 0.3.0. Instead define these keys in a keymap under the :editor.keys.vim.visual.cm tag"))
(doseq [[k v] ks]
(js/CodeMirror.Vim.map k v "visual"))))

Expand All @@ -92,6 +96,27 @@
(when-not (object/has-tag? this :editor.keys.vim)
(make-vim-editor this))))


(behavior ::load-cm-normal-keys
:triggers #{:app.keys.load}
:desc "Load CodeMirror normal keys into vim keymap"
:type :user
:reaction (fn [this]
(doseq [[k v] (:editor.keys.vim.normal.cm @kb/keys)]
(js/CodeMirror.Vim.map k
(if (string? (first v)) (first v) (str ":lt_normal_key " k))
"normal"))))

(behavior ::load-cm-visual-keys
:triggers #{:app.keys.load}
:desc "Load CodeMirror visual keys into vim keymap"
:type :user
:reaction (fn [this]
(doseq [[k v] (:editor.keys.vim.visual.cm @kb/keys)]
(js/CodeMirror.Vim.map k
(if (string? (first v)) (first v) (str ":lt_visual_key " k))
"visual"))))

;; Ex commands
;; ===========
(command {:command :vim-save
Expand Down Expand Up @@ -167,6 +192,20 @@
(first)
(keyword)) (next (.-args info))))})

(defn run-commands [cmds]
(doseq [cmd cmds]
(if (sequential? cmd)
(apply cmd/exec! cmd)
(cmd/exec! cmd))))

(ex-command {:name "lt_normal_key"
:func (fn [cm info]
(run-commands (get (:editor.keys.vim.normal.cm @kb/keys) (.trim (.-argString info)))))})

(ex-command {:name "lt_visual_key"
:func (fn [cm info]
(run-commands (get (:editor.keys.vim.visual.cm @kb/keys) (.trim (.-argString info)))))})

;; TODO: Add support for interactive prompt
;; Move to main LT repo once this is done
(js/CodeMirror.defineExtension
Expand Down
8 changes: 2 additions & 6 deletions vim.behaviors
@@ -1,12 +1,8 @@
[
[:app :lt.objs.plugins/load-js ["vim.js" "vim_compiled.js"]]
[:app :lt.objs.plugins/load-keymap "vim.keymap"]
[:app :lt.plugins.vim/map-keys {"N" ":ltexec find.prev"
"n" ":ltexec find.next"
"/" ":ltexec vim.find"
":" ":ltexec vim.ex"
"=" ":ltexec smart-indent-selection"
"?" ":ltexec vim.find true"}]
[:app :lt.plugins.vim/load-cm-normal-keys]
[:app :lt.plugins.vim/load-cm-visual-keys]
[:editor :lt.plugins.vim/activate-vim]
[:editor.keys.vim :lt.plugins.vim/mode-change]
[:find-bar :lt.plugins.vim/find-bar-inactive]
Expand Down
14 changes: 10 additions & 4 deletions vim.keymap
@@ -1,6 +1,12 @@
[
[:editor.keys.vim "ctrl-r" :passthrough]
[:editor.keys.vim.normal "ctrl-d" :passthrough]
[:editor.keys.vim.normal "K" :editor.doc.toggle]
[:find-bar.vim "enter" :find.hide]
[:editor.keys.vim "ctrl-r" :passthrough]
[:editor.keys.vim.normal "ctrl-d" :passthrough]
[:editor.keys.vim.normal.cm "K" :editor.doc.toggle]
[:editor.keys.vim.normal.cm "N" :find.prev]
[:editor.keys.vim.normal.cm "n" :find.next]
[:editor.keys.vim.normal.cm "/" :vim.find]
[:editor.keys.vim.normal.cm ":" :vim.ex]
[:editor.keys.vim.normal.cm "=" :smart-indent-selection]
[:editor.keys.vim.normal.cm "?" (:vim.find true)]
[:find-bar.vim "enter" :find.hide]
]

0 comments on commit 74b3bbe

Please sign in to comment.