Skip to content

Commit e5321ee

Browse files
geksilladvcrn
authored andcommitted
feat(core): Provide option to chose input mode for non-vim users. (dvcrn#222)
* Added option `proton.core.inputProvider` with default value `:vim-mode-plus`. Available options are `:vim-mode-plus`, `:vim-mode`, `:atomic-emacs`, `:default` * Added deprecated warning for `proton.core.vim-provider` option. * Added options `proton.core.leaderKey` and `proton.core.modeKey` to configure leader and major mode alias keystrokes * Added info regarding `proton.core.leaderKey`, `proton.core.modeKey`, `proton.core.inputProvider` to core layer Readme.
1 parent 5130f3c commit e5321ee

File tree

8 files changed

+83
-34
lines changed

8 files changed

+83
-34
lines changed

plugin/templates/proton.edn

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
["proton.core.relativeLineNumbers" false]
8989

9090
;; prefer classic vim mode over vim-mode-plus? Change this to :vim-mode
91-
["proton.core.vim-provider" :vim-mode-plus]
91+
["proton.core.inputProvider" :vim-mode-plus]
9292
]
9393

9494
;; Don't like a keybinding or want to add something yourself? Do it here

src/cljs/proton/config/editor.cljs

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
["welcome.showOnStartup" false]
66

77
;; Better default font ;)
8-
["editor.fontFamily" "Hack"]]
8+
["editor.fontFamily" "Hack"]
9+
["proton.core.leaderKey" "space"]
10+
["proton.core.modeKey" ","]
11+
["proton.core.inputProvider" :vim-mode-plus]]
912

1013
:keymaps
1114
[]

src/cljs/proton/core.cljs

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
;; set the user config
174174
(atom-env/insert-process-step! "Applying user configuration")
175175
(doall (map #(apply atom-env/set-config! %) all-configuration))
176+
(proton/show-deprecated-configs all-configuration)
176177
(atom-env/mark-last-step-as-completed!)
177178

178179
;; Make sure all collected packages are definitely enabled

src/cljs/proton/layers/core/README.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ The core layer should get included by default. No installation needed.
88

99
### Configuration
1010

11-
| Name | Default | Type | Description |
12-
|-----------------------------------|----------------|-------------|--------------------------------------------------------------------------------------------|
13-
| `proton.core.showTabBar` | false | __boolean__ | whether the tab bar should be visible by default |
14-
| `proton.core.relativeLineNumbers` | false | __boolean__ | whether to use relative line numbers instead of absolute ones |
15-
| `proton.core.vim-provider` | :vim-mode-plus | __keyword__ | which vim emulation provider to use. Possible options are `:vim-mode-plus` and `:vim-mode` |
16-
| `proton.core.wipeUserConfigs` | true | __boolean__ | always reset atom configuration before applying conifgs from layers and `~/.proton` |
17-
| `proton.core.whichKeyDelay` | 0.4 | __number__ | which-key modal delay in seconds |
18-
| `proton.core.whichKeyDelayOnInit` | false | __boolean__ | which-key modal delay for first show up and no delay when shown |
19-
| `proton.core.whichKeyDisabled` | false | __boolean__ | which-key modal always hidden |
11+
| Name | Default | Type | Description |
12+
|-----------------------------------|----------------|-------------|---------------------------------------------------------------------------------------------------------------------------------------------|
13+
| `proton.core.showTabBar` | false | __boolean__ | whether the tab bar should be visible by default |
14+
| `proton.core.relativeLineNumbers` | false | __boolean__ | whether to use relative line numbers instead of absolute ones |
15+
| `proton.core.vim-provider` | :vim-mode-plus | __keyword__ | which vim emulation provider to use. Possible options are `:vim-mode-plus` and `:vim-mode` |
16+
| `proton.core.wipeUserConfigs` | true | __boolean__ | always reset atom configuration before applying conifgs from layers and `~/.proton` |
17+
| `proton.core.whichKeyDelay` | 0.4 | __number__ | which-key modal delay in seconds |
18+
| `proton.core.whichKeyDelayOnInit` | false | __boolean__ | which-key modal delay for first show up and no delay when shown |
19+
| `proton.core.whichKeyDisabled` | false | __boolean__ | which-key modal always hidden |
20+
| `proton.core.inputProvider` | :vim-mode-plus | __keyword__ | editor input. available options are :vim-mode-plus, :vim-mode, :emacs, :default. You need to reload editor to apply changes properly |
21+
| `proton.core.leaderKey` | "space" | __string__ | proton leader key binding. For non-vim modes its value is `alt-m` if not set manualy by user |
22+
| `proton.core.modeKey` | "," | __string__ | proton major mode key alias to quickly invoke `SPC m`. For non-vim modes its value is `ctrl-alt-m` if not set by user |
2023

2124

2225
### Key Bindings

src/cljs/proton/layers/core/core.cljs

+9-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
[proton.lib.atom :as atom-env :refer [get-config set-config! set-keymap!]]
1010
[cljs.nodejs :as node]))
1111

