maglev-webtools 0.2.1 → 1.0

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 (296) hide show
  1. data/README.rdoc +38 -29
  2. data/bin/webtools +22 -1
  3. data/lib/web_tools/code_browser.rb +156 -0
  4. data/lib/web_tools/debugger.rb +96 -199
  5. data/lib/web_tools/info.rb +1 -3
  6. data/lib/web_tools/method_list.rb +67 -0
  7. data/lib/web_tools/middleware/debugger.rb +10 -16
  8. data/lib/web_tools/object_log.rb +24 -0
  9. data/lib/web_tools/session_list.rb +119 -0
  10. data/lib/web_tools/shared_page_cache.rb +30 -0
  11. data/lib/web_tools/stat_process.rb +24 -0
  12. data/lib/web_tools/statistics.rb +14 -0
  13. data/lib/web_tools/support/app_model.rb +66 -101
  14. data/lib/web_tools/support/code_browser.rb +88 -86
  15. data/lib/web_tools/support/debugger.rb +303 -0
  16. data/lib/web_tools/support/error_log.rb +33 -0
  17. data/lib/web_tools/support/ruby.rb +60 -121
  18. data/lib/web_tools/support/service_helper.rb +38 -10
  19. data/lib/web_tools/support/smalltalk_extensions.rb +12 -0
  20. data/lib/web_tools/tool.rb +48 -0
  21. data/lib/web_tools/ui.rb +14 -53
  22. data/lib/web_tools/version_report.rb +29 -0
  23. data/lib/web_tools/workspace.rb +77 -0
  24. data/lib/web_tools.rb +14 -4
  25. data/public/CodeBrowser.html +51 -0
  26. data/public/CodeMirror/LICENSE +19 -0
  27. data/public/CodeMirror/css/codemirror.css +67 -0
  28. data/public/CodeMirror/css/default.css +18 -0
  29. data/public/CodeMirror/js/codemirror.js +2131 -0
  30. data/public/CodeMirror/js/ruby.js +195 -0
  31. data/public/CodeMirror/js/smalltalk.js +139 -0
  32. data/public/Debugger.html +28 -0
  33. data/public/MethodList.html +20 -0
  34. data/public/ObjectLog.html +20 -0
  35. data/public/SessionList.html +31 -0
  36. data/public/SharedPageCache.html +78 -0
  37. data/public/Statistics.html +64 -0
  38. data/public/VersionReport.html +50 -0
  39. data/public/Workspace.html +39 -0
  40. data/public/css/CodeBrowser.css +24 -0
  41. data/public/css/Debugger.css +32 -0
  42. data/public/css/MethodList.css +8 -0
  43. data/public/css/ObjectLog.css +5 -0
  44. data/public/css/SessionList.css +9 -0
  45. data/public/css/Statistics.css +24 -0
  46. data/public/{stylesheets → css}/base/images/ui-anim_basic_16x16.gif +0 -0
  47. data/public/{stylesheets → css}/base/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  48. data/public/{stylesheets → css}/base/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  49. data/public/{stylesheets → css}/base/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  50. data/public/{stylesheets → css}/base/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  51. data/public/{stylesheets → css}/base/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  52. data/public/{stylesheets → css}/base/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  53. data/public/{stylesheets → css}/base/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  54. data/public/{stylesheets → css}/base/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  55. data/public/{stylesheets → css}/base/images/ui-icons_222222_256x240.png +0 -0
  56. data/public/{stylesheets → css}/base/images/ui-icons_2e83ff_256x240.png +0 -0
  57. data/public/{stylesheets → css}/base/images/ui-icons_454545_256x240.png +0 -0
  58. data/public/{stylesheets → css}/base/images/ui-icons_888888_256x240.png +0 -0
  59. data/public/{stylesheets → css}/base/images/ui-icons_cd0a0a_256x240.png +0 -0
  60. data/public/{stylesheets → css}/base/jquery.ui.accordion.css +0 -0
  61. data/public/{stylesheets → css}/base/jquery.ui.all.css +0 -0
  62. data/public/{stylesheets → css}/base/jquery.ui.autocomplete.css +0 -0
  63. data/public/{stylesheets → css}/base/jquery.ui.base.css +0 -0
  64. data/public/{stylesheets → css}/base/jquery.ui.button.css +0 -0
  65. data/public/{stylesheets → css}/base/jquery.ui.core.css +0 -0
  66. data/public/{stylesheets → css}/base/jquery.ui.datepicker.css +0 -0
  67. data/public/{stylesheets → css}/base/jquery.ui.dialog.css +0 -0
  68. data/public/{stylesheets → css}/base/jquery.ui.progressbar.css +0 -0
  69. data/public/{stylesheets → css}/base/jquery.ui.resizable.css +0 -0
  70. data/public/{stylesheets → css}/base/jquery.ui.selectable.css +0 -0
  71. data/public/{stylesheets → css}/base/jquery.ui.slider.css +0 -0
  72. data/public/{stylesheets → css}/base/jquery.ui.tabs.css +0 -0
  73. data/public/{stylesheets → css}/base/jquery.ui.theme.css +0 -0
  74. data/public/css/editor.css +25 -0
  75. data/public/css/jjmenu.css +33 -0
  76. data/public/css/menuitem.gif +0 -0
  77. data/public/css/more.gif +0 -0
  78. data/public/{stylesheets → css}/reset.css +1 -1
  79. data/public/css/smoothness/images/ui-anim_basic_16x16.gif +0 -0
  80. data/public/css/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  81. data/public/css/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  82. data/public/css/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  83. data/public/css/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  84. data/public/css/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  85. data/public/css/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  86. data/public/css/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  87. data/public/css/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  88. data/public/css/smoothness/images/ui-icons_222222_256x240.png +0 -0
  89. data/public/css/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
  90. data/public/css/smoothness/images/ui-icons_454545_256x240.png +0 -0
  91. data/public/css/smoothness/images/ui-icons_888888_256x240.png +0 -0
  92. data/public/css/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
  93. data/public/css/smoothness/jquery-ui.css +489 -0
  94. data/public/css/ui-lightness/images/ui-anim_basic_16x16.gif +0 -0
  95. data/public/css/ui-lightness/images/ui-bg_diagonals-thick_18_b81900_40x40.png +0 -0
  96. data/public/css/ui-lightness/images/ui-bg_diagonals-thick_20_666666_40x40.png +0 -0
  97. data/public/css/ui-lightness/images/ui-bg_flat_10_000000_40x100.png +0 -0
  98. data/public/css/ui-lightness/images/ui-bg_glass_100_f6f6f6_1x400.png +0 -0
  99. data/public/css/ui-lightness/images/ui-bg_glass_100_fdf5ce_1x400.png +0 -0
  100. data/public/css/ui-lightness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  101. data/public/css/ui-lightness/images/ui-bg_gloss-wave_35_f6a828_500x100.png +0 -0
  102. data/public/css/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
  103. data/public/css/ui-lightness/images/ui-bg_highlight-soft_75_ffe45c_1x100.png +0 -0
  104. data/public/css/ui-lightness/images/ui-icons_222222_256x240.png +0 -0
  105. data/public/css/ui-lightness/images/ui-icons_228ef1_256x240.png +0 -0
  106. data/public/css/ui-lightness/images/ui-icons_ef8c08_256x240.png +0 -0
  107. data/public/css/ui-lightness/images/ui-icons_ffd27a_256x240.png +0 -0
  108. data/public/css/ui-lightness/images/ui-icons_ffffff_256x240.png +0 -0
  109. data/public/css/ui-lightness/jquery-ui.css +489 -0
  110. data/public/css/webtools.css +65 -0
  111. data/public/favicon.ico +0 -0
  112. data/public/flot/API.txt +1201 -0
  113. data/public/flot/FAQ.txt +76 -0
  114. data/public/flot/LICENSE.txt +22 -0
  115. data/public/flot/Makefile +9 -0
  116. data/public/flot/NEWS.txt +508 -0
  117. data/public/flot/PLUGINS.txt +137 -0
  118. data/public/flot/README.txt +90 -0
  119. data/public/flot/examples/ajax.html +143 -0
  120. data/public/flot/examples/annotating.html +75 -0
  121. data/public/flot/examples/arrow-down.gif +0 -0
  122. data/public/flot/examples/arrow-left.gif +0 -0
  123. data/public/flot/examples/arrow-right.gif +0 -0
  124. data/public/flot/examples/arrow-up.gif +0 -0
  125. data/public/flot/examples/basic.html +38 -0
  126. data/public/flot/examples/data-eu-gdp-growth-1.json +4 -0
  127. data/public/flot/examples/data-eu-gdp-growth-2.json +4 -0
  128. data/public/flot/examples/data-eu-gdp-growth-3.json +4 -0
  129. data/public/flot/examples/data-eu-gdp-growth-4.json +4 -0
  130. data/public/flot/examples/data-eu-gdp-growth-5.json +4 -0
  131. data/public/flot/examples/data-eu-gdp-growth.json +4 -0
  132. data/public/flot/examples/data-japan-gdp-growth.json +4 -0
  133. data/public/flot/examples/data-usa-gdp-growth.json +4 -0
  134. data/public/flot/examples/graph-types.html +75 -0
  135. data/public/flot/examples/hs-2004-27-a-large_web.jpg +0 -0
  136. data/public/flot/examples/image.html +45 -0
  137. data/public/flot/examples/index.html +44 -0
  138. data/public/flot/examples/interacting-axes.html +97 -0
  139. data/public/flot/examples/interacting.html +93 -0
  140. data/public/flot/examples/layout.css +6 -0
  141. data/public/flot/examples/multiple-axes.html +60 -0
  142. data/public/flot/examples/navigate.html +118 -0
  143. data/public/flot/examples/percentiles.html +57 -0
  144. data/public/flot/examples/pie.html +756 -0
  145. data/public/flot/examples/realtime.html +83 -0
  146. data/public/flot/examples/resize.html +61 -0
  147. data/public/flot/examples/selection.html +114 -0
  148. data/public/flot/examples/setting-options.html +61 -0
  149. data/public/flot/examples/stacking.html +77 -0
  150. data/public/flot/examples/symbols.html +49 -0
  151. data/public/flot/examples/thresholding.html +54 -0
  152. data/public/flot/examples/time.html +71 -0
  153. data/public/flot/examples/tracking.html +95 -0
  154. data/public/flot/examples/turning-series.html +98 -0
  155. data/public/flot/examples/visitors.html +90 -0
  156. data/public/flot/examples/zooming.html +98 -0
  157. data/public/flot/excanvas.js +1427 -0
  158. data/public/flot/excanvas.min.js +1 -0
  159. data/public/flot/jquery.colorhelpers.js +179 -0
  160. data/public/flot/jquery.colorhelpers.min.js +1 -0
  161. data/public/flot/jquery.flot.crosshair.js +167 -0
  162. data/public/flot/jquery.flot.crosshair.min.js +1 -0
  163. data/public/flot/jquery.flot.fillbetween.js +183 -0
  164. data/public/flot/jquery.flot.fillbetween.min.js +1 -0
  165. data/public/flot/jquery.flot.image.js +238 -0
  166. data/public/flot/jquery.flot.image.min.js +1 -0
  167. data/public/flot/jquery.flot.js +2599 -0
  168. data/public/flot/jquery.flot.min.js +6 -0
  169. data/public/flot/jquery.flot.navigate.js +336 -0
  170. data/public/flot/jquery.flot.navigate.min.js +1 -0
  171. data/public/flot/jquery.flot.pie.js +750 -0
  172. data/public/flot/jquery.flot.pie.min.js +1 -0
  173. data/public/flot/jquery.flot.resize.js +60 -0
  174. data/public/flot/jquery.flot.resize.min.js +1 -0
  175. data/public/flot/jquery.flot.selection.js +344 -0
  176. data/public/flot/jquery.flot.selection.min.js +1 -0
  177. data/public/flot/jquery.flot.stack.js +184 -0
  178. data/public/flot/jquery.flot.stack.min.js +1 -0
  179. data/public/flot/jquery.flot.symbol.js +70 -0
  180. data/public/flot/jquery.flot.symbol.min.js +1 -0
  181. data/public/flot/jquery.flot.threshold.js +103 -0
  182. data/public/flot/jquery.flot.threshold.min.js +1 -0
  183. data/public/flot/jquery.js +8316 -0
  184. data/public/flot/jquery.min.js +23 -0
  185. data/public/genPacking.pl +30 -0
  186. data/public/index.html +28 -0
  187. data/public/install.tpz +66 -0
  188. data/public/installAndRun.tpz +5 -0
  189. data/public/jsTree/jquery.jstree.js +3510 -0
  190. data/public/jsTree/themes/default/d.gif +0 -0
  191. data/public/jsTree/themes/default/d.png +0 -0
  192. data/public/jsTree/themes/default/style.css +73 -0
  193. data/public/jsTree/themes/default/throbber.gif +0 -0
  194. data/public/readMe.txt +9 -0
  195. data/public/scripts/CodeBrowser.js +645 -0
  196. data/public/scripts/Debugger.js +134 -0
  197. data/public/scripts/MethodList.js +72 -0
  198. data/public/scripts/ObjectLog.js +59 -0
  199. data/public/scripts/SessionList.js +164 -0
  200. data/public/scripts/Statistics.js +266 -0
  201. data/public/scripts/editor.js +430 -0
  202. data/public/scripts/jjmenu.js +375 -0
  203. data/public/scripts/jquery-ui.js +1012 -0
  204. data/public/scripts/jquery.js +154 -0
  205. data/public/scripts/webtools.js +374 -0
  206. data/public/src/CodeBrowser.gs +416 -0
  207. data/public/src/Debugger.gs +132 -0
  208. data/public/src/MethodList.gs +130 -0
  209. data/public/src/ObjectLog.gs +73 -0
  210. data/public/src/Server.gs +544 -0
  211. data/public/src/SessionList.gs +172 -0
  212. data/public/src/SharedPageCache.gs +66 -0
  213. data/public/src/StatProcess.gs +280 -0
  214. data/public/src/StatProcessType.gs +242 -0
  215. data/public/src/StatStatistic.gs +381 -0
  216. data/public/src/Statistics.gs +222 -0
  217. data/public/src/Statmonitor.gs +490 -0
  218. data/public/src/Tool.gs +86 -0
  219. data/public/src/VersionReport.gs +52 -0
  220. data/public/src/Workspace.gs +163 -0
  221. data/views/debugger.rhtml +6 -9
  222. metadata +344 -243
  223. data/lib/web_tools/#debugger.rb# +0 -212
  224. data/lib/web_tools/browser.rb +0 -45
  225. data/public/images/favicon.ico +0 -0
  226. data/public/javascript/CodeMirror/LICENSE +0 -23
  227. data/public/javascript/CodeMirror/css/Smalltalk.css +0 -34
  228. data/public/javascript/CodeMirror/js/codemirror.js +0 -582
  229. data/public/javascript/CodeMirror/js/editor.js +0 -1671
  230. data/public/javascript/CodeMirror/js/highlight.js +0 -68
  231. data/public/javascript/CodeMirror/js/parseSmalltalk.js +0 -126
  232. data/public/javascript/CodeMirror/js/parsedummy.js +0 -32
  233. data/public/javascript/CodeMirror/js/select.js +0 -699
  234. data/public/javascript/CodeMirror/js/stringstream.js +0 -159
  235. data/public/javascript/CodeMirror/js/tokenize.js +0 -57
  236. data/public/javascript/CodeMirror/js/undo.js +0 -413
  237. data/public/javascript/CodeMirror/js/util.js +0 -133
  238. data/public/javascript/CodeMirror/testSmalltalkParser.html +0 -116
  239. data/public/javascript/ace/ace-uncompressed.js +0 -17299
  240. data/public/javascript/ace/ace.js +0 -1
  241. data/public/javascript/ace/keybinding-emacs.js +0 -1
  242. data/public/javascript/ace/keybinding-vim.js +0 -1
  243. data/public/javascript/ace/mode-c_cpp.js +0 -1
  244. data/public/javascript/ace/mode-clojure.js +0 -1
  245. data/public/javascript/ace/mode-coffee.js +0 -1
  246. data/public/javascript/ace/mode-csharp.js +0 -1
  247. data/public/javascript/ace/mode-css.js +0 -1
  248. data/public/javascript/ace/mode-groovy.js +0 -1
  249. data/public/javascript/ace/mode-html.js +0 -1
  250. data/public/javascript/ace/mode-java.js +0 -1
  251. data/public/javascript/ace/mode-javascript.js +0 -1
  252. data/public/javascript/ace/mode-json.js +0 -1
  253. data/public/javascript/ace/mode-lua.js +0 -1
  254. data/public/javascript/ace/mode-markdown.js +0 -1
  255. data/public/javascript/ace/mode-ocaml.js +0 -1
  256. data/public/javascript/ace/mode-perl.js +0 -1
  257. data/public/javascript/ace/mode-php.js +0 -1
  258. data/public/javascript/ace/mode-python.js +0 -1
  259. data/public/javascript/ace/mode-ruby.js +0 -1
  260. data/public/javascript/ace/mode-scad.js +0 -1
  261. data/public/javascript/ace/mode-scala.js +0 -1
  262. data/public/javascript/ace/mode-scss.js +0 -1
  263. data/public/javascript/ace/mode-svg.js +0 -1
  264. data/public/javascript/ace/mode-textile.js +0 -1
  265. data/public/javascript/ace/mode-xml.js +0 -1
  266. data/public/javascript/ace/theme-clouds.js +0 -1
  267. data/public/javascript/ace/theme-clouds_midnight.js +0 -1
  268. data/public/javascript/ace/theme-cobalt.js +0 -1
  269. data/public/javascript/ace/theme-crimson_editor.js +0 -1
  270. data/public/javascript/ace/theme-dawn.js +0 -1
  271. data/public/javascript/ace/theme-eclipse.js +0 -1
  272. data/public/javascript/ace/theme-idle_fingers.js +0 -1
  273. data/public/javascript/ace/theme-kr_theme.js +0 -1
  274. data/public/javascript/ace/theme-merbivore.js +0 -1
  275. data/public/javascript/ace/theme-merbivore_soft.js +0 -1
  276. data/public/javascript/ace/theme-mono_industrial.js +0 -1
  277. data/public/javascript/ace/theme-monokai.js +0 -1
  278. data/public/javascript/ace/theme-pastel_on_dark.js +0 -1
  279. data/public/javascript/ace/theme-solarized_dark.js +0 -1
  280. data/public/javascript/ace/theme-solarized_light.js +0 -1
  281. data/public/javascript/ace/theme-textmate.js +0 -1
  282. data/public/javascript/ace/theme-twilight.js +0 -1
  283. data/public/javascript/ace/theme-vibrant_ink.js +0 -1
  284. data/public/javascript/ace/worker-coffee.js +0 -1
  285. data/public/javascript/ace/worker-css.js +0 -1
  286. data/public/javascript/ace/worker-javascript.js +0 -1
  287. data/public/javascript/webtools/browser.js +0 -260
  288. data/public/javascript/webtools/debugger.coffee +0 -286
  289. data/public/javascript/webtools/debugger.js +0 -366
  290. data/public/javascript/webtools/sessions.coffee +0 -17
  291. data/public/javascript/webtools/sessions.js +0 -27
  292. data/public/javascript/webtools/version.coffee +0 -14
  293. data/public/javascript/webtools/version.js +0 -20
  294. data/public/stylesheets/jquery.contextMenu.css +0 -62
  295. data/public/stylesheets/webtools.css +0 -53
  296. data/public/test.html +0 -47
