fluentd-ui 0.3.13 → 0.3.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (287) hide show
  1. data/ChangeLog +9 -0
  2. data/Gemfile.lock +47 -44
  3. data/README.md +4 -4
  4. data/app/assets/javascripts/vue/in_tail_format.js +7 -2
  5. data/app/assets/stylesheets/common.css.scss +16 -0
  6. data/app/controllers/concerns/setting_history_concern.rb +6 -0
  7. data/app/controllers/fluentd/settings_controller.rb +37 -8
  8. data/app/models/fluent_gem.rb +6 -3
  9. data/app/models/fluentd/agent.rb +1 -0
  10. data/app/models/fluentd/agent/common.rb +4 -0
  11. data/app/models/fluentd/agent/fluentd_gem.rb +9 -13
  12. data/app/models/fluentd/agent/local_common.rb +23 -0
  13. data/app/models/fluentd/agent/td_agent.rb +4 -0
  14. data/app/models/fluentd/agent/td_agent/macosx.rb +0 -4
  15. data/app/models/fluentd/agent/td_agent/unix.rb +0 -4
  16. data/app/views/fluentd/settings/edit.html.haml +1 -3
  17. data/app/views/fluentd/settings/histories/show.html.haml +10 -0
  18. data/app/views/fluentd/settings/running_backup/show.html.haml +11 -0
  19. data/app/views/shared/settings/_diff.html.erb +21 -0
  20. data/circle.yml +5 -0
  21. data/config/application.rb +1 -0
  22. data/config/locales/translation_en.yml +7 -0
  23. data/config/locales/translation_ja.yml +7 -0
  24. data/fluentd-ui.gemspec +1 -0
  25. data/lib/fluentd-ui.rb +5 -1
  26. data/lib/fluentd-ui/version.rb +1 -1
  27. data/spec/features/fluentd/setting/histories_spec.rb +23 -1
  28. data/spec/features/fluentd/setting/running_backup_spec.rb +22 -1
  29. data/spec/models/fluentd/agent/local_common_spec.rb +4 -2
  30. data/spec/models/fluentd/agent_spec.rb +1 -48
  31. data/spec/models/plugin_spec.rb +2 -2
  32. data/spec/spec_helper.rb +10 -0
  33. data/spec/support/fluentd_agent_common_behavior.rb +53 -0
  34. data/vendor/assets/javascripts/bower/.gitignore +10 -0
  35. data/vendor/assets/javascripts/bower/vue/.bower.json +4 -4
  36. data/vendor/assets/javascripts/bower/vue/bower.json +17 -0
  37. data/vendor/assets/javascripts/bower/vue/dist/vue.js +329 -198
  38. data/vendor/assets/javascripts/bower/vue/dist/vue.min.js +3 -3
  39. metadata +107 -291
  40. checksums.yaml +0 -7
  41. data/vendor/assets/javascripts/bower/codemirror/AUTHORS +0 -423
  42. data/vendor/assets/javascripts/bower/codemirror/CONTRIBUTING.md +0 -76
  43. data/vendor/assets/javascripts/bower/codemirror/LICENSE +0 -19
  44. data/vendor/assets/javascripts/bower/codemirror/README.md +0 -11
  45. data/vendor/assets/javascripts/bower/codemirror/addon/comment/comment.js +0 -183
  46. data/vendor/assets/javascripts/bower/codemirror/addon/comment/continuecomment.js +0 -85
  47. data/vendor/assets/javascripts/bower/codemirror/addon/dialog/dialog.css +0 -32
  48. data/vendor/assets/javascripts/bower/codemirror/addon/dialog/dialog.js +0 -155
  49. data/vendor/assets/javascripts/bower/codemirror/addon/display/fullscreen.css +0 -6
  50. data/vendor/assets/javascripts/bower/codemirror/addon/display/fullscreen.js +0 -41
  51. data/vendor/assets/javascripts/bower/codemirror/addon/display/panel.js +0 -94
  52. data/vendor/assets/javascripts/bower/codemirror/addon/display/placeholder.js +0 -58
  53. data/vendor/assets/javascripts/bower/codemirror/addon/display/rulers.js +0 -64
  54. data/vendor/assets/javascripts/bower/codemirror/addon/edit/closebrackets.js +0 -159
  55. data/vendor/assets/javascripts/bower/codemirror/addon/edit/closetag.js +0 -159
  56. data/vendor/assets/javascripts/bower/codemirror/addon/edit/continuelist.js +0 -51
  57. data/vendor/assets/javascripts/bower/codemirror/addon/edit/matchbrackets.js +0 -120
  58. data/vendor/assets/javascripts/bower/codemirror/addon/edit/matchtags.js +0 -66
  59. data/vendor/assets/javascripts/bower/codemirror/addon/edit/trailingspace.js +0 -27
  60. data/vendor/assets/javascripts/bower/codemirror/addon/fold/brace-fold.js +0 -105
  61. data/vendor/assets/javascripts/bower/codemirror/addon/fold/comment-fold.js +0 -57
  62. data/vendor/assets/javascripts/bower/codemirror/addon/fold/foldcode.js +0 -145
  63. data/vendor/assets/javascripts/bower/codemirror/addon/fold/foldgutter.css +0 -20
  64. data/vendor/assets/javascripts/bower/codemirror/addon/fold/foldgutter.js +0 -134
  65. data/vendor/assets/javascripts/bower/codemirror/addon/fold/indent-fold.js +0 -44
  66. data/vendor/assets/javascripts/bower/codemirror/addon/fold/markdown-fold.js +0 -49
  67. data/vendor/assets/javascripts/bower/codemirror/addon/fold/xml-fold.js +0 -182
  68. data/vendor/assets/javascripts/bower/codemirror/addon/hint/anyword-hint.js +0 -41
  69. data/vendor/assets/javascripts/bower/codemirror/addon/hint/css-hint.js +0 -56
  70. data/vendor/assets/javascripts/bower/codemirror/addon/hint/html-hint.js +0 -348
  71. data/vendor/assets/javascripts/bower/codemirror/addon/hint/javascript-hint.js +0 -146
  72. data/vendor/assets/javascripts/bower/codemirror/addon/hint/show-hint.css +0 -38
  73. data/vendor/assets/javascripts/bower/codemirror/addon/hint/show-hint.js +0 -389
  74. data/vendor/assets/javascripts/bower/codemirror/addon/hint/sql-hint.js +0 -197
  75. data/vendor/assets/javascripts/bower/codemirror/addon/hint/xml-hint.js +0 -110
  76. data/vendor/assets/javascripts/bower/codemirror/addon/lint/coffeescript-lint.js +0 -41
  77. data/vendor/assets/javascripts/bower/codemirror/addon/lint/css-lint.js +0 -35
  78. data/vendor/assets/javascripts/bower/codemirror/addon/lint/javascript-lint.js +0 -136
  79. data/vendor/assets/javascripts/bower/codemirror/addon/lint/json-lint.js +0 -31
  80. data/vendor/assets/javascripts/bower/codemirror/addon/lint/lint.css +0 -73
  81. data/vendor/assets/javascripts/bower/codemirror/addon/lint/lint.js +0 -204
  82. data/vendor/assets/javascripts/bower/codemirror/addon/lint/yaml-lint.js +0 -28
  83. data/vendor/assets/javascripts/bower/codemirror/addon/merge/merge.css +0 -112
  84. data/vendor/assets/javascripts/bower/codemirror/addon/merge/merge.js +0 -643
  85. data/vendor/assets/javascripts/bower/codemirror/addon/mode/loadmode.js +0 -64
  86. data/vendor/assets/javascripts/bower/codemirror/addon/mode/multiplex.js +0 -118
  87. data/vendor/assets/javascripts/bower/codemirror/addon/mode/multiplex_test.js +0 -33
  88. data/vendor/assets/javascripts/bower/codemirror/addon/mode/overlay.js +0 -85
  89. data/vendor/assets/javascripts/bower/codemirror/addon/mode/simple.js +0 -213
  90. data/vendor/assets/javascripts/bower/codemirror/addon/runmode/colorize.js +0 -40
  91. data/vendor/assets/javascripts/bower/codemirror/addon/runmode/runmode-standalone.js +0 -157
  92. data/vendor/assets/javascripts/bower/codemirror/addon/runmode/runmode.js +0 -72
  93. data/vendor/assets/javascripts/bower/codemirror/addon/runmode/runmode.node.js +0 -120
  94. data/vendor/assets/javascripts/bower/codemirror/addon/scroll/annotatescrollbar.js +0 -76
  95. data/vendor/assets/javascripts/bower/codemirror/addon/scroll/scrollpastend.js +0 -46
  96. data/vendor/assets/javascripts/bower/codemirror/addon/scroll/simplescrollbars.css +0 -66
  97. data/vendor/assets/javascripts/bower/codemirror/addon/scroll/simplescrollbars.js +0 -139
  98. data/vendor/assets/javascripts/bower/codemirror/addon/search/match-highlighter.js +0 -128
  99. data/vendor/assets/javascripts/bower/codemirror/addon/search/matchesonscrollbar.css +0 -8
  100. data/vendor/assets/javascripts/bower/codemirror/addon/search/matchesonscrollbar.js +0 -90
  101. data/vendor/assets/javascripts/bower/codemirror/addon/search/search.js +0 -164
  102. data/vendor/assets/javascripts/bower/codemirror/addon/search/searchcursor.js +0 -189
  103. data/vendor/assets/javascripts/bower/codemirror/addon/selection/active-line.js +0 -71
  104. data/vendor/assets/javascripts/bower/codemirror/addon/selection/mark-selection.js +0 -118
  105. data/vendor/assets/javascripts/bower/codemirror/addon/tern/tern.css +0 -86
  106. data/vendor/assets/javascripts/bower/codemirror/addon/tern/tern.js +0 -670
  107. data/vendor/assets/javascripts/bower/codemirror/addon/tern/worker.js +0 -44
  108. data/vendor/assets/javascripts/bower/codemirror/addon/wrap/hardwrap.js +0 -139
  109. data/vendor/assets/javascripts/bower/codemirror/keymap/emacs.js +0 -411
  110. data/vendor/assets/javascripts/bower/codemirror/keymap/sublime.js +0 -540
  111. data/vendor/assets/javascripts/bower/codemirror/keymap/vim.js +0 -4901
  112. data/vendor/assets/javascripts/bower/codemirror/mode/apl/apl.js +0 -175
  113. data/vendor/assets/javascripts/bower/codemirror/mode/asterisk/asterisk.js +0 -198
  114. data/vendor/assets/javascripts/bower/codemirror/mode/clike/clike.js +0 -489
  115. data/vendor/assets/javascripts/bower/codemirror/mode/clike/scala.html +0 -767
  116. data/vendor/assets/javascripts/bower/codemirror/mode/clojure/clojure.js +0 -243
  117. data/vendor/assets/javascripts/bower/codemirror/mode/cobol/cobol.js +0 -255
  118. data/vendor/assets/javascripts/bower/codemirror/mode/coffeescript/coffeescript.js +0 -369
  119. data/vendor/assets/javascripts/bower/codemirror/mode/commonlisp/commonlisp.js +0 -122
  120. data/vendor/assets/javascripts/bower/codemirror/mode/css/css.js +0 -717
  121. data/vendor/assets/javascripts/bower/codemirror/mode/css/less.html +0 -152
  122. data/vendor/assets/javascripts/bower/codemirror/mode/css/less_test.js +0 -51
  123. data/vendor/assets/javascripts/bower/codemirror/mode/css/scss.html +0 -157
  124. data/vendor/assets/javascripts/bower/codemirror/mode/css/scss_test.js +0 -110
  125. data/vendor/assets/javascripts/bower/codemirror/mode/css/test.js +0 -135
  126. data/vendor/assets/javascripts/bower/codemirror/mode/cypher/cypher.js +0 -146
  127. data/vendor/assets/javascripts/bower/codemirror/mode/d/d.js +0 -218
  128. data/vendor/assets/javascripts/bower/codemirror/mode/dart/dart.js +0 -50
  129. data/vendor/assets/javascripts/bower/codemirror/mode/diff/diff.js +0 -47
  130. data/vendor/assets/javascripts/bower/codemirror/mode/django/django.js +0 -67
  131. data/vendor/assets/javascripts/bower/codemirror/mode/dockerfile/dockerfile.js +0 -76
  132. data/vendor/assets/javascripts/bower/codemirror/mode/dtd/dtd.js +0 -142
  133. data/vendor/assets/javascripts/bower/codemirror/mode/dylan/dylan.js +0 -299
  134. data/vendor/assets/javascripts/bower/codemirror/mode/ebnf/ebnf.js +0 -195
  135. data/vendor/assets/javascripts/bower/codemirror/mode/ecl/ecl.js +0 -207
  136. data/vendor/assets/javascripts/bower/codemirror/mode/eiffel/eiffel.js +0 -162
  137. data/vendor/assets/javascripts/bower/codemirror/mode/erlang/erlang.js +0 -622
  138. data/vendor/assets/javascripts/bower/codemirror/mode/fortran/fortran.js +0 -188
  139. data/vendor/assets/javascripts/bower/codemirror/mode/gas/gas.js +0 -345
  140. data/vendor/assets/javascripts/bower/codemirror/mode/gfm/gfm.js +0 -123
  141. data/vendor/assets/javascripts/bower/codemirror/mode/gfm/test.js +0 -213
  142. data/vendor/assets/javascripts/bower/codemirror/mode/gherkin/gherkin.js +0 -178
  143. data/vendor/assets/javascripts/bower/codemirror/mode/go/go.js +0 -184
  144. data/vendor/assets/javascripts/bower/codemirror/mode/groovy/groovy.js +0 -226
  145. data/vendor/assets/javascripts/bower/codemirror/mode/haml/haml.js +0 -159
  146. data/vendor/assets/javascripts/bower/codemirror/mode/haml/test.js +0 -97
  147. data/vendor/assets/javascripts/bower/codemirror/mode/haskell/haskell.js +0 -267
  148. data/vendor/assets/javascripts/bower/codemirror/mode/haxe/haxe.js +0 -518
  149. data/vendor/assets/javascripts/bower/codemirror/mode/htmlembedded/htmlembedded.js +0 -86
  150. data/vendor/assets/javascripts/bower/codemirror/mode/htmlmixed/htmlmixed.js +0 -121
  151. data/vendor/assets/javascripts/bower/codemirror/mode/http/http.js +0 -113
  152. data/vendor/assets/javascripts/bower/codemirror/mode/idl/idl.js +0 -290
  153. data/vendor/assets/javascripts/bower/codemirror/mode/jade/jade.js +0 -590
  154. data/vendor/assets/javascripts/bower/codemirror/mode/javascript/javascript.js +0 -686
  155. data/vendor/assets/javascripts/bower/codemirror/mode/javascript/json-ld.html +0 -72
  156. data/vendor/assets/javascripts/bower/codemirror/mode/javascript/test.js +0 -200
  157. data/vendor/assets/javascripts/bower/codemirror/mode/javascript/typescript.html +0 -61
  158. data/vendor/assets/javascripts/bower/codemirror/mode/jinja2/jinja2.js +0 -142
  159. data/vendor/assets/javascripts/bower/codemirror/mode/julia/julia.js +0 -301
  160. data/vendor/assets/javascripts/bower/codemirror/mode/kotlin/kotlin.js +0 -280
  161. data/vendor/assets/javascripts/bower/codemirror/mode/livescript/livescript.js +0 -280
  162. data/vendor/assets/javascripts/bower/codemirror/mode/lua/lua.js +0 -159
  163. data/vendor/assets/javascripts/bower/codemirror/mode/markdown/markdown.js +0 -765
  164. data/vendor/assets/javascripts/bower/codemirror/mode/markdown/test.js +0 -754
  165. data/vendor/assets/javascripts/bower/codemirror/mode/meta.js +0 -165
  166. data/vendor/assets/javascripts/bower/codemirror/mode/mirc/mirc.js +0 -193
  167. data/vendor/assets/javascripts/bower/codemirror/mode/mllike/mllike.js +0 -205
  168. data/vendor/assets/javascripts/bower/codemirror/mode/modelica/modelica.js +0 -245
  169. data/vendor/assets/javascripts/bower/codemirror/mode/nginx/nginx.js +0 -178
  170. data/vendor/assets/javascripts/bower/codemirror/mode/ntriples/ntriples.js +0 -186
  171. data/vendor/assets/javascripts/bower/codemirror/mode/octave/octave.js +0 -135
  172. data/vendor/assets/javascripts/bower/codemirror/mode/pascal/pascal.js +0 -109
  173. data/vendor/assets/javascripts/bower/codemirror/mode/pegjs/pegjs.js +0 -114
  174. data/vendor/assets/javascripts/bower/codemirror/mode/perl/perl.js +0 -832
  175. data/vendor/assets/javascripts/bower/codemirror/mode/php/php.js +0 -226
  176. data/vendor/assets/javascripts/bower/codemirror/mode/php/test.js +0 -154
  177. data/vendor/assets/javascripts/bower/codemirror/mode/pig/pig.js +0 -188
  178. data/vendor/assets/javascripts/bower/codemirror/mode/properties/properties.js +0 -78
  179. data/vendor/assets/javascripts/bower/codemirror/mode/puppet/puppet.js +0 -220
  180. data/vendor/assets/javascripts/bower/codemirror/mode/python/python.js +0 -359
  181. data/vendor/assets/javascripts/bower/codemirror/mode/q/q.js +0 -139
  182. data/vendor/assets/javascripts/bower/codemirror/mode/r/r.js +0 -162
  183. data/vendor/assets/javascripts/bower/codemirror/mode/rpm/rpm.js +0 -101
  184. data/vendor/assets/javascripts/bower/codemirror/mode/rst/rst.js +0 -557
  185. data/vendor/assets/javascripts/bower/codemirror/mode/ruby/ruby.js +0 -285
  186. data/vendor/assets/javascripts/bower/codemirror/mode/ruby/test.js +0 -14
  187. data/vendor/assets/javascripts/bower/codemirror/mode/rust/rust.js +0 -451
  188. data/vendor/assets/javascripts/bower/codemirror/mode/sass/sass.js +0 -327
  189. data/vendor/assets/javascripts/bower/codemirror/mode/scheme/scheme.js +0 -248
  190. data/vendor/assets/javascripts/bower/codemirror/mode/shell/shell.js +0 -139
  191. data/vendor/assets/javascripts/bower/codemirror/mode/shell/test.js +0 -58
  192. data/vendor/assets/javascripts/bower/codemirror/mode/sieve/sieve.js +0 -193
  193. data/vendor/assets/javascripts/bower/codemirror/mode/slim/slim.js +0 -575
  194. data/vendor/assets/javascripts/bower/codemirror/mode/slim/test.js +0 -96
  195. data/vendor/assets/javascripts/bower/codemirror/mode/smalltalk/smalltalk.js +0 -168
  196. data/vendor/assets/javascripts/bower/codemirror/mode/smarty/smarty.js +0 -221
  197. data/vendor/assets/javascripts/bower/codemirror/mode/smartymixed/smartymixed.js +0 -197
  198. data/vendor/assets/javascripts/bower/codemirror/mode/solr/solr.js +0 -104
  199. data/vendor/assets/javascripts/bower/codemirror/mode/soy/soy.js +0 -198
  200. data/vendor/assets/javascripts/bower/codemirror/mode/sparql/sparql.js +0 -174
  201. data/vendor/assets/javascripts/bower/codemirror/mode/spreadsheet/spreadsheet.js +0 -109
  202. data/vendor/assets/javascripts/bower/codemirror/mode/sql/sql.js +0 -391
  203. data/vendor/assets/javascripts/bower/codemirror/mode/stex/stex.js +0 -251
  204. data/vendor/assets/javascripts/bower/codemirror/mode/stex/test.js +0 -123
  205. data/vendor/assets/javascripts/bower/codemirror/mode/tcl/tcl.js +0 -147
  206. data/vendor/assets/javascripts/bower/codemirror/mode/textile/test.js +0 -417
  207. data/vendor/assets/javascripts/bower/codemirror/mode/textile/textile.js +0 -469
  208. data/vendor/assets/javascripts/bower/codemirror/mode/tiddlywiki/tiddlywiki.css +0 -14
  209. data/vendor/assets/javascripts/bower/codemirror/mode/tiddlywiki/tiddlywiki.js +0 -369
  210. data/vendor/assets/javascripts/bower/codemirror/mode/tiki/tiki.css +0 -26
  211. data/vendor/assets/javascripts/bower/codemirror/mode/tiki/tiki.js +0 -323
  212. data/vendor/assets/javascripts/bower/codemirror/mode/toml/toml.js +0 -88
  213. data/vendor/assets/javascripts/bower/codemirror/mode/tornado/tornado.js +0 -68
  214. data/vendor/assets/javascripts/bower/codemirror/mode/turtle/turtle.js +0 -162
  215. data/vendor/assets/javascripts/bower/codemirror/mode/vb/vb.js +0 -274
  216. data/vendor/assets/javascripts/bower/codemirror/mode/vbscript/vbscript.js +0 -350
  217. data/vendor/assets/javascripts/bower/codemirror/mode/velocity/velocity.js +0 -201
  218. data/vendor/assets/javascripts/bower/codemirror/mode/verilog/test.js +0 -273
  219. data/vendor/assets/javascripts/bower/codemirror/mode/verilog/verilog.js +0 -364
  220. data/vendor/assets/javascripts/bower/codemirror/mode/xml/test.js +0 -51
  221. data/vendor/assets/javascripts/bower/codemirror/mode/xml/xml.js +0 -384
  222. data/vendor/assets/javascripts/bower/codemirror/mode/xquery/test.js +0 -67
  223. data/vendor/assets/javascripts/bower/codemirror/mode/xquery/xquery.js +0 -447
  224. data/vendor/assets/javascripts/bower/codemirror/mode/yaml/yaml.js +0 -117
  225. data/vendor/assets/javascripts/bower/codemirror/mode/z80/z80.js +0 -100
  226. data/vendor/assets/javascripts/bower/vue/LICENSE +0 -21
  227. data/vendor/assets/javascripts/bower/vue/src/api/child.js +0 -53
  228. data/vendor/assets/javascripts/bower/vue/src/api/data.js +0 -161
  229. data/vendor/assets/javascripts/bower/vue/src/api/dom.js +0 -211
  230. data/vendor/assets/javascripts/bower/vue/src/api/events.js +0 -176
  231. data/vendor/assets/javascripts/bower/vue/src/api/global.js +0 -146
  232. data/vendor/assets/javascripts/bower/vue/src/api/lifecycle.js +0 -72
  233. data/vendor/assets/javascripts/bower/vue/src/batcher.js +0 -65
  234. data/vendor/assets/javascripts/bower/vue/src/cache.js +0 -112
  235. data/vendor/assets/javascripts/bower/vue/src/compiler/compile.js +0 -549
  236. data/vendor/assets/javascripts/bower/vue/src/compiler/transclude.js +0 -146
  237. data/vendor/assets/javascripts/bower/vue/src/config.js +0 -79
  238. data/vendor/assets/javascripts/bower/vue/src/directive.js +0 -219
  239. data/vendor/assets/javascripts/bower/vue/src/directives/attr.js +0 -32
  240. data/vendor/assets/javascripts/bower/vue/src/directives/class.js +0 -18
  241. data/vendor/assets/javascripts/bower/vue/src/directives/cloak.js +0 -12
  242. data/vendor/assets/javascripts/bower/vue/src/directives/component.js +0 -196
  243. data/vendor/assets/javascripts/bower/vue/src/directives/el.js +0 -13
  244. data/vendor/assets/javascripts/bower/vue/src/directives/html.js +0 -38
  245. data/vendor/assets/javascripts/bower/vue/src/directives/if.js +0 -87
  246. data/vendor/assets/javascripts/bower/vue/src/directives/index.js +0 -22
  247. data/vendor/assets/javascripts/bower/vue/src/directives/model/checkbox.js +0 -25
  248. data/vendor/assets/javascripts/bower/vue/src/directives/model/default.js +0 -123
  249. data/vendor/assets/javascripts/bower/vue/src/directives/model/index.js +0 -56
  250. data/vendor/assets/javascripts/bower/vue/src/directives/model/radio.js +0 -26
  251. data/vendor/assets/javascripts/bower/vue/src/directives/model/select.js +0 -166
  252. data/vendor/assets/javascripts/bower/vue/src/directives/on.js +0 -59
  253. data/vendor/assets/javascripts/bower/vue/src/directives/partial.js +0 -44
  254. data/vendor/assets/javascripts/bower/vue/src/directives/ref.js +0 -24
  255. data/vendor/assets/javascripts/bower/vue/src/directives/repeat.js +0 -503
  256. data/vendor/assets/javascripts/bower/vue/src/directives/show.js +0 -8
  257. data/vendor/assets/javascripts/bower/vue/src/directives/style.js +0 -58
  258. data/vendor/assets/javascripts/bower/vue/src/directives/text.js +0 -15
  259. data/vendor/assets/javascripts/bower/vue/src/directives/transition.js +0 -12
  260. data/vendor/assets/javascripts/bower/vue/src/directives/with.js +0 -47
  261. data/vendor/assets/javascripts/bower/vue/src/filters/array-filters.js +0 -87
  262. data/vendor/assets/javascripts/bower/vue/src/filters/index.js +0 -135
  263. data/vendor/assets/javascripts/bower/vue/src/instance/compile.js +0 -179
  264. data/vendor/assets/javascripts/bower/vue/src/instance/events.js +0 -122
  265. data/vendor/assets/javascripts/bower/vue/src/instance/init.js +0 -76
  266. data/vendor/assets/javascripts/bower/vue/src/instance/scope.js +0 -217
  267. data/vendor/assets/javascripts/bower/vue/src/observer/array.js +0 -90
  268. data/vendor/assets/javascripts/bower/vue/src/observer/dep.js +0 -50
  269. data/vendor/assets/javascripts/bower/vue/src/observer/index.js +0 -235
  270. data/vendor/assets/javascripts/bower/vue/src/observer/object.js +0 -66
  271. data/vendor/assets/javascripts/bower/vue/src/parsers/directive.js +0 -159
  272. data/vendor/assets/javascripts/bower/vue/src/parsers/expression.js +0 -226
  273. data/vendor/assets/javascripts/bower/vue/src/parsers/path.js +0 -300
  274. data/vendor/assets/javascripts/bower/vue/src/parsers/template.js +0 -248
  275. data/vendor/assets/javascripts/bower/vue/src/parsers/text.js +0 -178
  276. data/vendor/assets/javascripts/bower/vue/src/transition/css.js +0 -189
  277. data/vendor/assets/javascripts/bower/vue/src/transition/index.js +0 -151
  278. data/vendor/assets/javascripts/bower/vue/src/transition/js.js +0 -43
  279. data/vendor/assets/javascripts/bower/vue/src/util/debug.js +0 -50
  280. data/vendor/assets/javascripts/bower/vue/src/util/dom.js +0 -197
  281. data/vendor/assets/javascripts/bower/vue/src/util/env.js +0 -74
  282. data/vendor/assets/javascripts/bower/vue/src/util/filter.js +0 -72
  283. data/vendor/assets/javascripts/bower/vue/src/util/index.js +0 -8
  284. data/vendor/assets/javascripts/bower/vue/src/util/lang.js +0 -175
  285. data/vendor/assets/javascripts/bower/vue/src/util/merge-option.js +0 -255
  286. data/vendor/assets/javascripts/bower/vue/src/vue.js +0 -84
  287. data/vendor/assets/javascripts/bower/vue/src/watcher.js +0 -240