12+
(defn- add-packages [p]
13+
(swap! packages #(into [] (concat % p))))
14+
1215
(def keymaps (atom
1316
[{:selector "body" :keymap [["ctrl-j" "core:move-down"]
1417
["ctrl-k" "core:move-up"]]}
@@ -26,7 +29,6 @@
2629
["proton.core.relativeLineNumbers" false]
2730
["proton.core.quickOpenProvider" :normal]
2831
["proton.core.post-init-timeout" 3000]
29-
["proton.core.vim-provider" :vim-mode-plus]
3032
["proton.core.wipeUserConfigs" true]
3133
["proton.core.whichKeyDelay" 0.4]
3234
["proton.core.whichKeyDelayOnInit" false]
@@ -69,9 +71,12 @@
6971
(swap! state assoc-in [:relative-numbers] (config-map "proton.core.relativeLineNumbers"))
7072
(swap! state assoc-in [:tabs] (config-map "proton.core.showTabBar"))
7173

72-
(case (config-map "proton.core.vim-provider")
73-
:vim-mode (swap! packages #(into [] (concat % [:vim-mode :vim-surround])))
74-
:vim-mode-plus (swap! packages #(into [] (concat % [:vim-mode-plus :vim-mode-plus-ex-mode]))))))
74+
;; install additional packages based on proton.core.inputProvider if needed
75+
(case (config-map "proton.core.inputProvider")
76+
:vim-mode (add-packages [:vim-mode :vim-surround])
77+
:vim-mode-plus (add-packages [:vim-mode-plus :vim-mode-plus-ex-mode])
78+
:emacs (add-packages [:atomic-emacs])
79+
:default)))
7580

7681
(defmethod init-package [:core :theme-switch] []
7782
(let [core-themes (string/join " " (atom-env/get-config "core.themes"))

src/cljs/proton/lib/atom.cljs

+12-10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
(def grammars (.-grammars (.-grammars js/atom)))
2424
(def workspace-view (.getView views workspace))
2525
(def packages (.-packages js/atom))
26+
(def notifications (.-notifications js/atom))
2627

2728
(def element (atom (generate-div "test" "proton-which-key")))
2829
(def bottom-panel (atom (.addBottomPanel workspace
@@ -99,20 +100,22 @@
99100
(amend-last-step! (str (get (last @steps) 0)) "<span class='proton-status-ok'>[ok]</span>"))
100101

101102
(defn input-provider-class []
102-
(if-let [selected-provider (get-config "proton.core.vim-provider")]
103+
(if-let [selected-provider (get-config "proton.core.inputProvider")]
103104
(do
104105
(case selected-provider
105106
"vim-mode" ["vim-mode"]
106-
"vim-mode-plus" ["vim-mode-plus"]))
107-
[""]))
107+
"vim-mode-plus" ["vim-mode-plus"]
108+
[]))
109+
[]))
108110

109111
(defn editor-toggle-classes [class-list remove?]
110-
(let [editors (.getTextEditors workspace)]
111-
(doseq [editor editors]
112-
(let [editor-view (.getView views editor)
113-
classList (.-classList editor-view)
114-
class-list-fn (fn [class-name] (if remove? (.remove classList class-name) (.add classList class-name)))]
115-
(doall (map class-list-fn class-list))))))
112+
(when (not (empty? class-list))
113+
(let [editors (.getTextEditors workspace)]
114+
(doseq [editor editors]
115+
(let [editor-view (.getView views editor)
116+
classList (.-classList editor-view)
117+
class-list-fn (fn [class-name] (if remove? (.remove classList class-name) (.add classList class-name)))]
118+
(doall (map class-list-fn class-list)))))))
116119

117120
(defn activate-proton-mode! []
118121
(console! "Chain activated!")
@@ -170,7 +173,6 @@
170173
(swap! parsed-config conj (str config-prefix "." config-postfix))))))))
171174
@parsed-config))
172175