@@ -0,0 +1,77 @@
1
+ require 'sinatra/base'
2
+ require 'web_tools'
3
+
4
+ module WebTools
5
+ class Workspace < Tool
6
+ def self.description
7
+ 'Code workspace'
8
+ end
9
+
10
+ post '/deleteProcess' do
11
+ return {} unless params["oop"]
12
+ Support::ErrorLog.delete(system.object_by_id(params["oop"].to_i))
13
+ json({})
14
+ end
15
+
16
+ post '/evaluate' do
17
+ response = nil
18
+ debug_on_exception do
19
+ run_evaluation params["text"]
20
+ end
21
+ end
22
+
23
+ post '/saveMethod' do
24
+ klass = reflect(Object).constant(params["klass"]).value
25
+ source = params["source"]
26
+ if params["isMeta"] == "true"
27
+ unless source =~ /^\s*def\s+self\./
28
+ # Compiling sth for the singleton_class without a self., so we
29
+ # compile it on the singleton_class object
30
+ klass = klass.singleton_class
31
+ end
32
+ end
33
+ begin
34
+ klass.method(params["selector"]).source = source
35
+ return json({"selector" => params["selector"], "warnings" => nil})
36
+ rescue SyntaxError => e
37
+ # Magic values taken from a Smalltalk CompileError
38
+ return json("compileError" => [[1031, 1, e.message, nil, nil]])
39
+ end
40
+ end
41
+
42
+ # Run block in a separate thread, which gets suspended if an
43
+ # error occurs.
44
+ def debug_on_exception
45
+ response = nil
46
+ client = Thread.start do
47
+ begin
48
+ response = yield
49
+ rescue Exception => e
50
+ entry = Support::ErrorLog.add :thread => Thread.current, :exception => e
51
+ response = json("errorType" => entry.exception.class.inspect,
52
+ "description" => entry.exception.message,
53
+ "oop" => entry.object_id)
54
+ Thread.stop
55
+ end
56
+ end
57
+ sleep 0.2 until client.stop?
58
+ response
59
+ end
60
+
61
+ def run_evaluation(text)
62
+ value = eval(params["text"])
63
+ result = { "klass" => value.class.inspect,
64
+ "string" => value.inspect }
65
+ if value.is_a? Module
66
+ result["dict"] = ""
67
+ result["name"] = value.inspect
68
+ result["cat"] = ""
69
+ end
70
+ json(result)
71
+ rescue SyntaxError => e
72
+ json("errorType" => "compileError",
73
+ "errorDetails" => [[1031, 1, e.message, nil, nil]])
74
+ end
75
+
76
+ end
77
+ end
data/lib/web_tools.rb CHANGED
@@ -3,8 +3,18 @@ module WebTools
3
3
  module Middleware; end
4
4
 
5
5
  path = File.expand_path("../web_tools", __FILE__)
6
- autoload :Browser, File.join(path, "browser.rb")
7
- autoload :Debugger, File.join(path, "debugger.rb")
8
- autoload :Info, File.join(path, "info.rb")
9
- autoload :UI, File.join(path, "ui.rb")
6
+ require File.join(path, "tool.rb")
7
+ require File.join(path, "code_browser.rb")
8
+ require File.join(path, "debugger.rb")
9
+ require File.join(path, "method_list.rb")
10
+ require File.join(path, "workspace.rb")
11
+ require File.join(path, "ui.rb")
12
+
13
+ if defined? Maglev
14
+ require File.join(path, "shared_page_cache.rb")
15
+ require File.join(path, "statistics.rb")
16
+ require File.join(path, "version_report.rb")
17
+ require File.join(path, "object_log.rb")
18
+ require File.join(path, "session_list.rb")
19
+ end
10
20
  end