@@ -1,4901 +0,0 @@
1
- // CodeMirror, copyright (c) by Marijn Haverbeke and others
2
- // Distributed under an MIT license: http://codemirror.net/LICENSE
3
-
4
- /**
5
- * Supported keybindings:
6
- *
7
- * Motion:
8
- * h, j, k, l
9
- * gj, gk
10
- * e, E, w, W, b, B, ge, gE
11
- * f<character>, F<character>, t<character>, T<character>
12
- * $, ^, 0, -, +, _
13
- * gg, G
14
- * %
15
- * '<character>, `<character>
16
- *
17
- * Operator:
18
- * d, y, c
19
- * dd, yy, cc
20
- * g~, g~g~
21
- * >, <, >>, <<
22
- *
23
- * Operator-Motion:
24
- * x, X, D, Y, C, ~
25
- *
26
- * Action:
27
- * a, i, s, A, I, S, o, O
28
- * zz, z., z<CR>, zt, zb, z-
29
- * J
30
- * u, Ctrl-r
31
- * m<character>
32
- * r<character>
33
- *
34
- * Modes:
35
- * ESC - leave insert mode, visual mode, and clear input state.
36
- * Ctrl-[, Ctrl-c - same as ESC.
37
- *
38
- * Registers: unnamed, -, a-z, A-Z, 0-9
39
- * (Does not respect the special case for number registers when delete
40
- * operator is made with these commands: %, (, ), , /, ?, n, N, {, } )
41
- * TODO: Implement the remaining registers.
42
- * Marks: a-z, A-Z, and 0-9
43
- * TODO: Implement the remaining special marks. They have more complex
44
- * behavior.
45
- *
46
- * Events:
47
- * 'vim-mode-change' - raised on the editor anytime the current mode changes,
48
- * Event object: {mode: "visual", subMode: "linewise"}
49
- *
50
- * Code structure:
51
- * 1. Default keymap
52
- * 2. Variable declarations and short basic helpers
53
- * 3. Instance (External API) implementation
54
- * 4. Internal state tracking objects (input state, counter) implementation
55
- * and instanstiation
56
- * 5. Key handler (the main command dispatcher) implementation
57
- * 6. Motion, operator, and action implementations
58
- * 7. Helper functions for the key handler, motions, operators, and actions
59
- * 8. Set up Vim to work as a keymap for CodeMirror.
60
- */
61
-
62
- (function(mod) {
63
- if (typeof exports == "object" && typeof module == "object") // CommonJS
64
- mod(require("../lib/codemirror"), require("../addon/search/searchcursor"), require("../addon/dialog/dialog"), require("../addon/edit/matchbrackets.js"));
65
- else if (typeof define == "function" && define.amd) // AMD
66
- define(["../lib/codemirror", "../addon/search/searchcursor", "../addon/dialog/dialog", "../addon/edit/matchbrackets"], mod);
67
- else // Plain browser env
68
- mod(CodeMirror);
69
- })(function(CodeMirror) {
70
- 'use strict';
71
-
72
- var defaultKeymap = [
73
- // Key to key mapping. This goes first to make it possible to override
74
- // existing mappings.
75
- { keys: '<Left>', type: 'keyToKey', toKeys: 'h' },
76
- { keys: '<Right>', type: 'keyToKey', toKeys: 'l' },
77
- { keys: '<Up>', type: 'keyToKey', toKeys: 'k' },
78
- { keys: '<Down>', type: 'keyToKey', toKeys: 'j' },
79
- { keys: '<Space>', type: 'keyToKey', toKeys: 'l' },
80
- { keys: '<BS>', type: 'keyToKey', toKeys: 'h', context: 'normal'},
81
- { keys: '<C-Space>', type: 'keyToKey', toKeys: 'W' },
82
- { keys: '<C-BS>', type: 'keyToKey', toKeys: 'B', context: 'normal' },
83
- { keys: '<S-Space>', type: 'keyToKey', toKeys: 'w' },
84
- { keys: '<S-BS>', type: 'keyToKey', toKeys: 'b', context: 'normal' },
85
- { keys: '<C-n>', type: 'keyToKey', toKeys: 'j' },
86
- { keys: '<C-p>', type: 'keyToKey', toKeys: 'k' },
87
- { keys: '<C-[>', type: 'keyToKey', toKeys: '<Esc>' },
88
- { keys: '<C-c>', type: 'keyToKey', toKeys: '<Esc>' },
89
- { keys: '<C-[>', type: 'keyToKey', toKeys: '<Esc>', context: 'insert' },
90
- { keys: '<C-c>', type: 'keyToKey', toKeys: '<Esc>', context: 'insert' },
91
- { keys: 's', type: 'keyToKey', toKeys: 'cl', context: 'normal' },
92
- { keys: 's', type: 'keyToKey', toKeys: 'xi', context: 'visual'},
93
- { keys: 'S', type: 'keyToKey', toKeys: 'cc', context: 'normal' },
94
- { keys: 'S', type: 'keyToKey', toKeys: 'dcc', context: 'visual' },
95
- { keys: '<Home>', type: 'keyToKey', toKeys: '0' },
96
- { keys: '<End>', type: 'keyToKey', toKeys: '$' },
97
- { keys: '<PageUp>', type: 'keyToKey', toKeys: '<C-b>' },
98
- { keys: '<PageDown>', type: 'keyToKey', toKeys: '<C-f>' },
99
- { keys: '<CR>', type: 'keyToKey', toKeys: 'j^', context: 'normal' },
100
- // Motions
101
- { keys: 'H', type: 'motion', motion: 'moveToTopLine', motionArgs: { linewise: true, toJumplist: true }},
102
- { keys: 'M', type: 'motion', motion: 'moveToMiddleLine', motionArgs: { linewise: true, toJumplist: true }},
103
- { keys: 'L', type: 'motion', motion: 'moveToBottomLine', motionArgs: { linewise: true, toJumplist: true }},
104
- { keys: 'h', type: 'motion', motion: 'moveByCharacters', motionArgs: { forward: false }},
105
- { keys: 'l', type: 'motion', motion: 'moveByCharacters', motionArgs: { forward: true }},
106
- { keys: 'j', type: 'motion', motion: 'moveByLines', motionArgs: { forward: true, linewise: true }},
107
- { keys: 'k', type: 'motion', motion: 'moveByLines', motionArgs: { forward: false, linewise: true }},
108
- { keys: 'gj', type: 'motion', motion: 'moveByDisplayLines', motionArgs: { forward: true }},
109
- { keys: 'gk', type: 'motion', motion: 'moveByDisplayLines', motionArgs: { forward: false }},
110
- { keys: 'w', type: 'motion', motion: 'moveByWords', motionArgs: { forward: true, wordEnd: false }},
111
- { keys: 'W', type: 'motion', motion: 'moveByWords', motionArgs: { forward: true, wordEnd: false, bigWord: true }},
112
- { keys: 'e', type: 'motion', motion: 'moveByWords', motionArgs: { forward: true, wordEnd: true, inclusive: true }},
113
- { keys: 'E', type: 'motion', motion: 'moveByWords', motionArgs: { forward: true, wordEnd: true, bigWord: true, inclusive: true }},
114
- { keys: 'b', type: 'motion', motion: 'moveByWords', motionArgs: { forward: false, wordEnd: false }},
115
- { keys: 'B', type: 'motion', motion: 'moveByWords', motionArgs: { forward: false, wordEnd: false, bigWord: true }},
116
- { keys: 'ge', type: 'motion', motion: 'moveByWords', motionArgs: { forward: false, wordEnd: true, inclusive: true }},
117
- { keys: 'gE', type: 'motion', motion: 'moveByWords', motionArgs: { forward: false, wordEnd: true, bigWord: true, inclusive: true }},
118
- { keys: '{', type: 'motion', motion: 'moveByParagraph', motionArgs: { forward: false, toJumplist: true }},
119
- { keys: '}', type: 'motion', motion: 'moveByParagraph', motionArgs: { forward: true, toJumplist: true }},
120
- { keys: '<C-f>', type: 'motion', motion: 'moveByPage', motionArgs: { forward: true }},
121
- { keys: '<C-b>', type: 'motion', motion: 'moveByPage', motionArgs: { forward: false }},
122
- { keys: '<C-d>', type: 'motion', motion: 'moveByScroll', motionArgs: { forward: true, explicitRepeat: true }},
123
- { keys: '<C-u>', type: 'motion', motion: 'moveByScroll', motionArgs: { forward: false, explicitRepeat: true }},
124
- { keys: 'gg', type: 'motion', motion: 'moveToLineOrEdgeOfDocument', motionArgs: { forward: false, explicitRepeat: true, linewise: true, toJumplist: true }},
125
- { keys: 'G', type: 'motion', motion: 'moveToLineOrEdgeOfDocument', motionArgs: { forward: true, explicitRepeat: true, linewise: true, toJumplist: true }},
126
- { keys: '0', type: 'motion', motion: 'moveToStartOfLine' },
127
- { keys: '^', type: 'motion', motion: 'moveToFirstNonWhiteSpaceCharacter' },
128
- { keys: '+', type: 'motion', motion: 'moveByLines', motionArgs: { forward: true, toFirstChar:true }},
129
- { keys: '-', type: 'motion', motion: 'moveByLines', motionArgs: { forward: false, toFirstChar:true }},
130
- { keys: '_', type: 'motion', motion: 'moveByLines', motionArgs: { forward: true, toFirstChar:true, repeatOffset:-1 }},
131
- { keys: '$', type: 'motion', motion: 'moveToEol', motionArgs: { inclusive: true }},
132
- { keys: '%', type: 'motion', motion: 'moveToMatchedSymbol', motionArgs: { inclusive: true, toJumplist: true }},
133
- { keys: 'f<character>', type: 'motion', motion: 'moveToCharacter', motionArgs: { forward: true , inclusive: true }},
134
- { keys: 'F<character>', type: 'motion', motion: 'moveToCharacter', motionArgs: { forward: false }},
135
- { keys: 't<character>', type: 'motion', motion: 'moveTillCharacter', motionArgs: { forward: true, inclusive: true }},
136
- { keys: 'T<character>', type: 'motion', motion: 'moveTillCharacter', motionArgs: { forward: false }},
137
- { keys: ';', type: 'motion', motion: 'repeatLastCharacterSearch', motionArgs: { forward: true }},
138
- { keys: ',', type: 'motion', motion: 'repeatLastCharacterSearch', motionArgs: { forward: false }},
139
- { keys: '\'<character>', type: 'motion', motion: 'goToMark', motionArgs: {toJumplist: true, linewise: true}},
140
- { keys: '`<character>', type: 'motion', motion: 'goToMark', motionArgs: {toJumplist: true}},
141
- { keys: ']`', type: 'motion', motion: 'jumpToMark', motionArgs: { forward: true } },
142
- { keys: '[`', type: 'motion', motion: 'jumpToMark', motionArgs: { forward: false } },
143
- { keys: ']\'', type: 'motion', motion: 'jumpToMark', motionArgs: { forward: true, linewise: true } },
144
- { keys: '[\'', type: 'motion', motion: 'jumpToMark', motionArgs: { forward: false, linewise: true } },
145
- // the next two aren't motions but must come before more general motion declarations
146
- { keys: ']p', type: 'action', action: 'paste', isEdit: true, actionArgs: { after: true, isEdit: true, matchIndent: true}},
147
- { keys: '[p', type: 'action', action: 'paste', isEdit: true, actionArgs: { after: false, isEdit: true, matchIndent: true}},
148
- { keys: ']<character>', type: 'motion', motion: 'moveToSymbol', motionArgs: { forward: true, toJumplist: true}},
149
- { keys: '[<character>', type: 'motion', motion: 'moveToSymbol', motionArgs: { forward: false, toJumplist: true}},
150
- { keys: '|', type: 'motion', motion: 'moveToColumn'},
151
- { keys: 'o', type: 'motion', motion: 'moveToOtherHighlightedEnd', context:'visual'},
152
- { keys: 'O', type: 'motion', motion: 'moveToOtherHighlightedEnd', motionArgs: {sameLine: true}, context:'visual'},
153
- // Operators
154
- { keys: 'd', type: 'operator', operator: 'delete' },
155
- { keys: 'y', type: 'operator', operator: 'yank' },
156
- { keys: 'c', type: 'operator', operator: 'change' },
157
- { keys: '>', type: 'operator', operator: 'indent', operatorArgs: { indentRight: true }},
158
- { keys: '<', type: 'operator', operator: 'indent', operatorArgs: { indentRight: false }},
159
- { keys: 'g~', type: 'operator', operator: 'changeCase' },
160
- { keys: 'gu', type: 'operator', operator: 'changeCase', operatorArgs: {toLower: true}, isEdit: true },
161
- { keys: 'gU', type: 'operator', operator: 'changeCase', operatorArgs: {toLower: false}, isEdit: true },
162
- { keys: 'n', type: 'motion', motion: 'findNext', motionArgs: { forward: true, toJumplist: true }},
163
- { keys: 'N', type: 'motion', motion: 'findNext', motionArgs: { forward: false, toJumplist: true }},
164
- // Operator-Motion dual commands
165
- { keys: 'x', type: 'operatorMotion', operator: 'delete', motion: 'moveByCharacters', motionArgs: { forward: true }, operatorMotionArgs: { visualLine: false }},
166
- { keys: 'X', type: 'operatorMotion', operator: 'delete', motion: 'moveByCharacters', motionArgs: { forward: false }, operatorMotionArgs: { visualLine: true }},
167
- { keys: 'D', type: 'operatorMotion', operator: 'delete', motion: 'moveToEol', motionArgs: { inclusive: true }, context: 'normal'},
168
- { keys: 'D', type: 'operator', operator: 'delete', operatorArgs: { linewise: true }, context: 'visual'},
169
- { keys: 'Y', type: 'operatorMotion', operator: 'yank', motion: 'moveToEol', motionArgs: { inclusive: true }, context: 'normal'},
170
- { keys: 'Y', type: 'operator', operator: 'yank', operatorArgs: { linewise: true }, context: 'visual'},
171
- { keys: 'C', type: 'operatorMotion', operator: 'change', motion: 'moveToEol', motionArgs: { inclusive: true }, context: 'normal'},
172
- { keys: 'C', type: 'operator', operator: 'change', operatorArgs: { linewise: true }, context: 'visual'},
173
- { keys: '~', type: 'operatorMotion', operator: 'changeCase', motion: 'moveByCharacters', motionArgs: { forward: true }, operatorArgs: { shouldMoveCursor: true }, context: 'normal'},
174
- { keys: '~', type: 'operator', operator: 'changeCase', context: 'visual'},
175
- { keys: '<C-w>', type: 'operatorMotion', operator: 'delete', motion: 'moveByWords', motionArgs: { forward: false, wordEnd: false }, context: 'insert' },
176
- // Actions
177
- { keys: '<C-i>', type: 'action', action: 'jumpListWalk', actionArgs: { forward: true }},
178
- { keys: '<C-o>', type: 'action', action: 'jumpListWalk', actionArgs: { forward: false }},
179
- { keys: '<C-e>', type: 'action', action: 'scroll', actionArgs: { forward: true, linewise: true }},
180
- { keys: '<C-y>', type: 'action', action: 'scroll', actionArgs: { forward: false, linewise: true }},
181
- { keys: 'a', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'charAfter' }, context: 'normal' },
182
- { keys: 'A', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'eol' }, context: 'normal' },
183
- { keys: 'A', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'endOfSelectedArea' }, context: 'visual' },
184
- { keys: 'i', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'inplace' }, context: 'normal' },
185
- { keys: 'I', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'firstNonBlank'}, context: 'normal' },
186
- { keys: 'I', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'startOfSelectedArea' }, context: 'visual' },
187
- { keys: 'o', type: 'action', action: 'newLineAndEnterInsertMode', isEdit: true, interlaceInsertRepeat: true, actionArgs: { after: true }, context: 'normal' },
188
- { keys: 'O', type: 'action', action: 'newLineAndEnterInsertMode', isEdit: true, interlaceInsertRepeat: true, actionArgs: { after: false }, context: 'normal' },
189
- { keys: 'v', type: 'action', action: 'toggleVisualMode' },
190
- { keys: 'V', type: 'action', action: 'toggleVisualMode', actionArgs: { linewise: true }},
191
- { keys: '<C-v>', type: 'action', action: 'toggleVisualMode', actionArgs: { blockwise: true }},
192
- { keys: 'gv', type: 'action', action: 'reselectLastSelection' },
193
- { keys: 'J', type: 'action', action: 'joinLines', isEdit: true },
194
- { keys: 'p', type: 'action', action: 'paste', isEdit: true, actionArgs: { after: true, isEdit: true }},
195
- { keys: 'P', type: 'action', action: 'paste', isEdit: true, actionArgs: { after: false, isEdit: true }},
196
- { keys: 'r<character>', type: 'action', action: 'replace', isEdit: true },
197
- { keys: '@<character>', type: 'action', action: 'replayMacro' },
198
- { keys: 'q<character>', type: 'action', action: 'enterMacroRecordMode' },
199
- // Handle Replace-mode as a special case of insert mode.
200
- { keys: 'R', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { replace: true }},
201
- { keys: 'u', type: 'action', action: 'undo', context: 'normal' },
202
- { keys: 'u', type: 'operator', operator: 'changeCase', operatorArgs: {toLower: true}, context: 'visual', isEdit: true },
203
- { keys: 'U', type: 'operator', operator: 'changeCase', operatorArgs: {toLower: false}, context: 'visual', isEdit: true },
204
- { keys: '<C-r>', type: 'action', action: 'redo' },
205
- { keys: 'm<character>', type: 'action', action: 'setMark' },
206
- { keys: '"<character>', type: 'action', action: 'setRegister' },
207
- { keys: 'zz', type: 'action', action: 'scrollToCursor', actionArgs: { position: 'center' }},
208
- { keys: 'z.', type: 'action', action: 'scrollToCursor', actionArgs: { position: 'center' }, motion: 'moveToFirstNonWhiteSpaceCharacter' },
209
- { keys: 'zt', type: 'action', action: 'scrollToCursor', actionArgs: { position: 'top' }},
210
- { keys: 'z<CR>', type: 'action', action: 'scrollToCursor', actionArgs: { position: 'top' }, motion: 'moveToFirstNonWhiteSpaceCharacter' },
211
- { keys: 'z-', type: 'action', action: 'scrollToCursor', actionArgs: { position: 'bottom' }},
212
- { keys: 'zb', type: 'action', action: 'scrollToCursor', actionArgs: { position: 'bottom' }, motion: 'moveToFirstNonWhiteSpaceCharacter' },
213
- { keys: '.', type: 'action', action: 'repeatLastEdit' },
214
- { keys: '<C-a>', type: 'action', action: 'incrementNumberToken', isEdit: true, actionArgs: {increase: true, backtrack: false}},
215
- { keys: '<C-x>', type: 'action', action: 'incrementNumberToken', isEdit: true, actionArgs: {increase: false, backtrack: false}},
216
- // Text object motions
217
- { keys: 'a<character>', type: 'motion', motion: 'textObjectManipulation' },
218
- { keys: 'i<character>', type: 'motion', motion: 'textObjectManipulation', motionArgs: { textObjectInner: true }},
219
- // Search
220
- { keys: '/', type: 'search', searchArgs: { forward: true, querySrc: 'prompt', toJumplist: true }},
221
- { keys: '?', type: 'search', searchArgs: { forward: false, querySrc: 'prompt', toJumplist: true }},
222
- { keys: '*', type: 'search', searchArgs: { forward: true, querySrc: 'wordUnderCursor', wholeWordOnly: true, toJumplist: true }},
223
- { keys: '#', type: 'search', searchArgs: { forward: false, querySrc: 'wordUnderCursor', wholeWordOnly: true, toJumplist: true }},
224
- { keys: 'g*', type: 'search', searchArgs: { forward: true, querySrc: 'wordUnderCursor', toJumplist: true }},
225
- { keys: 'g#', type: 'search', searchArgs: { forward: false, querySrc: 'wordUnderCursor', toJumplist: true }},
226
- // Ex command
227
- { keys: ':', type: 'ex' }
228
- ];
229
-
230
- var Pos = CodeMirror.Pos;
231
-
232
- var Vim = function() {
233
- function enterVimMode(cm) {
234
- cm.setOption('disableInput', true);
235
- cm.setOption('showCursorWhenSelecting', false);
236
- CodeMirror.signal(cm, "vim-mode-change", {mode: "normal"});
237
- cm.on('cursorActivity', onCursorActivity);
238
- maybeInitVimState(cm);
239
- CodeMirror.on(cm.getInputField(), 'paste', getOnPasteFn(cm));
240
- }
241
-
242
- function leaveVimMode(cm) {
243
- cm.setOption('disableInput', false);
244
- cm.off('cursorActivity', onCursorActivity);
245
- CodeMirror.off(cm.getInputField(), 'paste', getOnPasteFn(cm));
246
- cm.state.vim = null;
247
- }
248
-
249
- function detachVimMap(cm, next) {
250
- if (this == CodeMirror.keyMap.vim)
251
- CodeMirror.rmClass(cm.getWrapperElement(), "cm-fat-cursor");
252
-
253
- if (!next || next.attach != attachVimMap)
254
- leaveVimMode(cm, false);
255
- }
256
- function attachVimMap(cm, prev) {
257
- if (this == CodeMirror.keyMap.vim)
258
- CodeMirror.addClass(cm.getWrapperElement(), "cm-fat-cursor");
259
-
260
- if (!prev || prev.attach != attachVimMap)
261
- enterVimMode(cm);
262
- }
263
-
264
- // Deprecated, simply setting the keymap works again.
265
- CodeMirror.defineOption('vimMode', false, function(cm, val, prev) {
266
- if (val && cm.getOption("keyMap") != "vim")
267
- cm.setOption("keyMap", "vim");
268
- else if (!val && prev != CodeMirror.Init && /^vim/.test(cm.getOption("keyMap")))
269
- cm.setOption("keyMap", "default");
270
- });
271
-
272
- function cmKey(key, cm) {
273
- if (!cm) { return undefined; }
274
- var vimKey = cmKeyToVimKey(key);
275
- if (!vimKey) {
276
- return false;
277
- }
278
- var cmd = CodeMirror.Vim.findKey(cm, vimKey);
279
- if (typeof cmd == 'function') {
280
- CodeMirror.signal(cm, 'vim-keypress', vimKey);
281
- }
282
- return cmd;
283
- }
284
-
285
- var modifiers = {'Shift': 'S', 'Ctrl': 'C', 'Alt': 'A', 'Cmd': 'D', 'Mod': 'A'};
286
- var specialKeys = {Enter:'CR',Backspace:'BS',Delete:'Del'};
287
- function cmKeyToVimKey(key) {
288
- if (key.charAt(0) == '\'') {
289
- // Keypress character binding of format "'a'"
290
- return key.charAt(1);
291
- }
292
- var pieces = key.split('-');
293
- if (/-$/.test(key)) {
294
- // If the - key was typed, split will result in 2 extra empty strings
295
- // in the array. Replace them with 1 '-'.
296
- pieces.splice(-2, 2, '-');
297
- }
298
- var lastPiece = pieces[pieces.length - 1];
299
- if (pieces.length == 1 && pieces[0].length == 1) {
300
- // No-modifier bindings use literal character bindings above. Skip.
301
- return false;
302
- } else if (pieces.length == 2 && pieces[0] == 'Shift' && lastPiece.length == 1) {
303
- // Ignore Shift+char bindings as they should be handled by literal character.
304
- return false;
305
- }
306
- var hasCharacter = false;
307
- for (var i = 0; i < pieces.length; i++) {
308
- var piece = pieces[i];
309
- if (piece in modifiers) { pieces[i] = modifiers[piece]; }
310
- else { hasCharacter = true; }
311
- if (piece in specialKeys) { pieces[i] = specialKeys[piece]; }
312
- }
313
- if (!hasCharacter) {
314
- // Vim does not support modifier only keys.
315
- return false;
316
- }
317
- // TODO: Current bindings expect the character to be lower case, but
318
- // it looks like vim key notation uses upper case.
319
- if (isUpperCase(lastPiece)) {
320
- pieces[pieces.length - 1] = lastPiece.toLowerCase();
321
- }
322
- return '<' + pieces.join('-') + '>';
323
- }
324
-
325
- function getOnPasteFn(cm) {
326
- var vim = cm.state.vim;
327
- if (!vim.onPasteFn) {
328
- vim.onPasteFn = function() {
329
- if (!vim.insertMode) {
330
- cm.setCursor(offsetCursor(cm.getCursor(), 0, 1));
331
- actions.enterInsertMode(cm, {}, vim);
332
- }
333
- };
334
- }
335
- return vim.onPasteFn;
336
- }
337
-
338
- var numberRegex = /[\d]/;
339
- var wordRegexp = [(/\w/), (/[^\w\s]/)], bigWordRegexp = [(/\S/)];
340
- function makeKeyRange(start, size) {
341
- var keys = [];
342
- for (var i = start; i < start + size; i++) {
343
- keys.push(String.fromCharCode(i));
344
- }
345
- return keys;
346
- }
347
- var upperCaseAlphabet = makeKeyRange(65, 26);
348
- var lowerCaseAlphabet = makeKeyRange(97, 26);
349
- var numbers = makeKeyRange(48, 10);
350
- var validMarks = [].concat(upperCaseAlphabet, lowerCaseAlphabet, numbers, ['<', '>']);
351
- var validRegisters = [].concat(upperCaseAlphabet, lowerCaseAlphabet, numbers, ['-', '"', '.', ':', '/']);
352
-
353
- function isLine(cm, line) {
354
- return line >= cm.firstLine() && line <= cm.lastLine();
355
- }
356
- function isLowerCase(k) {
357
- return (/^[a-z]$/).test(k);
358
- }
359
- function isMatchableSymbol(k) {
360
- return '()[]{}'.indexOf(k) != -1;
361
- }
362
- function isNumber(k) {
363
- return numberRegex.test(k);
364
- }
365
- function isUpperCase(k) {
366
- return (/^[A-Z]$/).test(k);
367
- }
368
- function isWhiteSpaceString(k) {
369
- return (/^\s*$/).test(k);
370
- }
371
- function inArray(val, arr) {
372
- for (var i = 0; i < arr.length; i++) {
373
- if (arr[i] == val) {
374
- return true;
375
- }
376
- }
377
- return false;
378
- }
379
-
380
- var options = {};
381
- function defineOption(name, defaultValue, type) {
382
- if (defaultValue === undefined) { throw Error('defaultValue is required'); }
383
- if (!type) { type = 'string'; }
384
- options[name] = {
385
- type: type,
386
- defaultValue: defaultValue
387
- };
388
- setOption(name, defaultValue);
389
- }
390
-
391
- function setOption(name, value) {
392
- var option = options[name];
393
- if (!option) {
394
- throw Error('Unknown option: ' + name);
395
- }
396
- if (option.type == 'boolean') {
397
- if (value && value !== true) {
398
- throw Error('Invalid argument: ' + name + '=' + value);
399
- } else if (value !== false) {
400
- // Boolean options are set to true if value is not defined.
401
- value = true;
402
- }
403
- }
404
- option.value = option.type == 'boolean' ? !!value : value;
405
- }
406
-
407
- function getOption(name) {
408
- var option = options[name];
409
- if (!option) {
410
- throw Error('Unknown option: ' + name);
411
- }
412
- return option.value;
413
- }
414
-
415
- var createCircularJumpList = function() {
416
- var size = 100;
417
- var pointer = -1;
418
- var head = 0;
419
- var tail = 0;
420
- var buffer = new Array(size);
421
- function add(cm, oldCur, newCur) {
422
- var current = pointer % size;
423
- var curMark = buffer[current];
424
- function useNextSlot(cursor) {
425
- var next = ++pointer % size;
426
- var trashMark = buffer[next];
427
- if (trashMark) {
428
- trashMark.clear();
429
- }
430
- buffer[next] = cm.setBookmark(cursor);
431
- }
432
- if (curMark) {
433
- var markPos = curMark.find();
434
- // avoid recording redundant cursor position
435
- if (markPos && !cursorEqual(markPos, oldCur)) {
436
- useNextSlot(oldCur);
437
- }
438
- } else {
439
- useNextSlot(oldCur);
440
- }
441
- useNextSlot(newCur);
442
- head = pointer;
443
- tail = pointer - size + 1;
444
- if (tail < 0) {
445
- tail = 0;
446
- }
447
- }
448
- function move(cm, offset) {
449
- pointer += offset;
450
- if (pointer > head) {
451
- pointer = head;
452
- } else if (pointer < tail) {
453
- pointer = tail;
454
- }
455
- var mark = buffer[(size + pointer) % size];
456
- // skip marks that are temporarily removed from text buffer
457
- if (mark && !mark.find()) {
458
- var inc = offset > 0 ? 1 : -1;
459
- var newCur;
460
- var oldCur = cm.getCursor();
461
- do {
462
- pointer += inc;
463
- mark = buffer[(size + pointer) % size];
464
- // skip marks that are the same as current position
465
- if (mark &&
466
- (newCur = mark.find()) &&
467
- !cursorEqual(oldCur, newCur)) {
468
- break;
469
- }
470
- } while (pointer < head && pointer > tail);
471
- }
472
- return mark;
473
- }
474
- return {
475
- cachedCursor: undefined, //used for # and * jumps
476
- add: add,
477
- move: move
478
- };
479
- };
480
-
481
- // Returns an object to track the changes associated insert mode. It
482
- // clones the object that is passed in, or creates an empty object one if
483
- // none is provided.
484
- var createInsertModeChanges = function(c) {
485
- if (c) {
486
- // Copy construction
487
- return {
488
- changes: c.changes,
489
- expectCursorActivityForChange: c.expectCursorActivityForChange
490
- };
491
- }
492
- return {
493
- // Change list
494
- changes: [],
495
- // Set to true on change, false on cursorActivity.
496
- expectCursorActivityForChange: false
497
- };
498
- };
499
-
500
- function MacroModeState() {
501
- this.latestRegister = undefined;
502
- this.isPlaying = false;
503
- this.isRecording = false;
504
- this.replaySearchQueries = [];
505
- this.onRecordingDone = undefined;
506
- this.lastInsertModeChanges = createInsertModeChanges();
507
- }
508
- MacroModeState.prototype = {
509
- exitMacroRecordMode: function() {
510
- var macroModeState = vimGlobalState.macroModeState;
511
- if (macroModeState.onRecordingDone) {
512
- macroModeState.onRecordingDone(); // close dialog
513
- }
514
- macroModeState.onRecordingDone = undefined;
515
- macroModeState.isRecording = false;
516
- },
517
- enterMacroRecordMode: function(cm, registerName) {
518
- var register =
519
- vimGlobalState.registerController.getRegister(registerName);
520
- if (register) {
521
- register.clear();
522
- this.latestRegister = registerName;
523
- if (cm.openDialog) {
524
- this.onRecordingDone = cm.openDialog(
525
- '(recording)['+registerName+']', null, {bottom:true});
526
- }
527
- this.isRecording = true;
528
- }
529
- }
530
- };
531
-
532
- function maybeInitVimState(cm) {
533
- if (!cm.state.vim) {
534
- // Store instance state in the CodeMirror object.
535
- cm.state.vim = {
536
- inputState: new InputState(),
537
- // Vim's input state that triggered the last edit, used to repeat
538
- // motions and operators with '.'.
539
- lastEditInputState: undefined,
540
- // Vim's action command before the last edit, used to repeat actions
541
- // with '.' and insert mode repeat.
542
- lastEditActionCommand: undefined,
543
- // When using jk for navigation, if you move from a longer line to a
544
- // shorter line, the cursor may clip to the end of the shorter line.
545
- // If j is pressed again and cursor goes to the next line, the
546
- // cursor should go back to its horizontal position on the longer
547
- // line if it can. This is to keep track of the horizontal position.
548
- lastHPos: -1,
549
- // Doing the same with screen-position for gj/gk
550
- lastHSPos: -1,
551
- // The last motion command run. Cleared if a non-motion command gets
552
- // executed in between.
553
- lastMotion: null,
554
- marks: {},
555
- // Mark for rendering fake cursor for visual mode.
556
- fakeCursor: null,
557
- insertMode: false,
558
- // Repeat count for changes made in insert mode, triggered by key
559
- // sequences like 3,i. Only exists when insertMode is true.
560
- insertModeRepeat: undefined,
561
- visualMode: false,
562
- // If we are in visual line mode. No effect if visualMode is false.
563
- visualLine: false,
564
- visualBlock: false,
565
- lastSelection: null,
566
- lastPastedText: null,
567
- sel: {
568
- }
569
- };
570
- }
571
- return cm.state.vim;
572
- }
573
- var vimGlobalState;
574
- function resetVimGlobalState() {
575
- vimGlobalState = {
576
- // The current search query.
577
- searchQuery: null,
578
- // Whether we are searching backwards.
579
- searchIsReversed: false,
580
- // Replace part of the last substituted pattern
581
- lastSubstituteReplacePart: undefined,
582
- jumpList: createCircularJumpList(),
583
- macroModeState: new MacroModeState,
584
- // Recording latest f, t, F or T motion command.
585
- lastChararacterSearch: {increment:0, forward:true, selectedCharacter:''},
586
- registerController: new RegisterController({}),
587
- // search history buffer
588
- searchHistoryController: new HistoryController({}),
589
- // ex Command history buffer
590
- exCommandHistoryController : new HistoryController({})
591
- };
592
- for (var optionName in options) {
593
- var option = options[optionName];
594
- option.value = option.defaultValue;
595
- }
596
- }
597
-
598
- var lastInsertModeKeyTimer;
599
- var vimApi= {
600
- buildKeyMap: function() {
601
- // TODO: Convert keymap into dictionary format for fast lookup.
602
- },
603
- // Testing hook, though it might be useful to expose the register
604
- // controller anyways.
605
- getRegisterController: function() {
606
- return vimGlobalState.registerController;
607
- },
608
- // Testing hook.
609
- resetVimGlobalState_: resetVimGlobalState,
610
-
611
- // Testing hook.
612
- getVimGlobalState_: function() {
613
- return vimGlobalState;
614
- },
615
-
616
- // Testing hook.
617
- maybeInitVimState_: maybeInitVimState,
618
-
619
- suppressErrorLogging: false,
620
-
621
- InsertModeKey: InsertModeKey,
622
- map: function(lhs, rhs, ctx) {
623
- // Add user defined key bindings.
624
- exCommandDispatcher.map(lhs, rhs, ctx);
625
- },
626
- setOption: setOption,
627
- getOption: getOption,
628
- defineOption: defineOption,
629
- defineEx: function(name, prefix, func){
630
- if (name.indexOf(prefix) !== 0) {
631
- throw new Error('(Vim.defineEx) "'+prefix+'" is not a prefix of "'+name+'", command not registered');
632
- }
633
- exCommands[name]=func;
634
- exCommandDispatcher.commandMap_[prefix]={name:name, shortName:prefix, type:'api'};
635
- },
636
- handleKey: function (cm, key, origin) {
637
- var command = this.findKey(cm, key, origin);
638
- if (typeof command === 'function') {
639
- return command();
640
- }
641
- },
642
- /**
643
- * This is the outermost function called by CodeMirror, after keys have
644
- * been mapped to their Vim equivalents.
645
- *
646
- * Finds a command based on the key (and cached keys if there is a
647
- * multi-key sequence). Returns `undefined` if no key is matched, a noop
648
- * function if a partial match is found (multi-key), and a function to
649
- * execute the bound command if a a key is matched. The function always
650
- * returns true.
651
- */
652
- findKey: function(cm, key, origin) {
653
- var vim = maybeInitVimState(cm);
654
- function handleMacroRecording() {
655
- var macroModeState = vimGlobalState.macroModeState;
656
- if (macroModeState.isRecording) {
657
- if (key == 'q') {
658
- macroModeState.exitMacroRecordMode();
659
- clearInputState(cm);
660
- return true;
661
- }
662
- if (origin != 'mapping') {
663
- logKey(macroModeState, key);
664
- }
665
- }
666
- }
667
- function handleEsc() {
668
- if (key == '<Esc>') {
669
- // Clear input state and get back to normal mode.
670
- clearInputState(cm);
671
- if (vim.visualMode) {
672
- exitVisualMode(cm);
673
- } else if (vim.insertMode) {
674
- exitInsertMode(cm);
675
- }
676
- return true;
677
- }
678
- }
679
- function doKeyToKey(keys) {
680
- // TODO: prevent infinite recursion.
681
- var match;
682
- while (keys) {
683
- // Pull off one command key, which is either a single character
684
- // or a special sequence wrapped in '<' and '>', e.g. '<Space>'.
685
- match = (/<\w+-.+?>|<\w+>|./).exec(keys);
686
- key = match[0];
687
- keys = keys.substring(match.index + key.length);
688
- CodeMirror.Vim.handleKey(cm, key, 'mapping');
689
- }
690
- }
691
-
692
- function handleKeyInsertMode() {
693
- if (handleEsc()) { return true; }
694
- var keys = vim.inputState.keyBuffer = vim.inputState.keyBuffer + key;
695
- var keysAreChars = key.length == 1;
696
- var match = commandDispatcher.matchCommand(keys, defaultKeymap, vim.inputState, 'insert');
697
- // Need to check all key substrings in insert mode.
698
- while (keys.length > 1 && match.type != 'full') {
699
- var keys = vim.inputState.keyBuffer = keys.slice(1);
700
- var thisMatch = commandDispatcher.matchCommand(keys, defaultKeymap, vim.inputState, 'insert');
701
- if (thisMatch.type != 'none') { match = thisMatch; }
702
- }
703
- if (match.type == 'none') { clearInputState(cm); return false; }
704
- else if (match.type == 'partial') {
705
- if (lastInsertModeKeyTimer) { window.clearTimeout(lastInsertModeKeyTimer); }
706
- lastInsertModeKeyTimer = window.setTimeout(
707
- function() { if (vim.insertMode && vim.inputState.keyBuffer) { clearInputState(cm); } },
708
- getOption('insertModeEscKeysTimeout'));
709
- return !keysAreChars;
710
- }
711
-
712
- if (lastInsertModeKeyTimer) { window.clearTimeout(lastInsertModeKeyTimer); }
713
- if (keysAreChars) {
714
- var here = cm.getCursor();
715
- cm.replaceRange('', offsetCursor(here, 0, -(keys.length - 1)), here, '+input');
716
- }
717
- clearInputState(cm);
718
- return match.command;
719
- }
720
-
721
- function handleKeyNonInsertMode() {
722
- if (handleMacroRecording() || handleEsc()) { return true; };
723
-
724
- var keys = vim.inputState.keyBuffer = vim.inputState.keyBuffer + key;
725
- if (/^[1-9]\d*$/.test(keys)) { return true; }
726
-
727
- var keysMatcher = /^(\d*)(.*)$/.exec(keys);
728
- if (!keysMatcher) { clearInputState(cm); return false; }
729
- var context = vim.visualMode ? 'visual' :
730
- 'normal';
731
- var match = commandDispatcher.matchCommand(keysMatcher[2] || keysMatcher[1], defaultKeymap, vim.inputState, context);
732
- if (match.type == 'none') { clearInputState(cm); return false; }
733
- else if (match.type == 'partial') { return true; }
734
-
735
- vim.inputState.keyBuffer = '';
736
- var keysMatcher = /^(\d*)(.*)$/.exec(keys);
737
- if (keysMatcher[1] && keysMatcher[1] != '0') {
738
- vim.inputState.pushRepeatDigit(keysMatcher[1]);
739
- }
740
- return match.command;
741
- }
742
-
743
- var command;
744
- if (vim.insertMode) { command = handleKeyInsertMode(); }
745
- else { command = handleKeyNonInsertMode(); }
746
- if (command === false) {
747
- return undefined;
748
- } else if (command === true) {
749
- // TODO: Look into using CodeMirror's multi-key handling.
750
- // Return no-op since we are caching the key. Counts as handled, but
751
- // don't want act on it just yet.
752
- return function() {};
753
- } else {
754
- return function() {
755
- return cm.operation(function() {
756
- cm.curOp.isVimOp = true;
757
- try {
758
- if (command.type == 'keyToKey') {
759
- doKeyToKey(command.toKeys);
760
- } else {
761
- commandDispatcher.processCommand(cm, vim, command);
762
- }
763
- } catch (e) {
764
- // clear VIM state in case it's in a bad state.
765
- cm.state.vim = undefined;
766
- maybeInitVimState(cm);
767
- if (!CodeMirror.Vim.suppressErrorLogging) {
768
- console['log'](e);
769
- }
770
- throw e;
771
- }
772
- return true;
773
- });
774
- };
775
- }
776
- },
777
- handleEx: function(cm, input) {
778
- exCommandDispatcher.processCommand(cm, input);
779
- }
780
- };
781
-
782
- // Represents the current input state.
783
- function InputState() {
784
- this.prefixRepeat = [];
785
- this.motionRepeat = [];
786
-
787
- this.operator = null;
788
- this.operatorArgs = null;
789
- this.motion = null;
790
- this.motionArgs = null;
791
- this.keyBuffer = []; // For matching multi-key commands.
792
- this.registerName = null; // Defaults to the unnamed register.
793
- }
794
- InputState.prototype.pushRepeatDigit = function(n) {
795
- if (!this.operator) {
796
- this.prefixRepeat = this.prefixRepeat.concat(n);
797
- } else {
798
- this.motionRepeat = this.motionRepeat.concat(n);
799
- }
800
- };
801
- InputState.prototype.getRepeat = function() {
802
- var repeat = 0;
803
- if (this.prefixRepeat.length > 0 || this.motionRepeat.length > 0) {
804
- repeat = 1;
805
- if (this.prefixRepeat.length > 0) {
806
- repeat *= parseInt(this.prefixRepeat.join(''), 10);
807
- }
808
- if (this.motionRepeat.length > 0) {
809
- repeat *= parseInt(this.motionRepeat.join(''), 10);
810
- }
811
- }
812
- return repeat;
813
- };
814
-
815
- function clearInputState(cm, reason) {
816
- cm.state.vim.inputState = new InputState();
817
- CodeMirror.signal(cm, 'vim-command-done', reason);
818
- }
819
-
820
- /*
821
- * Register stores information about copy and paste registers. Besides
822
- * text, a register must store whether it is linewise (i.e., when it is
823
- * pasted, should it insert itself into a new line, or should the text be
824
- * inserted at the cursor position.)
825
- */
826
- function Register(text, linewise, blockwise) {
827
- this.clear();
828
- this.keyBuffer = [text || ''];
829
- this.insertModeChanges = [];
830
- this.searchQueries = [];
831
- this.linewise = !!linewise;
832
- this.blockwise = !!blockwise;
833
- }
834
- Register.prototype = {
835
- setText: function(text, linewise, blockwise) {
836
- this.keyBuffer = [text || ''];
837
- this.linewise = !!linewise;
838
- this.blockwise = !!blockwise;
839
- },
840
- pushText: function(text, linewise) {
841
- // if this register has ever been set to linewise, use linewise.
842
- if (linewise) {
843
- if (!this.linewise) {
844
- this.keyBuffer.push('\n');
845
- }
846
- this.linewise = true;
847
- }
848
- this.keyBuffer.push(text);
849
- },
850
- pushInsertModeChanges: function(changes) {
851
- this.insertModeChanges.push(createInsertModeChanges(changes));
852
- },
853
- pushSearchQuery: function(query) {
854
- this.searchQueries.push(query);
855
- },
856
- clear: function() {
857
- this.keyBuffer = [];
858
- this.insertModeChanges = [];
859
- this.searchQueries = [];
860
- this.linewise = false;
861
- },
862
- toString: function() {
863
- return this.keyBuffer.join('');
864
- }
865
- };
866
-
867
- /*
868
- * vim registers allow you to keep many independent copy and paste buffers.
869
- * See http://usevim.com/2012/04/13/registers/ for an introduction.
870
- *
871
- * RegisterController keeps the state of all the registers. An initial
872
- * state may be passed in. The unnamed register '"' will always be
873
- * overridden.
874
- */
875
- function RegisterController(registers) {
876
- this.registers = registers;
877
- this.unnamedRegister = registers['"'] = new Register();
878
- registers['.'] = new Register();
879
- registers[':'] = new Register();
880
- registers['/'] = new Register();
881
- }
882
- RegisterController.prototype = {
883
- pushText: function(registerName, operator, text, linewise, blockwise) {
884
- if (linewise && text.charAt(0) == '\n') {
885
- text = text.slice(1) + '\n';
886
- }
887
- if (linewise && text.charAt(text.length - 1) !== '\n'){
888
- text += '\n';
889
- }
890
- // Lowercase and uppercase registers refer to the same register.
891
- // Uppercase just means append.
892
- var register = this.isValidRegister(registerName) ?
893
- this.getRegister(registerName) : null;
894
- // if no register/an invalid register was specified, things go to the
895
- // default registers
896
- if (!register) {
897
- switch (operator) {
898
- case 'yank':
899
- // The 0 register contains the text from the most recent yank.
900
- this.registers['0'] = new Register(text, linewise, blockwise);
901
- break;
902
- case 'delete':
903
- case 'change':
904
- if (text.indexOf('\n') == -1) {
905
- // Delete less than 1 line. Update the small delete register.
906
- this.registers['-'] = new Register(text, linewise);
907
- } else {
908
- // Shift down the contents of the numbered registers and put the
909
- // deleted text into register 1.
910
- this.shiftNumericRegisters_();
911
- this.registers['1'] = new Register(text, linewise);
912
- }
913
- break;
914
- }
915
- // Make sure the unnamed register is set to what just happened
916
- this.unnamedRegister.setText(text, linewise, blockwise);
917
- return;
918
- }
919
-
920
- // If we've gotten to this point, we've actually specified a register
921
- var append = isUpperCase(registerName);
922
- if (append) {
923
- register.pushText(text, linewise);
924
- } else {
925
- register.setText(text, linewise, blockwise);
926
- }
927
- // The unnamed register always has the same value as the last used
928
- // register.
929
- this.unnamedRegister.setText(register.toString(), linewise);
930
- },
931
- // Gets the register named @name. If one of @name doesn't already exist,
932
- // create it. If @name is invalid, return the unnamedRegister.
933
- getRegister: function(name) {
934
- if (!this.isValidRegister(name)) {
935
- return this.unnamedRegister;
936
- }
937
- name = name.toLowerCase();
938
- if (!this.registers[name]) {
939
- this.registers[name] = new Register();
940
- }
941
- return this.registers[name];
942
- },
943
- isValidRegister: function(name) {
944
- return name && inArray(name, validRegisters);
945
- },
946
- shiftNumericRegisters_: function() {
947
- for (var i = 9; i >= 2; i--) {
948
- this.registers[i] = this.getRegister('' + (i - 1));
949
- }
950
- }
951
- };
952
- function HistoryController() {
953
- this.historyBuffer = [];
954
- this.iterator;
955
- this.initialPrefix = null;
956
- }
957
- HistoryController.prototype = {
958
- // the input argument here acts a user entered prefix for a small time
959
- // until we start autocompletion in which case it is the autocompleted.
960
- nextMatch: function (input, up) {
961
- var historyBuffer = this.historyBuffer;
962
- var dir = up ? -1 : 1;
963
- if (this.initialPrefix === null) this.initialPrefix = input;
964
- for (var i = this.iterator + dir; up ? i >= 0 : i < historyBuffer.length; i+= dir) {
965
- var element = historyBuffer[i];
966
- for (var j = 0; j <= element.length; j++) {
967
- if (this.initialPrefix == element.substring(0, j)) {
968
- this.iterator = i;
969
- return element;
970
- }
971
- }
972
- }
973
- // should return the user input in case we reach the end of buffer.
974
- if (i >= historyBuffer.length) {
975
- this.iterator = historyBuffer.length;
976
- return this.initialPrefix;
977
- }
978
- // return the last autocompleted query or exCommand as it is.
979
- if (i < 0 ) return input;
980
- },
981
- pushInput: function(input) {
982
- var index = this.historyBuffer.indexOf(input);
983
- if (index > -1) this.historyBuffer.splice(index, 1);
984
- if (input.length) this.historyBuffer.push(input);
985
- },
986
- reset: function() {
987
- this.initialPrefix = null;
988
- this.iterator = this.historyBuffer.length;
989
- }
990
- };
991
- var commandDispatcher = {
992
- matchCommand: function(keys, keyMap, inputState, context) {
993
- var matches = commandMatches(keys, keyMap, context, inputState);
994
- if (!matches.full && !matches.partial) {
995
- return {type: 'none'};
996
- } else if (!matches.full && matches.partial) {
997
- return {type: 'partial'};
998
- }
999
-
1000
- var bestMatch;
1001
- for (var i = 0; i < matches.full.length; i++) {
1002
- var match = matches.full[i];
1003
- if (!bestMatch) {
1004
- bestMatch = match;
1005
- }
1006
- }
1007
- if (bestMatch.keys.slice(-11) == '<character>') {
1008
- inputState.selectedCharacter = lastChar(keys);
1009
- }
1010
- return {type: 'full', command: bestMatch};
1011
- },
1012
- processCommand: function(cm, vim, command) {
1013
- vim.inputState.repeatOverride = command.repeatOverride;
1014
- switch (command.type) {
1015
- case 'motion':
1016
- this.processMotion(cm, vim, command);
1017
- break;
1018
- case 'operator':
1019
- this.processOperator(cm, vim, command);
1020
- break;
1021
- case 'operatorMotion':
1022
- this.processOperatorMotion(cm, vim, command);
1023
- break;
1024
- case 'action':
1025
- this.processAction(cm, vim, command);
1026
- break;
1027
- case 'search':
1028
- this.processSearch(cm, vim, command);
1029
- clearInputState(cm);
1030
- break;
1031
- case 'ex':
1032
- case 'keyToEx':
1033
- this.processEx(cm, vim, command);
1034
- clearInputState(cm);
1035
- break;
1036
- default:
1037
- break;
1038
- }
1039
- },
1040
- processMotion: function(cm, vim, command) {
1041
- vim.inputState.motion = command.motion;
1042
- vim.inputState.motionArgs = copyArgs(command.motionArgs);
1043
- this.evalInput(cm, vim);
1044
- },
1045
- processOperator: function(cm, vim, command) {
1046
- var inputState = vim.inputState;
1047
- if (inputState.operator) {
1048
- if (inputState.operator == command.operator) {
1049
- // Typing an operator twice like 'dd' makes the operator operate
1050
- // linewise
1051
- inputState.motion = 'expandToLine';
1052
- inputState.motionArgs = { linewise: true };
1053
- this.evalInput(cm, vim);
1054
- return;
1055
- } else {
1056
- // 2 different operators in a row doesn't make sense.
1057
- clearInputState(cm);
1058
- }
1059
- }
1060
- inputState.operator = command.operator;
1061
- inputState.operatorArgs = copyArgs(command.operatorArgs);
1062
- if (vim.visualMode) {
1063
- // Operating on a selection in visual mode. We don't need a motion.
1064
- this.evalInput(cm, vim);
1065
- }
1066
- },
1067
- processOperatorMotion: function(cm, vim, command) {
1068
- var visualMode = vim.visualMode;
1069
- var operatorMotionArgs = copyArgs(command.operatorMotionArgs);
1070
- if (operatorMotionArgs) {
1071
- // Operator motions may have special behavior in visual mode.
1072
- if (visualMode && operatorMotionArgs.visualLine) {
1073
- vim.visualLine = true;
1074
- }
1075
- }
1076
- this.processOperator(cm, vim, command);
1077
- if (!visualMode) {
1078
- this.processMotion(cm, vim, command);
1079
- }
1080
- },
1081
- processAction: function(cm, vim, command) {
1082
- var inputState = vim.inputState;
1083
- var repeat = inputState.getRepeat();
1084
- var repeatIsExplicit = !!repeat;
1085
- var actionArgs = copyArgs(command.actionArgs) || {};
1086
- if (inputState.selectedCharacter) {
1087
- actionArgs.selectedCharacter = inputState.selectedCharacter;
1088
- }
1089
- // Actions may or may not have motions and operators. Do these first.
1090
- if (command.operator) {
1091
- this.processOperator(cm, vim, command);
1092
- }
1093
- if (command.motion) {
1094
- this.processMotion(cm, vim, command);
1095
- }
1096
- if (command.motion || command.operator) {
1097
- this.evalInput(cm, vim);
1098
- }
1099
- actionArgs.repeat = repeat || 1;
1100
- actionArgs.repeatIsExplicit = repeatIsExplicit;
1101
- actionArgs.registerName = inputState.registerName;
1102
- clearInputState(cm);
1103
- vim.lastMotion = null;
1104
- if (command.isEdit) {
1105
- this.recordLastEdit(vim, inputState, command);
1106
- }
1107
- actions[command.action](cm, actionArgs, vim);
1108
- },
1109
- processSearch: function(cm, vim, command) {
1110
- if (!cm.getSearchCursor) {
1111
- // Search depends on SearchCursor.
1112
- return;
1113
- }
1114
- var forward = command.searchArgs.forward;
1115
- var wholeWordOnly = command.searchArgs.wholeWordOnly;
1116
- getSearchState(cm).setReversed(!forward);
1117
- var promptPrefix = (forward) ? '/' : '?';
1118
- var originalQuery = getSearchState(cm).getQuery();
1119
- var originalScrollPos = cm.getScrollInfo();
1120
- function handleQuery(query, ignoreCase, smartCase) {
1121
- vimGlobalState.searchHistoryController.pushInput(query);
1122
- vimGlobalState.searchHistoryController.reset();
1123
- try {
1124
- updateSearchQuery(cm, query, ignoreCase, smartCase);
1125
- } catch (e) {
1126
- showConfirm(cm, 'Invalid regex: ' + query);
1127
- return;
1128
- }
1129
- commandDispatcher.processMotion(cm, vim, {
1130
- type: 'motion',
1131
- motion: 'findNext',
1132
- motionArgs: { forward: true, toJumplist: command.searchArgs.toJumplist }
1133
- });
1134
- }
1135
- function onPromptClose(query) {
1136
- cm.scrollTo(originalScrollPos.left, originalScrollPos.top);
1137
- handleQuery(query, true /** ignoreCase */, true /** smartCase */);
1138
- var macroModeState = vimGlobalState.macroModeState;
1139
- if (macroModeState.isRecording) {
1140
- logSearchQuery(macroModeState, query);
1141
- }
1142
- }
1143
- function onPromptKeyUp(e, query, close) {
1144
- var keyName = CodeMirror.keyName(e), up;
1145
- if (keyName == 'Up' || keyName == 'Down') {
1146
- up = keyName == 'Up' ? true : false;
1147
- query = vimGlobalState.searchHistoryController.nextMatch(query, up) || '';
1148
- close(query);
1149
- } else {
1150
- if ( keyName != 'Left' && keyName != 'Right' && keyName != 'Ctrl' && keyName != 'Alt' && keyName != 'Shift')
1151
- vimGlobalState.searchHistoryController.reset();
1152
- }
1153
- var parsedQuery;
1154
- try {
1155
- parsedQuery = updateSearchQuery(cm, query,
1156
- true /** ignoreCase */, true /** smartCase */);
1157
- } catch (e) {
1158
- // Swallow bad regexes for incremental search.
1159
- }
1160
- if (parsedQuery) {
1161
- cm.scrollIntoView(findNext(cm, !forward, parsedQuery), 30);
1162
- } else {
1163
- clearSearchHighlight(cm);
1164
- cm.scrollTo(originalScrollPos.left, originalScrollPos.top);
1165
- }
1166
- }
1167
- function onPromptKeyDown(e, query, close) {
1168
- var keyName = CodeMirror.keyName(e);
1169
- if (keyName == 'Esc' || keyName == 'Ctrl-C' || keyName == 'Ctrl-[') {
1170
- vimGlobalState.searchHistoryController.pushInput(query);
1171
- vimGlobalState.searchHistoryController.reset();
1172
- updateSearchQuery(cm, originalQuery);
1173
- clearSearchHighlight(cm);
1174
- cm.scrollTo(originalScrollPos.left, originalScrollPos.top);
1175
- CodeMirror.e_stop(e);
1176
- close();
1177
- cm.focus();
1178
- }
1179
- }
1180
- switch (command.searchArgs.querySrc) {
1181
- case 'prompt':
1182
- var macroModeState = vimGlobalState.macroModeState;
1183
- if (macroModeState.isPlaying) {
1184
- var query = macroModeState.replaySearchQueries.shift();
1185
- handleQuery(query, true /** ignoreCase */, false /** smartCase */);
1186
- } else {
1187
- showPrompt(cm, {
1188
- onClose: onPromptClose,
1189
- prefix: promptPrefix,
1190
- desc: searchPromptDesc,
1191
- onKeyUp: onPromptKeyUp,
1192
- onKeyDown: onPromptKeyDown
1193
- });
1194
- }
1195
- break;
1196
- case 'wordUnderCursor':
1197
- var word = expandWordUnderCursor(cm, false /** inclusive */,
1198
- true /** forward */, false /** bigWord */,
1199
- true /** noSymbol */);
1200
- var isKeyword = true;
1201
- if (!word) {
1202
- word = expandWordUnderCursor(cm, false /** inclusive */,
1203
- true /** forward */, false /** bigWord */,
1204
- false /** noSymbol */);
1205
- isKeyword = false;
1206
- }
1207
- if (!word) {
1208
- return;
1209
- }
1210
- var query = cm.getLine(word.start.line).substring(word.start.ch,
1211
- word.end.ch);
1212
- if (isKeyword && wholeWordOnly) {
1213
- query = '\\b' + query + '\\b';
1214
- } else {
1215
- query = escapeRegex(query);
1216
- }
1217
-
1218
- // cachedCursor is used to save the old position of the cursor
1219
- // when * or # causes vim to seek for the nearest word and shift
1220
- // the cursor before entering the motion.
1221
- vimGlobalState.jumpList.cachedCursor = cm.getCursor();
1222
- cm.setCursor(word.start);
1223
-
1224
- handleQuery(query, true /** ignoreCase */, false /** smartCase */);
1225
- break;
1226
- }
1227
- },
1228
- processEx: function(cm, vim, command) {
1229
- function onPromptClose(input) {
1230
- // Give the prompt some time to close so that if processCommand shows
1231
- // an error, the elements don't overlap.
1232
- vimGlobalState.exCommandHistoryController.pushInput(input);
1233
- vimGlobalState.exCommandHistoryController.reset();
1234
- exCommandDispatcher.processCommand(cm, input);
1235
- }
1236
- function onPromptKeyDown(e, input, close) {
1237
- var keyName = CodeMirror.keyName(e), up;
1238
- if (keyName == 'Esc' || keyName == 'Ctrl-C' || keyName == 'Ctrl-[') {
1239
- vimGlobalState.exCommandHistoryController.pushInput(input);
1240
- vimGlobalState.exCommandHistoryController.reset();
1241
- CodeMirror.e_stop(e);
1242
- close();
1243
- cm.focus();
1244
- }
1245
- if (keyName == 'Up' || keyName == 'Down') {
1246
- up = keyName == 'Up' ? true : false;
1247
- input = vimGlobalState.exCommandHistoryController.nextMatch(input, up) || '';
1248
- close(input);
1249
- } else {
1250
- if ( keyName != 'Left' && keyName != 'Right' && keyName != 'Ctrl' && keyName != 'Alt' && keyName != 'Shift')
1251
- vimGlobalState.exCommandHistoryController.reset();
1252
- }
1253
- }
1254
- if (command.type == 'keyToEx') {
1255
- // Handle user defined Ex to Ex mappings
1256
- exCommandDispatcher.processCommand(cm, command.exArgs.input);
1257
- } else {
1258
- if (vim.visualMode) {
1259
- showPrompt(cm, { onClose: onPromptClose, prefix: ':', value: '\'<,\'>',
1260
- onKeyDown: onPromptKeyDown});
1261
- } else {
1262
- showPrompt(cm, { onClose: onPromptClose, prefix: ':',
1263
- onKeyDown: onPromptKeyDown});
1264
- }
1265
- }
1266
- },
1267
- evalInput: function(cm, vim) {
1268
- // If the motion comand is set, execute both the operator and motion.
1269
- // Otherwise return.
1270
- var inputState = vim.inputState;
1271
- var motion = inputState.motion;
1272
- var motionArgs = inputState.motionArgs || {};
1273
- var operator = inputState.operator;
1274
- var operatorArgs = inputState.operatorArgs || {};
1275
- var registerName = inputState.registerName;
1276
- var sel = vim.sel;
1277
- // TODO: Make sure cm and vim selections are identical outside visual mode.
1278
- var origHead = copyCursor(vim.visualMode ? sel.head: cm.getCursor('head'));
1279
- var origAnchor = copyCursor(vim.visualMode ? sel.anchor : cm.getCursor('anchor'));
1280
- var oldHead = copyCursor(origHead);
1281
- var oldAnchor = copyCursor(origAnchor);
1282
- var newHead, newAnchor;
1283
- var repeat;
1284
- if (operator) {
1285
- this.recordLastEdit(vim, inputState);
1286
- }
1287
- if (inputState.repeatOverride !== undefined) {
1288
- // If repeatOverride is specified, that takes precedence over the
1289
- // input state's repeat. Used by Ex mode and can be user defined.
1290
- repeat = inputState.repeatOverride;
1291
- } else {
1292
- repeat = inputState.getRepeat();
1293
- }
1294
- if (repeat > 0 && motionArgs.explicitRepeat) {
1295
- motionArgs.repeatIsExplicit = true;
1296
- } else if (motionArgs.noRepeat ||
1297
- (!motionArgs.explicitRepeat && repeat === 0)) {
1298
- repeat = 1;
1299
- motionArgs.repeatIsExplicit = false;
1300
- }
1301
- if (inputState.selectedCharacter) {
1302
- // If there is a character input, stick it in all of the arg arrays.
1303
- motionArgs.selectedCharacter = operatorArgs.selectedCharacter =
1304
- inputState.selectedCharacter;
1305
- }
1306
- motionArgs.repeat = repeat;
1307
- clearInputState(cm);
1308
- if (motion) {
1309
- var motionResult = motions[motion](cm, origHead, motionArgs, vim);
1310
- vim.lastMotion = motions[motion];
1311
- if (!motionResult) {
1312
- return;
1313
- }
1314
- if (motionArgs.toJumplist) {
1315
- var jumpList = vimGlobalState.jumpList;
1316
- // if the current motion is # or *, use cachedCursor
1317
- var cachedCursor = jumpList.cachedCursor;
1318
- if (cachedCursor) {
1319
- recordJumpPosition(cm, cachedCursor, motionResult);
1320
- delete jumpList.cachedCursor;
1321
- } else {
1322
- recordJumpPosition(cm, origHead, motionResult);
1323
- }
1324
- }
1325
- if (motionResult instanceof Array) {
1326
- newAnchor = motionResult[0];
1327
- newHead = motionResult[1];
1328
- } else {
1329
- newHead = motionResult;
1330
- }
1331
- // TODO: Handle null returns from motion commands better.
1332
- if (!newHead) {
1333
- newHead = copyCursor(origHead);
1334
- }
1335
- if (vim.visualMode) {
1336
- if (!(vim.visualBlock && newHead.ch === Infinity)) {
1337
- newHead = clipCursorToContent(cm, newHead, vim.visualBlock);
1338
- }
1339
- if (newAnchor) {
1340
- newAnchor = clipCursorToContent(cm, newAnchor, true);
1341
- }
1342
- newAnchor = newAnchor || oldAnchor;
1343
- sel.anchor = newAnchor;
1344
- sel.head = newHead;
1345
- updateCmSelection(cm);
1346
- updateMark(cm, vim, '<',
1347
- cursorIsBefore(newAnchor, newHead) ? newAnchor
1348
- : newHead);
1349
- updateMark(cm, vim, '>',
1350
- cursorIsBefore(newAnchor, newHead) ? newHead
1351
- : newAnchor);
1352
- } else if (!operator) {
1353
- newHead = clipCursorToContent(cm, newHead);
1354
- cm.setCursor(newHead.line, newHead.ch);
1355
- }
1356
- }
1357
- if (operator) {
1358
- if (operatorArgs.lastSel) {
1359
- // Replaying a visual mode operation
1360
- newAnchor = oldAnchor;
1361
- var lastSel = operatorArgs.lastSel;
1362
- var lineOffset = Math.abs(lastSel.head.line - lastSel.anchor.line);
1363
- var chOffset = Math.abs(lastSel.head.ch - lastSel.anchor.ch);
1364
- if (lastSel.visualLine) {
1365
- // Linewise Visual mode: The same number of lines.
1366
- newHead = Pos(oldAnchor.line + lineOffset, oldAnchor.ch);
1367
- } else if (lastSel.visualBlock) {
1368
- // Blockwise Visual mode: The same number of lines and columns.
1369
- newHead = Pos(oldAnchor.line + lineOffset, oldAnchor.ch + chOffset);
1370
- } else if (lastSel.head.line == lastSel.anchor.line) {
1371
- // Normal Visual mode within one line: The same number of characters.
1372
- newHead = Pos(oldAnchor.line, oldAnchor.ch + chOffset);
1373
- } else {
1374
- // Normal Visual mode with several lines: The same number of lines, in the
1375
- // last line the same number of characters as in the last line the last time.
1376
- newHead = Pos(oldAnchor.line + lineOffset, oldAnchor.ch);
1377
- }
1378
- vim.visualMode = true;
1379
- vim.visualLine = lastSel.visualLine;
1380
- vim.visualBlock = lastSel.visualBlock;
1381
- sel = vim.sel = {
1382
- anchor: newAnchor,
1383
- head: newHead
1384
- };
1385
- updateCmSelection(cm);
1386
- } else if (vim.visualMode) {
1387
- operatorArgs.lastSel = {
1388
- anchor: copyCursor(sel.anchor),
1389
- head: copyCursor(sel.head),
1390
- visualBlock: vim.visualBlock,
1391
- visualLine: vim.visualLine
1392
- };
1393
- }
1394
- var curStart, curEnd, linewise, mode;
1395
- var cmSel;
1396
- if (vim.visualMode) {
1397
- // Init visual op
1398
- curStart = cursorMin(sel.head, sel.anchor);
1399
- curEnd = cursorMax(sel.head, sel.anchor);
1400
- linewise = vim.visualLine || operatorArgs.linewise;
1401
- mode = vim.visualBlock ? 'block' :
1402
- linewise ? 'line' :
1403
- 'char';
1404
- cmSel = makeCmSelection(cm, {
1405
- anchor: curStart,
1406
- head: curEnd
1407
- }, mode);
1408
- if (linewise) {
1409
- var ranges = cmSel.ranges;
1410
- if (mode == 'block') {
1411
- // Linewise operators in visual block mode extend to end of line
1412
- for (var i = 0; i < ranges.length; i++) {
1413
- ranges[i].head.ch = lineLength(cm, ranges[i].head.line);
1414
- }
1415
- } else if (mode == 'line') {
1416
- ranges[0].head = Pos(ranges[0].head.line + 1, 0);
1417
- }
1418
- }
1419
- } else {
1420
- // Init motion op
1421
- curStart = copyCursor(newAnchor || oldAnchor);
1422
- curEnd = copyCursor(newHead || oldHead);
1423
- if (cursorIsBefore(curEnd, curStart)) {
1424
- var tmp = curStart;
1425
- curStart = curEnd;
1426
- curEnd = tmp;
1427
- }
1428
- linewise = motionArgs.linewise || operatorArgs.linewise;
1429
- if (linewise) {
1430
- // Expand selection to entire line.
1431
- expandSelectionToLine(cm, curStart, curEnd);
1432
- } else if (motionArgs.forward) {
1433
- // Clip to trailing newlines only if the motion goes forward.
1434
- clipToLine(cm, curStart, curEnd);
1435
- }
1436
- mode = 'char';
1437
- var exclusive = !motionArgs.inclusive || linewise;
1438
- cmSel = makeCmSelection(cm, {
1439
- anchor: curStart,
1440
- head: curEnd
1441
- }, mode, exclusive);
1442
- }
1443
- cm.setSelections(cmSel.ranges, cmSel.primary);
1444
- vim.lastMotion = null;
1445
- operatorArgs.repeat = repeat; // For indent in visual mode.
1446
- operatorArgs.registerName = registerName;
1447
- // Keep track of linewise as it affects how paste and change behave.
1448
- operatorArgs.linewise = linewise;
1449
- var operatorMoveTo = operators[operator](
1450
- cm, operatorArgs, cmSel.ranges, oldAnchor, newHead);
1451
- if (vim.visualMode) {
1452
- exitVisualMode(cm);
1453
- }
1454
- if (operatorMoveTo) {
1455
- cm.setCursor(operatorMoveTo);
1456
- }
1457
- }
1458
- },
1459
- recordLastEdit: function(vim, inputState, actionCommand) {
1460
- var macroModeState = vimGlobalState.macroModeState;
1461
- if (macroModeState.isPlaying) { return; }
1462
- vim.lastEditInputState = inputState;
1463
- vim.lastEditActionCommand = actionCommand;
1464
- macroModeState.lastInsertModeChanges.changes = [];
1465
- macroModeState.lastInsertModeChanges.expectCursorActivityForChange = false;
1466
- }
1467
- };
1468
-
1469
- /**
1470
- * typedef {Object{line:number,ch:number}} Cursor An object containing the
1471
- * position of the cursor.
1472
- */
1473
- // All of the functions below return Cursor objects.
1474
- var motions = {
1475
- moveToTopLine: function(cm, _head, motionArgs) {
1476
- var line = getUserVisibleLines(cm).top + motionArgs.repeat -1;
1477
- return Pos(line, findFirstNonWhiteSpaceCharacter(cm.getLine(line)));
1478
- },
1479
- moveToMiddleLine: function(cm) {
1480
- var range = getUserVisibleLines(cm);
1481
- var line = Math.floor((range.top + range.bottom) * 0.5);
1482
- return Pos(line, findFirstNonWhiteSpaceCharacter(cm.getLine(line)));
1483
- },
1484
- moveToBottomLine: function(cm, _head, motionArgs) {
1485
- var line = getUserVisibleLines(cm).bottom - motionArgs.repeat +1;
1486
- return Pos(line, findFirstNonWhiteSpaceCharacter(cm.getLine(line)));
1487
- },
1488
- expandToLine: function(_cm, head, motionArgs) {
1489
- // Expands forward to end of line, and then to next line if repeat is
1490
- // >1. Does not handle backward motion!
1491
- var cur = head;
1492
- return Pos(cur.line + motionArgs.repeat - 1, Infinity);
1493
- },
1494
- findNext: function(cm, _head, motionArgs) {
1495
- var state = getSearchState(cm);
1496
- var query = state.getQuery();
1497
- if (!query) {
1498
- return;
1499
- }
1500
- var prev = !motionArgs.forward;
1501
- // If search is initiated with ? instead of /, negate direction.
1502
- prev = (state.isReversed()) ? !prev : prev;
1503
- highlightSearchMatches(cm, query);
1504
- return findNext(cm, prev/** prev */, query, motionArgs.repeat);
1505
- },
1506
- goToMark: function(cm, _head, motionArgs, vim) {
1507
- var mark = vim.marks[motionArgs.selectedCharacter];
1508
- if (mark) {
1509
- var pos = mark.find();
1510
- return motionArgs.linewise ? { line: pos.line, ch: findFirstNonWhiteSpaceCharacter(cm.getLine(pos.line)) } : pos;
1511
- }
1512
- return null;
1513
- },
1514
- moveToOtherHighlightedEnd: function(cm, _head, motionArgs, vim) {
1515
- if (vim.visualBlock && motionArgs.sameLine) {
1516
- var sel = vim.sel;
1517
- return [
1518
- clipCursorToContent(cm, Pos(sel.anchor.line, sel.head.ch)),
1519
- clipCursorToContent(cm, Pos(sel.head.line, sel.anchor.ch))
1520
- ];
1521
- } else {
1522
- return ([vim.sel.head, vim.sel.anchor]);
1523
- }
1524
- },
1525
- jumpToMark: function(cm, head, motionArgs, vim) {
1526
- var best = head;
1527
- for (var i = 0; i < motionArgs.repeat; i++) {
1528
- var cursor = best;
1529
- for (var key in vim.marks) {
1530
- if (!isLowerCase(key)) {
1531
- continue;
1532
- }
1533
- var mark = vim.marks[key].find();
1534
- var isWrongDirection = (motionArgs.forward) ?
1535
- cursorIsBefore(mark, cursor) : cursorIsBefore(cursor, mark);
1536
-
1537
- if (isWrongDirection) {
1538
- continue;
1539
- }
1540
- if (motionArgs.linewise && (mark.line == cursor.line)) {
1541
- continue;
1542
- }
1543
-
1544
- var equal = cursorEqual(cursor, best);
1545
- var between = (motionArgs.forward) ?
1546
- cursorIsBetween(cursor, mark, best) :
1547
- cursorIsBetween(best, mark, cursor);
1548
-
1549
- if (equal || between) {
1550
- best = mark;
1551
- }
1552
- }
1553
- }
1554
-
1555
- if (motionArgs.linewise) {
1556
- // Vim places the cursor on the first non-whitespace character of
1557
- // the line if there is one, else it places the cursor at the end
1558
- // of the line, regardless of whether a mark was found.
1559
- best = Pos(best.line, findFirstNonWhiteSpaceCharacter(cm.getLine(best.line)));
1560
- }
1561
- return best;
1562
- },
1563
- moveByCharacters: function(_cm, head, motionArgs) {
1564
- var cur = head;
1565
- var repeat = motionArgs.repeat;
1566
- var ch = motionArgs.forward ? cur.ch + repeat : cur.ch - repeat;
1567
- return Pos(cur.line, ch);
1568
- },
1569
- moveByLines: function(cm, head, motionArgs, vim) {
1570
- var cur = head;
1571
- var endCh = cur.ch;
1572
- // Depending what our last motion was, we may want to do different
1573
- // things. If our last motion was moving vertically, we want to
1574
- // preserve the HPos from our last horizontal move. If our last motion
1575
- // was going to the end of a line, moving vertically we should go to
1576
- // the end of the line, etc.
1577
- switch (vim.lastMotion) {
1578
- case this.moveByLines:
1579
- case this.moveByDisplayLines:
1580
- case this.moveByScroll:
1581
- case this.moveToColumn:
1582
- case this.moveToEol:
1583
- endCh = vim.lastHPos;
1584
- break;
1585
- default:
1586
- vim.lastHPos = endCh;
1587
- }
1588
- var repeat = motionArgs.repeat+(motionArgs.repeatOffset||0);
1589
- var line = motionArgs.forward ? cur.line + repeat : cur.line - repeat;
1590
- var first = cm.firstLine();
1591
- var last = cm.lastLine();
1592
- // Vim cancels linewise motions that start on an edge and move beyond
1593
- // that edge. It does not cancel motions that do not start on an edge.
1594
- if ((line < first && cur.line == first) ||
1595
- (line > last && cur.line == last)) {
1596
- return;
1597
- }
1598
- if (motionArgs.toFirstChar){
1599
- endCh=findFirstNonWhiteSpaceCharacter(cm.getLine(line));
1600
- vim.lastHPos = endCh;
1601
- }
1602
- vim.lastHSPos = cm.charCoords(Pos(line, endCh),'div').left;
1603
- return Pos(line, endCh);
1604
- },
1605
- moveByDisplayLines: function(cm, head, motionArgs, vim) {
1606
- var cur = head;
1607
- switch (vim.lastMotion) {
1608
- case this.moveByDisplayLines:
1609
- case this.moveByScroll:
1610
- case this.moveByLines:
1611
- case this.moveToColumn:
1612
- case this.moveToEol:
1613
- break;
1614
- default:
1615
- vim.lastHSPos = cm.charCoords(cur,'div').left;
1616
- }
1617
- var repeat = motionArgs.repeat;
1618
- var res=cm.findPosV(cur,(motionArgs.forward ? repeat : -repeat),'line',vim.lastHSPos);
1619
- if (res.hitSide) {
1620
- if (motionArgs.forward) {
1621
- var lastCharCoords = cm.charCoords(res, 'div');
1622
- var goalCoords = { top: lastCharCoords.top + 8, left: vim.lastHSPos };
1623
- var res = cm.coordsChar(goalCoords, 'div');
1624
- } else {
1625
- var resCoords = cm.charCoords(Pos(cm.firstLine(), 0), 'div');
1626
- resCoords.left = vim.lastHSPos;
1627
- res = cm.coordsChar(resCoords, 'div');
1628
- }
1629
- }
1630
- vim.lastHPos = res.ch;
1631
- return res;
1632
- },
1633
- moveByPage: function(cm, head, motionArgs) {
1634
- // CodeMirror only exposes functions that move the cursor page down, so
1635
- // doing this bad hack to move the cursor and move it back. evalInput
1636
- // will move the cursor to where it should be in the end.
1637
- var curStart = head;
1638
- var repeat = motionArgs.repeat;
1639
- return cm.findPosV(curStart, (motionArgs.forward ? repeat : -repeat), 'page');
1640
- },
1641
- moveByParagraph: function(cm, head, motionArgs) {
1642
- var dir = motionArgs.forward ? 1 : -1;
1643
- return findParagraph(cm, head, motionArgs.repeat, dir);
1644
- },
1645
- moveByScroll: function(cm, head, motionArgs, vim) {
1646
- var scrollbox = cm.getScrollInfo();
1647
- var curEnd = null;
1648
- var repeat = motionArgs.repeat;
1649
- if (!repeat) {
1650
- repeat = scrollbox.clientHeight / (2 * cm.defaultTextHeight());
1651
- }
1652
- var orig = cm.charCoords(head, 'local');
1653
- motionArgs.repeat = repeat;
1654
- var curEnd = motions.moveByDisplayLines(cm, head, motionArgs, vim);
1655
- if (!curEnd) {
1656
- return null;
1657
- }
1658
- var dest = cm.charCoords(curEnd, 'local');
1659
- cm.scrollTo(null, scrollbox.top + dest.top - orig.top);
1660
- return curEnd;
1661
- },
1662
- moveByWords: function(cm, head, motionArgs) {
1663
- return moveToWord(cm, head, motionArgs.repeat, !!motionArgs.forward,
1664
- !!motionArgs.wordEnd, !!motionArgs.bigWord);
1665
- },
1666
- moveTillCharacter: function(cm, _head, motionArgs) {
1667
- var repeat = motionArgs.repeat;
1668
- var curEnd = moveToCharacter(cm, repeat, motionArgs.forward,
1669
- motionArgs.selectedCharacter);
1670
- var increment = motionArgs.forward ? -1 : 1;
1671
- recordLastCharacterSearch(increment, motionArgs);
1672
- if (!curEnd) return null;
1673
- curEnd.ch += increment;
1674
- return curEnd;
1675
- },
1676
- moveToCharacter: function(cm, head, motionArgs) {
1677
- var repeat = motionArgs.repeat;
1678
- recordLastCharacterSearch(0, motionArgs);
1679
- return moveToCharacter(cm, repeat, motionArgs.forward,
1680
- motionArgs.selectedCharacter) || head;
1681
- },
1682
- moveToSymbol: function(cm, head, motionArgs) {
1683
- var repeat = motionArgs.repeat;
1684
- return findSymbol(cm, repeat, motionArgs.forward,
1685
- motionArgs.selectedCharacter) || head;
1686
- },
1687
- moveToColumn: function(cm, head, motionArgs, vim) {
1688
- var repeat = motionArgs.repeat;
1689
- // repeat is equivalent to which column we want to move to!
1690
- vim.lastHPos = repeat - 1;
1691
- vim.lastHSPos = cm.charCoords(head,'div').left;
1692
- return moveToColumn(cm, repeat);
1693
- },
1694
- moveToEol: function(cm, head, motionArgs, vim) {
1695
- var cur = head;
1696
- vim.lastHPos = Infinity;
1697
- var retval= Pos(cur.line + motionArgs.repeat - 1, Infinity);
1698
- var end=cm.clipPos(retval);
1699
- end.ch--;
1700
- vim.lastHSPos = cm.charCoords(end,'div').left;
1701
- return retval;
1702
- },
1703
- moveToFirstNonWhiteSpaceCharacter: function(cm, head) {
1704
- // Go to the start of the line where the text begins, or the end for
1705
- // whitespace-only lines
1706
- var cursor = head;
1707
- return Pos(cursor.line,
1708
- findFirstNonWhiteSpaceCharacter(cm.getLine(cursor.line)));
1709
- },
1710
- moveToMatchedSymbol: function(cm, head) {
1711
- var cursor = head;
1712
- var line = cursor.line;
1713
- var ch = cursor.ch;
1714
- var lineText = cm.getLine(line);
1715
- var symbol;
1716
- do {
1717
- symbol = lineText.charAt(ch++);
1718
- if (symbol && isMatchableSymbol(symbol)) {
1719
- var style = cm.getTokenTypeAt(Pos(line, ch));
1720
- if (style !== "string" && style !== "comment") {
1721
- break;
1722
- }
1723
- }
1724
- } while (symbol);
1725
- if (symbol) {
1726
- var matched = cm.findMatchingBracket(Pos(line, ch));
1727
- return matched.to;
1728
- } else {
1729
- return cursor;
1730
- }
1731
- },
1732
- moveToStartOfLine: function(_cm, head) {
1733
- return Pos(head.line, 0);
1734
- },
1735
- moveToLineOrEdgeOfDocument: function(cm, _head, motionArgs) {
1736
- var lineNum = motionArgs.forward ? cm.lastLine() : cm.firstLine();
1737
- if (motionArgs.repeatIsExplicit) {
1738
- lineNum = motionArgs.repeat - cm.getOption('firstLineNumber');
1739
- }
1740
- return Pos(lineNum,
1741
- findFirstNonWhiteSpaceCharacter(cm.getLine(lineNum)));
1742
- },
1743
- textObjectManipulation: function(cm, head, motionArgs, vim) {
1744
- // TODO: lots of possible exceptions that can be thrown here. Try da(
1745
- // outside of a () block.
1746
-
1747
- // TODO: adding <> >< to this map doesn't work, presumably because
1748
- // they're operators
1749
- var mirroredPairs = {'(': ')', ')': '(',
1750
- '{': '}', '}': '{',
1751
- '[': ']', ']': '['};
1752
- var selfPaired = {'\'': true, '"': true};
1753
-
1754
- var character = motionArgs.selectedCharacter;
1755
- // 'b' refers to '()' block.
1756
- // 'B' refers to '{}' block.
1757
- if (character == 'b') {
1758
- character = '(';
1759
- } else if (character == 'B') {
1760
- character = '{';
1761
- }
1762
-
1763
- // Inclusive is the difference between a and i
1764
- // TODO: Instead of using the additional text object map to perform text
1765
- // object operations, merge the map into the defaultKeyMap and use
1766
- // motionArgs to define behavior. Define separate entries for 'aw',
1767
- // 'iw', 'a[', 'i[', etc.
1768
- var inclusive = !motionArgs.textObjectInner;
1769
-
1770
- var tmp;
1771
- if (mirroredPairs[character]) {
1772
- tmp = selectCompanionObject(cm, head, character, inclusive);
1773
- } else if (selfPaired[character]) {
1774
- tmp = findBeginningAndEnd(cm, head, character, inclusive);
1775
- } else if (character === 'W') {
1776
- tmp = expandWordUnderCursor(cm, inclusive, true /** forward */,
1777
- true /** bigWord */);
1778
- } else if (character === 'w') {
1779
- tmp = expandWordUnderCursor(cm, inclusive, true /** forward */,
1780
- false /** bigWord */);
1781
- } else if (character === 'p') {
1782
- tmp = findParagraph(cm, head, motionArgs.repeat, 0, inclusive);
1783
- motionArgs.linewise = true;
1784
- if (vim.visualMode) {
1785
- if (!vim.visualLine) { vim.visualLine = true; }
1786
- } else {
1787
- var operatorArgs = vim.inputState.operatorArgs;
1788
- if (operatorArgs) { operatorArgs.linewise = true; }
1789
- tmp.end.line--;
1790
- }
1791
- } else {
1792
- // No text object defined for this, don't move.
1793
- return null;
1794
- }
1795
-
1796
- if (!cm.state.vim.visualMode) {
1797
- return [tmp.start, tmp.end];
1798
- } else {
1799
- return expandSelection(cm, tmp.start, tmp.end);
1800
- }
1801
- },
1802
-
1803
- repeatLastCharacterSearch: function(cm, head, motionArgs) {
1804
- var lastSearch = vimGlobalState.lastChararacterSearch;
1805
- var repeat = motionArgs.repeat;
1806
- var forward = motionArgs.forward === lastSearch.forward;
1807
- var increment = (lastSearch.increment ? 1 : 0) * (forward ? -1 : 1);
1808
- cm.moveH(-increment, 'char');
1809
- motionArgs.inclusive = forward ? true : false;
1810
- var curEnd = moveToCharacter(cm, repeat, forward, lastSearch.selectedCharacter);
1811
- if (!curEnd) {
1812
- cm.moveH(increment, 'char');
1813
- return head;
1814
- }
1815
- curEnd.ch += increment;
1816
- return curEnd;
1817
- }
1818
- };
1819
-
1820
- function fillArray(val, times) {
1821
- var arr = [];
1822
- for (var i = 0; i < times; i++) {
1823
- arr.push(val);
1824
- }
1825
- return arr;
1826
- }
1827
- /**
1828
- * An operator acts on a text selection. It receives the list of selections
1829
- * as input. The corresponding CodeMirror selection is guaranteed to
1830
- * match the input selection.
1831
- */
1832
- var operators = {
1833
- change: function(cm, args, ranges) {
1834
- var finalHead, text;
1835
- var vim = cm.state.vim;
1836
- vimGlobalState.macroModeState.lastInsertModeChanges.inVisualBlock = vim.visualBlock;
1837
- if (!vim.visualMode) {
1838
- var anchor = ranges[0].anchor,
1839
- head = ranges[0].head;
1840
- text = cm.getRange(anchor, head);
1841
- if (!isWhiteSpaceString(text)) {
1842
- // Exclude trailing whitespace if the range is not all whitespace.
1843
- var match = (/\s+$/).exec(text);
1844
- if (match) {
1845
- head = offsetCursor(head, 0, - match[0].length);
1846
- text = text.slice(0, - match[0].length);
1847
- }
1848
- }
1849
- var wasLastLine = head.line - 1 == cm.lastLine();
1850
- cm.replaceRange('', anchor, head);
1851
- if (args.linewise && !wasLastLine) {
1852
- // Push the next line back down, if there is a next line.
1853
- CodeMirror.commands.newlineAndIndent(cm);
1854
- // null ch so setCursor moves to end of line.
1855
- anchor.ch = null;
1856
- }
1857
- finalHead = anchor;
1858
- } else {
1859
- text = cm.getSelection();
1860
- var replacement = fillArray('', ranges.length);
1861
- cm.replaceSelections(replacement);
1862
- finalHead = cursorMin(ranges[0].head, ranges[0].anchor);
1863
- }
1864
- vimGlobalState.registerController.pushText(
1865
- args.registerName, 'change', text,
1866
- args.linewise, ranges.length > 1);
1867
- actions.enterInsertMode(cm, {head: finalHead}, cm.state.vim);
1868
- },
1869
- // delete is a javascript keyword.
1870
- 'delete': function(cm, args, ranges) {
1871
- var finalHead, text;
1872
- var vim = cm.state.vim;
1873
- if (!vim.visualBlock) {
1874
- var anchor = ranges[0].anchor,
1875
- head = ranges[0].head;
1876
- if (args.linewise &&
1877
- head.line != cm.firstLine() &&
1878
- anchor.line == cm.lastLine() &&
1879
- anchor.line == head.line - 1) {
1880
- // Special case for dd on last line (and first line).
1881
- if (anchor.line == cm.firstLine()) {
1882
- anchor.ch = 0;
1883
- } else {
1884
- anchor = Pos(anchor.line - 1, lineLength(cm, anchor.line - 1));
1885
- }
1886
- }
1887
- text = cm.getRange(anchor, head);
1888
- cm.replaceRange('', anchor, head);
1889
- finalHead = anchor;
1890
- if (args.linewise) {
1891
- finalHead = motions.moveToFirstNonWhiteSpaceCharacter(cm, anchor);
1892
- }
1893
- } else {
1894
- text = cm.getSelection();
1895
- var replacement = fillArray('', ranges.length);
1896
- cm.replaceSelections(replacement);
1897
- finalHead = ranges[0].anchor;
1898
- }
1899
- vimGlobalState.registerController.pushText(
1900
- args.registerName, 'delete', text,
1901
- args.linewise, vim.visualBlock);
1902
- return finalHead;
1903
- },
1904
- indent: function(cm, args, ranges) {
1905
- var vim = cm.state.vim;
1906
- var startLine = ranges[0].anchor.line;
1907
- var endLine = vim.visualBlock ?
1908
- ranges[ranges.length - 1].anchor.line :
1909
- ranges[0].head.line;
1910
- // In visual mode, n> shifts the selection right n times, instead of
1911
- // shifting n lines right once.
1912
- var repeat = (vim.visualMode) ? args.repeat : 1;
1913
- if (args.linewise) {
1914
- // The only way to delete a newline is to delete until the start of
1915
- // the next line, so in linewise mode evalInput will include the next
1916
- // line. We don't want this in indent, so we go back a line.
1917
- endLine--;
1918
- }
1919
- for (var i = startLine; i <= endLine; i++) {
1920
- for (var j = 0; j < repeat; j++) {
1921
- cm.indentLine(i, args.indentRight);
1922
- }
1923
- }
1924
- return motions.moveToFirstNonWhiteSpaceCharacter(cm, ranges[0].anchor);
1925
- },
1926
- changeCase: function(cm, args, ranges, oldAnchor, newHead) {
1927
- var selections = cm.getSelections();
1928
- var swapped = [];
1929
- var toLower = args.toLower;
1930
- for (var j = 0; j < selections.length; j++) {
1931
- var toSwap = selections[j];
1932
- var text = '';
1933
- if (toLower === true) {
1934
- text = toSwap.toLowerCase();
1935
- } else if (toLower === false) {
1936
- text = toSwap.toUpperCase();
1937
- } else {
1938
- for (var i = 0; i < toSwap.length; i++) {
1939
- var character = toSwap.charAt(i);
1940
- text += isUpperCase(character) ? character.toLowerCase() :
1941
- character.toUpperCase();
1942
- }
1943
- }
1944
- swapped.push(text);
1945
- }
1946
- cm.replaceSelections(swapped);
1947
- if (args.shouldMoveCursor){
1948
- return newHead;
1949
- } else if (!cm.state.vim.visualMode && args.linewise && ranges[0].anchor.line + 1 == ranges[0].head.line) {
1950
- return motions.moveToFirstNonWhiteSpaceCharacter(cm, oldAnchor);
1951
- } else if (args.linewise){
1952
- return oldAnchor;
1953
- } else {
1954
- return cursorMin(ranges[0].anchor, ranges[0].head);
1955
- }
1956
- },
1957
- yank: function(cm, args, ranges, oldAnchor) {
1958
- var vim = cm.state.vim;
1959
- var text = cm.getSelection();
1960
- var endPos = vim.visualMode
1961
- ? cursorMin(vim.sel.anchor, vim.sel.head, ranges[0].head, ranges[0].anchor)
1962
- : oldAnchor;
1963
- vimGlobalState.registerController.pushText(
1964
- args.registerName, 'yank',
1965
- text, args.linewise, vim.visualBlock);
1966
- return endPos;
1967
- }
1968
- };
1969
-
1970
- var actions = {
1971
- jumpListWalk: function(cm, actionArgs, vim) {
1972
- if (vim.visualMode) {
1973
- return;
1974
- }
1975
- var repeat = actionArgs.repeat;
1976
- var forward = actionArgs.forward;
1977
- var jumpList = vimGlobalState.jumpList;
1978
-
1979
- var mark = jumpList.move(cm, forward ? repeat : -repeat);
1980
- var markPos = mark ? mark.find() : undefined;
1981
- markPos = markPos ? markPos : cm.getCursor();
1982
- cm.setCursor(markPos);
1983
- },
1984
- scroll: function(cm, actionArgs, vim) {
1985
- if (vim.visualMode) {
1986
- return;
1987
- }
1988
- var repeat = actionArgs.repeat || 1;
1989
- var lineHeight = cm.defaultTextHeight();
1990
- var top = cm.getScrollInfo().top;
1991
- var delta = lineHeight * repeat;
1992
- var newPos = actionArgs.forward ? top + delta : top - delta;
1993
- var cursor = copyCursor(cm.getCursor());
1994
- var cursorCoords = cm.charCoords(cursor, 'local');
1995
- if (actionArgs.forward) {
1996
- if (newPos > cursorCoords.top) {
1997
- cursor.line += (newPos - cursorCoords.top) / lineHeight;
1998
- cursor.line = Math.ceil(cursor.line);
1999
- cm.setCursor(cursor);
2000
- cursorCoords = cm.charCoords(cursor, 'local');
2001
- cm.scrollTo(null, cursorCoords.top);
2002
- } else {
2003
- // Cursor stays within bounds. Just reposition the scroll window.
2004
- cm.scrollTo(null, newPos);
2005
- }
2006
- } else {
2007
- var newBottom = newPos + cm.getScrollInfo().clientHeight;
2008
- if (newBottom < cursorCoords.bottom) {
2009
- cursor.line -= (cursorCoords.bottom - newBottom) / lineHeight;
2010
- cursor.line = Math.floor(cursor.line);
2011
- cm.setCursor(cursor);
2012
- cursorCoords = cm.charCoords(cursor, 'local');
2013
- cm.scrollTo(
2014
- null, cursorCoords.bottom - cm.getScrollInfo().clientHeight);
2015
- } else {
2016
- // Cursor stays within bounds. Just reposition the scroll window.
2017
- cm.scrollTo(null, newPos);
2018
- }
2019
- }
2020
- },
2021
- scrollToCursor: function(cm, actionArgs) {
2022
- var lineNum = cm.getCursor().line;
2023
- var charCoords = cm.charCoords(Pos(lineNum, 0), 'local');
2024
- var height = cm.getScrollInfo().clientHeight;
2025
- var y = charCoords.top;
2026
- var lineHeight = charCoords.bottom - y;
2027
- switch (actionArgs.position) {
2028
- case 'center': y = y - (height / 2) + lineHeight;
2029
- break;
2030
- case 'bottom': y = y - height + lineHeight*1.4;
2031
- break;
2032
- case 'top': y = y + lineHeight*0.4;
2033
- break;
2034
- }
2035
- cm.scrollTo(null, y);
2036
- },
2037
- replayMacro: function(cm, actionArgs, vim) {
2038
- var registerName = actionArgs.selectedCharacter;
2039
- var repeat = actionArgs.repeat;
2040
- var macroModeState = vimGlobalState.macroModeState;
2041
- if (registerName == '@') {
2042
- registerName = macroModeState.latestRegister;
2043
- }
2044
- while(repeat--){
2045
- executeMacroRegister(cm, vim, macroModeState, registerName);
2046
- }
2047
- },
2048
- enterMacroRecordMode: function(cm, actionArgs) {
2049
- var macroModeState = vimGlobalState.macroModeState;
2050
- var registerName = actionArgs.selectedCharacter;
2051
- macroModeState.enterMacroRecordMode(cm, registerName);
2052
- },
2053
- enterInsertMode: function(cm, actionArgs, vim) {
2054
- if (cm.getOption('readOnly')) { return; }
2055
- vim.insertMode = true;
2056
- vim.insertModeRepeat = actionArgs && actionArgs.repeat || 1;
2057
- var insertAt = (actionArgs) ? actionArgs.insertAt : null;
2058
- var sel = vim.sel;
2059
- var head = actionArgs.head || cm.getCursor('head');
2060
- var height = cm.listSelections().length;
2061
- if (insertAt == 'eol') {
2062
- head = Pos(head.line, lineLength(cm, head.line));
2063
- } else if (insertAt == 'charAfter') {
2064
- head = offsetCursor(head, 0, 1);
2065
- } else if (insertAt == 'firstNonBlank') {
2066
- head = motions.moveToFirstNonWhiteSpaceCharacter(cm, head);
2067
- } else if (insertAt == 'startOfSelectedArea') {
2068
- if (!vim.visualBlock) {
2069
- if (sel.head.line < sel.anchor.line) {
2070
- head = sel.head;
2071
- } else {
2072
- head = Pos(sel.anchor.line, 0);
2073
- }
2074
- } else {
2075
- head = Pos(
2076
- Math.min(sel.head.line, sel.anchor.line),
2077
- Math.min(sel.head.ch, sel.anchor.ch));
2078
- height = Math.abs(sel.head.line - sel.anchor.line) + 1;
2079
- }
2080
- } else if (insertAt == 'endOfSelectedArea') {
2081
- if (!vim.visualBlock) {
2082
- if (sel.head.line >= sel.anchor.line) {
2083
- head = offsetCursor(sel.head, 0, 1);
2084
- } else {
2085
- head = Pos(sel.anchor.line, 0);
2086
- }
2087
- } else {
2088
- head = Pos(
2089
- Math.min(sel.head.line, sel.anchor.line),
2090
- Math.max(sel.head.ch + 1, sel.anchor.ch));
2091
- height = Math.abs(sel.head.line - sel.anchor.line) + 1;
2092
- }
2093
- } else if (insertAt == 'inplace') {
2094
- if (vim.visualMode){
2095
- return;
2096
- }
2097
- }
2098
- cm.setOption('keyMap', 'vim-insert');
2099
- cm.setOption('disableInput', false);
2100
- if (actionArgs && actionArgs.replace) {
2101
- // Handle Replace-mode as a special case of insert mode.
2102
- cm.toggleOverwrite(true);
2103
- cm.setOption('keyMap', 'vim-replace');
2104
- CodeMirror.signal(cm, "vim-mode-change", {mode: "replace"});
2105
- } else {
2106
- cm.setOption('keyMap', 'vim-insert');
2107
- CodeMirror.signal(cm, "vim-mode-change", {mode: "insert"});
2108
- }
2109
- if (!vimGlobalState.macroModeState.isPlaying) {
2110
- // Only record if not replaying.
2111
- cm.on('change', onChange);
2112
- CodeMirror.on(cm.getInputField(), 'keydown', onKeyEventTargetKeyDown);
2113
- }
2114
- if (vim.visualMode) {
2115
- exitVisualMode(cm);
2116
- }
2117
- selectForInsert(cm, head, height);
2118
- },
2119
- toggleVisualMode: function(cm, actionArgs, vim) {
2120
- var repeat = actionArgs.repeat;
2121
- var anchor = cm.getCursor();
2122
- var head;
2123
- // TODO: The repeat should actually select number of characters/lines
2124
- // equal to the repeat times the size of the previous visual
2125
- // operation.
2126
- if (!vim.visualMode) {
2127
- // Entering visual mode
2128
- vim.visualMode = true;
2129
- vim.visualLine = !!actionArgs.linewise;
2130
- vim.visualBlock = !!actionArgs.blockwise;
2131
- head = clipCursorToContent(
2132
- cm, Pos(anchor.line, anchor.ch + repeat - 1),
2133
- true /** includeLineBreak */);
2134
- vim.sel = {
2135
- anchor: anchor,
2136
- head: head
2137
- };
2138
- CodeMirror.signal(cm, "vim-mode-change", {mode: "visual", subMode: vim.visualLine ? "linewise" : vim.visualBlock ? "blockwise" : ""});
2139
- updateCmSelection(cm);
2140
- updateMark(cm, vim, '<', cursorMin(anchor, head));
2141
- updateMark(cm, vim, '>', cursorMax(anchor, head));
2142
- } else if (vim.visualLine ^ actionArgs.linewise ||
2143
- vim.visualBlock ^ actionArgs.blockwise) {
2144
- // Toggling between modes
2145
- vim.visualLine = !!actionArgs.linewise;
2146
- vim.visualBlock = !!actionArgs.blockwise;
2147
- CodeMirror.signal(cm, "vim-mode-change", {mode: "visual", subMode: vim.visualLine ? "linewise" : vim.visualBlock ? "blockwise" : ""});
2148
- updateCmSelection(cm);
2149
- } else {
2150
- exitVisualMode(cm);
2151
- }
2152
- },
2153
- reselectLastSelection: function(cm, _actionArgs, vim) {
2154
- var lastSelection = vim.lastSelection;
2155
- if (vim.visualMode) {
2156
- updateLastSelection(cm, vim);
2157
- }
2158
- if (lastSelection) {
2159
- var anchor = lastSelection.anchorMark.find();
2160
- var head = lastSelection.headMark.find();
2161
- if (!anchor || !head) {
2162
- // If the marks have been destroyed due to edits, do nothing.
2163
- return;
2164
- }
2165
- vim.sel = {
2166
- anchor: anchor,
2167
- head: head
2168
- };
2169
- vim.visualMode = true;
2170
- vim.visualLine = lastSelection.visualLine;
2171
- vim.visualBlock = lastSelection.visualBlock;
2172
- updateCmSelection(cm);
2173
- updateMark(cm, vim, '<', cursorMin(anchor, head));
2174
- updateMark(cm, vim, '>', cursorMax(anchor, head));
2175
- CodeMirror.signal(cm, 'vim-mode-change', {
2176
- mode: 'visual',
2177
- subMode: vim.visualLine ? 'linewise' :
2178
- vim.visualBlock ? 'blockwise' : ''});
2179
- }
2180
- },
2181
- joinLines: function(cm, actionArgs, vim) {
2182
- var curStart, curEnd;
2183
- if (vim.visualMode) {
2184
- curStart = cm.getCursor('anchor');
2185
- curEnd = cm.getCursor('head');
2186
- curEnd.ch = lineLength(cm, curEnd.line) - 1;
2187
- } else {
2188
- // Repeat is the number of lines to join. Minimum 2 lines.
2189
- var repeat = Math.max(actionArgs.repeat, 2);
2190
- curStart = cm.getCursor();
2191
- curEnd = clipCursorToContent(cm, Pos(curStart.line + repeat - 1,
2192
- Infinity));
2193
- }
2194
- var finalCh = 0;
2195
- for (var i = curStart.line; i < curEnd.line; i++) {
2196
- finalCh = lineLength(cm, curStart.line);
2197
- var tmp = Pos(curStart.line + 1,
2198
- lineLength(cm, curStart.line + 1));
2199
- var text = cm.getRange(curStart, tmp);
2200
- text = text.replace(/\n\s*/g, ' ');
2201
- cm.replaceRange(text, curStart, tmp);
2202
- }
2203
- var curFinalPos = Pos(curStart.line, finalCh);
2204
- cm.setCursor(curFinalPos);
2205
- if (vim.visualMode) {
2206
- exitVisualMode(cm);
2207
- }
2208
- },
2209
- newLineAndEnterInsertMode: function(cm, actionArgs, vim) {
2210
- vim.insertMode = true;
2211
- var insertAt = copyCursor(cm.getCursor());
2212
- if (insertAt.line === cm.firstLine() && !actionArgs.after) {
2213
- // Special case for inserting newline before start of document.
2214
- cm.replaceRange('\n', Pos(cm.firstLine(), 0));
2215
- cm.setCursor(cm.firstLine(), 0);
2216
- } else {
2217
- insertAt.line = (actionArgs.after) ? insertAt.line :
2218
- insertAt.line - 1;
2219
- insertAt.ch = lineLength(cm, insertAt.line);
2220
- cm.setCursor(insertAt);
2221
- var newlineFn = CodeMirror.commands.newlineAndIndentContinueComment ||
2222
- CodeMirror.commands.newlineAndIndent;
2223
- newlineFn(cm);
2224
- }
2225
- this.enterInsertMode(cm, { repeat: actionArgs.repeat }, vim);
2226
- },
2227
- paste: function(cm, actionArgs, vim) {
2228
- var cur = copyCursor(cm.getCursor());
2229
- var register = vimGlobalState.registerController.getRegister(
2230
- actionArgs.registerName);
2231
- var text = register.toString();
2232
- if (!text) {
2233
- return;
2234
- }
2235
- if (actionArgs.matchIndent) {
2236
- var tabSize = cm.getOption("tabSize");
2237
- // length that considers tabs and tabSize
2238
- var whitespaceLength = function(str) {
2239
- var tabs = (str.split("\t").length - 1);
2240
- var spaces = (str.split(" ").length - 1);
2241
- return tabs * tabSize + spaces * 1;
2242
- };
2243
- var currentLine = cm.getLine(cm.getCursor().line);
2244
- var indent = whitespaceLength(currentLine.match(/^\s*/)[0]);
2245
- // chomp last newline b/c don't want it to match /^\s*/gm
2246
- var chompedText = text.replace(/\n$/, '');
2247
- var wasChomped = text !== chompedText;
2248
- var firstIndent = whitespaceLength(text.match(/^\s*/)[0]);
2249
- var text = chompedText.replace(/^\s*/gm, function(wspace) {
2250
- var newIndent = indent + (whitespaceLength(wspace) - firstIndent);
2251
- if (newIndent < 0) {
2252
- return "";
2253
- }
2254
- else if (cm.getOption("indentWithTabs")) {
2255
- var quotient = Math.floor(newIndent / tabSize);
2256
- return Array(quotient + 1).join('\t');
2257
- }
2258
- else {
2259
- return Array(newIndent + 1).join(' ');
2260
- }
2261
- });
2262
- text += wasChomped ? "\n" : "";
2263
- }
2264
- if (actionArgs.repeat > 1) {
2265
- var text = Array(actionArgs.repeat + 1).join(text);
2266
- }
2267
- var linewise = register.linewise;
2268
- var blockwise = register.blockwise;
2269
- if (linewise) {
2270
- if(vim.visualMode) {
2271
- text = vim.visualLine ? text.slice(0, -1) : '\n' + text.slice(0, text.length - 1) + '\n';
2272
- } else if (actionArgs.after) {
2273
- // Move the newline at the end to the start instead, and paste just
2274
- // before the newline character of the line we are on right now.
2275
- text = '\n' + text.slice(0, text.length - 1);
2276
- cur.ch = lineLength(cm, cur.line);
2277
- } else {
2278
- cur.ch = 0;
2279
- }
2280
- } else {
2281
- if (blockwise) {
2282
- text = text.split('\n');
2283
- for (var i = 0; i < text.length; i++) {
2284
- text[i] = (text[i] == '') ? ' ' : text[i];
2285
- }
2286
- }
2287
- cur.ch += actionArgs.after ? 1 : 0;
2288
- }
2289
- var curPosFinal;
2290
- var idx;
2291
- if (vim.visualMode) {
2292
- // save the pasted text for reselection if the need arises
2293
- vim.lastPastedText = text;
2294
- var lastSelectionCurEnd;
2295
- var selectedArea = getSelectedAreaRange(cm, vim);
2296
- var selectionStart = selectedArea[0];
2297
- var selectionEnd = selectedArea[1];
2298
- var selectedText = cm.getSelection();
2299
- var selections = cm.listSelections();
2300
- var emptyStrings = new Array(selections.length).join('1').split('1');
2301
- // save the curEnd marker before it get cleared due to cm.replaceRange.
2302
- if (vim.lastSelection) {
2303
- lastSelectionCurEnd = vim.lastSelection.headMark.find();
2304
- }
2305
- // push the previously selected text to unnamed register
2306
- vimGlobalState.registerController.unnamedRegister.setText(selectedText);
2307
- if (blockwise) {
2308
- // first delete the selected text
2309
- cm.replaceSelections(emptyStrings);
2310
- // Set new selections as per the block length of the yanked text
2311
- selectionEnd = Pos(selectionStart.line + text.length-1, selectionStart.ch);
2312
- cm.setCursor(selectionStart);
2313
- selectBlock(cm, selectionEnd);
2314
- cm.replaceSelections(text);
2315
- curPosFinal = selectionStart;
2316
- } else if (vim.visualBlock) {
2317
- cm.replaceSelections(emptyStrings);
2318
- cm.setCursor(selectionStart);
2319
- cm.replaceRange(text, selectionStart, selectionStart);
2320
- curPosFinal = selectionStart;
2321
- } else {
2322
- cm.replaceRange(text, selectionStart, selectionEnd);
2323
- curPosFinal = cm.posFromIndex(cm.indexFromPos(selectionStart) + text.length - 1);
2324
- }
2325
- // restore the the curEnd marker
2326
- if(lastSelectionCurEnd) {
2327
- vim.lastSelection.headMark = cm.setBookmark(lastSelectionCurEnd);
2328
- }
2329
- if (linewise) {
2330
- curPosFinal.ch=0;
2331
- }
2332
- } else {
2333
- if (blockwise) {
2334
- cm.setCursor(cur);
2335
- for (var i = 0; i < text.length; i++) {
2336
- var line = cur.line+i;
2337
- if (line > cm.lastLine()) {
2338
- cm.replaceRange('\n', Pos(line, 0));
2339
- }
2340
- var lastCh = lineLength(cm, line);
2341
- if (lastCh < cur.ch) {
2342
- extendLineToColumn(cm, line, cur.ch);
2343
- }
2344
- }
2345
- cm.setCursor(cur);
2346
- selectBlock(cm, Pos(cur.line + text.length-1, cur.ch));
2347
- cm.replaceSelections(text);
2348
- curPosFinal = cur;
2349
- } else {
2350
- cm.replaceRange(text, cur);
2351
- // Now fine tune the cursor to where we want it.
2352
- if (linewise && actionArgs.after) {
2353
- curPosFinal = Pos(
2354
- cur.line + 1,
2355
- findFirstNonWhiteSpaceCharacter(cm.getLine(cur.line + 1)));
2356
- } else if (linewise && !actionArgs.after) {
2357
- curPosFinal = Pos(
2358
- cur.line,
2359
- findFirstNonWhiteSpaceCharacter(cm.getLine(cur.line)));
2360
- } else if (!linewise && actionArgs.after) {
2361
- idx = cm.indexFromPos(cur);
2362
- curPosFinal = cm.posFromIndex(idx + text.length - 1);
2363
- } else {
2364
- idx = cm.indexFromPos(cur);
2365
- curPosFinal = cm.posFromIndex(idx + text.length);
2366
- }
2367
- }
2368
- }
2369
- if (vim.visualMode) {
2370
- exitVisualMode(cm);
2371
- }
2372
- cm.setCursor(curPosFinal);
2373
- },
2374
- undo: function(cm, actionArgs) {
2375
- cm.operation(function() {
2376
- repeatFn(cm, CodeMirror.commands.undo, actionArgs.repeat)();
2377
- cm.setCursor(cm.getCursor('anchor'));
2378
- });
2379
- },
2380
- redo: function(cm, actionArgs) {
2381
- repeatFn(cm, CodeMirror.commands.redo, actionArgs.repeat)();
2382
- },
2383
- setRegister: function(_cm, actionArgs, vim) {
2384
- vim.inputState.registerName = actionArgs.selectedCharacter;
2385
- },
2386
- setMark: function(cm, actionArgs, vim) {
2387
- var markName = actionArgs.selectedCharacter;
2388
- updateMark(cm, vim, markName, cm.getCursor());
2389
- },
2390
- replace: function(cm, actionArgs, vim) {
2391
- var replaceWith = actionArgs.selectedCharacter;
2392
- var curStart = cm.getCursor();
2393
- var replaceTo;
2394
- var curEnd;
2395
- var selections = cm.listSelections();
2396
- if (vim.visualMode) {
2397
- curStart = cm.getCursor('start');
2398
- curEnd = cm.getCursor('end');
2399
- } else {
2400
- var line = cm.getLine(curStart.line);
2401
- replaceTo = curStart.ch + actionArgs.repeat;
2402
- if (replaceTo > line.length) {
2403
- replaceTo=line.length;
2404
- }
2405
- curEnd = Pos(curStart.line, replaceTo);
2406
- }
2407
- if (replaceWith=='\n') {
2408
- if (!vim.visualMode) cm.replaceRange('', curStart, curEnd);
2409
- // special case, where vim help says to replace by just one line-break
2410
- (CodeMirror.commands.newlineAndIndentContinueComment || CodeMirror.commands.newlineAndIndent)(cm);
2411
- } else {
2412
- var replaceWithStr = cm.getRange(curStart, curEnd);
2413
- //replace all characters in range by selected, but keep linebreaks
2414
- replaceWithStr = replaceWithStr.replace(/[^\n]/g, replaceWith);
2415
- if (vim.visualBlock) {
2416
- // Tabs are split in visua block before replacing
2417
- var spaces = new Array(cm.getOption("tabSize")+1).join(' ');
2418
- replaceWithStr = cm.getSelection();
2419
- replaceWithStr = replaceWithStr.replace(/\t/g, spaces).replace(/[^\n]/g, replaceWith).split('\n');
2420
- cm.replaceSelections(replaceWithStr);
2421
- } else {
2422
- cm.replaceRange(replaceWithStr, curStart, curEnd);
2423
- }
2424
- if (vim.visualMode) {
2425
- curStart = cursorIsBefore(selections[0].anchor, selections[0].head) ?
2426
- selections[0].anchor : selections[0].head;
2427
- cm.setCursor(curStart);
2428
- exitVisualMode(cm);
2429
- } else {
2430
- cm.setCursor(offsetCursor(curEnd, 0, -1));
2431
- }
2432
- }
2433
- },
2434
- incrementNumberToken: function(cm, actionArgs) {
2435
- var cur = cm.getCursor();
2436
- var lineStr = cm.getLine(cur.line);
2437
- var re = /-?\d+/g;
2438
- var match;
2439
- var start;
2440
- var end;
2441
- var numberStr;
2442
- var token;
2443
- while ((match = re.exec(lineStr)) !== null) {
2444
- token = match[0];
2445
- start = match.index;
2446
- end = start + token.length;
2447
- if (cur.ch < end)break;
2448
- }
2449
- if (!actionArgs.backtrack && (end <= cur.ch))return;
2450
- if (token) {
2451
- var increment = actionArgs.increase ? 1 : -1;
2452
- var number = parseInt(token) + (increment * actionArgs.repeat);
2453
- var from = Pos(cur.line, start);
2454
- var to = Pos(cur.line, end);
2455
- numberStr = number.toString();
2456
- cm.replaceRange(numberStr, from, to);
2457
- } else {
2458
- return;
2459
- }
2460
- cm.setCursor(Pos(cur.line, start + numberStr.length - 1));
2461
- },
2462
- repeatLastEdit: function(cm, actionArgs, vim) {
2463
- var lastEditInputState = vim.lastEditInputState;
2464
- if (!lastEditInputState) { return; }
2465
- var repeat = actionArgs.repeat;
2466
- if (repeat && actionArgs.repeatIsExplicit) {
2467
- vim.lastEditInputState.repeatOverride = repeat;
2468
- } else {
2469
- repeat = vim.lastEditInputState.repeatOverride || repeat;
2470
- }
2471
- repeatLastEdit(cm, vim, repeat, false /** repeatForInsert */);
2472
- },
2473
- exitInsertMode: exitInsertMode
2474
- };
2475
-
2476
- /*
2477
- * Below are miscellaneous utility functions used by vim.js
2478
- */
2479
-
2480
- /**
2481
- * Clips cursor to ensure that line is within the buffer's range
2482
- * If includeLineBreak is true, then allow cur.ch == lineLength.
2483
- */
2484
- function clipCursorToContent(cm, cur, includeLineBreak) {
2485
- var line = Math.min(Math.max(cm.firstLine(), cur.line), cm.lastLine() );
2486
- var maxCh = lineLength(cm, line) - 1;
2487
- maxCh = (includeLineBreak) ? maxCh + 1 : maxCh;
2488
- var ch = Math.min(Math.max(0, cur.ch), maxCh);
2489
- return Pos(line, ch);
2490
- }
2491
- function copyArgs(args) {
2492
- var ret = {};
2493
- for (var prop in args) {
2494
- if (args.hasOwnProperty(prop)) {
2495
- ret[prop] = args[prop];
2496
- }
2497
- }
2498
- return ret;
2499
- }
2500
- function offsetCursor(cur, offsetLine, offsetCh) {
2501
- if (typeof offsetLine === 'object') {
2502
- offsetCh = offsetLine.ch;
2503
- offsetLine = offsetLine.line;
2504
- }
2505
- return Pos(cur.line + offsetLine, cur.ch + offsetCh);
2506
- }
2507
- function getOffset(anchor, head) {
2508
- return {
2509
- line: head.line - anchor.line,
2510
- ch: head.line - anchor.line
2511
- };
2512
- }
2513
- function commandMatches(keys, keyMap, context, inputState) {
2514
- // Partial matches are not applied. They inform the key handler
2515
- // that the current key sequence is a subsequence of a valid key
2516
- // sequence, so that the key buffer is not cleared.
2517
- var match, partial = [], full = [];
2518
- for (var i = 0; i < keyMap.length; i++) {
2519
- var command = keyMap[i];
2520
- if (context == 'insert' && command.context != 'insert' ||
2521
- command.context && command.context != context ||
2522
- inputState.operator && command.type == 'action' ||
2523
- !(match = commandMatch(keys, command.keys))) { continue; }
2524
- if (match == 'partial') { partial.push(command); }
2525
- if (match == 'full') { full.push(command); }
2526
- }
2527
- return {
2528
- partial: partial.length && partial,
2529
- full: full.length && full
2530
- };
2531
- }
2532
- function commandMatch(pressed, mapped) {
2533
- if (mapped.slice(-11) == '<character>') {
2534
- // Last character matches anything.
2535
- var prefixLen = mapped.length - 11;
2536
- var pressedPrefix = pressed.slice(0, prefixLen);
2537
- var mappedPrefix = mapped.slice(0, prefixLen);
2538
- return pressedPrefix == mappedPrefix && pressed.length > prefixLen ? 'full' :
2539
- mappedPrefix.indexOf(pressedPrefix) == 0 ? 'partial' : false;
2540
- } else {
2541
- return pressed == mapped ? 'full' :
2542
- mapped.indexOf(pressed) == 0 ? 'partial' : false;
2543
- }
2544
- }
2545
- function lastChar(keys) {
2546
- var match = /^.*(<[\w\-]+>)$/.exec(keys);
2547
- var selectedCharacter = match ? match[1] : keys.slice(-1);
2548
- if (selectedCharacter.length > 1){
2549
- switch(selectedCharacter){
2550
- case '<CR>':
2551
- selectedCharacter='\n';
2552
- break;
2553
- case '<Space>':
2554
- selectedCharacter=' ';
2555
- break;
2556
- default:
2557
- break;
2558
- }
2559
- }
2560
- return selectedCharacter;
2561
- }
2562
- function repeatFn(cm, fn, repeat) {
2563
- return function() {
2564
- for (var i = 0; i < repeat; i++) {
2565
- fn(cm);
2566
- }
2567
- };
2568
- }
2569
- function copyCursor(cur) {
2570
- return Pos(cur.line, cur.ch);
2571
- }
2572
- function cursorEqual(cur1, cur2) {
2573
- return cur1.ch == cur2.ch && cur1.line == cur2.line;
2574
- }
2575
- function cursorIsBefore(cur1, cur2) {
2576
- if (cur1.line < cur2.line) {
2577
- return true;
2578
- }
2579
- if (cur1.line == cur2.line && cur1.ch < cur2.ch) {
2580
- return true;
2581
- }
2582
- return false;
2583
- }
2584
- function cursorMin(cur1, cur2) {
2585
- if (arguments.length > 2) {
2586
- cur2 = cursorMin.apply(undefined, Array.prototype.slice.call(arguments, 1));
2587
- }
2588
- return cursorIsBefore(cur1, cur2) ? cur1 : cur2;
2589
- }
2590
- function cursorMax(cur1, cur2) {
2591
- if (arguments.length > 2) {
2592
- cur2 = cursorMax.apply(undefined, Array.prototype.slice.call(arguments, 1));
2593
- }
2594
- return cursorIsBefore(cur1, cur2) ? cur2 : cur1;
2595
- }
2596
- function cursorIsBetween(cur1, cur2, cur3) {
2597
- // returns true if cur2 is between cur1 and cur3.
2598
- var cur1before2 = cursorIsBefore(cur1, cur2);
2599
- var cur2before3 = cursorIsBefore(cur2, cur3);
2600
- return cur1before2 && cur2before3;
2601
- }
2602
- function lineLength(cm, lineNum) {
2603
- return cm.getLine(lineNum).length;
2604
- }
2605
- function reverse(s){
2606
- return s.split('').reverse().join('');
2607
- }
2608
- function trim(s) {
2609
- if (s.trim) {
2610
- return s.trim();
2611
- }
2612
- return s.replace(/^\s+|\s+$/g, '');
2613
- }
2614
- function escapeRegex(s) {
2615
- return s.replace(/([.?*+$\[\]\/\\(){}|\-])/g, '\\$1');
2616
- }
2617
- function extendLineToColumn(cm, lineNum, column) {
2618
- var endCh = lineLength(cm, lineNum);
2619
- var spaces = new Array(column-endCh+1).join(' ');
2620
- cm.setCursor(Pos(lineNum, endCh));
2621
- cm.replaceRange(spaces, cm.getCursor());
2622
- }
2623
- // This functions selects a rectangular block
2624
- // of text with selectionEnd as any of its corner
2625
- // Height of block:
2626
- // Difference in selectionEnd.line and first/last selection.line
2627
- // Width of the block:
2628
- // Distance between selectionEnd.ch and any(first considered here) selection.ch
2629
- function selectBlock(cm, selectionEnd) {
2630
- var selections = [], ranges = cm.listSelections();
2631
- var head = copyCursor(cm.clipPos(selectionEnd));
2632
- var isClipped = !cursorEqual(selectionEnd, head);
2633
- var curHead = cm.getCursor('head');
2634
- var primIndex = getIndex(ranges, curHead);
2635
- var wasClipped = cursorEqual(ranges[primIndex].head, ranges[primIndex].anchor);
2636
- var max = ranges.length - 1;
2637
- var index = max - primIndex > primIndex ? max : 0;
2638
- var base = ranges[index].anchor;
2639
-
2640
- var firstLine = Math.min(base.line, head.line);
2641
- var lastLine = Math.max(base.line, head.line);
2642
- var baseCh = base.ch, headCh = head.ch;
2643
-
2644
- var dir = ranges[index].head.ch - baseCh;
2645
- var newDir = headCh - baseCh;
2646
- if (dir > 0 && newDir <= 0) {
2647
- baseCh++;
2648
- if (!isClipped) { headCh--; }
2649
- } else if (dir < 0 && newDir >= 0) {
2650
- baseCh--;
2651
- if (!wasClipped) { headCh++; }
2652
- } else if (dir < 0 && newDir == -1) {
2653
- baseCh--;
2654
- headCh++;
2655
- }
2656
- for (var line = firstLine; line <= lastLine; line++) {
2657
- var range = {anchor: new Pos(line, baseCh), head: new Pos(line, headCh)};
2658
- selections.push(range);
2659
- }
2660
- primIndex = head.line == lastLine ? selections.length - 1 : 0;
2661
- cm.setSelections(selections);
2662
- selectionEnd.ch = headCh;
2663
- base.ch = baseCh;
2664
- return base;
2665
- }
2666
- function selectForInsert(cm, head, height) {
2667
- var sel = [];
2668
- for (var i = 0; i < height; i++) {
2669
- var lineHead = offsetCursor(head, i, 0);
2670
- sel.push({anchor: lineHead, head: lineHead});
2671
- }
2672
- cm.setSelections(sel, 0);
2673
- }
2674
- // getIndex returns the index of the cursor in the selections.
2675
- function getIndex(ranges, cursor, end) {
2676
- for (var i = 0; i < ranges.length; i++) {
2677
- var atAnchor = end != 'head' && cursorEqual(ranges[i].anchor, cursor);
2678
- var atHead = end != 'anchor' && cursorEqual(ranges[i].head, cursor);
2679
- if (atAnchor || atHead) {
2680
- return i;
2681
- }
2682
- }
2683
- return -1;
2684
- }
2685
- function getSelectedAreaRange(cm, vim) {
2686
- var lastSelection = vim.lastSelection;
2687
- var getCurrentSelectedAreaRange = function() {
2688
- var selections = cm.listSelections();
2689
- var start = selections[0];
2690
- var end = selections[selections.length-1];
2691
- var selectionStart = cursorIsBefore(start.anchor, start.head) ? start.anchor : start.head;
2692
- var selectionEnd = cursorIsBefore(end.anchor, end.head) ? end.head : end.anchor;
2693
- return [selectionStart, selectionEnd];
2694
- };
2695
- var getLastSelectedAreaRange = function() {
2696
- var selectionStart = cm.getCursor();
2697
- var selectionEnd = cm.getCursor();
2698
- var block = lastSelection.visualBlock;
2699
- if (block) {
2700
- var width = block.width;
2701
- var height = block.height;
2702
- selectionEnd = Pos(selectionStart.line + height, selectionStart.ch + width);
2703
- var selections = [];
2704
- // selectBlock creates a 'proper' rectangular block.
2705
- // We do not want that in all cases, so we manually set selections.
2706
- for (var i = selectionStart.line; i < selectionEnd.line; i++) {
2707
- var anchor = Pos(i, selectionStart.ch);
2708
- var head = Pos(i, selectionEnd.ch);
2709
- var range = {anchor: anchor, head: head};
2710
- selections.push(range);
2711
- }
2712
- cm.setSelections(selections);
2713
- } else {
2714
- var start = lastSelection.anchorMark.find();
2715
- var end = lastSelection.headMark.find();
2716
- var line = end.line - start.line;
2717
- var ch = end.ch - start.ch;
2718
- selectionEnd = {line: selectionEnd.line + line, ch: line ? selectionEnd.ch : ch + selectionEnd.ch};
2719
- if (lastSelection.visualLine) {
2720
- selectionStart = Pos(selectionStart.line, 0);
2721
- selectionEnd = Pos(selectionEnd.line, lineLength(cm, selectionEnd.line));
2722
- }
2723
- cm.setSelection(selectionStart, selectionEnd);
2724
- }
2725
- return [selectionStart, selectionEnd];
2726
- };
2727
- if (!vim.visualMode) {
2728
- // In case of replaying the action.
2729
- return getLastSelectedAreaRange();
2730
- } else {
2731
- return getCurrentSelectedAreaRange();
2732
- }
2733
- }
2734
- // Updates the previous selection with the current selection's values. This
2735
- // should only be called in visual mode.
2736
- function updateLastSelection(cm, vim) {
2737
- var anchor = vim.sel.anchor;
2738
- var head = vim.sel.head;
2739
- // To accommodate the effect of lastPastedText in the last selection
2740
- if (vim.lastPastedText) {
2741
- head = cm.posFromIndex(cm.indexFromPos(anchor) + vim.lastPastedText.length);
2742
- vim.lastPastedText = null;
2743
- }
2744
- vim.lastSelection = {'anchorMark': cm.setBookmark(anchor),
2745
- 'headMark': cm.setBookmark(head),
2746
- 'anchor': copyCursor(anchor),
2747
- 'head': copyCursor(head),
2748
- 'visualMode': vim.visualMode,
2749
- 'visualLine': vim.visualLine,
2750
- 'visualBlock': vim.visualBlock};
2751
- }
2752
- function expandSelection(cm, start, end) {
2753
- var sel = cm.state.vim.sel;
2754
- var head = sel.head;
2755
- var anchor = sel.anchor;
2756
- var tmp;
2757
- if (cursorIsBefore(end, start)) {
2758
- tmp = end;
2759
- end = start;
2760
- start = tmp;
2761
- }
2762
- if (cursorIsBefore(head, anchor)) {
2763
- head = cursorMin(start, head);
2764
- anchor = cursorMax(anchor, end);
2765
- } else {
2766
- anchor = cursorMin(start, anchor);
2767
- head = cursorMax(head, end);
2768
- head = offsetCursor(head, 0, -1);
2769
- if (head.ch == -1 && head.line != cm.firstLine()) {
2770
- head = Pos(head.line - 1, lineLength(cm, head.line - 1));
2771
- }
2772
- }
2773
- return [anchor, head];
2774
- }
2775
- /**
2776
- * Updates the CodeMirror selection to match the provided vim selection.
2777
- * If no arguments are given, it uses the current vim selection state.
2778
- */
2779
- function updateCmSelection(cm, sel, mode) {
2780
- var vim = cm.state.vim;
2781
- sel = sel || vim.sel;
2782
- var mode = mode ||
2783
- vim.visualLine ? 'line' : vim.visualBlock ? 'block' : 'char';
2784
- var cmSel = makeCmSelection(cm, sel, mode);
2785
- cm.setSelections(cmSel.ranges, cmSel.primary);
2786
- updateFakeCursor(cm);
2787
- }
2788
- function makeCmSelection(cm, sel, mode, exclusive) {
2789
- var head = copyCursor(sel.head);
2790
- var anchor = copyCursor(sel.anchor);
2791
- if (mode == 'char') {
2792
- var headOffset = !exclusive && !cursorIsBefore(sel.head, sel.anchor) ? 1 : 0;
2793
- var anchorOffset = cursorIsBefore(sel.head, sel.anchor) ? 1 : 0;
2794
- head = offsetCursor(sel.head, 0, headOffset);
2795
- anchor = offsetCursor(sel.anchor, 0, anchorOffset);
2796
- return {
2797
- ranges: [{anchor: anchor, head: head}],
2798
- primary: 0
2799
- };
2800
- } else if (mode == 'line') {
2801
- if (!cursorIsBefore(sel.head, sel.anchor)) {
2802
- anchor.ch = 0;
2803
-
2804
- var lastLine = cm.lastLine();
2805
- if (head.line > lastLine) {
2806
- head.line = lastLine;
2807
- }
2808
- head.ch = lineLength(cm, head.line);
2809
- } else {
2810
- head.ch = 0;
2811
- anchor.ch = lineLength(cm, anchor.line);
2812
- }
2813
- return {
2814
- ranges: [{anchor: anchor, head: head}],
2815
- primary: 0
2816
- };
2817
- } else if (mode == 'block') {
2818
- var top = Math.min(anchor.line, head.line),
2819
- left = Math.min(anchor.ch, head.ch),
2820
- bottom = Math.max(anchor.line, head.line),
2821
- right = Math.max(anchor.ch, head.ch) + 1;
2822
- var height = bottom - top + 1;
2823
- var primary = head.line == top ? 0 : height - 1;
2824
- var ranges = [];
2825
- for (var i = 0; i < height; i++) {
2826
- ranges.push({
2827
- anchor: Pos(top + i, left),
2828
- head: Pos(top + i, right)
2829
- });
2830
- }
2831
- return {
2832
- ranges: ranges,
2833
- primary: primary
2834
- };
2835
- }
2836
- }
2837
- function getHead(cm) {
2838
- var cur = cm.getCursor('head');
2839
- if (cm.getSelection().length == 1) {
2840
- // Small corner case when only 1 character is selected. The "real"
2841
- // head is the left of head and anchor.
2842
- cur = cursorMin(cur, cm.getCursor('anchor'));
2843
- }
2844
- return cur;
2845
- }
2846
-
2847
- /**
2848
- * If moveHead is set to false, the CodeMirror selection will not be
2849
- * touched. The caller assumes the responsibility of putting the cursor
2850
- * in the right place.
2851
- */
2852
- function exitVisualMode(cm, moveHead) {
2853
- var vim = cm.state.vim;
2854
- if (moveHead !== false) {
2855
- cm.setCursor(clipCursorToContent(cm, vim.sel.head));
2856
- }
2857
- updateLastSelection(cm, vim);
2858
- vim.visualMode = false;
2859
- vim.visualLine = false;
2860
- vim.visualBlock = false;
2861
- CodeMirror.signal(cm, "vim-mode-change", {mode: "normal"});
2862
- if (vim.fakeCursor) {
2863
- vim.fakeCursor.clear();
2864
- }
2865
- }
2866
-
2867
- // Remove any trailing newlines from the selection. For
2868
- // example, with the caret at the start of the last word on the line,
2869
- // 'dw' should word, but not the newline, while 'w' should advance the
2870
- // caret to the first character of the next line.
2871
- function clipToLine(cm, curStart, curEnd) {
2872
- var selection = cm.getRange(curStart, curEnd);
2873
- // Only clip if the selection ends with trailing newline + whitespace
2874
- if (/\n\s*$/.test(selection)) {
2875
- var lines = selection.split('\n');
2876
- // We know this is all whitepsace.
2877
- lines.pop();
2878
-
2879
- // Cases:
2880
- // 1. Last word is an empty line - do not clip the trailing '\n'
2881
- // 2. Last word is not an empty line - clip the trailing '\n'
2882
- var line;
2883
- // Find the line containing the last word, and clip all whitespace up
2884
- // to it.
2885
- for (var line = lines.pop(); lines.length > 0 && line && isWhiteSpaceString(line); line = lines.pop()) {
2886
- curEnd.line--;
2887
- curEnd.ch = 0;
2888
- }
2889
- // If the last word is not an empty line, clip an additional newline
2890
- if (line) {
2891
- curEnd.line--;
2892
- curEnd.ch = lineLength(cm, curEnd.line);
2893
- } else {
2894
- curEnd.ch = 0;
2895
- }
2896
- }
2897
- }
2898
-
2899
- // Expand the selection to line ends.
2900
- function expandSelectionToLine(_cm, curStart, curEnd) {
2901
- curStart.ch = 0;
2902
- curEnd.ch = 0;
2903
- curEnd.line++;
2904
- }
2905
-
2906
- function findFirstNonWhiteSpaceCharacter(text) {
2907
- if (!text) {
2908
- return 0;
2909
- }
2910
- var firstNonWS = text.search(/\S/);
2911
- return firstNonWS == -1 ? text.length : firstNonWS;
2912
- }
2913
-
2914
- function expandWordUnderCursor(cm, inclusive, _forward, bigWord, noSymbol) {
2915
- var cur = getHead(cm);
2916
- var line = cm.getLine(cur.line);
2917
- var idx = cur.ch;
2918
-
2919
- // Seek to first word or non-whitespace character, depending on if
2920
- // noSymbol is true.
2921
- var textAfterIdx = line.substring(idx);
2922
- var firstMatchedChar;
2923
- if (noSymbol) {
2924
- firstMatchedChar = textAfterIdx.search(/\w/);
2925
- } else {
2926
- firstMatchedChar = textAfterIdx.search(/\S/);
2927
- }
2928
- if (firstMatchedChar == -1) {
2929
- return null;
2930
- }
2931
- idx += firstMatchedChar;
2932
- textAfterIdx = line.substring(idx);
2933
- var textBeforeIdx = line.substring(0, idx);
2934
-
2935
- var matchRegex;
2936
- // Greedy matchers for the "word" we are trying to expand.
2937
- if (bigWord) {
2938
- matchRegex = /^\S+/;
2939
- } else {
2940
- if ((/\w/).test(line.charAt(idx))) {
2941
- matchRegex = /^\w+/;
2942
- } else {
2943
- matchRegex = /^[^\w\s]+/;
2944
- }
2945
- }
2946
-
2947
- var wordAfterRegex = matchRegex.exec(textAfterIdx);
2948
- var wordStart = idx;
2949
- var wordEnd = idx + wordAfterRegex[0].length;
2950
- // TODO: Find a better way to do this. It will be slow on very long lines.
2951
- var revTextBeforeIdx = reverse(textBeforeIdx);
2952
- var wordBeforeRegex = matchRegex.exec(revTextBeforeIdx);
2953
- if (wordBeforeRegex) {
2954
- wordStart -= wordBeforeRegex[0].length;
2955
- }
2956
-
2957
- if (inclusive) {
2958
- // If present, trim all whitespace after word.
2959
- // Otherwise, trim all whitespace before word.
2960
- var textAfterWordEnd = line.substring(wordEnd);
2961
- var whitespacesAfterWord = textAfterWordEnd.match(/^\s*/)[0].length;
2962
- if (whitespacesAfterWord > 0) {
2963
- wordEnd += whitespacesAfterWord;
2964
- } else {
2965
- var revTrim = revTextBeforeIdx.length - wordStart;
2966
- var textBeforeWordStart = revTextBeforeIdx.substring(revTrim);
2967
- var whitespacesBeforeWord = textBeforeWordStart.match(/^\s*/)[0].length;
2968
- wordStart -= whitespacesBeforeWord;
2969
- }
2970
- }
2971
-
2972
- return { start: Pos(cur.line, wordStart),
2973
- end: Pos(cur.line, wordEnd) };
2974
- }
2975
-
2976
- function recordJumpPosition(cm, oldCur, newCur) {
2977
- if (!cursorEqual(oldCur, newCur)) {
2978
- vimGlobalState.jumpList.add(cm, oldCur, newCur);
2979
- }
2980
- }
2981
-
2982
- function recordLastCharacterSearch(increment, args) {
2983
- vimGlobalState.lastChararacterSearch.increment = increment;
2984
- vimGlobalState.lastChararacterSearch.forward = args.forward;
2985
- vimGlobalState.lastChararacterSearch.selectedCharacter = args.selectedCharacter;
2986
- }
2987
-
2988
- var symbolToMode = {
2989
- '(': 'bracket', ')': 'bracket', '{': 'bracket', '}': 'bracket',
2990
- '[': 'section', ']': 'section',
2991
- '*': 'comment', '/': 'comment',
2992
- 'm': 'method', 'M': 'method',
2993
- '#': 'preprocess'
2994
- };
2995
- var findSymbolModes = {
2996
- bracket: {
2997
- isComplete: function(state) {
2998
- if (state.nextCh === state.symb) {
2999
- state.depth++;
3000
- if (state.depth >= 1)return true;
3001
- } else if (state.nextCh === state.reverseSymb) {
3002
- state.depth--;
3003
- }
3004
- return false;
3005
- }
3006
- },
3007
- section: {
3008
- init: function(state) {
3009
- state.curMoveThrough = true;
3010
- state.symb = (state.forward ? ']' : '[') === state.symb ? '{' : '}';
3011
- },
3012
- isComplete: function(state) {
3013
- return state.index === 0 && state.nextCh === state.symb;
3014
- }
3015
- },
3016
- comment: {
3017
- isComplete: function(state) {
3018
- var found = state.lastCh === '*' && state.nextCh === '/';
3019
- state.lastCh = state.nextCh;
3020
- return found;
3021
- }
3022
- },
3023
- // TODO: The original Vim implementation only operates on level 1 and 2.
3024
- // The current implementation doesn't check for code block level and
3025
- // therefore it operates on any levels.
3026
- method: {
3027
- init: function(state) {
3028
- state.symb = (state.symb === 'm' ? '{' : '}');
3029
- state.reverseSymb = state.symb === '{' ? '}' : '{';
3030
- },
3031
- isComplete: function(state) {
3032
- if (state.nextCh === state.symb)return true;
3033
- return false;
3034
- }
3035
- },
3036
- preprocess: {
3037
- init: function(state) {
3038
- state.index = 0;
3039
- },
3040
- isComplete: function(state) {
3041
- if (state.nextCh === '#') {
3042
- var token = state.lineText.match(/#(\w+)/)[1];
3043
- if (token === 'endif') {
3044
- if (state.forward && state.depth === 0) {
3045
- return true;
3046
- }
3047
- state.depth++;
3048
- } else if (token === 'if') {
3049
- if (!state.forward && state.depth === 0) {
3050
- return true;
3051
- }
3052
- state.depth--;
3053
- }
3054
- if (token === 'else' && state.depth === 0)return true;
3055
- }
3056
- return false;
3057
- }
3058
- }
3059
- };
3060
- function findSymbol(cm, repeat, forward, symb) {
3061
- var cur = copyCursor(cm.getCursor());
3062
- var increment = forward ? 1 : -1;
3063
- var endLine = forward ? cm.lineCount() : -1;
3064
- var curCh = cur.ch;
3065
- var line = cur.line;
3066
- var lineText = cm.getLine(line);
3067
- var state = {
3068
- lineText: lineText,
3069
- nextCh: lineText.charAt(curCh),
3070
- lastCh: null,
3071
- index: curCh,
3072
- symb: symb,
3073
- reverseSymb: (forward ? { ')': '(', '}': '{' } : { '(': ')', '{': '}' })[symb],
3074
- forward: forward,
3075
- depth: 0,
3076
- curMoveThrough: false
3077
- };
3078
- var mode = symbolToMode[symb];
3079
- if (!mode)return cur;
3080
- var init = findSymbolModes[mode].init;
3081
- var isComplete = findSymbolModes[mode].isComplete;
3082
- if (init) { init(state); }
3083
- while (line !== endLine && repeat) {
3084
- state.index += increment;
3085
- state.nextCh = state.lineText.charAt(state.index);
3086
- if (!state.nextCh) {
3087
- line += increment;
3088
- state.lineText = cm.getLine(line) || '';
3089
- if (increment > 0) {
3090
- state.index = 0;
3091
- } else {
3092
- var lineLen = state.lineText.length;
3093
- state.index = (lineLen > 0) ? (lineLen-1) : 0;
3094
- }
3095
- state.nextCh = state.lineText.charAt(state.index);
3096
- }
3097
- if (isComplete(state)) {
3098
- cur.line = line;
3099
- cur.ch = state.index;
3100
- repeat--;
3101
- }
3102
- }
3103
- if (state.nextCh || state.curMoveThrough) {
3104
- return Pos(line, state.index);
3105
- }
3106
- return cur;
3107
- }
3108
-
3109
- /*
3110
- * Returns the boundaries of the next word. If the cursor in the middle of
3111
- * the word, then returns the boundaries of the current word, starting at
3112
- * the cursor. If the cursor is at the start/end of a word, and we are going
3113
- * forward/backward, respectively, find the boundaries of the next word.
3114
- *
3115
- * @param {CodeMirror} cm CodeMirror object.
3116
- * @param {Cursor} cur The cursor position.
3117
- * @param {boolean} forward True to search forward. False to search
3118
- * backward.
3119
- * @param {boolean} bigWord True if punctuation count as part of the word.
3120
- * False if only [a-zA-Z0-9] characters count as part of the word.
3121
- * @param {boolean} emptyLineIsWord True if empty lines should be treated
3122
- * as words.
3123
- * @return {Object{from:number, to:number, line: number}} The boundaries of
3124
- * the word, or null if there are no more words.
3125
- */
3126
- function findWord(cm, cur, forward, bigWord, emptyLineIsWord) {
3127
- var lineNum = cur.line;
3128
- var pos = cur.ch;
3129
- var line = cm.getLine(lineNum);
3130
- var dir = forward ? 1 : -1;
3131
- var regexps = bigWord ? bigWordRegexp : wordRegexp;
3132
-
3133
- if (emptyLineIsWord && line == '') {
3134
- lineNum += dir;
3135
- line = cm.getLine(lineNum);
3136
- if (!isLine(cm, lineNum)) {
3137
- return null;
3138
- }
3139
- pos = (forward) ? 0 : line.length;
3140
- }
3141
-
3142
- while (true) {
3143
- if (emptyLineIsWord && line == '') {
3144
- return { from: 0, to: 0, line: lineNum };
3145
- }
3146
- var stop = (dir > 0) ? line.length : -1;
3147
- var wordStart = stop, wordEnd = stop;
3148
- // Find bounds of next word.
3149
- while (pos != stop) {
3150
- var foundWord = false;
3151
- for (var i = 0; i < regexps.length && !foundWord; ++i) {
3152
- if (regexps[i].test(line.charAt(pos))) {
3153
- wordStart = pos;
3154
- // Advance to end of word.
3155
- while (pos != stop && regexps[i].test(line.charAt(pos))) {
3156
- pos += dir;
3157
- }
3158
- wordEnd = pos;
3159
- foundWord = wordStart != wordEnd;
3160
- if (wordStart == cur.ch && lineNum == cur.line &&
3161
- wordEnd == wordStart + dir) {
3162
- // We started at the end of a word. Find the next one.
3163
- continue;
3164
- } else {
3165
- return {
3166
- from: Math.min(wordStart, wordEnd + 1),
3167
- to: Math.max(wordStart, wordEnd),
3168
- line: lineNum };
3169
- }
3170
- }
3171
- }
3172
- if (!foundWord) {
3173
- pos += dir;
3174
- }
3175
- }
3176
- // Advance to next/prev line.
3177
- lineNum += dir;
3178
- if (!isLine(cm, lineNum)) {
3179
- return null;
3180
- }
3181
- line = cm.getLine(lineNum);
3182
- pos = (dir > 0) ? 0 : line.length;
3183
- }
3184
- // Should never get here.
3185
- throw new Error('The impossible happened.');
3186
- }
3187
-
3188
- /**
3189
- * @param {CodeMirror} cm CodeMirror object.
3190
- * @param {Pos} cur The position to start from.
3191
- * @param {int} repeat Number of words to move past.
3192
- * @param {boolean} forward True to search forward. False to search
3193
- * backward.
3194
- * @param {boolean} wordEnd True to move to end of word. False to move to
3195
- * beginning of word.
3196
- * @param {boolean} bigWord True if punctuation count as part of the word.
3197
- * False if only alphabet characters count as part of the word.
3198
- * @return {Cursor} The position the cursor should move to.
3199
- */
3200
- function moveToWord(cm, cur, repeat, forward, wordEnd, bigWord) {
3201
- var curStart = copyCursor(cur);
3202
- var words = [];
3203
- if (forward && !wordEnd || !forward && wordEnd) {
3204
- repeat++;
3205
- }
3206
- // For 'e', empty lines are not considered words, go figure.
3207
- var emptyLineIsWord = !(forward && wordEnd);
3208
- for (var i = 0; i < repeat; i++) {
3209
- var word = findWord(cm, cur, forward, bigWord, emptyLineIsWord);
3210
- if (!word) {
3211
- var eodCh = lineLength(cm, cm.lastLine());
3212
- words.push(forward
3213
- ? {line: cm.lastLine(), from: eodCh, to: eodCh}
3214
- : {line: 0, from: 0, to: 0});
3215
- break;
3216
- }
3217
- words.push(word);
3218
- cur = Pos(word.line, forward ? (word.to - 1) : word.from);
3219
- }
3220
- var shortCircuit = words.length != repeat;
3221
- var firstWord = words[0];
3222
- var lastWord = words.pop();
3223
- if (forward && !wordEnd) {
3224
- // w
3225
- if (!shortCircuit && (firstWord.from != curStart.ch || firstWord.line != curStart.line)) {
3226
- // We did not start in the middle of a word. Discard the extra word at the end.
3227
- lastWord = words.pop();
3228
- }
3229
- return Pos(lastWord.line, lastWord.from);
3230
- } else if (forward && wordEnd) {
3231
- return Pos(lastWord.line, lastWord.to - 1);
3232
- } else if (!forward && wordEnd) {
3233
- // ge
3234
- if (!shortCircuit && (firstWord.to != curStart.ch || firstWord.line != curStart.line)) {
3235
- // We did not start in the middle of a word. Discard the extra word at the end.
3236
- lastWord = words.pop();
3237
- }
3238
- return Pos(lastWord.line, lastWord.to);
3239
- } else {
3240
- // b
3241
- return Pos(lastWord.line, lastWord.from);
3242
- }
3243
- }
3244
-
3245
- function moveToCharacter(cm, repeat, forward, character) {
3246
- var cur = cm.getCursor();
3247
- var start = cur.ch;
3248
- var idx;
3249
- for (var i = 0; i < repeat; i ++) {
3250
- var line = cm.getLine(cur.line);
3251
- idx = charIdxInLine(start, line, character, forward, true);
3252
- if (idx == -1) {
3253
- return null;
3254
- }
3255
- start = idx;
3256
- }
3257
- return Pos(cm.getCursor().line, idx);
3258
- }
3259
-
3260
- function moveToColumn(cm, repeat) {
3261
- // repeat is always >= 1, so repeat - 1 always corresponds
3262
- // to the column we want to go to.
3263
- var line = cm.getCursor().line;
3264
- return clipCursorToContent(cm, Pos(line, repeat - 1));
3265
- }
3266
-
3267
- function updateMark(cm, vim, markName, pos) {
3268
- if (!inArray(markName, validMarks)) {
3269
- return;
3270
- }
3271
- if (vim.marks[markName]) {
3272
- vim.marks[markName].clear();
3273
- }
3274
- vim.marks[markName] = cm.setBookmark(pos);
3275
- }
3276
-
3277
- function charIdxInLine(start, line, character, forward, includeChar) {
3278
- // Search for char in line.
3279
- // motion_options: {forward, includeChar}
3280
- // If includeChar = true, include it too.
3281
- // If forward = true, search forward, else search backwards.
3282
- // If char is not found on this line, do nothing
3283
- var idx;
3284
- if (forward) {
3285
- idx = line.indexOf(character, start + 1);
3286
- if (idx != -1 && !includeChar) {
3287
- idx -= 1;
3288
- }
3289
- } else {
3290
- idx = line.lastIndexOf(character, start - 1);
3291
- if (idx != -1 && !includeChar) {
3292
- idx += 1;
3293
- }
3294
- }
3295
- return idx;
3296
- }
3297
-
3298
- function findParagraph(cm, head, repeat, dir, inclusive) {
3299
- var line = head.line;
3300
- var min = cm.firstLine();
3301
- var max = cm.lastLine();
3302
- var start, end, i = line;
3303
- function isEmpty(i) { return !cm.getLine(i); }
3304
- function isBoundary(i, dir, any) {
3305
- if (any) { return isEmpty(i) != isEmpty(i + dir); }
3306
- return !isEmpty(i) && isEmpty(i + dir);
3307
- }
3308
- if (dir) {
3309
- while (min <= i && i <= max && repeat > 0) {
3310
- if (isBoundary(i, dir)) { repeat--; }
3311
- i += dir;
3312
- }
3313
- return new Pos(i, 0);
3314
- }
3315
-
3316
- var vim = cm.state.vim;
3317
- if (vim.visualLine && isBoundary(line, 1, true)) {
3318
- var anchor = vim.sel.anchor;
3319
- if (isBoundary(anchor.line, -1, true)) {
3320
- if (!inclusive || anchor.line != line) {
3321
- line += 1;
3322
- }
3323
- }
3324
- }
3325
- var startState = isEmpty(line);
3326
- for (i = line; i <= max && repeat; i++) {
3327
- if (isBoundary(i, 1, true)) {
3328
- if (!inclusive || isEmpty(i) != startState) {
3329
- repeat--;
3330
- }
3331
- }
3332
- }
3333
- end = new Pos(i, 0);
3334
- // select boundary before paragraph for the last one
3335
- if (i > max && !startState) { startState = true; }
3336
- else { inclusive = false; }
3337
- for (i = line; i > min; i--) {
3338
- if (!inclusive || isEmpty(i) == startState || i == line) {
3339
- if (isBoundary(i, -1, true)) { break; }
3340
- }
3341
- }
3342
- start = new Pos(i, 0);
3343
- return { start: start, end: end };
3344
- }
3345
-
3346
- // TODO: perhaps this finagling of start and end positions belonds
3347
- // in codmirror/replaceRange?
3348
- function selectCompanionObject(cm, head, symb, inclusive) {
3349
- var cur = head, start, end;
3350
-
3351
- var bracketRegexp = ({
3352
- '(': /[()]/, ')': /[()]/,
3353
- '[': /[[\]]/, ']': /[[\]]/,
3354
- '{': /[{}]/, '}': /[{}]/})[symb];
3355
- var openSym = ({
3356
- '(': '(', ')': '(',
3357
- '[': '[', ']': '[',
3358
- '{': '{', '}': '{'})[symb];
3359
- var curChar = cm.getLine(cur.line).charAt(cur.ch);
3360
- // Due to the behavior of scanForBracket, we need to add an offset if the
3361
- // cursor is on a matching open bracket.
3362
- var offset = curChar === openSym ? 1 : 0;
3363
-
3364
- start = cm.scanForBracket(Pos(cur.line, cur.ch + offset), -1, null, {'bracketRegex': bracketRegexp});
3365
- end = cm.scanForBracket(Pos(cur.line, cur.ch + offset), 1, null, {'bracketRegex': bracketRegexp});
3366
-
3367
- if (!start || !end) {
3368
- return { start: cur, end: cur };
3369
- }
3370
-
3371
- start = start.pos;
3372
- end = end.pos;
3373
-
3374
- if ((start.line == end.line && start.ch > end.ch)
3375
- || (start.line > end.line)) {
3376
- var tmp = start;
3377
- start = end;
3378
- end = tmp;
3379
- }
3380
-
3381
- if (inclusive) {
3382
- end.ch += 1;
3383
- } else {
3384
- start.ch += 1;
3385
- }
3386
-
3387
- return { start: start, end: end };
3388
- }
3389
-
3390
- // Takes in a symbol and a cursor and tries to simulate text objects that
3391
- // have identical opening and closing symbols
3392
- // TODO support across multiple lines
3393
- function findBeginningAndEnd(cm, head, symb, inclusive) {
3394
- var cur = copyCursor(head);
3395
- var line = cm.getLine(cur.line);
3396
- var chars = line.split('');
3397
- var start, end, i, len;
3398
- var firstIndex = chars.indexOf(symb);
3399
-
3400
- // the decision tree is to always look backwards for the beginning first,
3401
- // but if the cursor is in front of the first instance of the symb,
3402
- // then move the cursor forward
3403
- if (cur.ch < firstIndex) {
3404
- cur.ch = firstIndex;
3405
- // Why is this line even here???
3406
- // cm.setCursor(cur.line, firstIndex+1);
3407
- }
3408
- // otherwise if the cursor is currently on the closing symbol
3409
- else if (firstIndex < cur.ch && chars[cur.ch] == symb) {
3410
- end = cur.ch; // assign end to the current cursor
3411
- --cur.ch; // make sure to look backwards
3412
- }
3413
-
3414
- // if we're currently on the symbol, we've got a start
3415
- if (chars[cur.ch] == symb && !end) {
3416
- start = cur.ch + 1; // assign start to ahead of the cursor
3417
- } else {
3418
- // go backwards to find the start
3419
- for (i = cur.ch; i > -1 && !start; i--) {
3420
- if (chars[i] == symb) {
3421
- start = i + 1;
3422
- }
3423
- }
3424
- }
3425
-
3426
- // look forwards for the end symbol
3427
- if (start && !end) {
3428
- for (i = start, len = chars.length; i < len && !end; i++) {
3429
- if (chars[i] == symb) {
3430
- end = i;
3431
- }
3432
- }
3433
- }
3434
-
3435
- // nothing found
3436
- if (!start || !end) {
3437
- return { start: cur, end: cur };
3438
- }
3439
-
3440
- // include the symbols
3441
- if (inclusive) {
3442
- --start; ++end;
3443
- }
3444
-
3445
- return {
3446
- start: Pos(cur.line, start),
3447
- end: Pos(cur.line, end)
3448
- };
3449
- }
3450
-
3451
- // Search functions
3452
- defineOption('pcre', true, 'boolean');
3453
- function SearchState() {}
3454
- SearchState.prototype = {
3455
- getQuery: function() {
3456
- return vimGlobalState.query;
3457
- },
3458
- setQuery: function(query) {
3459
- vimGlobalState.query = query;
3460
- },
3461
- getOverlay: function() {
3462
- return this.searchOverlay;
3463
- },
3464
- setOverlay: function(overlay) {
3465
- this.searchOverlay = overlay;
3466
- },
3467
- isReversed: function() {
3468
- return vimGlobalState.isReversed;
3469
- },
3470
- setReversed: function(reversed) {
3471
- vimGlobalState.isReversed = reversed;
3472
- }
3473
- };
3474
- function getSearchState(cm) {
3475
- var vim = cm.state.vim;
3476
- return vim.searchState_ || (vim.searchState_ = new SearchState());
3477
- }
3478
- function dialog(cm, template, shortText, onClose, options) {
3479
- if (cm.openDialog) {
3480
- cm.openDialog(template, onClose, { bottom: true, value: options.value,
3481
- onKeyDown: options.onKeyDown, onKeyUp: options.onKeyUp });
3482
- }
3483
- else {
3484
- onClose(prompt(shortText, ''));
3485
- }
3486
- }
3487
- function splitBySlash(argString) {
3488
- var slashes = findUnescapedSlashes(argString) || [];
3489
- if (!slashes.length) return [];
3490
- var tokens = [];
3491
- // in case of strings like foo/bar
3492
- if (slashes[0] !== 0) return;
3493
- for (var i = 0; i < slashes.length; i++) {
3494
- if (typeof slashes[i] == 'number')
3495
- tokens.push(argString.substring(slashes[i] + 1, slashes[i+1]));
3496
- }
3497
- return tokens;
3498
- }
3499
-
3500
- function findUnescapedSlashes(str) {
3501
- var escapeNextChar = false;
3502
- var slashes = [];
3503
- for (var i = 0; i < str.length; i++) {
3504
- var c = str.charAt(i);
3505
- if (!escapeNextChar && c == '/') {
3506
- slashes.push(i);
3507
- }
3508
- escapeNextChar = !escapeNextChar && (c == '\\');
3509
- }
3510
- return slashes;
3511
- }
3512
-
3513
- // Translates a search string from ex (vim) syntax into javascript form.
3514
- function translateRegex(str) {
3515
- // When these match, add a '\' if unescaped or remove one if escaped.
3516
- var specials = '|(){';
3517
- // Remove, but never add, a '\' for these.
3518
- var unescape = '}';
3519
- var escapeNextChar = false;
3520
- var out = [];
3521
- for (var i = -1; i < str.length; i++) {
3522
- var c = str.charAt(i) || '';
3523
- var n = str.charAt(i+1) || '';
3524
- var specialComesNext = (n && specials.indexOf(n) != -1);
3525
- if (escapeNextChar) {
3526
- if (c !== '\\' || !specialComesNext) {
3527
- out.push(c);
3528
- }
3529
- escapeNextChar = false;
3530
- } else {
3531
- if (c === '\\') {
3532
- escapeNextChar = true;
3533
- // Treat the unescape list as special for removing, but not adding '\'.
3534
- if (n && unescape.indexOf(n) != -1) {
3535
- specialComesNext = true;
3536
- }
3537
- // Not passing this test means removing a '\'.
3538
- if (!specialComesNext || n === '\\') {
3539
- out.push(c);
3540
- }
3541
- } else {
3542
- out.push(c);
3543
- if (specialComesNext && n !== '\\') {
3544
- out.push('\\');
3545
- }
3546
- }
3547
- }
3548
- }
3549
- return out.join('');
3550
- }
3551
-
3552
- // Translates the replace part of a search and replace from ex (vim) syntax into
3553
- // javascript form. Similar to translateRegex, but additionally fixes back references
3554
- // (translates '\[0..9]' to '$[0..9]') and follows different rules for escaping '$'.
3555
- function translateRegexReplace(str) {
3556
- var escapeNextChar = false;
3557
- var out = [];
3558
- for (var i = -1; i < str.length; i++) {
3559
- var c = str.charAt(i) || '';
3560
- var n = str.charAt(i+1) || '';
3561
- if (escapeNextChar) {
3562
- // At any point in the loop, escapeNextChar is true if the previous
3563
- // character was a '\' and was not escaped.
3564
- out.push(c);
3565
- escapeNextChar = false;
3566
- } else {
3567
- if (c === '\\') {
3568
- escapeNextChar = true;
3569
- if ((isNumber(n) || n === '$')) {
3570
- out.push('$');
3571
- } else if (n !== '/' && n !== '\\') {
3572
- out.push('\\');
3573
- }
3574
- } else {
3575
- if (c === '$') {
3576
- out.push('$');
3577
- }
3578
- out.push(c);
3579
- if (n === '/') {
3580
- out.push('\\');
3581
- }
3582
- }
3583
- }
3584
- }
3585
- return out.join('');
3586
- }
3587
-
3588
- // Unescape \ and / in the replace part, for PCRE mode.
3589
- function unescapeRegexReplace(str) {
3590
- var stream = new CodeMirror.StringStream(str);
3591
- var output = [];
3592
- while (!stream.eol()) {
3593
- // Search for \.
3594
- while (stream.peek() && stream.peek() != '\\') {
3595
- output.push(stream.next());
3596
- }
3597
- if (stream.match('\\/', true)) {
3598
- // \/ => /
3599
- output.push('/');
3600
- } else if (stream.match('\\\\', true)) {
3601
- // \\ => \
3602
- output.push('\\');
3603
- } else {
3604
- // Don't change anything
3605
- output.push(stream.next());
3606
- }
3607
- }
3608
- return output.join('');
3609
- }
3610
-
3611
- /**
3612
- * Extract the regular expression from the query and return a Regexp object.
3613
- * Returns null if the query is blank.
3614
- * If ignoreCase is passed in, the Regexp object will have the 'i' flag set.
3615
- * If smartCase is passed in, and the query contains upper case letters,
3616
- * then ignoreCase is overridden, and the 'i' flag will not be set.
3617
- * If the query contains the /i in the flag part of the regular expression,
3618
- * then both ignoreCase and smartCase are ignored, and 'i' will be passed
3619
- * through to the Regex object.
3620
- */
3621
- function parseQuery(query, ignoreCase, smartCase) {
3622
- // First update the last search register
3623
- var lastSearchRegister = vimGlobalState.registerController.getRegister('/');
3624
- lastSearchRegister.setText(query);
3625
- // Check if the query is already a regex.
3626
- if (query instanceof RegExp) { return query; }
3627
- // First try to extract regex + flags from the input. If no flags found,
3628
- // extract just the regex. IE does not accept flags directly defined in
3629
- // the regex string in the form /regex/flags
3630
- var slashes = findUnescapedSlashes(query);
3631
- var regexPart;
3632
- var forceIgnoreCase;
3633
- if (!slashes.length) {
3634
- // Query looks like 'regexp'
3635
- regexPart = query;
3636
- } else {
3637
- // Query looks like 'regexp/...'
3638
- regexPart = query.substring(0, slashes[0]);
3639
- var flagsPart = query.substring(slashes[0]);
3640
- forceIgnoreCase = (flagsPart.indexOf('i') != -1);
3641
- }
3642
- if (!regexPart) {
3643
- return null;
3644
- }
3645
- if (!getOption('pcre')) {
3646
- regexPart = translateRegex(regexPart);
3647
- }
3648
- if (smartCase) {
3649
- ignoreCase = (/^[^A-Z]*$/).test(regexPart);
3650
- }
3651
- var regexp = new RegExp(regexPart,
3652
- (ignoreCase || forceIgnoreCase) ? 'i' : undefined);
3653
- return regexp;
3654
- }
3655
- function showConfirm(cm, text) {
3656
- if (cm.openNotification) {
3657
- cm.openNotification('<span style="color: red">' + text + '</span>',
3658
- {bottom: true, duration: 5000});
3659
- } else {
3660
- alert(text);
3661
- }
3662
- }
3663
- function makePrompt(prefix, desc) {
3664
- var raw = '';
3665
- if (prefix) {
3666
- raw += '<span style="font-family: monospace">' + prefix + '</span>';
3667
- }
3668
- raw += '<input type="text"/> ' +
3669
- '<span style="color: #888">';
3670
- if (desc) {
3671
- raw += '<span style="color: #888">';
3672
- raw += desc;
3673
- raw += '</span>';
3674
- }
3675
- return raw;
3676
- }
3677
- var searchPromptDesc = '(Javascript regexp)';
3678
- function showPrompt(cm, options) {
3679
- var shortText = (options.prefix || '') + ' ' + (options.desc || '');
3680
- var prompt = makePrompt(options.prefix, options.desc);
3681
- dialog(cm, prompt, shortText, options.onClose, options);
3682
- }
3683
- function regexEqual(r1, r2) {
3684
- if (r1 instanceof RegExp && r2 instanceof RegExp) {
3685
- var props = ['global', 'multiline', 'ignoreCase', 'source'];
3686
- for (var i = 0; i < props.length; i++) {
3687
- var prop = props[i];
3688
- if (r1[prop] !== r2[prop]) {
3689
- return false;
3690
- }
3691
- }
3692
- return true;
3693
- }
3694
- return false;
3695
- }
3696
- // Returns true if the query is valid.
3697
- function updateSearchQuery(cm, rawQuery, ignoreCase, smartCase) {
3698
- if (!rawQuery) {
3699
- return;
3700
- }
3701
- var state = getSearchState(cm);
3702
- var query = parseQuery(rawQuery, !!ignoreCase, !!smartCase);
3703
- if (!query) {
3704
- return;
3705
- }
3706
- highlightSearchMatches(cm, query);
3707
- if (regexEqual(query, state.getQuery())) {
3708
- return query;
3709
- }
3710
- state.setQuery(query);
3711
- return query;
3712
- }
3713
- function searchOverlay(query) {
3714
- if (query.source.charAt(0) == '^') {
3715
- var matchSol = true;
3716
- }
3717
- return {
3718
- token: function(stream) {
3719
- if (matchSol && !stream.sol()) {
3720
- stream.skipToEnd();
3721
- return;
3722
- }
3723
- var match = stream.match(query, false);
3724
- if (match) {
3725
- if (match[0].length == 0) {
3726
- // Matched empty string, skip to next.
3727
- stream.next();
3728
- return 'searching';
3729
- }
3730
- if (!stream.sol()) {
3731
- // Backtrack 1 to match \b
3732
- stream.backUp(1);
3733
- if (!query.exec(stream.next() + match[0])) {
3734
- stream.next();
3735
- return null;
3736
- }
3737
- }
3738
- stream.match(query);
3739
- return 'searching';
3740
- }
3741
- while (!stream.eol()) {
3742
- stream.next();
3743
- if (stream.match(query, false)) break;
3744
- }
3745
- },
3746
- query: query
3747
- };
3748
- }
3749
- function highlightSearchMatches(cm, query) {
3750
- var overlay = getSearchState(cm).getOverlay();
3751
- if (!overlay || query != overlay.query) {
3752
- if (overlay) {
3753
- cm.removeOverlay(overlay);
3754
- }
3755
- overlay = searchOverlay(query);
3756
- cm.addOverlay(overlay);
3757
- getSearchState(cm).setOverlay(overlay);
3758
- }
3759
- }
3760
- function findNext(cm, prev, query, repeat) {
3761
- if (repeat === undefined) { repeat = 1; }
3762
- return cm.operation(function() {
3763
- var pos = cm.getCursor();
3764
- var cursor = cm.getSearchCursor(query, pos);
3765
- for (var i = 0; i < repeat; i++) {
3766
- var found = cursor.find(prev);
3767
- if (i == 0 && found && cursorEqual(cursor.from(), pos)) { found = cursor.find(prev); }
3768
- if (!found) {
3769
- // SearchCursor may have returned null because it hit EOF, wrap
3770
- // around and try again.
3771
- cursor = cm.getSearchCursor(query,
3772
- (prev) ? Pos(cm.lastLine()) : Pos(cm.firstLine(), 0) );
3773
- if (!cursor.find(prev)) {
3774
- return;
3775
- }
3776
- }
3777
- }
3778
- return cursor.from();
3779
- });
3780
- }
3781
- function clearSearchHighlight(cm) {
3782
- cm.removeOverlay(getSearchState(cm).getOverlay());
3783
- getSearchState(cm).setOverlay(null);
3784
- }
3785
- /**
3786
- * Check if pos is in the specified range, INCLUSIVE.
3787
- * Range can be specified with 1 or 2 arguments.
3788
- * If the first range argument is an array, treat it as an array of line
3789
- * numbers. Match pos against any of the lines.
3790
- * If the first range argument is a number,
3791
- * if there is only 1 range argument, check if pos has the same line
3792
- * number
3793
- * if there are 2 range arguments, then check if pos is in between the two
3794
- * range arguments.
3795
- */
3796
- function isInRange(pos, start, end) {
3797
- if (typeof pos != 'number') {
3798
- // Assume it is a cursor position. Get the line number.
3799
- pos = pos.line;
3800
- }
3801
- if (start instanceof Array) {
3802
- return inArray(pos, start);
3803
- } else {
3804
- if (end) {
3805
- return (pos >= start && pos <= end);
3806
- } else {
3807
- return pos == start;
3808
- }
3809
- }
3810
- }
3811
- function getUserVisibleLines(cm) {
3812
- var scrollInfo = cm.getScrollInfo();
3813
- var occludeToleranceTop = 6;
3814
- var occludeToleranceBottom = 10;
3815
- var from = cm.coordsChar({left:0, top: occludeToleranceTop + scrollInfo.top}, 'local');
3816
- var bottomY = scrollInfo.clientHeight - occludeToleranceBottom + scrollInfo.top;
3817
- var to = cm.coordsChar({left:0, top: bottomY}, 'local');
3818
- return {top: from.line, bottom: to.line};
3819
- }
3820
-
3821
- // Ex command handling
3822
- // Care must be taken when adding to the default Ex command map. For any
3823
- // pair of commands that have a shared prefix, at least one of their
3824
- // shortNames must not match the prefix of the other command.
3825
- var defaultExCommandMap = [
3826
- { name: 'map' },
3827
- { name: 'imap', shortName: 'im' },
3828
- { name: 'nmap', shortName: 'nm' },
3829
- { name: 'vmap', shortName: 'vm' },
3830
- { name: 'unmap' },
3831
- { name: 'write', shortName: 'w' },
3832
- { name: 'undo', shortName: 'u' },
3833
- { name: 'redo', shortName: 'red' },
3834
- { name: 'set', shortName: 'set' },
3835
- { name: 'sort', shortName: 'sor' },
3836
- { name: 'substitute', shortName: 's', possiblyAsync: true },
3837
- { name: 'nohlsearch', shortName: 'noh' },
3838
- { name: 'delmarks', shortName: 'delm' },
3839
- { name: 'registers', shortName: 'reg', excludeFromCommandHistory: true },
3840
- { name: 'global', shortName: 'g' }
3841
- ];
3842
- var ExCommandDispatcher = function() {
3843
- this.buildCommandMap_();
3844
- };
3845
- ExCommandDispatcher.prototype = {
3846
- processCommand: function(cm, input, opt_params) {
3847
- var vim = cm.state.vim;
3848
- var commandHistoryRegister = vimGlobalState.registerController.getRegister(':');
3849
- var previousCommand = commandHistoryRegister.toString();
3850
- if (vim.visualMode) {
3851
- exitVisualMode(cm);
3852
- }
3853
- var inputStream = new CodeMirror.StringStream(input);
3854
- // update ": with the latest command whether valid or invalid
3855
- commandHistoryRegister.setText(input);
3856
- var params = opt_params || {};
3857
- params.input = input;
3858
- try {
3859
- this.parseInput_(cm, inputStream, params);
3860
- } catch(e) {
3861
- showConfirm(cm, e);
3862
- throw e;
3863
- }
3864
- var command;
3865
- var commandName;
3866
- if (!params.commandName) {
3867
- // If only a line range is defined, move to the line.
3868
- if (params.line !== undefined) {
3869
- commandName = 'move';
3870
- }
3871
- } else {
3872
- command = this.matchCommand_(params.commandName);
3873
- if (command) {
3874
- commandName = command.name;
3875
- if (command.excludeFromCommandHistory) {
3876
- commandHistoryRegister.setText(previousCommand);
3877
- }
3878
- this.parseCommandArgs_(inputStream, params, command);
3879
- if (command.type == 'exToKey') {
3880
- // Handle Ex to Key mapping.
3881
- for (var i = 0; i < command.toKeys.length; i++) {
3882
- CodeMirror.Vim.handleKey(cm, command.toKeys[i], 'mapping');
3883
- }
3884
- return;
3885
- } else if (command.type == 'exToEx') {
3886
- // Handle Ex to Ex mapping.
3887
- this.processCommand(cm, command.toInput);
3888
- return;
3889
- }
3890
- }
3891
- }
3892
- if (!commandName) {
3893
- showConfirm(cm, 'Not an editor command ":' + input + '"');
3894
- return;
3895
- }
3896
- try {
3897
- exCommands[commandName](cm, params);
3898
- // Possibly asynchronous commands (e.g. substitute, which might have a
3899
- // user confirmation), are responsible for calling the callback when
3900
- // done. All others have it taken care of for them here.
3901
- if ((!command || !command.possiblyAsync) && params.callback) {
3902
- params.callback();
3903
- }
3904
- } catch(e) {
3905
- showConfirm(cm, e);
3906
- throw e;
3907
- }
3908
- },
3909
- parseInput_: function(cm, inputStream, result) {
3910
- inputStream.eatWhile(':');
3911
- // Parse range.
3912
- if (inputStream.eat('%')) {
3913
- result.line = cm.firstLine();
3914
- result.lineEnd = cm.lastLine();
3915
- } else {
3916
- result.line = this.parseLineSpec_(cm, inputStream);
3917
- if (result.line !== undefined && inputStream.eat(',')) {
3918
- result.lineEnd = this.parseLineSpec_(cm, inputStream);
3919
- }
3920
- }
3921
-
3922
- // Parse command name.
3923
- var commandMatch = inputStream.match(/^(\w+)/);
3924
- if (commandMatch) {
3925
- result.commandName = commandMatch[1];
3926
- } else {
3927
- result.commandName = inputStream.match(/.*/)[0];
3928
- }
3929
-
3930
- return result;
3931
- },
3932
- parseLineSpec_: function(cm, inputStream) {
3933
- var numberMatch = inputStream.match(/^(\d+)/);
3934
- if (numberMatch) {
3935
- return parseInt(numberMatch[1], 10) - 1;
3936
- }
3937
- switch (inputStream.next()) {
3938
- case '.':
3939
- return cm.getCursor().line;
3940
- case '$':
3941
- return cm.lastLine();
3942
- case '\'':
3943
- var mark = cm.state.vim.marks[inputStream.next()];
3944
- if (mark && mark.find()) {
3945
- return mark.find().line;
3946
- }
3947
- throw new Error('Mark not set');
3948
- default:
3949
- inputStream.backUp(1);
3950
- return undefined;
3951
- }
3952
- },
3953
- parseCommandArgs_: function(inputStream, params, command) {
3954
- if (inputStream.eol()) {
3955
- return;
3956
- }
3957
- params.argString = inputStream.match(/.*/)[0];
3958
- // Parse command-line arguments
3959
- var delim = command.argDelimiter || /\s+/;
3960
- var args = trim(params.argString).split(delim);
3961
- if (args.length && args[0]) {
3962
- params.args = args;
3963
- }
3964
- },
3965
- matchCommand_: function(commandName) {
3966
- // Return the command in the command map that matches the shortest
3967
- // prefix of the passed in command name. The match is guaranteed to be
3968
- // unambiguous if the defaultExCommandMap's shortNames are set up
3969
- // correctly. (see @code{defaultExCommandMap}).
3970
- for (var i = commandName.length; i > 0; i--) {
3971
- var prefix = commandName.substring(0, i);
3972
- if (this.commandMap_[prefix]) {
3973
- var command = this.commandMap_[prefix];
3974
- if (command.name.indexOf(commandName) === 0) {
3975
- return command;
3976
- }
3977
- }
3978
- }
3979
- return null;
3980
- },
3981
- buildCommandMap_: function() {
3982
- this.commandMap_ = {};
3983
- for (var i = 0; i < defaultExCommandMap.length; i++) {
3984
- var command = defaultExCommandMap[i];
3985
- var key = command.shortName || command.name;
3986
- this.commandMap_[key] = command;
3987
- }
3988
- },
3989
- map: function(lhs, rhs, ctx) {
3990
- if (lhs != ':' && lhs.charAt(0) == ':') {
3991
- if (ctx) { throw Error('Mode not supported for ex mappings'); }
3992
- var commandName = lhs.substring(1);
3993
- if (rhs != ':' && rhs.charAt(0) == ':') {
3994
- // Ex to Ex mapping
3995
- this.commandMap_[commandName] = {
3996
- name: commandName,
3997
- type: 'exToEx',
3998
- toInput: rhs.substring(1),
3999
- user: true
4000
- };
4001
- } else {
4002
- // Ex to key mapping
4003
- this.commandMap_[commandName] = {
4004
- name: commandName,
4005
- type: 'exToKey',
4006
- toKeys: rhs,
4007
- user: true
4008
- };
4009
- }
4010
- } else {
4011
- if (rhs != ':' && rhs.charAt(0) == ':') {
4012
- // Key to Ex mapping.
4013
- var mapping = {
4014
- keys: lhs,
4015
- type: 'keyToEx',
4016
- exArgs: { input: rhs.substring(1) },
4017
- user: true};
4018
- if (ctx) { mapping.context = ctx; }
4019
- defaultKeymap.unshift(mapping);
4020
- } else {
4021
- // Key to key mapping
4022
- var mapping = {
4023
- keys: lhs,
4024
- type: 'keyToKey',
4025
- toKeys: rhs,
4026
- user: true
4027
- };
4028
- if (ctx) { mapping.context = ctx; }
4029
- defaultKeymap.unshift(mapping);
4030
- }
4031
- }
4032
- },
4033
- unmap: function(lhs, ctx) {
4034
- if (lhs != ':' && lhs.charAt(0) == ':') {
4035
- // Ex to Ex or Ex to key mapping
4036
- if (ctx) { throw Error('Mode not supported for ex mappings'); }
4037
- var commandName = lhs.substring(1);
4038
- if (this.commandMap_[commandName] && this.commandMap_[commandName].user) {
4039
- delete this.commandMap_[commandName];
4040
- return;
4041
- }
4042
- } else {
4043
- // Key to Ex or key to key mapping
4044
- var keys = lhs;
4045
- for (var i = 0; i < defaultKeymap.length; i++) {
4046
- if (keys == defaultKeymap[i].keys
4047
- && defaultKeymap[i].context === ctx
4048
- && defaultKeymap[i].user) {
4049
- defaultKeymap.splice(i, 1);
4050
- return;
4051
- }
4052
- }
4053
- }
4054
- throw Error('No such mapping.');
4055
- }
4056
- };
4057
-
4058
- var exCommands = {
4059
- map: function(cm, params, ctx) {
4060
- var mapArgs = params.args;
4061
- if (!mapArgs || mapArgs.length < 2) {
4062
- if (cm) {
4063
- showConfirm(cm, 'Invalid mapping: ' + params.input);
4064
- }
4065
- return;
4066
- }
4067
- exCommandDispatcher.map(mapArgs[0], mapArgs[1], ctx);
4068
- },
4069
- imap: function(cm, params) { this.map(cm, params, 'insert'); },
4070
- nmap: function(cm, params) { this.map(cm, params, 'normal'); },
4071
- vmap: function(cm, params) { this.map(cm, params, 'visual'); },
4072
- unmap: function(cm, params, ctx) {
4073
- var mapArgs = params.args;
4074
- if (!mapArgs || mapArgs.length < 1) {
4075
- if (cm) {
4076
- showConfirm(cm, 'No such mapping: ' + params.input);
4077
- }
4078
- return;
4079
- }
4080
- exCommandDispatcher.unmap(mapArgs[0], ctx);
4081
- },
4082
- move: function(cm, params) {
4083
- commandDispatcher.processCommand(cm, cm.state.vim, {
4084
- type: 'motion',
4085
- motion: 'moveToLineOrEdgeOfDocument',
4086
- motionArgs: { forward: false, explicitRepeat: true,
4087
- linewise: true },
4088
- repeatOverride: params.line+1});
4089
- },
4090
- set: function(cm, params) {
4091
- var setArgs = params.args;
4092
- if (!setArgs || setArgs.length < 1) {
4093
- if (cm) {
4094
- showConfirm(cm, 'Invalid mapping: ' + params.input);
4095
- }
4096
- return;
4097
- }
4098
- var expr = setArgs[0].split('=');
4099
- var optionName = expr[0];
4100
- var value = expr[1];
4101
- var forceGet = false;
4102
-
4103
- if (optionName.charAt(optionName.length - 1) == '?') {
4104
- // If post-fixed with ?, then the set is actually a get.
4105
- if (value) { throw Error('Trailing characters: ' + params.argString); }
4106
- optionName = optionName.substring(0, optionName.length - 1);
4107
- forceGet = true;
4108
- }
4109
- if (value === undefined && optionName.substring(0, 2) == 'no') {
4110
- // To set boolean options to false, the option name is prefixed with
4111
- // 'no'.
4112
- optionName = optionName.substring(2);
4113
- value = false;
4114
- }
4115
- var optionIsBoolean = options[optionName] && options[optionName].type == 'boolean';
4116
- if (optionIsBoolean && value == undefined) {
4117
- // Calling set with a boolean option sets it to true.
4118
- value = true;
4119
- }
4120
- if (!optionIsBoolean && !value || forceGet) {
4121
- var oldValue = getOption(optionName);
4122
- // If no value is provided, then we assume this is a get.
4123
- if (oldValue === true || oldValue === false) {
4124
- showConfirm(cm, ' ' + (oldValue ? '' : 'no') + optionName);
4125
- } else {
4126
- showConfirm(cm, ' ' + optionName + '=' + oldValue);
4127
- }
4128
- } else {
4129
- setOption(optionName, value);
4130
- }
4131
- },
4132
- registers: function(cm,params) {
4133
- var regArgs = params.args;
4134
- var registers = vimGlobalState.registerController.registers;
4135
- var regInfo = '----------Registers----------<br><br>';
4136
- if (!regArgs) {
4137
- for (var registerName in registers) {
4138
- var text = registers[registerName].toString();
4139
- if (text.length) {
4140
- regInfo += '"' + registerName + ' ' + text + '<br>';
4141
- }
4142
- }
4143
- } else {
4144
- var registerName;
4145
- regArgs = regArgs.join('');
4146
- for (var i = 0; i < regArgs.length; i++) {
4147
- registerName = regArgs.charAt(i);
4148
- if (!vimGlobalState.registerController.isValidRegister(registerName)) {
4149
- continue;
4150
- }
4151
- var register = registers[registerName] || new Register();
4152
- regInfo += '"' + registerName + ' ' + register.toString() + '<br>';
4153
- }
4154
- }
4155
- showConfirm(cm, regInfo);
4156
- },
4157
- sort: function(cm, params) {
4158
- var reverse, ignoreCase, unique, number;
4159
- function parseArgs() {
4160
- if (params.argString) {
4161
- var args = new CodeMirror.StringStream(params.argString);
4162
- if (args.eat('!')) { reverse = true; }
4163
- if (args.eol()) { return; }
4164
- if (!args.eatSpace()) { return 'Invalid arguments'; }
4165
- var opts = args.match(/[a-z]+/);
4166
- if (opts) {
4167
- opts = opts[0];
4168
- ignoreCase = opts.indexOf('i') != -1;
4169
- unique = opts.indexOf('u') != -1;
4170
- var decimal = opts.indexOf('d') != -1 && 1;
4171
- var hex = opts.indexOf('x') != -1 && 1;
4172
- var octal = opts.indexOf('o') != -1 && 1;
4173
- if (decimal + hex + octal > 1) { return 'Invalid arguments'; }
4174
- number = decimal && 'decimal' || hex && 'hex' || octal && 'octal';
4175
- }
4176
- if (args.eatSpace() && args.match(/\/.*\//)) { 'patterns not supported'; }
4177
- }
4178
- }
4179
- var err = parseArgs();
4180
- if (err) {
4181
- showConfirm(cm, err + ': ' + params.argString);
4182
- return;
4183
- }
4184
- var lineStart = params.line || cm.firstLine();
4185
- var lineEnd = params.lineEnd || params.line || cm.lastLine();
4186
- if (lineStart == lineEnd) { return; }
4187
- var curStart = Pos(lineStart, 0);
4188
- var curEnd = Pos(lineEnd, lineLength(cm, lineEnd));
4189
- var text = cm.getRange(curStart, curEnd).split('\n');
4190
- var numberRegex = (number == 'decimal') ? /(-?)([\d]+)/ :
4191
- (number == 'hex') ? /(-?)(?:0x)?([0-9a-f]+)/i :
4192
- (number == 'octal') ? /([0-7]+)/ : null;
4193
- var radix = (number == 'decimal') ? 10 : (number == 'hex') ? 16 : (number == 'octal') ? 8 : null;
4194
- var numPart = [], textPart = [];
4195
- if (number) {
4196
- for (var i = 0; i < text.length; i++) {
4197
- if (numberRegex.exec(text[i])) {
4198
- numPart.push(text[i]);
4199
- } else {
4200
- textPart.push(text[i]);
4201
- }
4202
- }
4203
- } else {
4204
- textPart = text;
4205
- }
4206
- function compareFn(a, b) {
4207
- if (reverse) { var tmp; tmp = a; a = b; b = tmp; }
4208
- if (ignoreCase) { a = a.toLowerCase(); b = b.toLowerCase(); }
4209
- var anum = number && numberRegex.exec(a);
4210
- var bnum = number && numberRegex.exec(b);
4211
- if (!anum) { return a < b ? -1 : 1; }
4212
- anum = parseInt((anum[1] + anum[2]).toLowerCase(), radix);
4213
- bnum = parseInt((bnum[1] + bnum[2]).toLowerCase(), radix);
4214
- return anum - bnum;
4215
- }
4216
- numPart.sort(compareFn);
4217
- textPart.sort(compareFn);
4218
- text = (!reverse) ? textPart.concat(numPart) : numPart.concat(textPart);
4219
- if (unique) { // Remove duplicate lines
4220
- var textOld = text;
4221
- var lastLine;
4222
- text = [];
4223
- for (var i = 0; i < textOld.length; i++) {
4224
- if (textOld[i] != lastLine) {
4225
- text.push(textOld[i]);
4226
- }
4227
- lastLine = textOld[i];
4228
- }
4229
- }
4230
- cm.replaceRange(text.join('\n'), curStart, curEnd);
4231
- },
4232
- global: function(cm, params) {
4233
- // a global command is of the form
4234
- // :[range]g/pattern/[cmd]
4235
- // argString holds the string /pattern/[cmd]
4236
- var argString = params.argString;
4237
- if (!argString) {
4238
- showConfirm(cm, 'Regular Expression missing from global');
4239
- return;
4240
- }
4241
- // range is specified here
4242
- var lineStart = (params.line !== undefined) ? params.line : cm.firstLine();
4243
- var lineEnd = params.lineEnd || params.line || cm.lastLine();
4244
- // get the tokens from argString
4245
- var tokens = splitBySlash(argString);
4246
- var regexPart = argString, cmd;
4247
- if (tokens.length) {
4248
- regexPart = tokens[0];
4249
- cmd = tokens.slice(1, tokens.length).join('/');
4250
- }
4251
- if (regexPart) {
4252
- // If regex part is empty, then use the previous query. Otherwise
4253
- // use the regex part as the new query.
4254
- try {
4255
- updateSearchQuery(cm, regexPart, true /** ignoreCase */,
4256
- true /** smartCase */);
4257
- } catch (e) {
4258
- showConfirm(cm, 'Invalid regex: ' + regexPart);
4259
- return;
4260
- }
4261
- }
4262
- // now that we have the regexPart, search for regex matches in the
4263
- // specified range of lines
4264
- var query = getSearchState(cm).getQuery();
4265
- var matchedLines = [], content = '';
4266
- for (var i = lineStart; i <= lineEnd; i++) {
4267
- var matched = query.test(cm.getLine(i));
4268
- if (matched) {
4269
- matchedLines.push(i+1);
4270
- content+= cm.getLine(i) + '<br>';
4271
- }
4272
- }
4273
- // if there is no [cmd], just display the list of matched lines
4274
- if (!cmd) {
4275
- showConfirm(cm, content);
4276
- return;
4277
- }
4278
- var index = 0;
4279
- var nextCommand = function() {
4280
- if (index < matchedLines.length) {
4281
- var command = matchedLines[index] + cmd;
4282
- exCommandDispatcher.processCommand(cm, command, {
4283
- callback: nextCommand
4284
- });
4285
- }
4286
- index++;
4287
- };
4288
- nextCommand();
4289
- },
4290
- substitute: function(cm, params) {
4291
- if (!cm.getSearchCursor) {
4292
- throw new Error('Search feature not available. Requires searchcursor.js or ' +
4293
- 'any other getSearchCursor implementation.');
4294
- }
4295
- var argString = params.argString;
4296
- var tokens = argString ? splitBySlash(argString) : [];
4297
- var regexPart, replacePart = '', trailing, flagsPart, count;
4298
- var confirm = false; // Whether to confirm each replace.
4299
- var global = false; // True to replace all instances on a line, false to replace only 1.
4300
- if (tokens.length) {
4301
- regexPart = tokens[0];
4302
- replacePart = tokens[1];
4303
- if (replacePart !== undefined) {
4304
- if (getOption('pcre')) {
4305
- replacePart = unescapeRegexReplace(replacePart);
4306
- } else {
4307
- replacePart = translateRegexReplace(replacePart);
4308
- }
4309
- vimGlobalState.lastSubstituteReplacePart = replacePart;
4310
- }
4311
- trailing = tokens[2] ? tokens[2].split(' ') : [];
4312
- } else {
4313
- // either the argString is empty or its of the form ' hello/world'
4314
- // actually splitBySlash returns a list of tokens
4315
- // only if the string starts with a '/'
4316
- if (argString && argString.length) {
4317
- showConfirm(cm, 'Substitutions should be of the form ' +
4318
- ':s/pattern/replace/');
4319
- return;
4320
- }
4321
- }
4322
- // After the 3rd slash, we can have flags followed by a space followed
4323
- // by count.
4324
- if (trailing) {
4325
- flagsPart = trailing[0];
4326
- count = parseInt(trailing[1]);
4327
- if (flagsPart) {
4328
- if (flagsPart.indexOf('c') != -1) {
4329
- confirm = true;
4330
- flagsPart.replace('c', '');
4331
- }
4332
- if (flagsPart.indexOf('g') != -1) {
4333
- global = true;
4334
- flagsPart.replace('g', '');
4335
- }
4336
- regexPart = regexPart + '/' + flagsPart;
4337
- }
4338
- }
4339
- if (regexPart) {
4340
- // If regex part is empty, then use the previous query. Otherwise use
4341
- // the regex part as the new query.
4342
- try {
4343
- updateSearchQuery(cm, regexPart, true /** ignoreCase */,
4344
- true /** smartCase */);
4345
- } catch (e) {
4346
- showConfirm(cm, 'Invalid regex: ' + regexPart);
4347
- return;
4348
- }
4349
- }
4350
- replacePart = replacePart || vimGlobalState.lastSubstituteReplacePart;
4351
- if (replacePart === undefined) {
4352
- showConfirm(cm, 'No previous substitute regular expression');
4353
- return;
4354
- }
4355
- var state = getSearchState(cm);
4356
- var query = state.getQuery();
4357
- var lineStart = (params.line !== undefined) ? params.line : cm.getCursor().line;
4358
- var lineEnd = params.lineEnd || lineStart;
4359
- if (count) {
4360
- lineStart = lineEnd;
4361
- lineEnd = lineStart + count - 1;
4362
- }
4363
- var startPos = clipCursorToContent(cm, Pos(lineStart, 0));
4364
- var cursor = cm.getSearchCursor(query, startPos);
4365
- doReplace(cm, confirm, global, lineStart, lineEnd, cursor, query, replacePart, params.callback);
4366
- },
4367
- redo: CodeMirror.commands.redo,
4368
- undo: CodeMirror.commands.undo,
4369
- write: function(cm) {
4370
- if (CodeMirror.commands.save) {
4371
- // If a save command is defined, call it.
4372
- CodeMirror.commands.save(cm);
4373
- } else {
4374
- // Saves to text area if no save command is defined.
4375
- cm.save();
4376
- }
4377
- },
4378
- nohlsearch: function(cm) {
4379
- clearSearchHighlight(cm);
4380
- },
4381
- delmarks: function(cm, params) {
4382
- if (!params.argString || !trim(params.argString)) {
4383
- showConfirm(cm, 'Argument required');
4384
- return;
4385
- }
4386
-
4387
- var state = cm.state.vim;
4388
- var stream = new CodeMirror.StringStream(trim(params.argString));
4389
- while (!stream.eol()) {
4390
- stream.eatSpace();
4391
-
4392
- // Record the streams position at the beginning of the loop for use
4393
- // in error messages.
4394
- var count = stream.pos;
4395
-
4396
- if (!stream.match(/[a-zA-Z]/, false)) {
4397
- showConfirm(cm, 'Invalid argument: ' + params.argString.substring(count));
4398
- return;
4399
- }
4400
-
4401
- var sym = stream.next();
4402
- // Check if this symbol is part of a range
4403
- if (stream.match('-', true)) {
4404
- // This symbol is part of a range.
4405
-
4406
- // The range must terminate at an alphabetic character.
4407
- if (!stream.match(/[a-zA-Z]/, false)) {
4408
- showConfirm(cm, 'Invalid argument: ' + params.argString.substring(count));
4409
- return;
4410
- }
4411
-
4412
- var startMark = sym;
4413
- var finishMark = stream.next();
4414
- // The range must terminate at an alphabetic character which
4415
- // shares the same case as the start of the range.
4416
- if (isLowerCase(startMark) && isLowerCase(finishMark) ||
4417
- isUpperCase(startMark) && isUpperCase(finishMark)) {
4418
- var start = startMark.charCodeAt(0);
4419
- var finish = finishMark.charCodeAt(0);
4420
- if (start >= finish) {
4421
- showConfirm(cm, 'Invalid argument: ' + params.argString.substring(count));
4422
- return;
4423
- }
4424
-
4425
- // Because marks are always ASCII values, and we have
4426
- // determined that they are the same case, we can use
4427
- // their char codes to iterate through the defined range.
4428
- for (var j = 0; j <= finish - start; j++) {
4429
- var mark = String.fromCharCode(start + j);
4430
- delete state.marks[mark];
4431
- }
4432
- } else {
4433
- showConfirm(cm, 'Invalid argument: ' + startMark + '-');
4434
- return;
4435
- }
4436
- } else {
4437
- // This symbol is a valid mark, and is not part of a range.
4438
- delete state.marks[sym];
4439
- }
4440
- }
4441
- }
4442
- };
4443
-
4444
- var exCommandDispatcher = new ExCommandDispatcher();
4445
-
4446
- /**
4447
- * @param {CodeMirror} cm CodeMirror instance we are in.
4448
- * @param {boolean} confirm Whether to confirm each replace.
4449
- * @param {Cursor} lineStart Line to start replacing from.
4450
- * @param {Cursor} lineEnd Line to stop replacing at.
4451
- * @param {RegExp} query Query for performing matches with.
4452
- * @param {string} replaceWith Text to replace matches with. May contain $1,
4453
- * $2, etc for replacing captured groups using Javascript replace.
4454
- * @param {function()} callback A callback for when the replace is done.
4455
- */
4456
- function doReplace(cm, confirm, global, lineStart, lineEnd, searchCursor, query,
4457
- replaceWith, callback) {
4458
- // Set up all the functions.
4459
- cm.state.vim.exMode = true;
4460
- var done = false;
4461
- var lastPos = searchCursor.from();
4462
- function replaceAll() {
4463
- cm.operation(function() {
4464
- while (!done) {
4465
- replace();
4466
- next();
4467
- }
4468
- stop();
4469
- });
4470
- }
4471
- function replace() {
4472
- var text = cm.getRange(searchCursor.from(), searchCursor.to());
4473
- var newText = text.replace(query, replaceWith);
4474
- searchCursor.replace(newText);
4475
- }
4476
- function next() {
4477
- var found;
4478
- // The below only loops to skip over multiple occurrences on the same
4479
- // line when 'global' is not true.
4480
- while(found = searchCursor.findNext() &&
4481
- isInRange(searchCursor.from(), lineStart, lineEnd)) {
4482
- if (!global && lastPos && searchCursor.from().line == lastPos.line) {
4483
- continue;
4484
- }
4485
- cm.scrollIntoView(searchCursor.from(), 30);
4486
- cm.setSelection(searchCursor.from(), searchCursor.to());
4487
- lastPos = searchCursor.from();
4488
- done = false;
4489
- return;
4490
- }
4491
- done = true;
4492
- }
4493
- function stop(close) {
4494
- if (close) { close(); }
4495
- cm.focus();
4496
- if (lastPos) {
4497
- cm.setCursor(lastPos);
4498
- var vim = cm.state.vim;
4499
- vim.exMode = false;
4500
- vim.lastHPos = vim.lastHSPos = lastPos.ch;
4501
- }
4502
- if (callback) { callback(); }
4503
- }
4504
- function onPromptKeyDown(e, _value, close) {
4505
- // Swallow all keys.
4506
- CodeMirror.e_stop(e);
4507
- var keyName = CodeMirror.keyName(e);
4508
- switch (keyName) {
4509
- case 'Y':
4510
- replace(); next(); break;
4511
- case 'N':
4512
- next(); break;
4513
- case 'A':
4514
- // replaceAll contains a call to close of its own. We don't want it
4515
- // to fire too early or multiple times.
4516
- var savedCallback = callback;
4517
- callback = undefined;
4518
- cm.operation(replaceAll);
4519
- callback = savedCallback;
4520
- break;
4521
- case 'L':
4522
- replace();
4523
- // fall through and exit.
4524
- case 'Q':
4525
- case 'Esc':
4526
- case 'Ctrl-C':
4527
- case 'Ctrl-[':
4528
- stop(close);
4529
- break;
4530
- }
4531
- if (done) { stop(close); }
4532
- return true;
4533
- }
4534
-
4535
- // Actually do replace.
4536
- next();
4537
- if (done) {
4538
- showConfirm(cm, 'No matches for ' + query.source);
4539
- return;
4540
- }
4541
- if (!confirm) {
4542
- replaceAll();
4543
- if (callback) { callback(); };
4544
- return;
4545
- }
4546
- showPrompt(cm, {
4547
- prefix: 'replace with <strong>' + replaceWith + '</strong> (y/n/a/q/l)',
4548
- onKeyDown: onPromptKeyDown
4549
- });
4550
- }
4551
-
4552
- CodeMirror.keyMap.vim = {
4553
- attach: attachVimMap,
4554
- detach: detachVimMap,
4555
- call: cmKey
4556
- };
4557
-
4558
- function exitInsertMode(cm) {
4559
- var vim = cm.state.vim;
4560
- var macroModeState = vimGlobalState.macroModeState;
4561
- var insertModeChangeRegister = vimGlobalState.registerController.getRegister('.');
4562
- var isPlaying = macroModeState.isPlaying;
4563
- var lastChange = macroModeState.lastInsertModeChanges;
4564
- // In case of visual block, the insertModeChanges are not saved as a
4565
- // single word, so we convert them to a single word
4566
- // so as to update the ". register as expected in real vim.
4567
- var text = [];
4568
- if (!isPlaying) {
4569
- var selLength = lastChange.inVisualBlock ? vim.lastSelection.visualBlock.height : 1;
4570
- var changes = lastChange.changes;
4571
- var text = [];
4572
- var i = 0;
4573
- // In case of multiple selections in blockwise visual,
4574
- // the inserted text, for example: 'f<Backspace>oo', is stored as
4575
- // 'f', 'f', InsertModeKey 'o', 'o', 'o', 'o'. (if you have a block with 2 lines).
4576
- // We push the contents of the changes array as per the following:
4577
- // 1. In case of InsertModeKey, just increment by 1.
4578
- // 2. In case of a character, jump by selLength (2 in the example).
4579
- while (i < changes.length) {
4580
- // This loop will convert 'ff<bs>oooo' to 'f<bs>oo'.
4581
- text.push(changes[i]);
4582
- if (changes[i] instanceof InsertModeKey) {
4583
- i++;
4584
- } else {
4585
- i+= selLength;
4586
- }
4587
- }
4588
- lastChange.changes = text;
4589
- cm.off('change', onChange);
4590
- CodeMirror.off(cm.getInputField(), 'keydown', onKeyEventTargetKeyDown);
4591
- }
4592
- if (!isPlaying && vim.insertModeRepeat > 1) {
4593
- // Perform insert mode repeat for commands like 3,a and 3,o.
4594
- repeatLastEdit(cm, vim, vim.insertModeRepeat - 1,
4595
- true /** repeatForInsert */);
4596
- vim.lastEditInputState.repeatOverride = vim.insertModeRepeat;
4597
- }
4598
- delete vim.insertModeRepeat;
4599
- vim.insertMode = false;
4600
- cm.setCursor(cm.getCursor().line, cm.getCursor().ch-1);
4601
- cm.setOption('keyMap', 'vim');
4602
- cm.setOption('disableInput', true);
4603
- cm.toggleOverwrite(false); // exit replace mode if we were in it.
4604
- // update the ". register before exiting insert mode
4605
- insertModeChangeRegister.setText(lastChange.changes.join(''));
4606
- CodeMirror.signal(cm, "vim-mode-change", {mode: "normal"});
4607
- if (macroModeState.isRecording) {
4608
- logInsertModeChange(macroModeState);
4609
- }
4610
- }
4611
-
4612
- // The timeout in milliseconds for the two-character ESC keymap should be
4613
- // adjusted according to your typing speed to prevent false positives.
4614
- defineOption('insertModeEscKeysTimeout', 200, 'number');
4615
-
4616
- CodeMirror.keyMap['vim-insert'] = {
4617
- // TODO: override navigation keys so that Esc will cancel automatic
4618
- // indentation from o, O, i_<CR>
4619
- 'Ctrl-N': 'autocomplete',
4620
- 'Ctrl-P': 'autocomplete',
4621
- 'Enter': function(cm) {
4622
- var fn = CodeMirror.commands.newlineAndIndentContinueComment ||
4623
- CodeMirror.commands.newlineAndIndent;
4624
- fn(cm);
4625
- },
4626
- fallthrough: ['default'],
4627
- attach: attachVimMap,
4628
- detach: detachVimMap,
4629
- call: cmKey
4630
- };
4631
-
4632
- CodeMirror.keyMap['vim-replace'] = {
4633
- 'Backspace': 'goCharLeft',
4634
- fallthrough: ['vim-insert'],
4635
- attach: attachVimMap,
4636
- detach: detachVimMap,
4637
- call: cmKey
4638
- };
4639
-
4640
- function executeMacroRegister(cm, vim, macroModeState, registerName) {
4641
- var register = vimGlobalState.registerController.getRegister(registerName);
4642
- var keyBuffer = register.keyBuffer;
4643
- var imc = 0;
4644
- macroModeState.isPlaying = true;
4645
- macroModeState.replaySearchQueries = register.searchQueries.slice(0);
4646
- for (var i = 0; i < keyBuffer.length; i++) {
4647
- var text = keyBuffer[i];
4648
- var match, key;
4649
- while (text) {
4650
- // Pull off one command key, which is either a single character
4651
- // or a special sequence wrapped in '<' and '>', e.g. '<Space>'.
4652
- match = (/<\w+-.+?>|<\w+>|./).exec(text);
4653
- key = match[0];
4654
- text = text.substring(match.index + key.length);
4655
- CodeMirror.Vim.handleKey(cm, key, 'macro');
4656
- if (vim.insertMode) {
4657
- var changes = register.insertModeChanges[imc++].changes;
4658
- vimGlobalState.macroModeState.lastInsertModeChanges.changes =
4659
- changes;
4660
- repeatInsertModeChanges(cm, changes, 1);
4661
- exitInsertMode(cm);
4662
- }
4663
- }
4664
- };
4665
- macroModeState.isPlaying = false;
4666
- }
4667
-
4668
- function logKey(macroModeState, key) {
4669
- if (macroModeState.isPlaying) { return; }
4670
- var registerName = macroModeState.latestRegister;
4671
- var register = vimGlobalState.registerController.getRegister(registerName);
4672
- if (register) {
4673
- register.pushText(key);
4674
- }
4675
- }
4676
-
4677
- function logInsertModeChange(macroModeState) {
4678
- if (macroModeState.isPlaying) { return; }
4679
- var registerName = macroModeState.latestRegister;
4680
- var register = vimGlobalState.registerController.getRegister(registerName);
4681
- if (register) {
4682
- register.pushInsertModeChanges(macroModeState.lastInsertModeChanges);
4683
- }
4684
- }
4685
-
4686
- function logSearchQuery(macroModeState, query) {
4687
- if (macroModeState.isPlaying) { return; }
4688
- var registerName = macroModeState.latestRegister;
4689
- var register = vimGlobalState.registerController.getRegister(registerName);
4690
- if (register) {
4691
- register.pushSearchQuery(query);
4692
- }
4693
- }
4694
-
4695
- /**
4696
- * Listens for changes made in insert mode.
4697
- * Should only be active in insert mode.
4698
- */
4699
- function onChange(_cm, changeObj) {
4700
- var macroModeState = vimGlobalState.macroModeState;
4701
- var lastChange = macroModeState.lastInsertModeChanges;
4702
- if (!macroModeState.isPlaying) {
4703
- while(changeObj) {
4704
- lastChange.expectCursorActivityForChange = true;
4705
- if (changeObj.origin == '+input' || changeObj.origin == 'paste'
4706
- || changeObj.origin === undefined /* only in testing */) {
4707
- var text = changeObj.text.join('\n');
4708
- lastChange.changes.push(text);
4709
- }
4710
- // Change objects may be chained with next.
4711
- changeObj = changeObj.next;
4712
- }
4713
- }
4714
- }
4715
-
4716
- /**
4717
- * Listens for any kind of cursor activity on CodeMirror.
4718
- */
4719
- function onCursorActivity(cm) {
4720
- var vim = cm.state.vim;
4721
- if (vim.insertMode) {
4722
- // Tracking cursor activity in insert mode (for macro support).
4723
- var macroModeState = vimGlobalState.macroModeState;
4724
- if (macroModeState.isPlaying) { return; }
4725
- var lastChange = macroModeState.lastInsertModeChanges;
4726
- if (lastChange.expectCursorActivityForChange) {
4727
- lastChange.expectCursorActivityForChange = false;
4728
- } else {
4729
- // Cursor moved outside the context of an edit. Reset the change.
4730
- lastChange.changes = [];
4731
- }
4732
- } else if (!cm.curOp.isVimOp) {
4733
- handleExternalSelection(cm, vim);
4734
- }
4735
- if (vim.visualMode) {
4736
- updateFakeCursor(cm);
4737
- }
4738
- }
4739
- function updateFakeCursor(cm) {
4740
- var vim = cm.state.vim;
4741
- var from = copyCursor(vim.sel.head);
4742
- var to = offsetCursor(from, 0, 1);
4743
- if (vim.fakeCursor) {
4744
- vim.fakeCursor.clear();
4745
- }
4746
- vim.fakeCursor = cm.markText(from, to, {className: 'cm-animate-fat-cursor'});
4747
- }
4748
- function handleExternalSelection(cm, vim) {
4749
- var anchor = cm.getCursor('anchor');
4750
- var head = cm.getCursor('head');
4751
- // Enter or exit visual mode to match mouse selection.
4752
- if (vim.visualMode && cursorEqual(head, anchor) && lineLength(cm, head.line) > head.ch) {
4753
- exitVisualMode(cm, false);
4754
- } else if (!vim.visualMode && !vim.insertMode && cm.somethingSelected()) {
4755
- vim.visualMode = true;
4756
- vim.visualLine = false;
4757
- CodeMirror.signal(cm, "vim-mode-change", {mode: "visual"});
4758
- }
4759
- if (vim.visualMode) {
4760
- // Bind CodeMirror selection model to vim selection model.
4761
- // Mouse selections are considered visual characterwise.
4762
- var headOffset = !cursorIsBefore(head, anchor) ? -1 : 0;
4763
- var anchorOffset = cursorIsBefore(head, anchor) ? -1 : 0;
4764
- head = offsetCursor(head, 0, headOffset);
4765
- anchor = offsetCursor(anchor, 0, anchorOffset);
4766
- vim.sel = {
4767
- anchor: anchor,
4768
- head: head
4769
- };
4770
- updateMark(cm, vim, '<', cursorMin(head, anchor));
4771
- updateMark(cm, vim, '>', cursorMax(head, anchor));
4772
- } else if (!vim.insertMode) {
4773
- // Reset lastHPos if selection was modified by something outside of vim mode e.g. by mouse.
4774
- vim.lastHPos = cm.getCursor().ch;
4775
- }
4776
- }
4777
-
4778
- /** Wrapper for special keys pressed in insert mode */
4779
- function InsertModeKey(keyName) {
4780
- this.keyName = keyName;
4781
- }
4782
-
4783
- /**
4784
- * Handles raw key down events from the text area.
4785
- * - Should only be active in insert mode.
4786
- * - For recording deletes in insert mode.
4787
- */
4788
- function onKeyEventTargetKeyDown(e) {
4789
- var macroModeState = vimGlobalState.macroModeState;
4790
- var lastChange = macroModeState.lastInsertModeChanges;
4791
- var keyName = CodeMirror.keyName(e);
4792
- function onKeyFound() {
4793
- lastChange.changes.push(new InsertModeKey(keyName));
4794
- return true;
4795
- }
4796
- if (keyName.indexOf('Delete') != -1 || keyName.indexOf('Backspace') != -1) {
4797
- CodeMirror.lookupKey(keyName, 'vim-insert', onKeyFound);
4798
- }
4799
- }
4800
-
4801
- /**
4802
- * Repeats the last edit, which includes exactly 1 command and at most 1
4803
- * insert. Operator and motion commands are read from lastEditInputState,
4804
- * while action commands are read from lastEditActionCommand.
4805
- *
4806
- * If repeatForInsert is true, then the function was called by
4807
- * exitInsertMode to repeat the insert mode changes the user just made. The
4808
- * corresponding enterInsertMode call was made with a count.
4809
- */
4810
- function repeatLastEdit(cm, vim, repeat, repeatForInsert) {
4811
- var macroModeState = vimGlobalState.macroModeState;
4812
- macroModeState.isPlaying = true;
4813
- var isAction = !!vim.lastEditActionCommand;
4814
- var cachedInputState = vim.inputState;
4815
- function repeatCommand() {
4816
- if (isAction) {
4817
- commandDispatcher.processAction(cm, vim, vim.lastEditActionCommand);
4818
- } else {
4819
- commandDispatcher.evalInput(cm, vim);
4820
- }
4821
- }
4822
- function repeatInsert(repeat) {
4823
- if (macroModeState.lastInsertModeChanges.changes.length > 0) {
4824
- // For some reason, repeat cw in desktop VIM does not repeat
4825
- // insert mode changes. Will conform to that behavior.
4826
- repeat = !vim.lastEditActionCommand ? 1 : repeat;
4827
- var changeObject = macroModeState.lastInsertModeChanges;
4828
- repeatInsertModeChanges(cm, changeObject.changes, repeat);
4829
- }
4830
- }
4831
- vim.inputState = vim.lastEditInputState;
4832
- if (isAction && vim.lastEditActionCommand.interlaceInsertRepeat) {
4833
- // o and O repeat have to be interlaced with insert repeats so that the
4834
- // insertions appear on separate lines instead of the last line.
4835
- for (var i = 0; i < repeat; i++) {
4836
- repeatCommand();
4837
- repeatInsert(1);
4838
- }
4839
- } else {
4840
- if (!repeatForInsert) {
4841
- // Hack to get the cursor to end up at the right place. If I is
4842
- // repeated in insert mode repeat, cursor will be 1 insert
4843
- // change set left of where it should be.
4844
- repeatCommand();
4845
- }
4846
- repeatInsert(repeat);
4847
- }
4848
- vim.inputState = cachedInputState;
4849
- if (vim.insertMode && !repeatForInsert) {
4850
- // Don't exit insert mode twice. If repeatForInsert is set, then we
4851
- // were called by an exitInsertMode call lower on the stack.
4852
- exitInsertMode(cm);
4853
- }
4854
- macroModeState.isPlaying = false;
4855
- };
4856
-
4857
- function repeatInsertModeChanges(cm, changes, repeat) {
4858
- function keyHandler(binding) {
4859
- if (typeof binding == 'string') {
4860
- CodeMirror.commands[binding](cm);
4861
- } else {
4862
- binding(cm);
4863
- }
4864
- return true;
4865
- }
4866
- var head = cm.getCursor('head');
4867
- var inVisualBlock = vimGlobalState.macroModeState.lastInsertModeChanges.inVisualBlock;
4868
- if (inVisualBlock) {
4869
- // Set up block selection again for repeating the changes.
4870
- var vim = cm.state.vim;
4871
- var lastSel = vim.lastSelection;
4872
- var offset = getOffset(lastSel.anchor, lastSel.head);
4873
- selectForInsert(cm, head, offset.line + 1);
4874
- repeat = cm.listSelections().length;
4875
- cm.setCursor(head);
4876
- }
4877
- for (var i = 0; i < repeat; i++) {
4878
- if (inVisualBlock) {
4879
- cm.setCursor(offsetCursor(head, i, 0));
4880
- }
4881
- for (var j = 0; j < changes.length; j++) {
4882
- var change = changes[j];
4883
- if (change instanceof InsertModeKey) {
4884
- CodeMirror.lookupKey(change.keyName, 'vim-insert', keyHandler);
4885
- } else {
4886
- var cur = cm.getCursor();
4887
- cm.replaceRange(change, cur, cur);
4888
- }
4889
- }
4890
- }
4891
- if (inVisualBlock) {
4892
- cm.setCursor(offsetCursor(head, 0, 1));
4893
- }
4894
- }
4895
-
4896
- resetVimGlobalState();
4897
- return vimApi;
4898
- };
4899
- // Initialize Vim and make it available as an API.
4900
- CodeMirror.Vim = Vim();
4901
- });