Light Tableの中のCodeMirrorのAPIを使うと、カーソル動かせていろいろできそう(Pluginとかで)
SyntaxをClojureScriptに設定して、Light Table UIにConnectして遊ぼう
(ns sample.core
(:require [lt.objs.editor :as editor]
[lt.objs.editor.pool :as pool]))
(def cm (editor/->cm-ed (pool/last-active)))
※(editor/->cm-ed (pool/last-active))
でCodeMirrorのオブジェクトが取れるらしい
.execCommandを使って移動
.execCommand
でいろいろできる。
goLineEnd
で行の最後に移動
goLineStart
で行の先頭、goLineStartSmart
で行の非空白文字の先頭に移動
(.execCommand cm "goLineEnd")
(.execCommand cm "goLineStart")
(.execCommand cm "goLineStartSmart")
goLineLeft
とgoLineRight
は上と似てるけど、行の折り返しがあったときには表示上の左右端まで移動になる
(.execCommand cm "goLineLeft")
(.execCommand cm "goLineRight")
1文字だけ左右に移動したいときはgoCharLeft
goCharRight
上下はgoLineUp
goLineDown
(.execCommand cm "goLineUp")
(.execCommand cm "goLineDown")
(.execCommand cm "goCharLeft")
(.execCommand cm "goCharRight")
goColumnLeft
は1文字左移動だけど、goCharLeft
とは違って行の先頭ではそれ以上左(つまり上の行の最後)には移動しない。
(.execCommand cm "goColumnLeft")
(.execCommand cm "goColumnRight")
.setExtendingで選択
(.setExtending cm true)
とするとカーソル移動のときに移動した部分が選択される。
行の非空白文字の先頭から行末まで選択するには、以下のようにする
(do
(.execCommand cm "goLineStartSmart")
(.setExtending cm true)
(.execCommand cm "goLineEnd")
(.setExtending cm false))
Plugin化
ショートカットキーから呼べるようにしたいので、Light TableのPluginにしてみる
上の.execCommandにあるやつはLight Tableのコマンドとして既に用意されてるので、.setExtendingができるようにする
作るのは結構簡単
-
lein new lt-plugin cursorkit
コマンドでプラグインの雛形を作る - Light TableのWorkspaceに作った雛形のディレクトリを追加
- Light Tableでsrcディレクトリ内のcursorkit.cljsを開いて以下に書き換えて保存(自動でjsにコンパイル)
- cursorkit.behaviorsファイルの不要な部分(
:cursorkit.hello [:lt.plugins.cursorkit/on-close-destroy]
)を消す
(ns lt.plugins.cursorkit
(:require [lt.objs.command :as cmd]
[lt.objs.editor :as editor]
[lt.objs.editor.pool :as pool]))
(defn cm [] (editor/->cm-ed (pool/last-active)))
(cmd/command {:command ::extend-true
:desc "cursorkit: Set Extending true"
:exec (fn [] (.setExtending (cm) true))})
(cmd/command {:command ::extend-false
:desc "cursorkit: Set Extending false"
:exec (fn [] (.setExtending (cm) false))})
そうすれば、User keymapで"ctrl-b" [:editor.line-start-smart :lt.plugins.cursorkit/extend-true :editor.line-end :lt.plugins.cursorkit/extend-false]
みたいに設定できるよ!