LoginSignup
10
8

More than 5 years have passed since last update.

AtomでClojureの開発環境を構築する

Posted at

Atom Clojure Setup
を参考にすると、AtomでClojureのコードを書く環境がある程度できますが
さらに便利にするために、以下を行うといい感じの環境になります。

追加で導入するパッケージ

  • maximize-panes 別Paneで開くREPLタブの表示/非表示がctrl-shift-enterでできて便利

パッケージの設定

  • language-clojureのNon Word Charactersは ()",;~@[]{}` に
  • lisp-pareditのIndentation Formsはtry, catch, finally, /^let/, are, /^def/, fn, case, /^condp?/, /^if/, for, /^when/, testing, /^do/, loop, ns, binding, /^with/, comment, proxy, reify, locking, future, /^extend/, while
  • proto-replのLein Pathを設定する

Atomの設定

  • Packages With Keymaps Disabledの設定にinkも加えて、lisp-paredit, ink

init.cljsとkeymap.csonの設定

Atomのinit.coffeeをinit.cljs(ClojureScript)で置き換える方法 を導入したうえで

init.cljs
(def workspace global.atom.workspace)
(def commands global.atom.commands)
(def views global.atom.views)

(defn get-editor [] (.getActiveTextEditor workspace))
(defn get-editor-view [] (.getView views (get-editor)))
(defn get-cursor [] (.getLastCursor (get-editor)))
(defn get-cursors [] (.getCursors (get-editor)))

(defn add-command [name & fs]
  (.add commands "atom-text-editor" name #(doseq [f fs] (f))))
(defn dispatch [name] (.dispatch commands (get-editor-view) name))


; S式の選択
(add-command "cljs:select-sexp"
             #(dispatch "lisp-paredit:up-sexp")
             #(dispatch "lisp-paredit:expand-selection"))

; S式内部の選択
(add-command "cljs:select-sexp-inside"
             #(dispatch "core:move-left")
             #(dispatch "core:move-right")
             #(dispatch "bracket-matcher:select-inside-brackets"))

; Enter
(add-command "cljs:enter"
             #(if (.isAtEndOfLine (get-cursor))
                (dispatch "editor:newline")
                (dispatch "lisp-paredit:newline")))

; コード実行
(add-command "cljs:execute-block"
             #(if (pos? (count (.getSelectedText (get-editor))))
                (dispatch "proto-repl:execute-selected-text")
                (dispatch "proto-repl:execute-top-block")))

; 自動で閉じる挙動をOFFにしてシングルクオート
(add-command "cljs:single-quote"
             #(doseq [s (.getSelections (get-editor))]
                (.insertText s "'")))

; 選択部分のマクロ展開
(add-command "cljs:macro-expand"
             #(let [s (.getLastSelection (get-editor))
                    code (str "(macroexpand-1 '" (.getText s) ")")
                    options (clj->js {:inlineOptions {:editor (get-editor)
                                                      :range (.getBufferRange s)}})]
                (.executeCodeInNs global.protoRepl code options)))

keymap.cson
'atom-text-editor':
  'ctrl-q': 'tree-view:toggle'
  'ctrl-t': 'tool-bar:toggle'

'atom-workspace atom-text-editor[data-grammar~="clojure"].autocomplete-active':
  'enter': 'autocomplete-plus:confirm'

'atom-text-editor[data-grammar~="clojure"]:not([mini])':
  'enter': 'cljs:enter'
  'shift-enter': 'cljs:execute-block'
  'f4': 'cljs:select-sexp'
  'f5': 'cljs:select-sexp-inside'
  'ctrl-i': 'lisp-paredit:indent'
  'ctrl-d': 'proto-repl:print-var-documentation'
  'ctrl-delete': 'editor:delete-to-end-of-line'
  'insert right': 'lisp-paredit:slurp-forwards'
  'alt-c': 'inline-results:clear-all'
  'ctrl-e': 'cljs:macro-expand'
  '&': 'cljs:single-quote'
  • keymap.csonの'&': 'cljs:single-quote'は、日本語キーボードでの(?)シングルクオートのキーで発行されるコマンドです。
10
8
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
10
8