6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Light Tableでカーソルをいろいろ移動する

Last updated at Posted at 2014-06-09

Light Tableの中のCodeMirrorのAPIを使うと、カーソル動かせていろいろできそう(Pluginとかで)
SyntaxをClojureScriptに設定して、Light Table UIにConnectして遊ぼう

sample.cljs
(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")

goLineLeftgoLineRightは上と似てるけど、行の折り返しがあったときには表示上の左右端まで移動になる

  (.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ができるようにする

作るのは結構簡単

  1. lein new lt-plugin cursorkitコマンドでプラグインの雛形を作る
  2. Light TableのWorkspaceに作った雛形のディレクトリを追加
  3. Light Tableでsrcディレクトリ内のcursorkit.cljsを開いて以下に書き換えて保存(自動でjsにコンパイル)
  4. cursorkit.behaviorsファイルの不要な部分(:cursorkit.hello [:lt.plugins.cursorkit/on-close-destroy])を消す
cursorkit.cljs
(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]みたいに設定できるよ!

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?