173-
174176
(defn set-keymap!
175177
([selector bindings]
176178
(set-keymap! selector bindings 0))

src/cljs/proton/lib/helpers.cljs

+2-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@
143143
ignored-editor-classes #{"insert-mode" "mini"}
144144
ignored-editor-class-ok? (empty? (cljset/intersection class-list ignored-editor-classes))
145145
required-editor-classes #{"vim-mode" "vim-mode-plus"}
146-
required-editor-class-ok? (not (empty? (cljset/intersection class-list required-editor-classes)))]
146+
vim-mode? (some #{(js/atom.config.get "proton.core.inputProvider")} ["vim-mode" "vim-mode-plus"])
147+
required-editor-class-ok? (or (not (empty? (cljset/intersection class-list required-editor-classes))) (not vim-mode?))]
147148
(if tag-ok?
148149
(if (= tag-name "atom-text-editor")
149150
(and required-editor-class-ok? ignored-attrs-ok? ignored-editor-class-ok?)

src/cljs/proton/lib/proton.cljs

+42-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#_(:require [proton.core])
33
(:require [cljs.reader :as reader]
44
[cljs.nodejs :as node]
5-
[clojure.string :as string :refer [upper-case join]]
5+
[clojure.string :as string :refer [upper-case join starts-with?]]
66
[proton.lib.mode :as mode-manager]
77
[proton.lib.helpers :as helpers]
88
[proton.lib.atom :as atom-env]
@@ -90,17 +90,51 @@
9090
ret-map (reduce #(assoc %1 %2 command) {} (map identity ret))]
9191
(atom-env/set-keymap! selector ret-map 100)))
9292

93+
(defn- vim-input? [provider] (some #{provider} [:vim-mode :vim-mode-plus]))
94+
95+
(defn- configure-leader-keys [config-map]
96+
(let [vim-provider? (vim-input? (config-map "proton.core.inputProvider"))
97+
current-keys [(config-map "proton.core.leaderKey") (config-map "proton.core.modeKey")]
98+
vim-keys ["space" ","]
99+
non-vim-keys ["alt-m" "ctrl-alt-m"]]
100+
(cond
101+
(and vim-provider? (= non-vim-keys current-keys)) vim-keys
102+
(and (not vim-provider?) (= vim-keys current-keys)) non-vim-keys
103+
:else current-keys)))
104+
105+
(defn- clear-conflicted-keystrokes! [keystrokes]
106+
(let [reg (re-pattern (str "^(" (join "|" keystrokes) ")"))]
107+
(set! (.-keyBindings atom-env/keymaps)
108+
(clj->js
109+
(filter
110+
#(not (and (re-find reg (.-keystrokes %)) (not (re-find (re-pattern "^proton:") (.-command %)))))
111+
(array-seq atom-env/keymaps.keyBindings))))))
112+
93113
(defn init-proton-leader-keys! [configs]
94114
(let [config-map (into (hash-map) configs)
95-
selectors ["body atom-workspace:not(.proton-mode) atom-text-editor:not([mini]):not(.insert-mode)"
96-
"body atom-workspace:not(.proton-mode) atom-panel-container.left"
97-
"body atom-workspace:not(.proton-mode) atom-panel-container.right"
98-
"body atom-workspace:not(.proton-mode) atom-panel-container.bottom"
99-
"atom-workspace:not(.proton-mode)"]
115+
vim-provider? (vim-input? (config-map "proton.core.inputProvider"))
116+
selectors (conj
117+
["body atom-workspace:not(.proton-mode) atom-panel-container.left"
118+
"body atom-workspace:not(.proton-mode) atom-panel-container.right"
119+
"body atom-workspace:not(.proton-mode) atom-panel-container.bottom"
120+
"atom-workspace:not(.proton-mode)"]
121+
(if vim-provider?
122+
"body atom-workspace:not(.proton-mode) atom-text-editor:not([mini]):not(.insert-mode)"
123+
"body atom-workspace:not(.proton-mode) atom-text-editor:not([mini])"))
100124
selector (string/join ", " selectors)
101125
leader-command "proton:toggle"
102-
proton-leader-key "space"
103-
proton-mode-key (name (nth proton.core/mode-keys 1))
126+
proton-keys (configure-leader-keys config-map)
127+
proton-leader-key (proton-keys 0)
128+
proton-mode-key (proton-keys 1)
104129
mode-command "proton:toggleMode"
105130
keymap (hash-map proton-leader-key leader-command proton-mode-key mode-command)]
131+
(when (not vim-provider?)
132+
(clear-conflicted-keystrokes! [proton-leader-key proton-mode-key]))
106133
(atom-env/set-keymap! selector keymap 100)))
134+
135+
(defn show-deprecated-configs [configs]
136+
(let [config-map (into (hash-map) configs)]
137+
(when (config-map "proton.core.vim-provider")
138+
(.addInfo atom-env/notifications "proton-mode: deprecated config <strong>proton.core.vim-provider</strong>"
139+
(clj->js {:detail "Please use proton.core.inputProvider"
140+
:dismissable true})))))

0 commit comments

Comments
 (0)