@@ -0,0 +1,51 @@
1
+ <script type="text/javascript" language="javascript">
2
+ GemStone.runJs('scripts/CodeBrowser.js');
3
+ </script>
4
+
5
+ <div id='newTab' class='browserPanel hidden'>
6
+ <div class='navigation'>
7
+ <div class='dictPckgTabs ui-widget-content ui-corner-all'>
8
+ <ul>
9
+ <li><a title='Show classes organized by SymbolDictionary'>Dicts</a></li>
10
+ <li><a title='Show classes organized by Monticello Package'>Pkgs</a></li>
11
+ </ul>
12
+ <div class='dictList divList' tabindex=0></div>
13
+ <div class='pckgList divList' tabindex=0></div>
14
+ </div>
15
+ <div class='classCats divList ui-widget-content ui-corner-all'></div>
16
+ <div class='classesTab ui-widget-content ui-corner-all'>
17
+ <ul>
18
+ <li><a title='Show classes in alphabetical order'>Classes</a></li>
19
+ <li><a title='Show classes in hierarichal order'>Hierarchy</a></li>
20
+ </ul>
21
+ <div class='classList divList' tabindex=0></div>
22
+ <div class='classTree divList'></div>
23
+ </div>
24
+ <div class='methodFilter ui-widget-content ui-corner-all'>
25
+ <div class='methodFilterList divList' tabindex=0></div>
26
+ <div class='catsVariablesTabs tabs-empty'>
27
+ <ul>
28
+ <li><a title='Group methods by category'>Categories</a></li>
29
+ <li><a title='Group methods by variable references'>Vars</a></li>
30
+ </ul>
31
+ <div class='categoriesPanel'></div>
32
+ <div class='variablesPanel'></div>
33
+ </div>
34
+ <div class='instanceClassTabs tabs-bottom tabs-empty ui-corner-bottom'>
35
+ <ul>
36
+ <li><a title='Show instance-side methods'>Instance</a></li>
37
+ <li><a title='Show class-side methods'>Class</a></li>
38
+ </ul>
39
+ <div class='instancePanel'>Instance</div>
40
+ <div class='classPanel'>Class</div>
41
+ </div>
42
+ </div>
43
+ <div class='methodDiv'>
44
+ <select class='superList'></select>
45
+ <div class='methodList divList ui-widget-content ui-corner-all' tabindex=0></div>
46
+ <select class='implList'></select>
47
+ </div>
48
+ </div>
49
+ <div class='editArea' tabindex=0>
50
+ </div>
51
+ </div>
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2011 by Marijn Haverbeke <marijnh@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,67 @@
1
+ .CodeMirror {
2
+ line-height: 1em;
3
+ font-family: monospace;
4
+ }
5
+
6
+ .CodeMirror-scroll {
7
+ overflow: auto;
8
+ height: 300px;
9
+ /* This is needed to prevent an IE[67] bug where the scrolled content
10
+ is visible outside of the scrolling box. */
11
+ position: relative;
12
+ }
13
+
14
+ .CodeMirror-gutter {
15
+ position: absolute; left: 0; top: 0;
16
+ background-color: #f7f7f7;
17
+ border-right: 1px solid #eee;
18
+ min-width: 2em;
19
+ height: 100%;
20
+ }
21
+ .CodeMirror-gutter-text {
22
+ color: #aaa;
23
+ text-align: right;
24
+ padding: .4em .2em .4em .4em;
25
+ }
26
+ .CodeMirror-lines {
27
+ padding: .4em;
28
+ }
29
+
30
+ .CodeMirror pre {
31
+ -moz-border-radius: 0;
32
+ -webkit-border-radius: 0;
33
+ -o-border-radius: 0;
34
+ border-radius: 0;
35
+ border-width: 0; margin: 0; padding: 0; background: transparent;
36
+ font-family: inherit;
37
+ font-size: inherit;
38
+ padding: 0; margin: 0;
39
+ white-space: pre;
40
+ word-wrap: normal;
41
+ }
42
+
43
+ .CodeMirror textarea {
44
+ font-family: inherit !important;
45
+ font-size: inherit !important;
46
+ }
47
+
48
+ .CodeMirror-cursor {
49
+ z-index: 10;
50
+ position: absolute;
51
+ visibility: hidden;
52
+ border-left: 1px solid black !important;
53
+ }
54
+ .CodeMirror-focused .CodeMirror-cursor {
55
+ visibility: visible;
56
+ }
57
+
58
+ span.CodeMirror-selected {
59
+ background: #ccc !important;
60
+ color: HighlightText !important;
61
+ }
62
+ .CodeMirror-focused span.CodeMirror-selected {
63
+ background: Highlight !important;
64
+ }
65
+
66
+ .CodeMirror-matchingbracket {color: #0f0 !important;}
67
+ .CodeMirror-nonmatchingbracket {color: #f22 !important;}
@@ -0,0 +1,18 @@
1
+ .cm-s-default span.cm-keyword {color: #708;}
2
+ .cm-s-default span.cm-atom {color: #219;}
3
+ .cm-s-default span.cm-number {color: #164;}
4
+ .cm-s-default span.cm-def {color: #00f;}
5
+ .cm-s-default span.cm-variable {color: black;}
6
+ .cm-s-default span.cm-variable-2 {color: #05a;}
7
+ .cm-s-default span.cm-variable-3 {color: #0a5;}
8
+ .cm-s-default span.cm-property {color: black;}
9
+ .cm-s-default span.cm-operator {color: black;}
10
+ .cm-s-default span.cm-comment {color: #a50;}
11
+ .cm-s-default span.cm-string {color: #a11;}
12
+ .cm-s-default span.cm-meta {color: #555;}
13
+ .cm-s-default span.cm-error {color: #f00;}
14
+ .cm-s-default span.cm-qualifier {color: #555;}
15
+ .cm-s-default span.cm-builtin {color: #30a;}
16
+ .cm-s-default span.cm-bracket {color: #cc7;}
17
+ .cm-s-default span.cm-tag {color: #170;}
18
+ .cm-s-default span.cm-attribute {color: #00c;}
@@ -0,0 +1,2131 @@
1
+ // All functions that need access to the editor's state live inside
2
+ // the CodeMirror function. Below that, at the bottom of the file,
3
+ // some utilities are defined.
4
+
5
+ // CodeMirror is the only global var we claim
6
+ var CodeMirror = (function() {
7
+ // This is the function that produces an editor instance. It's
8
+ // closure is used to store the editor state.
9
+ function CodeMirror(place, givenOptions) {
10
+ // Determine effective options based on given values and defaults.
11
+ var options = {}, defaults = CodeMirror.defaults;
12
+ for (var opt in defaults)
13
+ if (defaults.hasOwnProperty(opt))
14
+ options[opt] = (givenOptions && givenOptions.hasOwnProperty(opt) ? givenOptions : defaults)[opt];
15
+
16
+ var targetDocument = options["document"];
17
+ // The element in which the editor lives.
18
+ var wrapper = targetDocument.createElement("div");
19
+ wrapper.className = "CodeMirror";
20
+ // This mess creates the base DOM structure for the editor.
21
+ wrapper.innerHTML =
22
+ '<div style="overflow: hidden; position: relative; width: 1px; height: 0px;">' + // Wraps and hides input textarea
23
+ '<textarea style="position: absolute; width: 2px;" wrap="off"></textarea></div>' +
24
+ '<div class="CodeMirror-scroll cm-s-' + options.theme + '">' +
25
+ '<div style="position: relative">' + // Set to the height of the text, causes scrolling
26
+ '<div style="position: absolute; height: 0; width: 0; overflow: hidden;"></div>' +
27
+ '<div style="position: relative">' + // Moved around its parent to cover visible view
28
+ '<div class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div>' +
29
+ // Provides positioning relative to (visible) text origin
30
+ '<div class="CodeMirror-lines"><div style="position: relative">' +
31
+ '<pre class="CodeMirror-cursor">&#160;</pre>' + // Absolutely positioned blinky cursor
32
+ '<div></div>' + // This DIV contains the actual code
33
+ '</div></div></div></div></div>';
34
+ if (place.appendChild) place.appendChild(wrapper); else place(wrapper);
35
+ // I've never seen more elegant code in my life.
36
+ var inputDiv = wrapper.firstChild, input = inputDiv.firstChild,
37
+ scroller = wrapper.lastChild, code = scroller.firstChild,
38
+ measure = code.firstChild, mover = measure.nextSibling,
39
+ gutter = mover.firstChild, gutterText = gutter.firstChild,
40
+ lineSpace = gutter.nextSibling.firstChild,
41
+ cursor = lineSpace.firstChild, lineDiv = cursor.nextSibling;
42
+ if (options.tabindex != null) input.tabindex = options.tabindex;
43
+ if (!options.gutter && !options.lineNumbers) gutter.style.display = "none";
44
+
45
+ // Delayed object wrap timeouts, making sure only one is active. blinker holds an interval.
46
+ var poll = new Delayed(), highlight = new Delayed(), blinker;
47
+
48
+ // mode holds a mode API object. lines an array of Line objects
49
+ // (see Line constructor), work an array of lines that should be
50
+ // parsed, and history the undo history (instance of History
51
+ // constructor).
52
+ var mode, lines = [new Line("")], work, history = new History(), focused;
53
+ loadMode();
54
+ // The selection. These are always maintained to point at valid
55
+ // positions. Inverted is used to remember that the user is
56
+ // selecting bottom-to-top.
57
+ var sel = {from: {line: 0, ch: 0}, to: {line: 0, ch: 0}, inverted: false};
58
+ // Selection-related flags. shiftSelecting obviously tracks
59
+ // whether the user is holding shift. reducedSelection is a hack
60
+ // to get around the fact that we can't create inverted
61
+ // selections. See below.
62
+ var shiftSelecting, reducedSelection, lastDoubleClick;
63
+ // Variables used by startOperation/endOperation to track what
64
+ // happened during the operation.
65
+ var updateInput, changes, textChanged, selectionChanged, leaveInputAlone;
66
+ // Current visible range (may be bigger than the view window).
67
+ var showingFrom = 0, showingTo = 0, lastHeight = 0, curKeyId = null;
68
+ // editing will hold an object describing the things we put in the
69
+ // textarea, to help figure out whether something changed.
70
+ // bracketHighlighted is used to remember that a backet has been
71
+ // marked.
72
+ var editing, bracketHighlighted;
73
+ // Tracks the maximum line length so that the horizontal scrollbar
74
+ // can be kept static when scrolling.
75
+ var maxLine = "";
76
+
77
+ // Initialize the content.
78
+ operation(function(){setValue(options.value || ""); updateInput = false;})();
79
+
80
+ // Register our event handlers.
81
+ connect(scroller, "mousedown", operation(onMouseDown));
82
+ // Gecko browsers fire contextmenu *after* opening the menu, at
83
+ // which point we can't mess with it anymore. Context menu is
84
+ // handled in onMouseDown for Gecko.
85
+ if (!gecko) connect(scroller, "contextmenu", onContextMenu);
86
+ connect(code, "dblclick", operation(onDblClick));
87
+ connect(scroller, "scroll", function() {updateDisplay([]); if (options.onScroll) options.onScroll(instance);});
88
+ connect(window, "resize", function() {updateDisplay(true);});
89
+ connect(input, "keyup", operation(onKeyUp));
90
+ connect(input, "keydown", operation(onKeyDown));
91
+ connect(input, "keypress", operation(onKeyPress));
92
+ connect(input, "focus", onFocus);
93
+ connect(input, "blur", onBlur);
94
+
95
+ connect(scroller, "dragenter", function(e){e.stop();});
96
+ connect(scroller, "dragover", function(e){e.stop();});
97
+ connect(scroller, "drop", operation(onDrop));
98
+ connect(scroller, "paste", function(){focusInput(); fastPoll();});
99
+ connect(input, "paste", function(){fastPoll();});
100
+ connect(input, "cut", function(){fastPoll();});
101
+
102
+ // IE throws unspecified error in certain cases, when
103
+ // trying to access activeElement before onload
104
+ var hasFocus; try { hasFocus = (targetDocument.activeElement == input); } catch(e) { }
105
+ if (hasFocus) setTimeout(onFocus, 20);
106
+ else onBlur();
107
+
108
+ function isLine(l) {return l >= 0 && l < lines.length;}
109
+ // The instance object that we'll return. Mostly calls out to
110
+ // local functions in the CodeMirror function. Some do some extra
111
+ // range checking and/or clipping. operation is used to wrap the
112
+ // call so that changes it makes are tracked, and the display is
113
+ // updated afterwards.
114
+ var instance = {
115
+ getValue: getValue,
116
+ setValue: operation(setValue),
117
+ getSelection: getSelection,
118
+ replaceSelection: operation(replaceSelection),
119
+ focus: function(){focusInput(); onFocus(); fastPoll();},
120
+ setOption: function(option, value) {
121
+ options[option] = value;
122
+ if (option == "lineNumbers" || option == "gutter") gutterChanged();
123
+ else if (option == "mode" || option == "indentUnit") loadMode();
124
+ else if (option == "readOnly" && value == "nocursor") input.blur();
125
+ else if (option == "theme") scroller.className = scroller.className.replace(/cm-s-\w+/, "cm-s-" + value);
126
+ },
127
+ getOption: function(option) {return options[option];},
128
+ undo: operation(undo),
129
+ redo: operation(redo),
130
+ indentLine: operation(function(n) {if (isLine(n)) indentLine(n, "smart");}),
131
+ historySize: function() {return {undo: history.done.length, redo: history.undone.length};},
132
+ matchBrackets: operation(function(){matchBrackets(true);}),
133
+ getTokenAt: function(pos) {
134
+ pos = clipPos(pos);
135
+ return lines[pos.line].getTokenAt(mode, getStateBefore(pos.line), pos.ch);
136
+ },
137
+ getStateAfter: function(line) {
138
+ line = clipLine(line == null ? lines.length - 1: line);
139
+ return getStateBefore(line + 1);
140
+ },
141
+ cursorCoords: function(start){
142
+ if (start == null) start = sel.inverted;
143
+ return pageCoords(start ? sel.from : sel.to);
144
+ },
145
+ charCoords: function(pos){return pageCoords(clipPos(pos));},
146
+ coordsChar: function(coords) {
147
+ var off = eltOffset(lineSpace);
148
+ var line = clipLine(Math.min(lines.length - 1, showingFrom + Math.floor((coords.y - off.top) / lineHeight())));
149
+ return clipPos({line: line, ch: charFromX(clipLine(line), coords.x - off.left)});
150
+ },
151
+ getSearchCursor: function(query, pos, caseFold) {return new SearchCursor(query, pos, caseFold);},
152
+ markText: operation(function(a, b, c){return operation(markText(a, b, c));}),
153
+ setMarker: addGutterMarker,
154
+ clearMarker: removeGutterMarker,
155
+ setLineClass: operation(setLineClass),
156
+ lineInfo: lineInfo,
157
+ addWidget: function(pos, node, scroll, where) {
158
+ pos = localCoords(clipPos(pos));
159
+ var top = pos.yBot, left = pos.x;
160
+ node.style.position = "absolute";
161
+ code.appendChild(node);
162
+ node.style.left = left + "px";
163
+ if (where == "over") top = pos.y;
164
+ else if (where == "fit") {
165
+ var vspace = lines.length * lineHeight(), hspace = code.clientWidth - paddingLeft();
166
+ top = pos.y + node.offsetHeight > vspace ? vspace - node.offsetHeight : pos.y;
167
+ if (left + node.offsetWidth > hspace) left = hspace - node.offsetWidth;
168
+ }
169
+ node.style.top = (top + paddingTop()) + "px";
170
+ node.style.left = (left + paddingLeft()) + "px";
171
+ if (scroll)
172
+ scrollIntoView(left, top, left + node.offsetWidth, top + node.offsetHeight);
173
+ },
174
+
175
+ lineCount: function() {return lines.length;},
176
+ getCursor: function(start) {
177
+ if (start == null) start = sel.inverted;
178
+ return copyPos(start ? sel.from : sel.to);
179
+ },
180
+ somethingSelected: function() {return !posEq(sel.from, sel.to);},
181
+ setCursor: operation(function(line, ch) {
182
+ if (ch == null && typeof line.line == "number") setCursor(line.line, line.ch);
183
+ else setCursor(line, ch);
184
+ }),
185
+ setSelection: operation(function(from, to) {setSelection(clipPos(from), clipPos(to || from));}),
186
+ getLine: function(line) {if (isLine(line)) return lines[line].text;},
187
+ setLine: operation(function(line, text) {
188
+ if (isLine(line)) replaceRange(text, {line: line, ch: 0}, {line: line, ch: lines[line].text.length});
189
+ }),
190
+ removeLine: operation(function(line) {
191
+ if (isLine(line)) replaceRange("", {line: line, ch: 0}, clipPos({line: line+1, ch: 0}));
192
+ }),
193
+ replaceRange: operation(replaceRange),
194
+ getRange: function(from, to) {return getRange(clipPos(from), clipPos(to));},
195
+
196
+ operation: function(f){return operation(f)();},
197
+ refresh: function(){updateDisplay(true);},
198
+ getInputField: function(){return input;},
199
+ getWrapperElement: function(){return wrapper;},
200
+ getScrollerElement: function(){return scroller;}
201
+ };
202
+
203
+ function setValue(code) {
204
+ history = null;
205
+ var top = {line: 0, ch: 0};
206
+ updateLines(top, {line: lines.length - 1, ch: lines[lines.length-1].text.length},
207
+ splitLines(code), top, top);
208
+ history = new History();
209
+ }
210
+ function getValue(code) {
211
+ var text = [];
212
+ for (var i = 0, l = lines.length; i < l; ++i)
213
+ text.push(lines[i].text);
214
+ return text.join("\n");
215
+ }
216
+
217
+ function onMouseDown(e) {
218
+ var ld = lastDoubleClick; lastDoubleClick = null;
219
+ // First, see if this is a click in the gutter
220
+ for (var n = e.target(); n != wrapper; n = n.parentNode)
221
+ if (n.parentNode == gutterText) {
222
+ if (options.onGutterClick)
223
+ options.onGutterClick(instance, indexOf(gutterText.childNodes, n) + showingFrom);
224
+ return e.stop();
225
+ }
226
+
227
+ var start = posFromMouse(e);
228
+ switch (e.button()) {
229
+ case 3:
230
+ if (gecko && !mac) onContextMenu(e);
231
+ return;
232
+ case 2:
233
+ if (start) setCursor(start.line, start.ch, true);
234
+ return;
235
+ }
236
+ // For button 1, if it was clicked inside the editor
237
+ // (posFromMouse returning non-null), we have to adjust the
238
+ // selection.
239
+ if (!start) {if (e.target() == scroller) e.stop(); return;}
240
+
241
+ if (!focused) onFocus();
242
+ e.stop();
243
+ if (ld && +new Date - ld < 400) return selectLine(start.line);
244
+
245
+ setCursor(start.line, start.ch, true);
246
+ var last = start, going;
247
+ // And then we have to see if it's a drag event, in which case
248
+ // the dragged-over text must be selected.
249
+ function end() {
250
+ focusInput();
251
+ updateInput = true;
252
+ move(); up();
253
+ }
254
+ function extend(e) {
255
+ var cur = posFromMouse(e, true);
256
+ if (cur && !posEq(cur, last)) {
257
+ if (!focused) onFocus();
258
+ last = cur;
259
+ setSelectionUser(start, cur);
260
+ updateInput = false;
261
+ var visible = visibleLines();
262
+ if (cur.line >= visible.to || cur.line < visible.from)
263
+ going = setTimeout(operation(function(){extend(e);}), 150);
264
+ }
265
+ }
266
+
267
+ var move = connect(targetDocument, "mousemove", operation(function(e) {
268
+ clearTimeout(going);
269
+ e.stop();
270
+ extend(e);
271
+ }), true);
272
+ var up = connect(targetDocument, "mouseup", operation(function(e) {
273
+ clearTimeout(going);
274
+ var cur = posFromMouse(e);
275
+ if (cur) setSelectionUser(start, cur);
276
+ e.stop();
277
+ end();
278
+ }), true);
279
+ }
280
+ function onDblClick(e) {
281
+ var pos = posFromMouse(e);
282
+ if (!pos) return;
283
+ selectWordAt(pos);
284
+ e.stop();
285
+ lastDoubleClick = +new Date;
286
+ }
287
+ function onDrop(e) {
288
+ e.e.preventDefault();
289
+ var pos = posFromMouse(e, true), files = e.e.dataTransfer.files;
290
+ if (!pos || options.readOnly) return;
291
+ if (files && files.length && window.FileReader && window.File) {
292
+ function loadFile(file, i) {
293
+ var reader = new FileReader;
294
+ reader.onload = function() {
295
+ text[i] = reader.result;
296
+ if (++read == n) replaceRange(text.join(""), clipPos(pos), clipPos(pos));
297
+ };
298
+ reader.readAsText(file);
299
+ }
300
+ var n = files.length, text = Array(n), read = 0;
301
+ for (var i = 0; i < n; ++i) loadFile(files[i], i);
302
+ }
303
+ else {
304
+ try {
305
+ var text = e.e.dataTransfer.getData("Text");
306
+ if (text) replaceRange(text, pos, pos);
307
+ }
308
+ catch(e){}
309
+ }
310
+ }
311
+ function onKeyDown(e) {
312
+ if (!focused) onFocus();
313
+
314
+ var code = e.e.keyCode;
315
+ // IE does strange things with escape.
316
+ if (ie && code == 27) { e.e.returnValue = false; }
317
+ // Tries to detect ctrl on non-mac, cmd on mac.
318
+ var mod = (mac ? e.e.metaKey : e.e.ctrlKey) && !e.e.altKey, anyMod = e.e.ctrlKey || e.e.altKey || e.e.metaKey;
319
+ if (code == 16 || e.e.shiftKey) shiftSelecting = shiftSelecting || (sel.inverted ? sel.to : sel.from);
320
+ else shiftSelecting = null;
321
+ // First give onKeyEvent option a chance to handle this.
322
+ if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e.e))) return;
323
+
324
+ if (code == 33 || code == 34) {scrollPage(code == 34); return e.stop();} // page up/down
325
+ if (mod && ((code == 36 || code == 35) || // ctrl-home/end
326
+ mac && (code == 38 || code == 40))) { // cmd-up/down
327
+ scrollEnd(code == 36 || code == 38); return e.stop();
328
+ }
329
+ if (mod && code == 65) {selectAll(); return e.stop();} // ctrl-a
330
+ if (!options.readOnly) {
331
+ if (!anyMod && code == 13) {return;} // enter
332
+ if (!anyMod && code == 9 && handleTab(e.e.shiftKey)) return e.stop(); // tab
333
+ if (mod && code == 90) {undo(); return e.stop();} // ctrl-z
334
+ if (mod && ((e.e.shiftKey && code == 90) || code == 89)) {redo(); return e.stop();} // ctrl-shift-z, ctrl-y
335
+ }
336
+
337
+ // Key id to use in the movementKeys map. We also pass it to
338
+ // fastPoll in order to 'self learn'. We need this because
339
+ // reducedSelection, the hack where we collapse the selection to
340
+ // its start when it is inverted and a movement key is pressed
341
+ // (and later restore it again), shouldn't be used for
342
+ // non-movement keys.
343
+ curKeyId = (mod ? "c" : "") + code;
344
+ if (sel.inverted && movementKeys.hasOwnProperty(curKeyId)) {
345
+ var range = selRange(input);
346
+ if (range) {
347
+ reducedSelection = {anchor: range.start};
348
+ setSelRange(input, range.start, range.start);
349
+ }
350
+ }
351
+ fastPoll(curKeyId);
352
+ }
353
+ function onKeyUp(e) {
354
+ if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e.e))) return;
355
+ if (reducedSelection) {
356
+ reducedSelection = null;
357
+ updateInput = true;
358
+ }
359
+ if (e.e.keyCode == 16) shiftSelecting = null;
360
+ }
361
+ function onKeyPress(e) {
362
+ if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e.e))) return;
363
+ if (options.electricChars && mode.electricChars) {
364
+ var ch = String.fromCharCode(e.e.charCode == null ? e.e.keyCode : e.e.charCode);
365
+ if (mode.electricChars.indexOf(ch) > -1)
366
+ setTimeout(operation(function() {indentLine(sel.to.line, "smart");}), 50);
367
+ }
368
+ var code = e.e.keyCode;
369
+ // Re-stop tab and enter. Necessary on some browsers.
370
+ if (code == 13) {if (!options.readOnly) handleEnter(); e.stop();}
371
+ else if (!e.e.ctrlKey && !e.e.altKey && !e.e.metaKey && code == 9 && options.tabMode != "default") e.stop();
372
+ else fastPoll(curKeyId);
373
+ }
374
+
375
+ function onFocus() {
376
+ if (options.readOnly == "nocursor") return;
377
+ if (!focused) {
378
+ if (options.onFocus) options.onFocus(instance);
379
+ focused = true;
380
+ if (wrapper.className.search(/\bCodeMirror-focused\b/) == -1)
381
+ wrapper.className += " CodeMirror-focused";
382
+ if (!leaveInputAlone) prepareInput();
383
+ }
384
+ slowPoll();
385
+ restartBlink();
386
+ }
387
+ function onBlur() {
388
+ if (focused) {
389
+ if (options.onBlur) options.onBlur(instance);
390
+ focused = false;
391
+ wrapper.className = wrapper.className.replace(" CodeMirror-focused", "");
392
+ }
393
+ clearInterval(blinker);
394
+ setTimeout(function() {if (!focused) shiftSelecting = null;}, 150);
395
+ }
396
+
397
+ // Replace the range from from to to by the strings in newText.
398
+ // Afterwards, set the selection to selFrom, selTo.
399
+ function updateLines(from, to, newText, selFrom, selTo) {
400
+ if (history) {
401
+ var old = [];
402
+ for (var i = from.line, e = to.line + 1; i < e; ++i) old.push(lines[i].text);
403
+ history.addChange(from.line, newText.length, old);
404
+ while (history.done.length > options.undoDepth) history.done.shift();
405
+ }
406
+ updateLinesNoUndo(from, to, newText, selFrom, selTo);
407
+ }
408
+ function unredoHelper(from, to) {
409
+ var change = from.pop();
410
+ if (change) {
411
+ var replaced = [], end = change.start + change.added;
412
+ for (var i = change.start; i < end; ++i) replaced.push(lines[i].text);
413
+ to.push({start: change.start, added: change.old.length, old: replaced});
414
+ var pos = clipPos({line: change.start + change.old.length - 1,
415
+ ch: editEnd(replaced[replaced.length-1], change.old[change.old.length-1])});
416
+ updateLinesNoUndo({line: change.start, ch: 0}, {line: end - 1, ch: lines[end-1].text.length}, change.old, pos, pos);
417
+ }
418
+ }
419
+ function undo() {unredoHelper(history.done, history.undone);}
420
+ function redo() {unredoHelper(history.undone, history.done);}
421
+
422
+ function updateLinesNoUndo(from, to, newText, selFrom, selTo) {
423
+ var recomputeMaxLength = false, maxLineLength = maxLine.length;
424
+ for (var i = from.line; i <= to.line; ++i) {
425
+ if (lines[i].text.length == maxLineLength) {recomputeMaxLength = true; break;}
426
+ }
427
+
428
+ var nlines = to.line - from.line, firstLine = lines[from.line], lastLine = lines[to.line];
429
+ // First adjust the line structure, taking some care to leave highlighting intact.
430
+ if (firstLine == lastLine) {
431
+ if (newText.length == 1)
432
+ firstLine.replace(from.ch, to.ch, newText[0]);
433
+ else {
434
+ lastLine = firstLine.split(to.ch, newText[newText.length-1]);
435
+ var spliceargs = [from.line + 1, nlines];
436
+ firstLine.replace(from.ch, firstLine.text.length, newText[0]);
437
+ for (var i = 1, e = newText.length - 1; i < e; ++i) spliceargs.push(new Line(newText[i]));
438
+ spliceargs.push(lastLine);
439
+ lines.splice.apply(lines, spliceargs);
440
+ }
441
+ }
442
+ else if (newText.length == 1) {
443
+ firstLine.replace(from.ch, firstLine.text.length, newText[0] + lastLine.text.slice(to.ch));
444
+ lines.splice(from.line + 1, nlines);
445
+ }
446
+ else {
447
+ var spliceargs = [from.line + 1, nlines - 1];
448
+ firstLine.replace(from.ch, firstLine.text.length, newText[0]);
449
+ lastLine.replace(0, to.ch, newText[newText.length-1]);
450
+ for (var i = 1, e = newText.length - 1; i < e; ++i) spliceargs.push(new Line(newText[i]));
451
+ lines.splice.apply(lines, spliceargs);
452
+ }
453
+
454
+
455
+ for (var i = from.line, e = i + newText.length; i < e; ++i) {
456
+ var l = lines[i].text;
457
+ if (l.length > maxLineLength) {
458
+ maxLine = l; maxLineLength = l.length;
459
+ recomputeMaxLength = false;
460
+ }
461
+ }
462
+ if (recomputeMaxLength) {
463
+ maxLineLength = 0; maxLine = "";
464
+ for (var i = 0, e = lines.length; i < e; ++i) {
465
+ var l = lines[i].text;
466
+ if (l.length > maxLineLength) {
467
+ maxLineLength = l.length; maxLine = l;
468
+ }
469
+ }
470
+ }
471
+
472
+ // Add these lines to the work array, so that they will be
473
+ // highlighted. Adjust work lines if lines were added/removed.
474
+ var newWork = [], lendiff = newText.length - nlines - 1;
475
+ for (var i = 0, l = work.length; i < l; ++i) {
476
+ var task = work[i];
477
+ if (task < from.line) newWork.push(task);
478
+ else if (task > to.line) newWork.push(task + lendiff);
479
+ }
480
+ if (newText.length < 5) {
481
+ highlightLines(from.line, from.line + newText.length);
482
+ newWork.push(from.line + newText.length);
483
+ } else {
484
+ newWork.push(from.line);
485
+ }
486
+ work = newWork;
487
+ startWorker(100);
488
+ // Remember that these lines changed, for updating the display
489
+ changes.push({from: from.line, to: to.line + 1, diff: lendiff});
490
+ textChanged = {from: from, to: to, text: newText};
491
+
492
+ // Update the selection
493
+ function updateLine(n) {return n <= Math.min(to.line, to.line + lendiff) ? n : n + lendiff;}
494
+ setSelection(selFrom, selTo, updateLine(sel.from.line), updateLine(sel.to.line));
495
+
496
+ // Make sure the scroll-size div has the correct height.
497
+ code.style.height = (lines.length * lineHeight() + 2 * paddingTop()) + "px";
498
+ }
499
+
500
+ function replaceRange(code, from, to) {
501
+ from = clipPos(from);
502
+ if (!to) to = from; else to = clipPos(to);
503
+ code = splitLines(code);
504
+ function adjustPos(pos) {
505
+ if (posLess(pos, from)) return pos;
506
+ if (!posLess(to, pos)) return end;
507
+ var line = pos.line + code.length - (to.line - from.line) - 1;
508
+ var ch = pos.ch;
509
+ if (pos.line == to.line)
510
+ ch += code[code.length-1].length - (to.ch - (to.line == from.line ? from.ch : 0));
511
+ return {line: line, ch: ch};
512
+ }
513
+ var end;
514
+ replaceRange1(code, from, to, function(end1) {
515
+ end = end1;
516
+ return {from: adjustPos(sel.from), to: adjustPos(sel.to)};
517
+ });
518
+ return end;
519
+ }
520
+ function replaceSelection(code, collapse) {
521
+ replaceRange1(splitLines(code), sel.from, sel.to, function(end) {
522
+ if (collapse == "end") return {from: end, to: end};
523
+ else if (collapse == "start") return {from: sel.from, to: sel.from};
524
+ else return {from: sel.from, to: end};
525
+ });
526
+ }
527
+ function replaceRange1(code, from, to, computeSel) {
528
+ var endch = code.length == 1 ? code[0].length + from.ch : code[code.length-1].length;
529
+ var newSel = computeSel({line: from.line + code.length - 1, ch: endch});
530
+ updateLines(from, to, code, newSel.from, newSel.to);
531
+ }
532
+
533
+ function getRange(from, to) {
534
+ var l1 = from.line, l2 = to.line;
535
+ if (l1 == l2) return lines[l1].text.slice(from.ch, to.ch);
536
+ var code = [lines[l1].text.slice(from.ch)];
537
+ for (var i = l1 + 1; i < l2; ++i) code.push(lines[i].text);
538
+ code.push(lines[l2].text.slice(0, to.ch));
539
+ return code.join("\n");
540
+ }
541
+ function getSelection() {
542
+ return getRange(sel.from, sel.to);
543
+ }
544
+
545
+ var pollingFast = false; // Ensures slowPoll doesn't cancel fastPoll
546
+ function slowPoll() {
547
+ if (pollingFast) return;
548
+ poll.set(2000, function() {
549
+ startOperation();
550
+ readInput();
551
+ if (focused) slowPoll();
552
+ endOperation();
553
+ });
554
+ }
555
+ function fastPoll(keyId) {
556
+ var missed = false;
557
+ pollingFast = true;
558
+ function p() {
559
+ startOperation();
560
+ var changed = readInput();
561
+ if (changed == "moved" && keyId) movementKeys[keyId] = true;
562
+ if (!changed && !missed) {missed = true; poll.set(80, p);}
563
+ else {pollingFast = false; slowPoll();}
564
+ endOperation();
565
+ }
566
+ poll.set(20, p);
567
+ }
568
+
569
+ // Inspects the textarea, compares its state (content, selection)
570
+ // to the data in the editing variable, and updates the editor
571
+ // content or cursor if something changed.
572
+ function readInput() {
573
+ if (leaveInputAlone) return;
574
+ var changed = false, text = input.value, sr = selRange(input);
575
+ if (!sr) return false;
576
+ var changed = editing.text != text, rs = reducedSelection;
577
+ var moved = changed || sr.start != editing.start || sr.end != (rs ? editing.start : editing.end);
578
+ if (!moved && !rs) return false;
579
+ if (changed) {
580
+ shiftSelecting = reducedSelection = null;
581
+ if (options.readOnly) {updateInput = true; return "changed";}
582
+ }
583
+
584
+ // Compute selection start and end based on start/end offsets in textarea
585
+ function computeOffset(n, startLine) {
586
+ var pos = 0;
587
+ for (;;) {
588
+ var found = text.indexOf("\n", pos);
589
+ if (found == -1 || (text.charAt(found-1) == "\r" ? found - 1 : found) >= n)
590
+ return {line: startLine, ch: n - pos};
591
+ ++startLine;
592
+ pos = found + 1;
593
+ }
594
+ }
595
+ var from = computeOffset(sr.start, editing.from),
596
+ to = computeOffset(sr.end, editing.from);
597
+ // Here we have to take the reducedSelection hack into account,
598
+ // so that you can, for example, press shift-up at the start of
599
+ // your selection and have the right thing happen.
600
+ if (rs) {
601
+ var head = sr.start == rs.anchor ? to : from;
602
+ var tail = shiftSelecting ? sel.to : sr.start == rs.anchor ? from : to;
603
+ if (sel.inverted = posLess(head, tail)) { from = head; to = tail; }
604
+ else { reducedSelection = null; from = tail; to = head; }
605
+ }
606
+
607
+ // In some cases (cursor on same line as before), we don't have
608
+ // to update the textarea content at all.
609
+ if (from.line == to.line && from.line == sel.from.line && from.line == sel.to.line && !shiftSelecting)
610
+ updateInput = false;
611
+
612
+ // Magic mess to extract precise edited range from the changed
613
+ // string.
614
+ if (changed) {
615
+ var start = 0, end = text.length, len = Math.min(end, editing.text.length);
616
+ var c, line = editing.from, nl = -1;
617
+ while (start < len && (c = text.charAt(start)) == editing.text.charAt(start)) {
618
+ ++start;
619
+ if (c == "\n") {line++; nl = start;}
620
+ }
621
+ var ch = nl > -1 ? start - nl : start, endline = editing.to - 1, edend = editing.text.length;
622
+ for (;;) {
623
+ c = editing.text.charAt(edend);
624
+ if (text.charAt(end) != c) {++end; ++edend; break;}
625
+ if (c == "\n") endline--;
626
+ if (edend <= start || end <= start) break;
627
+ --end; --edend;
628
+ }
629
+ var nl = editing.text.lastIndexOf("\n", edend - 1), endch = nl == -1 ? edend : edend - nl - 1;
630
+ updateLines({line: line, ch: ch}, {line: endline, ch: endch}, splitLines(text.slice(start, end)), from, to);
631
+ if (line != endline || from.line != line) updateInput = true;
632
+ }
633
+ else setSelection(from, to);
634
+
635
+ editing.text = text; editing.start = sr.start; editing.end = sr.end;
636
+ return changed ? "changed" : moved ? "moved" : false;
637
+ }
638
+
639
+ // Set the textarea content and selection range to match the
640
+ // editor state.
641
+ function prepareInput() {
642
+ var text = [];
643
+ var from = Math.max(0, sel.from.line - 1), to = Math.min(lines.length, sel.to.line + 2);
644
+ for (var i = from; i < to; ++i) text.push(lines[i].text);
645
+ text = input.value = text.join(lineSep);
646
+ var startch = sel.from.ch, endch = sel.to.ch;
647
+ for (var i = from; i < sel.from.line; ++i)
648
+ startch += lineSep.length + lines[i].text.length;
649
+ for (var i = from; i < sel.to.line; ++i)
650
+ endch += lineSep.length + lines[i].text.length;
651
+ editing = {text: text, from: from, to: to, start: startch, end: endch};
652
+ setSelRange(input, startch, reducedSelection ? startch : endch);
653
+ }
654
+ function focusInput() {
655
+ if (options.readOnly != "nocursor") input.focus();
656
+ }
657
+
658
+ function scrollCursorIntoView() {
659
+ var cursor = localCoords(sel.inverted ? sel.from : sel.to);
660
+ return scrollIntoView(cursor.x, cursor.y, cursor.x, cursor.yBot);
661
+ }
662
+ function scrollIntoView(x1, y1, x2, y2) {
663
+ var pl = paddingLeft(), pt = paddingTop(), lh = lineHeight();
664
+ y1 += pt; y2 += pt; x1 += pl; x2 += pl;
665
+ var screen = scroller.clientHeight, screentop = scroller.scrollTop, scrolled = false, result = true;
666
+ if (y1 < screentop) {scroller.scrollTop = Math.max(0, y1 - 2*lh); scrolled = true;}
667
+ else if (y2 > screentop + screen) {scroller.scrollTop = y2 + lh - screen; scrolled = true;}
668
+
669
+ var screenw = scroller.clientWidth, screenleft = scroller.scrollLeft;
670
+ if (x1 < screenleft) {
671
+ if (x1 < 50) x1 = 0;
672
+ scroller.scrollLeft = Math.max(0, x1 - 10);
673
+ scrolled = true;
674
+ }
675
+ else if (x2 > screenw + screenleft) {
676
+ scroller.scrollLeft = x2 + 10 - screenw;
677
+ scrolled = true;
678
+ if (x2 > code.clientWidth) result = false;
679
+ }
680
+ if (scrolled && options.onScroll) options.onScroll(instance);
681
+ return result;
682
+ }
683
+
684
+ function visibleLines() {
685
+ var lh = lineHeight(), top = scroller.scrollTop - paddingTop();
686
+ return {from: Math.min(lines.length, Math.max(0, Math.floor(top / lh))),
687
+ to: Math.min(lines.length, Math.ceil((top + scroller.clientHeight) / lh))};
688
+ }
689
+ // Uses a set of changes plus the current scroll position to
690
+ // determine which DOM updates have to be made, and makes the
691
+ // updates.
692
+ function updateDisplay(changes) {
693
+ if (!scroller.clientWidth) {
694
+ showingFrom = showingTo = 0;
695
+ return;
696
+ }
697
+ // First create a range of theoretically intact lines, and punch
698
+ // holes in that using the change info.
699
+ var intact = changes === true ? [] : [{from: showingFrom, to: showingTo, domStart: 0}];
700
+ for (var i = 0, l = changes.length || 0; i < l; ++i) {
701
+ var change = changes[i], intact2 = [], diff = change.diff || 0;
702
+ for (var j = 0, l2 = intact.length; j < l2; ++j) {
703
+ var range = intact[j];
704
+ if (change.to <= range.from)
705
+ intact2.push({from: range.from + diff, to: range.to + diff, domStart: range.domStart});
706
+ else if (range.to <= change.from)
707
+ intact2.push(range);
708
+ else {
709
+ if (change.from > range.from)
710
+ intact2.push({from: range.from, to: change.from, domStart: range.domStart})
711
+ if (change.to < range.to)
712
+ intact2.push({from: change.to + diff, to: range.to + diff,
713
+ domStart: range.domStart + (change.to - range.from)});
714
+ }
715
+ }
716
+ intact = intact2;
717
+ }
718
+
719
+ // Then, determine which lines we'd want to see, and which
720
+ // updates have to be made to get there.
721
+ var visible = visibleLines();
722
+ var from = Math.min(showingFrom, Math.max(visible.from - 3, 0)),
723
+ to = Math.min(lines.length, Math.max(showingTo, visible.to + 3)),
724
+ updates = [], domPos = 0, domEnd = showingTo - showingFrom, pos = from, changedLines = 0;
725
+
726
+ for (var i = 0, l = intact.length; i < l; ++i) {
727
+ var range = intact[i];
728
+ if (range.to <= from) continue;
729
+ if (range.from >= to) break;
730
+ if (range.domStart > domPos || range.from > pos) {
731
+ updates.push({from: pos, to: range.from, domSize: range.domStart - domPos, domStart: domPos});
732
+ changedLines += range.from - pos;
733
+ }
734
+ pos = range.to;
735
+ domPos = range.domStart + (range.to - range.from);
736
+ }
737
+ if (domPos != domEnd || pos != to) {
738
+ changedLines += Math.abs(to - pos);
739
+ updates.push({from: pos, to: to, domSize: domEnd - domPos, domStart: domPos});
740
+ }
741
+
742
+ if (!updates.length) return;
743
+ lineDiv.style.display = "none";
744
+ // If more than 30% of the screen needs update, just do a full
745
+ // redraw (which is quicker than patching)
746
+ if (changedLines > (visible.to - visible.from) * .3)
747
+ refreshDisplay(from = Math.max(visible.from - 10, 0), to = Math.min(visible.to + 7, lines.length));
748
+ // Otherwise, only update the stuff that needs updating.
749
+ else
750
+ patchDisplay(updates);
751
+ lineDiv.style.display = "";
752
+
753
+ // Position the mover div to align with the lines it's supposed
754
+ // to be showing (which will cover the visible display)
755
+ var different = from != showingFrom || to != showingTo || lastHeight != scroller.clientHeight;
756
+ showingFrom = from; showingTo = to;
757
+ mover.style.top = (from * lineHeight()) + "px";
758
+ if (different) {
759
+ lastHeight = scroller.clientHeight;
760
+ code.style.height = (lines.length * lineHeight() + 2 * paddingTop()) + "px";
761
+ updateGutter();
762
+ }
763
+
764
+ var textWidth = stringWidth(maxLine);
765
+ lineSpace.style.width = textWidth > scroller.clientWidth ? textWidth + "px" : "";
766
+ // Needed to prevent odd wrapping/hiding of widgets placed in here.
767
+ // each resize of the window makes the editor five pixels narrower!
768
+ // code.style.width = (lineSpace.offsetWidth + lineSpace.offsetLeft) + "px";
769
+
770
+ // Since this is all rather error prone, it is honoured with the
771
+ // only assertion in the whole file.
772
+ if (lineDiv.childNodes.length != showingTo - showingFrom)
773
+ throw new Error("BAD PATCH! " + JSON.stringify(updates) + " size=" + (showingTo - showingFrom) +
774
+ " nodes=" + lineDiv.childNodes.length);
775
+ updateCursor();
776
+ }
777
+
778
+ function refreshDisplay(from, to) {
779
+ var html = [], start = {line: from, ch: 0}, inSel = posLess(sel.from, start) && !posLess(sel.to, start);
780
+ for (var i = from; i < to; ++i) {
781
+ var ch1 = null, ch2 = null;
782
+ if (inSel) {
783
+ ch1 = 0;
784
+ if (sel.to.line == i) {inSel = false; ch2 = sel.to.ch;}
785
+ }
786
+ else if (sel.from.line == i) {
787
+ if (sel.to.line == i) {ch1 = sel.from.ch; ch2 = sel.to.ch;}
788
+ else {inSel = true; ch1 = sel.from.ch;}
789
+ }
790
+ html.push(lines[i].getHTML(ch1, ch2, true));
791
+ }
792
+ lineDiv.innerHTML = html.join("");
793
+ }
794
+ function patchDisplay(updates) {
795
+ // Slightly different algorithm for IE (badInnerHTML), since
796
+ // there .innerHTML on PRE nodes is dumb, and discards
797
+ // whitespace.
798
+ var sfrom = sel.from.line, sto = sel.to.line, off = 0,
799
+ scratch = badInnerHTML && targetDocument.createElement("div");
800
+ for (var i = 0, e = updates.length; i < e; ++i) {
801
+ var rec = updates[i];
802
+ var extra = (rec.to - rec.from) - rec.domSize;
803
+ var nodeAfter = lineDiv.childNodes[rec.domStart + rec.domSize + off] || null;
804
+ if (badInnerHTML)
805
+ for (var j = Math.max(-extra, rec.domSize); j > 0; --j)
806
+ lineDiv.removeChild(nodeAfter ? nodeAfter.previousSibling : lineDiv.lastChild);
807
+ else if (extra) {
808
+ for (var j = Math.max(0, extra); j > 0; --j)
809
+ lineDiv.insertBefore(targetDocument.createElement("pre"), nodeAfter);
810
+ for (var j = Math.max(0, -extra); j > 0; --j)
811
+ lineDiv.removeChild(nodeAfter ? nodeAfter.previousSibling : lineDiv.lastChild);
812
+ }
813
+ var node = lineDiv.childNodes[rec.domStart + off], inSel = sfrom < rec.from && sto >= rec.from;
814
+ for (var j = rec.from; j < rec.to; ++j) {
815
+ var ch1 = null, ch2 = null;
816
+ if (inSel) {
817
+ ch1 = 0;
818
+ if (sto == j) {inSel = false; ch2 = sel.to.ch;}
819
+ }
820
+ else if (sfrom == j) {
821
+ if (sto == j) {ch1 = sel.from.ch; ch2 = sel.to.ch;}
822
+ else {inSel = true; ch1 = sel.from.ch;}
823
+ }
824
+ if (badInnerHTML) {
825
+ scratch.innerHTML = lines[j].getHTML(ch1, ch2, true);
826
+ lineDiv.insertBefore(scratch.firstChild, nodeAfter);
827
+ }
828
+ else {
829
+ node.innerHTML = lines[j].getHTML(ch1, ch2, false);
830
+ node.className = lines[j].className || "";
831
+ node = node.nextSibling;
832
+ }
833
+ }
834
+ off += extra;
835
+ }
836
+ }
837
+
838
+ function updateGutter() {
839
+ if (!options.gutter && !options.lineNumbers) return;
840
+ var hText = mover.offsetHeight, hEditor = scroller.clientHeight;
841
+ gutter.style.height = (hText - hEditor < 2 ? hEditor : hText) + "px";
842
+ var html = [];
843
+ for (var i = showingFrom; i < Math.max(showingTo, showingFrom + 1); ++i) {
844
+ var marker = lines[i].gutterMarker;
845
+ var text = options.lineNumbers ? i + options.firstLineNumber : null;
846
+ if (marker && marker.text)
847
+ text = marker.text.replace("%N%", text != null ? text : "");
848
+ else if (text == null)
849
+ text = "\u00a0";
850
+ html.push((marker && marker.style ? '<pre class="' + marker.style + '">' : "<pre>"), text, "</pre>");
851
+ }
852
+ gutter.style.display = "none";
853
+ gutterText.innerHTML = html.join("");
854
+ var minwidth = String(lines.length).length, firstNode = gutterText.firstChild, val = eltText(firstNode), pad = "";
855
+ while (val.length + pad.length < minwidth) pad += "\u00a0";
856
+ if (pad) firstNode.insertBefore(targetDocument.createTextNode(pad), firstNode.firstChild);
857
+ gutter.style.display = "";
858
+ lineSpace.style.marginLeft = gutter.offsetWidth + "px";
859
+ }
860
+ function updateCursor() {
861
+ var head = sel.inverted ? sel.from : sel.to, lh = lineHeight();
862
+ var x = charX(head.line, head.ch) + "px", y = (head.line - showingFrom) * lh + "px";
863
+ inputDiv.style.top = (head.line * lh - scroller.scrollTop) + "px";
864
+ if (posEq(sel.from, sel.to)) {
865
+ cursor.style.top = y; cursor.style.left = x;
866
+ cursor.style.display = "";
867
+ }
868
+ else cursor.style.display = "none";
869
+ }
870
+
871
+ function setSelectionUser(from, to) {
872
+ var sh = shiftSelecting && clipPos(shiftSelecting);
873
+ if (sh) {
874
+ if (posLess(sh, from)) from = sh;
875
+ else if (posLess(to, sh)) to = sh;
876
+ }
877
+ setSelection(from, to);
878
+ }
879
+ // Update the selection. Last two args are only used by
880
+ // updateLines, since they have to be expressed in the line
881
+ // numbers before the update.
882
+ function setSelection(from, to, oldFrom, oldTo) {
883
+ if (posEq(sel.from, from) && posEq(sel.to, to)) return;
884
+ if (posLess(to, from)) {var tmp = to; to = from; from = tmp;}
885
+
886
+ if (posEq(from, to)) sel.inverted = false;
887
+ else if (posEq(from, sel.to)) sel.inverted = false;
888
+ else if (posEq(to, sel.from)) sel.inverted = true;
889
+
890
+ // Some ugly logic used to only mark the lines that actually did
891
+ // see a change in selection as changed, rather than the whole
892
+ // selected range.
893
+ if (oldFrom == null) {oldFrom = sel.from.line; oldTo = sel.to.line;}
894
+ if (posEq(from, to)) {
895
+ if (!posEq(sel.from, sel.to))
896
+ changes.push({from: oldFrom, to: oldTo + 1});
897
+ }
898
+ else if (posEq(sel.from, sel.to)) {
899
+ changes.push({from: from.line, to: to.line + 1});
900
+ }
901
+ else {
902
+ if (!posEq(from, sel.from)) {
903
+ if (from.line < oldFrom)
904
+ changes.push({from: from.line, to: Math.min(to.line, oldFrom) + 1});
905
+ else
906
+ changes.push({from: oldFrom, to: Math.min(oldTo, from.line) + 1});
907
+ }
908
+ if (!posEq(to, sel.to)) {
909
+ if (to.line < oldTo)
910
+ changes.push({from: Math.max(oldFrom, from.line), to: oldTo + 1});
911
+ else
912
+ changes.push({from: Math.max(from.line, oldTo), to: to.line + 1});
913
+ }
914
+ }
915
+ sel.from = from; sel.to = to;
916
+ selectionChanged = true;
917
+ }
918
+ function setCursor(line, ch, user) {
919
+ var pos = clipPos({line: line, ch: ch || 0});
920
+ (user ? setSelectionUser : setSelection)(pos, pos);
921
+ }
922
+
923
+ function clipLine(n) {return Math.max(0, Math.min(n, lines.length-1));}
924
+ function clipPos(pos) {
925
+ if (pos.line < 0) return {line: 0, ch: 0};
926
+ if (pos.line >= lines.length) return {line: lines.length-1, ch: lines[lines.length-1].text.length};
927
+ var ch = pos.ch, linelen = lines[pos.line].text.length;
928
+ if (ch == null || ch > linelen) return {line: pos.line, ch: linelen};
929
+ else if (ch < 0) return {line: pos.line, ch: 0};
930
+ else return pos;
931
+ }
932
+
933
+ function scrollPage(down) {
934
+ var linesPerPage = Math.floor(scroller.clientHeight / lineHeight()), head = sel.inverted ? sel.from : sel.to;
935
+ setCursor(head.line + (Math.max(linesPerPage - 1, 1) * (down ? 1 : -1)), head.ch, true);
936
+ }
937
+ function scrollEnd(top) {
938
+ var pos = top ? {line: 0, ch: 0} : {line: lines.length - 1, ch: lines[lines.length-1].text.length};
939
+ setSelectionUser(pos, pos);
940
+ }
941
+ function selectAll() {
942
+ var endLine = lines.length - 1;
943
+ setSelection({line: 0, ch: 0}, {line: endLine, ch: lines[endLine].text.length});
944
+ }
945
+ function selectWordAt(pos) {
946
+ var line = lines[pos.line].text;
947
+ var start = pos.ch, end = pos.ch;
948
+ while (start > 0 && /\w/.test(line.charAt(start - 1))) --start;
949
+ while (end < line.length && /\w/.test(line.charAt(end))) ++end;
950
+ setSelectionUser({line: pos.line, ch: start}, {line: pos.line, ch: end});
951
+ }
952
+ function selectLine(line) {
953
+ setSelectionUser({line: line, ch: 0}, {line: line, ch: lines[line].text.length});
954
+ }
955
+ function handleEnter() {
956
+ replaceSelection("\n", "end");
957
+ if (options.enterMode != "flat")
958
+ indentLine(sel.from.line, options.enterMode == "keep" ? "prev" : "smart");
959
+ }
960
+ function handleTab(shift) {
961
+ function indentSelected(mode) {
962
+ if (posEq(sel.from, sel.to)) return indentLine(sel.from.line, mode);
963
+ var e = sel.to.line - (sel.to.ch ? 1 : 0);
964
+ for (var i = sel.from.line; i < e; ++i) indentLine(i, mode);
965
+ }
966
+ shiftSelecting = null;
967
+ switch (options.tabMode) {
968
+ case "default":
969
+ return false;
970
+ case "indent":
971
+ indentSelected("smart");
972
+ break;
973
+ case "classic":
974
+ if (posEq(sel.from, sel.to)) {
975
+ if (shift) indentLine(sel.from.line, "smart");
976
+ else replaceSelection("\t", "end");
977
+ break;
978
+ }
979
+ case "shift":
980
+ indentSelected(shift ? "subtract" : "add");
981
+ break;
982
+ }
983
+ return true;
984
+ }
985
+
986
+ function indentLine(n, how) {
987
+ if (how == "smart") {
988
+ if (!mode.indent) how = "prev";
989
+ else var state = getStateBefore(n);
990
+ }
991
+
992
+ var line = lines[n], curSpace = line.indentation(), curSpaceString = line.text.match(/^\s*/)[0], indentation;
993
+ if (how == "prev") {
994
+ if (n) indentation = lines[n-1].indentation();
995
+ else indentation = 0;
996
+ }
997
+ else if (how == "smart") indentation = mode.indent(state, line.text.slice(curSpaceString.length));
998
+ else if (how == "add") indentation = curSpace + options.indentUnit;
999
+ else if (how == "subtract") indentation = curSpace - options.indentUnit;
1000
+ indentation = Math.max(0, indentation);
1001
+ var diff = indentation - curSpace;
1002
+
1003
+ if (!diff) {
1004
+ if (sel.from.line != n && sel.to.line != n) return;
1005
+ var indentString = curSpaceString;
1006
+ }
1007
+ else {
1008
+ var indentString = "", pos = 0;
1009
+ if (options.indentWithTabs)
1010
+ for (var i = Math.floor(indentation / tabSize); i; --i) {pos += tabSize; indentString += "\t";}
1011
+ while (pos < indentation) {++pos; indentString += " ";}
1012
+ }
1013
+
1014
+ replaceRange(indentString, {line: n, ch: 0}, {line: n, ch: curSpaceString.length});
1015
+ }
1016
+
1017
+ function loadMode() {
1018
+ mode = CodeMirror.getMode(options, options.mode);
1019
+ for (var i = 0, l = lines.length; i < l; ++i)
1020
+ lines[i].stateAfter = null;
1021
+ work = [0];
1022
+ startWorker();
1023
+ }
1024
+ function gutterChanged() {
1025
+ var visible = options.gutter || options.lineNumbers;
1026
+ gutter.style.display = visible ? "" : "none";
1027
+ if (visible) updateGutter();
1028
+ else lineDiv.parentNode.style.marginLeft = 0;
1029
+ }
1030
+
1031
+ function markText(from, to, className) {
1032
+ from = clipPos(from); to = clipPos(to);
1033
+ var accum = [];
1034
+ function add(line, from, to, className) {
1035
+ var line = lines[line], mark = line.addMark(from, to, className);
1036
+ mark.line = line;
1037
+ accum.push(mark);
1038
+ }
1039
+ if (from.line == to.line) add(from.line, from.ch, to.ch, className);
1040
+ else {
1041
+ add(from.line, from.ch, null, className);
1042
+ for (var i = from.line + 1, e = to.line; i < e; ++i)
1043
+ add(i, 0, null, className);
1044
+ add(to.line, 0, to.ch, className);
1045
+ }
1046
+ changes.push({from: from.line, to: to.line + 1});
1047
+ return function() {
1048
+ var start, end;
1049
+ for (var i = 0; i < accum.length; ++i) {
1050
+ var mark = accum[i], found = indexOf(lines, mark.line);
1051
+ mark.line.removeMark(mark);
1052
+ if (found > -1) {
1053
+ if (start == null) start = found;
1054
+ end = found;
1055
+ }
1056
+ }
1057
+ if (start != null) changes.push({from: start, to: end + 1});
1058
+ };
1059
+ }
1060
+
1061
+ function addGutterMarker(line, text, className) {
1062
+ if (typeof line == "number") line = lines[clipLine(line)];
1063
+ line.gutterMarker = {text: text, style: className};
1064
+ updateGutter();
1065
+ return line;
1066
+ }
1067
+ function removeGutterMarker(line) {
1068
+ if (typeof line == "number") line = lines[clipLine(line)];
1069
+ line.gutterMarker = null;
1070
+ updateGutter();
1071
+ }
1072
+ function setLineClass(line, className) {
1073
+ if (typeof line == "number") {
1074
+ var no = line;
1075
+ line = lines[clipLine(line)];
1076
+ }
1077
+ else {
1078
+ var no = indexOf(lines, line);
1079
+ if (no == -1) return null;
1080
+ }
1081
+ if (line.className != className) {
1082
+ line.className = className;
1083
+ changes.push({from: no, to: no + 1});
1084
+ }
1085
+ return line;
1086
+ }
1087
+
1088
+ function lineInfo(line) {
1089
+ if (typeof line == "number") {
1090
+ var n = line;
1091
+ line = lines[line];
1092
+ if (!line) return null;
1093
+ }
1094
+ else {
1095
+ var n = indexOf(lines, line);
1096
+ if (n == -1) return null;
1097
+ }
1098
+ var marker = line.gutterMarker;
1099
+ return {line: n, text: line.text, markerText: marker && marker.text, markerClass: marker && marker.style};
1100
+ }
1101
+
1102
+ function stringWidth(str) {
1103
+ measure.innerHTML = "<pre><span>x</span></pre>";
1104
+ measure.firstChild.firstChild.firstChild.nodeValue = str;
1105
+ return measure.firstChild.firstChild.offsetWidth || 10;
1106
+ }
1107
+ // These are used to go from pixel positions to character
1108
+ // positions, taking varying character widths into account.
1109
+ function charX(line, pos) {
1110
+ if (pos == 0) return 0;
1111
+ measure.innerHTML = "<pre><span>" + lines[line].getHTML(null, null, false, pos) + "</span></pre>";
1112
+ return measure.firstChild.firstChild.offsetWidth;
1113
+ }
1114
+ function charFromX(line, x) {
1115
+ if (x <= 0) return 0;
1116
+ var lineObj = lines[line], text = lineObj.text;
1117
+ function getX(len) {
1118
+ measure.innerHTML = "<pre><span>" + lineObj.getHTML(null, null, false, len) + "</span></pre>";
1119
+ return measure.firstChild.firstChild.offsetWidth;
1120
+ }
1121
+ var from = 0, fromX = 0, to = text.length, toX;
1122
+ // Guess a suitable upper bound for our search.
1123
+ var estimated = Math.min(to, Math.ceil(x / stringWidth("x")));
1124
+ for (;;) {
1125
+ var estX = getX(estimated);
1126
+ if (estX <= x && estimated < to) estimated = Math.min(to, Math.ceil(estimated * 1.2));
1127
+ else {toX = estX; to = estimated; break;}
1128
+ }
1129
+ if (x > toX) return to;
1130
+ // Try to guess a suitable lower bound as well.
1131
+ estimated = Math.floor(to * 0.8); estX = getX(estimated);
1132
+ if (estX < x) {from = estimated; fromX = estX;}
1133
+ // Do a binary search between these bounds.
1134
+ for (;;) {
1135
+ if (to - from <= 1) return (toX - x > x - fromX) ? from : to;
1136
+ var middle = Math.ceil((from + to) / 2), middleX = getX(middle);
1137
+ if (middleX > x) {to = middle; toX = middleX;}
1138
+ else {from = middle; fromX = middleX;}
1139
+ }
1140
+ }
1141
+
1142
+ function localCoords(pos, inLineWrap) {
1143
+ var lh = lineHeight(), line = pos.line - (inLineWrap ? showingFrom : 0);
1144
+ return {x: charX(pos.line, pos.ch), y: line * lh, yBot: (line + 1) * lh};
1145
+ }
1146
+ function pageCoords(pos) {
1147
+ var local = localCoords(pos, true), off = eltOffset(lineSpace);
1148
+ return {x: off.left + local.x, y: off.top + local.y, yBot: off.top + local.yBot};
1149
+ }
1150
+
1151
+ function lineHeight() {
1152
+ var nlines = lineDiv.childNodes.length;
1153
+ if (nlines) return (lineDiv.offsetHeight / nlines) || 1;
1154
+ measure.innerHTML = "<pre>x</pre>";
1155
+ return measure.firstChild.offsetHeight || 1;
1156
+ }
1157
+ function paddingTop() {return lineSpace.offsetTop;}
1158
+ function paddingLeft() {return lineSpace.offsetLeft;}
1159
+
1160
+ function posFromMouse(e, liberal) {
1161
+ var offW = eltOffset(scroller, true), x, y;
1162
+ // Fails unpredictably on IE[67] when mouse is dragged around quickly.
1163
+ try { x = e.e.clientX; y = e.e.clientY; } catch (e) { return null; }
1164
+ // This is a mess of a heuristic to try and determine whether a
1165
+ // scroll-bar was clicked or not, and to return null if one was
1166
+ // (and !liberal).
1167
+ if (!liberal && (x - offW.left > scroller.clientWidth || y - offW.top > scroller.clientHeight))
1168
+ return null;
1169
+ var offL = eltOffset(lineSpace, true);
1170
+ var line = showingFrom + Math.floor((y - offL.top) / lineHeight());
1171
+ return clipPos({line: line, ch: charFromX(clipLine(line), x - offL.left)});
1172
+ }
1173
+ function onContextMenu(e) {
1174
+ var pos = posFromMouse(e);
1175
+ if (!pos || window.opera) return; // Opera is difficult.
1176
+ if (posEq(sel.from, sel.to) || posLess(pos, sel.from) || !posLess(pos, sel.to))
1177
+ operation(setCursor)(pos.line, pos.ch);
1178
+
1179
+ var oldCSS = input.style.cssText;
1180
+ inputDiv.style.position = "absolute";
1181
+ input.style.cssText = "position: fixed; width: 30px; height: 30px; top: " + (e.pageY() - 1) +
1182
+ "px; left: " + (e.pageX() - 1) + "px; z-index: 1000; background: white; " +
1183
+ "border-width: 0; outline: none; overflow: hidden; opacity: .05; filter: alpha(opacity=5);";
1184
+ leaveInputAlone = true;
1185
+ var val = input.value = getSelection();
1186
+ focusInput();
1187
+ setSelRange(input, 0, input.value.length);
1188
+ function rehide() {
1189
+ var newVal = splitLines(input.value).join("\n");
1190
+ if (newVal != val) operation(replaceSelection)(newVal, "end");
1191
+ inputDiv.style.position = "relative";
1192
+ input.style.cssText = oldCSS;
1193
+ leaveInputAlone = false;
1194
+ prepareInput();
1195
+ slowPoll();
1196
+ }
1197
+
1198
+ if (gecko) {
1199
+ e.stop();
1200
+ var mouseup = connect(window, "mouseup", function() {
1201
+ mouseup();
1202
+ setTimeout(rehide, 20);
1203
+ }, true);
1204
+ }
1205
+ else {
1206
+ setTimeout(rehide, 50);
1207
+ }
1208
+ }
1209
+
1210
+ // Cursor-blinking
1211
+ function restartBlink() {
1212
+ clearInterval(blinker);
1213
+ var on = true;
1214
+ cursor.style.visibility = "";
1215
+ blinker = setInterval(function() {
1216
+ cursor.style.visibility = (on = !on) ? "" : "hidden";
1217
+ }, 650);
1218
+ }
1219
+
1220
+ var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"};
1221
+ function matchBrackets(autoclear) {
1222
+ var head = sel.inverted ? sel.from : sel.to, line = lines[head.line], pos = head.ch - 1;
1223
+ var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)];
1224
+ if (!match) return;
1225
+ var ch = match.charAt(0), forward = match.charAt(1) == ">", d = forward ? 1 : -1, st = line.styles;
1226
+ for (var off = pos + 1, i = 0, e = st.length; i < e; i+=2)
1227
+ if ((off -= st[i].length) <= 0) {var style = st[i+1]; break;}
1228
+
1229
+ var stack = [line.text.charAt(pos)], re = /[(){}[\]]/;
1230
+ function scan(line, from, to) {
1231
+ if (!line.text) return;
1232
+ var st = line.styles, pos = forward ? 0 : line.text.length - 1, cur;
1233
+ for (var i = forward ? 0 : st.length - 2, e = forward ? st.length : -2; i != e; i += 2*d) {
1234
+ var text = st[i];
1235
+ if (st[i+1] != null && st[i+1] != style) {pos += d * text.length; continue;}
1236
+ for (var j = forward ? 0 : text.length - 1, te = forward ? text.length : -1; j != te; j += d, pos+=d) {
1237
+ if (pos >= from && pos < to && re.test(cur = text.charAt(j))) {
1238
+ var match = matching[cur];
1239
+ if (match.charAt(1) == ">" == forward) stack.push(cur);
1240
+ else if (stack.pop() != match.charAt(0)) return {pos: pos, match: false};
1241
+ else if (!stack.length) return {pos: pos, match: true};
1242
+ }
1243
+ }
1244
+ }
1245
+ }
1246
+ for (var i = head.line, e = forward ? Math.min(i + 100, lines.length) : Math.max(-1, i - 100); i != e; i+=d) {
1247
+ var line = lines[i], first = i == head.line;
1248
+ var found = scan(line, first && forward ? pos + 1 : 0, first && !forward ? pos : line.text.length);
1249
+ if (found) break;
1250
+ }
1251
+ if (!found) found = {pos: null, match: false};
1252
+ var style = found.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";
1253
+ var one = markText({line: head.line, ch: pos}, {line: head.line, ch: pos+1}, style),
1254
+ two = found.pos != null
1255
+ ? markText({line: i, ch: found.pos}, {line: i, ch: found.pos + 1}, style)
1256
+ : function() {};
1257
+ var clear = operation(function(){one(); two();});
1258
+ if (autoclear) setTimeout(clear, 800);
1259
+ else bracketHighlighted = clear;
1260
+ }
1261
+
1262
+ // Finds the line to start with when starting a parse. Tries to
1263
+ // find a line with a stateAfter, so that it can start with a
1264
+ // valid state. If that fails, it returns the line with the
1265
+ // smallest indentation, which tends to need the least context to
1266
+ // parse correctly.
1267
+ function findStartLine(n) {
1268
+ var minindent, minline;
1269
+ for (var search = n, lim = n - 40; search > lim; --search) {
1270
+ if (search == 0) return 0;
1271
+ var line = lines[search-1];
1272
+ if (line.stateAfter) return search;
1273
+ var indented = line.indentation();
1274
+ if (minline == null || minindent > indented) {
1275
+ minline = search;
1276
+ minindent = indented;
1277
+ }
1278
+ }
1279
+ return minline;
1280
+ }
1281
+ function getStateBefore(n) {
1282
+ var start = findStartLine(n), state = start && lines[start-1].stateAfter;
1283
+ if (!state) state = startState(mode);
1284
+ else state = copyState(mode, state);
1285
+ for (var i = start; i < n; ++i) {
1286
+ var line = lines[i];
1287
+ line.highlight(mode, state);
1288
+ line.stateAfter = copyState(mode, state);
1289
+ }
1290
+ if (!lines[n].stateAfter) work.push(n);
1291
+ return state;
1292
+ }
1293
+ function highlightLines(start, end) {
1294
+ var state = getStateBefore(start);
1295
+ for (var i = start; i < end; ++i) {
1296
+ var line = lines[i];
1297
+ line.highlight(mode, state);
1298
+ line.stateAfter = copyState(mode, state);
1299
+ }
1300
+ }
1301
+ function highlightWorker() {
1302
+ var end = +new Date + options.workTime;
1303
+ var foundWork = work.length;
1304
+ while (work.length) {
1305
+ if (!lines[showingFrom].stateAfter) var task = showingFrom;
1306
+ else var task = work.pop();
1307
+ if (task >= lines.length) continue;
1308
+ var start = findStartLine(task), state = start && lines[start-1].stateAfter;
1309
+ if (state) state = copyState(mode, state);
1310
+ else state = startState(mode);
1311
+
1312
+ var unchanged = 0, compare = mode.compareStates;
1313
+ for (var i = start, l = lines.length; i < l; ++i) {
1314
+ var line = lines[i], hadState = line.stateAfter;
1315
+ if (+new Date > end) {
1316
+ work.push(i);
1317
+ startWorker(options.workDelay);
1318
+ changes.push({from: task, to: i});
1319
+ return;
1320
+ }
1321
+ var changed = line.highlight(mode, state);
1322
+ line.stateAfter = copyState(mode, state);
1323
+ if (compare) {
1324
+ if (hadState && compare(hadState, state)) break;
1325
+ } else {
1326
+ if (changed || !hadState) unchanged = 0;
1327
+ else if (++unchanged > 3) break;
1328
+ }
1329
+ }
1330
+ changes.push({from: task, to: i});
1331
+ }
1332
+ if (foundWork && options.onHighlightComplete)
1333
+ options.onHighlightComplete(instance);
1334
+ }
1335
+ function startWorker(time) {
1336
+ if (!work.length) return;
1337
+ highlight.set(time, operation(highlightWorker));
1338
+ }
1339
+
1340
+ // Operations are used to wrap changes in such a way that each
1341
+ // change won't have to update the cursor and display (which would
1342
+ // be awkward, slow, and error-prone), but instead updates are
1343
+ // batched and then all combined and executed at once.
1344
+ function startOperation() {
1345
+ updateInput = null; changes = []; textChanged = selectionChanged = false;
1346
+ }
1347
+ function endOperation() {
1348
+ var reScroll = false;
1349
+ if (selectionChanged) reScroll = !scrollCursorIntoView();
1350
+ if (changes.length) updateDisplay(changes);
1351
+ else if (selectionChanged) updateCursor();
1352
+ if (reScroll) scrollCursorIntoView();
1353
+ if (selectionChanged) restartBlink();
1354
+
1355
+ // updateInput can be set to a boolean value to force/prevent an
1356
+ // update.
1357
+ if (focused && !leaveInputAlone &&
1358
+ (updateInput === true || (updateInput !== false && selectionChanged)))
1359
+ prepareInput();
1360
+
1361
+ if (selectionChanged && options.matchBrackets)
1362
+ setTimeout(operation(function() {
1363
+ if (bracketHighlighted) {bracketHighlighted(); bracketHighlighted = null;}
1364
+ matchBrackets(false);
1365
+ }), 20);
1366
+ var tc = textChanged; // textChanged can be reset by cursoractivity callback
1367
+ if (selectionChanged && options.onCursorActivity)
1368
+ options.onCursorActivity(instance);
1369
+ if (tc && options.onChange && instance)
1370
+ options.onChange(instance, tc);
1371
+ }
1372
+ var nestedOperation = 0;
1373
+ function operation(f) {
1374
+ return function() {
1375
+ if (!nestedOperation++) startOperation();
1376
+ try {var result = f.apply(this, arguments);}
1377
+ finally {if (!--nestedOperation) endOperation();}
1378
+ return result;
1379
+ };
1380
+ }
1381
+
1382
+ function SearchCursor(query, pos, caseFold) {
1383
+ this.atOccurrence = false;
1384
+ if (caseFold == null) caseFold = typeof query == "string" && query == query.toLowerCase();
1385
+
1386
+ if (pos && typeof pos == "object") pos = clipPos(pos);
1387
+ else pos = {line: 0, ch: 0};
1388
+ this.pos = {from: pos, to: pos};
1389
+
1390
+ // The matches method is filled in based on the type of query.
1391
+ // It takes a position and a direction, and returns an object
1392
+ // describing the next occurrence of the query, or null if no
1393
+ // more matches were found.
1394
+ if (typeof query != "string") // Regexp match
1395
+ this.matches = function(reverse, pos) {
1396
+ if (reverse) {
1397
+ var line = lines[pos.line].text.slice(0, pos.ch), match = line.match(query), start = 0;
1398
+ while (match) {
1399
+ var ind = line.indexOf(match[0]);
1400
+ start += ind;
1401
+ line = line.slice(ind + 1);
1402
+ var newmatch = line.match(query);
1403
+ if (newmatch) match = newmatch;
1404
+ else break;
1405
+ start++;
1406
+ }
1407
+ }
1408
+ else {
1409
+ var line = lines[pos.line].text.slice(pos.ch), match = line.match(query),
1410
+ start = match && pos.ch + line.indexOf(match[0]);
1411
+ }
1412
+ if (match)
1413
+ return {from: {line: pos.line, ch: start},
1414
+ to: {line: pos.line, ch: start + match[0].length},
1415
+ match: match};
1416
+ };
1417
+ else { // String query
1418
+ if (caseFold) query = query.toLowerCase();
1419
+ var fold = caseFold ? function(str){return str.toLowerCase();} : function(str){return str;};
1420
+ var target = query.split("\n");
1421
+ // Different methods for single-line and multi-line queries
1422
+ if (target.length == 1)
1423
+ this.matches = function(reverse, pos) {
1424
+ var line = fold(lines[pos.line].text), len = query.length, match;
1425
+ if (reverse ? (pos.ch >= len && (match = line.lastIndexOf(query, pos.ch - len)) != -1)
1426
+ : (match = line.indexOf(query, pos.ch)) != -1)
1427
+ return {from: {line: pos.line, ch: match},
1428
+ to: {line: pos.line, ch: match + len}};
1429
+ };
1430
+ else
1431
+ this.matches = function(reverse, pos) {
1432
+ var ln = pos.line, idx = (reverse ? target.length - 1 : 0), match = target[idx], line = fold(lines[ln].text);
1433
+ var offsetA = (reverse ? line.indexOf(match) + match.length : line.lastIndexOf(match));
1434
+ if (reverse ? offsetA >= pos.ch || offsetA != match.length
1435
+ : offsetA <= pos.ch || offsetA != line.length - match.length)
1436
+ return;
1437
+ for (;;) {
1438
+ if (reverse ? !ln : ln == lines.length - 1) return;
1439
+ line = fold(lines[ln += reverse ? -1 : 1].text);
1440
+ match = target[reverse ? --idx : ++idx];
1441
+ if (idx > 0 && idx < target.length - 1) {
1442
+ if (line != match) return;
1443
+ else continue;
1444
+ }
1445
+ var offsetB = (reverse ? line.lastIndexOf(match) : line.indexOf(match) + match.length);
1446
+ if (reverse ? offsetB != line.length - match.length : offsetB != match.length)
1447
+ return;
1448
+ var start = {line: pos.line, ch: offsetA}, end = {line: ln, ch: offsetB};
1449
+ return {from: reverse ? end : start, to: reverse ? start : end};
1450
+ }
1451
+ };
1452
+ }
1453
+ }
1454
+
1455
+ SearchCursor.prototype = {
1456
+ findNext: function() {return this.find(false);},
1457
+ findPrevious: function() {return this.find(true);},
1458
+
1459
+ find: function(reverse) {
1460
+ var self = this, pos = clipPos(reverse ? this.pos.from : this.pos.to);
1461
+ function savePosAndFail(line) {
1462
+ var pos = {line: line, ch: 0};
1463
+ self.pos = {from: pos, to: pos};
1464
+ self.atOccurrence = false;
1465
+ return false;
1466
+ }
1467
+
1468
+ for (;;) {
1469
+ if (this.pos = this.matches(reverse, pos)) {
1470
+ this.atOccurrence = true;
1471
+ return this.pos.match || true;
1472
+ }
1473
+ if (reverse) {
1474
+ if (!pos.line) return savePosAndFail(0);
1475
+ pos = {line: pos.line-1, ch: lines[pos.line-1].text.length};
1476
+ }
1477
+ else {
1478
+ if (pos.line == lines.length - 1) return savePosAndFail(lines.length);
1479
+ pos = {line: pos.line+1, ch: 0};
1480
+ }
1481
+ }
1482
+ },
1483
+
1484
+ from: function() {if (this.atOccurrence) return copyPos(this.pos.from);},
1485
+ to: function() {if (this.atOccurrence) return copyPos(this.pos.to);},
1486
+
1487
+ replace: function(newText) {
1488
+ var self = this;
1489
+ if (this.atOccurrence)
1490
+ operation(function() {
1491
+ self.pos.to = replaceRange(newText, self.pos.from, self.pos.to);
1492
+ })();
1493
+ }
1494
+ };
1495
+
1496
+ for (var ext in extensions)
1497
+ if (extensions.propertyIsEnumerable(ext) &&
1498
+ !instance.propertyIsEnumerable(ext))
1499
+ instance[ext] = extensions[ext];
1500
+ return instance;
1501
+ } // (end of function CodeMirror)
1502
+
1503
+ // The default configuration options.
1504
+ CodeMirror.defaults = {
1505
+ value: "",
1506
+ mode: null,
1507
+ theme: "default",
1508
+ indentUnit: 2,
1509
+ indentWithTabs: false,
1510
+ tabMode: "classic",
1511
+ enterMode: "indent",
1512
+ electricChars: true,
1513
+ onKeyEvent: null,
1514
+ lineNumbers: false,
1515
+ gutter: false,
1516
+ firstLineNumber: 1,
1517
+ readOnly: false,
1518
+ onChange: null,
1519
+ onCursorActivity: null,
1520
+ onGutterClick: null,
1521
+ onHighlightComplete: null,
1522
+ onFocus: null, onBlur: null, onScroll: null,
1523
+ matchBrackets: false,
1524
+ workTime: 100,
1525
+ workDelay: 200,
1526
+ undoDepth: 40,
1527
+ tabindex: null,
1528
+ document: window.document
1529
+ };
1530
+
1531
+ // Known modes, by name and by MIME
1532
+ var modes = {}, mimeModes = {};
1533
+ CodeMirror.defineMode = function(name, mode) {
1534
+ if (!CodeMirror.defaults.mode && name != "null") CodeMirror.defaults.mode = name;
1535
+ modes[name] = mode;
1536
+ };
1537
+ CodeMirror.defineMIME = function(mime, spec) {
1538
+ mimeModes[mime] = spec;
1539
+ };
1540
+ CodeMirror.getMode = function(options, spec) {
1541
+ if (typeof spec == "string" && mimeModes.hasOwnProperty(spec))
1542
+ spec = mimeModes[spec];
1543
+ if (typeof spec == "string")
1544
+ var mname = spec, config = {};
1545
+ else if (spec != null)
1546
+ var mname = spec.name, config = spec;
1547
+ var mfactory = modes[mname];
1548
+ if (!mfactory) {
1549
+ if (window.console) console.warn("No mode " + mname + " found, falling back to plain text.");
1550
+ return CodeMirror.getMode(options, "text/plain");
1551
+ }
1552
+ return mfactory(options, config || {});
1553
+ };
1554
+ CodeMirror.listModes = function() {
1555
+ var list = [];
1556
+ for (var m in modes)
1557
+ if (modes.propertyIsEnumerable(m)) list.push(m);
1558
+ return list;
1559
+ };
1560
+ CodeMirror.listMIMEs = function() {
1561
+ var list = [];
1562
+ for (var m in mimeModes)
1563
+ if (mimeModes.propertyIsEnumerable(m)) list.push(m);
1564
+ return list;
1565
+ };
1566
+
1567
+ var extensions = {};
1568
+ CodeMirror.defineExtension = function(name, func) {
1569
+ extensions[name] = func;
1570
+ };
1571
+
1572
+ CodeMirror.fromTextArea = function(textarea, options) {
1573
+ if (!options) options = {};
1574
+ options.value = textarea.value;
1575
+ if (!options.tabindex && textarea.tabindex)
1576
+ options.tabindex = textarea.tabindex;
1577
+
1578
+ function save() {textarea.value = instance.getValue();}
1579
+ if (textarea.form) {
1580
+ // Deplorable hack to make the submit method do the right thing.
1581
+ var rmSubmit = connect(textarea.form, "submit", save, true);
1582
+ if (typeof textarea.form.submit == "function") {
1583
+ var realSubmit = textarea.form.submit;
1584
+ function wrappedSubmit() {
1585
+ save();
1586
+ textarea.form.submit = realSubmit;
1587
+ textarea.form.submit();
1588
+ textarea.form.submit = wrappedSubmit;
1589
+ }
1590
+ textarea.form.submit = wrappedSubmit;
1591
+ }
1592
+ }
1593
+
1594
+ textarea.style.display = "none";
1595
+ var instance = CodeMirror(function(node) {
1596
+ textarea.parentNode.insertBefore(node, textarea.nextSibling);
1597
+ }, options);
1598
+ instance.save = save;
1599
+ instance.toTextArea = function() {
1600
+ save();
1601
+ textarea.parentNode.removeChild(instance.getWrapperElement());
1602
+ textarea.style.display = "";
1603
+ if (textarea.form) {
1604
+ rmSubmit();
1605
+ if (typeof textarea.form.submit == "function")
1606
+ textarea.form.submit = realSubmit;
1607
+ }
1608
+ };
1609
+ return instance;
1610
+ };
1611
+
1612
+ // Utility functions for working with state. Exported because modes
1613
+ // sometimes need to do this.
1614
+ function copyState(mode, state) {
1615
+ if (state === true) return state;
1616
+ if (mode.copyState) return mode.copyState(state);
1617
+ var nstate = {};
1618
+ for (var n in state) {
1619
+ var val = state[n];
1620
+ if (val instanceof Array) val = val.concat([]);
1621
+ nstate[n] = val;
1622
+ }
1623
+ return nstate;
1624
+ }
1625
+ CodeMirror.startState = startState;
1626
+ function startState(mode, a1, a2) {
1627
+ return mode.startState ? mode.startState(a1, a2) : true;
1628
+ }
1629
+ CodeMirror.copyState = copyState;
1630
+
1631
+ // The character stream used by a mode's parser.
1632
+ function StringStream(string) {
1633
+ this.pos = this.start = 0;
1634
+ this.string = string;
1635
+ }
1636
+ StringStream.prototype = {
1637
+ eol: function() {return this.pos >= this.string.length;},
1638
+ sol: function() {return this.pos == 0;},
1639
+ peek: function() {return this.string.charAt(this.pos);},
1640
+ next: function() {
1641
+ if (this.pos < this.string.length)
1642
+ return this.string.charAt(this.pos++);
1643
+ },
1644
+ eat: function(match) {
1645
+ var ch = this.string.charAt(this.pos);
1646
+ if (typeof match == "string") var ok = ch == match;
1647
+ else var ok = ch && (match.test ? match.test(ch) : match(ch));
1648
+ if (ok) {++this.pos; return ch;}
1649
+ },
1650
+ eatWhile: function(match) {
1651
+ var start = this.start;
1652
+ while (this.eat(match)){}
1653
+ return this.pos > start;
1654
+ },
1655
+ eatSpace: function() {
1656
+ var start = this.pos;
1657
+ while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
1658
+ return this.pos > start;
1659
+ },
1660
+ skipToEnd: function() {this.pos = this.string.length;},
1661
+ skipTo: function(ch) {
1662
+ var found = this.string.indexOf(ch, this.pos);
1663
+ if (found > -1) {this.pos = found; return true;}
1664
+ },
1665
+ backUp: function(n) {this.pos -= n;},
1666
+ column: function() {return countColumn(this.string, this.start);},
1667
+ indentation: function() {return countColumn(this.string);},
1668
+ match: function(pattern, consume, caseInsensitive) {
1669
+ if (typeof pattern == "string") {
1670
+ function cased(str) {return caseInsensitive ? str.toLowerCase() : str;}
1671
+ if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) {
1672
+ if (consume !== false) this.pos += pattern.length;
1673
+ return true;
1674
+ }
1675
+ }
1676
+ else {
1677
+ var match = this.string.slice(this.pos).match(pattern);
1678
+ if (match && consume !== false) this.pos += match[0].length;
1679
+ return match;
1680
+ }
1681
+ },
1682
+ current: function(){return this.string.slice(this.start, this.pos);}
1683
+ };
1684
+ CodeMirror.StringStream = StringStream;
1685
+
1686
+ // Line objects. These hold state related to a line, including
1687
+ // highlighting info (the styles array).
1688
+ function Line(text, styles) {
1689
+ this.styles = styles || [text, null];
1690
+ this.stateAfter = null;
1691
+ this.text = text;
1692
+ this.marked = this.gutterMarker = this.className = null;
1693
+ }
1694
+ Line.prototype = {
1695
+ // Replace a piece of a line, keeping the styles around it intact.
1696
+ replace: function(from, to, text) {
1697
+ var st = [], mk = this.marked;
1698
+ copyStyles(0, from, this.styles, st);
1699
+ if (text) st.push(text, null);
1700
+ copyStyles(to, this.text.length, this.styles, st);
1701
+ this.styles = st;
1702
+ this.text = this.text.slice(0, from) + text + this.text.slice(to);
1703
+ this.stateAfter = null;
1704
+ if (mk) {
1705
+ var diff = text.length - (to - from), end = this.text.length;
1706
+ function fix(n) {return n <= Math.min(to, to + diff) ? n : n + diff;}
1707
+ for (var i = 0; i < mk.length; ++i) {
1708
+ var mark = mk[i], del = false;
1709
+ if (mark.from >= end) del = true;
1710
+ else {mark.from = fix(mark.from); if (mark.to != null) mark.to = fix(mark.to);}
1711
+ if (del || mark.from >= mark.to) {mk.splice(i, 1); i--;}
1712
+ }
1713
+ }
1714
+ },
1715
+ // Split a line in two, again keeping styles intact.
1716
+ split: function(pos, textBefore) {
1717
+ var st = [textBefore, null];
1718
+ copyStyles(pos, this.text.length, this.styles, st);
1719
+ return new Line(textBefore + this.text.slice(pos), st);
1720
+ },
1721
+ addMark: function(from, to, style) {
1722
+ var mk = this.marked, mark = {from: from, to: to, style: style};
1723
+ if (this.marked == null) this.marked = [];
1724
+ this.marked.push(mark);
1725
+ this.marked.sort(function(a, b){return a.from - b.from;});
1726
+ return mark;
1727
+ },
1728
+ removeMark: function(mark) {
1729
+ var mk = this.marked;
1730
+ if (!mk) return;
1731
+ for (var i = 0; i < mk.length; ++i)
1732
+ if (mk[i] == mark) {mk.splice(i, 1); break;}
1733
+ },
1734
+ // Run the given mode's parser over a line, update the styles
1735
+ // array, which contains alternating fragments of text and CSS
1736
+ // classes.
1737
+ highlight: function(mode, state) {
1738
+ var stream = new StringStream(this.text), st = this.styles, pos = 0;
1739
+ var changed = false, curWord = st[0], prevWord;
1740
+ if (this.text == "" && mode.blankLine) mode.blankLine(state);
1741
+ while (!stream.eol()) {
1742
+ var style = mode.token(stream, state);
1743
+ var substr = this.text.slice(stream.start, stream.pos);
1744
+ stream.start = stream.pos;
1745
+ if (pos && st[pos-1] == style)
1746
+ st[pos-2] += substr;
1747
+ else if (substr) {
1748
+ if (!changed && (st[pos+1] != style || (pos && st[pos-2] != prevWord))) changed = true;
1749
+ st[pos++] = substr; st[pos++] = style;
1750
+ prevWord = curWord; curWord = st[pos];
1751
+ }
1752
+ // Give up when line is ridiculously long
1753
+ if (stream.pos > 5000) {
1754
+ st[pos++] = this.text.slice(stream.pos); st[pos++] = null;
1755
+ break;
1756
+ }
1757
+ }
1758
+ if (st.length != pos) {st.length = pos; changed = true;}
1759
+ if (pos && st[pos-2] != prevWord) changed = true;
1760
+ // Short lines with simple highlights always count as changed,
1761
+ // because they are likely to highlight the same way in various
1762
+ // contexts.
1763
+ return changed || (st.length < 5 && this.text.length < 10);
1764
+ },
1765
+ // Fetch the parser token for a given character. Useful for hacks
1766
+ // that want to inspect the mode state (say, for completion).
1767
+ getTokenAt: function(mode, state, ch) {
1768
+ var txt = this.text, stream = new StringStream(txt);
1769
+ while (stream.pos < ch && !stream.eol()) {
1770
+ stream.start = stream.pos;
1771
+ var style = mode.token(stream, state);
1772
+ }
1773
+ return {start: stream.start,
1774
+ end: stream.pos,
1775
+ string: stream.current(),
1776
+ className: style || null,
1777
+ state: state};
1778
+ },
1779
+ indentation: function() {return countColumn(this.text);},
1780
+ // Produces an HTML fragment for the line, taking selection,
1781
+ // marking, and highlighting into account.
1782
+ getHTML: function(sfrom, sto, includePre, endAt) {
1783
+ var html = [];
1784
+ if (includePre)
1785
+ html.push(this.className ? '<pre class="' + this.className + '">': "<pre>");
1786
+ function span(text, style) {
1787
+ if (!text) return;
1788
+ if (style) html.push('<span class="cm-', style, '">', htmlEscape(text), "</span>");
1789
+ else html.push(htmlEscape(text));
1790
+ }
1791
+ var st = this.styles, allText = this.text, marked = this.marked;
1792
+ if (sfrom == sto) sfrom = null;
1793
+ var len = allText.length;
1794
+ if (endAt != null) len = Math.min(endAt, len);
1795
+
1796
+ if (!allText && endAt == null)
1797
+ span(" ", sfrom != null && sto == null ? "CodeMirror-selected" : null);
1798
+ else if (!marked && sfrom == null)
1799
+ for (var i = 0, ch = 0; ch < len; i+=2) {
1800
+ var str = st[i], l = str.length;
1801
+ if (ch + l > len) str = str.slice(0, len - ch);
1802
+ ch += l;
1803
+ span(str, st[i+1]);
1804
+ }
1805
+ else {
1806
+ var pos = 0, i = 0, text = "", style, sg = 0;
1807
+ var markpos = -1, mark = null;
1808
+ function nextMark() {
1809
+ if (marked) {
1810
+ markpos += 1;
1811
+ mark = (markpos < marked.length) ? marked[markpos] : null;
1812
+ }
1813
+ }
1814
+ nextMark();
1815
+ while (pos < len) {
1816
+ var upto = len;
1817
+ var extraStyle = "";
1818
+ if (sfrom != null) {
1819
+ if (sfrom > pos) upto = sfrom;
1820
+ else if (sto == null || sto > pos) {
1821
+ extraStyle = " CodeMirror-selected";
1822
+ if (sto != null) upto = Math.min(upto, sto);
1823
+ }
1824
+ }
1825
+ while (mark && mark.to != null && mark.to <= pos) nextMark();
1826
+ if (mark) {
1827
+ if (mark.from > pos) upto = Math.min(upto, mark.from);
1828
+ else {
1829
+ extraStyle += " " + mark.style;
1830
+ if (mark.to != null) upto = Math.min(upto, mark.to);
1831
+ }
1832
+ }
1833
+ for (;;) {
1834
+ var end = pos + text.length;
1835
+ var apliedStyle = style;
1836
+ if (extraStyle) apliedStyle = style ? style + extraStyle : extraStyle;
1837
+ span(end > upto ? text.slice(0, upto - pos) : text, apliedStyle);
1838
+ if (end >= upto) {text = text.slice(upto - pos); pos = upto; break;}
1839
+ pos = end;
1840
+ text = st[i++]; style = st[i++];
1841
+ }
1842
+ }
1843
+ if (sfrom != null && sto == null) span(" ", "CodeMirror-selected");
1844
+ }
1845
+ if (includePre) html.push("</pre>");
1846
+ return html.join("");
1847
+ }
1848
+ };
1849
+ // Utility used by replace and split above
1850
+ function copyStyles(from, to, source, dest) {
1851
+ for (var i = 0, pos = 0, state = 0; pos < to; i+=2) {
1852
+ var part = source[i], end = pos + part.length;
1853
+ if (state == 0) {
1854
+ if (end > from) dest.push(part.slice(from - pos, Math.min(part.length, to - pos)), source[i+1]);
1855
+ if (end >= from) state = 1;
1856
+ }
1857
+ else if (state == 1) {
1858
+ if (end > to) dest.push(part.slice(0, to - pos), source[i+1]);
1859
+ else dest.push(part, source[i+1]);
1860
+ }
1861
+ pos = end;
1862
+ }
1863
+ }
1864
+
1865
+ // The history object 'chunks' changes that are made close together
1866
+ // and at almost the same time into bigger undoable units.
1867
+ function History() {
1868
+ this.time = 0;
1869
+ this.done = []; this.undone = [];
1870
+ }
1871
+ History.prototype = {
1872
+ addChange: function(start, added, old) {
1873
+ this.undone.length = 0;
1874
+ var time = +new Date, last = this.done[this.done.length - 1];
1875
+ if (time - this.time > 400 || !last ||
1876
+ last.start > start + added || last.start + last.added < start - last.added + last.old.length)
1877
+ this.done.push({start: start, added: added, old: old});
1878
+ else {
1879
+ var oldoff = 0;
1880
+ if (start < last.start) {
1881
+ for (var i = last.start - start - 1; i >= 0; --i)
1882
+ last.old.unshift(old[i]);
1883
+ last.added += last.start - start;
1884
+ last.start = start;
1885
+ }
1886
+ else if (last.start < start) {
1887
+ oldoff = start - last.start;
1888
+ added += oldoff;
1889
+ }
1890
+ for (var i = last.added - oldoff, e = old.length; i < e; ++i)
1891
+ last.old.push(old[i]);
1892
+ if (last.added < added) last.added = added;
1893
+ }
1894
+ this.time = time;
1895
+ }
1896
+ };
1897
+
1898
+ // Event stopping compatibility wrapper.
1899
+ function stopEvent() {
1900
+ if (this.preventDefault) {this.preventDefault(); this.stopPropagation();}
1901
+ else {this.returnValue = false; this.cancelBubble = true;}
1902
+ }
1903
+ // Ensure an event has a stop method.
1904
+ function addStop(event) {
1905
+ if (!event.stop) event.stop = stopEvent;
1906
+ return event;
1907
+ }
1908
+
1909
+ // Event wrapper, exposing the few operations we need.
1910
+ function Event(orig) {this.e = orig;}
1911
+ Event.prototype = {
1912
+ stop: function() {stopEvent.call(this.e);},
1913
+ target: function() {return this.e.target || this.e.srcElement;},
1914
+ button: function() {
1915
+ if (this.e.which) return this.e.which;
1916
+ else if (this.e.button & 1) return 1;
1917
+ else if (this.e.button & 2) return 3;
1918
+ else if (this.e.button & 4) return 2;
1919
+ },
1920
+ pageX: function() {
1921
+ if (this.e.pageX != null) return this.e.pageX;
1922
+ var doc = this.target().ownerDocument;
1923
+ return this.e.clientX + doc.body.scrollLeft + doc.documentElement.scrollLeft;
1924
+ },
1925
+ pageY: function() {
1926
+ if (this.e.pageY != null) return this.e.pageY;
1927
+ var doc = this.target().ownerDocument;
1928
+ return this.e.clientY + doc.body.scrollTop + doc.documentElement.scrollTop;
1929
+ }
1930
+ };
1931
+
1932
+ // Event handler registration. If disconnect is true, it'll return a
1933
+ // function that unregisters the handler.
1934
+ function connect(node, type, handler, disconnect) {
1935
+ function wrapHandler(event) {handler(new Event(event || window.event));}
1936
+ if (typeof node.addEventListener == "function") {
1937
+ node.addEventListener(type, wrapHandler, false);
1938
+ if (disconnect) return function() {node.removeEventListener(type, wrapHandler, false);};
1939
+ }
1940
+ else {
1941
+ node.attachEvent("on" + type, wrapHandler);
1942
+ if (disconnect) return function() {node.detachEvent("on" + type, wrapHandler);};
1943
+ }
1944
+ }
1945
+
1946
+ function Delayed() {this.id = null;}
1947
+ Delayed.prototype = {set: function(ms, f) {clearTimeout(this.id); this.id = setTimeout(f, ms);}};
1948
+
1949
+ // Some IE versions don't preserve whitespace when setting the
1950
+ // innerHTML of a PRE tag.
1951
+ var badInnerHTML = (function() {
1952
+ var pre = document.createElement("pre");
1953
+ pre.innerHTML = " "; return !pre.innerHTML;
1954
+ })();
1955
+
1956
+ var gecko = /gecko\/\d{7}/i.test(navigator.userAgent);
1957
+ var ie = /MSIE \d/.test(navigator.userAgent);
1958
+ var safari = /Apple Computer/.test(navigator.vendor);
1959
+
1960
+ var lineSep = "\n";
1961
+ // Feature-detect whether newlines in textareas are converted to \r\n
1962
+ (function () {
1963
+ var te = document.createElement("textarea");
1964
+ te.value = "foo\nbar";
1965
+ if (te.value.indexOf("\r") > -1) lineSep = "\r\n";
1966
+ }());
1967
+
1968
+ var tabSize = 8;
1969
+ var mac = /Mac/.test(navigator.platform);
1970
+ var movementKeys = {};
1971
+ for (var i = 35; i <= 40; ++i)
1972
+ movementKeys[i] = movementKeys["c" + i] = true;
1973
+
1974
+ // Counts the column offset in a string, taking tabs into account.
1975
+ // Used mostly to find indentation.
1976
+ function countColumn(string, end) {
1977
+ if (end == null) {
1978
+ end = string.search(/[^\s\u00a0]/);
1979
+ if (end == -1) end = string.length;
1980
+ }
1981
+ for (var i = 0, n = 0; i < end; ++i) {
1982
+ if (string.charAt(i) == "\t") n += tabSize - (n % tabSize);
1983
+ else ++n;
1984
+ }
1985
+ return n;
1986
+ }
1987
+
1988
+ function computedStyle(elt) {
1989
+ if (elt.currentStyle) return elt.currentStyle;
1990
+ return window.getComputedStyle(elt, null);
1991
+ }
1992
+ // Find the position of an element by following the offsetParent chain.
1993
+ // If screen==true, it returns screen (rather than page) coordinates.
1994
+ function eltOffset(node, screen) {
1995
+ var doc = node.ownerDocument.body;
1996
+ var x = 0, y = 0, skipDoc = false;
1997
+ for (var n = node; n; n = n.offsetParent) {
1998
+ x += n.offsetLeft; y += n.offsetTop;
1999
+ if (screen && computedStyle(n).position == "fixed")
2000
+ skipDoc = true;
2001
+ }
2002
+ var e = screen && !skipDoc ? null : doc;
2003
+ for (var n = node.parentNode; n != e; n = n.parentNode)
2004
+ if (n.scrollLeft != null) { x -= n.scrollLeft; y -= n.scrollTop;}
2005
+ return {left: x, top: y};
2006
+ }
2007
+ // Get a node's text content.
2008
+ function eltText(node) {
2009
+ return node.textContent || node.innerText || node.nodeValue || "";
2010
+ }
2011
+
2012
+ // Operations on {line, ch} objects.
2013
+ function posEq(a, b) {return a.line == b.line && a.ch == b.ch;}
2014
+ function posLess(a, b) {return a.line < b.line || (a.line == b.line && a.ch < b.ch);}
2015
+ function copyPos(x) {return {line: x.line, ch: x.ch};}
2016
+
2017
+ function htmlEscape(str) {
2018
+ return str.replace(/[<>&]/g, function(str) {
2019
+ return str == "&" ? "&amp;" : str == "<" ? "&lt;" : "&gt;";
2020
+ });
2021
+ }
2022
+ CodeMirror.htmlEscape = htmlEscape;
2023
+
2024
+ // Used to position the cursor after an undo/redo by finding the
2025
+ // last edited character.
2026
+ function editEnd(from, to) {
2027
+ if (!to) return from ? from.length : 0;
2028
+ if (!from) return to.length;
2029
+ for (var i = from.length, j = to.length; i >= 0 && j >= 0; --i, --j)
2030
+ if (from.charAt(i) != to.charAt(j)) break;
2031
+ return j + 1;
2032
+ }
2033
+
2034
+ function indexOf(collection, elt) {
2035
+ if (collection.indexOf) return collection.indexOf(elt);
2036
+ for (var i = 0, e = collection.length; i < e; ++i)
2037
+ if (collection[i] == elt) return i;
2038
+ return -1;
2039
+ }
2040
+
2041
+ // See if "".split is the broken IE version, if so, provide an
2042
+ // alternative way to split lines.
2043
+ var splitLines, selRange, setSelRange;
2044
+ if ("\n\nb".split(/\n/).length != 3)
2045
+ splitLines = function(string) {
2046
+ var pos = 0, nl, result = [];
2047
+ while ((nl = string.indexOf("\n", pos)) > -1) {
2048
+ result.push(string.slice(pos, string.charAt(nl-1) == "\r" ? nl - 1 : nl));
2049
+ pos = nl + 1;
2050
+ }
2051
+ result.push(string.slice(pos));
2052
+ return result;
2053
+ };
2054
+ else
2055
+ splitLines = function(string){return string.split(/\r?\n/);};
2056
+ CodeMirror.splitLines = splitLines;
2057
+
2058
+ // Sane model of finding and setting the selection in a textarea
2059
+ if (window.getSelection) {
2060
+ selRange = function(te) {
2061
+ try {return {start: te.selectionStart, end: te.selectionEnd};}
2062
+ catch(e) {return null;}
2063
+ };
2064
+ if (safari)
2065
+ // On Safari, selection set with setSelectionRange are in a sort
2066
+ // of limbo wrt their anchor. If you press shift-left in them,
2067
+ // the anchor is put at the end, and the selection expanded to
2068
+ // the left. If you press shift-right, the anchor ends up at the
2069
+ // front. This is not what CodeMirror wants, so it does a
2070
+ // spurious modify() call to get out of limbo.
2071
+ setSelRange = function(te, start, end) {
2072
+ if (start == end)
2073
+ te.setSelectionRange(start, end);
2074
+ else {
2075
+ te.setSelectionRange(start, end - 1);
2076
+ window.getSelection().modify("extend", "forward", "character");
2077
+ }
2078
+ };
2079
+ else
2080
+ setSelRange = function(te, start, end) {
2081
+ try {te.setSelectionRange(start, end);}
2082
+ catch(e) {} // Fails on Firefox when textarea isn't part of the document
2083
+ };
2084
+ }
2085
+ // IE model. Don't ask.
2086
+ else {
2087
+ selRange = function(te) {
2088
+ try {var range = te.ownerDocument.selection.createRange();}
2089
+ catch(e) {return null;}
2090
+ if (!range || range.parentElement() != te) return null;
2091
+ var val = te.value, len = val.length, localRange = te.createTextRange();
2092
+ localRange.moveToBookmark(range.getBookmark());
2093
+ var endRange = te.createTextRange();
2094
+ endRange.collapse(false);
2095
+
2096
+ if (localRange.compareEndPoints("StartToEnd", endRange) > -1)
2097
+ return {start: len, end: len};
2098
+
2099
+ var start = -localRange.moveStart("character", -len);
2100
+ for (var i = val.indexOf("\r"); i > -1 && i < start; i = val.indexOf("\r", i+1), start++) {}
2101
+
2102
+ if (localRange.compareEndPoints("EndToEnd", endRange) > -1)
2103
+ return {start: start, end: len};
2104
+
2105
+ var end = -localRange.moveEnd("character", -len);
2106
+ for (var i = val.indexOf("\r"); i > -1 && i < end; i = val.indexOf("\r", i+1), end++) {}
2107
+ return {start: start, end: end};
2108
+ };
2109
+ setSelRange = function(te, start, end) {
2110
+ var range = te.createTextRange();
2111
+ range.collapse(true);
2112
+ var endrange = range.duplicate();
2113
+ var newlines = 0, txt = te.value;
2114
+ for (var pos = txt.indexOf("\n"); pos > -1 && pos < start; pos = txt.indexOf("\n", pos + 1))
2115
+ ++newlines;
2116
+ range.move("character", start - newlines);
2117
+ for (; pos > -1 && pos < end; pos = txt.indexOf("\n", pos + 1))
2118
+ ++newlines;
2119
+ endrange.move("character", end - newlines);
2120
+ range.setEndPoint("EndToEnd", endrange);
2121
+ range.select();
2122
+ };
2123
+ }
2124
+
2125
+ CodeMirror.defineMode("null", function() {
2126
+ return {token: function(stream) {stream.skipToEnd();}};
2127
+ });
2128
+ CodeMirror.defineMIME("text/plain", "null");
2129
+
2130
+ return CodeMirror;
2131
+ })();