Press ctrl-space to activate autocompletion. The
+completion uses
+the anyword-hint.js
+module, which simply looks at nearby words in the buffer and completes
+to those.
Demonstration of
+ using linked documents
+ to provide a split view on a document, and
+ using swapDoc
+ to use a single editor to display multiple documents.
On changes to the content of the above editor, a (crude) script
+tries to auto-detect the language used, and switches the editor to
+either JavaScript or Scheme mode based on that.
The emacs keybindings are enabled by
+including keymap/emacs.js and setting
+the keyMap option to "emacs". Because
+CodeMirror's internal API is quite different from Emacs, they are only
+a loose approximation of actual emacs bindings, though.
+
+
Also note that a lot of browsers disallow certain keys from being
+captured. For example, Chrome blocks both Ctrl-W and Ctrl-N, with the
+result that idiomatic use of Emacs keys will constantly close your tab
+or open a new window.
Demonstration of
+ the fullscreen
+ addon. Press F11 when cursor is in the editor to
+ toggle full screen editing. Esc can also be used
+ to exit full screen editing.
Demonstration of
+the hardwrap addon.
+The above editor has its change event hooked up to
+the wrapParagraphsInRange method, so that the paragraphs
+are reflown as you are typing.
The merge
+addon provides an interface for displaying and merging diffs,
+either two-way
+or three-way. The left
+(or center) pane is editable, and the differences with the other
+pane(s) are optionally shown live as you edit it.
Demonstration of a multiplexing mode, which, at certain
+ boundary strings, switches to one or more inner modes. The out
+ (HTML) mode does not get fed the content of the <<
+ >> blocks. See
+ the manual and
+ the source for more
+ information.
Demonstration of a mode that parses HTML, highlighting
+ the Mustache templating
+ directives inside of it by using the code
+ in overlay.js. View
+ source to see the 15 lines of code needed to accomplish this.
The placeholder
+ plug-in adds an option placeholder that can be set to
+ make text appear in the editor when it is empty and not focused.
+ If the source textarea has a placeholder attribute,
+ it will automatically be inherited.
By setting a few CSS properties, and giving
+the viewportMargin
+a value of Infinity, CodeMirror can be made to
+automatically resize to fit its content.
If this is a function, it will be called for each token with
+ two arguments, the token's text and the token's style class (may
+ be null for unstyled tokens). If it is a DOM node,
+ the tokens will be converted to span elements as in
+ an editor, and inserted into the node
+ (through innerHTML).
Automatically derive odd wrapping behavior for your browser
+
+
+
This is a hack to automatically derive
+ a spanAffectsWrapping regexp for a browser. See the
+ comments above that variable
+ in lib/codemirror.js
+ for some more details.
The vim keybindings are enabled by
+including keymap/vim.js and setting
+the keyMap option to "vim". Because
+CodeMirror's internal API is quite different from Vim, they are only
+a loose approximation of actual vim bindings, though.
This demo runs JSHint over the code
+in the editor (which is the script used on this page), and
+inserts line widgets to
+display the warnings that JSHint comes up with.
Press ctrl-space, or type a '<' character to
+ activate autocompletion. This demo defines a simple schema that
+ guides completion. The schema can be customized—see
+ the manual.
+
+
Development of the xml-hint addon was kindly
+ sponsored
+ by www.xperiment.mobi.
+
+
+
diff --git a/plugins/codemirror/codemirror/doc/activebookmark.js b/plugins/codemirror/codemirror/doc/activebookmark.js
new file mode 100644
index 0000000..69a126e
--- /dev/null
+++ b/plugins/codemirror/codemirror/doc/activebookmark.js
@@ -0,0 +1,42 @@
+(function() {
+ var pending = false, prevVal = null;
+
+ function updateSoon() {
+ if (!pending) {
+ pending = true;
+ setTimeout(update, 250);
+ }
+ }
+
+ function update() {
+ pending = false;
+ var marks = document.getElementById("nav").getElementsByTagName("a"), found;
+ for (var i = 0; i < marks.length; ++i) {
+ var mark = marks[i], m;
+ if (mark.getAttribute("data-default")) {
+ if (found == null) found = i;
+ } else if (m = mark.href.match(/#(.*)/)) {
+ var ref = document.getElementById(m[1]);
+ if (ref && ref.getBoundingClientRect().top < 50)
+ found = i;
+ }
+ }
+ if (found != null && found != prevVal) {
+ prevVal = found;
+ var lis = document.getElementById("nav").getElementsByTagName("li");
+ for (var i = 0; i < lis.length; ++i) lis[i].className = "";
+ for (var i = 0; i < marks.length; ++i) {
+ if (found == i) {
+ marks[i].className = "active";
+ for (var n = marks[i]; n; n = n.parentNode)
+ if (n.nodeName == "LI") n.className = "active";
+ } else {
+ marks[i].className = "";
+ }
+ }
+ }
+ }
+
+ window.addEventListener("scroll", updateSoon);
+ window.addEventListener("load", updateSoon);
+})();
diff --git a/plugins/codemirror/codemirror/doc/compress.html b/plugins/codemirror/codemirror/doc/compress.html
new file mode 100644
index 0000000..3f3bfb0
--- /dev/null
+++ b/plugins/codemirror/codemirror/doc/compress.html
@@ -0,0 +1,234 @@
+
+
+CodeMirror: Compression Helper
+
+
+
+
To optimize loading CodeMirror, especially when including a
+ bunch of different modes, it is recommended that you combine and
+ minify (and preferably also gzip) the scripts. This page makes
+ those first two steps very easy. Simply select the version and
+ scripts you need in the form below, and
+ click Compress to download the minified script
+ file.
(Re-) Implementing A Syntax-Highlighting Editor in JavaScript
+
+
+ Topic: JavaScript, code editor implementation
+ Author: Marijn Haverbeke
+ Date: March 2nd 2011 (updated November 13th 2011)
+
+
+
Caution: this text was written briefly after
+version 2 was initially written. It no longer (even including the
+update at the bottom) fully represents the current implementation. I'm
+leaving it here as a historic document. For more up-to-date
+information, look at the entries
+tagged cm-internals
+on my blog.
+
+
This is a followup to
+my Brutal Odyssey to the
+Dark Side of the DOM Tree story. That one describes the
+mind-bending process of implementing (what would become) CodeMirror 1.
+This one describes the internals of CodeMirror 2, a complete rewrite
+and rethink of the old code base. I wanted to give this piece another
+Hunter Thompson copycat subtitle, but somehow that would be out of
+place—the process this time around was one of straightforward
+engineering, requiring no serious mind-bending whatsoever.
+
+
So, what is wrong with CodeMirror 1? I'd estimate, by mailing list
+activity and general search-engine presence, that it has been
+integrated into about a thousand systems by now. The most prominent
+one, since a few weeks,
+being Google
+code's project hosting. It works, and it's being used widely.
+
+
Still, I did not start replacing it because I was bored. CodeMirror
+1 was heavily reliant on designMode
+or contentEditable (depending on the browser). Neither of
+these are well specified (HTML5 tries
+to specify
+their basics), and, more importantly, they tend to be one of the more
+obscure and buggy areas of browser functionality—CodeMirror, by using
+this functionality in a non-typical way, was constantly running up
+against browser bugs. WebKit wouldn't show an empty line at the end of
+the document, and in some releases would suddenly get unbearably slow.
+Firefox would show the cursor in the wrong place. Internet Explorer
+would insist on linkifying everything that looked like a URL or email
+address, a behaviour that can't be turned off. Some bugs I managed to
+work around (which was often a frustrating, painful process), others,
+such as the Firefox cursor placement, I gave up on, and had to tell
+user after user that they were known problems, but not something I
+could help.
+
+
Also, there is the fact that designMode (which seemed
+to be less buggy than contentEditable in Webkit and
+Firefox, and was thus used by CodeMirror 1 in those browsers) requires
+a frame. Frames are another tricky area. It takes some effort to
+prevent getting tripped up by domain restrictions, they don't
+initialize synchronously, behave strangely in response to the back
+button, and, on several browsers, can't be moved around the DOM
+without having them re-initialize. They did provide a very nice way to
+namespace the library, though—CodeMirror 1 could freely pollute the
+namespace inside the frame.
+
+
Finally, working with an editable document means working with
+selection in arbitrary DOM structures. Internet Explorer (8 and
+before) has an utterly different (and awkward) selection API than all
+of the other browsers, and even among the different implementations of
+document.selection, details about how exactly a selection
+is represented vary quite a bit. Add to that the fact that Opera's
+selection support tended to be very buggy until recently, and you can
+imagine why CodeMirror 1 contains 700 lines of selection-handling
+code.
+
+
And that brings us to the main issue with the CodeMirror 1
+code base: The proportion of browser-bug-workarounds to real
+application code was getting dangerously high. By building on top of a
+few dodgy features, I put the system in a vulnerable position—any
+incompatibility and bugginess in these features, I had to paper over
+with my own code. Not only did I have to do some serious stunt-work to
+get it to work on older browsers (as detailed in the
+previous story), things
+also kept breaking in newly released versions, requiring me to come up
+with new scary hacks in order to keep up. This was starting
+to lose its appeal.
+
+
+
General Approach
+
+
What CodeMirror 2 does is try to sidestep most of the hairy hacks
+that came up in version 1. I owe a lot to the
+ACE editor for inspiration on how to
+approach this.
+
+
I absolutely did not want to be completely reliant on key events to
+generate my input. Every JavaScript programmer knows that key event
+information is horrible and incomplete. Some people (most awesomely
+Mihai Bazon with Ymacs) have been able
+to build more or less functioning editors by directly reading key
+events, but it takes a lot of work (the kind of never-ending, fragile
+work I described earlier), and will never be able to properly support
+things like multi-keystoke international character
+input. [see below for caveat]
+
+
So what I do is focus a hidden textarea, and let the browser
+believe that the user is typing into that. What we show to the user is
+a DOM structure we built to represent his document. If this is updated
+quickly enough, and shows some kind of believable cursor, it feels
+like a real text-input control.
+
+
Another big win is that this DOM representation does not have to
+span the whole document. Some CodeMirror 1 users insisted that they
+needed to put a 30 thousand line XML document into CodeMirror. Putting
+all that into the DOM takes a while, especially since, for some
+reason, an editable DOM tree is slower than a normal one on most
+browsers. If we have full control over what we show, we must only
+ensure that the visible part of the document has been added, and can
+do the rest only when needed. (Fortunately, the onscroll
+event works almost the same on all browsers, and lends itself well to
+displaying things only as they are scrolled into view.)
+
+
+
Input
+
+
ACE uses its hidden textarea only as a text input shim, and does
+all cursor movement and things like text deletion itself by directly
+handling key events. CodeMirror's way is to let the browser do its
+thing as much as possible, and not, for example, define its own set of
+key bindings. One way to do this would have been to have the whole
+document inside the hidden textarea, and after each key event update
+the display DOM to reflect what's in that textarea.
+
+
That'd be simple, but it is not realistic. For even medium-sized
+document the editor would be constantly munging huge strings, and get
+terribly slow. What CodeMirror 2 does is put the current selection,
+along with an extra line on the top and on the bottom, into the
+textarea.
+
+
This means that the arrow keys (and their ctrl-variations), home,
+end, etcetera, do not have to be handled specially. We just read the
+cursor position in the textarea, and update our cursor to match it.
+Also, copy and paste work pretty much for free, and people get their
+native key bindings, without any special work on my part. For example,
+I have emacs key bindings configured for Chrome and Firefox. There is
+no way for a script to detect this. [no longer the case]
+
+
Of course, since only a small part of the document sits in the
+textarea, keys like page up and ctrl-end won't do the right thing.
+CodeMirror is catching those events and handling them itself.
+
+
+
Selection
+
+
Getting and setting the selection range of a textarea in modern
+browsers is trivial—you just use the selectionStart
+and selectionEnd properties. On IE you have to do some
+insane stuff with temporary ranges and compensating for the fact that
+moving the selection by a 'character' will treat \r\n as a single
+character, but even there it is possible to build functions that
+reliably set and get the selection range.
+
+
But consider this typical case: When I'm somewhere in my document,
+press shift, and press the up arrow, something gets selected. Then, if
+I, still holding shift, press the up arrow again, the top of my
+selection is adjusted. The selection remembers where its head
+and its anchor are, and moves the head when we shift-move.
+This is a generally accepted property of selections, and done right by
+every editing component built in the past twenty years.
+
+
But not something that the browser selection APIs expose.
+
+
Great. So when someone creates an 'upside-down' selection, the next
+time CodeMirror has to update the textarea, it'll re-create the
+selection as an 'upside-up' selection, with the anchor at the top, and
+the next cursor motion will behave in an unexpected way—our second
+up-arrow press in the example above will not do anything, since it is
+interpreted in exactly the same way as the first.
+
+
No problem. We'll just, ehm, detect that the selection is
+upside-down (you can tell by the way it was created), and then, when
+an upside-down selection is present, and a cursor-moving key is
+pressed in combination with shift, we quickly collapse the selection
+in the textarea to its start, allow the key to take effect, and then
+combine its new head with its old anchor to get the real
+selection.
+
+
In short, scary hacks could not be avoided entirely in CodeMirror
+2.
+
+
And, the observant reader might ask, how do you even know that a
+key combo is a cursor-moving combo, if you claim you support any
+native key bindings? Well, we don't, but we can learn. The editor
+keeps a set known cursor-movement combos (initialized to the
+predictable defaults), and updates this set when it observes that
+pressing a certain key had (only) the effect of moving the cursor.
+This, of course, doesn't work if the first time the key is used was
+for extending an inverted selection, but it works most of the
+time.
+
+
+
Intelligent Updating
+
+
One thing that always comes up when you have a complicated internal
+state that's reflected in some user-visible external representation
+(in this case, the displayed code and the textarea's content) is
+keeping the two in sync. The naive way is to just update the display
+every time you change your state, but this is not only error prone
+(you'll forget), it also easily leads to duplicate work on big,
+composite operations. Then you start passing around flags indicating
+whether the display should be updated in an attempt to be efficient
+again and, well, at that point you might as well give up completely.
+
+
I did go down that road, but then switched to a much simpler model:
+simply keep track of all the things that have been changed during an
+action, and then, only at the end, use this information to update the
+user-visible display.
+
+
CodeMirror uses a concept of operations, which start by
+calling a specific set-up function that clears the state and end by
+calling another function that reads this state and does the required
+updating. Most event handlers, and all the user-visible methods that
+change state are wrapped like this. There's a method
+called operation that accepts a function, and returns
+another function that wraps the given function as an operation.
+
+
It's trivial to extend this (as CodeMirror does) to detect nesting,
+and, when an operation is started inside an operation, simply
+increment the nesting count, and only do the updating when this count
+reaches zero again.
+
+
If we have a set of changed ranges and know the currently shown
+range, we can (with some awkward code to deal with the fact that
+changes can add and remove lines, so we're dealing with a changing
+coordinate system) construct a map of the ranges that were left
+intact. We can then compare this map with the part of the document
+that's currently visible (based on scroll offset and editor height) to
+determine whether something needs to be updated.
+
+
CodeMirror uses two update algorithms—a full refresh, where it just
+discards the whole part of the DOM that contains the edited text and
+rebuilds it, and a patch algorithm, where it uses the information
+about changed and intact ranges to update only the out-of-date parts
+of the DOM. When more than 30 percent (which is the current heuristic,
+might change) of the lines need to be updated, the full refresh is
+chosen (since it's faster to do than painstakingly finding and
+updating all the changed lines), in the other case it does the
+patching (so that, if you scroll a line or select another character,
+the whole screen doesn't have to be
+re-rendered). [the full-refresh
+algorithm was dropped, it wasn't really faster than the patching
+one]
+
+
All updating uses innerHTML rather than direct DOM
+manipulation, since that still seems to be by far the fastest way to
+build documents. There's a per-line function that combines the
+highlighting, marking, and
+selection info for that line into a snippet of HTML. The patch updater
+uses this to reset individual lines, the refresh updater builds an
+HTML chunk for the whole visible document at once, and then uses a
+single innerHTML update to do the refresh.
+
+
+
Parsers can be Simple
+
+
When I wrote CodeMirror 1, I
+thought interruptable
+parsers were a hugely scary and complicated thing, and I used a
+bunch of heavyweight abstractions to keep this supposed complexity
+under control: parsers
+were iterators
+that consumed input from another iterator, and used funny
+closure-resetting tricks to copy and resume themselves.
+
+
This made for a rather nice system, in that parsers formed strictly
+separate modules, and could be composed in predictable ways.
+Unfortunately, it was quite slow (stacking three or four iterators on
+top of each other), and extremely intimidating to people not used to a
+functional programming style.
+
+
With a few small changes, however, we can keep all those
+advantages, but simplify the API and make the whole thing less
+indirect and inefficient. CodeMirror
+2's mode API uses explicit state
+objects, and makes the parser/tokenizer a function that simply takes a
+state and a character stream abstraction, advances the stream one
+token, and returns the way the token should be styled. This state may
+be copied, optionally in a mode-defined way, in order to be able to
+continue a parse at a given point. Even someone who's never touched a
+lambda in his life can understand this approach. Additionally, far
+fewer objects are allocated in the course of parsing now.
+
+
The biggest speedup comes from the fact that the parsing no longer
+has to touch the DOM though. In CodeMirror 1, on an older browser, you
+could see the parser work its way through the document,
+managing some twenty lines in each 50-millisecond time slice it got. It
+was reading its input from the DOM, and updating the DOM as it went
+along, which any experienced JavaScript programmer will immediately
+spot as a recipe for slowness. In CodeMirror 2, the parser usually
+finishes the whole document in a single 100-millisecond time slice—it
+manages some 1500 lines during that time on Chrome. All it has to do
+is munge strings, so there is no real reason for it to be slow
+anymore.
+
+
+
What Gives?
+
+
Given all this, what can you expect from CodeMirror 2?
+
+
+
+
Small. the base library is
+some 45k when minified
+now, 17k when gzipped. It's smaller than
+its own logo.
+
+
Lightweight. CodeMirror 2 initializes very
+quickly, and does almost no work when it is not focused. This means
+you can treat it almost like a textarea, have multiple instances on a
+page without trouble.
+
+
Huge document support. Since highlighting is
+really fast, and no DOM structure is being built for non-visible
+content, you don't have to worry about locking up your browser when a
+user enters a megabyte-sized document.
+
+
Extended API. Some things kept coming up in the
+mailing list, such as marking pieces of text or lines, which were
+extremely hard to do with CodeMirror 1. The new version has proper
+support for these built in.
+
+
Tab support. Tabs inside editable documents were,
+for some reason, a no-go. At least six different people announced they
+were going to add tab support to CodeMirror 1, none survived (I mean,
+none delivered a working version). CodeMirror 2 no longer removes tabs
+from your document.
+
+
Sane styling.iframe nodes aren't
+really known for respecting document flow. Now that an editor instance
+is a plain div element, it is much easier to size it to
+fit the surrounding elements. You don't even have to make it scroll if
+you do not want to.
+
+
+
+
On the downside, a CodeMirror 2 instance is not a native
+editable component. Though it does its best to emulate such a
+component as much as possible, there is functionality that browsers
+just do not allow us to hook into. Doing select-all from the context
+menu, for example, is not currently detected by CodeMirror.
+
+
[Updates from November 13th 2011] Recently, I've made
+some changes to the codebase that cause some of the text above to no
+longer be current. I've left the text intact, but added markers at the
+passages that are now inaccurate. The new situation is described
+below.
+
+
+
Content Representation
+
+
The original implementation of CodeMirror 2 represented the
+document as a flat array of line objects. This worked well—splicing
+arrays will require the part of the array after the splice to be
+moved, but this is basically just a simple memmove of a
+bunch of pointers, so it is cheap even for huge documents.
+
+
However, I recently added line wrapping and code folding (line
+collapsing, basically). Once lines start taking up a non-constant
+amount of vertical space, looking up a line by vertical position
+(which is needed when someone clicks the document, and to determine
+the visible part of the document during scrolling) can only be done
+with a linear scan through the whole array, summing up line heights as
+you go. Seeing how I've been going out of my way to make big documents
+fast, this is not acceptable.
+
+
The new representation is based on a B-tree. The leaves of the tree
+contain arrays of line objects, with a fixed minimum and maximum size,
+and the non-leaf nodes simply hold arrays of child nodes. Each node
+stores both the amount of lines that live below them and the vertical
+space taken up by these lines. This allows the tree to be indexed both
+by line number and by vertical position, and all access has
+logarithmic complexity in relation to the document size.
+
+
I gave line objects and tree nodes parent pointers, to the node
+above them. When a line has to update its height, it can simply walk
+these pointers to the top of the tree, adding or subtracting the
+difference in height from each node it encounters. The parent pointers
+also make it cheaper (in complexity terms, the difference is probably
+tiny in normal-sized documents) to find the current line number when
+given a line object. In the old approach, the whole document array had
+to be searched. Now, we can just walk up the tree and count the sizes
+of the nodes coming before us at each level.
+
+
I chose B-trees, not regular binary trees, mostly because they
+allow for very fast bulk insertions and deletions. When there is a big
+change to a document, it typically involves adding, deleting, or
+replacing a chunk of subsequent lines. In a regular balanced tree, all
+these inserts or deletes would have to be done separately, which could
+be really expensive. In a B-tree, to insert a chunk, you just walk
+down the tree once to find where it should go, insert them all in one
+shot, and then break up the node if needed. This breaking up might
+involve breaking up nodes further up, but only requires a single pass
+back up the tree. For deletion, I'm somewhat lax in keeping things
+balanced—I just collapse nodes into a leaf when their child count goes
+below a given number. This means that there are some weird editing
+patterns that may result in a seriously unbalanced tree, but even such
+an unbalanced tree will perform well, unless you spend a day making
+strangely repeating edits to a really big document.
+
+
+
Keymaps
+
+
Above, I claimed that directly catching key
+events for things like cursor movement is impractical because it
+requires some browser-specific kludges. I then proceeded to explain
+some awful hacks that were needed to make it
+possible for the selection changes to be detected through the
+textarea. In fact, the second hack is about as bad as the first.
+
+
On top of that, in the presence of user-configurable tab sizes and
+collapsed and wrapped lines, lining up cursor movement in the textarea
+with what's visible on the screen becomes a nightmare. Thus, I've
+decided to move to a model where the textarea's selection is no longer
+depended on.
+
+
So I moved to a model where all cursor movement is handled by my
+own code. This adds support for a goal column, proper interaction of
+cursor movement with collapsed lines, and makes it possible for
+vertical movement to move through wrapped lines properly, instead of
+just treating them like non-wrapped lines.
+
+
The key event handlers now translate the key event into a string,
+something like Ctrl-Home or Shift-Cmd-R, and
+use that string to look up an action to perform. To make keybinding
+customizable, this lookup goes through
+a table, using a scheme that
+allows such tables to be chained together (for example, the default
+Mac bindings fall through to a table named 'emacsy', which defines
+basic Emacs-style bindings like Ctrl-F, and which is also
+used by the custom Emacs bindings).
+
+
A new
+option extraKeys
+allows ad-hoc keybindings to be defined in a much nicer way than what
+was possible with the
+old onKeyEvent
+callback. You simply provide an object mapping key identifiers to
+functions, instead of painstakingly looking at raw key events.
+
+
Built-in commands map to strings, rather than functions, for
+example "goLineUp" is the default action bound to the up
+arrow key. This allows new keymaps to refer to them without
+duplicating any code. New commands can be defined by assigning to
+the CodeMirror.commands object, which maps such commands
+to functions.
+
+
The hidden textarea now only holds the current selection, with no
+extra characters around it. This has a nice advantage: polling for
+input becomes much, much faster. If there's a big selection, this text
+does not have to be read from the textarea every time—when we poll,
+just noticing that something is still selected is enough to tell us
+that no new text was typed.
+
+
The reason that cheap polling is important is that many browsers do
+not fire useful events on IME (input method engine) input, which is
+the thing where people inputting a language like Japanese or Chinese
+use multiple keystrokes to create a character or sequence of
+characters. Most modern browsers fire input when the
+composing is finished, but many don't fire anything when the character
+is updated during composition. So we poll, whenever the
+editor is focused, to provide immediate updates of the display.
CodeMirror is a code-editor component that can be embedded in
+ Web pages. The core library provides only the editor
+ component, no accompanying buttons, auto-completion, or other IDE
+ functionality. It does provide a rich API on top of which such
+ functionality can be straightforwardly implemented. See
+ the addons included in the distribution,
+ and the list
+ of externally hosted addons, for reusable
+ implementations of extra features.
+
+
CodeMirror works with language-specific modes. Modes are
+ JavaScript programs that help color (and optionally indent) text
+ written in a given language. The distribution comes with a number
+ of modes (see the mode/
+ directory), and it isn't hard to write new
+ ones for other languages.
+
+
+
+
Basic Usage
+
+
The easiest way to use CodeMirror is to simply load the script
+ and style sheet found under lib/ in the distribution,
+ plus a mode script from one of the mode/ directories.
+ (See the compression helper for an
+ easy way to combine scripts.) For example:
Having done this, an editor instance can be created like
+ this:
+
+
var myCodeMirror = CodeMirror(document.body);
+
+
The editor will be appended to the document body, will start
+ empty, and will use the mode that we loaded. To have more control
+ over the new editor, a configuration object can be passed
+ to CodeMirror as a second
+ argument:
This will initialize the editor with a piece of code already in
+ it, and explicitly tell it to use the JavaScript mode (which is
+ useful when multiple modes are loaded).
+ See below for a full discussion of the
+ configuration options that CodeMirror accepts.
+
+
In cases where you don't want to append the editor to an
+ element, and need more control over the way it is inserted, the
+ first argument to the CodeMirror function can also
+ be a function that, when given a DOM element, inserts it into the
+ document somewhere. This could be used to, for example, replace a
+ textarea with a real editor:
However, for this use case, which is a common way to use
+ CodeMirror, the library provides a much more powerful
+ shortcut:
+
+
var myCodeMirror = CodeMirror.fromTextArea(myTextArea);
+
+
This will, among other things, ensure that the textarea's value
+ is updated with the editor's contents when the form (if it is part
+ of a form) is submitted. See the API
+ reference for a full description of this method.
+
+
+
+
Configuration
+
+
Both the CodeMirror
+ function and its fromTextArea method take as second
+ (optional) argument an object containing configuration options.
+ Any option not supplied like this will be taken
+ from CodeMirror.defaults, an
+ object containing the default options. You can update this object
+ to change the defaults on your page.
+
+
Options are not checked in any way, so setting bogus option
+ values is bound to lead to odd errors.
+
+
These are the supported options:
+
+
+
value: string|CodeMirror.Doc
+
The starting value of the editor. Can be a string, or
+ a document object.
+
+
mode: string|object
+
The mode to use. When not given, this will default to the
+ first mode that was loaded. It may be a string, which either
+ simply names the mode or is
+ a MIME type
+ associated with the mode. Alternatively, it may be an object
+ containing configuration options for the mode, with
+ a name property that names the mode (for
+ example {name: "javascript", json: true}). The demo
+ pages for each mode contain information about what configuration
+ parameters the mode supports. You can ask CodeMirror which modes
+ and MIME types have been defined by inspecting
+ the CodeMirror.modes
+ and CodeMirror.mimeModes objects. The first maps
+ mode names to their constructors, and the second maps MIME types
+ to mode specs.
+
+
theme: string
+
The theme to style the editor with. You must make sure the
+ CSS file defining the corresponding .cm-s-[name]
+ styles is loaded (see
+ the theme directory in the
+ distribution). The default is "default", for which
+ colors are included in codemirror.css. It is
+ possible to use multiple theming classes at once—for
+ example "foo bar" will assign both
+ the cm-s-foo and the cm-s-bar classes
+ to the editor.
+
+
indentUnit: integer
+
How many spaces a block (whatever that means in the edited
+ language) should be indented. The default is 2.
+
+
smartIndent: boolean
+
Whether to use the context-sensitive indentation that the
+ mode provides (or just indent the same as the line before).
+ Defaults to true.
+
+
tabSize: integer
+
The width of a tab character. Defaults to 4.
+
+
indentWithTabs: boolean
+
Whether, when indenting, the first N*tabSize
+ spaces should be replaced by N tabs. Default is false.
+
+
electricChars: boolean
+
Configures whether the editor should re-indent the current
+ line when a character is typed that might change its proper
+ indentation (only works if the mode supports indentation).
+ Default is true.
+
+
specialChars: RegExp
+
A regular expression used to determine which characters
+ should be replaced by a
+ special placeholder.
+ Mostly useful for non-printing special characters. The default
+ is /[\u0000-\u0019\u00ad\u200b\u2028\u2029\ufeff]/.
+
specialCharPlaceholder: function(char) → Element
+
A function that, given a special character identified by
+ the specialChars
+ option, produces a DOM node that is used to represent the
+ character. By default, a red dot (•)
+ is shown, with a title tooltip to indicate the character code.
+
+
rtlMoveVisually: boolean
+
Determines whether horizontal cursor movement through
+ right-to-left (Arabic, Hebrew) text is visual (pressing the left
+ arrow moves the cursor left) or logical (pressing the left arrow
+ moves to the next lower index in the string, which is visually
+ right in right-to-left text). The default is false
+ on Windows, and true on other platforms.
+
+
keyMap: string
+
Configures the keymap to use. The default
+ is "default", which is the only keymap defined
+ in codemirror.js itself. Extra keymaps are found in
+ the keymap directory. See
+ the section on keymaps for more
+ information.
+
+
extraKeys: object
+
Can be used to specify extra keybindings for the editor,
+ alongside the ones defined
+ by keyMap. Should be
+ either null, or a valid keymap value.
+
+
lineWrapping: boolean
+
Whether CodeMirror should scroll or wrap for long lines.
+ Defaults to false (scroll).
+
+
lineNumbers: boolean
+
Whether to show line numbers to the left of the editor.
+
+
firstLineNumber: integer
+
At which number to start counting lines. Default is 1.
A function used to format line numbers. The function is
+ passed the line number, and should return a string that will be
+ shown in the gutter.
+
+
gutters: array<string>
+
Can be used to add extra gutters (beyond or instead of the
+ line number gutter). Should be an array of CSS class names, each
+ of which defines a width (and optionally a
+ background), and which will be used to draw the background of
+ the gutters. May include
+ the CodeMirror-linenumbers class, in order to
+ explicitly set the position of the line number gutter (it will
+ default to be to the right of all other gutters). These class
+ names are the keys passed
+ to setGutterMarker.
+
+
fixedGutter: boolean
+
Determines whether the gutter scrolls along with the content
+ horizontally (false) or whether it stays fixed during horizontal
+ scrolling (true, the default).
+
+
coverGutterNextToScrollbar: boolean
+
When fixedGutter
+ is on, and there is a horizontal scrollbar, by default the
+ gutter will be visible to the left of this scrollbar. If this
+ option is set to true, it will be covered by an element with
+ class CodeMirror-gutter-filler.
+
+
readOnly: boolean|string
+
This disables editing of the editor content by the user. If
+ the special value "nocursor" is given (instead of
+ simply true), focusing of the editor is also
+ disallowed.
+
+
showCursorWhenSelecting: boolean
+
Whether the cursor should be drawn when a selection is
+ active. Defaults to false.
+
+
undoDepth: integer
+
The maximum number of undo levels that the editor stores.
+ Defaults to 40.
+
+
historyEventDelay: integer
+
The period of inactivity (in milliseconds) that will cause a
+ new history event to be started when typing or deleting.
+ Defaults to 500.
+
+
tabindex: integer
+
The tab
+ index to assign to the editor. If not given, no tab index
+ will be assigned.
+
+
autofocus: boolean
+
Can be used to make CodeMirror focus itself on
+ initialization. Defaults to off.
+ When fromTextArea is
+ used, and no explicit value is given for this option, it will be
+ set to true when either the source textarea is focused, or it
+ has an autofocus attribute and no other element is
+ focused.
+
+
+
Below this a few more specialized, low-level options are
+ listed. These are only useful in very specific situations, you
+ might want to skip them the first time you read this manual.
+
+
+
dragDrop: boolean
+
Controls whether drag-and-drop is enabled. On by default.
Deprecated! See these event
+ handlers for the current recommended approach. When given,
+ this will be called when the editor is handling
+ a dragenter, dragover,
+ or drop event. It will be passed the editor
+ instance and the event object as arguments. The callback can
+ choose to handle the event itself, in which case it should
+ return true to indicate that CodeMirror should not
+ do anything further.
Deprecated! See these event
+ handlers for the current recommended approach. This
+ provides a rather low-level hook into CodeMirror's key handling.
+ If provided, this function will be called on
+ every keydown, keyup,
+ and keypress event that CodeMirror captures. It
+ will be passed two arguments, the editor instance and the key
+ event. This key event is pretty much the raw key event, except
+ that a stop() method is always added to it. You
+ could feed it to, for example, jQuery.Event to
+ further normalize it. This function can inspect the key
+ event, and handle it if it wants to. It may return true to tell
+ CodeMirror to ignore the event. Be wary that, on some browsers,
+ stopping a keydown does not stop
+ the keypress from firing, whereas on others it
+ does. If you respond to an event, you should probably inspect
+ its type property and only do something when it
+ is keydown (or keypress for actions
+ that need character data).
+
+
cursorBlinkRate: number
+
Half-period in milliseconds used for cursor blinking. The default blink
+ rate is 530ms. By setting this to zero, blinking can be disabled.
+
+
cursorScrollMargin: number
+
How much extra space to always keep above and below the
+ cursor when approaching the top or bottom of the visible view in
+ a scrollable document. Default is 0.
+
+
cursorHeight: number
+
Determines the height of the cursor. Default is 1, meaning
+ it spans the whole height of the line. For some fonts (and by
+ some tastes) a smaller height (for example 0.85),
+ which causes the cursor to not reach all the way to the bottom
+ of the line, looks better
+
+
resetSelectionOnContextMenu: boolean
+
Controls whether, when the context menu is opened with a
+ click outside of the current selection, the cursor is moved to
+ the point of the click. Defaults to true.
+
+
workTime, workDelay: number
+
Highlighting is done by a pseudo background-thread that will
+ work for workTime milliseconds, and then use
+ timeout to sleep for workDelay milliseconds. The
+ defaults are 200 and 300, you can change these options to make
+ the highlighting more or less aggressive.
Indicates how quickly CodeMirror should poll its input
+ textarea for changes (when focused). Most input is captured by
+ events, but some things, like IME input on some browsers, don't
+ generate events that allow CodeMirror to properly detect it.
+ Thus, it polls. Default is 100 milliseconds.
+
+
flattenSpans: boolean
+
By default, CodeMirror will combine adjacent tokens into a
+ single span if they have the same class. This will result in a
+ simpler DOM tree, and thus perform better. With some kinds of
+ styling (such as rounded corners), this will change the way the
+ document looks. You can set this option to false to disable this
+ behavior.
+
+
maxHighlightLength: number
+
When highlighting long lines, in order to stay responsive,
+ the editor will give up and simply style the rest of the line as
+ plain text when it reaches a certain position. The default is
+ 10 000. You can set this to Infinity to turn off
+ this behavior.
+
+
crudeMeasuringFrom: number
+
When measuring the character positions in long lines, any
+ line longer than this number (default is 10 000),
+ when line wrapping
+ is off, will simply be assumed to consist of
+ same-sized characters. This means that, on the one hand,
+ measuring will be inaccurate when characters of varying size,
+ right-to-left text, markers, or other irregular elements are
+ present. On the other hand, it means that having such a line
+ won't freeze the user interface because of the expensiveness of
+ the measurements.
+
+
viewportMargin: integer
+
Specifies the amount of lines that are rendered above and
+ below the part of the document that's currently scrolled into
+ view. This affects the amount of updates needed when scrolling,
+ and the amount of work that such an update does. You should
+ usually leave it at its default, 10. Can be set
+ to Infinity to make sure the whole document is
+ always rendered, and thus the browser's text search works on it.
+ This will have bad effects on performance of big
+ documents.
+
+
+
+
+
Events
+
+
Various CodeMirror-related objects emit events, which allow
+ client code to react to various situations. Handlers for such
+ events can be registed with the on
+ and off methods on the objects
+ that the event fires on. To fire your own events,
+ use CodeMirror.signal(target, name, args...),
+ where target is a non-DOM-node object.
+
+
An editor instance fires the following events.
+ The instance argument always refers to the editor
+ itself.
Fires every time the content of the editor is changed.
+ The changeObj is a {from, to, text, removed,
+ next} object containing information about the changes
+ that occurred as second argument. from
+ and to are the positions (in the pre-change
+ coordinate system) where the change started and ended (for
+ example, it might be {ch:0, line:18} if the
+ position is at the beginning of line #19). text is
+ an array of strings representing the text that replaced the
+ changed range (split by line). removed is the text
+ that used to be between from and to,
+ which is overwritten by this change. If multiple changes
+ happened during a single operation, the object will have
+ a next property pointing to another change object
+ (which may point to another, etc).
This event is fired before a change is applied, and its
+ handler may choose to modify or cancel the change.
+ The changeObj object
+ has from, to, and text
+ properties, as with
+ the "change" event, but
+ never a next property, since this is fired for each
+ individual change, and not batched per operation. It also has
+ a cancel() method, which can be called to cancel
+ the change, and, if the change isn't coming
+ from an undo or redo event, an update(from, to,
+ text) method, which may be used to modify the change.
+ Undo or redo changes can't be modified, because they hold some
+ metainformation for restoring old marked ranges that is only
+ valid for that specific change. All three arguments
+ to update are optional, and can be left off to
+ leave the existing value for that field
+ intact. Note: you may not do anything from
+ a "beforeChange" handler that would cause changes
+ to the document or its visualization. Doing so will, since this
+ handler is called directly from the bowels of the CodeMirror
+ implementation, probably cause the editor to become
+ corrupted.
+
+
"cursorActivity" (instance: CodeMirror)
+
Will be fired when the cursor or selection moves, or any
+ change is made to the editor content.
Fired after a key is handled through a
+ keymap. name is the name of the handled key (for
+ example "Ctrl-X" or "'q'"),
+ and event is the DOM keydown
+ or keypress event.
This event is fired before the selection is moved. Its
+ handler may modify the resulting selection head and anchor.
+ The selection parameter is an object
+ with head and anchor properties
+ holding {line, ch} objects, which the handler can
+ read and update. Handlers for this event have the same
+ restriction
+ as "beforeChange"
+ handlers — they should not do anything to directly update the
+ state of the editor.
+
+
"viewportChange" (instance: CodeMirror, from: number, to: number)
+
Fires whenever the view port of
+ the editor changes (due to scrolling, editing, or any other
+ factor). The from and to arguments
+ give the new start and end of the viewport.
+
+
"swapDoc" (instance: CodeMirror, oldDoc: Doc)
+
This is signalled when the editor's document is replaced
+ using the swapDoc
+ method.
Fires when the editor gutter (the line-number area) is
+ clicked. Will pass the editor instance as first argument, the
+ (zero-based) number of the line that was clicked as second
+ argument, the CSS class of the gutter that was clicked as third
+ argument, and the raw mousedown event object as
+ fourth argument.
Fires when the editor gutter (the line-number area)
+ receives a contextmenu event. Will pass the editor
+ instance as first argument, the (zero-based) number of the line
+ that was clicked as second argument, the CSS class of the
+ gutter that was clicked as third argument, and the raw
+ contextmenu mouse event object as fourth argument.
+ You can preventDefault the event, to signal that
+ CodeMirror should do no further handling.
+
+
"focus" (instance: CodeMirror)
+
Fires whenever the editor is focused.
+
+
"blur" (instance: CodeMirror)
+
Fires whenever the editor is unfocused.
+
+
"scroll" (instance: CodeMirror)
+
Fires when the editor is scrolled.
+
+
"update" (instance: CodeMirror)
+
Will be fired whenever CodeMirror updates its DOM display.
Fired whenever a line is (re-)rendered to the DOM. Fired
+ right after the DOM element is built, before it is
+ added to the document. The handler may mess with the style of
+ the resulting element, or add event handlers, but
+ should not try to change the state of the editor.
Fired when CodeMirror is handling a DOM event of this type.
+ You can preventDefault the event, or give it a
+ truthy codemirrorIgnore property, to signal that
+ CodeMirror should do no further handling.
+
+
+
Document objects (instances
+ of CodeMirror.Doc) emit the
+ following events:
+
+
+
"change" (doc: CodeMirror.Doc, changeObj: object)
+
Fired whenever a change occurs to the
+ document. changeObj has a similar type as the
+ object passed to the
+ editor's "change"
+ event, but it never has a next property, because
+ document change events are not batched (whereas editor change
+ events are).
Line handles (as returned by, for
+ example, getLineHandle)
+ support these events:
+
+
+
"delete" ()
+
Will be fired when the line object is deleted. A line object
+ is associated with the start of the line. Mostly useful
+ when you need to find out when your gutter
+ markers on a given line are removed.
+
"change" (line: LineHandle, changeObj: object)
+
Fires when the line's text content is changed in any way
+ (but the line is not deleted outright). The change
+ object is similar to the one passed
+ to change event on the editor
+ object.
+
+
+
Marked range handles (CodeMirror.TextMarker), as returned
+ by markText
+ and setBookmark, emit the
+ following events:
+
+
+
"beforeCursorEnter" ()
+
Fired when the cursor enters the marked range. From this
+ event handler, the editor state may be inspected
+ but not modified, with the exception that the range on
+ which the event fires may be cleared.
+
"clear" (from: {line, ch}, to: {line, ch})
+
Fired when the range is cleared, either through cursor
+ movement in combination
+ with clearOnEnter
+ or through a call to its clear() method. Will only
+ be fired once per handle. Note that deleting the range through
+ text editing does not fire this event, because an undo action
+ might bring the range back into existence. from
+ and to give the part of the document that the range
+ spanned when it was cleared.
+
"hide" ()
+
Fired when the last part of the marker is removed from the
+ document by editing operations.
+
"unhide" ()
+
Fired when, after the marker was removed by editing, a undo
+ operation brought the marker back.
+
+
+
Line widgets (CodeMirror.LineWidget), returned
+ by addLineWidget, fire
+ these events:
+
+
+
"redraw" ()
+
Fired whenever the editor re-adds the widget to the DOM.
+ This will happen once right after the widget is added (if it is
+ scrolled into view), and then again whenever it is scrolled out
+ of view and back in again, or when changes to the editor options
+ or the line the widget is on require the widget to be
+ redrawn.
+
+
+
+
+
Keymaps
+
+
Keymaps are ways to associate keys with functionality. A keymap
+ is an object mapping strings that identify the keys to functions
+ that implement their functionality.
+
+
Keys are identified either by name or by character.
+ The CodeMirror.keyNames object defines names for
+ common keys and associates them with their key codes. Examples of
+ names defined here are Enter, F5,
+ and Q. These can be prefixed
+ with Shift-, Cmd-, Ctrl-,
+ and Alt- (in that order!) to specify a modifier. So
+ for example, Shift-Ctrl-Space would be a valid key
+ identifier.
+
+
Common example: map the Tab key to insert spaces instead of a tab
+ character.
Alternatively, a character can be specified directly by
+ surrounding it in single quotes, for example '$'
+ or 'q'. Due to limitations in the way browsers fire
+ key events, these may not be prefixed with modifiers.
+
+
The CodeMirror.keyMap object associates keymaps
+ with names. User code and keymap definitions can assign extra
+ properties to this object. Anywhere where a keymap is expected, a
+ string can be given, which will be looked up in this object. It
+ also contains the "default" keymap holding the
+ default bindings.
+
+
The values of properties in keymaps can be either functions of
+ a single argument (the CodeMirror instance), strings, or
+ false. Such strings refer to properties of the
+ CodeMirror.commands object, which defines a number of
+ common commands that are used by the default keybindings, and maps
+ them to functions. If the property is set to false,
+ CodeMirror leaves handling of the key up to the browser. A key
+ handler function may return CodeMirror.Pass to indicate
+ that it has decided not to handle the key, and other handlers (or
+ the default behavior) should be given a turn.
+
+
Keys mapped to command names that start with the
+ characters "go" (which should be used for
+ cursor-movement actions) will be fired even when an
+ extra Shift modifier is present (i.e. "Up":
+ "goLineUp" matches both up and shift-up). This is used to
+ easily implement shift-selection.
+
+
Keymaps can defer to each other by defining
+ a fallthrough property. This indicates that when a
+ key is not found in the map itself, one or more other maps should
+ be searched. It can hold either a single keymap or an array of
+ keymaps.
+
+
When a keymap contains a nofallthrough property
+ set to true, keys matched against that map will be
+ ignored if they don't match any of the bindings in the map (no
+ further child maps will be tried). When
+ the disableInput property is set
+ to true, the default effect of inserting a character
+ will be suppressed when the keymap is active as the top-level
+ map.
+
+
+
+
Customized Styling
+
+
Up to a certain extent, CodeMirror's look can be changed by
+ modifying style sheet files. The style sheets supplied by modes
+ simply provide the colors for that mode, and can be adapted in a
+ very straightforward way. To style the editor itself, it is
+ possible to alter or override the styles defined
+ in codemirror.css.
+
+
Some care must be taken there, since a lot of the rules in this
+ file are necessary to have CodeMirror function properly. Adjusting
+ colors should be safe, of course, and with some care a lot of
+ other things can be changed as well. The CSS classes defined in
+ this file serve the following roles:
+
+
+
CodeMirror
+
The outer element of the editor. This should be used for the
+ editor width, height, borders and positioning. Can also be used
+ to set styles that should hold for everything inside the editor
+ (such as font and font size), or to set a background.
+
+
CodeMirror-scroll
+
Whether the editor scrolls (overflow: auto +
+ fixed height). By default, it does. Setting
+ the CodeMirror class to have height:
+ auto and giving this class overflow-x: auto;
+ overflow-y: hidden; will cause the editor
+ to resize to fit its
+ content.
+
+
CodeMirror-focused
+
Whenever the editor is focused, the top element gets this
+ class. This is used to hide the cursor and give the selection a
+ different color when the editor is not focused.
+
+
CodeMirror-gutters
+
This is the backdrop for all gutters. Use it to set the
+ default gutter background color, and optionally add a border on
+ the right of the gutters.
+
+
CodeMirror-linenumbers
+
Use this for giving a background or width to the line number
+ gutter.
+
+
CodeMirror-linenumber
+
Used to style the actual individual line numbers. These
+ won't be children of the CodeMirror-linenumbers
+ (plural) element, but rather will be absolutely positioned to
+ overlay it. Use this to set alignment and text properties for
+ the line numbers.
+
+
CodeMirror-lines
+
The visible lines. This is where you specify vertical
+ padding for the editor content.
+
+
CodeMirror-cursor
+
The cursor is a block element that is absolutely positioned.
+ You can make it look whichever way you want.
+
+
CodeMirror-selected
+
The selection is represented by span elements
+ with this class.
These are used to style matched (or unmatched) brackets.
+
+
+
If your page's style sheets do funky things to
+ all div or pre elements (you probably
+ shouldn't do that), you'll have to define rules to cancel these
+ effects out again for elements under the CodeMirror
+ class.
+
+
Themes are also simply CSS files, which define colors for
+ various syntactic elements. See the files in
+ the theme directory.
+
+
+
+
Programming API
+
+
A lot of CodeMirror features are only available through its
+ API. Thus, you need to write code (or
+ use addons) if you want to expose them to
+ your users.
+
+
Whenever points in the document are represented, the API uses
+ objects with line and ch properties.
+ Both are zero-based. CodeMirror makes sure to 'clip' any positions
+ passed by client code so that they fit inside the document, so you
+ shouldn't worry too much about sanitizing your coordinates. If you
+ give ch a value of null, or don't
+ specify it, it will be replaced with the length of the specified
+ line.
+
+
Methods prefixed with doc. can, unless otherwise
+ specified, be called both on CodeMirror (editor)
+ instances and CodeMirror.Doc instances. Methods
+ prefixed with cm. are only available
+ on CodeMirror instances.
+
+
Constructor
+
+
Constructing an editor instance is done with
+ the CodeMirror(place: Element|fn(Element),
+ ?option: object) constructor. If the place
+ argument is a DOM element, the editor will be appended to it. If
+ it is a function, it will be called, and is expected to place the
+ editor into the document. options may be an element
+ mapping option names to values. The options
+ that it doesn't explicitly specify (or all options, if it is not
+ passed) will be taken
+ from CodeMirror.defaults.
+
+
Note that the options object passed to the constructor will be
+ mutated when the instance's options
+ are changed, so you shouldn't share such
+ objects between instances.
Get the current editor content. You can pass it an optional
+ argument to specify the string to be used to separate lines
+ (defaults to "\n").
+
doc.setValue(content: string)
+
Set the editor content.
+
+
doc.getRange(from: {line, ch}, to: {line, ch}, ?separator: string) → string
+
Get the text between the given points in the editor, which
+ should be {line, ch} objects. An optional third
+ argument can be given to indicate the line separator string to
+ use (defaults to "\n").
+
doc.replaceRange(replacement: string, from: {line, ch}, to: {line, ch})
+
Replace the part of the document between from
+ and to with the given string. from
+ and to must be {line, ch}
+ objects. to can be left off to simply insert the
+ string at position from.
+
+
doc.getLine(n: integer) → string
+
Get the content of line n.
+
doc.setLine(n: integer, text: string)
+
Set the content of line n.
+
doc.removeLine(n: integer)
+
Remove the given line from the document.
+
+
doc.lineCount() → integer
+
Get the number of lines in the editor.
+
doc.firstLine() → integer
+
Get the first line of the editor. This will
+ usually be zero but for linked sub-views,
+ or documents instantiated with a non-zero
+ first line, it might return other values.
+
doc.lastLine() → integer
+
Get the last line of the editor. This will
+ usually be doc.lineCount() - 1,
+ but for linked sub-views,
+ it might return other values.
+
+
doc.getLineHandle(num: integer) → LineHandle
+
Fetches the line handle for the given line number.
+
doc.getLineNumber(handle: LineHandle) → integer
+
Given a line handle, returns the current position of that
+ line (or null when it is no longer in the
+ document).
Iterate over the whole document, or if start
+ and end line numbers are given, the range
+ from start up to (not including) end,
+ and call f for each line, passing the line handle.
+ This is a faster way to visit a range of line handlers than
+ calling getLineHandle
+ for each of them. Note that line handles have
+ a text property containing the line's content (as a
+ string).
+
+
doc.markClean()
+
Set the editor content as 'clean', a flag that it will
+ retain until it is edited, and which will be set again when such
+ an edit is undone again. Useful to track whether the content
+ needs to be saved. This function is deprecated in favor
+ of changeGeneration,
+ which allows multiple subsystems to track different notions of
+ cleanness without interfering.
+
doc.changeGeneration() → integer
+
Returns a number that can later be passed
+ to isClean to test whether
+ any edits were made (and not undone) in the meantime.
+
doc.isClean(?generation: integer) → boolean
+
Returns whether the document is currently clean — not
+ modified since initialization or the last call
+ to markClean if no
+ argument is passed, or since the matching call
+ to changeGeneration
+ if a generation value is given.
Replace the selection with the given string. By default, the
+ new selection will span the inserted text. The
+ optional collapse argument can be used to change
+ this—passing "start" or "end" will
+ collapse the selection to the start or end of the inserted
+ text.
+
+
doc.getCursor(?start: string) → {line, ch}
+
start is a an optional string indicating which
+ end of the selection to return. It may
+ be "start", "end", "head"
+ (the side of the selection that moves when you press
+ shift+arrow), or "anchor" (the fixed side of the
+ selection). Omitting the argument is the same as
+ passing "head". A {line, ch} object
+ will be returned.
+
doc.somethingSelected() → boolean
+
Return true if any text is selected.
+
doc.setCursor(pos: {line, ch})
+
Set the cursor position. You can either pass a
+ single {line, ch} object, or the line and the
+ character as two separate parameters.
Similar
+ to setSelection, but
+ will, if shift is held or
+ the extending flag is set, move the
+ head of the selection while leaving the anchor at its current
+ place. to is optional, and can be passed to
+ ensure a region (for example a word or paragraph) will end up
+ selected (in addition to whatever lies between that region and
+ the current anchor).
+
doc.setExtending(value: boolean)
+
Sets or clears the 'extending' flag, which acts similar to
+ the shift key, in that it will cause cursor movement and calls
+ to extendSelection
+ to leave the selection anchor in place.
Used to find the target position for horizontal cursor
+ motion. start is a {line, ch}
+ object, amount an integer (may be negative),
+ and unit one of the
+ string "char", "column",
+ or "word". Will return a position that is produced
+ by moving amount times the distance specified
+ by unit. When visually is true, motion
+ in right-to-left text will be visual rather than logical. When
+ the motion was clipped by hitting the end or start of the
+ document, the returned value will have a hitSide
+ property set to true.
Similar to findPosH,
+ but used for vertical motion. unit may
+ be "line" or "page". The other
+ arguments and the returned value have the same interpretation as
+ they have in findPosH.
+
+
+
Configuration methods
+
+
+
cm.setOption(option: string, value: any)
+
Change the configuration of the editor. option
+ should the name of an option,
+ and value should be a valid value for that
+ option.
+
cm.getOption(option: string) → any
+
Retrieves the current value of the given option for this
+ editor instance.
+
+
cm.addKeyMap(map: object, bottom: boolean)
+
Attach an additional keymap to the
+ editor. This is mostly useful for addons that need to register
+ some key handlers without trampling on
+ the extraKeys
+ option. Maps added in this way have a higher precedence than
+ the extraKeys
+ and keyMap options,
+ and between them, the maps added earlier have a lower precedence
+ than those added later, unless the bottom argument
+ was passed, in which case they end up below other keymaps added
+ with this method.
+
cm.removeKeyMap(map: object)
+
Disable a keymap added
+ with addKeyMap. Either
+ pass in the keymap object itself, or a string, which will be
+ compared against the name property of the active
+ keymaps.
Enable a highlighting overlay. This is a stateless mini-mode
+ that can be used to add extra highlighting. For example,
+ the search addon uses it to
+ highlight the term that's currently being
+ searched. mode can be a mode
+ spec or a mode object (an object with
+ a token method).
+ The options parameter is optional. If given, it
+ should be an object. Currently, only the opaque
+ option is recognized. This defaults to off, but can be given to
+ allow the overlay styling, when not null, to
+ override the styling of the base mode entirely, instead of the
+ two being applied together.
+
cm.removeOverlay(mode: string|object)
+
Pass this the exact value passed for the mode
+ parameter to addOverlay,
+ or a string that corresponds to the name propery of
+ that value, to remove an overlay again.
+
+
cm.on(type: string, func: (...args))
+
Register an event handler for the given event type (a
+ string) on the editor instance. There is also
+ a CodeMirror.on(object, type, func) version
+ that allows registering of events on any object.
+
cm.off(type: string, func: (...args))
+
Remove an event handler on the editor instance. An
+ equivalent CodeMirror.off(object, type,
+ func) also exists.
+
+
+
Document management methods
+
+
Each editor is associated with an instance
+ of CodeMirror.Doc, its document. A document
+ represents the editor content, plus a selection, an undo history,
+ and a mode. A document can only be
+ associated with a single editor at a time. You can create new
+ documents by calling the CodeMirror.Doc(text, mode,
+ firstLineNumber) constructor. The last two arguments are
+ optional and can be used to set a mode for the document and make
+ it start at a line number other than 0, respectively.
+
+
+
cm.getDoc() → Doc
+
Retrieve the currently active document from an editor.
+
doc.getEditor() → CodeMirror
+
Retrieve the editor associated with a document. May
+ return null.
+
+
cm.swapDoc(doc: CodeMirror.Doc) → Doc
+
Attach a new document to the editor. Returns the old
+ document, which is now no longer associated with an editor.
+
+
doc.copy(copyHistory: boolean) → Doc
+
Create an identical copy of the given doc.
+ When copyHistory is true, the history will also be
+ copied. Can not be called directly on an editor.
+
+
doc.linkedDoc(options: object) → Doc
+
Create a new document that's linked to the target document.
+ Linked documents will stay in sync (changes to one are also
+ applied to the other) until unlinked.
+ These are the options that are supported:
+
+
sharedHist: boolean
+
When turned on, the linked copy will share an undo
+ history with the original. Thus, something done in one of
+ the two can be undone in the other, and vice versa.
+
from: integer
+
to: integer
+
Can be given to make the new document a subview of the
+ original. Subviews only show a given range of lines. Note
+ that line coordinates inside the subview will be consistent
+ with those of the parent, so that for example a subview
+ starting at line 10 will refer to its first line as line 10,
+ not 0.
+
mode: string|object
+
By default, the new document inherits the mode of the
+ parent. This option can be set to
+ a mode spec to give it a
+ different mode.
+
+
doc.unlinkDoc(doc: CodeMirror.Doc)
+
Break the link between two documents. After calling this,
+ changes will no longer propagate between the documents, and, if
+ they had a shared history, the history will become
+ separate.
Will call the given function for all documents linked to the
+ target document. It will be passed two arguments, the linked document
+ and a boolean indicating whether that document shares history
+ with the target.
Returns an object with {undo, redo} properties,
+ both of which hold integers, indicating the amount of stored
+ undo and redo operations.
+
doc.clearHistory()
+
Clears the editor's undo history.
+
doc.getHistory() → object
+
Get a (JSON-serializeable) representation of the undo history.
+
doc.setHistory(history: object)
+
Replace the editor's undo history with the one provided,
+ which must be a value as returned
+ by getHistory. Note that
+ this will have entirely undefined results if the editor content
+ isn't also the same as it was when getHistory was
+ called.
+
+
+
Text-marking methods
+
+
+
doc.markText(from: {line, ch}, to: {line, ch}, ?options: object) → TextMarker
+
Can be used to mark a range of text with a specific CSS
+ class name. from and to should
+ be {line, ch} objects. The options
+ parameter is optional. When given, it should be an object that
+ may contain the following configuration options:
+
+
className: string
+
Assigns a CSS class to the marked stretch of text.
+
inclusiveLeft: boolean
+
Determines whether
+ text inserted on the left of the marker will end up inside
+ or outside of it.
+
inclusiveRight: boolean
+
Like inclusiveLeft,
+ but for the right side.
+
atomic: boolean
+
Atomic ranges act as a single unit when cursor movement is
+ concerned—i.e. it is impossible to place the cursor inside of
+ them. In atomic ranges, inclusiveLeft
+ and inclusiveRight have a different meaning—they
+ will prevent the cursor from being placed respectively
+ directly before and directly after the range.
+
collapsed: boolean
+
Collapsed ranges do not show up in the display. Setting a
+ range to be collapsed will automatically make it atomic.
+
clearOnEnter: boolean
+
When enabled, will cause the mark to clear itself whenever
+ the cursor enters its range. This is mostly useful for
+ text-replacement widgets that need to 'snap open' when the
+ user tries to edit them. The
+ "clear" event
+ fired on the range handle can be used to be notified when this
+ happens.
+
replacedWith: Element
+
Use a given node to display this range. Implies both
+ collapsed and atomic. The given DOM node must be an
+ inline element (as opposed to a block element).
+
handleMouseEvents: boolean
+
When replacedWith is given, this determines
+ whether the editor will capture mouse and drag events
+ occurring in this widget. Default is false—the events will be
+ left alone for the default browser handler, or specific
+ handlers on the widget, to capture.
+
readOnly: boolean
+
A read-only span can, as long as it is not cleared, not be
+ modified except by
+ calling setValue to reset
+ the whole document. Note: adding a read-only span
+ currently clears the undo history of the editor, because
+ existing undo events being partially nullified by read-only
+ spans would corrupt the history (in the current
+ implementation).
+
addToHistory: boolean
+
When set to true (default is false), adding this marker
+ will create an event in the undo history that can be
+ individually undone (clearing the marker).
+
startStyle: string
Can be used to specify
+ an extra CSS class to be applied to the leftmost span that
+ is part of the marker.
+
endStyle: string
Equivalent
+ to startStyle, but for the rightmost span.
+
title:
+ string
When given, will give the nodes created
+ for this span a HTML title attribute with the
+ given value.
+
shared: boolean
When the
+ target document is linked to other
+ documents, you can set shared to true to make the
+ marker appear in all documents. By default, a marker appears
+ only in its target document.
+
+ The method will return an object that represents the marker
+ (with constuctor CodeMirror.TextMarker), which
+ exposes three methods:
+ clear(), to remove the mark,
+ find(), which returns
+ a {from, to} object (both holding document
+ positions), indicating the current position of the marked range,
+ or undefined if the marker is no longer in the
+ document, and finally changed(),
+ which you can call if you've done something that might change
+ the size of the marker (for example changing the content of
+ a replacedWith
+ node), and want to cheaply update the display.
Inserts a bookmark, a handle that follows the text around it
+ as it is being edited, at the given position. A bookmark has two
+ methods find() and clear(). The first
+ returns the current position of the bookmark, if it is still in
+ the document, and the second explicitly removes the bookmark.
+ The options argument is optional. If given, the following
+ properties are recognized:
+
+
widget: Element
Can be used to display a DOM
+ node at the current location of the bookmark (analogous to
+ the replacedWith
+ option to markText).
+
insertLeft: boolean
By default, text typed
+ when the cursor is on top of the bookmark will end up to the
+ right of the bookmark. Set this option to true to make it go
+ to the left instead.
Sets the gutter marker for the given gutter (identified by
+ its CSS class, see
+ the gutters option)
+ to the given value. Value can be either null, to
+ clear the marker, or a DOM element, to set it. The DOM element
+ will be shown in the specified gutter next to the specified
+ line.
+
+
cm.clearGutter(gutterID: string)
+
Remove all gutter markers in
+ the gutter with the given ID.
Set a CSS class name for the given line. line
+ can be a number or a line handle. where determines
+ to which element this class should be applied, can can be one
+ of "text" (the text element, which lies in front of
+ the selection), "background" (a background element
+ that will be behind the selection), or "wrap" (the
+ wrapper node that wraps all of the line's elements, including
+ gutter elements). class should be the name of the
+ class to apply.
Remove a CSS class from a line. line can be a
+ line handle or number. where should be one
+ of "text", "background",
+ or "wrap"
+ (see addLineClass). class
+ can be left off to remove all classes for the specified node, or
+ be a string to remove only a specific class.
+
+
cm.lineInfo(line: integer|LineHandle) → object
+
Returns the line number, text content, and marker status of
+ the given line, which can be either a number or a line handle.
+ The returned object has the structure {line, handle, text,
+ gutterMarkers, textClass, bgClass, wrapClass, widgets},
+ where gutterMarkers is an object mapping gutter IDs
+ to marker elements, and widgets is an array
+ of line widgets attached to this
+ line, and the various class properties refer to classes added
+ with addLineClass.
Puts node, which should be an absolutely
+ positioned DOM node, into the editor, positioned right below the
+ given {line, ch} position.
+ When scrollIntoView is true, the editor will ensure
+ that the entire node is visible (if possible). To remove the
+ widget again, simply use DOM methods (move it somewhere else, or
+ call removeChild on its parent).
Adds a line widget, an element shown below a line, spanning
+ the whole of the editor's width, and moving the lines below it
+ downwards. line should be either an integer or a
+ line handle, and node should be a DOM node, which
+ will be displayed below the given line. options,
+ when given, should be an object that configures the behavior of
+ the widget. The following options are supported (all default to
+ false):
+
+
coverGutter: boolean
+
Whether the widget should cover the gutter.
+
noHScroll: boolean
+
Whether the widget should stay fixed in the face of
+ horizontal scrolling.
+
above: boolean
+
Causes the widget to be placed above instead of below
+ the text of the line.
+
showIfHidden: boolean
+
When true, will cause the widget to be rendered even if
+ the line it is associated with is hidden.
+
handleMouseEvents: boolean
+
Determines whether the editor will capture mouse and
+ drag events occurring in this widget. Default is false—the
+ events will be left alone for the default browser handler,
+ or specific handlers on the widget, to capture.
+
insertAt: integer
+
By default, the widget is added below other widgets for
+ the line. This option can be used to place it at a different
+ position (zero for the top, N to put it after the Nth other
+ widget). Note that this only has effect once, when the
+ widget is created.
+
+ Note that the widget node will become a descendant of nodes with
+ CodeMirror-specific CSS classes, and those classes might in some
+ cases affect it. This method returns an object that represents
+ the widget placement. It'll have a line property
+ pointing at the line handle that it is associated with, and the following methods:
+
+
clear()
Removes the widget.
+
changed()
Call
+ this if you made some change to the widget's DOM node that
+ might affect its height. It'll force CodeMirror to update
+ the height of the line that contains the widget.
Programatically set the size of the editor (overriding the
+ applicable CSS
+ rules). width and height
+ can be either numbers (interpreted as pixels) or CSS units
+ ("100%", for example). You can
+ pass null for either of them to indicate that that
+ dimension should not be changed.
+
+
cm.scrollTo(x: number, y: number)
+
Scroll the editor to a given (pixel) position. Both
+ arguments may be left as null
+ or undefined to have no effect.
Get an {left, top, width, height, clientWidth,
+ clientHeight} object that represents the current scroll
+ position, the size of the scrollable area, and the size of the
+ visible area (minus scrollbars).
Scrolls the given position into view. what may
+ be null to scroll the cursor into view,
+ a {line, ch} position to scroll a character into
+ view, a {left, top, right, bottom} pixel range (in
+ editor-local coordinates), or a range {from, to}
+ containing either two character positions or two pixel squares.
+ The margin parameter is optional. When given, it
+ indicates the amount of vertical pixels around the given area
+ that should be made visible as well.
Returns an {left, top, bottom} object
+ containing the coordinates of the cursor position.
+ If mode is "local", they will be
+ relative to the top-left corner of the editable document. If it
+ is "page" or not given, they are relative to the
+ top-left corner of the page. where can be a boolean
+ indicating whether you want the start (true) or the
+ end (false) of the selection, or, if a {line,
+ ch} object is given, it specifies the precise position at
+ which you want to measure.
Returns the position and dimensions of an arbitrary
+ character. pos should be a {line, ch}
+ object. This differs from cursorCoords in that
+ it'll give the size of the whole character, rather than just the
+ position that the cursor would have when it would sit at that
+ position.
Given an {left, top} object, returns
+ the {line, ch} position that corresponds to it. The
+ optional mode parameter determines relative to what
+ the coordinates are interpreted. It may
+ be "window", "page" (the default),
+ or "local".
+
cm.lineAtHeight(height: number, ?mode: string) → number
+
Computes the line at the given pixel
+ height. mode can be one of the same strings
+ that coordsChar
+ accepts.
+
cm.heightAtLine(line: number, ?mode: string) → number
+
Computes the height of the top of a line, in the coordinate
+ system specified by mode
+ (see coordsChar), which
+ defaults to "page". When a line below the bottom of
+ the document is specified, the returned value is the bottom of
+ the last line in the document.
+
cm.defaultTextHeight() → number
+
Returns the line height of the default font for the editor.
+
cm.defaultCharWidth() → number
+
Returns the pixel width of an 'x' in the default font for
+ the editor. (Note that for non-monospace fonts, this is mostly
+ useless, and even for monospace fonts, non-ascii characters
+ might have a different width).
+
+
cm.getViewport() → {from: number, to: number}
+
Returns a {from, to} object indicating the
+ start (inclusive) and end (exclusive) of the currently rendered
+ part of the document. In big documents, when most content is
+ scrolled out of view, CodeMirror will only render the visible
+ part, and a margin around it. See also
+ the viewportChange
+ event.
+
+
cm.refresh()
+
If your code does something to change the size of the editor
+ element (window resizes are already listened for), or unhides
+ it, you should probably follow up by calling this method to
+ ensure CodeMirror is still looking as intended.
+
+
+
Mode, state, and token-related methods
+
+
When writing language-aware functionality, it can often be
+ useful to hook into the knowledge that the CodeMirror language
+ mode has. See the section on modes for a
+ more detailed description of how these work.
+
+
+
doc.getMode() → object
+
Gets the (outer) mode object for the editor. Note that this
+ is distinct from getOption("mode"), which gives you
+ the mode specification, rather than the resolved, instantiated
+ mode object.
+
+
doc.getModeAt(pos: {line, ch}) → object
+
Gets the inner mode at a given position. This will return
+ the same as getMode for
+ simple modes, but will return an inner mode for nesting modes
+ (such as htmlmixed).
Retrieves information about the token the current mode found
+ before the given position (a {line, ch} object). The
+ returned object has the following properties:
+
+
start
The character (on the given line) at which the token starts.
+
end
The character at which the token ends.
+
string
The token's string.
+
type
The token type the mode assigned
+ to the token, such as "keyword"
+ or "comment" (may also be null).
+
state
The mode's state at the end of this token.
+
+ If precise is true, the token will be guaranteed to be accurate based on recent edits. If false or
+ not specified, the token will use cached state information, which will be faster but might not be accurate if
+ edits were recently made and highlighting has not yet completed.
+
+
+
cm.getTokenTypeAt(pos: {line, ch}) → string
+
This is a (much) cheaper version
+ of getTokenAt useful for
+ when you just need the type of the token at a given position,
+ and no other information. Will return null for
+ unstyled tokens, and a string, potentially containing multiple
+ space-separated style names, otherwise.
Fetch appropriate helper for the given position. Helpers
+ provide a way to look up functionality appropriate for a mode.
+ The type argument provides the helper namespace
+ (see
+ also registerHelper),
+ in which the value will be looked up. The key that is used
+ depends on the mode active at the given
+ position. If the mode object contains a property with the same
+ name as the type argument, that is tried first.
+ Next, the mode's helperType, if any, is tried. And
+ finally, the mode's name.
Returns the mode's parser state, if any, at the end of the
+ given line number. If no line number is given, the state at the
+ end of the document is returned. This can be useful for storing
+ parsing errors in the state, or getting other kinds of
+ contextual information for a line. precise is defined
+ as in getTokenAt().
+
+
+
Miscellaneous methods
+
+
+
cm.operation(func: () → any) → any
+
CodeMirror internally buffers changes and only updates its
+ DOM structure after it has finished performing some operation.
+ If you need to perform a lot of operations on a CodeMirror
+ instance, you can call this method with a function argument. It
+ will call the function, buffering up all changes, and only doing
+ the expensive update after the function returns. This can be a
+ lot faster. The return value from this method will be the return
+ value of your function.
Adjust the indentation of the given line. The second
+ argument (which defaults to "smart") may be one of:
+
+
"prev"
+
Base indentation on the indentation of the previous line.
+
"smart"
+
Use the mode's smart indentation if available, behave
+ like "prev" otherwise.
+
"add"
+
Increase the indentation of the line by
+ one indent unit.
+
"subtract"
+
Reduce the indentation of the line.
+
<integer>
+
Add (positive number) or reduce (negative number) the
+ indentation by the given amount of spaces.
+
+
+
cm.toggleOverwrite(?value: bool)
+
Switches between overwrite and normal insert mode (when not
+ given an argument), or sets the overwrite mode to a specific
+ state (when given an argument).
+
+
cm.execCommand(name: string)
+
Runs the command with the given name on the editor.
+
+
doc.posFromIndex(index: integer) → {line, ch}
+
Calculates and returns a {line, ch} object for a
+ zero-based index who's value is relative to the start of the
+ editor's text. If the index is out of range of the text then
+ the returned object is clipped to start or end of the text
+ respectively.
Returns the DOM node that represents the editor, and
+ controls its size. Remove this from your tree to delete an
+ editor instance.
+
cm.getScrollerElement() → Element
+
Returns the DOM node that is responsible for the scrolling
+ of the editor.
+
cm.getGutterElement() → Element
+
Fetches the DOM node that contains the editor gutters.
+
+
+
Static properties
+
The CodeMirror object itself provides
+ several useful properties.
+
+
+
CodeMirror.version: string
+
It contains a string that indicates the version of the
+ library. This is a triple of
+ integers "major.minor.patch",
+ where patch is zero for releases, and something
+ else (usually one) for dev snapshots.
+ The method provides another way to initialize an editor. It
+ takes a textarea DOM node as first argument and an optional
+ configuration object as second. It will replace the textarea
+ with a CodeMirror instance, and wire up the form of that
+ textarea (if any) to make sure the editor contents are put
+ into the textarea when the form is submitted. The text in the
+ textarea will provide the content for the editor. A CodeMirror
+ instance created this way has three additional methods:
+
+
cm.save()
+
Copy the content of the editor into the textarea.
+
+
cm.toTextArea()
+
Remove the editor, and restore the original textarea (with
+ the editor's current content).
+
+
cm.getTextArea() → TextAreaElement
+
Returns the textarea that the instance was based on.
+
+
+
+
CodeMirror.defaults: object
+
An object containing default values for
+ all options. You can assign to its
+ properties to modify defaults (though this won't affect editors
+ that have already been created).
If you want to define extra methods in terms of the
+ CodeMirror API, it is possible to
+ use defineExtension. This will cause the given
+ value (usually a method) to be added to all CodeMirror instances
+ created from then on.
Similarly, defineOption can be used to define new options for
+ CodeMirror. The updateFunc will be called with the
+ editor instance and the new value when an editor is initialized,
+ and whenever the option is modified
+ through setOption.
+
+
CodeMirror.defineInitHook(func: function)
+
If your extention just needs to run some
+ code whenever a CodeMirror instance is initialized,
+ use CodeMirror.defineInitHook. Give it a function as
+ its only argument, and from then on, that function will be called
+ (with the instance as argument) whenever a new CodeMirror instance
+ is initialized.
Registers a helper value with the given name in
+ the given namespace (type). This is used to define
+ functionality that may be looked up by mode. Will create (if it
+ doesn't already exist) a property on the CodeMirror
+ object for the given type, pointing to an object
+ that maps names to values. I.e. after
+ doing CodeMirror.registerHelper("hint", "foo",
+ myFoo), the value CodeMirror.hint.foo will
+ point to myFoo.
+
+
CodeMirror.Pos(line: integer, ?ch: integer)
+
A constructor for the {line, ch} objects that
+ are used to represent positions in editor documents.
+
+
CodeMirror.changeEnd(change: object) → {line, ch}
+
Utility function that computes an end position from a change
+ (an object with from, to,
+ and text properties, as passed to
+ various event handlers). The
+ returned position will be the end of the changed
+ range, after the change is applied.
+
+
+
+
+
Addons
+
+
The addon directory in the distribution contains a
+ number of reusable components that implement extra editor
+ functionality. In brief, they are:
Provides a very simple way to query users for text input.
+ Adds an openDialog method to
+ CodeMirror instances, which can be called with an HTML fragment
+ or a detached DOM node that provides the prompt (should include
+ an input tag), and a callback function that is called
+ when text has been entered. Also adds
+ an openNotification function that
+ simply shows an HTML fragment as a notification. Depends
+ on addon/dialog/dialog.css.
Adds the getSearchCursor(query, start, caseFold) →
+ cursor method to CodeMirror instances, which can be used
+ to implement search/replace functionality. query
+ can be a regular expression or a string (only strings will match
+ across lines—if they contain newlines). start
+ provides the starting position of the search. It can be
+ a {line, ch} object, or can be left off to default
+ to the start of the document. caseFold is only
+ relevant when matching a string. It will cause the search to be
+ case-insensitive. A search cursor has the following methods:
+
+
findNext() → boolean
+
findPrevious() → boolean
+
Search forward or backward from the current position.
+ The return value indicates whether a match was found. If
+ matching a regular expression, the return value will be the
+ array returned by the match method, in case you
+ want to extract matched groups.
+
from() → {line, ch}
+
to() → {line, ch}
+
These are only valid when the last call
+ to findNext or findPrevious did
+ not return false. They will return {line, ch}
+ objects pointing at the start and end of the match.
+
replace(text: string)
+
Replaces the currently found match with the given text
+ and adjusts the cursor position to reflect the
+ replacement.
Implements the search commands. CodeMirror has keys bound to
+ these by default, but will not do anything with them unless an
+ implementation is provided. Depends
+ on searchcursor.js, and will make use
+ of openDialog when
+ available to make prompting for search queries less ugly.
Defines an option matchBrackets which, when set
+ to true, causes matching brackets to be highlighted whenever the
+ cursor is next to them. It also adds a
+ method matchBrackets that forces this to happen
+ once, and a method findMatchingBracket that can be
+ used to run the bracket-finding algorithm that this uses
+ internally.
Defines an option autoCloseBrackets that will
+ auto-close brackets and quotes when typed. By default, it'll
+ auto-close ()[]{}''"", but you can pass it a string
+ similar to that (containing pairs of matching characters), or an
+ object with pairs and
+ optionally explode properties to customize
+ it. explode should be a similar string that gives
+ the pairs of characters that, when enter is pressed between
+ them, should have the second character also moved to its own
+ line. Demo here.
Defines an option matchTags that, when enabled,
+ will cause the tags around the cursor to be highlighted (using
+ the CodeMirror-matchingtag class). Also
+ defines
+ a commandtoMatchingTag,
+ which you can bind a key to in order to jump to the tag mathing
+ the one under the cursor. Depends on
+ the addon/fold/xml-fold.js
+ addon. Demo here.
Adds an option showTrailingSpace which, when
+ enabled, adds the CSS class cm-trailingspace to
+ stretches of whitespace at the end of lines.
+ The demo has a nice
+ squiggly underline style for this class.
Markdown specific. Defines
+ a "newlineAndIndentContinueMarkdownList"command
+ command that can be bound to enter to automatically
+ insert the leading characters for continuing a list. See
+ the Markdown mode
+ demo.
Helps with code folding. Adds a foldCode method
+ to editor instances, which will try to do a code fold starting
+ at the given line, or unfold the fold that is already present.
+ The method takes as first argument the position that should be
+ folded (may be a line number or
+ a Pos), and as second optional
+ argument either a range-finder function, or an options object,
+ supporting the following properties:
+
+
rangeFinder: fn(CodeMirror, Pos)
+
The function that is used to find foldable ranges. If this
+ is not directly passed, it will
+ call getHelper with
+ a "fold" type to find one that's appropriate for
+ the mode. There are files in
+ the addon/fold/
+ directory providing CodeMirror.fold.brace, which
+ finds blocks in brace languages (JavaScript, C, Java,
+ etc), CodeMirror.fold.indent, for languages where
+ indentation determines block structure (Python, Haskell),
+ and CodeMirror.fold.xml, for XML-style languages,
+ and CodeMirror.fold.comment, for folding comment
+ blocks.
+
widget: string|Element
+
The widget to show for folded ranges. Can be either a
+ string, in which case it'll become a span with
+ class CodeMirror-foldmarker, or a DOM node.
+
scanUp: boolean
+
When true (default is false), the addon will try to find
+ foldable ranges on the lines above the current one if there
+ isn't an eligible one on the given line.
+
minFoldSize: integer
+
The minimum amount of lines that a fold should span to be
+ accepted. Defaults to 0, which also allows single-line
+ folds.
Provides an option foldGutter, which can be
+ used to create a gutter with markers indicating the blocks that
+ can be folded. Create a gutter using
+ the gutters option,
+ giving it the class CodeMirror-foldgutter or
+ something else if you configure the addon to use a different
+ class, and this addon will show markers next to folded and
+ foldable blocks, and handle clicks in this gutter. Note that
+ CSS styles should be applied to make the gutter, and the fold
+ markers within it, visible. A default set of CSS styles are
+ available in:
+
+ addon/fold/foldgutter.css
+ .
+ The option
+ can be either set to true, or an object containing
+ the following optional option fields:
+
+
gutter: string
+
The CSS class of the gutter. Defaults
+ to "CodeMirror-foldgutter". You will have to
+ style this yourself to give it a width (and possibly a
+ background). See the default gutter style rules above.
+
indicatorOpen: string | Element
+
A CSS class or DOM element to be used as the marker for
+ open, foldable blocks. Defaults
+ to "CodeMirror-foldgutter-open".
+
indicatorFolded: string | Element
+
A CSS class or DOM element to be used as the marker for
+ folded blocks. Defaults to "CodeMirror-foldgutter-folded".
+
rangeFinder: fn(CodeMirror, Pos)
+
The range-finder function to use when determining whether
+ something can be folded. When not
+ given, getHelper will be
+ used to determine a default.
Can be used to run a CodeMirror mode over text without
+ actually opening an editor instance.
+ See the demo for an example.
+ There are alternate versions of the file avaible for
+ running stand-alone
+ (without including all of CodeMirror) and
+ for running under
+ node.js.
Provides a convenient way to syntax-highlight code snippets
+ in a webpage. Depends on
+ the runmode addon (or
+ its standalone variant). Provides
+ a CodeMirror.colorize function that can be called
+ with an array (or other array-ish collection) of DOM nodes that
+ represent the code snippets. By default, it'll get
+ all pre tags. Will read the data-lang
+ attribute of these nodes to figure out their language, and
+ syntax-color their content using the relevant CodeMirror mode
+ (you'll have to load the scripts for the relevant modes
+ yourself). A second argument may be provided to give a default
+ mode, used when no language attribute is found for a node. Used
+ in this manual to highlight example code.
Mode combinator that can be used to extend a mode with an
+ 'overlay' — a secondary mode is run over the stream, along with
+ the base mode, and can color specific pieces of text without
+ interfering with the base mode.
+ Defines CodeMirror.overlayMode, which is used to
+ create such a mode. See this
+ demo for a detailed example.
Mode combinator that can be used to easily 'multiplex'
+ between several modes.
+ Defines CodeMirror.multiplexingMode which, when
+ given as first argument a mode object, and as other arguments
+ any number of {open, close, mode [, delimStyle, innerStyle]}
+ objects, will return a mode object that starts parsing using the
+ mode passed as first argument, but will switch to another mode
+ as soon as it encounters a string that occurs in one of
+ the open fields of the passed objects. When in a
+ sub-mode, it will go back to the top mode again when
+ the close string is encountered.
+ Pass "\n" for open or close
+ if you want to switch on a blank line.
+
When delimStyle is specified, it will be the token
+ style returned for the delimiter tokens.
+
When innerStyle is specified, it will be the token
+ style added for each inner mode token.
+ The outer mode will not see the content between the delimiters.
+ See this demo for an
+ example.
Provides a framework for showing autocompletion hints.
+ Defines CodeMirror.showHint, which takes a
+ CodeMirror instance, a hinting function, and optionally an
+ options object, and pops up a widget that allows the user to
+ select a completion. Hinting functions are function that take an
+ editor instance and an optional options object, and return
+ a {list, from, to} object, where list
+ is an array of strings or objects (the completions),
+ and from and to give the start and end
+ of the token that is being completed as {line, ch}
+ objects. If no hinting function is given, the addon will try to
+ use getHelper with
+ the "hint" type to find one. When completions
+ aren't simple strings, they should be objects with the folowing
+ properties:
+
+
text: string
+
The completion text. This is the only required
+ property.
+
displayText: string
+
The text that should be displayed in the menu.
+
className: string
+
A CSS class name to apply to the completion's line in the
+ menu.
+
render: fn(Element, self, data)
+
A method used to create the DOM structure for showing the
+ completion by appending it to its first argument.
+
hint: fn(CodeMirror, self, data)
+
A method used to actually apply the completion, instead of
+ the default behavior.
+
+ The plugin understands the following options (the options object
+ will also be passed along to the hinting function, which may
+ understand additional options):
+
+
async: boolean
+
When set to true, the hinting function's signature should
+ be (cm, callback, ?options), and the completion
+ interface will only be popped up when the hinting function
+ calls the callback, passing it the object holding the
+ completions.
+
completeSingle: boolean
+
Determines whether, when only a single completion is
+ available, it is completed without showing the dialog.
+ Defaults to true.
+
alignWithWord: boolean
+
Whether the pop-up should be horizontally aligned with the
+ start of the word (true, default), or with the cursor (false).
+
closeOnUnfocus: boolean
+
When enabled (which is the default), the pop-up will close
+ when the editor is unfocused.
+
customKeys: keymap
+
Allows you to provide a custom keymap of keys to be active
+ when the pop-up is active. The handlers will be called with an
+ extra argument, a handle to the completion menu, which
+ has moveFocus(n), setFocus(n), pick(),
+ and close() methods (see the source for details),
+ that can be used to change the focused element, pick the
+ current element or close the menu.
+
extraKeys: keymap
+
Like customKeys above, but the bindings will
+ be added to the set of default bindings, instead of replacing
+ them.
+
+ The following events will be fired on the completions object
+ during completion:
+
+
"shown" ()
+
Fired when the pop-up is shown.
+
"select" (completion, Element)
+
Fired when a completion is selected. Passed the completion
+ value (string or object) and the DOM node that represents it
+ in the menu.
+
"close" ()
+
Fired when the completion is finished.
+
+ This addon depends styles
+ from addon/hint/show-hint.css. Check
+ out the demo for an
+ example.
Defines a simple hinting function for JavaScript
+ (CodeMirror.hint.javascript) and CoffeeScript
+ (CodeMirror.hint.coffeescript) code. This will
+ simply use the JavaScript environment that the editor runs in as
+ a source of information about objects and their properties.
Defines CodeMirror.hint.xml, which produces
+ hints for XML tagnames, attribute names, and attribute values,
+ guided by a schemaInfo option (a property of the
+ second argument passed to the hinting function, or the third
+ argument passed to CodeMirror.showHint). The
+ schema info should be an object mapping tag names to information
+ about these tags, with optionally a "!top" property
+ containing a list of the names of valid top-level tags. The
+ values of the properties should be objects with optional
+ properties children (an array of valid child
+ element names, omit to simply allow all tags to appear)
+ and attrs (an object mapping attribute names
+ to null for free-form attributes, and an array of
+ valid values for restricted
+ attributes). Demo
+ here.
Provides schema info to
+ the xml-hint addon for HTML
+ documents. Defines a schema
+ object CodeMirror.htmlSchema that you can pass to
+ as a schemaInfo option, and
+ a CodeMirror.hint.html hinting function that
+ automatically calls CodeMirror.hint.xml with this
+ schema data. See
+ the demo.
A very simple hinting function
+ (CodeMirror.hint.anyword) that simply looks for
+ words in the nearby code and completes to those. Takes two
+ optional options, word, a regular expression that
+ matches words (sequences of one or more character),
+ and range, which defines how many lines the addon
+ should scan when completing (defaults to 500).
Adds a highlightSelectionMatches option that
+ can be enabled to highlight all instances of a currently
+ selected word. Can be set either to true or to an object
+ containing the following options: minChars, for the
+ minimum amount of selected characters that triggers a highlight
+ (default 2), style, for the style to be used to
+ highlight the matches (default "matchhighlight",
+ which will correspond to CSS
+ class cm-matchhighlight),
+ and showToken which can be set to true
+ or to a regexp matching the characters that make up a word. When
+ enabled, it causes the current word to be highlighted when
+ nothing is selected (defaults to off).
+ Demo here.
Defines an interface component for showing linting warnings,
+ with pluggable warning sources
+ (see json-lint.js,
+ javascript-lint.js,
+ and css-lint.js
+ in the same directory). Defines a lint option that
+ can be set to a warning source (for
+ example CodeMirror.lint.javascript), or
+ to true, in which
+ case getHelper with
+ type "lint" is used to determined a validator
+ function. Depends on addon/lint/lint.css. A demo
+ can be found here.
Causes the selected text to be marked with the CSS class
+ CodeMirror-selectedtext when the styleSelectedText option
+ is enabled. Useful to change the colour of the selection (in addition to the background),
+ like in this demo.
Defines a styleActiveLine option that, when enabled,
+ gives the wrapper of the active line the class CodeMirror-activeline,
+ and adds a background with the class CodeMirror-activeline-background.
+ is enabled. See the demo.
Defines a CodeMirror.requireMode(modename,
+ callback) function that will try to load a given mode and
+ call the callback when it succeeded. You'll have to
+ set CodeMirror.modeURL to a string that mode paths
+ can be constructed from, for
+ example "mode/%N/%N.js"—the %N's will
+ be replaced with the mode name. Also
+ defines CodeMirror.autoLoadMode(instance, mode),
+ which will ensure the given mode is loaded and cause the given
+ editor instance to refresh its mode when the loading
+ succeeded. See the demo.
Adds an continueComments option, which can be
+ set to true to have the editor prefix new lines inside C-like
+ block comments with an asterisk when Enter is pressed. It can
+ also be set to a string in order to bind this functionality to a
+ specific key..
Adds a placeholder option that can be used to
+ make text appear in the editor when it is empty and not focused.
+ Also gives the editor a CodeMirror-empty CSS class
+ whenever it doesn't contain any text.
+ See the demo.
Defines an option fullScreen that, when set
+ to true, will make the editor full-screen (as in,
+ taking up the whole browser window). Depends
+ on fullscreen.css. Demo
+ here.
Addon to perform hard line wrapping/breaking for paragraphs
+ of text. Adds these methods to editor instances:
+
+
wrapParagraph(?pos: {line, ch}, ?options: object)
+
Wraps the paragraph at the given position.
+ If pos is not given, it defaults to the cursor
+ position.
+
wrapRange(from: {line, ch}, to: {line, ch}, ?options: object)
+
Wraps the given range as one big paragraph.
+
wrapParagraphsInRange(from: {line, ch}, to: {line, ch}, ?options: object)
+
Wrapps the paragraphs in (and overlapping with) the
+ given range individually.
+
+ The following options are recognized:
+
+
paragraphStart, paragraphEnd: RegExp
+
Blank lines are always considered paragraph boundaries.
+ These options can be used to specify a pattern that causes
+ lines to be considered the start or end of a paragraph.
+
column: number
+
The column to wrap at. Defaults to 80.
+
wrapOn: RegExp
+
A regular expression that matches only those
+ two-character strings that allow wrapping. By default, the
+ addon wraps on whitespace and after dash characters.
+
killTrailingSpace: boolean
+
Whether trailing space caused by wrapping should be
+ preserved, or deleted. Defaults to true.
Implements an interface for merging changes, using either a
+ 2-way or a 3-way view. The CodeMirror.MergeView
+ constructor takes arguments similar to
+ the CodeMirror
+ constructor, first a node to append the interface to, and then
+ an options object. Two extra optional options are
+ recognized, origLeft and origRight,
+ which may be strings that provide original versions of the
+ document, which will be shown to the left and right of the
+ editor in non-editable CodeMirror instances. The merge interface
+ will highlight changes between the editable document and the
+ original(s) (demo).
Provides integration with
+ the Tern JavaScript analysis
+ engine, for completion, definition finding, and minor
+ refactoring help. See the demo
+ for a very simple integration. For more involved scenarios, see
+ the comments at the top of
+ the addon and the
+ implementation of the
+ (multi-file) demonstration
+ on the Tern website.
+
+
+
+
+
Writing CodeMirror Modes
+
+
Modes typically consist of a single JavaScript file. This file
+ defines, in the simplest case, a lexer (tokenizer) for your
+ language—a function that takes a character stream as input,
+ advances it past a token, and returns a style for that token. More
+ advanced modes can also handle indentation for the language.
+
+
The mode script should
+ call CodeMirror.defineMode to
+ register itself with CodeMirror. This function takes two
+ arguments. The first should be the name of the mode, for which you
+ should use a lowercase string, preferably one that is also the
+ name of the files that define the mode (i.e. "xml" is
+ defined in xml.js). The second argument should be a
+ function that, given a CodeMirror configuration object (the thing
+ passed to the CodeMirror function) and an optional
+ mode configuration object (as in
+ the mode option), returns
+ a mode object.
+
+
Typically, you should use this second argument
+ to defineMode as your module scope function (modes
+ should not leak anything into the global scope!), i.e. write your
+ whole mode inside this function.
+
+
The main responsibility of a mode script is parsing
+ the content of the editor. Depending on the language and the
+ amount of functionality desired, this can be done in really easy
+ or extremely complicated ways. Some parsers can be stateless,
+ meaning that they look at one element (token) of the code
+ at a time, with no memory of what came before. Most, however, will
+ need to remember something. This is done by using a state
+ object, which is an object that is always passed when
+ reading a token, and which can be mutated by the tokenizer.
+
+
Modes that use a state must define
+ a startState method on their mode
+ object. This is a function of no arguments that produces a state
+ object to be used at the start of a document.
+
+
The most important part of a mode object is
+ its token(stream, state) method. All
+ modes must define this method. It should read one token from the
+ stream it is given as an argument, optionally update its state,
+ and return a style string, or null for tokens that do
+ not have to be styled. For your styles, you are encouraged to use
+ the 'standard' names defined in the themes (without
+ the cm- prefix). If that fails, it is also possible
+ to come up with your own and write your own CSS theme file.
+
+
A typical token string would
+ be "variable" or "comment". Multiple
+ styles can be returned (separated by spaces), for
+ example "string error" for a thing that looks like a
+ string but is invalid somehow (say, missing its closing quote).
+ When a style is prefixed by "line-"
+ or "line-background-", the style will be applied to
+ the whole line, analogous to what
+ the addLineClass method
+ does—styling the "text" in the simple case, and
+ the "background" element
+ when "line-background-" is prefixed.
+
+
The stream object that's passed
+ to token encapsulates a line of code (tokens may
+ never span lines) and our current position in that line. It has
+ the following API:
+
+
+
eol() → boolean
+
Returns true only if the stream is at the end of the
+ line.
+
sol() → boolean
+
Returns true only if the stream is at the start of the
+ line.
+
+
peek() → string
+
Returns the next character in the stream without advancing
+ it. Will return an null at the end of the
+ line.
+
next() → string
+
Returns the next character in the stream and advances it.
+ Also returns null when no more characters are
+ available.
match can be a character, a regular expression,
+ or a function that takes a character and returns a boolean. If
+ the next character in the stream 'matches' the given argument,
+ it is consumed and returned. Otherwise, undefined
+ is returned.
Repeatedly calls eat with the given argument,
+ until it fails. Returns true if any characters were eaten.
+
eatSpace() → boolean
+
Shortcut for eatWhile when matching
+ white-space.
+
skipToEnd()
+
Moves the position to the end of the line.
+
skipTo(ch: string) → boolean
+
Skips to the next occurrence of the given character, if
+ found on the current line (doesn't advance the stream if the
+ character does not occur on the line). Returns true if the
+ character was found.
Act like a
+ multi-character eat—if consume is true
+ or not given—or a look-ahead that doesn't update the stream
+ position—if it is false. pattern can be either a
+ string or a regular expression starting with ^.
+ When it is a string, caseFold can be set to true to
+ make the match case-insensitive. When successfully matching a
+ regular expression, the returned value will be the array
+ returned by match, in case you need to extract
+ matched groups.
+
+
backUp(n: integer)
+
Backs up the stream n characters. Backing it up
+ further than the start of the current token will cause things to
+ break, so be careful.
+
column() → integer
+
Returns the column (taking into account tabs) at which the
+ current token starts.
+
indentation() → integer
+
Tells you how far the current line has been indented, in
+ spaces. Corrects for tab characters.
+
+
current() → string
+
Get the string between the start of the current token and
+ the current stream position.
+
+
+
By default, blank lines are simply skipped when
+ tokenizing a document. For languages that have significant blank
+ lines, you can define
+ a blankLine(state) method on your
+ mode that will get called whenever a blank line is passed over, so
+ that it can update the parser state.
+
+
Because state object are mutated, and CodeMirror
+ needs to keep valid versions of a state around so that it can
+ restart a parse at any line, copies must be made of state objects.
+ The default algorithm used is that a new state object is created,
+ which gets all the properties of the old object. Any properties
+ which hold arrays get a copy of these arrays (since arrays tend to
+ be used as mutable stacks). When this is not correct, for example
+ because a mode mutates non-array properties of its state object, a
+ mode object should define
+ a copyState method, which is given a
+ state and should return a safe copy of that state.
+
+
If you want your mode to provide smart indentation
+ (through the indentLine
+ method and the indentAuto
+ and newlineAndIndent commands, to which keys can be
+ bound), you must define
+ an indent(state, textAfter) method
+ on your mode object.
+
+
The indentation method should inspect the given state object,
+ and optionally the textAfter string, which contains
+ the text on the line that is being indented, and return an
+ integer, the amount of spaces to indent. It should usually take
+ the indentUnit
+ option into account. An indentation method may
+ return CodeMirror.Pass to indicate that it
+ could not come up with a precise indentation.
+
+
To work well with
+ the commenting addon, a mode may
+ define lineComment (string that
+ starts a line
+ comment), blockCommentStart, blockCommentEnd
+ (strings that start and end block comments),
+ and blockCommentLead (a string to put at the start of
+ continued lines in a block comment). All of these are
+ optional.
+
+
Finally, a mode may define
+ an electricChars property, which should hold a string
+ containing all the characters that should trigger the behaviour
+ described for
+ the electricChars
+ option.
+
+
So, to summarize, a mode must provide
+ a token method, and it may
+ provide startState, copyState,
+ and indent methods. For an example of a trivial mode,
+ see the diff mode, for a more
+ involved example, see the C-like
+ mode.
+
+
Sometimes, it is useful for modes to nest—to have one
+ mode delegate work to another mode. An example of this kind of
+ mode is the mixed-mode HTML
+ mode. To implement such nesting, it is usually necessary to
+ create mode objects and copy states yourself. To create a mode
+ object, there are CodeMirror.getMode(options,
+ parserConfig), where the first argument is a configuration
+ object as passed to the mode constructor function, and the second
+ argument is a mode specification as in
+ the mode option. To copy a
+ state object, call CodeMirror.copyState(mode, state),
+ where mode is the mode that created the given
+ state.
+
+
In a nested mode, it is recommended to add an
+ extra method, innerMode which, given
+ a state object, returns a {state, mode} object with
+ the inner mode and its state for the current position. These are
+ used by utility scripts such as the tag
+ closer to get context information. Use
+ the CodeMirror.innerMode helper function to, starting
+ from a mode and a state, recursively walk down to the innermost
+ mode and state.
+
+
To make indentation work properly in a nested parser, it is
+ advisable to give the startState method of modes that
+ are intended to be nested an optional argument that provides the
+ base indentation for the block of code. The JavaScript and CSS
+ parser do this, for example, to allow JavaScript and CSS code
+ inside the mixed-mode HTML mode to be properly indented.
+
+
It is possible, and encouraged, to associate
+ your mode, or a certain configuration of your mode, with
+ a MIME type. For
+ example, the JavaScript mode associates itself
+ with text/javascript, and its JSON variant
+ with application/json. To do this,
+ call CodeMirror.defineMIME(mime,
+ modeSpec), where modeSpec can be a string or
+ object specifying a mode, as in
+ the mode option.
+
+
Sometimes, it is useful to add or override mode
+ object properties from external code.
+ The CodeMirror.extendMode function
+ can be used to add properties to mode objects produced for a
+ specific mode. Its first argument is the name of the mode, its
+ second an object that specifies the properties that should be
+ added. This is mostly useful to add utilities that can later be
+ looked up through getMode.
CodeMirror 2 is a complete rewrite that's
+ faster, smaller, simpler to use, and less dependent on browser
+ quirks. See this
+ and this
+ for more information.
More consistent page-up/page-down behaviour
+ across browsers. Fix some issues with hidden editors looping forever
+ when line-numbers were enabled. Make PHP parser parse
+ "\\" correctly. Have jumpToLine work on
+ line handles, and add cursorLine function to fetch the
+ line handle where the cursor currently is. Add new
+ setStylesheet function to switch style-sheets in a
+ running editor.
Adds removeLine method to API.
+ Introduces the PLSQL parser.
+ Marks XML errors by adding (rather than replacing) a CSS class, so
+ that they can be disabled by modifying their style. Fixes several
+ selection bugs, and a number of small glitches.
Add support for having both line-wrapping and
+ line-numbers turned on, make paren-highlighting style customisable
+ (markParen and unmarkParen config
+ options), work around a selection bug that Opera
+ reintroduced in version 10.
Solves some issues introduced by the
+ paste-handling changes from the previous release. Adds
+ setSpellcheck, setTextWrapping,
+ setIndentUnit, setUndoDepth,
+ setTabMode, and setLineNumbers to
+ customise a running editor. Introduces an SQL parser. Fixes a few small
+ problems in the Python
+ parser. And, as usual, add workarounds for various newly discovered
+ browser incompatibilities.
Overhaul of paste-handling (less fragile), fixes for several
+ serious IE8 issues (cursor jumping, end-of-document bugs) and a number
+ of small problems.
Introduces Python
+ and Lua parsers. Add
+ setParser (on-the-fly mode changing) and
+ clearHistory methods. Make parsing passes time-based
+ instead of lines-based (see the passTime option).
So you found a problem in CodeMirror. By all means, report it! Bug
+reports from users are the main drive behind improvements to
+CodeMirror. But first, please read over these points:
+
+
+
CodeMirror is maintained by volunteers. They don't owe you
+ anything, so be polite. Reports with an indignant or belligerent
+ tone tend to be moved to the bottom of the pile.
+
+
Include information about the browser in which the
+ problem occurred. Even if you tested several browsers, and
+ the problem occurred in all of them, mention this fact in the bug
+ report. Also include browser version numbers and the operating
+ system that you're on.
+
+
Mention which release of CodeMirror you're using. Preferably,
+ try also with the current development snapshot, to ensure the
+ problem has not already been fixed.
+
+
Mention very precisely what went wrong. "X is broken" is not a
+ good bug report. What did you expect to happen? What happened
+ instead? Describe the exact steps a maintainer has to take to make
+ the problem occur. We can not fix something that we can not
+ observe.
+
+
If the problem can not be reproduced in any of the demos
+ included in the CodeMirror distribution, please provide an HTML
+ document that demonstrates the problem. The best way to do this is
+ to go to jsbin.com, enter
+ it there, press save, and include the resulting link in your bug
+ report.
There are a few things in the 2.2 release that require some care
+when upgrading.
+
+
No more default.css
+
+
The default theme is now included
+in codemirror.css, so
+you do not have to included it separately anymore. (It was tiny, so
+even if you're not using it, the extra data overhead is negligible.)
+
+
Different key customization
+
+
CodeMirror has moved to a system
+where keymaps are used to
+bind behavior to keys. This means custom
+bindings are now possible.
+
+
Three options that influenced key
+behavior, tabMode, enterMode,
+and smartHome, are no longer supported. Instead, you can
+provide custom bindings to influence the way these keys act. This is
+done through the
+new extraKeys
+option, which can hold an object mapping key names to functionality. A
+simple example would be:
Keys can be mapped either to functions, which will be given the
+editor instance as argument, or to strings, which are mapped through
+functions through the CodeMirror.commands table, which
+contains all the built-in editing commands, and can be inspected and
+extended by external code.
+
+
By default, the Home key is bound to
+the "goLineStartSmart" command, which moves the cursor to
+the first non-whitespace character on the line. You can set do this to
+make it always go to the very start instead:
+
+
extraKeys: {"Home": "goLineStart"}
+
+
Similarly, Enter is bound
+to "newlineAndIndent" by default. You can bind it to
+something else to get different behavior. To disable special handling
+completely and only get a newline character inserted, you can bind it
+to false:
+
+
extraKeys: {"Enter": false}
+
+
The same works for Tab. If you don't want CodeMirror
+to handle it, bind it to false. The default behaviour is
+to indent the current line more ("indentMore" command),
+and indent it less when shift is held ("indentLess").
+There are also "indentAuto" (smart indent)
+and "insertTab" commands provided for alternate
+behaviors. Or you can write your own handler function to do something
+different altogether.
+
+
Tabs
+
+
Handling of tabs changed completely. The display width of tabs can
+now be set with the tabSize option, and tabs can
+be styled by setting CSS rules
+for the cm-tab class.
+
+
The default width for tabs is now 4, as opposed to the 8 that is
+hard-wired into browsers. If you are relying on 8-space tabs, make
+sure you explicitly set tabSize: 8 in your options.
Version 3 does not depart too much from 2.x API, and sites that use
+CodeMirror in a very simple way might be able to upgrade without
+trouble. But it does introduce a number of incompatibilities. Please
+at least skim this text before upgrading.
+
+
Note that version 3 drops full support for Internet
+Explorer 7. The editor will mostly work on that browser, but
+it'll be significantly glitchy.
+
+
+
DOM structure
+
+
This one is the most likely to cause problems. The internal
+structure of the editor has changed quite a lot, mostly to implement a
+new scrolling model.
+
+
Editor height is now set on the outer wrapper element (CSS
+class CodeMirror), not on the scroller element
+(CodeMirror-scroll).
+
+
Other nodes were moved, dropped, and added. If you have any code
+that makes assumptions about the internal DOM structure of the editor,
+you'll have to re-test it and probably update it to work with v3.
+
+
See the styling section of the
+manual for more information.
+
+
+
Gutter model
+
+
In CodeMirror 2.x, there was a single gutter, and line markers
+created with setMarker would have to somehow coexist with
+the line numbers (if present). Version 3 allows you to specify an
+array of gutters, by class
+name,
+use setGutterMarker
+to add or remove markers in individual gutters, and clear whole
+gutters
+with clearGutter.
+Gutter markers are now specified as DOM nodes, rather than HTML
+snippets.
+
+
The gutters no longer horizontally scrolls along with the content.
+The fixedGutter option was removed (since it is now the
+only behavior).
+
+
+<style>
+ /* Define a gutter style */
+ .note-gutter { width: 3em; background: cyan; }
+</style>
+<script>
+ // Create an instance with two gutters -- line numbers and notes
+ var cm = new CodeMirror(document.body, {
+ gutters: ["note-gutter", "CodeMirror-linenumbers"],
+ lineNumbers: true
+ });
+ // Add a note to line 0
+ cm.setGutterMarker(0, "note-gutter", document.createTextNode("hi"));
+</script>
+
+
+
+
Event handling
+
+
Most of the onXYZ options have been removed. The same
+effect is now obtained by calling
+the on method with a string
+identifying the event type. Multiple handlers can now be registered
+(and individually unregistered) for an event, and objects such as line
+handlers now also expose events. See the
+full list here.
+
+
(The onKeyEvent and onDragEvent options,
+which act more as hooks than as event handlers, are still there in
+their old form.)
The markText method
+(which has gained some interesting new features, such as creating
+atomic and read-only spans, or replacing spans with widgets) no longer
+takes the CSS class name as a separate argument, but makes it an
+optional field in the options object instead.
+
+
+// Style first ten lines, and forbid the cursor from entering them
+cm.markText({line: 0, ch: 0}, {line: 10, ch: 0}, {
+ className: "magic-text",
+ inclusiveLeft: true,
+ atomic: true
+});
+
+
+
+
Line folding
+
+
The interface for hiding lines has been
+removed. markText can
+now be used to do the same in a more flexible and powerful way.
+
+
The folding script has been
+updated to use the new interface, and should now be more robust.
+
+
+// Fold a range, replacing it with the text "??"
+var range = cm.markText({line: 4, ch: 2}, {line: 8, ch: 1}, {
+ replacedWith: document.createTextNode("??"),
+ // Auto-unfold when cursor moves into the range
+ clearOnEnter: true
+});
+// Get notified when auto-unfolding
+CodeMirror.on(range, "clear", function() {
+ console.log("boom");
+});
+
+
+
+
Line CSS classes
+
+
The setLineClass method has been replaced
+by addLineClass
+and removeLineClass,
+which allow more modular control over the classes attached to a line.
All methods that take or return objects that represent screen
+positions now use {left, top, bottom, right} properties
+(not always all of them) instead of the {x, y, yBot} used
+by some methods in v2.x.
The matchBrackets
+option is no longer defined in the core editor.
+Load addon/edit/matchbrackets.js to enable it.
+
+
+
Mode management
+
+
The CodeMirror.listModes
+and CodeMirror.listMIMEs functions, used for listing
+defined modes, are gone. You are now encouraged to simply
+inspect CodeMirror.modes (mapping mode names to mode
+constructors) and CodeMirror.mimeModes (mapping MIME
+strings to mode specs).
+
+
+
New features
+
+
Some more reasons to upgrade to version 3.
+
+
+
Bi-directional text support. CodeMirror will now mostly do the
+ right thing when editing Arabic or Hebrew text.
+
Arbitrary line heights. Using fonts with different heights
+ inside the editor (whether off by one pixel or fifty) is now
+ supported and handled gracefully.
CodeMirror is a versatile text editor
+ implemented in JavaScript for the browser. It is specialized for
+ editing code, and comes with a number of language modes and addons
+ that implement more advanced editing functionaly.
+
+
A rich programming API and a
+ CSS theming system are
+ available for customizing CodeMirror to fit your application, and
+ extending it with new functionality.
Development and bug tracking happens
+ on github
+ (alternate git
+ repository).
+ Please read these
+ pointers before submitting a bug. Use pull requests to submit
+ patches. All contributions must be released under the same MIT
+ license that CodeMirror uses.
+
+
Discussion around the project is done on
+ a mailing list.
+ There is also
+ the codemirror-announce
+ list, which is only used for major announcements (such as new
+ versions). If needed, you can
+ contact the maintainer
+ directly.
+
+
A list of CodeMirror-related software that is not part of the
+ main distribution is maintained
+ on our
+ wiki. Feel free to add your project.
+
+
+
+
Browser support
+
The desktop versions of the following browsers,
+ in standards mode (HTML5 <!doctype html>
+ recommended) are supported:
+
+
Firefox
version 3 and up
+
Chrome
any version
+
Safari
version 5.2 and up
+
Internet Explorer
version 8 and up
+
Opera
version 9 and up
+
+
Modern mobile browsers tend to partly work. Bug reports and
+ patches for mobile support are welcome, but the maintainer does not
+ have the time or budget to actually work on it himself.
+
+
+
diff --git a/plugins/codemirror/codemirror/keymap/emacs.js b/plugins/codemirror/codemirror/keymap/emacs.js
new file mode 100644
index 0000000..7a3dfb1
--- /dev/null
+++ b/plugins/codemirror/codemirror/keymap/emacs.js
@@ -0,0 +1,387 @@
+(function() {
+ "use strict";
+
+ var Pos = CodeMirror.Pos;
+ function posEq(a, b) { return a.line == b.line && a.ch == b.ch; }
+
+ // Kill 'ring'
+
+ var killRing = [];
+ function addToRing(str) {
+ killRing.push(str);
+ if (killRing.length > 50) killRing.shift();
+ }
+ function growRingTop(str) {
+ if (!killRing.length) return addToRing(str);
+ killRing[killRing.length - 1] += str;
+ }
+ function getFromRing(n) { return killRing[killRing.length - (n ? Math.min(n, 1) : 1)] || ""; }
+ function popFromRing() { if (killRing.length > 1) killRing.pop(); return getFromRing(); }
+
+ var lastKill = null;
+
+ function kill(cm, from, to, mayGrow, text) {
+ if (text == null) text = cm.getRange(from, to);
+
+ if (mayGrow && lastKill && lastKill.cm == cm && posEq(from, lastKill.pos) && cm.isClean(lastKill.gen))
+ growRingTop(text);
+ else
+ addToRing(text);
+ cm.replaceRange("", from, to, "+delete");
+
+ if (mayGrow) lastKill = {cm: cm, pos: from, gen: cm.changeGeneration()};
+ else lastKill = null;
+ }
+
+ // Boundaries of various units
+
+ function byChar(cm, pos, dir) {
+ return cm.findPosH(pos, dir, "char", true);
+ }
+
+ function byWord(cm, pos, dir) {
+ return cm.findPosH(pos, dir, "word", true);
+ }
+
+ function byLine(cm, pos, dir) {
+ return cm.findPosV(pos, dir, "line", cm.doc.sel.goalColumn);
+ }
+
+ function byPage(cm, pos, dir) {
+ return cm.findPosV(pos, dir, "page", cm.doc.sel.goalColumn);
+ }
+
+ function byParagraph(cm, pos, dir) {
+ var no = pos.line, line = cm.getLine(no);
+ var sawText = /\S/.test(dir < 0 ? line.slice(0, pos.ch) : line.slice(pos.ch));
+ var fst = cm.firstLine(), lst = cm.lastLine();
+ for (;;) {
+ no += dir;
+ if (no < fst || no > lst)
+ return cm.clipPos(Pos(no - dir, dir < 0 ? 0 : null));
+ line = cm.getLine(no);
+ var hasText = /\S/.test(line);
+ if (hasText) sawText = true;
+ else if (sawText) return Pos(no, 0);
+ }
+ }
+
+ function bySentence(cm, pos, dir) {
+ var line = pos.line, ch = pos.ch;
+ var text = cm.getLine(pos.line), sawWord = false;
+ for (;;) {
+ var next = text.charAt(ch + (dir < 0 ? -1 : 0));
+ if (!next) { // End/beginning of line reached
+ if (line == (dir < 0 ? cm.firstLine() : cm.lastLine())) return Pos(line, ch);
+ text = cm.getLine(line + dir);
+ if (!/\S/.test(text)) return Pos(line, ch);
+ line += dir;
+ ch = dir < 0 ? text.length : 0;
+ continue;
+ }
+ if (sawWord && /[!?.]/.test(next)) return Pos(line, ch + (dir > 0 ? 1 : 0));
+ if (!sawWord) sawWord = /\w/.test(next);
+ ch += dir;
+ }
+ }
+
+ function byExpr(cm, pos, dir) {
+ var wrap;
+ if (cm.findMatchingBracket && (wrap = cm.findMatchingBracket(pos, true))
+ && wrap.match && (wrap.forward ? 1 : -1) == dir)
+ return dir > 0 ? Pos(wrap.to.line, wrap.to.ch + 1) : wrap.to;
+
+ for (var first = true;; first = false) {
+ var token = cm.getTokenAt(pos);
+ var after = Pos(pos.line, dir < 0 ? token.start : token.end);
+ if (first && dir > 0 && token.end == pos.ch || !/\w/.test(token.string)) {
+ var newPos = cm.findPosH(after, dir, "char");
+ if (posEq(after, newPos)) return pos;
+ else pos = newPos;
+ } else {
+ return after;
+ }
+ }
+ }
+
+ // Prefixes (only crudely supported)
+
+ function getPrefix(cm, precise) {
+ var digits = cm.state.emacsPrefix;
+ if (!digits) return precise ? null : 1;
+ clearPrefix(cm);
+ return digits == "-" ? -1 : Number(digits);
+ }
+
+ function repeated(cmd) {
+ var f = typeof cmd == "string" ? function(cm) { cm.execCommand(cmd); } : cmd;
+ return function(cm) {
+ var prefix = getPrefix(cm);
+ f(cm);
+ for (var i = 1; i < prefix; ++i) f(cm);
+ };
+ }
+
+ function findEnd(cm, by, dir) {
+ var pos = cm.getCursor(), prefix = getPrefix(cm);
+ if (prefix < 0) { dir = -dir; prefix = -prefix; }
+ for (var i = 0; i < prefix; ++i) {
+ var newPos = by(cm, pos, dir);
+ if (posEq(newPos, pos)) break;
+ pos = newPos;
+ }
+ return pos;
+ }
+
+ function move(by, dir) {
+ var f = function(cm) {
+ cm.extendSelection(findEnd(cm, by, dir));
+ };
+ f.motion = true;
+ return f;
+ }
+
+ function killTo(cm, by, dir) {
+ kill(cm, cm.getCursor(), findEnd(cm, by, dir), true);
+ }
+
+ function addPrefix(cm, digit) {
+ if (cm.state.emacsPrefix) {
+ if (digit != "-") cm.state.emacsPrefix += digit;
+ return;
+ }
+ // Not active yet
+ cm.state.emacsPrefix = digit;
+ cm.on("keyHandled", maybeClearPrefix);
+ cm.on("inputRead", maybeDuplicateInput);
+ }
+
+ var prefixPreservingKeys = {"Alt-G": true, "Ctrl-X": true, "Ctrl-Q": true, "Ctrl-U": true};
+
+ function maybeClearPrefix(cm, arg) {
+ if (!cm.state.emacsPrefixMap && !prefixPreservingKeys.hasOwnProperty(arg))
+ clearPrefix(cm);
+ }
+
+ function clearPrefix(cm) {
+ cm.state.emacsPrefix = null;
+ cm.off("keyHandled", maybeClearPrefix);
+ cm.off("inputRead", maybeDuplicateInput);
+ }
+
+ function maybeDuplicateInput(cm, event) {
+ var dup = getPrefix(cm);
+ if (dup > 1 && event.origin == "+input") {
+ var one = event.text.join("\n"), txt = "";
+ for (var i = 1; i < dup; ++i) txt += one;
+ cm.replaceSelection(txt, "end", "+input");
+ }
+ }
+
+ function addPrefixMap(cm) {
+ cm.state.emacsPrefixMap = true;
+ cm.addKeyMap(prefixMap);
+ cm.on("keyHandled", maybeRemovePrefixMap);
+ cm.on("inputRead", maybeRemovePrefixMap);
+ }
+
+ function maybeRemovePrefixMap(cm, arg) {
+ if (typeof arg == "string" && (/^\d$/.test(arg) || arg == "Ctrl-U")) return;
+ cm.removeKeyMap(prefixMap);
+ cm.state.emacsPrefixMap = false;
+ cm.off("keyHandled", maybeRemovePrefixMap);
+ cm.off("inputRead", maybeRemovePrefixMap);
+ }
+
+ // Utilities
+
+ function setMark(cm) {
+ cm.setCursor(cm.getCursor());
+ cm.setExtending(true);
+ cm.on("change", function() { cm.setExtending(false); });
+ }
+
+ function getInput(cm, msg, f) {
+ if (cm.openDialog)
+ cm.openDialog(msg + ": ", f, {bottom: true});
+ else
+ f(prompt(msg, ""));
+ }
+
+ function operateOnWord(cm, op) {
+ var start = cm.getCursor(), end = cm.findPosH(start, 1, "word");
+ cm.replaceRange(op(cm.getRange(start, end)), start, end);
+ cm.setCursor(end);
+ }
+
+ function toEnclosingExpr(cm) {
+ var pos = cm.getCursor(), line = pos.line, ch = pos.ch;
+ var stack = [];
+ while (line >= cm.firstLine()) {
+ var text = cm.getLine(line);
+ for (var i = ch == null ? text.length : ch; i > 0;) {
+ var ch = text.charAt(--i);
+ if (ch == ")")
+ stack.push("(");
+ else if (ch == "]")
+ stack.push("[");
+ else if (ch == "}")
+ stack.push("{");
+ else if (/[\(\{\[]/.test(ch) && (!stack.length || stack.pop() != ch))
+ return cm.extendSelection(Pos(line, i));
+ }
+ --line; ch = null;
+ }
+ }
+
+ // Actual keymap
+
+ var keyMap = CodeMirror.keyMap.emacs = {
+ "Ctrl-W": function(cm) {kill(cm, cm.getCursor("start"), cm.getCursor("end"));},
+ "Ctrl-K": repeated(function(cm) {
+ var start = cm.getCursor(), end = cm.clipPos(Pos(start.line));
+ var text = cm.getRange(start, end);
+ if (!/\S/.test(text)) {
+ text += "\n";
+ end = Pos(start.line + 1, 0);
+ }
+ kill(cm, start, end, true, text);
+ }),
+ "Alt-W": function(cm) {
+ addToRing(cm.getSelection());
+ },
+ "Ctrl-Y": function(cm) {
+ var start = cm.getCursor();
+ cm.replaceRange(getFromRing(getPrefix(cm)), start, start, "paste");
+ cm.setSelection(start, cm.getCursor());
+ },
+ "Alt-Y": function(cm) {cm.replaceSelection(popFromRing());},
+
+ "Ctrl-Space": setMark, "Ctrl-Shift-2": setMark,
+
+ "Ctrl-F": move(byChar, 1), "Ctrl-B": move(byChar, -1),
+ "Right": move(byChar, 1), "Left": move(byChar, -1),
+ "Ctrl-D": function(cm) { killTo(cm, byChar, 1); },
+ "Delete": function(cm) { killTo(cm, byChar, 1); },
+ "Ctrl-H": function(cm) { killTo(cm, byChar, -1); },
+ "Backspace": function(cm) { killTo(cm, byChar, -1); },
+
+ "Alt-F": move(byWord, 1), "Alt-B": move(byWord, -1),
+ "Alt-D": function(cm) { killTo(cm, byWord, 1); },
+ "Alt-Backspace": function(cm) { killTo(cm, byWord, -1); },
+
+ "Ctrl-N": move(byLine, 1), "Ctrl-P": move(byLine, -1),
+ "Down": move(byLine, 1), "Up": move(byLine, -1),
+ "Ctrl-A": "goLineStart", "Ctrl-E": "goLineEnd",
+ "End": "goLineEnd", "Home": "goLineStart",
+
+ "Alt-V": move(byPage, -1), "Ctrl-V": move(byPage, 1),
+ "PageUp": move(byPage, -1), "PageDown": move(byPage, 1),
+
+ "Ctrl-Up": move(byParagraph, -1), "Ctrl-Down": move(byParagraph, 1),
+
+ "Alt-A": move(bySentence, -1), "Alt-E": move(bySentence, 1),
+ "Alt-K": function(cm) { killTo(cm, bySentence, 1); },
+
+ "Ctrl-Alt-K": function(cm) { killTo(cm, byExpr, 1); },
+ "Ctrl-Alt-Backspace": function(cm) { killTo(cm, byExpr, -1); },
+ "Ctrl-Alt-F": move(byExpr, 1), "Ctrl-Alt-B": move(byExpr, -1),
+
+ "Shift-Ctrl-Alt-2": function(cm) {
+ cm.setSelection(findEnd(cm, byExpr, 1), cm.getCursor());
+ },
+ "Ctrl-Alt-T": function(cm) {
+ var leftStart = byExpr(cm, cm.getCursor(), -1), leftEnd = byExpr(cm, leftStart, 1);
+ var rightEnd = byExpr(cm, leftEnd, 1), rightStart = byExpr(cm, rightEnd, -1);
+ cm.replaceRange(cm.getRange(rightStart, rightEnd) + cm.getRange(leftEnd, rightStart) +
+ cm.getRange(leftStart, leftEnd), leftStart, rightEnd);
+ },
+ "Ctrl-Alt-U": repeated(toEnclosingExpr),
+
+ "Alt-Space": function(cm) {
+ var pos = cm.getCursor(), from = pos.ch, to = pos.ch, text = cm.getLine(pos.line);
+ while (from && /\s/.test(text.charAt(from - 1))) --from;
+ while (to < text.length && /\s/.test(text.charAt(to))) ++to;
+ cm.replaceRange(" ", Pos(pos.line, from), Pos(pos.line, to));
+ },
+ "Ctrl-O": repeated(function(cm) { cm.replaceSelection("\n", "start"); }),
+ "Ctrl-T": repeated(function(cm) {
+ var pos = cm.getCursor();
+ if (pos.ch < cm.getLine(pos.line).length) pos = Pos(pos.line, pos.ch + 1);
+ var from = cm.findPosH(pos, -2, "char");
+ var range = cm.getRange(from, pos);
+ if (range.length != 2) return;
+ cm.setSelection(from, pos);
+ cm.replaceSelection(range.charAt(1) + range.charAt(0), "end");
+ }),
+
+ "Alt-C": repeated(function(cm) {
+ operateOnWord(cm, function(w) {
+ var letter = w.search(/\w/);
+ if (letter == -1) return w;
+ return w.slice(0, letter) + w.charAt(letter).toUpperCase() + w.slice(letter + 1).toLowerCase();
+ });
+ }),
+ "Alt-U": repeated(function(cm) {
+ operateOnWord(cm, function(w) { return w.toUpperCase(); });
+ }),
+ "Alt-L": repeated(function(cm) {
+ operateOnWord(cm, function(w) { return w.toLowerCase(); });
+ }),
+
+ "Alt-;": "toggleComment",
+
+ "Ctrl-/": repeated("undo"), "Shift-Ctrl--": repeated("undo"),
+ "Ctrl-Z": repeated("undo"), "Cmd-Z": repeated("undo"),
+ "Shift-Alt-,": "goDocStart", "Shift-Alt-.": "goDocEnd",
+ "Ctrl-S": "findNext", "Ctrl-R": "findPrev", "Ctrl-G": "clearSearch", "Shift-Alt-5": "replace",
+ "Alt-/": "autocomplete",
+ "Ctrl-J": "newlineAndIndent", "Enter": false, "Tab": "indentAuto",
+
+ "Alt-G": function(cm) {cm.setOption("keyMap", "emacs-Alt-G");},
+ "Ctrl-X": function(cm) {cm.setOption("keyMap", "emacs-Ctrl-X");},
+ "Ctrl-Q": function(cm) {cm.setOption("keyMap", "emacs-Ctrl-Q");},
+ "Ctrl-U": addPrefixMap
+ };
+
+ CodeMirror.keyMap["emacs-Ctrl-X"] = {
+ "Tab": function(cm) {
+ cm.indentSelection(getPrefix(cm, true) || cm.getOption("indentUnit"));
+ },
+ "Ctrl-X": function(cm) {
+ cm.setSelection(cm.getCursor("head"), cm.getCursor("anchor"));
+ },
+
+ "Ctrl-S": "save", "Ctrl-W": "save", "S": "saveAll", "F": "open", "U": repeated("undo"), "K": "close",
+ "Delete": function(cm) { kill(cm, cm.getCursor(), bySentence(cm, cm.getCursor(), 1), true); },
+ auto: "emacs", nofallthrough: true, disableInput: true
+ };
+
+ CodeMirror.keyMap["emacs-Alt-G"] = {
+ "G": function(cm) {
+ var prefix = getPrefix(cm, true);
+ if (prefix != null && prefix > 0) return cm.setCursor(prefix - 1);
+
+ getInput(cm, "Goto line", function(str) {
+ var num;
+ if (str && !isNaN(num = Number(str)) && num == num|0 && num > 0)
+ cm.setCursor(num - 1);
+ });
+ },
+ auto: "emacs", nofallthrough: true, disableInput: true
+ };
+
+ CodeMirror.keyMap["emacs-Ctrl-Q"] = {
+ "Tab": repeated("insertTab"),
+ auto: "emacs", nofallthrough: true
+ };
+
+ var prefixMap = {"Ctrl-G": clearPrefix};
+ function regPrefix(d) {
+ prefixMap[d] = function(cm) { addPrefix(cm, d); };
+ keyMap["Ctrl-" + d] = function(cm) { addPrefix(cm, d); };
+ prefixPreservingKeys["Ctrl-" + d] = true;
+ }
+ for (var i = 0; i < 10; ++i) regPrefix(String(i));
+ regPrefix("-");
+})();
diff --git a/plugins/codemirror/codemirror/keymap/extra.js b/plugins/codemirror/codemirror/keymap/extra.js
new file mode 100644
index 0000000..18dd5a9
--- /dev/null
+++ b/plugins/codemirror/codemirror/keymap/extra.js
@@ -0,0 +1,43 @@
+// A number of additional default bindings that are too obscure to
+// include in the core codemirror.js file.
+
+(function() {
+ "use strict";
+
+ var Pos = CodeMirror.Pos;
+
+ function moveLines(cm, start, end, dist) {
+ if (!dist || start > end) return 0;
+
+ var from = cm.clipPos(Pos(start, 0)), to = cm.clipPos(Pos(end));
+ var text = cm.getRange(from, to);
+
+ if (start <= cm.firstLine())
+ cm.replaceRange("", from, Pos(to.line + 1, 0));
+ else
+ cm.replaceRange("", Pos(from.line - 1), to);
+ var target = from.line + dist;
+ if (target <= cm.firstLine()) {
+ cm.replaceRange(text + "\n", Pos(target, 0));
+ return cm.firstLine() - from.line;
+ } else {
+ var targetPos = cm.clipPos(Pos(target - 1));
+ cm.replaceRange("\n" + text, targetPos);
+ return targetPos.line + 1 - from.line;
+ }
+ }
+
+ function moveSelectedLines(cm, dist) {
+ var head = cm.getCursor("head"), anchor = cm.getCursor("anchor");
+ cm.operation(function() {
+ var moved = moveLines(cm, Math.min(head.line, anchor.line), Math.max(head.line, anchor.line), dist);
+ cm.setSelection(Pos(anchor.line + moved, anchor.ch), Pos(head.line + moved, head.ch));
+ });
+ }
+
+ CodeMirror.commands.moveLinesUp = function(cm) { moveSelectedLines(cm, -1); };
+ CodeMirror.commands.moveLinesDown = function(cm) { moveSelectedLines(cm, 1); };
+
+ CodeMirror.keyMap["default"]["Alt-Up"] = "moveLinesUp";
+ CodeMirror.keyMap["default"]["Alt-Down"] = "moveLinesDown";
+})();
diff --git a/plugins/codemirror/codemirror/keymap/vim.js b/plugins/codemirror/codemirror/keymap/vim.js
new file mode 100644
index 0000000..dab10e2
--- /dev/null
+++ b/plugins/codemirror/codemirror/keymap/vim.js
@@ -0,0 +1,3703 @@
+/**
+ * Supported keybindings:
+ *
+ * Motion:
+ * h, j, k, l
+ * gj, gk
+ * e, E, w, W, b, B, ge, gE
+ * f, F, t, T
+ * $, ^, 0, -, +, _
+ * gg, G
+ * %
+ * ', `
+ *
+ * Operator:
+ * d, y, c
+ * dd, yy, cc
+ * g~, g~g~
+ * >, <, >>, <<
+ *
+ * Operator-Motion:
+ * x, X, D, Y, C, ~
+ *
+ * Action:
+ * a, i, s, A, I, S, o, O
+ * zz, z., z, zt, zb, z-
+ * J
+ * u, Ctrl-r
+ * m
+ * r
+ *
+ * Modes:
+ * ESC - leave insert mode, visual mode, and clear input state.
+ * Ctrl-[, Ctrl-c - same as ESC.
+ *
+ * Registers: unamed, -, a-z, A-Z, 0-9
+ * (Does not respect the special case for number registers when delete
+ * operator is made with these commands: %, (, ), , /, ?, n, N, {, } )
+ * TODO: Implement the remaining registers.
+ * Marks: a-z, A-Z, and 0-9
+ * TODO: Implement the remaining special marks. They have more complex
+ * behavior.
+ *
+ * Events:
+ * 'vim-mode-change' - raised on the editor anytime the current mode changes,
+ * Event object: {mode: "visual", subMode: "linewise"}
+ *
+ * Code structure:
+ * 1. Default keymap
+ * 2. Variable declarations and short basic helpers
+ * 3. Instance (External API) implementation
+ * 4. Internal state tracking objects (input state, counter) implementation
+ * and instanstiation
+ * 5. Key handler (the main command dispatcher) implementation
+ * 6. Motion, operator, and action implementations
+ * 7. Helper functions for the key handler, motions, operators, and actions
+ * 8. Set up Vim to work as a keymap for CodeMirror.
+ */
+
+(function() {
+ 'use strict';
+
+ var defaultKeymap = [
+ // Key to key mapping. This goes first to make it possible to override
+ // existing mappings.
+ { keys: [''], type: 'keyToKey', toKeys: ['h'] },
+ { keys: [''], type: 'keyToKey', toKeys: ['l'] },
+ { keys: [''], type: 'keyToKey', toKeys: ['k'] },
+ { keys: [''], type: 'keyToKey', toKeys: ['j'] },
+ { keys: [''], type: 'keyToKey', toKeys: ['l'] },
+ { keys: [''], type: 'keyToKey', toKeys: ['h'] },
+ { keys: [''], type: 'keyToKey', toKeys: ['W'] },
+ { keys: [''], type: 'keyToKey', toKeys: ['B'] },
+ { keys: [''], type: 'keyToKey', toKeys: ['w'] },
+ { keys: [''], type: 'keyToKey', toKeys: ['b'] },
+ { keys: [''], type: 'keyToKey', toKeys: ['j'] },
+ { keys: [''], type: 'keyToKey', toKeys: ['k'] },
+ { keys: ['C-['], type: 'keyToKey', toKeys: [''] },
+ { keys: [''], type: 'keyToKey', toKeys: [''] },
+ { keys: ['s'], type: 'keyToKey', toKeys: ['c', 'l'], context: 'normal' },
+ { keys: ['s'], type: 'keyToKey', toKeys: ['x', 'i'], context: 'visual'},
+ { keys: ['S'], type: 'keyToKey', toKeys: ['c', 'c'], context: 'normal' },
+ { keys: ['S'], type: 'keyToKey', toKeys: ['d', 'c', 'c'], context: 'visual' },
+ { keys: [''], type: 'keyToKey', toKeys: ['0'] },
+ { keys: [''], type: 'keyToKey', toKeys: ['$'] },
+ { keys: [''], type: 'keyToKey', toKeys: [''] },
+ { keys: [''], type: 'keyToKey', toKeys: [''] },
+ // Motions
+ { keys: ['H'], type: 'motion',
+ motion: 'moveToTopLine',
+ motionArgs: { linewise: true, toJumplist: true }},
+ { keys: ['M'], type: 'motion',
+ motion: 'moveToMiddleLine',
+ motionArgs: { linewise: true, toJumplist: true }},
+ { keys: ['L'], type: 'motion',
+ motion: 'moveToBottomLine',
+ motionArgs: { linewise: true, toJumplist: true }},
+ { keys: ['h'], type: 'motion',
+ motion: 'moveByCharacters',
+ motionArgs: { forward: false }},
+ { keys: ['l'], type: 'motion',
+ motion: 'moveByCharacters',
+ motionArgs: { forward: true }},
+ { keys: ['j'], type: 'motion',
+ motion: 'moveByLines',
+ motionArgs: { forward: true, linewise: true }},
+ { keys: ['k'], type: 'motion',
+ motion: 'moveByLines',
+ motionArgs: { forward: false, linewise: true }},
+ { keys: ['g','j'], type: 'motion',
+ motion: 'moveByDisplayLines',
+ motionArgs: { forward: true }},
+ { keys: ['g','k'], type: 'motion',
+ motion: 'moveByDisplayLines',
+ motionArgs: { forward: false }},
+ { keys: ['w'], type: 'motion',
+ motion: 'moveByWords',
+ motionArgs: { forward: true, wordEnd: false }},
+ { keys: ['W'], type: 'motion',
+ motion: 'moveByWords',
+ motionArgs: { forward: true, wordEnd: false, bigWord: true }},
+ { keys: ['e'], type: 'motion',
+ motion: 'moveByWords',
+ motionArgs: { forward: true, wordEnd: true, inclusive: true }},
+ { keys: ['E'], type: 'motion',
+ motion: 'moveByWords',
+ motionArgs: { forward: true, wordEnd: true, bigWord: true,
+ inclusive: true }},
+ { keys: ['b'], type: 'motion',
+ motion: 'moveByWords',
+ motionArgs: { forward: false, wordEnd: false }},
+ { keys: ['B'], type: 'motion',
+ motion: 'moveByWords',
+ motionArgs: { forward: false, wordEnd: false, bigWord: true }},
+ { keys: ['g', 'e'], type: 'motion',
+ motion: 'moveByWords',
+ motionArgs: { forward: false, wordEnd: true, inclusive: true }},
+ { keys: ['g', 'E'], type: 'motion',
+ motion: 'moveByWords',
+ motionArgs: { forward: false, wordEnd: true, bigWord: true,
+ inclusive: true }},
+ { keys: ['{'], type: 'motion', motion: 'moveByParagraph',
+ motionArgs: { forward: false, toJumplist: true }},
+ { keys: ['}'], type: 'motion', motion: 'moveByParagraph',
+ motionArgs: { forward: true, toJumplist: true }},
+ { keys: [''], type: 'motion',
+ motion: 'moveByPage', motionArgs: { forward: true }},
+ { keys: [''], type: 'motion',
+ motion: 'moveByPage', motionArgs: { forward: false }},
+ { keys: [''], type: 'motion',
+ motion: 'moveByScroll',
+ motionArgs: { forward: true, explicitRepeat: true }},
+ { keys: [''], type: 'motion',
+ motion: 'moveByScroll',
+ motionArgs: { forward: false, explicitRepeat: true }},
+ { keys: ['g', 'g'], type: 'motion',
+ motion: 'moveToLineOrEdgeOfDocument',
+ motionArgs: { forward: false, explicitRepeat: true, linewise: true, toJumplist: true }},
+ { keys: ['G'], type: 'motion',
+ motion: 'moveToLineOrEdgeOfDocument',
+ motionArgs: { forward: true, explicitRepeat: true, linewise: true, toJumplist: true }},
+ { keys: ['0'], type: 'motion', motion: 'moveToStartOfLine' },
+ { keys: ['^'], type: 'motion',
+ motion: 'moveToFirstNonWhiteSpaceCharacter' },
+ { keys: ['+'], type: 'motion',
+ motion: 'moveByLines',
+ motionArgs: { forward: true, toFirstChar:true }},
+ { keys: ['-'], type: 'motion',
+ motion: 'moveByLines',
+ motionArgs: { forward: false, toFirstChar:true }},
+ { keys: ['_'], type: 'motion',
+ motion: 'moveByLines',
+ motionArgs: { forward: true, toFirstChar:true, repeatOffset:-1 }},
+ { keys: ['$'], type: 'motion',
+ motion: 'moveToEol',
+ motionArgs: { inclusive: true }},
+ { keys: ['%'], type: 'motion',
+ motion: 'moveToMatchedSymbol',
+ motionArgs: { inclusive: true, toJumplist: true }},
+ { keys: ['f', 'character'], type: 'motion',
+ motion: 'moveToCharacter',
+ motionArgs: { forward: true , inclusive: true }},
+ { keys: ['F', 'character'], type: 'motion',
+ motion: 'moveToCharacter',
+ motionArgs: { forward: false }},
+ { keys: ['t', 'character'], type: 'motion',
+ motion: 'moveTillCharacter',
+ motionArgs: { forward: true, inclusive: true }},
+ { keys: ['T', 'character'], type: 'motion',
+ motion: 'moveTillCharacter',
+ motionArgs: { forward: false }},
+ { keys: [';'], type: 'motion', motion: 'repeatLastCharacterSearch',
+ motionArgs: { forward: true }},
+ { keys: [','], type: 'motion', motion: 'repeatLastCharacterSearch',
+ motionArgs: { forward: false }},
+ { keys: ['\'', 'character'], type: 'motion', motion: 'goToMark',
+ motionArgs: {toJumplist: true}},
+ { keys: ['`', 'character'], type: 'motion', motion: 'goToMark',
+ motionArgs: {toJumplist: true}},
+ { keys: [']', '`'], type: 'motion', motion: 'jumpToMark', motionArgs: { forward: true } },
+ { keys: ['[', '`'], type: 'motion', motion: 'jumpToMark', motionArgs: { forward: false } },
+ { keys: [']', '\''], type: 'motion', motion: 'jumpToMark', motionArgs: { forward: true, linewise: true } },
+ { keys: ['[', '\''], type: 'motion', motion: 'jumpToMark', motionArgs: { forward: false, linewise: true } },
+ { keys: [']', 'character'], type: 'motion',
+ motion: 'moveToSymbol',
+ motionArgs: { forward: true, toJumplist: true}},
+ { keys: ['[', 'character'], type: 'motion',
+ motion: 'moveToSymbol',
+ motionArgs: { forward: false, toJumplist: true}},
+ { keys: ['|'], type: 'motion',
+ motion: 'moveToColumn',
+ motionArgs: { }},
+ // Operators
+ { keys: ['d'], type: 'operator', operator: 'delete' },
+ { keys: ['y'], type: 'operator', operator: 'yank' },
+ { keys: ['c'], type: 'operator', operator: 'change' },
+ { keys: ['>'], type: 'operator', operator: 'indent',
+ operatorArgs: { indentRight: true }},
+ { keys: ['<'], type: 'operator', operator: 'indent',
+ operatorArgs: { indentRight: false }},
+ { keys: ['g', '~'], type: 'operator', operator: 'swapcase' },
+ { keys: ['n'], type: 'motion', motion: 'findNext',
+ motionArgs: { forward: true, toJumplist: true }},
+ { keys: ['N'], type: 'motion', motion: 'findNext',
+ motionArgs: { forward: false, toJumplist: true }},
+ // Operator-Motion dual commands
+ { keys: ['x'], type: 'operatorMotion', operator: 'delete',
+ motion: 'moveByCharacters', motionArgs: { forward: true },
+ operatorMotionArgs: { visualLine: false }},
+ { keys: ['X'], type: 'operatorMotion', operator: 'delete',
+ motion: 'moveByCharacters', motionArgs: { forward: false },
+ operatorMotionArgs: { visualLine: true }},
+ { keys: ['D'], type: 'operatorMotion', operator: 'delete',
+ motion: 'moveToEol', motionArgs: { inclusive: true },
+ operatorMotionArgs: { visualLine: true }},
+ { keys: ['Y'], type: 'operatorMotion', operator: 'yank',
+ motion: 'moveToEol', motionArgs: { inclusive: true },
+ operatorMotionArgs: { visualLine: true }},
+ { keys: ['C'], type: 'operatorMotion',
+ operator: 'change',
+ motion: 'moveToEol', motionArgs: { inclusive: true },
+ operatorMotionArgs: { visualLine: true }},
+ { keys: ['~'], type: 'operatorMotion',
+ operator: 'swapcase', operatorArgs: { shouldMoveCursor: true },
+ motion: 'moveByCharacters', motionArgs: { forward: true }},
+ // Actions
+ { keys: [''], type: 'action', action: 'jumpListWalk',
+ actionArgs: { forward: true }},
+ { keys: [''], type: 'action', action: 'jumpListWalk',
+ actionArgs: { forward: false }},
+ { keys: ['a'], type: 'action', action: 'enterInsertMode', isEdit: true,
+ actionArgs: { insertAt: 'charAfter' }},
+ { keys: ['A'], type: 'action', action: 'enterInsertMode', isEdit: true,
+ actionArgs: { insertAt: 'eol' }},
+ { keys: ['i'], type: 'action', action: 'enterInsertMode', isEdit: true,
+ actionArgs: { insertAt: 'inplace' }},
+ { keys: ['I'], type: 'action', action: 'enterInsertMode', isEdit: true,
+ actionArgs: { insertAt: 'firstNonBlank' }},
+ { keys: ['o'], type: 'action', action: 'newLineAndEnterInsertMode',
+ isEdit: true, interlaceInsertRepeat: true,
+ actionArgs: { after: true }},
+ { keys: ['O'], type: 'action', action: 'newLineAndEnterInsertMode',
+ isEdit: true, interlaceInsertRepeat: true,
+ actionArgs: { after: false }},
+ { keys: ['v'], type: 'action', action: 'toggleVisualMode' },
+ { keys: ['V'], type: 'action', action: 'toggleVisualMode',
+ actionArgs: { linewise: true }},
+ { keys: ['J'], type: 'action', action: 'joinLines', isEdit: true },
+ { keys: ['p'], type: 'action', action: 'paste', isEdit: true,
+ actionArgs: { after: true, isEdit: true }},
+ { keys: ['P'], type: 'action', action: 'paste', isEdit: true,
+ actionArgs: { after: false, isEdit: true }},
+ { keys: ['r', 'character'], type: 'action', action: 'replace', isEdit: true },
+ { keys: ['@', 'character'], type: 'action', action: 'replayMacro' },
+ { keys: ['q', 'character'], type: 'action', action: 'enterMacroRecordMode' },
+ // Handle Replace-mode as a special case of insert mode.
+ { keys: ['R'], type: 'action', action: 'enterInsertMode', isEdit: true,
+ actionArgs: { replace: true }},
+ { keys: ['u'], type: 'action', action: 'undo' },
+ { keys: [''], type: 'action', action: 'redo' },
+ { keys: ['m', 'character'], type: 'action', action: 'setMark' },
+ { keys: ['"', 'character'], type: 'action', action: 'setRegister' },
+ { keys: ['z', 'z'], type: 'action', action: 'scrollToCursor',
+ actionArgs: { position: 'center' }},
+ { keys: ['z', '.'], type: 'action', action: 'scrollToCursor',
+ actionArgs: { position: 'center' },
+ motion: 'moveToFirstNonWhiteSpaceCharacter' },
+ { keys: ['z', 't'], type: 'action', action: 'scrollToCursor',
+ actionArgs: { position: 'top' }},
+ { keys: ['z', ''], type: 'action', action: 'scrollToCursor',
+ actionArgs: { position: 'top' },
+ motion: 'moveToFirstNonWhiteSpaceCharacter' },
+ { keys: ['z', '-'], type: 'action', action: 'scrollToCursor',
+ actionArgs: { position: 'bottom' }},
+ { keys: ['z', 'b'], type: 'action', action: 'scrollToCursor',
+ actionArgs: { position: 'bottom' },
+ motion: 'moveToFirstNonWhiteSpaceCharacter' },
+ { keys: ['.'], type: 'action', action: 'repeatLastEdit' },
+ { keys: [''], type: 'action', action: 'incrementNumberToken',
+ isEdit: true,
+ actionArgs: {increase: true, backtrack: false}},
+ { keys: [''], type: 'action', action: 'incrementNumberToken',
+ isEdit: true,
+ actionArgs: {increase: false, backtrack: false}},
+ // Text object motions
+ { keys: ['a', 'character'], type: 'motion',
+ motion: 'textObjectManipulation' },
+ { keys: ['i', 'character'], type: 'motion',
+ motion: 'textObjectManipulation',
+ motionArgs: { textObjectInner: true }},
+ // Search
+ { keys: ['/'], type: 'search',
+ searchArgs: { forward: true, querySrc: 'prompt', toJumplist: true }},
+ { keys: ['?'], type: 'search',
+ searchArgs: { forward: false, querySrc: 'prompt', toJumplist: true }},
+ { keys: ['*'], type: 'search',
+ searchArgs: { forward: true, querySrc: 'wordUnderCursor', toJumplist: true }},
+ { keys: ['#'], type: 'search',
+ searchArgs: { forward: false, querySrc: 'wordUnderCursor', toJumplist: true }},
+ // Ex command
+ { keys: [':'], type: 'ex' }
+ ];
+
+ var Vim = function() {
+ CodeMirror.defineOption('vimMode', false, function(cm, val) {
+ if (val) {
+ cm.setOption('keyMap', 'vim');
+ CodeMirror.signal(cm, "vim-mode-change", {mode: "normal"});
+ cm.on('beforeSelectionChange', beforeSelectionChange);
+ maybeInitVimState(cm);
+ } else if (cm.state.vim) {
+ cm.setOption('keyMap', 'default');
+ cm.off('beforeSelectionChange', beforeSelectionChange);
+ cm.state.vim = null;
+ }
+ });
+ function beforeSelectionChange(cm, cur) {
+ var vim = cm.state.vim;
+ if (vim.insertMode || vim.exMode) return;
+
+ var head = cur.head;
+ if (head.ch && head.ch == cm.doc.getLine(head.line).length) {
+ head.ch--;
+ }
+ }
+
+ var numberRegex = /[\d]/;
+ var wordRegexp = [(/\w/), (/[^\w\s]/)], bigWordRegexp = [(/\S/)];
+ function makeKeyRange(start, size) {
+ var keys = [];
+ for (var i = start; i < start + size; i++) {
+ keys.push(String.fromCharCode(i));
+ }
+ return keys;
+ }
+ var upperCaseAlphabet = makeKeyRange(65, 26);
+ var lowerCaseAlphabet = makeKeyRange(97, 26);
+ var numbers = makeKeyRange(48, 10);
+ var specialSymbols = '~`!@#$%^&*()_-+=[{}]\\|/?.,<>:;"\''.split('');
+ var specialKeys = ['Left', 'Right', 'Up', 'Down', 'Space', 'Backspace',
+ 'Esc', 'Home', 'End', 'PageUp', 'PageDown', 'Enter'];
+ var validMarks = [].concat(upperCaseAlphabet, lowerCaseAlphabet, numbers, ['<', '>']);
+ var validRegisters = [].concat(upperCaseAlphabet, lowerCaseAlphabet, numbers, ['-', '"']);
+
+ function isLine(cm, line) {
+ return line >= cm.firstLine() && line <= cm.lastLine();
+ }
+ function isLowerCase(k) {
+ return (/^[a-z]$/).test(k);
+ }
+ function isMatchableSymbol(k) {
+ return '()[]{}'.indexOf(k) != -1;
+ }
+ function isNumber(k) {
+ return numberRegex.test(k);
+ }
+ function isUpperCase(k) {
+ return (/^[A-Z]$/).test(k);
+ }
+ function isWhiteSpaceString(k) {
+ return (/^\s*$/).test(k);
+ }
+ function inArray(val, arr) {
+ for (var i = 0; i < arr.length; i++) {
+ if (arr[i] == val) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ var createCircularJumpList = function() {
+ var size = 100;
+ var pointer = -1;
+ var head = 0;
+ var tail = 0;
+ var buffer = new Array(size);
+ function add(cm, oldCur, newCur) {
+ var current = pointer % size;
+ var curMark = buffer[current];
+ function useNextSlot(cursor) {
+ var next = ++pointer % size;
+ var trashMark = buffer[next];
+ if (trashMark) {
+ trashMark.clear();
+ }
+ buffer[next] = cm.setBookmark(cursor);
+ }
+ if (curMark) {
+ var markPos = curMark.find();
+ // avoid recording redundant cursor position
+ if (markPos && !cursorEqual(markPos, oldCur)) {
+ useNextSlot(oldCur);
+ }
+ } else {
+ useNextSlot(oldCur);
+ }
+ useNextSlot(newCur);
+ head = pointer;
+ tail = pointer - size + 1;
+ if (tail < 0) {
+ tail = 0;
+ }
+ }
+ function move(cm, offset) {
+ pointer += offset;
+ if (pointer > head) {
+ pointer = head;
+ } else if (pointer < tail) {
+ pointer = tail;
+ }
+ var mark = buffer[(size + pointer) % size];
+ // skip marks that are temporarily removed from text buffer
+ if (mark && !mark.find()) {
+ var inc = offset > 0 ? 1 : -1;
+ var newCur;
+ var oldCur = cm.getCursor();
+ do {
+ pointer += inc;
+ mark = buffer[(size + pointer) % size];
+ // skip marks that are the same as current position
+ if (mark &&
+ (newCur = mark.find()) &&
+ !cursorEqual(oldCur, newCur)) {
+ break;
+ }
+ } while (pointer < head && pointer > tail);
+ }
+ return mark;
+ }
+ return {
+ cachedCursor: undefined, //used for # and * jumps
+ add: add,
+ move: move
+ };
+ };
+
+ var createMacroState = function() {
+ return {
+ macroKeyBuffer: [],
+ latestRegister: undefined,
+ inReplay: false,
+ lastInsertModeChanges: {
+ changes: [], // Change list
+ expectCursorActivityForChange: false // Set to true on change, false on cursorActivity.
+ },
+ enteredMacroMode: undefined,
+ isMacroPlaying: false,
+ toggle: function(cm, registerName) {
+ if (this.enteredMacroMode) { //onExit
+ this.enteredMacroMode(); // close dialog
+ this.enteredMacroMode = undefined;
+ } else { //onEnter
+ this.latestRegister = registerName;
+ this.enteredMacroMode = cm.openDialog(
+ '(recording)['+registerName+']', null, {bottom:true});
+ }
+ }
+ };
+ };
+
+
+ function maybeInitVimState(cm) {
+ if (!cm.state.vim) {
+ // Store instance state in the CodeMirror object.
+ cm.state.vim = {
+ inputState: new InputState(),
+ // Vim's input state that triggered the last edit, used to repeat
+ // motions and operators with '.'.
+ lastEditInputState: undefined,
+ // Vim's action command before the last edit, used to repeat actions
+ // with '.' and insert mode repeat.
+ lastEditActionCommand: undefined,
+ // When using jk for navigation, if you move from a longer line to a
+ // shorter line, the cursor may clip to the end of the shorter line.
+ // If j is pressed again and cursor goes to the next line, the
+ // cursor should go back to its horizontal position on the longer
+ // line if it can. This is to keep track of the horizontal position.
+ lastHPos: -1,
+ // Doing the same with screen-position for gj/gk
+ lastHSPos: -1,
+ // The last motion command run. Cleared if a non-motion command gets
+ // executed in between.
+ lastMotion: null,
+ marks: {},
+ insertMode: false,
+ // Repeat count for changes made in insert mode, triggered by key
+ // sequences like 3,i. Only exists when insertMode is true.
+ insertModeRepeat: undefined,
+ visualMode: false,
+ // If we are in visual line mode. No effect if visualMode is false.
+ visualLine: false
+ };
+ }
+ return cm.state.vim;
+ }
+ var vimGlobalState;
+ function resetVimGlobalState() {
+ vimGlobalState = {
+ // The current search query.
+ searchQuery: null,
+ // Whether we are searching backwards.
+ searchIsReversed: false,
+ jumpList: createCircularJumpList(),
+ macroModeState: createMacroState(),
+ // Recording latest f, t, F or T motion command.
+ lastChararacterSearch: {increment:0, forward:true, selectedCharacter:''},
+ registerController: new RegisterController({})
+ };
+ }
+
+ var vimApi= {
+ buildKeyMap: function() {
+ // TODO: Convert keymap into dictionary format for fast lookup.
+ },
+ // Testing hook, though it might be useful to expose the register
+ // controller anyways.
+ getRegisterController: function() {
+ return vimGlobalState.registerController;
+ },
+ // Testing hook.
+ resetVimGlobalState_: resetVimGlobalState,
+
+ // Testing hook.
+ getVimGlobalState_: function() {
+ return vimGlobalState;
+ },
+
+ // Testing hook.
+ maybeInitVimState_: maybeInitVimState,
+
+ InsertModeKey: InsertModeKey,
+ map: function(lhs, rhs) {
+ // Add user defined key bindings.
+ exCommandDispatcher.map(lhs, rhs);
+ },
+ defineEx: function(name, prefix, func){
+ if (name.indexOf(prefix) !== 0) {
+ throw new Error('(Vim.defineEx) "'+prefix+'" is not a prefix of "'+name+'", command not registered');
+ }
+ exCommands[name]=func;
+ exCommandDispatcher.commandMap_[prefix]={name:name, shortName:prefix, type:'api'};
+ },
+ // This is the outermost function called by CodeMirror, after keys have
+ // been mapped to their Vim equivalents.
+ handleKey: function(cm, key) {
+ var command;
+ var vim = maybeInitVimState(cm);
+ var macroModeState = vimGlobalState.macroModeState;
+ if (macroModeState.enteredMacroMode) {
+ if (key == 'q') {
+ actions.exitMacroRecordMode();
+ vim.inputState = new InputState();
+ return;
+ }
+ }
+ if (key == '') {
+ // Clear input state and get back to normal mode.
+ vim.inputState = new InputState();
+ if (vim.visualMode) {
+ exitVisualMode(cm);
+ }
+ return;
+ }
+ // Enter visual mode when the mouse selects text.
+ if (!vim.visualMode &&
+ !cursorEqual(cm.getCursor('head'), cm.getCursor('anchor'))) {
+ vim.visualMode = true;
+ vim.visualLine = false;
+ CodeMirror.signal(cm, "vim-mode-change", {mode: "visual"});
+ cm.on('mousedown', exitVisualMode);
+ }
+ if (key != '0' || (key == '0' && vim.inputState.getRepeat() === 0)) {
+ // Have to special case 0 since it's both a motion and a number.
+ command = commandDispatcher.matchCommand(key, defaultKeymap, vim);
+ }
+ if (!command) {
+ if (isNumber(key)) {
+ // Increment count unless count is 0 and key is 0.
+ vim.inputState.pushRepeatDigit(key);
+ }
+ return;
+ }
+ if (command.type == 'keyToKey') {
+ // TODO: prevent infinite recursion.
+ for (var i = 0; i < command.toKeys.length; i++) {
+ this.handleKey(cm, command.toKeys[i]);
+ }
+ } else {
+ if (macroModeState.enteredMacroMode) {
+ logKey(macroModeState, key);
+ }
+ commandDispatcher.processCommand(cm, vim, command);
+ }
+ },
+ handleEx: function(cm, input) {
+ exCommandDispatcher.processCommand(cm, input);
+ }
+ };
+
+ // Represents the current input state.
+ function InputState() {
+ this.prefixRepeat = [];
+ this.motionRepeat = [];
+
+ this.operator = null;
+ this.operatorArgs = null;
+ this.motion = null;
+ this.motionArgs = null;
+ this.keyBuffer = []; // For matching multi-key commands.
+ this.registerName = null; // Defaults to the unamed register.
+ }
+ InputState.prototype.pushRepeatDigit = function(n) {
+ if (!this.operator) {
+ this.prefixRepeat = this.prefixRepeat.concat(n);
+ } else {
+ this.motionRepeat = this.motionRepeat.concat(n);
+ }
+ };
+ InputState.prototype.getRepeat = function() {
+ var repeat = 0;
+ if (this.prefixRepeat.length > 0 || this.motionRepeat.length > 0) {
+ repeat = 1;
+ if (this.prefixRepeat.length > 0) {
+ repeat *= parseInt(this.prefixRepeat.join(''), 10);
+ }
+ if (this.motionRepeat.length > 0) {
+ repeat *= parseInt(this.motionRepeat.join(''), 10);
+ }
+ }
+ return repeat;
+ };
+
+ /*
+ * Register stores information about copy and paste registers. Besides
+ * text, a register must store whether it is linewise (i.e., when it is
+ * pasted, should it insert itself into a new line, or should the text be
+ * inserted at the cursor position.)
+ */
+ function Register(text, linewise) {
+ this.clear();
+ if (text) {
+ this.set(text, linewise);
+ }
+ }
+ Register.prototype = {
+ set: function(text, linewise) {
+ this.text = text;
+ this.linewise = !!linewise;
+ },
+ append: function(text, linewise) {
+ // if this register has ever been set to linewise, use linewise.
+ if (linewise || this.linewise) {
+ this.text += '\n' + text;
+ this.linewise = true;
+ } else {
+ this.text += text;
+ }
+ },
+ clear: function() {
+ this.text = '';
+ this.linewise = false;
+ },
+ toString: function() { return this.text; }
+ };
+
+ /*
+ * vim registers allow you to keep many independent copy and paste buffers.
+ * See http://usevim.com/2012/04/13/registers/ for an introduction.
+ *
+ * RegisterController keeps the state of all the registers. An initial
+ * state may be passed in. The unnamed register '"' will always be
+ * overridden.
+ */
+ function RegisterController(registers) {
+ this.registers = registers;
+ this.unamedRegister = registers['"'] = new Register();
+ }
+ RegisterController.prototype = {
+ pushText: function(registerName, operator, text, linewise) {
+ if (linewise && text.charAt(0) == '\n') {
+ text = text.slice(1) + '\n';
+ }
+ if(linewise && text.charAt(text.length - 1) !== '\n'){
+ text += '\n';
+ }
+ // Lowercase and uppercase registers refer to the same register.
+ // Uppercase just means append.
+ var register = this.isValidRegister(registerName) ?
+ this.getRegister(registerName) : null;
+ // if no register/an invalid register was specified, things go to the
+ // default registers
+ if (!register) {
+ switch (operator) {
+ case 'yank':
+ // The 0 register contains the text from the most recent yank.
+ this.registers['0'] = new Register(text, linewise);
+ break;
+ case 'delete':
+ case 'change':
+ if (text.indexOf('\n') == -1) {
+ // Delete less than 1 line. Update the small delete register.
+ this.registers['-'] = new Register(text, linewise);
+ } else {
+ // Shift down the contents of the numbered registers and put the
+ // deleted text into register 1.
+ this.shiftNumericRegisters_();
+ this.registers['1'] = new Register(text, linewise);
+ }
+ break;
+ }
+ // Make sure the unnamed register is set to what just happened
+ this.unamedRegister.set(text, linewise);
+ return;
+ }
+
+ // If we've gotten to this point, we've actually specified a register
+ var append = isUpperCase(registerName);
+ if (append) {
+ register.append(text, linewise);
+ // The unamed register always has the same value as the last used
+ // register.
+ this.unamedRegister.append(text, linewise);
+ } else {
+ register.set(text, linewise);
+ this.unamedRegister.set(text, linewise);
+ }
+ },
+ setRegisterText: function(name, text, linewise) {
+ this.getRegister(name).set(text, linewise);
+ },
+ // Gets the register named @name. If one of @name doesn't already exist,
+ // create it. If @name is invalid, return the unamedRegister.
+ getRegister: function(name) {
+ if (!this.isValidRegister(name)) {
+ return this.unamedRegister;
+ }
+ name = name.toLowerCase();
+ if (!this.registers[name]) {
+ this.registers[name] = new Register();
+ }
+ return this.registers[name];
+ },
+ isValidRegister: function(name) {
+ return name && inArray(name, validRegisters);
+ },
+ shiftNumericRegisters_: function() {
+ for (var i = 9; i >= 2; i--) {
+ this.registers[i] = this.getRegister('' + (i - 1));
+ }
+ }
+ };
+
+ var commandDispatcher = {
+ matchCommand: function(key, keyMap, vim) {
+ var inputState = vim.inputState;
+ var keys = inputState.keyBuffer.concat(key);
+ var matchedCommands = [];
+ var selectedCharacter;
+ for (var i = 0; i < keyMap.length; i++) {
+ var command = keyMap[i];
+ if (matchKeysPartial(keys, command.keys)) {
+ if (inputState.operator && command.type == 'action') {
+ // Ignore matched action commands after an operator. Operators
+ // only operate on motions. This check is really for text
+ // objects since aW, a[ etcs conflicts with a.
+ continue;
+ }
+ // Match commands that take as an argument.
+ if (command.keys[keys.length - 1] == 'character') {
+ selectedCharacter = keys[keys.length - 1];
+ if(selectedCharacter.length>1){
+ switch(selectedCharacter){
+ case '':
+ selectedCharacter='\n';
+ break;
+ case '':
+ selectedCharacter=' ';
+ break;
+ default:
+ continue;
+ }
+ }
+ }
+ // Add the command to the list of matched commands. Choose the best
+ // command later.
+ matchedCommands.push(command);
+ }
+ }
+
+ // Returns the command if it is a full match, or null if not.
+ function getFullyMatchedCommandOrNull(command) {
+ if (keys.length < command.keys.length) {
+ // Matches part of a multi-key command. Buffer and wait for next
+ // stroke.
+ inputState.keyBuffer.push(key);
+ return null;
+ } else {
+ if (command.keys[keys.length - 1] == 'character') {
+ inputState.selectedCharacter = selectedCharacter;
+ }
+ // Clear the buffer since a full match was found.
+ inputState.keyBuffer = [];
+ return command;
+ }
+ }
+
+ if (!matchedCommands.length) {
+ // Clear the buffer since there were no matches.
+ inputState.keyBuffer = [];
+ return null;
+ } else if (matchedCommands.length == 1) {
+ return getFullyMatchedCommandOrNull(matchedCommands[0]);
+ } else {
+ // Find the best match in the list of matchedCommands.
+ var context = vim.visualMode ? 'visual' : 'normal';
+ var bestMatch = matchedCommands[0]; // Default to first in the list.
+ for (var i = 0; i < matchedCommands.length; i++) {
+ if (matchedCommands[i].context == context) {
+ bestMatch = matchedCommands[i];
+ break;
+ }
+ }
+ return getFullyMatchedCommandOrNull(bestMatch);
+ }
+ },
+ processCommand: function(cm, vim, command) {
+ vim.inputState.repeatOverride = command.repeatOverride;
+ switch (command.type) {
+ case 'motion':
+ this.processMotion(cm, vim, command);
+ break;
+ case 'operator':
+ this.processOperator(cm, vim, command);
+ break;
+ case 'operatorMotion':
+ this.processOperatorMotion(cm, vim, command);
+ break;
+ case 'action':
+ this.processAction(cm, vim, command);
+ break;
+ case 'search':
+ this.processSearch(cm, vim, command);
+ break;
+ case 'ex':
+ case 'keyToEx':
+ this.processEx(cm, vim, command);
+ break;
+ default:
+ break;
+ }
+ },
+ processMotion: function(cm, vim, command) {
+ vim.inputState.motion = command.motion;
+ vim.inputState.motionArgs = copyArgs(command.motionArgs);
+ this.evalInput(cm, vim);
+ },
+ processOperator: function(cm, vim, command) {
+ var inputState = vim.inputState;
+ if (inputState.operator) {
+ if (inputState.operator == command.operator) {
+ // Typing an operator twice like 'dd' makes the operator operate
+ // linewise
+ inputState.motion = 'expandToLine';
+ inputState.motionArgs = { linewise: true };
+ this.evalInput(cm, vim);
+ return;
+ } else {
+ // 2 different operators in a row doesn't make sense.
+ vim.inputState = new InputState();
+ }
+ }
+ inputState.operator = command.operator;
+ inputState.operatorArgs = copyArgs(command.operatorArgs);
+ if (vim.visualMode) {
+ // Operating on a selection in visual mode. We don't need a motion.
+ this.evalInput(cm, vim);
+ }
+ },
+ processOperatorMotion: function(cm, vim, command) {
+ var visualMode = vim.visualMode;
+ var operatorMotionArgs = copyArgs(command.operatorMotionArgs);
+ if (operatorMotionArgs) {
+ // Operator motions may have special behavior in visual mode.
+ if (visualMode && operatorMotionArgs.visualLine) {
+ vim.visualLine = true;
+ }
+ }
+ this.processOperator(cm, vim, command);
+ if (!visualMode) {
+ this.processMotion(cm, vim, command);
+ }
+ },
+ processAction: function(cm, vim, command) {
+ var inputState = vim.inputState;
+ var repeat = inputState.getRepeat();
+ var repeatIsExplicit = !!repeat;
+ var actionArgs = copyArgs(command.actionArgs) || {};
+ if (inputState.selectedCharacter) {
+ actionArgs.selectedCharacter = inputState.selectedCharacter;
+ }
+ // Actions may or may not have motions and operators. Do these first.
+ if (command.operator) {
+ this.processOperator(cm, vim, command);
+ }
+ if (command.motion) {
+ this.processMotion(cm, vim, command);
+ }
+ if (command.motion || command.operator) {
+ this.evalInput(cm, vim);
+ }
+ actionArgs.repeat = repeat || 1;
+ actionArgs.repeatIsExplicit = repeatIsExplicit;
+ actionArgs.registerName = inputState.registerName;
+ vim.inputState = new InputState();
+ vim.lastMotion = null;
+ if (command.isEdit) {
+ this.recordLastEdit(vim, inputState, command);
+ }
+ actions[command.action](cm, actionArgs, vim);
+ },
+ processSearch: function(cm, vim, command) {
+ if (!cm.getSearchCursor) {
+ // Search depends on SearchCursor.
+ return;
+ }
+ var forward = command.searchArgs.forward;
+ getSearchState(cm).setReversed(!forward);
+ var promptPrefix = (forward) ? '/' : '?';
+ var originalQuery = getSearchState(cm).getQuery();
+ var originalScrollPos = cm.getScrollInfo();
+ function handleQuery(query, ignoreCase, smartCase) {
+ try {
+ updateSearchQuery(cm, query, ignoreCase, smartCase);
+ } catch (e) {
+ showConfirm(cm, 'Invalid regex: ' + query);
+ return;
+ }
+ commandDispatcher.processMotion(cm, vim, {
+ type: 'motion',
+ motion: 'findNext',
+ motionArgs: { forward: true, toJumplist: command.searchArgs.toJumplist }
+ });
+ }
+ function onPromptClose(query) {
+ cm.scrollTo(originalScrollPos.left, originalScrollPos.top);
+ handleQuery(query, true /** ignoreCase */, true /** smartCase */);
+ }
+ function onPromptKeyUp(_e, query) {
+ var parsedQuery;
+ try {
+ parsedQuery = updateSearchQuery(cm, query,
+ true /** ignoreCase */, true /** smartCase */);
+ } catch (e) {
+ // Swallow bad regexes for incremental search.
+ }
+ if (parsedQuery) {
+ cm.scrollIntoView(findNext(cm, !forward, parsedQuery), 30);
+ } else {
+ clearSearchHighlight(cm);
+ cm.scrollTo(originalScrollPos.left, originalScrollPos.top);
+ }
+ }
+ function onPromptKeyDown(e, _query, close) {
+ var keyName = CodeMirror.keyName(e);
+ if (keyName == 'Esc' || keyName == 'Ctrl-C' || keyName == 'Ctrl-[') {
+ updateSearchQuery(cm, originalQuery);
+ clearSearchHighlight(cm);
+ cm.scrollTo(originalScrollPos.left, originalScrollPos.top);
+
+ CodeMirror.e_stop(e);
+ close();
+ cm.focus();
+ }
+ }
+ switch (command.searchArgs.querySrc) {
+ case 'prompt':
+ showPrompt(cm, {
+ onClose: onPromptClose,
+ prefix: promptPrefix,
+ desc: searchPromptDesc,
+ onKeyUp: onPromptKeyUp,
+ onKeyDown: onPromptKeyDown
+ });
+ break;
+ case 'wordUnderCursor':
+ var word = expandWordUnderCursor(cm, false /** inclusive */,
+ true /** forward */, false /** bigWord */,
+ true /** noSymbol */);
+ var isKeyword = true;
+ if (!word) {
+ word = expandWordUnderCursor(cm, false /** inclusive */,
+ true /** forward */, false /** bigWord */,
+ false /** noSymbol */);
+ isKeyword = false;
+ }
+ if (!word) {
+ return;
+ }
+ var query = cm.getLine(word.start.line).substring(word.start.ch,
+ word.end.ch);
+ if (isKeyword) {
+ query = '\\b' + query + '\\b';
+ } else {
+ query = escapeRegex(query);
+ }
+
+ // cachedCursor is used to save the old position of the cursor
+ // when * or # causes vim to seek for the nearest word and shift
+ // the cursor before entering the motion.
+ vimGlobalState.jumpList.cachedCursor = cm.getCursor();
+ cm.setCursor(word.start);
+
+ handleQuery(query, true /** ignoreCase */, false /** smartCase */);
+ break;
+ }
+ },
+ processEx: function(cm, vim, command) {
+ function onPromptClose(input) {
+ // Give the prompt some time to close so that if processCommand shows
+ // an error, the elements don't overlap.
+ exCommandDispatcher.processCommand(cm, input);
+ }
+ function onPromptKeyDown(e, _input, close) {
+ var keyName = CodeMirror.keyName(e);
+ if (keyName == 'Esc' || keyName == 'Ctrl-C' || keyName == 'Ctrl-[') {
+ CodeMirror.e_stop(e);
+ close();
+ cm.focus();
+ }
+ }
+ if (command.type == 'keyToEx') {
+ // Handle user defined Ex to Ex mappings
+ exCommandDispatcher.processCommand(cm, command.exArgs.input);
+ } else {
+ if (vim.visualMode) {
+ showPrompt(cm, { onClose: onPromptClose, prefix: ':', value: '\'<,\'>',
+ onKeyDown: onPromptKeyDown});
+ } else {
+ showPrompt(cm, { onClose: onPromptClose, prefix: ':',
+ onKeyDown: onPromptKeyDown});
+ }
+ }
+ },
+ evalInput: function(cm, vim) {
+ // If the motion comand is set, execute both the operator and motion.
+ // Otherwise return.
+ var inputState = vim.inputState;
+ var motion = inputState.motion;
+ var motionArgs = inputState.motionArgs || {};
+ var operator = inputState.operator;
+ var operatorArgs = inputState.operatorArgs || {};
+ var registerName = inputState.registerName;
+ var selectionEnd = cm.getCursor('head');
+ var selectionStart = cm.getCursor('anchor');
+ // The difference between cur and selection cursors are that cur is
+ // being operated on and ignores that there is a selection.
+ var curStart = copyCursor(selectionEnd);
+ var curOriginal = copyCursor(curStart);
+ var curEnd;
+ var repeat;
+ if (operator) {
+ this.recordLastEdit(vim, inputState);
+ }
+ if (inputState.repeatOverride !== undefined) {
+ // If repeatOverride is specified, that takes precedence over the
+ // input state's repeat. Used by Ex mode and can be user defined.
+ repeat = inputState.repeatOverride;
+ } else {
+ repeat = inputState.getRepeat();
+ }
+ if (repeat > 0 && motionArgs.explicitRepeat) {
+ motionArgs.repeatIsExplicit = true;
+ } else if (motionArgs.noRepeat ||
+ (!motionArgs.explicitRepeat && repeat === 0)) {
+ repeat = 1;
+ motionArgs.repeatIsExplicit = false;
+ }
+ if (inputState.selectedCharacter) {
+ // If there is a character input, stick it in all of the arg arrays.
+ motionArgs.selectedCharacter = operatorArgs.selectedCharacter =
+ inputState.selectedCharacter;
+ }
+ motionArgs.repeat = repeat;
+ vim.inputState = new InputState();
+ if (motion) {
+ var motionResult = motions[motion](cm, motionArgs, vim);
+ vim.lastMotion = motions[motion];
+ if (!motionResult) {
+ return;
+ }
+ if (motionArgs.toJumplist) {
+ var jumpList = vimGlobalState.jumpList;
+ // if the current motion is # or *, use cachedCursor
+ var cachedCursor = jumpList.cachedCursor;
+ if (cachedCursor) {
+ recordJumpPosition(cm, cachedCursor, motionResult);
+ delete jumpList.cachedCursor;
+ } else {
+ recordJumpPosition(cm, curOriginal, motionResult);
+ }
+ }
+ if (motionResult instanceof Array) {
+ curStart = motionResult[0];
+ curEnd = motionResult[1];
+ } else {
+ curEnd = motionResult;
+ }
+ // TODO: Handle null returns from motion commands better.
+ if (!curEnd) {
+ curEnd = { ch: curStart.ch, line: curStart.line };
+ }
+ if (vim.visualMode) {
+ // Check if the selection crossed over itself. Will need to shift
+ // the start point if that happened.
+ if (cursorIsBefore(selectionStart, selectionEnd) &&
+ (cursorEqual(selectionStart, curEnd) ||
+ cursorIsBefore(curEnd, selectionStart))) {
+ // The end of the selection has moved from after the start to
+ // before the start. We will shift the start right by 1.
+ selectionStart.ch += 1;
+ } else if (cursorIsBefore(selectionEnd, selectionStart) &&
+ (cursorEqual(selectionStart, curEnd) ||
+ cursorIsBefore(selectionStart, curEnd))) {
+ // The opposite happened. We will shift the start left by 1.
+ selectionStart.ch -= 1;
+ }
+ selectionEnd = curEnd;
+ if (vim.visualLine) {
+ if (cursorIsBefore(selectionStart, selectionEnd)) {
+ selectionStart.ch = 0;
+
+ var lastLine = cm.lastLine();
+ if (selectionEnd.line > lastLine) {
+ selectionEnd.line = lastLine;
+ }
+ selectionEnd.ch = lineLength(cm, selectionEnd.line);
+ } else {
+ selectionEnd.ch = 0;
+ selectionStart.ch = lineLength(cm, selectionStart.line);
+ }
+ }
+ cm.setSelection(selectionStart, selectionEnd);
+ updateMark(cm, vim, '<',
+ cursorIsBefore(selectionStart, selectionEnd) ? selectionStart
+ : selectionEnd);
+ updateMark(cm, vim, '>',
+ cursorIsBefore(selectionStart, selectionEnd) ? selectionEnd
+ : selectionStart);
+ } else if (!operator) {
+ curEnd = clipCursorToContent(cm, curEnd);
+ cm.setCursor(curEnd.line, curEnd.ch);
+ }
+ }
+
+ if (operator) {
+ var inverted = false;
+ vim.lastMotion = null;
+ operatorArgs.repeat = repeat; // Indent in visual mode needs this.
+ if (vim.visualMode) {
+ curStart = selectionStart;
+ curEnd = selectionEnd;
+ motionArgs.inclusive = true;
+ }
+ // Swap start and end if motion was backward.
+ if (cursorIsBefore(curEnd, curStart)) {
+ var tmp = curStart;
+ curStart = curEnd;
+ curEnd = tmp;
+ inverted = true;
+ }
+ if (motionArgs.inclusive && !(vim.visualMode && inverted)) {
+ // Move the selection end one to the right to include the last
+ // character.
+ curEnd.ch++;
+ }
+ var linewise = motionArgs.linewise ||
+ (vim.visualMode && vim.visualLine);
+ if (linewise) {
+ // Expand selection to entire line.
+ expandSelectionToLine(cm, curStart, curEnd);
+ } else if (motionArgs.forward) {
+ // Clip to trailing newlines only if the motion goes forward.
+ clipToLine(cm, curStart, curEnd);
+ }
+ operatorArgs.registerName = registerName;
+ // Keep track of linewise as it affects how paste and change behave.
+ operatorArgs.linewise = linewise;
+ operators[operator](cm, operatorArgs, vim, curStart,
+ curEnd, curOriginal);
+ if (vim.visualMode) {
+ exitVisualMode(cm);
+ }
+ }
+ },
+ recordLastEdit: function(vim, inputState, actionCommand) {
+ var macroModeState = vimGlobalState.macroModeState;
+ if (macroModeState.inReplay) { return; }
+ vim.lastEditInputState = inputState;
+ vim.lastEditActionCommand = actionCommand;
+ macroModeState.lastInsertModeChanges.changes = [];
+ macroModeState.lastInsertModeChanges.expectCursorActivityForChange = false;
+ }
+ };
+
+ /**
+ * typedef {Object{line:number,ch:number}} Cursor An object containing the
+ * position of the cursor.
+ */
+ // All of the functions below return Cursor objects.
+ var motions = {
+ moveToTopLine: function(cm, motionArgs) {
+ var line = getUserVisibleLines(cm).top + motionArgs.repeat -1;
+ return { line: line, ch: findFirstNonWhiteSpaceCharacter(cm.getLine(line)) };
+ },
+ moveToMiddleLine: function(cm) {
+ var range = getUserVisibleLines(cm);
+ var line = Math.floor((range.top + range.bottom) * 0.5);
+ return { line: line, ch: findFirstNonWhiteSpaceCharacter(cm.getLine(line)) };
+ },
+ moveToBottomLine: function(cm, motionArgs) {
+ var line = getUserVisibleLines(cm).bottom - motionArgs.repeat +1;
+ return { line: line, ch: findFirstNonWhiteSpaceCharacter(cm.getLine(line)) };
+ },
+ expandToLine: function(cm, motionArgs) {
+ // Expands forward to end of line, and then to next line if repeat is
+ // >1. Does not handle backward motion!
+ var cur = cm.getCursor();
+ return { line: cur.line + motionArgs.repeat - 1, ch: Infinity };
+ },
+ findNext: function(cm, motionArgs) {
+ var state = getSearchState(cm);
+ var query = state.getQuery();
+ if (!query) {
+ return;
+ }
+ var prev = !motionArgs.forward;
+ // If search is initiated with ? instead of /, negate direction.
+ prev = (state.isReversed()) ? !prev : prev;
+ highlightSearchMatches(cm, query);
+ return findNext(cm, prev/** prev */, query, motionArgs.repeat);
+ },
+ goToMark: function(_cm, motionArgs, vim) {
+ var mark = vim.marks[motionArgs.selectedCharacter];
+ if (mark) {
+ return mark.find();
+ }
+ return null;
+ },
+ jumpToMark: function(cm, motionArgs, vim) {
+ var best = cm.getCursor();
+ for (var i = 0; i < motionArgs.repeat; i++) {
+ var cursor = best;
+ for (var key in vim.marks) {
+ if (!isLowerCase(key)) {
+ continue;
+ }
+ var mark = vim.marks[key].find();
+ var isWrongDirection = (motionArgs.forward) ?
+ cursorIsBefore(mark, cursor) : cursorIsBefore(cursor, mark);
+
+ if (isWrongDirection) {
+ continue;
+ }
+ if (motionArgs.linewise && (mark.line == cursor.line)) {
+ continue;
+ }
+
+ var equal = cursorEqual(cursor, best);
+ var between = (motionArgs.forward) ?
+ cusrorIsBetween(cursor, mark, best) :
+ cusrorIsBetween(best, mark, cursor);
+
+ if (equal || between) {
+ best = mark;
+ }
+ }
+ }
+
+ if (motionArgs.linewise) {
+ // Vim places the cursor on the first non-whitespace character of
+ // the line if there is one, else it places the cursor at the end
+ // of the line, regardless of whether a mark was found.
+ best.ch = findFirstNonWhiteSpaceCharacter(cm.getLine(best.line));
+ }
+ return best;
+ },
+ moveByCharacters: function(cm, motionArgs) {
+ var cur = cm.getCursor();
+ var repeat = motionArgs.repeat;
+ var ch = motionArgs.forward ? cur.ch + repeat : cur.ch - repeat;
+ return { line: cur.line, ch: ch };
+ },
+ moveByLines: function(cm, motionArgs, vim) {
+ var cur = cm.getCursor();
+ var endCh = cur.ch;
+ // Depending what our last motion was, we may want to do different
+ // things. If our last motion was moving vertically, we want to
+ // preserve the HPos from our last horizontal move. If our last motion
+ // was going to the end of a line, moving vertically we should go to
+ // the end of the line, etc.
+ switch (vim.lastMotion) {
+ case this.moveByLines:
+ case this.moveByDisplayLines:
+ case this.moveByScroll:
+ case this.moveToColumn:
+ case this.moveToEol:
+ endCh = vim.lastHPos;
+ break;
+ default:
+ vim.lastHPos = endCh;
+ }
+ var repeat = motionArgs.repeat+(motionArgs.repeatOffset||0);
+ var line = motionArgs.forward ? cur.line + repeat : cur.line - repeat;
+ var first = cm.firstLine();
+ var last = cm.lastLine();
+ // Vim cancels linewise motions that start on an edge and move beyond
+ // that edge. It does not cancel motions that do not start on an edge.
+ if ((line < first && cur.line == first) ||
+ (line > last && cur.line == last)) {
+ return;
+ }
+ if(motionArgs.toFirstChar){
+ endCh=findFirstNonWhiteSpaceCharacter(cm.getLine(line));
+ vim.lastHPos = endCh;
+ }
+ vim.lastHSPos = cm.charCoords({line:line, ch:endCh},'div').left;
+ return { line: line, ch: endCh };
+ },
+ moveByDisplayLines: function(cm, motionArgs, vim) {
+ var cur = cm.getCursor();
+ switch (vim.lastMotion) {
+ case this.moveByDisplayLines:
+ case this.moveByScroll:
+ case this.moveByLines:
+ case this.moveToColumn:
+ case this.moveToEol:
+ break;
+ default:
+ vim.lastHSPos = cm.charCoords(cur,'div').left;
+ }
+ var repeat = motionArgs.repeat;
+ var res=cm.findPosV(cur,(motionArgs.forward ? repeat : -repeat),'line',vim.lastHSPos);
+ if (res.hitSide) {
+ if (motionArgs.forward) {
+ var lastCharCoords = cm.charCoords(res, 'div');
+ var goalCoords = { top: lastCharCoords.top + 8, left: vim.lastHSPos };
+ var res = cm.coordsChar(goalCoords, 'div');
+ } else {
+ var resCoords = cm.charCoords({ line: cm.firstLine(), ch: 0}, 'div');
+ resCoords.left = vim.lastHSPos;
+ res = cm.coordsChar(resCoords, 'div');
+ }
+ }
+ vim.lastHPos = res.ch;
+ return res;
+ },
+ moveByPage: function(cm, motionArgs) {
+ // CodeMirror only exposes functions that move the cursor page down, so
+ // doing this bad hack to move the cursor and move it back. evalInput
+ // will move the cursor to where it should be in the end.
+ var curStart = cm.getCursor();
+ var repeat = motionArgs.repeat;
+ cm.moveV((motionArgs.forward ? repeat : -repeat), 'page');
+ var curEnd = cm.getCursor();
+ cm.setCursor(curStart);
+ return curEnd;
+ },
+ moveByParagraph: function(cm, motionArgs) {
+ var line = cm.getCursor().line;
+ var repeat = motionArgs.repeat;
+ var inc = motionArgs.forward ? 1 : -1;
+ for (var i = 0; i < repeat; i++) {
+ if ((!motionArgs.forward && line === cm.firstLine() ) ||
+ (motionArgs.forward && line == cm.lastLine())) {
+ break;
+ }
+ line += inc;
+ while (line !== cm.firstLine() && line != cm.lastLine() && cm.getLine(line)) {
+ line += inc;
+ }
+ }
+ return { line: line, ch: 0 };
+ },
+ moveByScroll: function(cm, motionArgs, vim) {
+ var scrollbox = cm.getScrollInfo();
+ var curEnd = null;
+ var repeat = motionArgs.repeat;
+ if (!repeat) {
+ repeat = scrollbox.clientHeight / (2 * cm.defaultTextHeight());
+ }
+ var orig = cm.charCoords(cm.getCursor(), 'local');
+ motionArgs.repeat = repeat;
+ var curEnd = motions.moveByDisplayLines(cm, motionArgs, vim);
+ if (!curEnd) {
+ return null;
+ }
+ var dest = cm.charCoords(curEnd, 'local');
+ cm.scrollTo(null, scrollbox.top + dest.top - orig.top);
+ return curEnd;
+ },
+ moveByWords: function(cm, motionArgs) {
+ return moveToWord(cm, motionArgs.repeat, !!motionArgs.forward,
+ !!motionArgs.wordEnd, !!motionArgs.bigWord);
+ },
+ moveTillCharacter: function(cm, motionArgs) {
+ var repeat = motionArgs.repeat;
+ var curEnd = moveToCharacter(cm, repeat, motionArgs.forward,
+ motionArgs.selectedCharacter);
+ var increment = motionArgs.forward ? -1 : 1;
+ recordLastCharacterSearch(increment, motionArgs);
+ if(!curEnd)return cm.getCursor();
+ curEnd.ch += increment;
+ return curEnd;
+ },
+ moveToCharacter: function(cm, motionArgs) {
+ var repeat = motionArgs.repeat;
+ recordLastCharacterSearch(0, motionArgs);
+ return moveToCharacter(cm, repeat, motionArgs.forward,
+ motionArgs.selectedCharacter) || cm.getCursor();
+ },
+ moveToSymbol: function(cm, motionArgs) {
+ var repeat = motionArgs.repeat;
+ return findSymbol(cm, repeat, motionArgs.forward,
+ motionArgs.selectedCharacter) || cm.getCursor();
+ },
+ moveToColumn: function(cm, motionArgs, vim) {
+ var repeat = motionArgs.repeat;
+ // repeat is equivalent to which column we want to move to!
+ vim.lastHPos = repeat - 1;
+ vim.lastHSPos = cm.charCoords(cm.getCursor(),'div').left;
+ return moveToColumn(cm, repeat);
+ },
+ moveToEol: function(cm, motionArgs, vim) {
+ var cur = cm.getCursor();
+ vim.lastHPos = Infinity;
+ var retval={ line: cur.line + motionArgs.repeat - 1, ch: Infinity };
+ var end=cm.clipPos(retval);
+ end.ch--;
+ vim.lastHSPos = cm.charCoords(end,'div').left;
+ return retval;
+ },
+ moveToFirstNonWhiteSpaceCharacter: function(cm) {
+ // Go to the start of the line where the text begins, or the end for
+ // whitespace-only lines
+ var cursor = cm.getCursor();
+ return { line: cursor.line,
+ ch: findFirstNonWhiteSpaceCharacter(cm.getLine(cursor.line)) };
+ },
+ moveToMatchedSymbol: function(cm) {
+ var cursor = cm.getCursor();
+ var line = cursor.line;
+ var ch = cursor.ch;
+ var lineText = cm.getLine(line);
+ var symbol;
+ var startContext = cm.getTokenAt(cursor).type;
+ var startCtxLevel = getContextLevel(startContext);
+ do {
+ symbol = lineText.charAt(ch++);
+ if (symbol && isMatchableSymbol(symbol)) {
+ var endContext = cm.getTokenAt({line:line, ch:ch}).type;
+ var endCtxLevel = getContextLevel(endContext);
+ if (startCtxLevel >= endCtxLevel) {
+ break;
+ }
+ }
+ } while (symbol);
+ if (symbol) {
+ return findMatchedSymbol(cm, {line:line, ch:ch-1}, symbol);
+ } else {
+ return cursor;
+ }
+ },
+ moveToStartOfLine: function(cm) {
+ var cursor = cm.getCursor();
+ return { line: cursor.line, ch: 0 };
+ },
+ moveToLineOrEdgeOfDocument: function(cm, motionArgs) {
+ var lineNum = motionArgs.forward ? cm.lastLine() : cm.firstLine();
+ if (motionArgs.repeatIsExplicit) {
+ lineNum = motionArgs.repeat - cm.getOption('firstLineNumber');
+ }
+ return { line: lineNum,
+ ch: findFirstNonWhiteSpaceCharacter(cm.getLine(lineNum)) };
+ },
+ textObjectManipulation: function(cm, motionArgs) {
+ var character = motionArgs.selectedCharacter;
+ // Inclusive is the difference between a and i
+ // TODO: Instead of using the additional text object map to perform text
+ // object operations, merge the map into the defaultKeyMap and use
+ // motionArgs to define behavior. Define separate entries for 'aw',
+ // 'iw', 'a[', 'i[', etc.
+ var inclusive = !motionArgs.textObjectInner;
+ if (!textObjects[character]) {
+ // No text object defined for this, don't move.
+ return null;
+ }
+ var tmp = textObjects[character](cm, inclusive);
+ var start = tmp.start;
+ var end = tmp.end;
+ return [start, end];
+ },
+ repeatLastCharacterSearch: function(cm, motionArgs) {
+ var lastSearch = vimGlobalState.lastChararacterSearch;
+ var repeat = motionArgs.repeat;
+ var forward = motionArgs.forward === lastSearch.forward;
+ var increment = (lastSearch.increment ? 1 : 0) * (forward ? -1 : 1);
+ cm.moveH(-increment, 'char');
+ motionArgs.inclusive = forward ? true : false;
+ var curEnd = moveToCharacter(cm, repeat, forward, lastSearch.selectedCharacter);
+ if (!curEnd) {
+ cm.moveH(increment, 'char');
+ return cm.getCursor();
+ }
+ curEnd.ch += increment;
+ return curEnd;
+ }
+ };
+
+ var operators = {
+ change: function(cm, operatorArgs, _vim, curStart, curEnd) {
+ vimGlobalState.registerController.pushText(
+ operatorArgs.registerName, 'change', cm.getRange(curStart, curEnd),
+ operatorArgs.linewise);
+ if (operatorArgs.linewise) {
+ // Push the next line back down, if there is a next line.
+ var replacement = curEnd.line > cm.lastLine() ? '' : '\n';
+ cm.replaceRange(replacement, curStart, curEnd);
+ cm.indentLine(curStart.line, 'smart');
+ // null ch so setCursor moves to end of line.
+ curStart.ch = null;
+ } else {
+ // Exclude trailing whitespace if the range is not all whitespace.
+ var text = cm.getRange(curStart, curEnd);
+ if (!isWhiteSpaceString(text)) {
+ var match = (/\s+$/).exec(text);
+ if (match) {
+ curEnd = offsetCursor(curEnd, 0, - match[0].length);
+ }
+ }
+ cm.replaceRange('', curStart, curEnd);
+ }
+ actions.enterInsertMode(cm, {}, cm.state.vim);
+ cm.setCursor(curStart);
+ },
+ // delete is a javascript keyword.
+ 'delete': function(cm, operatorArgs, _vim, curStart, curEnd) {
+ // If the ending line is past the last line, inclusive, instead of
+ // including the trailing \n, include the \n before the starting line
+ if (operatorArgs.linewise &&
+ curEnd.line > cm.lastLine() && curStart.line > cm.firstLine()) {
+ curStart.line--;
+ curStart.ch = lineLength(cm, curStart.line);
+ }
+ vimGlobalState.registerController.pushText(
+ operatorArgs.registerName, 'delete', cm.getRange(curStart, curEnd),
+ operatorArgs.linewise);
+ cm.replaceRange('', curStart, curEnd);
+ if (operatorArgs.linewise) {
+ cm.setCursor(motions.moveToFirstNonWhiteSpaceCharacter(cm));
+ } else {
+ cm.setCursor(curStart);
+ }
+ },
+ indent: function(cm, operatorArgs, vim, curStart, curEnd) {
+ var startLine = curStart.line;
+ var endLine = curEnd.line;
+ // In visual mode, n> shifts the selection right n times, instead of
+ // shifting n lines right once.
+ var repeat = (vim.visualMode) ? operatorArgs.repeat : 1;
+ if (operatorArgs.linewise) {
+ // The only way to delete a newline is to delete until the start of
+ // the next line, so in linewise mode evalInput will include the next
+ // line. We don't want this in indent, so we go back a line.
+ endLine--;
+ }
+ for (var i = startLine; i <= endLine; i++) {
+ for (var j = 0; j < repeat; j++) {
+ cm.indentLine(i, operatorArgs.indentRight);
+ }
+ }
+ cm.setCursor(curStart);
+ cm.setCursor(motions.moveToFirstNonWhiteSpaceCharacter(cm));
+ },
+ swapcase: function(cm, operatorArgs, _vim, curStart, curEnd, curOriginal) {
+ var toSwap = cm.getRange(curStart, curEnd);
+ var swapped = '';
+ for (var i = 0; i < toSwap.length; i++) {
+ var character = toSwap.charAt(i);
+ swapped += isUpperCase(character) ? character.toLowerCase() :
+ character.toUpperCase();
+ }
+ cm.replaceRange(swapped, curStart, curEnd);
+ if (!operatorArgs.shouldMoveCursor) {
+ cm.setCursor(curOriginal);
+ }
+ },
+ yank: function(cm, operatorArgs, _vim, curStart, curEnd, curOriginal) {
+ vimGlobalState.registerController.pushText(
+ operatorArgs.registerName, 'yank',
+ cm.getRange(curStart, curEnd), operatorArgs.linewise);
+ cm.setCursor(curOriginal);
+ }
+ };
+
+ var actions = {
+ jumpListWalk: function(cm, actionArgs, vim) {
+ if (vim.visualMode) {
+ return;
+ }
+ var repeat = actionArgs.repeat;
+ var forward = actionArgs.forward;
+ var jumpList = vimGlobalState.jumpList;
+
+ var mark = jumpList.move(cm, forward ? repeat : -repeat);
+ var markPos = mark ? mark.find() : undefined;
+ markPos = markPos ? markPos : cm.getCursor();
+ cm.setCursor(markPos);
+ },
+ scrollToCursor: function(cm, actionArgs) {
+ var lineNum = cm.getCursor().line;
+ var charCoords = cm.charCoords({line: lineNum, ch: 0}, 'local');
+ var height = cm.getScrollInfo().clientHeight;
+ var y = charCoords.top;
+ var lineHeight = charCoords.bottom - y;
+ switch (actionArgs.position) {
+ case 'center': y = y - (height / 2) + lineHeight;
+ break;
+ case 'bottom': y = y - height + lineHeight*1.4;
+ break;
+ case 'top': y = y + lineHeight*0.4;
+ break;
+ }
+ cm.scrollTo(null, y);
+ },
+ replayMacro: function(cm, actionArgs) {
+ var registerName = actionArgs.selectedCharacter;
+ var repeat = actionArgs.repeat;
+ var macroModeState = vimGlobalState.macroModeState;
+ if (registerName == '@') {
+ registerName = macroModeState.latestRegister;
+ }
+ var keyBuffer = parseRegisterToKeyBuffer(macroModeState, registerName);
+ while(repeat--){
+ executeMacroKeyBuffer(cm, macroModeState, keyBuffer);
+ }
+ },
+ exitMacroRecordMode: function() {
+ var macroModeState = vimGlobalState.macroModeState;
+ macroModeState.toggle();
+ parseKeyBufferToRegister(macroModeState.latestRegister,
+ macroModeState.macroKeyBuffer);
+ },
+ enterMacroRecordMode: function(cm, actionArgs) {
+ var macroModeState = vimGlobalState.macroModeState;
+ var registerName = actionArgs.selectedCharacter;
+ macroModeState.toggle(cm, registerName);
+ emptyMacroKeyBuffer(macroModeState);
+ },
+ enterInsertMode: function(cm, actionArgs, vim) {
+ if (cm.getOption('readOnly')) { return; }
+ vim.insertMode = true;
+ vim.insertModeRepeat = actionArgs && actionArgs.repeat || 1;
+ var insertAt = (actionArgs) ? actionArgs.insertAt : null;
+ if (insertAt == 'eol') {
+ var cursor = cm.getCursor();
+ cursor = { line: cursor.line, ch: lineLength(cm, cursor.line) };
+ cm.setCursor(cursor);
+ } else if (insertAt == 'charAfter') {
+ cm.setCursor(offsetCursor(cm.getCursor(), 0, 1));
+ } else if (insertAt == 'firstNonBlank') {
+ cm.setCursor(motions.moveToFirstNonWhiteSpaceCharacter(cm));
+ }
+ cm.setOption('keyMap', 'vim-insert');
+ if (actionArgs && actionArgs.replace) {
+ // Handle Replace-mode as a special case of insert mode.
+ cm.toggleOverwrite(true);
+ cm.setOption('keyMap', 'vim-replace');
+ CodeMirror.signal(cm, "vim-mode-change", {mode: "replace"});
+ } else {
+ cm.setOption('keyMap', 'vim-insert');
+ CodeMirror.signal(cm, "vim-mode-change", {mode: "insert"});
+ }
+ if (!vimGlobalState.macroModeState.inReplay) {
+ // Only record if not replaying.
+ cm.on('change', onChange);
+ cm.on('cursorActivity', onCursorActivity);
+ CodeMirror.on(cm.getInputField(), 'keydown', onKeyEventTargetKeyDown);
+ }
+ },
+ toggleVisualMode: function(cm, actionArgs, vim) {
+ var repeat = actionArgs.repeat;
+ var curStart = cm.getCursor();
+ var curEnd;
+ // TODO: The repeat should actually select number of characters/lines
+ // equal to the repeat times the size of the previous visual
+ // operation.
+ if (!vim.visualMode) {
+ cm.on('mousedown', exitVisualMode);
+ vim.visualMode = true;
+ vim.visualLine = !!actionArgs.linewise;
+ if (vim.visualLine) {
+ curStart.ch = 0;
+ curEnd = clipCursorToContent(cm, {
+ line: curStart.line + repeat - 1,
+ ch: lineLength(cm, curStart.line)
+ }, true /** includeLineBreak */);
+ } else {
+ curEnd = clipCursorToContent(cm, {
+ line: curStart.line,
+ ch: curStart.ch + repeat
+ }, true /** includeLineBreak */);
+ }
+ // Make the initial selection.
+ if (!actionArgs.repeatIsExplicit && !vim.visualLine) {
+ // This is a strange case. Here the implicit repeat is 1. The
+ // following commands lets the cursor hover over the 1 character
+ // selection.
+ cm.setCursor(curEnd);
+ cm.setSelection(curEnd, curStart);
+ } else {
+ cm.setSelection(curStart, curEnd);
+ }
+ CodeMirror.signal(cm, "vim-mode-change", {mode: "visual", subMode: vim.visualLine ? "linewise" : ""});
+ } else {
+ curStart = cm.getCursor('anchor');
+ curEnd = cm.getCursor('head');
+ if (!vim.visualLine && actionArgs.linewise) {
+ // Shift-V pressed in characterwise visual mode. Switch to linewise
+ // visual mode instead of exiting visual mode.
+ vim.visualLine = true;
+ curStart.ch = cursorIsBefore(curStart, curEnd) ? 0 :
+ lineLength(cm, curStart.line);
+ curEnd.ch = cursorIsBefore(curStart, curEnd) ?
+ lineLength(cm, curEnd.line) : 0;
+ cm.setSelection(curStart, curEnd);
+ CodeMirror.signal(cm, "vim-mode-change", {mode: "visual", subMode: "linewise"});
+ } else if (vim.visualLine && !actionArgs.linewise) {
+ // v pressed in linewise visual mode. Switch to characterwise visual
+ // mode instead of exiting visual mode.
+ vim.visualLine = false;
+ CodeMirror.signal(cm, "vim-mode-change", {mode: "visual"});
+ } else {
+ exitVisualMode(cm);
+ }
+ }
+ updateMark(cm, vim, '<', cursorIsBefore(curStart, curEnd) ? curStart
+ : curEnd);
+ updateMark(cm, vim, '>', cursorIsBefore(curStart, curEnd) ? curEnd
+ : curStart);
+ },
+ joinLines: function(cm, actionArgs, vim) {
+ var curStart, curEnd;
+ if (vim.visualMode) {
+ curStart = cm.getCursor('anchor');
+ curEnd = cm.getCursor('head');
+ curEnd.ch = lineLength(cm, curEnd.line) - 1;
+ } else {
+ // Repeat is the number of lines to join. Minimum 2 lines.
+ var repeat = Math.max(actionArgs.repeat, 2);
+ curStart = cm.getCursor();
+ curEnd = clipCursorToContent(cm, { line: curStart.line + repeat - 1,
+ ch: Infinity });
+ }
+ var finalCh = 0;
+ cm.operation(function() {
+ for (var i = curStart.line; i < curEnd.line; i++) {
+ finalCh = lineLength(cm, curStart.line);
+ var tmp = { line: curStart.line + 1,
+ ch: lineLength(cm, curStart.line + 1) };
+ var text = cm.getRange(curStart, tmp);
+ text = text.replace(/\n\s*/g, ' ');
+ cm.replaceRange(text, curStart, tmp);
+ }
+ var curFinalPos = { line: curStart.line, ch: finalCh };
+ cm.setCursor(curFinalPos);
+ });
+ },
+ newLineAndEnterInsertMode: function(cm, actionArgs, vim) {
+ vim.insertMode = true;
+ var insertAt = cm.getCursor();
+ if (insertAt.line === cm.firstLine() && !actionArgs.after) {
+ // Special case for inserting newline before start of document.
+ cm.replaceRange('\n', { line: cm.firstLine(), ch: 0 });
+ cm.setCursor(cm.firstLine(), 0);
+ } else {
+ insertAt.line = (actionArgs.after) ? insertAt.line :
+ insertAt.line - 1;
+ insertAt.ch = lineLength(cm, insertAt.line);
+ cm.setCursor(insertAt);
+ var newlineFn = CodeMirror.commands.newlineAndIndentContinueComment ||
+ CodeMirror.commands.newlineAndIndent;
+ newlineFn(cm);
+ }
+ this.enterInsertMode(cm, { repeat: actionArgs.repeat }, vim);
+ },
+ paste: function(cm, actionArgs) {
+ var cur = cm.getCursor();
+ var register = vimGlobalState.registerController.getRegister(
+ actionArgs.registerName);
+ if (!register.text) {
+ return;
+ }
+ for (var text = '', i = 0; i < actionArgs.repeat; i++) {
+ text += register.text;
+ }
+ var linewise = register.linewise;
+ if (linewise) {
+ if (actionArgs.after) {
+ // Move the newline at the end to the start instead, and paste just
+ // before the newline character of the line we are on right now.
+ text = '\n' + text.slice(0, text.length - 1);
+ cur.ch = lineLength(cm, cur.line);
+ } else {
+ cur.ch = 0;
+ }
+ } else {
+ cur.ch += actionArgs.after ? 1 : 0;
+ }
+ cm.replaceRange(text, cur);
+ // Now fine tune the cursor to where we want it.
+ var curPosFinal;
+ var idx;
+ if (linewise && actionArgs.after) {
+ curPosFinal = { line: cur.line + 1,
+ ch: findFirstNonWhiteSpaceCharacter(cm.getLine(cur.line + 1)) };
+ } else if (linewise && !actionArgs.after) {
+ curPosFinal = { line: cur.line,
+ ch: findFirstNonWhiteSpaceCharacter(cm.getLine(cur.line)) };
+ } else if (!linewise && actionArgs.after) {
+ idx = cm.indexFromPos(cur);
+ curPosFinal = cm.posFromIndex(idx + text.length - 1);
+ } else {
+ idx = cm.indexFromPos(cur);
+ curPosFinal = cm.posFromIndex(idx + text.length);
+ }
+ cm.setCursor(curPosFinal);
+ },
+ undo: function(cm, actionArgs) {
+ cm.operation(function() {
+ repeatFn(cm, CodeMirror.commands.undo, actionArgs.repeat)();
+ cm.setCursor(cm.getCursor('anchor'));
+ });
+ },
+ redo: function(cm, actionArgs) {
+ repeatFn(cm, CodeMirror.commands.redo, actionArgs.repeat)();
+ },
+ setRegister: function(_cm, actionArgs, vim) {
+ vim.inputState.registerName = actionArgs.selectedCharacter;
+ },
+ setMark: function(cm, actionArgs, vim) {
+ var markName = actionArgs.selectedCharacter;
+ updateMark(cm, vim, markName, cm.getCursor());
+ },
+ replace: function(cm, actionArgs, vim) {
+ var replaceWith = actionArgs.selectedCharacter;
+ var curStart = cm.getCursor();
+ var replaceTo;
+ var curEnd;
+ if(vim.visualMode){
+ curStart=cm.getCursor('start');
+ curEnd=cm.getCursor('end');
+ // workaround to catch the character under the cursor
+ // existing workaround doesn't cover actions
+ curEnd=cm.clipPos({line: curEnd.line, ch: curEnd.ch+1});
+ }else{
+ var line = cm.getLine(curStart.line);
+ replaceTo = curStart.ch + actionArgs.repeat;
+ if (replaceTo > line.length) {
+ replaceTo=line.length;
+ }
+ curEnd = { line: curStart.line, ch: replaceTo };
+ }
+ if(replaceWith=='\n'){
+ if(!vim.visualMode) cm.replaceRange('', curStart, curEnd);
+ // special case, where vim help says to replace by just one line-break
+ (CodeMirror.commands.newlineAndIndentContinueComment || CodeMirror.commands.newlineAndIndent)(cm);
+ }else {
+ var replaceWithStr=cm.getRange(curStart, curEnd);
+ //replace all characters in range by selected, but keep linebreaks
+ replaceWithStr=replaceWithStr.replace(/[^\n]/g,replaceWith);
+ cm.replaceRange(replaceWithStr, curStart, curEnd);
+ if(vim.visualMode){
+ cm.setCursor(curStart);
+ exitVisualMode(cm);
+ }else{
+ cm.setCursor(offsetCursor(curEnd, 0, -1));
+ }
+ }
+ },
+ incrementNumberToken: function(cm, actionArgs) {
+ var cur = cm.getCursor();
+ var lineStr = cm.getLine(cur.line);
+ var re = /-?\d+/g;
+ var match;
+ var start;
+ var end;
+ var numberStr;
+ var token;
+ while ((match = re.exec(lineStr)) !== null) {
+ token = match[0];
+ start = match.index;
+ end = start + token.length;
+ if(cur.ch < end)break;
+ }
+ if(!actionArgs.backtrack && (end <= cur.ch))return;
+ if (token) {
+ var increment = actionArgs.increase ? 1 : -1;
+ var number = parseInt(token) + (increment * actionArgs.repeat);
+ var from = {ch:start, line:cur.line};
+ var to = {ch:end, line:cur.line};
+ numberStr = number.toString();
+ cm.replaceRange(numberStr, from, to);
+ } else {
+ return;
+ }
+ cm.setCursor({line: cur.line, ch: start + numberStr.length - 1});
+ },
+ repeatLastEdit: function(cm, actionArgs, vim) {
+ var lastEditInputState = vim.lastEditInputState;
+ if (!lastEditInputState) { return; }
+ var repeat = actionArgs.repeat;
+ if (repeat && actionArgs.repeatIsExplicit) {
+ vim.lastEditInputState.repeatOverride = repeat;
+ } else {
+ repeat = vim.lastEditInputState.repeatOverride || repeat;
+ }
+ repeatLastEdit(cm, vim, repeat, false /** repeatForInsert */);
+ }
+ };
+
+ var textObjects = {
+ // TODO: lots of possible exceptions that can be thrown here. Try da(
+ // outside of a () block.
+ // TODO: implement text objects for the reverse like }. Should just be
+ // an additional mapping after moving to the defaultKeyMap.
+ 'w': function(cm, inclusive) {
+ return expandWordUnderCursor(cm, inclusive, true /** forward */,
+ false /** bigWord */);
+ },
+ 'W': function(cm, inclusive) {
+ return expandWordUnderCursor(cm, inclusive,
+ true /** forward */, true /** bigWord */);
+ },
+ '{': function(cm, inclusive) {
+ return selectCompanionObject(cm, '}', inclusive);
+ },
+ '(': function(cm, inclusive) {
+ return selectCompanionObject(cm, ')', inclusive);
+ },
+ '[': function(cm, inclusive) {
+ return selectCompanionObject(cm, ']', inclusive);
+ },
+ '\'': function(cm, inclusive) {
+ return findBeginningAndEnd(cm, "'", inclusive);
+ },
+ '"': function(cm, inclusive) {
+ return findBeginningAndEnd(cm, '"', inclusive);
+ }
+ };
+
+ /*
+ * Below are miscellaneous utility functions used by vim.js
+ */
+
+ /**
+ * Clips cursor to ensure that line is within the buffer's range
+ * If includeLineBreak is true, then allow cur.ch == lineLength.
+ */
+ function clipCursorToContent(cm, cur, includeLineBreak) {
+ var line = Math.min(Math.max(cm.firstLine(), cur.line), cm.lastLine() );
+ var maxCh = lineLength(cm, line) - 1;
+ maxCh = (includeLineBreak) ? maxCh + 1 : maxCh;
+ var ch = Math.min(Math.max(0, cur.ch), maxCh);
+ return { line: line, ch: ch };
+ }
+ function copyArgs(args) {
+ var ret = {};
+ for (var prop in args) {
+ if (args.hasOwnProperty(prop)) {
+ ret[prop] = args[prop];
+ }
+ }
+ return ret;
+ }
+ function offsetCursor(cur, offsetLine, offsetCh) {
+ return { line: cur.line + offsetLine, ch: cur.ch + offsetCh };
+ }
+ function matchKeysPartial(pressed, mapped) {
+ for (var i = 0; i < pressed.length; i++) {
+ // 'character' means any character. For mark, register commads, etc.
+ if (pressed[i] != mapped[i] && mapped[i] != 'character') {
+ return false;
+ }
+ }
+ return true;
+ }
+ function repeatFn(cm, fn, repeat) {
+ return function() {
+ for (var i = 0; i < repeat; i++) {
+ fn(cm);
+ }
+ };
+ }
+ function copyCursor(cur) {
+ return { line: cur.line, ch: cur.ch };
+ }
+ function cursorEqual(cur1, cur2) {
+ return cur1.ch == cur2.ch && cur1.line == cur2.line;
+ }
+ function cursorIsBefore(cur1, cur2) {
+ if (cur1.line < cur2.line) {
+ return true;
+ }
+ if (cur1.line == cur2.line && cur1.ch < cur2.ch) {
+ return true;
+ }
+ return false;
+ }
+ function cusrorIsBetween(cur1, cur2, cur3) {
+ // returns true if cur2 is between cur1 and cur3.
+ var cur1before2 = cursorIsBefore(cur1, cur2);
+ var cur2before3 = cursorIsBefore(cur2, cur3);
+ return cur1before2 && cur2before3;
+ }
+ function lineLength(cm, lineNum) {
+ return cm.getLine(lineNum).length;
+ }
+ function reverse(s){
+ return s.split('').reverse().join('');
+ }
+ function trim(s) {
+ if (s.trim) {
+ return s.trim();
+ }
+ return s.replace(/^\s+|\s+$/g, '');
+ }
+ function escapeRegex(s) {
+ return s.replace(/([.?*+$\[\]\/\\(){}|\-])/g, '\\$1');
+ }
+
+ function exitVisualMode(cm) {
+ cm.off('mousedown', exitVisualMode);
+ var vim = cm.state.vim;
+ vim.visualMode = false;
+ vim.visualLine = false;
+ var selectionStart = cm.getCursor('anchor');
+ var selectionEnd = cm.getCursor('head');
+ if (!cursorEqual(selectionStart, selectionEnd)) {
+ // Clear the selection and set the cursor only if the selection has not
+ // already been cleared. Otherwise we risk moving the cursor somewhere
+ // it's not supposed to be.
+ cm.setCursor(clipCursorToContent(cm, selectionEnd));
+ }
+ CodeMirror.signal(cm, "vim-mode-change", {mode: "normal"});
+ }
+
+ // Remove any trailing newlines from the selection. For
+ // example, with the caret at the start of the last word on the line,
+ // 'dw' should word, but not the newline, while 'w' should advance the
+ // caret to the first character of the next line.
+ function clipToLine(cm, curStart, curEnd) {
+ var selection = cm.getRange(curStart, curEnd);
+ // Only clip if the selection ends with trailing newline + whitespace
+ if (/\n\s*$/.test(selection)) {
+ var lines = selection.split('\n');
+ // We know this is all whitepsace.
+ lines.pop();
+
+ // Cases:
+ // 1. Last word is an empty line - do not clip the trailing '\n'
+ // 2. Last word is not an empty line - clip the trailing '\n'
+ var line;
+ // Find the line containing the last word, and clip all whitespace up
+ // to it.
+ for (var line = lines.pop(); lines.length > 0 && line && isWhiteSpaceString(line); line = lines.pop()) {
+ curEnd.line--;
+ curEnd.ch = 0;
+ }
+ // If the last word is not an empty line, clip an additional newline
+ if (line) {
+ curEnd.line--;
+ curEnd.ch = lineLength(cm, curEnd.line);
+ } else {
+ curEnd.ch = 0;
+ }
+ }
+ }
+
+ // Expand the selection to line ends.
+ function expandSelectionToLine(_cm, curStart, curEnd) {
+ curStart.ch = 0;
+ curEnd.ch = 0;
+ curEnd.line++;
+ }
+
+ function findFirstNonWhiteSpaceCharacter(text) {
+ if (!text) {
+ return 0;
+ }
+ var firstNonWS = text.search(/\S/);
+ return firstNonWS == -1 ? text.length : firstNonWS;
+ }
+
+ function expandWordUnderCursor(cm, inclusive, _forward, bigWord, noSymbol) {
+ var cur = cm.getCursor();
+ var line = cm.getLine(cur.line);
+ var idx = cur.ch;
+
+ // Seek to first word or non-whitespace character, depending on if
+ // noSymbol is true.
+ var textAfterIdx = line.substring(idx);
+ var firstMatchedChar;
+ if (noSymbol) {
+ firstMatchedChar = textAfterIdx.search(/\w/);
+ } else {
+ firstMatchedChar = textAfterIdx.search(/\S/);
+ }
+ if (firstMatchedChar == -1) {
+ return null;
+ }
+ idx += firstMatchedChar;
+ textAfterIdx = line.substring(idx);
+ var textBeforeIdx = line.substring(0, idx);
+
+ var matchRegex;
+ // Greedy matchers for the "word" we are trying to expand.
+ if (bigWord) {
+ matchRegex = /^\S+/;
+ } else {
+ if ((/\w/).test(line.charAt(idx))) {
+ matchRegex = /^\w+/;
+ } else {
+ matchRegex = /^[^\w\s]+/;
+ }
+ }
+
+ var wordAfterRegex = matchRegex.exec(textAfterIdx);
+ var wordStart = idx;
+ var wordEnd = idx + wordAfterRegex[0].length;
+ // TODO: Find a better way to do this. It will be slow on very long lines.
+ var revTextBeforeIdx = reverse(textBeforeIdx);
+ var wordBeforeRegex = matchRegex.exec(revTextBeforeIdx);
+ if (wordBeforeRegex) {
+ wordStart -= wordBeforeRegex[0].length;
+ }
+
+ if (inclusive) {
+ // If present, trim all whitespace after word.
+ // Otherwise, trim all whitespace before word.
+ var textAfterWordEnd = line.substring(wordEnd);
+ var whitespacesAfterWord = textAfterWordEnd.match(/^\s*/)[0].length;
+ if (whitespacesAfterWord > 0) {
+ wordEnd += whitespacesAfterWord;
+ } else {
+ var revTrim = revTextBeforeIdx.length - wordStart;
+ var textBeforeWordStart = revTextBeforeIdx.substring(revTrim);
+ var whitespacesBeforeWord = textBeforeWordStart.match(/^\s*/)[0].length;
+ wordStart -= whitespacesBeforeWord;
+ }
+ }
+
+ return { start: { line: cur.line, ch: wordStart },
+ end: { line: cur.line, ch: wordEnd }};
+ }
+
+ function recordJumpPosition(cm, oldCur, newCur) {
+ if(!cursorEqual(oldCur, newCur)) {
+ vimGlobalState.jumpList.add(cm, oldCur, newCur);
+ }
+ }
+
+ function recordLastCharacterSearch(increment, args) {
+ vimGlobalState.lastChararacterSearch.increment = increment;
+ vimGlobalState.lastChararacterSearch.forward = args.forward;
+ vimGlobalState.lastChararacterSearch.selectedCharacter = args.selectedCharacter;
+ }
+
+ var symbolToMode = {
+ '(': 'bracket', ')': 'bracket', '{': 'bracket', '}': 'bracket',
+ '[': 'section', ']': 'section',
+ '*': 'comment', '/': 'comment',
+ 'm': 'method', 'M': 'method',
+ '#': 'preprocess'
+ };
+ var findSymbolModes = {
+ bracket: {
+ isComplete: function(state) {
+ if (state.nextCh === state.symb) {
+ state.depth++;
+ if(state.depth >= 1)return true;
+ } else if (state.nextCh === state.reverseSymb) {
+ state.depth--;
+ }
+ return false;
+ }
+ },
+ section: {
+ init: function(state) {
+ state.curMoveThrough = true;
+ state.symb = (state.forward ? ']' : '[') === state.symb ? '{' : '}';
+ },
+ isComplete: function(state) {
+ return state.index === 0 && state.nextCh === state.symb;
+ }
+ },
+ comment: {
+ isComplete: function(state) {
+ var found = state.lastCh === '*' && state.nextCh === '/';
+ state.lastCh = state.nextCh;
+ return found;
+ }
+ },
+ // TODO: The original Vim implementation only operates on level 1 and 2.
+ // The current implementation doesn't check for code block level and
+ // therefore it operates on any levels.
+ method: {
+ init: function(state) {
+ state.symb = (state.symb === 'm' ? '{' : '}');
+ state.reverseSymb = state.symb === '{' ? '}' : '{';
+ },
+ isComplete: function(state) {
+ if(state.nextCh === state.symb)return true;
+ return false;
+ }
+ },
+ preprocess: {
+ init: function(state) {
+ state.index = 0;
+ },
+ isComplete: function(state) {
+ if (state.nextCh === '#') {
+ var token = state.lineText.match(/#(\w+)/)[1];
+ if (token === 'endif') {
+ if (state.forward && state.depth === 0) {
+ return true;
+ }
+ state.depth++;
+ } else if (token === 'if') {
+ if (!state.forward && state.depth === 0) {
+ return true;
+ }
+ state.depth--;
+ }
+ if(token === 'else' && state.depth === 0)return true;
+ }
+ return false;
+ }
+ }
+ };
+ function findSymbol(cm, repeat, forward, symb) {
+ var cur = cm.getCursor();
+ var increment = forward ? 1 : -1;
+ var endLine = forward ? cm.lineCount() : -1;
+ var curCh = cur.ch;
+ var line = cur.line;
+ var lineText = cm.getLine(line);
+ var state = {
+ lineText: lineText,
+ nextCh: lineText.charAt(curCh),
+ lastCh: null,
+ index: curCh,
+ symb: symb,
+ reverseSymb: (forward ? { ')': '(', '}': '{' } : { '(': ')', '{': '}' })[symb],
+ forward: forward,
+ depth: 0,
+ curMoveThrough: false
+ };
+ var mode = symbolToMode[symb];
+ if(!mode)return cur;
+ var init = findSymbolModes[mode].init;
+ var isComplete = findSymbolModes[mode].isComplete;
+ if(init)init(state);
+ while (line !== endLine && repeat) {
+ state.index += increment;
+ state.nextCh = state.lineText.charAt(state.index);
+ if (!state.nextCh) {
+ line += increment;
+ state.lineText = cm.getLine(line) || '';
+ if (increment > 0) {
+ state.index = 0;
+ } else {
+ var lineLen = state.lineText.length;
+ state.index = (lineLen > 0) ? (lineLen-1) : 0;
+ }
+ state.nextCh = state.lineText.charAt(state.index);
+ }
+ if (isComplete(state)) {
+ cur.line = line;
+ cur.ch = state.index;
+ repeat--;
+ }
+ }
+ if (state.nextCh || state.curMoveThrough) {
+ return { line: line, ch: state.index };
+ }
+ return cur;
+ }
+
+ /*
+ * Returns the boundaries of the next word. If the cursor in the middle of
+ * the word, then returns the boundaries of the current word, starting at
+ * the cursor. If the cursor is at the start/end of a word, and we are going
+ * forward/backward, respectively, find the boundaries of the next word.
+ *
+ * @param {CodeMirror} cm CodeMirror object.
+ * @param {Cursor} cur The cursor position.
+ * @param {boolean} forward True to search forward. False to search
+ * backward.
+ * @param {boolean} bigWord True if punctuation count as part of the word.
+ * False if only [a-zA-Z0-9] characters count as part of the word.
+ * @param {boolean} emptyLineIsWord True if empty lines should be treated
+ * as words.
+ * @return {Object{from:number, to:number, line: number}} The boundaries of
+ * the word, or null if there are no more words.
+ */
+ function findWord(cm, cur, forward, bigWord, emptyLineIsWord) {
+ var lineNum = cur.line;
+ var pos = cur.ch;
+ var line = cm.getLine(lineNum);
+ var dir = forward ? 1 : -1;
+ var regexps = bigWord ? bigWordRegexp : wordRegexp;
+
+ if (emptyLineIsWord && line == '') {
+ lineNum += dir;
+ line = cm.getLine(lineNum);
+ if (!isLine(cm, lineNum)) {
+ return null;
+ }
+ pos = (forward) ? 0 : line.length;
+ }
+
+ while (true) {
+ if (emptyLineIsWord && line == '') {
+ return { from: 0, to: 0, line: lineNum };
+ }
+ var stop = (dir > 0) ? line.length : -1;
+ var wordStart = stop, wordEnd = stop;
+ // Find bounds of next word.
+ while (pos != stop) {
+ var foundWord = false;
+ for (var i = 0; i < regexps.length && !foundWord; ++i) {
+ if (regexps[i].test(line.charAt(pos))) {
+ wordStart = pos;
+ // Advance to end of word.
+ while (pos != stop && regexps[i].test(line.charAt(pos))) {
+ pos += dir;
+ }
+ wordEnd = pos;
+ foundWord = wordStart != wordEnd;
+ if (wordStart == cur.ch && lineNum == cur.line &&
+ wordEnd == wordStart + dir) {
+ // We started at the end of a word. Find the next one.
+ continue;
+ } else {
+ return {
+ from: Math.min(wordStart, wordEnd + 1),
+ to: Math.max(wordStart, wordEnd),
+ line: lineNum };
+ }
+ }
+ }
+ if (!foundWord) {
+ pos += dir;
+ }
+ }
+ // Advance to next/prev line.
+ lineNum += dir;
+ if (!isLine(cm, lineNum)) {
+ return null;
+ }
+ line = cm.getLine(lineNum);
+ pos = (dir > 0) ? 0 : line.length;
+ }
+ // Should never get here.
+ throw new Error('The impossible happened.');
+ }
+
+ /**
+ * @param {CodeMirror} cm CodeMirror object.
+ * @param {int} repeat Number of words to move past.
+ * @param {boolean} forward True to search forward. False to search
+ * backward.
+ * @param {boolean} wordEnd True to move to end of word. False to move to
+ * beginning of word.
+ * @param {boolean} bigWord True if punctuation count as part of the word.
+ * False if only alphabet characters count as part of the word.
+ * @return {Cursor} The position the cursor should move to.
+ */
+ function moveToWord(cm, repeat, forward, wordEnd, bigWord) {
+ var cur = cm.getCursor();
+ var curStart = copyCursor(cur);
+ var words = [];
+ if (forward && !wordEnd || !forward && wordEnd) {
+ repeat++;
+ }
+ // For 'e', empty lines are not considered words, go figure.
+ var emptyLineIsWord = !(forward && wordEnd);
+ for (var i = 0; i < repeat; i++) {
+ var word = findWord(cm, cur, forward, bigWord, emptyLineIsWord);
+ if (!word) {
+ var eodCh = lineLength(cm, cm.lastLine());
+ words.push(forward
+ ? {line: cm.lastLine(), from: eodCh, to: eodCh}
+ : {line: 0, from: 0, to: 0});
+ break;
+ }
+ words.push(word);
+ cur = {line: word.line, ch: forward ? (word.to - 1) : word.from};
+ }
+ var shortCircuit = words.length != repeat;
+ var firstWord = words[0];
+ var lastWord = words.pop();
+ if (forward && !wordEnd) {
+ // w
+ if (!shortCircuit && (firstWord.from != curStart.ch || firstWord.line != curStart.line)) {
+ // We did not start in the middle of a word. Discard the extra word at the end.
+ lastWord = words.pop();
+ }
+ return {line: lastWord.line, ch: lastWord.from};
+ } else if (forward && wordEnd) {
+ return {line: lastWord.line, ch: lastWord.to - 1};
+ } else if (!forward && wordEnd) {
+ // ge
+ if (!shortCircuit && (firstWord.to != curStart.ch || firstWord.line != curStart.line)) {
+ // We did not start in the middle of a word. Discard the extra word at the end.
+ lastWord = words.pop();
+ }
+ return {line: lastWord.line, ch: lastWord.to};
+ } else {
+ // b
+ return {line: lastWord.line, ch: lastWord.from};
+ }
+ }
+
+ function moveToCharacter(cm, repeat, forward, character) {
+ var cur = cm.getCursor();
+ var start = cur.ch;
+ var idx;
+ for (var i = 0; i < repeat; i ++) {
+ var line = cm.getLine(cur.line);
+ idx = charIdxInLine(start, line, character, forward, true);
+ if (idx == -1) {
+ return null;
+ }
+ start = idx;
+ }
+ return { line: cm.getCursor().line, ch: idx };
+ }
+
+ function moveToColumn(cm, repeat) {
+ // repeat is always >= 1, so repeat - 1 always corresponds
+ // to the column we want to go to.
+ var line = cm.getCursor().line;
+ return clipCursorToContent(cm, { line: line, ch: repeat - 1 });
+ }
+
+ function updateMark(cm, vim, markName, pos) {
+ if (!inArray(markName, validMarks)) {
+ return;
+ }
+ if (vim.marks[markName]) {
+ vim.marks[markName].clear();
+ }
+ vim.marks[markName] = cm.setBookmark(pos);
+ }
+
+ function charIdxInLine(start, line, character, forward, includeChar) {
+ // Search for char in line.
+ // motion_options: {forward, includeChar}
+ // If includeChar = true, include it too.
+ // If forward = true, search forward, else search backwards.
+ // If char is not found on this line, do nothing
+ var idx;
+ if (forward) {
+ idx = line.indexOf(character, start + 1);
+ if (idx != -1 && !includeChar) {
+ idx -= 1;
+ }
+ } else {
+ idx = line.lastIndexOf(character, start - 1);
+ if (idx != -1 && !includeChar) {
+ idx += 1;
+ }
+ }
+ return idx;
+ }
+
+ function getContextLevel(ctx) {
+ return (ctx === 'string' || ctx === 'comment') ? 1 : 0;
+ }
+
+ function findMatchedSymbol(cm, cur, symb) {
+ var line = cur.line;
+ var ch = cur.ch;
+ symb = symb ? symb : cm.getLine(line).charAt(ch);
+
+ var symbContext = cm.getTokenAt({line:line, ch:ch+1}).type;
+ var symbCtxLevel = getContextLevel(symbContext);
+
+ var reverseSymb = ({
+ '(': ')', ')': '(',
+ '[': ']', ']': '[',
+ '{': '}', '}': '{'})[symb];
+
+ // Couldn't find a matching symbol, abort
+ if (!reverseSymb) {
+ return cur;
+ }
+
+ // set our increment to move forward (+1) or backwards (-1)
+ // depending on which bracket we're matching
+ var increment = ({'(': 1, '{': 1, '[': 1})[symb] || -1;
+ var endLine = increment === 1 ? cm.lineCount() : -1;
+ var depth = 1, nextCh = symb, index = ch, lineText = cm.getLine(line);
+ // Simple search for closing paren--just count openings and closings till
+ // we find our match
+ // TODO: use info from CodeMirror to ignore closing brackets in comments
+ // and quotes, etc.
+ while (line !== endLine && depth > 0) {
+ index += increment;
+ nextCh = lineText.charAt(index);
+ if (!nextCh) {
+ line += increment;
+ lineText = cm.getLine(line) || '';
+ if (increment > 0) {
+ index = 0;
+ } else {
+ var lineLen = lineText.length;
+ index = (lineLen > 0) ? (lineLen-1) : 0;
+ }
+ nextCh = lineText.charAt(index);
+ }
+ var revSymbContext = cm.getTokenAt({line:line, ch:index+1}).type;
+ var revSymbCtxLevel = getContextLevel(revSymbContext);
+ if (symbCtxLevel >= revSymbCtxLevel) {
+ if (nextCh === symb) {
+ depth++;
+ } else if (nextCh === reverseSymb) {
+ depth--;
+ }
+ }
+ }
+
+ if (nextCh) {
+ return { line: line, ch: index };
+ }
+ return cur;
+ }
+
+ function selectCompanionObject(cm, revSymb, inclusive) {
+ var cur = cm.getCursor();
+
+ var end = findMatchedSymbol(cm, cur, revSymb);
+ var start = findMatchedSymbol(cm, end);
+ start.ch += inclusive ? 1 : 0;
+ end.ch += inclusive ? 0 : 1;
+
+ return { start: start, end: end };
+ }
+
+ // Takes in a symbol and a cursor and tries to simulate text objects that
+ // have identical opening and closing symbols
+ // TODO support across multiple lines
+ function findBeginningAndEnd(cm, symb, inclusive) {
+ var cur = cm.getCursor();
+ var line = cm.getLine(cur.line);
+ var chars = line.split('');
+ var start, end, i, len;
+ var firstIndex = chars.indexOf(symb);
+
+ // the decision tree is to always look backwards for the beginning first,
+ // but if the cursor is in front of the first instance of the symb,
+ // then move the cursor forward
+ if (cur.ch < firstIndex) {
+ cur.ch = firstIndex;
+ // Why is this line even here???
+ // cm.setCursor(cur.line, firstIndex+1);
+ }
+ // otherwise if the cursor is currently on the closing symbol
+ else if (firstIndex < cur.ch && chars[cur.ch] == symb) {
+ end = cur.ch; // assign end to the current cursor
+ --cur.ch; // make sure to look backwards
+ }
+
+ // if we're currently on the symbol, we've got a start
+ if (chars[cur.ch] == symb && !end) {
+ start = cur.ch + 1; // assign start to ahead of the cursor
+ } else {
+ // go backwards to find the start
+ for (i = cur.ch; i > -1 && !start; i--) {
+ if (chars[i] == symb) {
+ start = i + 1;
+ }
+ }
+ }
+
+ // look forwards for the end symbol
+ if (start && !end) {
+ for (i = start, len = chars.length; i < len && !end; i++) {
+ if (chars[i] == symb) {
+ end = i;
+ }
+ }
+ }
+
+ // nothing found
+ if (!start || !end) {
+ return { start: cur, end: cur };
+ }
+
+ // include the symbols
+ if (inclusive) {
+ --start; ++end;
+ }
+
+ return {
+ start: { line: cur.line, ch: start },
+ end: { line: cur.line, ch: end }
+ };
+ }
+
+ // Search functions
+ function SearchState() {}
+ SearchState.prototype = {
+ getQuery: function() {
+ return vimGlobalState.query;
+ },
+ setQuery: function(query) {
+ vimGlobalState.query = query;
+ },
+ getOverlay: function() {
+ return this.searchOverlay;
+ },
+ setOverlay: function(overlay) {
+ this.searchOverlay = overlay;
+ },
+ isReversed: function() {
+ return vimGlobalState.isReversed;
+ },
+ setReversed: function(reversed) {
+ vimGlobalState.isReversed = reversed;
+ }
+ };
+ function getSearchState(cm) {
+ var vim = cm.state.vim;
+ return vim.searchState_ || (vim.searchState_ = new SearchState());
+ }
+ function dialog(cm, template, shortText, onClose, options) {
+ if (cm.openDialog) {
+ cm.openDialog(template, onClose, { bottom: true, value: options.value,
+ onKeyDown: options.onKeyDown, onKeyUp: options.onKeyUp });
+ }
+ else {
+ onClose(prompt(shortText, ''));
+ }
+ }
+
+ function findUnescapedSlashes(str) {
+ var escapeNextChar = false;
+ var slashes = [];
+ for (var i = 0; i < str.length; i++) {
+ var c = str.charAt(i);
+ if (!escapeNextChar && c == '/') {
+ slashes.push(i);
+ }
+ escapeNextChar = (c == '\\');
+ }
+ return slashes;
+ }
+ /**
+ * Extract the regular expression from the query and return a Regexp object.
+ * Returns null if the query is blank.
+ * If ignoreCase is passed in, the Regexp object will have the 'i' flag set.
+ * If smartCase is passed in, and the query contains upper case letters,
+ * then ignoreCase is overridden, and the 'i' flag will not be set.
+ * If the query contains the /i in the flag part of the regular expression,
+ * then both ignoreCase and smartCase are ignored, and 'i' will be passed
+ * through to the Regex object.
+ */
+ function parseQuery(query, ignoreCase, smartCase) {
+ // Check if the query is already a regex.
+ if (query instanceof RegExp) { return query; }
+ // First try to extract regex + flags from the input. If no flags found,
+ // extract just the regex. IE does not accept flags directly defined in
+ // the regex string in the form /regex/flags
+ var slashes = findUnescapedSlashes(query);
+ var regexPart;
+ var forceIgnoreCase;
+ if (!slashes.length) {
+ // Query looks like 'regexp'
+ regexPart = query;
+ } else {
+ // Query looks like 'regexp/...'
+ regexPart = query.substring(0, slashes[0]);
+ var flagsPart = query.substring(slashes[0]);
+ forceIgnoreCase = (flagsPart.indexOf('i') != -1);
+ }
+ if (!regexPart) {
+ return null;
+ }
+ if (smartCase) {
+ ignoreCase = (/^[^A-Z]*$/).test(regexPart);
+ }
+ var regexp = new RegExp(regexPart,
+ (ignoreCase || forceIgnoreCase) ? 'i' : undefined);
+ return regexp;
+ }
+ function showConfirm(cm, text) {
+ if (cm.openNotification) {
+ cm.openNotification('' + text + '',
+ {bottom: true, duration: 5000});
+ } else {
+ alert(text);
+ }
+ }
+ function makePrompt(prefix, desc) {
+ var raw = '';
+ if (prefix) {
+ raw += '' + prefix + '';
+ }
+ raw += ' ' +
+ '';
+ if (desc) {
+ raw += '';
+ raw += desc;
+ raw += '';
+ }
+ return raw;
+ }
+ var searchPromptDesc = '(Javascript regexp)';
+ function showPrompt(cm, options) {
+ var shortText = (options.prefix || '') + ' ' + (options.desc || '');
+ var prompt = makePrompt(options.prefix, options.desc);
+ dialog(cm, prompt, shortText, options.onClose, options);
+ }
+ function regexEqual(r1, r2) {
+ if (r1 instanceof RegExp && r2 instanceof RegExp) {
+ var props = ['global', 'multiline', 'ignoreCase', 'source'];
+ for (var i = 0; i < props.length; i++) {
+ var prop = props[i];
+ if (r1[prop] !== r2[prop]) {
+ return false;
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+ // Returns true if the query is valid.
+ function updateSearchQuery(cm, rawQuery, ignoreCase, smartCase) {
+ if (!rawQuery) {
+ return;
+ }
+ var state = getSearchState(cm);
+ var query = parseQuery(rawQuery, !!ignoreCase, !!smartCase);
+ if (!query) {
+ return;
+ }
+ highlightSearchMatches(cm, query);
+ if (regexEqual(query, state.getQuery())) {
+ return query;
+ }
+ state.setQuery(query);
+ return query;
+ }
+ function searchOverlay(query) {
+ if (query.source.charAt(0) == '^') {
+ var matchSol = true;
+ }
+ return {
+ token: function(stream) {
+ if (matchSol && !stream.sol()) {
+ stream.skipToEnd();
+ return;
+ }
+ var match = stream.match(query, false);
+ if (match) {
+ if (match[0].length == 0) {
+ // Matched empty string, skip to next.
+ stream.next();
+ return 'searching';
+ }
+ if (!stream.sol()) {
+ // Backtrack 1 to match \b
+ stream.backUp(1);
+ if (!query.exec(stream.next() + match[0])) {
+ stream.next();
+ return null;
+ }
+ }
+ stream.match(query);
+ return 'searching';
+ }
+ while (!stream.eol()) {
+ stream.next();
+ if (stream.match(query, false)) break;
+ }
+ },
+ query: query
+ };
+ }
+ function highlightSearchMatches(cm, query) {
+ var overlay = getSearchState(cm).getOverlay();
+ if (!overlay || query != overlay.query) {
+ if (overlay) {
+ cm.removeOverlay(overlay);
+ }
+ overlay = searchOverlay(query);
+ cm.addOverlay(overlay);
+ getSearchState(cm).setOverlay(overlay);
+ }
+ }
+ function findNext(cm, prev, query, repeat) {
+ if (repeat === undefined) { repeat = 1; }
+ return cm.operation(function() {
+ var pos = cm.getCursor();
+ var cursor = cm.getSearchCursor(query, pos);
+ for (var i = 0; i < repeat; i++) {
+ var found = cursor.find(prev);
+ if (i == 0 && found && cursorEqual(cursor.from(), pos)) { found = cursor.find(prev); }
+ if (!found) {
+ // SearchCursor may have returned null because it hit EOF, wrap
+ // around and try again.
+ cursor = cm.getSearchCursor(query,
+ (prev) ? { line: cm.lastLine() } : {line: cm.firstLine(), ch: 0} );
+ if (!cursor.find(prev)) {
+ return;
+ }
+ }
+ }
+ return cursor.from();
+ });
+ }
+ function clearSearchHighlight(cm) {
+ cm.removeOverlay(getSearchState(cm).getOverlay());
+ getSearchState(cm).setOverlay(null);
+ }
+ /**
+ * Check if pos is in the specified range, INCLUSIVE.
+ * Range can be specified with 1 or 2 arguments.
+ * If the first range argument is an array, treat it as an array of line
+ * numbers. Match pos against any of the lines.
+ * If the first range argument is a number,
+ * if there is only 1 range argument, check if pos has the same line
+ * number
+ * if there are 2 range arguments, then check if pos is in between the two
+ * range arguments.
+ */
+ function isInRange(pos, start, end) {
+ if (typeof pos != 'number') {
+ // Assume it is a cursor position. Get the line number.
+ pos = pos.line;
+ }
+ if (start instanceof Array) {
+ return inArray(pos, start);
+ } else {
+ if (end) {
+ return (pos >= start && pos <= end);
+ } else {
+ return pos == start;
+ }
+ }
+ }
+ function getUserVisibleLines(cm) {
+ var scrollInfo = cm.getScrollInfo();
+ var occludeToleranceTop = 6;
+ var occludeToleranceBottom = 10;
+ var from = cm.coordsChar({left:0, top: occludeToleranceTop + scrollInfo.top}, 'local');
+ var bottomY = scrollInfo.clientHeight - occludeToleranceBottom + scrollInfo.top;
+ var to = cm.coordsChar({left:0, top: bottomY}, 'local');
+ return {top: from.line, bottom: to.line};
+ }
+
+ // Ex command handling
+ // Care must be taken when adding to the default Ex command map. For any
+ // pair of commands that have a shared prefix, at least one of their
+ // shortNames must not match the prefix of the other command.
+ var defaultExCommandMap = [
+ { name: 'map', type: 'builtIn' },
+ { name: 'write', shortName: 'w', type: 'builtIn' },
+ { name: 'undo', shortName: 'u', type: 'builtIn' },
+ { name: 'redo', shortName: 'red', type: 'builtIn' },
+ { name: 'sort', shortName: 'sor', type: 'builtIn'},
+ { name: 'substitute', shortName: 's', type: 'builtIn'},
+ { name: 'nohlsearch', shortName: 'noh', type: 'builtIn'},
+ { name: 'delmarks', shortName: 'delm', type: 'builtin'}
+ ];
+ Vim.ExCommandDispatcher = function() {
+ this.buildCommandMap_();
+ };
+ Vim.ExCommandDispatcher.prototype = {
+ processCommand: function(cm, input) {
+ var vim = cm.state.vim;
+ if (vim.visualMode) {
+ exitVisualMode(cm);
+ }
+ var inputStream = new CodeMirror.StringStream(input);
+ var params = {};
+ params.input = input;
+ try {
+ this.parseInput_(cm, inputStream, params);
+ } catch(e) {
+ showConfirm(cm, e);
+ return;
+ }
+ var commandName;
+ if (!params.commandName) {
+ // If only a line range is defined, move to the line.
+ if (params.line !== undefined) {
+ commandName = 'move';
+ }
+ } else {
+ var command = this.matchCommand_(params.commandName);
+ if (command) {
+ commandName = command.name;
+ this.parseCommandArgs_(inputStream, params, command);
+ if (command.type == 'exToKey') {
+ // Handle Ex to Key mapping.
+ for (var i = 0; i < command.toKeys.length; i++) {
+ CodeMirror.Vim.handleKey(cm, command.toKeys[i]);
+ }
+ return;
+ } else if (command.type == 'exToEx') {
+ // Handle Ex to Ex mapping.
+ this.processCommand(cm, command.toInput);
+ return;
+ }
+ }
+ }
+ if (!commandName) {
+ showConfirm(cm, 'Not an editor command ":' + input + '"');
+ return;
+ }
+ try {
+ exCommands[commandName](cm, params);
+ } catch(e) {
+ showConfirm(cm, e);
+ }
+ },
+ parseInput_: function(cm, inputStream, result) {
+ inputStream.eatWhile(':');
+ // Parse range.
+ if (inputStream.eat('%')) {
+ result.line = cm.firstLine();
+ result.lineEnd = cm.lastLine();
+ } else {
+ result.line = this.parseLineSpec_(cm, inputStream);
+ if (result.line !== undefined && inputStream.eat(',')) {
+ result.lineEnd = this.parseLineSpec_(cm, inputStream);
+ }
+ }
+
+ // Parse command name.
+ var commandMatch = inputStream.match(/^(\w+)/);
+ if (commandMatch) {
+ result.commandName = commandMatch[1];
+ } else {
+ result.commandName = inputStream.match(/.*/)[0];
+ }
+
+ return result;
+ },
+ parseLineSpec_: function(cm, inputStream) {
+ var numberMatch = inputStream.match(/^(\d+)/);
+ if (numberMatch) {
+ return parseInt(numberMatch[1], 10) - 1;
+ }
+ switch (inputStream.next()) {
+ case '.':
+ return cm.getCursor().line;
+ case '$':
+ return cm.lastLine();
+ case '\'':
+ var mark = cm.state.vim.marks[inputStream.next()];
+ if (mark && mark.find()) {
+ return mark.find().line;
+ }
+ throw new Error('Mark not set');
+ default:
+ inputStream.backUp(1);
+ return undefined;
+ }
+ },
+ parseCommandArgs_: function(inputStream, params, command) {
+ if (inputStream.eol()) {
+ return;
+ }
+ params.argString = inputStream.match(/.*/)[0];
+ // Parse command-line arguments
+ var delim = command.argDelimiter || /\s+/;
+ var args = trim(params.argString).split(delim);
+ if (args.length && args[0]) {
+ params.args = args;
+ }
+ },
+ matchCommand_: function(commandName) {
+ // Return the command in the command map that matches the shortest
+ // prefix of the passed in command name. The match is guaranteed to be
+ // unambiguous if the defaultExCommandMap's shortNames are set up
+ // correctly. (see @code{defaultExCommandMap}).
+ for (var i = commandName.length; i > 0; i--) {
+ var prefix = commandName.substring(0, i);
+ if (this.commandMap_[prefix]) {
+ var command = this.commandMap_[prefix];
+ if (command.name.indexOf(commandName) === 0) {
+ return command;
+ }
+ }
+ }
+ return null;
+ },
+ buildCommandMap_: function() {
+ this.commandMap_ = {};
+ for (var i = 0; i < defaultExCommandMap.length; i++) {
+ var command = defaultExCommandMap[i];
+ var key = command.shortName || command.name;
+ this.commandMap_[key] = command;
+ }
+ },
+ map: function(lhs, rhs) {
+ if (lhs != ':' && lhs.charAt(0) == ':') {
+ var commandName = lhs.substring(1);
+ if (rhs != ':' && rhs.charAt(0) == ':') {
+ // Ex to Ex mapping
+ this.commandMap_[commandName] = {
+ name: commandName,
+ type: 'exToEx',
+ toInput: rhs.substring(1)
+ };
+ } else {
+ // Ex to key mapping
+ this.commandMap_[commandName] = {
+ name: commandName,
+ type: 'exToKey',
+ toKeys: parseKeyString(rhs)
+ };
+ }
+ } else {
+ if (rhs != ':' && rhs.charAt(0) == ':') {
+ // Key to Ex mapping.
+ defaultKeymap.unshift({
+ keys: parseKeyString(lhs),
+ type: 'keyToEx',
+ exArgs: { input: rhs.substring(1) }});
+ } else {
+ // Key to key mapping
+ defaultKeymap.unshift({
+ keys: parseKeyString(lhs),
+ type: 'keyToKey',
+ toKeys: parseKeyString(rhs)
+ });
+ }
+ }
+ }
+ };
+
+ // Converts a key string sequence of the form abd into Vim's
+ // keymap representation.
+ function parseKeyString(str) {
+ var key, match;
+ var keys = [];
+ while (str) {
+ match = (/<\w+-.+?>|<\w+>|./).exec(str);
+ if(match === null)break;
+ key = match[0];
+ str = str.substring(match.index + key.length);
+ keys.push(key);
+ }
+ return keys;
+ }
+
+ var exCommands = {
+ map: function(cm, params) {
+ var mapArgs = params.args;
+ if (!mapArgs || mapArgs.length < 2) {
+ if (cm) {
+ showConfirm(cm, 'Invalid mapping: ' + params.input);
+ }
+ return;
+ }
+ exCommandDispatcher.map(mapArgs[0], mapArgs[1], cm);
+ },
+ move: function(cm, params) {
+ commandDispatcher.processCommand(cm, cm.state.vim, {
+ type: 'motion',
+ motion: 'moveToLineOrEdgeOfDocument',
+ motionArgs: { forward: false, explicitRepeat: true,
+ linewise: true },
+ repeatOverride: params.line+1});
+ },
+ sort: function(cm, params) {
+ var reverse, ignoreCase, unique, number;
+ function parseArgs() {
+ if (params.argString) {
+ var args = new CodeMirror.StringStream(params.argString);
+ if (args.eat('!')) { reverse = true; }
+ if (args.eol()) { return; }
+ if (!args.eatSpace()) { throw new Error('invalid arguments ' + args.match(/.*/)[0]); }
+ var opts = args.match(/[a-z]+/);
+ if (opts) {
+ opts = opts[0];
+ ignoreCase = opts.indexOf('i') != -1;
+ unique = opts.indexOf('u') != -1;
+ var decimal = opts.indexOf('d') != -1 && 1;
+ var hex = opts.indexOf('x') != -1 && 1;
+ var octal = opts.indexOf('o') != -1 && 1;
+ if (decimal + hex + octal > 1) { throw new Error('invalid arguments'); }
+ number = decimal && 'decimal' || hex && 'hex' || octal && 'octal';
+ }
+ if (args.eatSpace() && args.match(/\/.*\//)) { throw new Error('patterns not supported'); }
+ }
+ }
+ parseArgs();
+ var lineStart = params.line || cm.firstLine();
+ var lineEnd = params.lineEnd || params.line || cm.lastLine();
+ if (lineStart == lineEnd) { return; }
+ var curStart = { line: lineStart, ch: 0 };
+ var curEnd = { line: lineEnd, ch: lineLength(cm, lineEnd) };
+ var text = cm.getRange(curStart, curEnd).split('\n');
+ var numberRegex = (number == 'decimal') ? /(-?)([\d]+)/ :
+ (number == 'hex') ? /(-?)(?:0x)?([0-9a-f]+)/i :
+ (number == 'octal') ? /([0-7]+)/ : null;
+ var radix = (number == 'decimal') ? 10 : (number == 'hex') ? 16 : (number == 'octal') ? 8 : null;
+ var numPart = [], textPart = [];
+ if (number) {
+ for (var i = 0; i < text.length; i++) {
+ if (numberRegex.exec(text[i])) {
+ numPart.push(text[i]);
+ } else {
+ textPart.push(text[i]);
+ }
+ }
+ } else {
+ textPart = text;
+ }
+ function compareFn(a, b) {
+ if (reverse) { var tmp; tmp = a; a = b; b = tmp; }
+ if (ignoreCase) { a = a.toLowerCase(); b = b.toLowerCase(); }
+ var anum = number && numberRegex.exec(a);
+ var bnum = number && numberRegex.exec(b);
+ if (!anum) { return a < b ? -1 : 1; }
+ anum = parseInt((anum[1] + anum[2]).toLowerCase(), radix);
+ bnum = parseInt((bnum[1] + bnum[2]).toLowerCase(), radix);
+ return anum - bnum;
+ }
+ numPart.sort(compareFn);
+ textPart.sort(compareFn);
+ text = (!reverse) ? textPart.concat(numPart) : numPart.concat(textPart);
+ if (unique) { // Remove duplicate lines
+ var textOld = text;
+ var lastLine;
+ text = [];
+ for (var i = 0; i < textOld.length; i++) {
+ if (textOld[i] != lastLine) {
+ text.push(textOld[i]);
+ }
+ lastLine = textOld[i];
+ }
+ }
+ cm.replaceRange(text.join('\n'), curStart, curEnd);
+ },
+ substitute: function(cm, params) {
+ if (!cm.getSearchCursor) {
+ throw new Error('Search feature not available. Requires searchcursor.js or ' +
+ 'any other getSearchCursor implementation.');
+ }
+ var argString = params.argString;
+ var slashes = findUnescapedSlashes(argString);
+ if (slashes[0] !== 0) {
+ showConfirm(cm, 'Substitutions should be of the form ' +
+ ':s/pattern/replace/');
+ return;
+ }
+ var regexPart = argString.substring(slashes[0] + 1, slashes[1]);
+ var replacePart = '';
+ var flagsPart;
+ var count;
+ var confirm = false; // Whether to confirm each replace.
+ if (slashes[1]) {
+ replacePart = argString.substring(slashes[1] + 1, slashes[2]);
+ }
+ if (slashes[2]) {
+ // After the 3rd slash, we can have flags followed by a space followed
+ // by count.
+ var trailing = argString.substring(slashes[2] + 1).split(' ');
+ flagsPart = trailing[0];
+ count = parseInt(trailing[1]);
+ }
+ if (flagsPart) {
+ if (flagsPart.indexOf('c') != -1) {
+ confirm = true;
+ flagsPart.replace('c', '');
+ }
+ regexPart = regexPart + '/' + flagsPart;
+ }
+ if (regexPart) {
+ // If regex part is empty, then use the previous query. Otherwise use
+ // the regex part as the new query.
+ try {
+ updateSearchQuery(cm, regexPart, true /** ignoreCase */,
+ true /** smartCase */);
+ } catch (e) {
+ showConfirm(cm, 'Invalid regex: ' + regexPart);
+ return;
+ }
+ }
+ var state = getSearchState(cm);
+ var query = state.getQuery();
+ var lineStart = (params.line !== undefined) ? params.line : cm.getCursor().line;
+ var lineEnd = params.lineEnd || lineStart;
+ if (count) {
+ lineStart = lineEnd;
+ lineEnd = lineStart + count - 1;
+ }
+ var startPos = clipCursorToContent(cm, { line: lineStart, ch: 0 });
+ var cursor = cm.getSearchCursor(query, startPos);
+ doReplace(cm, confirm, lineStart, lineEnd, cursor, query, replacePart);
+ },
+ redo: CodeMirror.commands.redo,
+ undo: CodeMirror.commands.undo,
+ write: function(cm) {
+ if (CodeMirror.commands.save) {
+ // If a save command is defined, call it.
+ CodeMirror.commands.save(cm);
+ } else {
+ // Saves to text area if no save command is defined.
+ cm.save();
+ }
+ },
+ nohlsearch: function(cm) {
+ clearSearchHighlight(cm);
+ },
+ delmarks: function(cm, params) {
+ if (!params.argString || !params.argString.trim()) {
+ showConfirm(cm, 'Argument required');
+ return;
+ }
+
+ var state = cm.state.vim;
+ var stream = new CodeMirror.StringStream(params.argString.trim());
+ while (!stream.eol()) {
+ stream.eatSpace();
+
+ // Record the streams position at the beginning of the loop for use
+ // in error messages.
+ var count = stream.pos;
+
+ if (!stream.match(/[a-zA-Z]/, false)) {
+ showConfirm(cm, 'Invalid argument: ' + params.argString.substring(count));
+ return;
+ }
+
+ var sym = stream.next();
+ // Check if this symbol is part of a range
+ if (stream.match('-', true)) {
+ // This symbol is part of a range.
+
+ // The range must terminate at an alphabetic character.
+ if (!stream.match(/[a-zA-Z]/, false)) {
+ showConfirm(cm, 'Invalid argument: ' + params.argString.substring(count));
+ return;
+ }
+
+ var startMark = sym;
+ var finishMark = stream.next();
+ // The range must terminate at an alphabetic character which
+ // shares the same case as the start of the range.
+ if (isLowerCase(startMark) && isLowerCase(finishMark) ||
+ isUpperCase(startMark) && isUpperCase(finishMark)) {
+ var start = startMark.charCodeAt(0);
+ var finish = finishMark.charCodeAt(0);
+ if (start >= finish) {
+ showConfirm(cm, 'Invalid argument: ' + params.argString.substring(count));
+ return;
+ }
+
+ // Because marks are always ASCII values, and we have
+ // determined that they are the same case, we can use
+ // their char codes to iterate through the defined range.
+ for (var j = 0; j <= finish - start; j++) {
+ var mark = String.fromCharCode(start + j);
+ delete state.marks[mark];
+ }
+ } else {
+ showConfirm(cm, 'Invalid argument: ' + startMark + '-');
+ return;
+ }
+ } else {
+ // This symbol is a valid mark, and is not part of a range.
+ delete state.marks[sym];
+ }
+ }
+ }
+ };
+
+ var exCommandDispatcher = new Vim.ExCommandDispatcher();
+
+ /**
+ * @param {CodeMirror} cm CodeMirror instance we are in.
+ * @param {boolean} confirm Whether to confirm each replace.
+ * @param {Cursor} lineStart Line to start replacing from.
+ * @param {Cursor} lineEnd Line to stop replacing at.
+ * @param {RegExp} query Query for performing matches with.
+ * @param {string} replaceWith Text to replace matches with. May contain $1,
+ * $2, etc for replacing captured groups using Javascript replace.
+ */
+ function doReplace(cm, confirm, lineStart, lineEnd, searchCursor, query,
+ replaceWith) {
+ // Set up all the functions.
+ cm.state.vim.exMode = true;
+ var done = false;
+ var lastPos = searchCursor.from();
+ function replaceAll() {
+ cm.operation(function() {
+ while (!done) {
+ replace();
+ next();
+ }
+ stop();
+ });
+ }
+ function replace() {
+ var text = cm.getRange(searchCursor.from(), searchCursor.to());
+ var newText = text.replace(query, replaceWith);
+ searchCursor.replace(newText);
+ }
+ function next() {
+ var found = searchCursor.findNext();
+ if (!found) {
+ done = true;
+ } else if (isInRange(searchCursor.from(), lineStart, lineEnd)) {
+ cm.scrollIntoView(searchCursor.from(), 30);
+ cm.setSelection(searchCursor.from(), searchCursor.to());
+ lastPos = searchCursor.from();
+ done = false;
+ } else {
+ done = true;
+ }
+ }
+ function stop(close) {
+ if (close) { close(); }
+ cm.focus();
+ if (lastPos) {
+ cm.setCursor(lastPos);
+ var vim = cm.state.vim;
+ vim.exMode = false;
+ vim.lastHPos = vim.lastHSPos = lastPos.ch;
+ }
+ }
+ function onPromptKeyDown(e, _value, close) {
+ // Swallow all keys.
+ CodeMirror.e_stop(e);
+ var keyName = CodeMirror.keyName(e);
+ switch (keyName) {
+ case 'Y':
+ replace(); next(); break;
+ case 'N':
+ next(); break;
+ case 'A':
+ cm.operation(replaceAll); break;
+ case 'L':
+ replace();
+ // fall through and exit.
+ case 'Q':
+ case 'Esc':
+ case 'Ctrl-C':
+ case 'Ctrl-[':
+ stop(close);
+ break;
+ }
+ if (done) { stop(close); }
+ }
+
+ // Actually do replace.
+ next();
+ if (done) {
+ throw new Error('No matches for ' + query.source);
+ }
+ if (!confirm) {
+ replaceAll();
+ return;
+ }
+ showPrompt(cm, {
+ prefix: 'replace with ' + replaceWith + ' (y/n/a/q/l)',
+ onKeyDown: onPromptKeyDown
+ });
+ }
+
+ // Register Vim with CodeMirror
+ function buildVimKeyMap() {
+ /**
+ * Handle the raw key event from CodeMirror. Translate the
+ * Shift + key modifier to the resulting letter, while preserving other
+ * modifers.
+ */
+ // TODO: Figure out a way to catch capslock.
+ function cmKeyToVimKey(key, modifier) {
+ var vimKey = key;
+ if (isUpperCase(vimKey)) {
+ // Convert to lower case if shift is not the modifier since the key
+ // we get from CodeMirror is always upper case.
+ if (modifier == 'Shift') {
+ modifier = null;
+ }
+ else {
+ vimKey = vimKey.toLowerCase();
+ }
+ }
+ if (modifier) {
+ // Vim will parse modifier+key combination as a single key.
+ vimKey = modifier.charAt(0) + '-' + vimKey;
+ }
+ var specialKey = ({Enter:'CR',Backspace:'BS',Delete:'Del'})[vimKey];
+ vimKey = specialKey ? specialKey : vimKey;
+ vimKey = vimKey.length > 1 ? '<'+ vimKey + '>' : vimKey;
+ return vimKey;
+ }
+
+ // Closure to bind CodeMirror, key, modifier.
+ function keyMapper(vimKey) {
+ return function(cm) {
+ CodeMirror.Vim.handleKey(cm, vimKey);
+ };
+ }
+
+ var cmToVimKeymap = {
+ 'nofallthrough': true,
+ 'disableInput': true,
+ 'style': 'fat-cursor'
+ };
+ function bindKeys(keys, modifier) {
+ for (var i = 0; i < keys.length; i++) {
+ var key = keys[i];
+ if (!modifier && inArray(key, specialSymbols)) {
+ // Wrap special symbols with '' because that's how CodeMirror binds
+ // them.
+ key = "'" + key + "'";
+ }
+ var vimKey = cmKeyToVimKey(keys[i], modifier);
+ var cmKey = modifier ? modifier + '-' + key : key;
+ cmToVimKeymap[cmKey] = keyMapper(vimKey);
+ }
+ }
+ bindKeys(upperCaseAlphabet);
+ bindKeys(upperCaseAlphabet, 'Shift');
+ bindKeys(upperCaseAlphabet, 'Ctrl');
+ bindKeys(specialSymbols);
+ bindKeys(specialSymbols, 'Ctrl');
+ bindKeys(numbers);
+ bindKeys(numbers, 'Ctrl');
+ bindKeys(specialKeys);
+ bindKeys(specialKeys, 'Ctrl');
+ return cmToVimKeymap;
+ }
+ CodeMirror.keyMap.vim = buildVimKeyMap();
+
+ function exitInsertMode(cm) {
+ var vim = cm.state.vim;
+ var inReplay = vimGlobalState.macroModeState.inReplay;
+ if (!inReplay) {
+ cm.off('change', onChange);
+ cm.off('cursorActivity', onCursorActivity);
+ CodeMirror.off(cm.getInputField(), 'keydown', onKeyEventTargetKeyDown);
+ }
+ if (!inReplay && vim.insertModeRepeat > 1) {
+ // Perform insert mode repeat for commands like 3,a and 3,o.
+ repeatLastEdit(cm, vim, vim.insertModeRepeat - 1,
+ true /** repeatForInsert */);
+ vim.lastEditInputState.repeatOverride = vim.insertModeRepeat;
+ }
+ delete vim.insertModeRepeat;
+ cm.setCursor(cm.getCursor().line, cm.getCursor().ch-1, true);
+ vim.insertMode = false;
+ cm.setOption('keyMap', 'vim');
+ cm.toggleOverwrite(false); // exit replace mode if we were in it.
+ CodeMirror.signal(cm, "vim-mode-change", {mode: "normal"});
+ }
+
+ CodeMirror.keyMap['vim-insert'] = {
+ // TODO: override navigation keys so that Esc will cancel automatic
+ // indentation from o, O, i_
+ 'Esc': exitInsertMode,
+ 'Ctrl-[': exitInsertMode,
+ 'Ctrl-C': exitInsertMode,
+ 'Ctrl-N': 'autocomplete',
+ 'Ctrl-P': 'autocomplete',
+ 'Enter': function(cm) {
+ var fn = CodeMirror.commands.newlineAndIndentContinueComment ||
+ CodeMirror.commands.newlineAndIndent;
+ fn(cm);
+ },
+ fallthrough: ['default']
+ };
+
+ CodeMirror.keyMap['vim-replace'] = {
+ 'Backspace': 'goCharLeft',
+ fallthrough: ['vim-insert']
+ };
+
+ function parseRegisterToKeyBuffer(macroModeState, registerName) {
+ var match, key;
+ var register = vimGlobalState.registerController.getRegister(registerName);
+ var text = register.toString();
+ var macroKeyBuffer = macroModeState.macroKeyBuffer;
+ emptyMacroKeyBuffer(macroModeState);
+ do {
+ match = (/<\w+-.+?>|<\w+>|./).exec(text);
+ if(match === null)break;
+ key = match[0];
+ text = text.substring(match.index + key.length);
+ macroKeyBuffer.push(key);
+ } while (text);
+ return macroKeyBuffer;
+ }
+
+ function parseKeyBufferToRegister(registerName, keyBuffer) {
+ var text = keyBuffer.join('');
+ vimGlobalState.registerController.setRegisterText(registerName, text);
+ }
+
+ function emptyMacroKeyBuffer(macroModeState) {
+ if(macroModeState.isMacroPlaying)return;
+ var macroKeyBuffer = macroModeState.macroKeyBuffer;
+ macroKeyBuffer.length = 0;
+ }
+
+ function executeMacroKeyBuffer(cm, macroModeState, keyBuffer) {
+ macroModeState.isMacroPlaying = true;
+ for (var i = 0, len = keyBuffer.length; i < len; i++) {
+ CodeMirror.Vim.handleKey(cm, keyBuffer[i]);
+ };
+ macroModeState.isMacroPlaying = false;
+ }
+
+ function logKey(macroModeState, key) {
+ if(macroModeState.isMacroPlaying)return;
+ var macroKeyBuffer = macroModeState.macroKeyBuffer;
+ macroKeyBuffer.push(key);
+ }
+
+ /**
+ * Listens for changes made in insert mode.
+ * Should only be active in insert mode.
+ */
+ function onChange(_cm, changeObj) {
+ var macroModeState = vimGlobalState.macroModeState;
+ var lastChange = macroModeState.lastInsertModeChanges;
+ while (changeObj) {
+ lastChange.expectCursorActivityForChange = true;
+ if (changeObj.origin == '+input' || changeObj.origin == 'paste'
+ || changeObj.origin === undefined /* only in testing */) {
+ var text = changeObj.text.join('\n');
+ lastChange.changes.push(text);
+ }
+ // Change objects may be chained with next.
+ changeObj = changeObj.next;
+ }
+ }
+
+ /**
+ * Listens for any kind of cursor activity on CodeMirror.
+ * - For tracking cursor activity in insert mode.
+ * - Should only be active in insert mode.
+ */
+ function onCursorActivity() {
+ var macroModeState = vimGlobalState.macroModeState;
+ var lastChange = macroModeState.lastInsertModeChanges;
+ if (lastChange.expectCursorActivityForChange) {
+ lastChange.expectCursorActivityForChange = false;
+ } else {
+ // Cursor moved outside the context of an edit. Reset the change.
+ lastChange.changes = [];
+ }
+ }
+
+ /** Wrapper for special keys pressed in insert mode */
+ function InsertModeKey(keyName) {
+ this.keyName = keyName;
+ }
+
+ /**
+ * Handles raw key down events from the text area.
+ * - Should only be active in insert mode.
+ * - For recording deletes in insert mode.
+ */
+ function onKeyEventTargetKeyDown(e) {
+ var macroModeState = vimGlobalState.macroModeState;
+ var lastChange = macroModeState.lastInsertModeChanges;
+ var keyName = CodeMirror.keyName(e);
+ function onKeyFound() {
+ lastChange.changes.push(new InsertModeKey(keyName));
+ return true;
+ }
+ if (keyName.indexOf('Delete') != -1 || keyName.indexOf('Backspace') != -1) {
+ CodeMirror.lookupKey(keyName, ['vim-insert'], onKeyFound);
+ }
+ }
+
+ /**
+ * Repeats the last edit, which includes exactly 1 command and at most 1
+ * insert. Operator and motion commands are read from lastEditInputState,
+ * while action commands are read from lastEditActionCommand.
+ *
+ * If repeatForInsert is true, then the function was called by
+ * exitInsertMode to repeat the insert mode changes the user just made. The
+ * corresponding enterInsertMode call was made with a count.
+ */
+ function repeatLastEdit(cm, vim, repeat, repeatForInsert) {
+ var macroModeState = vimGlobalState.macroModeState;
+ macroModeState.inReplay = true;
+ var isAction = !!vim.lastEditActionCommand;
+ var cachedInputState = vim.inputState;
+ function repeatCommand() {
+ if (isAction) {
+ commandDispatcher.processAction(cm, vim, vim.lastEditActionCommand);
+ } else {
+ commandDispatcher.evalInput(cm, vim);
+ }
+ }
+ function repeatInsert(repeat) {
+ if (macroModeState.lastInsertModeChanges.changes.length > 0) {
+ // For some reason, repeat cw in desktop VIM will does not repeat
+ // insert mode changes. Will conform to that behavior.
+ repeat = !vim.lastEditActionCommand ? 1 : repeat;
+ repeatLastInsertModeChanges(cm, repeat, macroModeState);
+ }
+ }
+ vim.inputState = vim.lastEditInputState;
+ if (isAction && vim.lastEditActionCommand.interlaceInsertRepeat) {
+ // o and O repeat have to be interlaced with insert repeats so that the
+ // insertions appear on separate lines instead of the last line.
+ for (var i = 0; i < repeat; i++) {
+ repeatCommand();
+ repeatInsert(1);
+ }
+ } else {
+ if (!repeatForInsert) {
+ // Hack to get the cursor to end up at the right place. If I is
+ // repeated in insert mode repeat, cursor will be 1 insert
+ // change set left of where it should be.
+ repeatCommand();
+ }
+ repeatInsert(repeat);
+ }
+ vim.inputState = cachedInputState;
+ if (vim.insertMode && !repeatForInsert) {
+ // Don't exit insert mode twice. If repeatForInsert is set, then we
+ // were called by an exitInsertMode call lower on the stack.
+ exitInsertMode(cm);
+ }
+ macroModeState.inReplay = false;
+ };
+
+ function repeatLastInsertModeChanges(cm, repeat, macroModeState) {
+ var lastChange = macroModeState.lastInsertModeChanges;
+ function keyHandler(binding) {
+ if (typeof binding == 'string') {
+ CodeMirror.commands[binding](cm);
+ } else {
+ binding(cm);
+ }
+ return true;
+ }
+ for (var i = 0; i < repeat; i++) {
+ for (var j = 0; j < lastChange.changes.length; j++) {
+ var change = lastChange.changes[j];
+ if (change instanceof InsertModeKey) {
+ CodeMirror.lookupKey(change.keyName, ['vim-insert'], keyHandler);
+ } else {
+ var cur = cm.getCursor();
+ cm.replaceRange(change, cur, cur);
+ }
+ }
+ }
+ }
+
+ resetVimGlobalState();
+ return vimApi;
+ };
+ // Initialize Vim and make it available as an API.
+ CodeMirror.Vim = Vim();
+}
+)();
diff --git a/plugins/codemirror/codemirror/lib/codemirror.css b/plugins/codemirror/codemirror/lib/codemirror.css
new file mode 100644
index 0000000..23eaf74
--- /dev/null
+++ b/plugins/codemirror/codemirror/lib/codemirror.css
@@ -0,0 +1,263 @@
+/* BASICS */
+
+.CodeMirror {
+ /* Set height, width, borders, and global font properties here */
+ font-family: monospace;
+ height: 300px;
+}
+.CodeMirror-scroll {
+ /* Set scrolling behaviour here */
+ overflow: auto;
+}
+
+/* PADDING */
+
+.CodeMirror-lines {
+ padding: 4px 0; /* Vertical padding around content */
+}
+.CodeMirror pre {
+ padding: 0 4px; /* Horizontal padding of content */
+}
+
+.CodeMirror-scrollbar-filler, .CodeMirror-gutter-filler {
+ background-color: white; /* The little square between H and V scrollbars */
+}
+
+/* GUTTER */
+
+.CodeMirror-gutters {
+ border-right: 1px solid #ddd;
+ background-color: #f7f7f7;
+ white-space: nowrap;
+}
+.CodeMirror-linenumbers {}
+.CodeMirror-linenumber {
+ padding: 0 3px 0 5px;
+ min-width: 20px;
+ text-align: right;
+ color: #999;
+}
+
+/* CURSOR */
+
+.CodeMirror div.CodeMirror-cursor {
+ border-left: 1px solid black;
+ z-index: 3;
+}
+/* Shown when moving in bi-directional text */
+.CodeMirror div.CodeMirror-secondarycursor {
+ border-left: 1px solid silver;
+}
+.CodeMirror.cm-keymap-fat-cursor div.CodeMirror-cursor {
+ width: auto;
+ border: 0;
+ background: #7e7;
+ z-index: 1;
+}
+/* Can style cursor different in overwrite (non-insert) mode */
+.CodeMirror div.CodeMirror-cursor.CodeMirror-overwrite {}
+
+.cm-tab { display: inline-block; }
+
+/* DEFAULT THEME */
+
+.cm-s-default .cm-keyword {color: #708;}
+.cm-s-default .cm-atom {color: #219;}
+.cm-s-default .cm-number {color: #164;}
+.cm-s-default .cm-def {color: #00f;}
+.cm-s-default .cm-variable {color: black;}
+.cm-s-default .cm-variable-2 {color: #05a;}
+.cm-s-default .cm-variable-3 {color: #085;}
+.cm-s-default .cm-property {color: black;}
+.cm-s-default .cm-operator {color: black;}
+.cm-s-default .cm-comment {color: #a50;}
+.cm-s-default .cm-string {color: #a11;}
+.cm-s-default .cm-string-2 {color: #f50;}
+.cm-s-default .cm-meta {color: #555;}
+.cm-s-default .cm-qualifier {color: #555;}
+.cm-s-default .cm-builtin {color: #30a;}
+.cm-s-default .cm-bracket {color: #997;}
+.cm-s-default .cm-tag {color: #170;}
+.cm-s-default .cm-attribute {color: #00c;}
+.cm-s-default .cm-header {color: blue;}
+.cm-s-default .cm-quote {color: #090;}
+.cm-s-default .cm-hr {color: #999;}
+.cm-s-default .cm-link {color: #00c;}
+
+.cm-negative {color: #d44;}
+.cm-positive {color: #292;}
+.cm-header, .cm-strong {font-weight: bold;}
+.cm-em {font-style: italic;}
+.cm-link {text-decoration: underline;}
+
+.cm-s-default .cm-error {color: #f00;}
+.cm-invalidchar {color: #f00;}
+
+div.CodeMirror span.CodeMirror-matchingbracket {color: #0f0;}
+div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #f22;}
+.CodeMirror-activeline-background {background: #e8f2ff;}
+
+/* STOP */
+
+/* The rest of this file contains styles related to the mechanics of
+ the editor. You probably shouldn't touch them. */
+
+.CodeMirror {
+ line-height: 1;
+ position: relative;
+ overflow: hidden;
+ background: white;
+ color: black;
+}
+
+.CodeMirror-scroll {
+ /* 30px is the magic margin used to hide the element's real scrollbars */
+ /* See overflow: hidden in .CodeMirror */
+ margin-bottom: -30px; margin-right: -30px;
+ padding-bottom: 30px; padding-right: 30px;
+ height: 100%;
+ outline: none; /* Prevent dragging from highlighting the element */
+ position: relative;
+ -moz-box-sizing: content-box;
+ box-sizing: content-box;
+}
+.CodeMirror-sizer {
+ position: relative;
+}
+
+/* The fake, visible scrollbars. Used to force redraw during scrolling
+ before actuall scrolling happens, thus preventing shaking and
+ flickering artifacts. */
+.CodeMirror-vscrollbar, .CodeMirror-hscrollbar, .CodeMirror-scrollbar-filler, .CodeMirror-gutter-filler {
+ position: absolute;
+ z-index: 6;
+ display: none;
+}
+.CodeMirror-vscrollbar {
+ right: 0; top: 0;
+ overflow-x: hidden;
+ overflow-y: scroll;
+}
+.CodeMirror-hscrollbar {
+ bottom: 0; left: 0;
+ overflow-y: hidden;
+ overflow-x: scroll;
+}
+.CodeMirror-scrollbar-filler {
+ right: 0; bottom: 0;
+}
+.CodeMirror-gutter-filler {
+ left: 0; bottom: 0;
+}
+
+.CodeMirror-gutters {
+ position: absolute; left: 0; top: 0;
+ padding-bottom: 30px;
+ z-index: 3;
+}
+.CodeMirror-gutter {
+ white-space: normal;
+ height: 100%;
+ -moz-box-sizing: content-box;
+ box-sizing: content-box;
+ padding-bottom: 30px;
+ margin-bottom: -32px;
+ display: inline-block;
+ /* Hack to make IE7 behave */
+ *zoom:1;
+ *display:inline;
+}
+.CodeMirror-gutter-elt {
+ position: absolute;
+ cursor: default;
+ z-index: 4;
+}
+
+.CodeMirror-lines {
+ cursor: text;
+}
+.CodeMirror pre {
+ /* Reset some styles that the rest of the page might have set */
+ -moz-border-radius: 0; -webkit-border-radius: 0; border-radius: 0;
+ border-width: 0;
+ background: transparent;
+ font-family: inherit;
+ font-size: inherit;
+ margin: 0;
+ white-space: pre;
+ word-wrap: normal;
+ line-height: inherit;
+ color: inherit;
+ z-index: 2;
+ position: relative;
+ overflow: visible;
+}
+.CodeMirror-wrap pre {
+ word-wrap: break-word;
+ white-space: pre-wrap;
+ word-break: normal;
+}
+.CodeMirror-code pre {
+ border-right: 30px solid transparent;
+ width: -webkit-fit-content;
+ width: -moz-fit-content;
+ width: fit-content;
+}
+.CodeMirror-wrap .CodeMirror-code pre {
+ border-right: none;
+ width: auto;
+}
+.CodeMirror-linebackground {
+ position: absolute;
+ left: 0; right: 0; top: 0; bottom: 0;
+ z-index: 0;
+}
+
+.CodeMirror-linewidget {
+ position: relative;
+ z-index: 2;
+ overflow: auto;
+}
+
+.CodeMirror-widget {}
+
+.CodeMirror-wrap .CodeMirror-scroll {
+ overflow-x: hidden;
+}
+
+.CodeMirror-measure {
+ position: absolute;
+ width: 100%;
+ height: 0;
+ overflow: hidden;
+ visibility: hidden;
+}
+.CodeMirror-measure pre { position: static; }
+
+.CodeMirror div.CodeMirror-cursor {
+ position: absolute;
+ visibility: hidden;
+ border-right: none;
+ width: 0;
+}
+.CodeMirror-focused div.CodeMirror-cursor {
+ visibility: visible;
+}
+
+.CodeMirror-selected { background: #d9d9d9; }
+.CodeMirror-focused .CodeMirror-selected { background: #d7d4f0; }
+
+.cm-searching {
+ background: #ffa;
+ background: rgba(255, 255, 0, .4);
+}
+
+/* IE7 hack to prevent it from returning funny offsetTops on the spans */
+.CodeMirror span { *vertical-align: text-bottom; }
+
+@media print {
+ /* Hide the cursor when printing */
+ .CodeMirror div.CodeMirror-cursor {
+ visibility: hidden;
+ }
+}
diff --git a/plugins/codemirror/codemirror/lib/codemirror.js b/plugins/codemirror/codemirror/lib/codemirror.js
new file mode 100644
index 0000000..f8b2af5
--- /dev/null
+++ b/plugins/codemirror/codemirror/lib/codemirror.js
@@ -0,0 +1,5944 @@
+// CodeMirror version 3.20
+//
+// CodeMirror is the only global var we claim
+window.CodeMirror = (function() {
+ "use strict";
+
+ // BROWSER SNIFFING
+
+ // Crude, but necessary to handle a number of hard-to-feature-detect
+ // bugs and behavior differences.
+ var gecko = /gecko\/\d/i.test(navigator.userAgent);
+ // IE11 currently doesn't count as 'ie', since it has almost none of
+ // the same bugs as earlier versions. Use ie_gt10 to handle
+ // incompatibilities in that version.
+ var ie = /MSIE \d/.test(navigator.userAgent);
+ var ie_lt8 = ie && (document.documentMode == null || document.documentMode < 8);
+ var ie_lt9 = ie && (document.documentMode == null || document.documentMode < 9);
+ var ie_gt10 = /Trident\/([7-9]|\d{2,})\./.test(navigator.userAgent);
+ var webkit = /WebKit\//.test(navigator.userAgent);
+ var qtwebkit = webkit && /Qt\/\d+\.\d+/.test(navigator.userAgent);
+ var chrome = /Chrome\//.test(navigator.userAgent);
+ var opera = /Opera\//.test(navigator.userAgent);
+ var safari = /Apple Computer/.test(navigator.vendor);
+ var khtml = /KHTML\//.test(navigator.userAgent);
+ var mac_geLion = /Mac OS X 1\d\D([7-9]|\d\d)\D/.test(navigator.userAgent);
+ var mac_geMountainLion = /Mac OS X 1\d\D([8-9]|\d\d)\D/.test(navigator.userAgent);
+ var phantom = /PhantomJS/.test(navigator.userAgent);
+
+ var ios = /AppleWebKit/.test(navigator.userAgent) && /Mobile\/\w+/.test(navigator.userAgent);
+ // This is woefully incomplete. Suggestions for alternative methods welcome.
+ var mobile = ios || /Android|webOS|BlackBerry|Opera Mini|Opera Mobi|IEMobile/i.test(navigator.userAgent);
+ var mac = ios || /Mac/.test(navigator.platform);
+ var windows = /win/i.test(navigator.platform);
+
+ var opera_version = opera && navigator.userAgent.match(/Version\/(\d*\.\d*)/);
+ if (opera_version) opera_version = Number(opera_version[1]);
+ if (opera_version && opera_version >= 15) { opera = false; webkit = true; }
+ // Some browsers use the wrong event properties to signal cmd/ctrl on OS X
+ var flipCtrlCmd = mac && (qtwebkit || opera && (opera_version == null || opera_version < 12.11));
+ var captureMiddleClick = gecko || (ie && !ie_lt9);
+
+ // Optimize some code when these features are not used
+ var sawReadOnlySpans = false, sawCollapsedSpans = false;
+
+ // CONSTRUCTOR
+
+ function CodeMirror(place, options) {
+ if (!(this instanceof CodeMirror)) return new CodeMirror(place, options);
+
+ this.options = options = options || {};
+ // Determine effective options based on given values and defaults.
+ for (var opt in defaults) if (!options.hasOwnProperty(opt) && defaults.hasOwnProperty(opt))
+ options[opt] = defaults[opt];
+ setGuttersForLineNumbers(options);
+
+ var docStart = typeof options.value == "string" ? 0 : options.value.first;
+ var display = this.display = makeDisplay(place, docStart);
+ display.wrapper.CodeMirror = this;
+ updateGutters(this);
+ if (options.autofocus && !mobile) focusInput(this);
+
+ this.state = {keyMaps: [],
+ overlays: [],
+ modeGen: 0,
+ overwrite: false, focused: false,
+ suppressEdits: false, pasteIncoming: false,
+ draggingText: false,
+ highlight: new Delayed()};
+
+ themeChanged(this);
+ if (options.lineWrapping)
+ this.display.wrapper.className += " CodeMirror-wrap";
+
+ var doc = options.value;
+ if (typeof doc == "string") doc = new Doc(options.value, options.mode);
+ operation(this, attachDoc)(this, doc);
+
+ // Override magic textarea content restore that IE sometimes does
+ // on our hidden textarea on reload
+ if (ie) setTimeout(bind(resetInput, this, true), 20);
+
+ registerEventHandlers(this);
+ // IE throws unspecified error in certain cases, when
+ // trying to access activeElement before onload
+ var hasFocus; try { hasFocus = (document.activeElement == display.input); } catch(e) { }
+ if (hasFocus || (options.autofocus && !mobile)) setTimeout(bind(onFocus, this), 20);
+ else onBlur(this);
+
+ operation(this, function() {
+ for (var opt in optionHandlers)
+ if (optionHandlers.propertyIsEnumerable(opt))
+ optionHandlers[opt](this, options[opt], Init);
+ for (var i = 0; i < initHooks.length; ++i) initHooks[i](this);
+ })();
+ }
+
+ // DISPLAY CONSTRUCTOR
+
+ function makeDisplay(place, docStart) {
+ var d = {};
+
+ var input = d.input = elt("textarea", null, null, "position: absolute; padding: 0; width: 1px; height: 1em; outline: none; font-size: 4px;");
+ if (webkit) input.style.width = "1000px";
+ else input.setAttribute("wrap", "off");
+ // if border: 0; -- iOS fails to open keyboard (issue #1287)
+ if (ios) input.style.border = "1px solid black";
+ input.setAttribute("autocorrect", "off"); input.setAttribute("autocapitalize", "off"); input.setAttribute("spellcheck", "false");
+
+ // Wraps and hides input textarea
+ d.inputDiv = elt("div", [input], null, "overflow: hidden; position: relative; width: 3px; height: 0px;");
+ // The actual fake scrollbars.
+ d.scrollbarH = elt("div", [elt("div", null, null, "height: 1px")], "CodeMirror-hscrollbar");
+ d.scrollbarV = elt("div", [elt("div", null, null, "width: 1px")], "CodeMirror-vscrollbar");
+ d.scrollbarFiller = elt("div", null, "CodeMirror-scrollbar-filler");
+ d.gutterFiller = elt("div", null, "CodeMirror-gutter-filler");
+ // DIVs containing the selection and the actual code
+ d.lineDiv = elt("div", null, "CodeMirror-code");
+ d.selectionDiv = elt("div", null, null, "position: relative; z-index: 1");
+ // Blinky cursor, and element used to ensure cursor fits at the end of a line
+ d.cursor = elt("div", "\u00a0", "CodeMirror-cursor");
+ // Secondary cursor, shown when on a 'jump' in bi-directional text
+ d.otherCursor = elt("div", "\u00a0", "CodeMirror-cursor CodeMirror-secondarycursor");
+ // Used to measure text size
+ d.measure = elt("div", null, "CodeMirror-measure");
+ // Wraps everything that needs to exist inside the vertically-padded coordinate system
+ d.lineSpace = elt("div", [d.measure, d.selectionDiv, d.lineDiv, d.cursor, d.otherCursor],
+ null, "position: relative; outline: none");
+ // Moved around its parent to cover visible view
+ d.mover = elt("div", [elt("div", [d.lineSpace], "CodeMirror-lines")], null, "position: relative");
+ // Set to the height of the text, causes scrolling
+ d.sizer = elt("div", [d.mover], "CodeMirror-sizer");
+ // D is needed because behavior of elts with overflow: auto and padding is inconsistent across browsers
+ d.heightForcer = elt("div", null, null, "position: absolute; height: " + scrollerCutOff + "px; width: 1px;");
+ // Will contain the gutters, if any
+ d.gutters = elt("div", null, "CodeMirror-gutters");
+ d.lineGutter = null;
+ // Provides scrolling
+ d.scroller = elt("div", [d.sizer, d.heightForcer, d.gutters], "CodeMirror-scroll");
+ d.scroller.setAttribute("tabIndex", "-1");
+ // The element in which the editor lives.
+ d.wrapper = elt("div", [d.inputDiv, d.scrollbarH, d.scrollbarV,
+ d.scrollbarFiller, d.gutterFiller, d.scroller], "CodeMirror");
+ // Work around IE7 z-index bug
+ if (ie_lt8) { d.gutters.style.zIndex = -1; d.scroller.style.paddingRight = 0; }
+ if (place.appendChild) place.appendChild(d.wrapper); else place(d.wrapper);
+
+ // Needed to hide big blue blinking cursor on Mobile Safari
+ if (ios) input.style.width = "0px";
+ if (!webkit) d.scroller.draggable = true;
+ // Needed to handle Tab key in KHTML
+ if (khtml) { d.inputDiv.style.height = "1px"; d.inputDiv.style.position = "absolute"; }
+ // Need to set a minimum width to see the scrollbar on IE7 (but must not set it on IE8).
+ else if (ie_lt8) d.scrollbarH.style.minWidth = d.scrollbarV.style.minWidth = "18px";
+
+ // Current visible range (may be bigger than the view window).
+ d.viewOffset = d.lastSizeC = 0;
+ d.showingFrom = d.showingTo = docStart;
+
+ // Used to only resize the line number gutter when necessary (when
+ // the amount of lines crosses a boundary that makes its width change)
+ d.lineNumWidth = d.lineNumInnerWidth = d.lineNumChars = null;
+ // See readInput and resetInput
+ d.prevInput = "";
+ // Set to true when a non-horizontal-scrolling widget is added. As
+ // an optimization, widget aligning is skipped when d is false.
+ d.alignWidgets = false;
+ // Flag that indicates whether we currently expect input to appear
+ // (after some event like 'keypress' or 'input') and are polling
+ // intensively.
+ d.pollingFast = false;
+ // Self-resetting timeout for the poller
+ d.poll = new Delayed();
+
+ d.cachedCharWidth = d.cachedTextHeight = null;
+ d.measureLineCache = [];
+ d.measureLineCachePos = 0;
+
+ // Tracks when resetInput has punted to just putting a short
+ // string instead of the (large) selection.
+ d.inaccurateSelection = false;
+
+ // Tracks the maximum line length so that the horizontal scrollbar
+ // can be kept static when scrolling.
+ d.maxLine = null;
+ d.maxLineLength = 0;
+ d.maxLineChanged = false;
+
+ // Used for measuring wheel scrolling granularity
+ d.wheelDX = d.wheelDY = d.wheelStartX = d.wheelStartY = null;
+
+ return d;
+ }
+
+ // STATE UPDATES
+
+ // Used to get the editor into a consistent state again when options change.
+
+ function loadMode(cm) {
+ cm.doc.mode = CodeMirror.getMode(cm.options, cm.doc.modeOption);
+ cm.doc.iter(function(line) {
+ if (line.stateAfter) line.stateAfter = null;
+ if (line.styles) line.styles = null;
+ });
+ cm.doc.frontier = cm.doc.first;
+ startWorker(cm, 100);
+ cm.state.modeGen++;
+ if (cm.curOp) regChange(cm);
+ }
+
+ function wrappingChanged(cm) {
+ if (cm.options.lineWrapping) {
+ cm.display.wrapper.className += " CodeMirror-wrap";
+ cm.display.sizer.style.minWidth = "";
+ } else {
+ cm.display.wrapper.className = cm.display.wrapper.className.replace(" CodeMirror-wrap", "");
+ computeMaxLength(cm);
+ }
+ estimateLineHeights(cm);
+ regChange(cm);
+ clearCaches(cm);
+ setTimeout(function(){updateScrollbars(cm);}, 100);
+ }
+
+ function estimateHeight(cm) {
+ var th = textHeight(cm.display), wrapping = cm.options.lineWrapping;
+ var perLine = wrapping && Math.max(5, cm.display.scroller.clientWidth / charWidth(cm.display) - 3);
+ return function(line) {
+ if (lineIsHidden(cm.doc, line))
+ return 0;
+ else if (wrapping)
+ return (Math.ceil(line.text.length / perLine) || 1) * th;
+ else
+ return th;
+ };
+ }
+
+ function estimateLineHeights(cm) {
+ var doc = cm.doc, est = estimateHeight(cm);
+ doc.iter(function(line) {
+ var estHeight = est(line);
+ if (estHeight != line.height) updateLineHeight(line, estHeight);
+ });
+ }
+
+ function keyMapChanged(cm) {
+ var map = keyMap[cm.options.keyMap], style = map.style;
+ cm.display.wrapper.className = cm.display.wrapper.className.replace(/\s*cm-keymap-\S+/g, "") +
+ (style ? " cm-keymap-" + style : "");
+ cm.state.disableInput = map.disableInput;
+ }
+
+ function themeChanged(cm) {
+ cm.display.wrapper.className = cm.display.wrapper.className.replace(/\s*cm-s-\S+/g, "") +
+ cm.options.theme.replace(/(^|\s)\s*/g, " cm-s-");
+ clearCaches(cm);
+ }
+
+ function guttersChanged(cm) {
+ updateGutters(cm);
+ regChange(cm);
+ setTimeout(function(){alignHorizontally(cm);}, 20);
+ }
+
+ function updateGutters(cm) {
+ var gutters = cm.display.gutters, specs = cm.options.gutters;
+ removeChildren(gutters);
+ for (var i = 0; i < specs.length; ++i) {
+ var gutterClass = specs[i];
+ var gElt = gutters.appendChild(elt("div", null, "CodeMirror-gutter " + gutterClass));
+ if (gutterClass == "CodeMirror-linenumbers") {
+ cm.display.lineGutter = gElt;
+ gElt.style.width = (cm.display.lineNumWidth || 1) + "px";
+ }
+ }
+ gutters.style.display = i ? "" : "none";
+ }
+
+ function lineLength(doc, line) {
+ if (line.height == 0) return 0;
+ var len = line.text.length, merged, cur = line;
+ while (merged = collapsedSpanAtStart(cur)) {
+ var found = merged.find();
+ cur = getLine(doc, found.from.line);
+ len += found.from.ch - found.to.ch;
+ }
+ cur = line;
+ while (merged = collapsedSpanAtEnd(cur)) {
+ var found = merged.find();
+ len -= cur.text.length - found.from.ch;
+ cur = getLine(doc, found.to.line);
+ len += cur.text.length - found.to.ch;
+ }
+ return len;
+ }
+
+ function computeMaxLength(cm) {
+ var d = cm.display, doc = cm.doc;
+ d.maxLine = getLine(doc, doc.first);
+ d.maxLineLength = lineLength(doc, d.maxLine);
+ d.maxLineChanged = true;
+ doc.iter(function(line) {
+ var len = lineLength(doc, line);
+ if (len > d.maxLineLength) {
+ d.maxLineLength = len;
+ d.maxLine = line;
+ }
+ });
+ }
+
+ // Make sure the gutters options contains the element
+ // "CodeMirror-linenumbers" when the lineNumbers option is true.
+ function setGuttersForLineNumbers(options) {
+ var found = indexOf(options.gutters, "CodeMirror-linenumbers");
+ if (found == -1 && options.lineNumbers) {
+ options.gutters = options.gutters.concat(["CodeMirror-linenumbers"]);
+ } else if (found > -1 && !options.lineNumbers) {
+ options.gutters = options.gutters.slice(0);
+ options.gutters.splice(found, 1);
+ }
+ }
+
+ // SCROLLBARS
+
+ // Re-synchronize the fake scrollbars with the actual size of the
+ // content. Optionally force a scrollTop.
+ function updateScrollbars(cm) {
+ var d = cm.display, docHeight = cm.doc.height;
+ var totalHeight = docHeight + paddingVert(d);
+ d.sizer.style.minHeight = d.heightForcer.style.top = totalHeight + "px";
+ d.gutters.style.height = Math.max(totalHeight, d.scroller.clientHeight - scrollerCutOff) + "px";
+ var scrollHeight = Math.max(totalHeight, d.scroller.scrollHeight);
+ var needsH = d.scroller.scrollWidth > (d.scroller.clientWidth + 1);
+ var needsV = scrollHeight > (d.scroller.clientHeight + 1);
+ if (needsV) {
+ d.scrollbarV.style.display = "block";
+ d.scrollbarV.style.bottom = needsH ? scrollbarWidth(d.measure) + "px" : "0";
+ d.scrollbarV.firstChild.style.height =
+ (scrollHeight - d.scroller.clientHeight + d.scrollbarV.clientHeight) + "px";
+ } else {
+ d.scrollbarV.style.display = "";
+ d.scrollbarV.firstChild.style.height = "0";
+ }
+ if (needsH) {
+ d.scrollbarH.style.display = "block";
+ d.scrollbarH.style.right = needsV ? scrollbarWidth(d.measure) + "px" : "0";
+ d.scrollbarH.firstChild.style.width =
+ (d.scroller.scrollWidth - d.scroller.clientWidth + d.scrollbarH.clientWidth) + "px";
+ } else {
+ d.scrollbarH.style.display = "";
+ d.scrollbarH.firstChild.style.width = "0";
+ }
+ if (needsH && needsV) {
+ d.scrollbarFiller.style.display = "block";
+ d.scrollbarFiller.style.height = d.scrollbarFiller.style.width = scrollbarWidth(d.measure) + "px";
+ } else d.scrollbarFiller.style.display = "";
+ if (needsH && cm.options.coverGutterNextToScrollbar && cm.options.fixedGutter) {
+ d.gutterFiller.style.display = "block";
+ d.gutterFiller.style.height = scrollbarWidth(d.measure) + "px";
+ d.gutterFiller.style.width = d.gutters.offsetWidth + "px";
+ } else d.gutterFiller.style.display = "";
+
+ if (mac_geLion && scrollbarWidth(d.measure) === 0) {
+ d.scrollbarV.style.minWidth = d.scrollbarH.style.minHeight = mac_geMountainLion ? "18px" : "12px";
+ d.scrollbarV.style.pointerEvents = d.scrollbarH.style.pointerEvents = "none";
+ }
+ }
+
+ function visibleLines(display, doc, viewPort) {
+ var top = display.scroller.scrollTop, height = display.wrapper.clientHeight;
+ if (typeof viewPort == "number") top = viewPort;
+ else if (viewPort) {top = viewPort.top; height = viewPort.bottom - viewPort.top;}
+ top = Math.floor(top - paddingTop(display));
+ var bottom = Math.ceil(top + height);
+ return {from: lineAtHeight(doc, top), to: lineAtHeight(doc, bottom)};
+ }
+
+ // LINE NUMBERS
+
+ function alignHorizontally(cm) {
+ var display = cm.display;
+ if (!display.alignWidgets && (!display.gutters.firstChild || !cm.options.fixedGutter)) return;
+ var comp = compensateForHScroll(display) - display.scroller.scrollLeft + cm.doc.scrollLeft;
+ var gutterW = display.gutters.offsetWidth, l = comp + "px";
+ for (var n = display.lineDiv.firstChild; n; n = n.nextSibling) if (n.alignable) {
+ for (var i = 0, a = n.alignable; i < a.length; ++i) a[i].style.left = l;
+ }
+ if (cm.options.fixedGutter)
+ display.gutters.style.left = (comp + gutterW) + "px";
+ }
+
+ function maybeUpdateLineNumberWidth(cm) {
+ if (!cm.options.lineNumbers) return false;
+ var doc = cm.doc, last = lineNumberFor(cm.options, doc.first + doc.size - 1), display = cm.display;
+ if (last.length != display.lineNumChars) {
+ var test = display.measure.appendChild(elt("div", [elt("div", last)],
+ "CodeMirror-linenumber CodeMirror-gutter-elt"));
+ var innerW = test.firstChild.offsetWidth, padding = test.offsetWidth - innerW;
+ display.lineGutter.style.width = "";
+ display.lineNumInnerWidth = Math.max(innerW, display.lineGutter.offsetWidth - padding);
+ display.lineNumWidth = display.lineNumInnerWidth + padding;
+ display.lineNumChars = display.lineNumInnerWidth ? last.length : -1;
+ display.lineGutter.style.width = display.lineNumWidth + "px";
+ return true;
+ }
+ return false;
+ }
+
+ function lineNumberFor(options, i) {
+ return String(options.lineNumberFormatter(i + options.firstLineNumber));
+ }
+ function compensateForHScroll(display) {
+ return getRect(display.scroller).left - getRect(display.sizer).left;
+ }
+
+ // DISPLAY DRAWING
+
+ function updateDisplay(cm, changes, viewPort, forced) {
+ var oldFrom = cm.display.showingFrom, oldTo = cm.display.showingTo, updated;
+ var visible = visibleLines(cm.display, cm.doc, viewPort);
+ for (var first = true;; first = false) {
+ var oldWidth = cm.display.scroller.clientWidth;
+ if (!updateDisplayInner(cm, changes, visible, forced)) break;
+ updated = true;
+ changes = [];
+ updateSelection(cm);
+ updateScrollbars(cm);
+ if (first && cm.options.lineWrapping && oldWidth != cm.display.scroller.clientWidth) {
+ forced = true;
+ continue;
+ }
+ forced = false;
+
+ // Clip forced viewport to actual scrollable area
+ if (viewPort)
+ viewPort = Math.min(cm.display.scroller.scrollHeight - cm.display.scroller.clientHeight,
+ typeof viewPort == "number" ? viewPort : viewPort.top);
+ visible = visibleLines(cm.display, cm.doc, viewPort);
+ if (visible.from >= cm.display.showingFrom && visible.to <= cm.display.showingTo)
+ break;
+ }
+
+ if (updated) {
+ signalLater(cm, "update", cm);
+ if (cm.display.showingFrom != oldFrom || cm.display.showingTo != oldTo)
+ signalLater(cm, "viewportChange", cm, cm.display.showingFrom, cm.display.showingTo);
+ }
+ return updated;
+ }
+
+ // Uses a set of changes plus the current scroll position to
+ // determine which DOM updates have to be made, and makes the
+ // updates.
+ function updateDisplayInner(cm, changes, visible, forced) {
+ var display = cm.display, doc = cm.doc;
+ if (!display.wrapper.clientWidth) {
+ display.showingFrom = display.showingTo = doc.first;
+ display.viewOffset = 0;
+ return;
+ }
+
+ // Bail out if the visible area is already rendered and nothing changed.
+ if (!forced && changes.length == 0 &&
+ visible.from > display.showingFrom && visible.to < display.showingTo)
+ return;
+
+ if (maybeUpdateLineNumberWidth(cm))
+ changes = [{from: doc.first, to: doc.first + doc.size}];
+ var gutterW = display.sizer.style.marginLeft = display.gutters.offsetWidth + "px";
+ display.scrollbarH.style.left = cm.options.fixedGutter ? gutterW : "0";
+
+ // Used to determine which lines need their line numbers updated
+ var positionsChangedFrom = Infinity;
+ if (cm.options.lineNumbers)
+ for (var i = 0; i < changes.length; ++i)
+ if (changes[i].diff && changes[i].from < positionsChangedFrom) { positionsChangedFrom = changes[i].from; }
+
+ var end = doc.first + doc.size;
+ var from = Math.max(visible.from - cm.options.viewportMargin, doc.first);
+ var to = Math.min(end, visible.to + cm.options.viewportMargin);
+ if (display.showingFrom < from && from - display.showingFrom < 20) from = Math.max(doc.first, display.showingFrom);
+ if (display.showingTo > to && display.showingTo - to < 20) to = Math.min(end, display.showingTo);
+ if (sawCollapsedSpans) {
+ from = lineNo(visualLine(doc, getLine(doc, from)));
+ while (to < end && lineIsHidden(doc, getLine(doc, to))) ++to;
+ }
+
+ // Create a range of theoretically intact lines, and punch holes
+ // in that using the change info.
+ var intact = [{from: Math.max(display.showingFrom, doc.first),
+ to: Math.min(display.showingTo, end)}];
+ if (intact[0].from >= intact[0].to) intact = [];
+ else intact = computeIntact(intact, changes);
+ // When merged lines are present, we might have to reduce the
+ // intact ranges because changes in continued fragments of the
+ // intact lines do require the lines to be redrawn.
+ if (sawCollapsedSpans)
+ for (var i = 0; i < intact.length; ++i) {
+ var range = intact[i], merged;
+ while (merged = collapsedSpanAtEnd(getLine(doc, range.to - 1))) {
+ var newTo = merged.find().from.line;
+ if (newTo > range.from) range.to = newTo;
+ else { intact.splice(i--, 1); break; }
+ }
+ }
+
+ // Clip off the parts that won't be visible
+ var intactLines = 0;
+ for (var i = 0; i < intact.length; ++i) {
+ var range = intact[i];
+ if (range.from < from) range.from = from;
+ if (range.to > to) range.to = to;
+ if (range.from >= range.to) intact.splice(i--, 1);
+ else intactLines += range.to - range.from;
+ }
+ if (!forced && intactLines == to - from && from == display.showingFrom && to == display.showingTo) {
+ updateViewOffset(cm);
+ return;
+ }
+ intact.sort(function(a, b) {return a.from - b.from;});
+
+ // Avoid crashing on IE's "unspecified error" when in iframes
+ try {
+ var focused = document.activeElement;
+ } catch(e) {}
+ if (intactLines < (to - from) * .7) display.lineDiv.style.display = "none";
+ patchDisplay(cm, from, to, intact, positionsChangedFrom);
+ display.lineDiv.style.display = "";
+ if (focused && document.activeElement != focused && focused.offsetHeight) focused.focus();
+
+ var different = from != display.showingFrom || to != display.showingTo ||
+ display.lastSizeC != display.wrapper.clientHeight;
+ // This is just a bogus formula that detects when the editor is
+ // resized or the font size changes.
+ if (different) {
+ display.lastSizeC = display.wrapper.clientHeight;
+ startWorker(cm, 400);
+ }
+ display.showingFrom = from; display.showingTo = to;
+
+ updateHeightsInViewport(cm);
+ updateViewOffset(cm);
+
+ return true;
+ }
+
+ function updateHeightsInViewport(cm) {
+ var display = cm.display;
+ var prevBottom = display.lineDiv.offsetTop;
+ for (var node = display.lineDiv.firstChild, height; node; node = node.nextSibling) if (node.lineObj) {
+ if (ie_lt8) {
+ var bot = node.offsetTop + node.offsetHeight;
+ height = bot - prevBottom;
+ prevBottom = bot;
+ } else {
+ var box = getRect(node);
+ height = box.bottom - box.top;
+ }
+ var diff = node.lineObj.height - height;
+ if (height < 2) height = textHeight(display);
+ if (diff > .001 || diff < -.001) {
+ updateLineHeight(node.lineObj, height);
+ var widgets = node.lineObj.widgets;
+ if (widgets) for (var i = 0; i < widgets.length; ++i)
+ widgets[i].height = widgets[i].node.offsetHeight;
+ }
+ }
+ }
+
+ function updateViewOffset(cm) {
+ var off = cm.display.viewOffset = heightAtLine(cm, getLine(cm.doc, cm.display.showingFrom));
+ // Position the mover div to align with the current virtual scroll position
+ cm.display.mover.style.top = off + "px";
+ }
+
+ function computeIntact(intact, changes) {
+ for (var i = 0, l = changes.length || 0; i < l; ++i) {
+ var change = changes[i], intact2 = [], diff = change.diff || 0;
+ for (var j = 0, l2 = intact.length; j < l2; ++j) {
+ var range = intact[j];
+ if (change.to <= range.from && change.diff) {
+ intact2.push({from: range.from + diff, to: range.to + diff});
+ } else if (change.to <= range.from || change.from >= range.to) {
+ intact2.push(range);
+ } else {
+ if (change.from > range.from)
+ intact2.push({from: range.from, to: change.from});
+ if (change.to < range.to)
+ intact2.push({from: change.to + diff, to: range.to + diff});
+ }
+ }
+ intact = intact2;
+ }
+ return intact;
+ }
+
+ function getDimensions(cm) {
+ var d = cm.display, left = {}, width = {};
+ for (var n = d.gutters.firstChild, i = 0; n; n = n.nextSibling, ++i) {
+ left[cm.options.gutters[i]] = n.offsetLeft;
+ width[cm.options.gutters[i]] = n.offsetWidth;
+ }
+ return {fixedPos: compensateForHScroll(d),
+ gutterTotalWidth: d.gutters.offsetWidth,
+ gutterLeft: left,
+ gutterWidth: width,
+ wrapperWidth: d.wrapper.clientWidth};
+ }
+
+ function patchDisplay(cm, from, to, intact, updateNumbersFrom) {
+ var dims = getDimensions(cm);
+ var display = cm.display, lineNumbers = cm.options.lineNumbers;
+ if (!intact.length && (!webkit || !cm.display.currentWheelTarget))
+ removeChildren(display.lineDiv);
+ var container = display.lineDiv, cur = container.firstChild;
+
+ function rm(node) {
+ var next = node.nextSibling;
+ if (webkit && mac && cm.display.currentWheelTarget == node) {
+ node.style.display = "none";
+ node.lineObj = null;
+ } else {
+ node.parentNode.removeChild(node);
+ }
+ return next;
+ }
+
+ var nextIntact = intact.shift(), lineN = from;
+ cm.doc.iter(from, to, function(line) {
+ if (nextIntact && nextIntact.to == lineN) nextIntact = intact.shift();
+ if (lineIsHidden(cm.doc, line)) {
+ if (line.height != 0) updateLineHeight(line, 0);
+ if (line.widgets && cur && cur.previousSibling) for (var i = 0; i < line.widgets.length; ++i) {
+ var w = line.widgets[i];
+ if (w.showIfHidden) {
+ var prev = cur.previousSibling;
+ if (/pre/i.test(prev.nodeName)) {
+ var wrap = elt("div", null, null, "position: relative");
+ prev.parentNode.replaceChild(wrap, prev);
+ wrap.appendChild(prev);
+ prev = wrap;
+ }
+ var wnode = prev.appendChild(elt("div", [w.node], "CodeMirror-linewidget"));
+ if (!w.handleMouseEvents) wnode.ignoreEvents = true;
+ positionLineWidget(w, wnode, prev, dims);
+ }
+ }
+ } else if (nextIntact && nextIntact.from <= lineN && nextIntact.to > lineN) {
+ // This line is intact. Skip to the actual node. Update its
+ // line number if needed.
+ while (cur.lineObj != line) cur = rm(cur);
+ if (lineNumbers && updateNumbersFrom <= lineN && cur.lineNumber)
+ setTextContent(cur.lineNumber, lineNumberFor(cm.options, lineN));
+ cur = cur.nextSibling;
+ } else {
+ // For lines with widgets, make an attempt to find and reuse
+ // the existing element, so that widgets aren't needlessly
+ // removed and re-inserted into the dom
+ if (line.widgets) for (var j = 0, search = cur, reuse; search && j < 20; ++j, search = search.nextSibling)
+ if (search.lineObj == line && /div/i.test(search.nodeName)) { reuse = search; break; }
+ // This line needs to be generated.
+ var lineNode = buildLineElement(cm, line, lineN, dims, reuse);
+ if (lineNode != reuse) {
+ container.insertBefore(lineNode, cur);
+ } else {
+ while (cur != reuse) cur = rm(cur);
+ cur = cur.nextSibling;
+ }
+
+ lineNode.lineObj = line;
+ }
+ ++lineN;
+ });
+ while (cur) cur = rm(cur);
+ }
+
+ function buildLineElement(cm, line, lineNo, dims, reuse) {
+ var built = buildLineContent(cm, line), lineElement = built.pre;
+ var markers = line.gutterMarkers, display = cm.display, wrap;
+
+ var bgClass = built.bgClass ? built.bgClass + " " + (line.bgClass || "") : line.bgClass;
+ if (!cm.options.lineNumbers && !markers && !bgClass && !line.wrapClass && !line.widgets)
+ return lineElement;
+
+ // Lines with gutter elements, widgets or a background class need
+ // to be wrapped again, and have the extra elements added to the
+ // wrapper div
+
+ if (reuse) {
+ reuse.alignable = null;
+ var isOk = true, widgetsSeen = 0, insertBefore = null;
+ for (var n = reuse.firstChild, next; n; n = next) {
+ next = n.nextSibling;
+ if (!/\bCodeMirror-linewidget\b/.test(n.className)) {
+ reuse.removeChild(n);
+ } else {
+ for (var i = 0; i < line.widgets.length; ++i) {
+ var widget = line.widgets[i];
+ if (widget.node == n.firstChild) {
+ if (!widget.above && !insertBefore) insertBefore = n;
+ positionLineWidget(widget, n, reuse, dims);
+ ++widgetsSeen;
+ break;
+ }
+ }
+ if (i == line.widgets.length) { isOk = false; break; }
+ }
+ }
+ reuse.insertBefore(lineElement, insertBefore);
+ if (isOk && widgetsSeen == line.widgets.length) {
+ wrap = reuse;
+ reuse.className = line.wrapClass || "";
+ }
+ }
+ if (!wrap) {
+ wrap = elt("div", null, line.wrapClass, "position: relative");
+ wrap.appendChild(lineElement);
+ }
+ // Kludge to make sure the styled element lies behind the selection (by z-index)
+ if (bgClass)
+ wrap.insertBefore(elt("div", null, bgClass + " CodeMirror-linebackground"), wrap.firstChild);
+ if (cm.options.lineNumbers || markers) {
+ var gutterWrap = wrap.insertBefore(elt("div", null, null, "position: absolute; left: " +
+ (cm.options.fixedGutter ? dims.fixedPos : -dims.gutterTotalWidth) + "px"),
+ wrap.firstChild);
+ if (cm.options.fixedGutter) (wrap.alignable || (wrap.alignable = [])).push(gutterWrap);
+ if (cm.options.lineNumbers && (!markers || !markers["CodeMirror-linenumbers"]))
+ wrap.lineNumber = gutterWrap.appendChild(
+ elt("div", lineNumberFor(cm.options, lineNo),
+ "CodeMirror-linenumber CodeMirror-gutter-elt",
+ "left: " + dims.gutterLeft["CodeMirror-linenumbers"] + "px; width: "
+ + display.lineNumInnerWidth + "px"));
+ if (markers)
+ for (var k = 0; k < cm.options.gutters.length; ++k) {
+ var id = cm.options.gutters[k], found = markers.hasOwnProperty(id) && markers[id];
+ if (found)
+ gutterWrap.appendChild(elt("div", [found], "CodeMirror-gutter-elt", "left: " +
+ dims.gutterLeft[id] + "px; width: " + dims.gutterWidth[id] + "px"));
+ }
+ }
+ if (ie_lt8) wrap.style.zIndex = 2;
+ if (line.widgets && wrap != reuse) for (var i = 0, ws = line.widgets; i < ws.length; ++i) {
+ var widget = ws[i], node = elt("div", [widget.node], "CodeMirror-linewidget");
+ if (!widget.handleMouseEvents) node.ignoreEvents = true;
+ positionLineWidget(widget, node, wrap, dims);
+ if (widget.above)
+ wrap.insertBefore(node, cm.options.lineNumbers && line.height != 0 ? gutterWrap : lineElement);
+ else
+ wrap.appendChild(node);
+ signalLater(widget, "redraw");
+ }
+ return wrap;
+ }
+
+ function positionLineWidget(widget, node, wrap, dims) {
+ if (widget.noHScroll) {
+ (wrap.alignable || (wrap.alignable = [])).push(node);
+ var width = dims.wrapperWidth;
+ node.style.left = dims.fixedPos + "px";
+ if (!widget.coverGutter) {
+ width -= dims.gutterTotalWidth;
+ node.style.paddingLeft = dims.gutterTotalWidth + "px";
+ }
+ node.style.width = width + "px";
+ }
+ if (widget.coverGutter) {
+ node.style.zIndex = 5;
+ node.style.position = "relative";
+ if (!widget.noHScroll) node.style.marginLeft = -dims.gutterTotalWidth + "px";
+ }
+ }
+
+ // SELECTION / CURSOR
+
+ function updateSelection(cm) {
+ var display = cm.display;
+ var collapsed = posEq(cm.doc.sel.from, cm.doc.sel.to);
+ if (collapsed || cm.options.showCursorWhenSelecting)
+ updateSelectionCursor(cm);
+ else
+ display.cursor.style.display = display.otherCursor.style.display = "none";
+ if (!collapsed)
+ updateSelectionRange(cm);
+ else
+ display.selectionDiv.style.display = "none";
+
+ // Move the hidden textarea near the cursor to prevent scrolling artifacts
+ if (cm.options.moveInputWithCursor) {
+ var headPos = cursorCoords(cm, cm.doc.sel.head, "div");
+ var wrapOff = getRect(display.wrapper), lineOff = getRect(display.lineDiv);
+ display.inputDiv.style.top = Math.max(0, Math.min(display.wrapper.clientHeight - 10,
+ headPos.top + lineOff.top - wrapOff.top)) + "px";
+ display.inputDiv.style.left = Math.max(0, Math.min(display.wrapper.clientWidth - 10,
+ headPos.left + lineOff.left - wrapOff.left)) + "px";
+ }
+ }
+
+ // No selection, plain cursor
+ function updateSelectionCursor(cm) {
+ var display = cm.display, pos = cursorCoords(cm, cm.doc.sel.head, "div");
+ display.cursor.style.left = pos.left + "px";
+ display.cursor.style.top = pos.top + "px";
+ display.cursor.style.height = Math.max(0, pos.bottom - pos.top) * cm.options.cursorHeight + "px";
+ display.cursor.style.display = "";
+
+ if (pos.other) {
+ display.otherCursor.style.display = "";
+ display.otherCursor.style.left = pos.other.left + "px";
+ display.otherCursor.style.top = pos.other.top + "px";
+ display.otherCursor.style.height = (pos.other.bottom - pos.other.top) * .85 + "px";
+ } else { display.otherCursor.style.display = "none"; }
+ }
+
+ // Highlight selection
+ function updateSelectionRange(cm) {
+ var display = cm.display, doc = cm.doc, sel = cm.doc.sel;
+ var fragment = document.createDocumentFragment();
+ var clientWidth = display.lineSpace.offsetWidth, pl = paddingLeft(cm.display);
+
+ function add(left, top, width, bottom) {
+ if (top < 0) top = 0;
+ fragment.appendChild(elt("div", null, "CodeMirror-selected", "position: absolute; left: " + left +
+ "px; top: " + top + "px; width: " + (width == null ? clientWidth - left : width) +
+ "px; height: " + (bottom - top) + "px"));
+ }
+
+ function drawForLine(line, fromArg, toArg) {
+ var lineObj = getLine(doc, line);
+ var lineLen = lineObj.text.length;
+ var start, end;
+ function coords(ch, bias) {
+ return charCoords(cm, Pos(line, ch), "div", lineObj, bias);
+ }
+
+ iterateBidiSections(getOrder(lineObj), fromArg || 0, toArg == null ? lineLen : toArg, function(from, to, dir) {
+ var leftPos = coords(from, "left"), rightPos, left, right;
+ if (from == to) {
+ rightPos = leftPos;
+ left = right = leftPos.left;
+ } else {
+ rightPos = coords(to - 1, "right");
+ if (dir == "rtl") { var tmp = leftPos; leftPos = rightPos; rightPos = tmp; }
+ left = leftPos.left;
+ right = rightPos.right;
+ }
+ if (fromArg == null && from == 0) left = pl;
+ if (rightPos.top - leftPos.top > 3) { // Different lines, draw top part
+ add(left, leftPos.top, null, leftPos.bottom);
+ left = pl;
+ if (leftPos.bottom < rightPos.top) add(left, leftPos.bottom, null, rightPos.top);
+ }
+ if (toArg == null && to == lineLen) right = clientWidth;
+ if (!start || leftPos.top < start.top || leftPos.top == start.top && leftPos.left < start.left)
+ start = leftPos;
+ if (!end || rightPos.bottom > end.bottom || rightPos.bottom == end.bottom && rightPos.right > end.right)
+ end = rightPos;
+ if (left < pl + 1) left = pl;
+ add(left, rightPos.top, right - left, rightPos.bottom);
+ });
+ return {start: start, end: end};
+ }
+
+ if (sel.from.line == sel.to.line) {
+ drawForLine(sel.from.line, sel.from.ch, sel.to.ch);
+ } else {
+ var fromLine = getLine(doc, sel.from.line), toLine = getLine(doc, sel.to.line);
+ var singleVLine = visualLine(doc, fromLine) == visualLine(doc, toLine);
+ var leftEnd = drawForLine(sel.from.line, sel.from.ch, singleVLine ? fromLine.text.length : null).end;
+ var rightStart = drawForLine(sel.to.line, singleVLine ? 0 : null, sel.to.ch).start;
+ if (singleVLine) {
+ if (leftEnd.top < rightStart.top - 2) {
+ add(leftEnd.right, leftEnd.top, null, leftEnd.bottom);
+ add(pl, rightStart.top, rightStart.left, rightStart.bottom);
+ } else {
+ add(leftEnd.right, leftEnd.top, rightStart.left - leftEnd.right, leftEnd.bottom);
+ }
+ }
+ if (leftEnd.bottom < rightStart.top)
+ add(pl, leftEnd.bottom, null, rightStart.top);
+ }
+
+ removeChildrenAndAdd(display.selectionDiv, fragment);
+ display.selectionDiv.style.display = "";
+ }
+
+ // Cursor-blinking
+ function restartBlink(cm) {
+ if (!cm.state.focused) return;
+ var display = cm.display;
+ clearInterval(display.blinker);
+ var on = true;
+ display.cursor.style.visibility = display.otherCursor.style.visibility = "";
+ if (cm.options.cursorBlinkRate > 0)
+ display.blinker = setInterval(function() {
+ display.cursor.style.visibility = display.otherCursor.style.visibility = (on = !on) ? "" : "hidden";
+ }, cm.options.cursorBlinkRate);
+ }
+
+ // HIGHLIGHT WORKER
+
+ function startWorker(cm, time) {
+ if (cm.doc.mode.startState && cm.doc.frontier < cm.display.showingTo)
+ cm.state.highlight.set(time, bind(highlightWorker, cm));
+ }
+
+ function highlightWorker(cm) {
+ var doc = cm.doc;
+ if (doc.frontier < doc.first) doc.frontier = doc.first;
+ if (doc.frontier >= cm.display.showingTo) return;
+ var end = +new Date + cm.options.workTime;
+ var state = copyState(doc.mode, getStateBefore(cm, doc.frontier));
+ var changed = [], prevChange;
+ doc.iter(doc.frontier, Math.min(doc.first + doc.size, cm.display.showingTo + 500), function(line) {
+ if (doc.frontier >= cm.display.showingFrom) { // Visible
+ var oldStyles = line.styles;
+ line.styles = highlightLine(cm, line, state, true);
+ var ischange = !oldStyles || oldStyles.length != line.styles.length;
+ for (var i = 0; !ischange && i < oldStyles.length; ++i) ischange = oldStyles[i] != line.styles[i];
+ if (ischange) {
+ if (prevChange && prevChange.end == doc.frontier) prevChange.end++;
+ else changed.push(prevChange = {start: doc.frontier, end: doc.frontier + 1});
+ }
+ line.stateAfter = copyState(doc.mode, state);
+ } else {
+ processLine(cm, line.text, state);
+ line.stateAfter = doc.frontier % 5 == 0 ? copyState(doc.mode, state) : null;
+ }
+ ++doc.frontier;
+ if (+new Date > end) {
+ startWorker(cm, cm.options.workDelay);
+ return true;
+ }
+ });
+ if (changed.length)
+ operation(cm, function() {
+ for (var i = 0; i < changed.length; ++i)
+ regChange(this, changed[i].start, changed[i].end);
+ })();
+ }
+
+ // Finds the line to start with when starting a parse. Tries to
+ // find a line with a stateAfter, so that it can start with a
+ // valid state. If that fails, it returns the line with the
+ // smallest indentation, which tends to need the least context to
+ // parse correctly.
+ function findStartLine(cm, n, precise) {
+ var minindent, minline, doc = cm.doc;
+ var lim = precise ? -1 : n - (cm.doc.mode.innerMode ? 1000 : 100);
+ for (var search = n; search > lim; --search) {
+ if (search <= doc.first) return doc.first;
+ var line = getLine(doc, search - 1);
+ if (line.stateAfter && (!precise || search <= doc.frontier)) return search;
+ var indented = countColumn(line.text, null, cm.options.tabSize);
+ if (minline == null || minindent > indented) {
+ minline = search - 1;
+ minindent = indented;
+ }
+ }
+ return minline;
+ }
+
+ function getStateBefore(cm, n, precise) {
+ var doc = cm.doc, display = cm.display;
+ if (!doc.mode.startState) return true;
+ var pos = findStartLine(cm, n, precise), state = pos > doc.first && getLine(doc, pos-1).stateAfter;
+ if (!state) state = startState(doc.mode);
+ else state = copyState(doc.mode, state);
+ doc.iter(pos, n, function(line) {
+ processLine(cm, line.text, state);
+ var save = pos == n - 1 || pos % 5 == 0 || pos >= display.showingFrom && pos < display.showingTo;
+ line.stateAfter = save ? copyState(doc.mode, state) : null;
+ ++pos;
+ });
+ if (precise) doc.frontier = pos;
+ return state;
+ }
+
+ // POSITION MEASUREMENT
+
+ function paddingTop(display) {return display.lineSpace.offsetTop;}
+ function paddingVert(display) {return display.mover.offsetHeight - display.lineSpace.offsetHeight;}
+ function paddingLeft(display) {
+ var e = removeChildrenAndAdd(display.measure, elt("pre", null, null, "text-align: left")).appendChild(elt("span", "x"));
+ return e.offsetLeft;
+ }
+
+ function measureChar(cm, line, ch, data, bias) {
+ var dir = -1;
+ data = data || measureLine(cm, line);
+ if (data.crude) {
+ var left = data.left + ch * data.width;
+ return {left: left, right: left + data.width, top: data.top, bottom: data.bottom};
+ }
+
+ for (var pos = ch;; pos += dir) {
+ var r = data[pos];
+ if (r) break;
+ if (dir < 0 && pos == 0) dir = 1;
+ }
+ bias = pos > ch ? "left" : pos < ch ? "right" : bias;
+ if (bias == "left" && r.leftSide) r = r.leftSide;
+ else if (bias == "right" && r.rightSide) r = r.rightSide;
+ return {left: pos < ch ? r.right : r.left,
+ right: pos > ch ? r.left : r.right,
+ top: r.top,
+ bottom: r.bottom};
+ }
+
+ function findCachedMeasurement(cm, line) {
+ var cache = cm.display.measureLineCache;
+ for (var i = 0; i < cache.length; ++i) {
+ var memo = cache[i];
+ if (memo.text == line.text && memo.markedSpans == line.markedSpans &&
+ cm.display.scroller.clientWidth == memo.width &&
+ memo.classes == line.textClass + "|" + line.wrapClass)
+ return memo;
+ }
+ }
+
+ function clearCachedMeasurement(cm, line) {
+ var exists = findCachedMeasurement(cm, line);
+ if (exists) exists.text = exists.measure = exists.markedSpans = null;
+ }
+
+ function measureLine(cm, line) {
+ // First look in the cache
+ var cached = findCachedMeasurement(cm, line);
+ if (cached) return cached.measure;
+
+ // Failing that, recompute and store result in cache
+ var measure = measureLineInner(cm, line);
+ var cache = cm.display.measureLineCache;
+ var memo = {text: line.text, width: cm.display.scroller.clientWidth,
+ markedSpans: line.markedSpans, measure: measure,
+ classes: line.textClass + "|" + line.wrapClass};
+ if (cache.length == 16) cache[++cm.display.measureLineCachePos % 16] = memo;
+ else cache.push(memo);
+ return measure;
+ }
+
+ function measureLineInner(cm, line) {
+ if (!cm.options.lineWrapping && line.text.length >= cm.options.crudeMeasuringFrom)
+ return crudelyMeasureLine(cm, line);
+
+ var display = cm.display, measure = emptyArray(line.text.length);
+ var pre = buildLineContent(cm, line, measure, true).pre;
+
+ // IE does not cache element positions of inline elements between
+ // calls to getBoundingClientRect. This makes the loop below,
+ // which gathers the positions of all the characters on the line,
+ // do an amount of layout work quadratic to the number of
+ // characters. When line wrapping is off, we try to improve things
+ // by first subdividing the line into a bunch of inline blocks, so
+ // that IE can reuse most of the layout information from caches
+ // for those blocks. This does interfere with line wrapping, so it
+ // doesn't work when wrapping is on, but in that case the
+ // situation is slightly better, since IE does cache line-wrapping
+ // information and only recomputes per-line.
+ if (ie && !ie_lt8 && !cm.options.lineWrapping && pre.childNodes.length > 100) {
+ var fragment = document.createDocumentFragment();
+ var chunk = 10, n = pre.childNodes.length;
+ for (var i = 0, chunks = Math.ceil(n / chunk); i < chunks; ++i) {
+ var wrap = elt("div", null, null, "display: inline-block");
+ for (var j = 0; j < chunk && n; ++j) {
+ wrap.appendChild(pre.firstChild);
+ --n;
+ }
+ fragment.appendChild(wrap);
+ }
+ pre.appendChild(fragment);
+ }
+
+ removeChildrenAndAdd(display.measure, pre);
+
+ var outer = getRect(display.lineDiv);
+ var vranges = [], data = emptyArray(line.text.length), maxBot = pre.offsetHeight;
+ // Work around an IE7/8 bug where it will sometimes have randomly
+ // replaced our pre with a clone at this point.
+ if (ie_lt9 && display.measure.first != pre)
+ removeChildrenAndAdd(display.measure, pre);
+
+ function measureRect(rect) {
+ var top = rect.top - outer.top, bot = rect.bottom - outer.top;
+ if (bot > maxBot) bot = maxBot;
+ if (top < 0) top = 0;
+ for (var i = vranges.length - 2; i >= 0; i -= 2) {
+ var rtop = vranges[i], rbot = vranges[i+1];
+ if (rtop > bot || rbot < top) continue;
+ if (rtop <= top && rbot >= bot ||
+ top <= rtop && bot >= rbot ||
+ Math.min(bot, rbot) - Math.max(top, rtop) >= (bot - top) >> 1) {
+ vranges[i] = Math.min(top, rtop);
+ vranges[i+1] = Math.max(bot, rbot);
+ break;
+ }
+ }
+ if (i < 0) { i = vranges.length; vranges.push(top, bot); }
+ return {left: rect.left - outer.left,
+ right: rect.right - outer.left,
+ top: i, bottom: null};
+ }
+ function finishRect(rect) {
+ rect.bottom = vranges[rect.top+1];
+ rect.top = vranges[rect.top];
+ }
+
+ for (var i = 0, cur; i < measure.length; ++i) if (cur = measure[i]) {
+ var node = cur, rect = null;
+ // A widget might wrap, needs special care
+ if (/\bCodeMirror-widget\b/.test(cur.className) && cur.getClientRects) {
+ if (cur.firstChild.nodeType == 1) node = cur.firstChild;
+ var rects = node.getClientRects();
+ if (rects.length > 1) {
+ rect = data[i] = measureRect(rects[0]);
+ rect.rightSide = measureRect(rects[rects.length - 1]);
+ }
+ }
+ if (!rect) rect = data[i] = measureRect(getRect(node));
+ if (cur.measureRight) rect.right = getRect(cur.measureRight).left;
+ if (cur.leftSide) rect.leftSide = measureRect(getRect(cur.leftSide));
+ }
+ removeChildren(cm.display.measure);
+ for (var i = 0, cur; i < data.length; ++i) if (cur = data[i]) {
+ finishRect(cur);
+ if (cur.leftSide) finishRect(cur.leftSide);
+ if (cur.rightSide) finishRect(cur.rightSide);
+ }
+ return data;
+ }
+
+ function crudelyMeasureLine(cm, line) {
+ var copy = new Line(line.text.slice(0, 100), null);
+ if (line.textClass) copy.textClass = line.textClass;
+ var measure = measureLineInner(cm, copy);
+ var left = measureChar(cm, copy, 0, measure, "left");
+ var right = measureChar(cm, copy, 99, measure, "right");
+ return {crude: true, top: left.top, left: left.left, bottom: left.bottom, width: (right.right - left.left) / 100};
+ }
+
+ function measureLineWidth(cm, line) {
+ var hasBadSpan = false;
+ if (line.markedSpans) for (var i = 0; i < line.markedSpans; ++i) {
+ var sp = line.markedSpans[i];
+ if (sp.collapsed && (sp.to == null || sp.to == line.text.length)) hasBadSpan = true;
+ }
+ var cached = !hasBadSpan && findCachedMeasurement(cm, line);
+ if (cached || line.text.length >= cm.options.crudeMeasuringFrom)
+ return measureChar(cm, line, line.text.length, cached && cached.measure, "right").right;
+
+ var pre = buildLineContent(cm, line, null, true).pre;
+ var end = pre.appendChild(zeroWidthElement(cm.display.measure));
+ removeChildrenAndAdd(cm.display.measure, pre);
+ return getRect(end).right - getRect(cm.display.lineDiv).left;
+ }
+
+ function clearCaches(cm) {
+ cm.display.measureLineCache.length = cm.display.measureLineCachePos = 0;
+ cm.display.cachedCharWidth = cm.display.cachedTextHeight = null;
+ if (!cm.options.lineWrapping) cm.display.maxLineChanged = true;
+ cm.display.lineNumChars = null;
+ }
+
+ function pageScrollX() { return window.pageXOffset || (document.documentElement || document.body).scrollLeft; }
+ function pageScrollY() { return window.pageYOffset || (document.documentElement || document.body).scrollTop; }
+
+ // Context is one of "line", "div" (display.lineDiv), "local"/null (editor), or "page"
+ function intoCoordSystem(cm, lineObj, rect, context) {
+ if (lineObj.widgets) for (var i = 0; i < lineObj.widgets.length; ++i) if (lineObj.widgets[i].above) {
+ var size = widgetHeight(lineObj.widgets[i]);
+ rect.top += size; rect.bottom += size;
+ }
+ if (context == "line") return rect;
+ if (!context) context = "local";
+ var yOff = heightAtLine(cm, lineObj);
+ if (context == "local") yOff += paddingTop(cm.display);
+ else yOff -= cm.display.viewOffset;
+ if (context == "page" || context == "window") {
+ var lOff = getRect(cm.display.lineSpace);
+ yOff += lOff.top + (context == "window" ? 0 : pageScrollY());
+ var xOff = lOff.left + (context == "window" ? 0 : pageScrollX());
+ rect.left += xOff; rect.right += xOff;
+ }
+ rect.top += yOff; rect.bottom += yOff;
+ return rect;
+ }
+
+ // Context may be "window", "page", "div", or "local"/null
+ // Result is in "div" coords
+ function fromCoordSystem(cm, coords, context) {
+ if (context == "div") return coords;
+ var left = coords.left, top = coords.top;
+ // First move into "page" coordinate system
+ if (context == "page") {
+ left -= pageScrollX();
+ top -= pageScrollY();
+ } else if (context == "local" || !context) {
+ var localBox = getRect(cm.display.sizer);
+ left += localBox.left;
+ top += localBox.top;
+ }
+
+ var lineSpaceBox = getRect(cm.display.lineSpace);
+ return {left: left - lineSpaceBox.left, top: top - lineSpaceBox.top};
+ }
+
+ function charCoords(cm, pos, context, lineObj, bias) {
+ if (!lineObj) lineObj = getLine(cm.doc, pos.line);
+ return intoCoordSystem(cm, lineObj, measureChar(cm, lineObj, pos.ch, null, bias), context);
+ }
+
+ function cursorCoords(cm, pos, context, lineObj, measurement) {
+ lineObj = lineObj || getLine(cm.doc, pos.line);
+ if (!measurement) measurement = measureLine(cm, lineObj);
+ function get(ch, right) {
+ var m = measureChar(cm, lineObj, ch, measurement, right ? "right" : "left");
+ if (right) m.left = m.right; else m.right = m.left;
+ return intoCoordSystem(cm, lineObj, m, context);
+ }
+ function getBidi(ch, partPos) {
+ var part = order[partPos], right = part.level % 2;
+ if (ch == bidiLeft(part) && partPos && part.level < order[partPos - 1].level) {
+ part = order[--partPos];
+ ch = bidiRight(part) - (part.level % 2 ? 0 : 1);
+ right = true;
+ } else if (ch == bidiRight(part) && partPos < order.length - 1 && part.level < order[partPos + 1].level) {
+ part = order[++partPos];
+ ch = bidiLeft(part) - part.level % 2;
+ right = false;
+ }
+ if (right && ch == part.to && ch > part.from) return get(ch - 1);
+ return get(ch, right);
+ }
+ var order = getOrder(lineObj), ch = pos.ch;
+ if (!order) return get(ch);
+ var partPos = getBidiPartAt(order, ch);
+ var val = getBidi(ch, partPos);
+ if (bidiOther != null) val.other = getBidi(ch, bidiOther);
+ return val;
+ }
+
+ function PosWithInfo(line, ch, outside, xRel) {
+ var pos = new Pos(line, ch);
+ pos.xRel = xRel;
+ if (outside) pos.outside = true;
+ return pos;
+ }
+
+ // Coords must be lineSpace-local
+ function coordsChar(cm, x, y) {
+ var doc = cm.doc;
+ y += cm.display.viewOffset;
+ if (y < 0) return PosWithInfo(doc.first, 0, true, -1);
+ var lineNo = lineAtHeight(doc, y), last = doc.first + doc.size - 1;
+ if (lineNo > last)
+ return PosWithInfo(doc.first + doc.size - 1, getLine(doc, last).text.length, true, 1);
+ if (x < 0) x = 0;
+
+ for (;;) {
+ var lineObj = getLine(doc, lineNo);
+ var found = coordsCharInner(cm, lineObj, lineNo, x, y);
+ var merged = collapsedSpanAtEnd(lineObj);
+ var mergedPos = merged && merged.find();
+ if (merged && (found.ch > mergedPos.from.ch || found.ch == mergedPos.from.ch && found.xRel > 0))
+ lineNo = mergedPos.to.line;
+ else
+ return found;
+ }
+ }
+
+ function coordsCharInner(cm, lineObj, lineNo, x, y) {
+ var innerOff = y - heightAtLine(cm, lineObj);
+ var wrongLine = false, adjust = 2 * cm.display.wrapper.clientWidth;
+ var measurement = measureLine(cm, lineObj);
+
+ function getX(ch) {
+ var sp = cursorCoords(cm, Pos(lineNo, ch), "line",
+ lineObj, measurement);
+ wrongLine = true;
+ if (innerOff > sp.bottom) return sp.left - adjust;
+ else if (innerOff < sp.top) return sp.left + adjust;
+ else wrongLine = false;
+ return sp.left;
+ }
+
+ var bidi = getOrder(lineObj), dist = lineObj.text.length;
+ var from = lineLeft(lineObj), to = lineRight(lineObj);
+ var fromX = getX(from), fromOutside = wrongLine, toX = getX(to), toOutside = wrongLine;
+
+ if (x > toX) return PosWithInfo(lineNo, to, toOutside, 1);
+ // Do a binary search between these bounds.
+ for (;;) {
+ if (bidi ? to == from || to == moveVisually(lineObj, from, 1) : to - from <= 1) {
+ var ch = x < fromX || x - fromX <= toX - x ? from : to;
+ var xDiff = x - (ch == from ? fromX : toX);
+ while (isExtendingChar.test(lineObj.text.charAt(ch))) ++ch;
+ var pos = PosWithInfo(lineNo, ch, ch == from ? fromOutside : toOutside,
+ xDiff < 0 ? -1 : xDiff ? 1 : 0);
+ return pos;
+ }
+ var step = Math.ceil(dist / 2), middle = from + step;
+ if (bidi) {
+ middle = from;
+ for (var i = 0; i < step; ++i) middle = moveVisually(lineObj, middle, 1);
+ }
+ var middleX = getX(middle);
+ if (middleX > x) {to = middle; toX = middleX; if (toOutside = wrongLine) toX += 1000; dist = step;}
+ else {from = middle; fromX = middleX; fromOutside = wrongLine; dist -= step;}
+ }
+ }
+
+ var measureText;
+ function textHeight(display) {
+ if (display.cachedTextHeight != null) return display.cachedTextHeight;
+ if (measureText == null) {
+ measureText = elt("pre");
+ // Measure a bunch of lines, for browsers that compute
+ // fractional heights.
+ for (var i = 0; i < 49; ++i) {
+ measureText.appendChild(document.createTextNode("x"));
+ measureText.appendChild(elt("br"));
+ }
+ measureText.appendChild(document.createTextNode("x"));
+ }
+ removeChildrenAndAdd(display.measure, measureText);
+ var height = measureText.offsetHeight / 50;
+ if (height > 3) display.cachedTextHeight = height;
+ removeChildren(display.measure);
+ return height || 1;
+ }
+
+ function charWidth(display) {
+ if (display.cachedCharWidth != null) return display.cachedCharWidth;
+ var anchor = elt("span", "x");
+ var pre = elt("pre", [anchor]);
+ removeChildrenAndAdd(display.measure, pre);
+ var width = anchor.offsetWidth;
+ if (width > 2) display.cachedCharWidth = width;
+ return width || 10;
+ }
+
+ // OPERATIONS
+
+ // Operations are used to wrap changes in such a way that each
+ // change won't have to update the cursor and display (which would
+ // be awkward, slow, and error-prone), but instead updates are
+ // batched and then all combined and executed at once.
+
+ var nextOpId = 0;
+ function startOperation(cm) {
+ cm.curOp = {
+ // An array of ranges of lines that have to be updated. See
+ // updateDisplay.
+ changes: [],
+ forceUpdate: false,
+ updateInput: null,
+ userSelChange: null,
+ textChanged: null,
+ selectionChanged: false,
+ cursorActivity: false,
+ updateMaxLine: false,
+ updateScrollPos: false,
+ id: ++nextOpId
+ };
+ if (!delayedCallbackDepth++) delayedCallbacks = [];
+ }
+
+ function endOperation(cm) {
+ var op = cm.curOp, doc = cm.doc, display = cm.display;
+ cm.curOp = null;
+
+ if (op.updateMaxLine) computeMaxLength(cm);
+ if (display.maxLineChanged && !cm.options.lineWrapping && display.maxLine) {
+ var width = measureLineWidth(cm, display.maxLine);
+ display.sizer.style.minWidth = Math.max(0, width + 3 + scrollerCutOff) + "px";
+ display.maxLineChanged = false;
+ var maxScrollLeft = Math.max(0, display.sizer.offsetLeft + display.sizer.offsetWidth - display.scroller.clientWidth);
+ if (maxScrollLeft < doc.scrollLeft && !op.updateScrollPos)
+ setScrollLeft(cm, Math.min(display.scroller.scrollLeft, maxScrollLeft), true);
+ }
+ var newScrollPos, updated;
+ if (op.updateScrollPos) {
+ newScrollPos = op.updateScrollPos;
+ } else if (op.selectionChanged && display.scroller.clientHeight) { // don't rescroll if not visible
+ var coords = cursorCoords(cm, doc.sel.head);
+ newScrollPos = calculateScrollPos(cm, coords.left, coords.top, coords.left, coords.bottom);
+ }
+ if (op.changes.length || op.forceUpdate || newScrollPos && newScrollPos.scrollTop != null) {
+ updated = updateDisplay(cm, op.changes, newScrollPos && newScrollPos.scrollTop, op.forceUpdate);
+ if (cm.display.scroller.offsetHeight) cm.doc.scrollTop = cm.display.scroller.scrollTop;
+ }
+ if (!updated && op.selectionChanged) updateSelection(cm);
+ if (op.updateScrollPos) {
+ var top = Math.max(0, Math.min(display.scroller.scrollHeight - display.scroller.clientHeight, newScrollPos.scrollTop));
+ var left = Math.max(0, Math.min(display.scroller.scrollWidth - display.scroller.clientWidth, newScrollPos.scrollLeft));
+ display.scroller.scrollTop = display.scrollbarV.scrollTop = doc.scrollTop = top;
+ display.scroller.scrollLeft = display.scrollbarH.scrollLeft = doc.scrollLeft = left;
+ alignHorizontally(cm);
+ if (op.scrollToPos)
+ scrollPosIntoView(cm, clipPos(cm.doc, op.scrollToPos.from),
+ clipPos(cm.doc, op.scrollToPos.to), op.scrollToPos.margin);
+ } else if (newScrollPos) {
+ scrollCursorIntoView(cm);
+ }
+ if (op.selectionChanged) restartBlink(cm);
+
+ if (cm.state.focused && op.updateInput)
+ resetInput(cm, op.userSelChange);
+
+ var hidden = op.maybeHiddenMarkers, unhidden = op.maybeUnhiddenMarkers;
+ if (hidden) for (var i = 0; i < hidden.length; ++i)
+ if (!hidden[i].lines.length) signal(hidden[i], "hide");
+ if (unhidden) for (var i = 0; i < unhidden.length; ++i)
+ if (unhidden[i].lines.length) signal(unhidden[i], "unhide");
+
+ var delayed;
+ if (!--delayedCallbackDepth) {
+ delayed = delayedCallbacks;
+ delayedCallbacks = null;
+ }
+ if (op.textChanged)
+ signal(cm, "change", cm, op.textChanged);
+ if (op.cursorActivity) signal(cm, "cursorActivity", cm);
+ if (delayed) for (var i = 0; i < delayed.length; ++i) delayed[i]();
+ }
+
+ // Wraps a function in an operation. Returns the wrapped function.
+ function operation(cm1, f) {
+ return function() {
+ var cm = cm1 || this, withOp = !cm.curOp;
+ if (withOp) startOperation(cm);
+ try { var result = f.apply(cm, arguments); }
+ finally { if (withOp) endOperation(cm); }
+ return result;
+ };
+ }
+ function docOperation(f) {
+ return function() {
+ var withOp = this.cm && !this.cm.curOp, result;
+ if (withOp) startOperation(this.cm);
+ try { result = f.apply(this, arguments); }
+ finally { if (withOp) endOperation(this.cm); }
+ return result;
+ };
+ }
+ function runInOp(cm, f) {
+ var withOp = !cm.curOp, result;
+ if (withOp) startOperation(cm);
+ try { result = f(); }
+ finally { if (withOp) endOperation(cm); }
+ return result;
+ }
+
+ function regChange(cm, from, to, lendiff) {
+ if (from == null) from = cm.doc.first;
+ if (to == null) to = cm.doc.first + cm.doc.size;
+ cm.curOp.changes.push({from: from, to: to, diff: lendiff});
+ }
+
+ // INPUT HANDLING
+
+ function slowPoll(cm) {
+ if (cm.display.pollingFast) return;
+ cm.display.poll.set(cm.options.pollInterval, function() {
+ readInput(cm);
+ if (cm.state.focused) slowPoll(cm);
+ });
+ }
+
+ function fastPoll(cm) {
+ var missed = false;
+ cm.display.pollingFast = true;
+ function p() {
+ var changed = readInput(cm);
+ if (!changed && !missed) {missed = true; cm.display.poll.set(60, p);}
+ else {cm.display.pollingFast = false; slowPoll(cm);}
+ }
+ cm.display.poll.set(20, p);
+ }
+
+ // prevInput is a hack to work with IME. If we reset the textarea
+ // on every change, that breaks IME. So we look for changes
+ // compared to the previous content instead. (Modern browsers have
+ // events that indicate IME taking place, but these are not widely
+ // supported or compatible enough yet to rely on.)
+ function readInput(cm) {
+ var input = cm.display.input, prevInput = cm.display.prevInput, doc = cm.doc, sel = doc.sel;
+ if (!cm.state.focused || hasSelection(input) || isReadOnly(cm) || cm.state.disableInput) return false;
+ if (cm.state.pasteIncoming && cm.state.fakedLastChar) {
+ input.value = input.value.substring(0, input.value.length - 1);
+ cm.state.fakedLastChar = false;
+ }
+ var text = input.value;
+ if (text == prevInput && posEq(sel.from, sel.to)) return false;
+ if (ie && !ie_lt9 && cm.display.inputHasSelection === text) {
+ resetInput(cm, true);
+ return false;
+ }
+
+ var withOp = !cm.curOp;
+ if (withOp) startOperation(cm);
+ sel.shift = false;
+ var same = 0, l = Math.min(prevInput.length, text.length);
+ while (same < l && prevInput.charCodeAt(same) == text.charCodeAt(same)) ++same;
+ var from = sel.from, to = sel.to;
+ if (same < prevInput.length)
+ from = Pos(from.line, from.ch - (prevInput.length - same));
+ else if (cm.state.overwrite && posEq(from, to) && !cm.state.pasteIncoming)
+ to = Pos(to.line, Math.min(getLine(doc, to.line).text.length, to.ch + (text.length - same)));
+
+ var updateInput = cm.curOp.updateInput;
+ var changeEvent = {from: from, to: to, text: splitLines(text.slice(same)),
+ origin: cm.state.pasteIncoming ? "paste" : "+input"};
+ makeChange(cm.doc, changeEvent, "end");
+ cm.curOp.updateInput = updateInput;
+ signalLater(cm, "inputRead", cm, changeEvent);
+
+ if (text.length > 1000 || text.indexOf("\n") > -1) input.value = cm.display.prevInput = "";
+ else cm.display.prevInput = text;
+ if (withOp) endOperation(cm);
+ cm.state.pasteIncoming = false;
+ return true;
+ }
+
+ function resetInput(cm, user) {
+ var minimal, selected, doc = cm.doc;
+ if (!posEq(doc.sel.from, doc.sel.to)) {
+ cm.display.prevInput = "";
+ minimal = hasCopyEvent &&
+ (doc.sel.to.line - doc.sel.from.line > 100 || (selected = cm.getSelection()).length > 1000);
+ var content = minimal ? "-" : selected || cm.getSelection();
+ cm.display.input.value = content;
+ if (cm.state.focused) selectInput(cm.display.input);
+ if (ie && !ie_lt9) cm.display.inputHasSelection = content;
+ } else if (user) {
+ cm.display.prevInput = cm.display.input.value = "";
+ if (ie && !ie_lt9) cm.display.inputHasSelection = null;
+ }
+ cm.display.inaccurateSelection = minimal;
+ }
+
+ function focusInput(cm) {
+ if (cm.options.readOnly != "nocursor" && (!mobile || document.activeElement != cm.display.input))
+ cm.display.input.focus();
+ }
+
+ function isReadOnly(cm) {
+ return cm.options.readOnly || cm.doc.cantEdit;
+ }
+
+ // EVENT HANDLERS
+
+ function registerEventHandlers(cm) {
+ var d = cm.display;
+ on(d.scroller, "mousedown", operation(cm, onMouseDown));
+ if (ie)
+ on(d.scroller, "dblclick", operation(cm, function(e) {
+ if (signalDOMEvent(cm, e)) return;
+ var pos = posFromMouse(cm, e);
+ if (!pos || clickInGutter(cm, e) || eventInWidget(cm.display, e)) return;
+ e_preventDefault(e);
+ var word = findWordAt(getLine(cm.doc, pos.line).text, pos);
+ extendSelection(cm.doc, word.from, word.to);
+ }));
+ else
+ on(d.scroller, "dblclick", function(e) { signalDOMEvent(cm, e) || e_preventDefault(e); });
+ on(d.lineSpace, "selectstart", function(e) {
+ if (!eventInWidget(d, e)) e_preventDefault(e);
+ });
+ // Gecko browsers fire contextmenu *after* opening the menu, at
+ // which point we can't mess with it anymore. Context menu is
+ // handled in onMouseDown for Gecko.
+ if (!captureMiddleClick) on(d.scroller, "contextmenu", function(e) {onContextMenu(cm, e);});
+
+ on(d.scroller, "scroll", function() {
+ if (d.scroller.clientHeight) {
+ setScrollTop(cm, d.scroller.scrollTop);
+ setScrollLeft(cm, d.scroller.scrollLeft, true);
+ signal(cm, "scroll", cm);
+ }
+ });
+ on(d.scrollbarV, "scroll", function() {
+ if (d.scroller.clientHeight) setScrollTop(cm, d.scrollbarV.scrollTop);
+ });
+ on(d.scrollbarH, "scroll", function() {
+ if (d.scroller.clientHeight) setScrollLeft(cm, d.scrollbarH.scrollLeft);
+ });
+
+ on(d.scroller, "mousewheel", function(e){onScrollWheel(cm, e);});
+ on(d.scroller, "DOMMouseScroll", function(e){onScrollWheel(cm, e);});
+
+ function reFocus() { if (cm.state.focused) setTimeout(bind(focusInput, cm), 0); }
+ on(d.scrollbarH, "mousedown", reFocus);
+ on(d.scrollbarV, "mousedown", reFocus);
+ // Prevent wrapper from ever scrolling
+ on(d.wrapper, "scroll", function() { d.wrapper.scrollTop = d.wrapper.scrollLeft = 0; });
+
+ var resizeTimer;
+ function onResize() {
+ if (resizeTimer == null) resizeTimer = setTimeout(function() {
+ resizeTimer = null;
+ // Might be a text scaling operation, clear size caches.
+ d.cachedCharWidth = d.cachedTextHeight = knownScrollbarWidth = null;
+ clearCaches(cm);
+ runInOp(cm, bind(regChange, cm));
+ }, 100);
+ }
+ on(window, "resize", onResize);
+ // Above handler holds on to the editor and its data structures.
+ // Here we poll to unregister it when the editor is no longer in
+ // the document, so that it can be garbage-collected.
+ function unregister() {
+ for (var p = d.wrapper.parentNode; p && p != document.body; p = p.parentNode) {}
+ if (p) setTimeout(unregister, 5000);
+ else off(window, "resize", onResize);
+ }
+ setTimeout(unregister, 5000);
+
+ on(d.input, "keyup", operation(cm, function(e) {
+ if (signalDOMEvent(cm, e) || cm.options.onKeyEvent && cm.options.onKeyEvent(cm, addStop(e))) return;
+ if (e.keyCode == 16) cm.doc.sel.shift = false;
+ }));
+ on(d.input, "input", function() {
+ if (ie && !ie_lt9 && cm.display.inputHasSelection) cm.display.inputHasSelection = null;
+ fastPoll(cm);
+ });
+ on(d.input, "keydown", operation(cm, onKeyDown));
+ on(d.input, "keypress", operation(cm, onKeyPress));
+ on(d.input, "focus", bind(onFocus, cm));
+ on(d.input, "blur", bind(onBlur, cm));
+
+ function drag_(e) {
+ if (signalDOMEvent(cm, e) || cm.options.onDragEvent && cm.options.onDragEvent(cm, addStop(e))) return;
+ e_stop(e);
+ }
+ if (cm.options.dragDrop) {
+ on(d.scroller, "dragstart", function(e){onDragStart(cm, e);});
+ on(d.scroller, "dragenter", drag_);
+ on(d.scroller, "dragover", drag_);
+ on(d.scroller, "drop", operation(cm, onDrop));
+ }
+ on(d.scroller, "paste", function(e) {
+ if (eventInWidget(d, e)) return;
+ focusInput(cm);
+ fastPoll(cm);
+ });
+ on(d.input, "paste", function() {
+ // Workaround for webkit bug https://bugs.webkit.org/show_bug.cgi?id=90206
+ // Add a char to the end of textarea before paste occur so that
+ // selection doesn't span to the end of textarea.
+ if (webkit && !cm.state.fakedLastChar && !(new Date - cm.state.lastMiddleDown < 200)) {
+ var start = d.input.selectionStart, end = d.input.selectionEnd;
+ d.input.value += "$";
+ d.input.selectionStart = start;
+ d.input.selectionEnd = end;
+ cm.state.fakedLastChar = true;
+ }
+ cm.state.pasteIncoming = true;
+ fastPoll(cm);
+ });
+
+ function prepareCopy() {
+ if (d.inaccurateSelection) {
+ d.prevInput = "";
+ d.inaccurateSelection = false;
+ d.input.value = cm.getSelection();
+ selectInput(d.input);
+ }
+ }
+ on(d.input, "cut", prepareCopy);
+ on(d.input, "copy", prepareCopy);
+
+ // Needed to handle Tab key in KHTML
+ if (khtml) on(d.sizer, "mouseup", function() {
+ if (document.activeElement == d.input) d.input.blur();
+ focusInput(cm);
+ });
+ }
+
+ function eventInWidget(display, e) {
+ for (var n = e_target(e); n != display.wrapper; n = n.parentNode) {
+ if (!n || n.ignoreEvents || n.parentNode == display.sizer && n != display.mover) return true;
+ }
+ }
+
+ function posFromMouse(cm, e, liberal) {
+ var display = cm.display;
+ if (!liberal) {
+ var target = e_target(e);
+ if (target == display.scrollbarH || target == display.scrollbarH.firstChild ||
+ target == display.scrollbarV || target == display.scrollbarV.firstChild ||
+ target == display.scrollbarFiller || target == display.gutterFiller) return null;
+ }
+ var x, y, space = getRect(display.lineSpace);
+ // Fails unpredictably on IE[67] when mouse is dragged around quickly.
+ try { x = e.clientX; y = e.clientY; } catch (e) { return null; }
+ return coordsChar(cm, x - space.left, y - space.top);
+ }
+
+ var lastClick, lastDoubleClick;
+ function onMouseDown(e) {
+ if (signalDOMEvent(this, e)) return;
+ var cm = this, display = cm.display, doc = cm.doc, sel = doc.sel;
+ sel.shift = e.shiftKey;
+
+ if (eventInWidget(display, e)) {
+ if (!webkit) {
+ display.scroller.draggable = false;
+ setTimeout(function(){display.scroller.draggable = true;}, 100);
+ }
+ return;
+ }
+ if (clickInGutter(cm, e)) return;
+ var start = posFromMouse(cm, e);
+
+ switch (e_button(e)) {
+ case 3:
+ if (captureMiddleClick) onContextMenu.call(cm, cm, e);
+ return;
+ case 2:
+ if (webkit) cm.state.lastMiddleDown = +new Date;
+ if (start) extendSelection(cm.doc, start);
+ setTimeout(bind(focusInput, cm), 20);
+ e_preventDefault(e);
+ return;
+ }
+ // For button 1, if it was clicked inside the editor
+ // (posFromMouse returning non-null), we have to adjust the
+ // selection.
+ if (!start) {if (e_target(e) == display.scroller) e_preventDefault(e); return;}
+
+ if (!cm.state.focused) onFocus(cm);
+
+ var now = +new Date, type = "single";
+ if (lastDoubleClick && lastDoubleClick.time > now - 400 && posEq(lastDoubleClick.pos, start)) {
+ type = "triple";
+ e_preventDefault(e);
+ setTimeout(bind(focusInput, cm), 20);
+ selectLine(cm, start.line);
+ } else if (lastClick && lastClick.time > now - 400 && posEq(lastClick.pos, start)) {
+ type = "double";
+ lastDoubleClick = {time: now, pos: start};
+ e_preventDefault(e);
+ var word = findWordAt(getLine(doc, start.line).text, start);
+ extendSelection(cm.doc, word.from, word.to);
+ } else { lastClick = {time: now, pos: start}; }
+
+ var last = start;
+ if (cm.options.dragDrop && dragAndDrop && !isReadOnly(cm) && !posEq(sel.from, sel.to) &&
+ !posLess(start, sel.from) && !posLess(sel.to, start) && type == "single") {
+ var dragEnd = operation(cm, function(e2) {
+ if (webkit) display.scroller.draggable = false;
+ cm.state.draggingText = false;
+ off(document, "mouseup", dragEnd);
+ off(display.scroller, "drop", dragEnd);
+ if (Math.abs(e.clientX - e2.clientX) + Math.abs(e.clientY - e2.clientY) < 10) {
+ e_preventDefault(e2);
+ extendSelection(cm.doc, start);
+ focusInput(cm);
+ }
+ });
+ // Let the drag handler handle this.
+ if (webkit) display.scroller.draggable = true;
+ cm.state.draggingText = dragEnd;
+ // IE's approach to draggable
+ if (display.scroller.dragDrop) display.scroller.dragDrop();
+ on(document, "mouseup", dragEnd);
+ on(display.scroller, "drop", dragEnd);
+ return;
+ }
+ e_preventDefault(e);
+ if (type == "single") extendSelection(cm.doc, clipPos(doc, start));
+
+ var startstart = sel.from, startend = sel.to, lastPos = start;
+
+ function doSelect(cur) {
+ if (posEq(lastPos, cur)) return;
+ lastPos = cur;
+
+ if (type == "single") {
+ extendSelection(cm.doc, clipPos(doc, start), cur);
+ return;
+ }
+
+ startstart = clipPos(doc, startstart);
+ startend = clipPos(doc, startend);
+ if (type == "double") {
+ var word = findWordAt(getLine(doc, cur.line).text, cur);
+ if (posLess(cur, startstart)) extendSelection(cm.doc, word.from, startend);
+ else extendSelection(cm.doc, startstart, word.to);
+ } else if (type == "triple") {
+ if (posLess(cur, startstart)) extendSelection(cm.doc, startend, clipPos(doc, Pos(cur.line, 0)));
+ else extendSelection(cm.doc, startstart, clipPos(doc, Pos(cur.line + 1, 0)));
+ }
+ }
+
+ var editorSize = getRect(display.wrapper);
+ // Used to ensure timeout re-tries don't fire when another extend
+ // happened in the meantime (clearTimeout isn't reliable -- at
+ // least on Chrome, the timeouts still happen even when cleared,
+ // if the clear happens after their scheduled firing time).
+ var counter = 0;
+
+ function extend(e) {
+ var curCount = ++counter;
+ var cur = posFromMouse(cm, e, true);
+ if (!cur) return;
+ if (!posEq(cur, last)) {
+ if (!cm.state.focused) onFocus(cm);
+ last = cur;
+ doSelect(cur);
+ var visible = visibleLines(display, doc);
+ if (cur.line >= visible.to || cur.line < visible.from)
+ setTimeout(operation(cm, function(){if (counter == curCount) extend(e);}), 150);
+ } else {
+ var outside = e.clientY < editorSize.top ? -20 : e.clientY > editorSize.bottom ? 20 : 0;
+ if (outside) setTimeout(operation(cm, function() {
+ if (counter != curCount) return;
+ display.scroller.scrollTop += outside;
+ extend(e);
+ }), 50);
+ }
+ }
+
+ function done(e) {
+ counter = Infinity;
+ e_preventDefault(e);
+ focusInput(cm);
+ off(document, "mousemove", move);
+ off(document, "mouseup", up);
+ }
+
+ var move = operation(cm, function(e) {
+ if (!ie && !e_button(e)) done(e);
+ else extend(e);
+ });
+ var up = operation(cm, done);
+ on(document, "mousemove", move);
+ on(document, "mouseup", up);
+ }
+
+ function gutterEvent(cm, e, type, prevent, signalfn) {
+ try { var mX = e.clientX, mY = e.clientY; }
+ catch(e) { return false; }
+ if (mX >= Math.floor(getRect(cm.display.gutters).right)) return false;
+ if (prevent) e_preventDefault(e);
+
+ var display = cm.display;
+ var lineBox = getRect(display.lineDiv);
+
+ if (mY > lineBox.bottom || !hasHandler(cm, type)) return e_defaultPrevented(e);
+ mY -= lineBox.top - display.viewOffset;
+
+ for (var i = 0; i < cm.options.gutters.length; ++i) {
+ var g = display.gutters.childNodes[i];
+ if (g && getRect(g).right >= mX) {
+ var line = lineAtHeight(cm.doc, mY);
+ var gutter = cm.options.gutters[i];
+ signalfn(cm, type, cm, line, gutter, e);
+ return e_defaultPrevented(e);
+ }
+ }
+ }
+
+ function contextMenuInGutter(cm, e) {
+ if (!hasHandler(cm, "gutterContextMenu")) return false;
+ return gutterEvent(cm, e, "gutterContextMenu", false, signal);
+ }
+
+ function clickInGutter(cm, e) {
+ return gutterEvent(cm, e, "gutterClick", true, signalLater);
+ }
+
+ // Kludge to work around strange IE behavior where it'll sometimes
+ // re-fire a series of drag-related events right after the drop (#1551)
+ var lastDrop = 0;
+
+ function onDrop(e) {
+ var cm = this;
+ if (signalDOMEvent(cm, e) || eventInWidget(cm.display, e) || (cm.options.onDragEvent && cm.options.onDragEvent(cm, addStop(e))))
+ return;
+ e_preventDefault(e);
+ if (ie) lastDrop = +new Date;
+ var pos = posFromMouse(cm, e, true), files = e.dataTransfer.files;
+ if (!pos || isReadOnly(cm)) return;
+ if (files && files.length && window.FileReader && window.File) {
+ var n = files.length, text = Array(n), read = 0;
+ var loadFile = function(file, i) {
+ var reader = new FileReader;
+ reader.onload = function() {
+ text[i] = reader.result;
+ if (++read == n) {
+ pos = clipPos(cm.doc, pos);
+ makeChange(cm.doc, {from: pos, to: pos, text: splitLines(text.join("\n")), origin: "paste"}, "around");
+ }
+ };
+ reader.readAsText(file);
+ };
+ for (var i = 0; i < n; ++i) loadFile(files[i], i);
+ } else {
+ // Don't do a replace if the drop happened inside of the selected text.
+ if (cm.state.draggingText && !(posLess(pos, cm.doc.sel.from) || posLess(cm.doc.sel.to, pos))) {
+ cm.state.draggingText(e);
+ // Ensure the editor is re-focused
+ setTimeout(bind(focusInput, cm), 20);
+ return;
+ }
+ try {
+ var text = e.dataTransfer.getData("Text");
+ if (text) {
+ var curFrom = cm.doc.sel.from, curTo = cm.doc.sel.to;
+ setSelection(cm.doc, pos, pos);
+ if (cm.state.draggingText) replaceRange(cm.doc, "", curFrom, curTo, "paste");
+ cm.replaceSelection(text, null, "paste");
+ focusInput(cm);
+ }
+ }
+ catch(e){}
+ }
+ }
+
+ function onDragStart(cm, e) {
+ if (ie && (!cm.state.draggingText || +new Date - lastDrop < 100)) { e_stop(e); return; }
+ if (signalDOMEvent(cm, e) || eventInWidget(cm.display, e)) return;
+
+ var txt = cm.getSelection();
+ e.dataTransfer.setData("Text", txt);
+
+ // Use dummy image instead of default browsers image.
+ // Recent Safari (~6.0.2) have a tendency to segfault when this happens, so we don't do it there.
+ if (e.dataTransfer.setDragImage && !safari) {
+ var img = elt("img", null, null, "position: fixed; left: 0; top: 0;");
+ img.src = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==";
+ if (opera) {
+ img.width = img.height = 1;
+ cm.display.wrapper.appendChild(img);
+ // Force a relayout, or Opera won't use our image for some obscure reason
+ img._top = img.offsetTop;
+ }
+ e.dataTransfer.setDragImage(img, 0, 0);
+ if (opera) img.parentNode.removeChild(img);
+ }
+ }
+
+ function setScrollTop(cm, val) {
+ if (Math.abs(cm.doc.scrollTop - val) < 2) return;
+ cm.doc.scrollTop = val;
+ if (!gecko) updateDisplay(cm, [], val);
+ if (cm.display.scroller.scrollTop != val) cm.display.scroller.scrollTop = val;
+ if (cm.display.scrollbarV.scrollTop != val) cm.display.scrollbarV.scrollTop = val;
+ if (gecko) updateDisplay(cm, []);
+ startWorker(cm, 100);
+ }
+ function setScrollLeft(cm, val, isScroller) {
+ if (isScroller ? val == cm.doc.scrollLeft : Math.abs(cm.doc.scrollLeft - val) < 2) return;
+ val = Math.min(val, cm.display.scroller.scrollWidth - cm.display.scroller.clientWidth);
+ cm.doc.scrollLeft = val;
+ alignHorizontally(cm);
+ if (cm.display.scroller.scrollLeft != val) cm.display.scroller.scrollLeft = val;
+ if (cm.display.scrollbarH.scrollLeft != val) cm.display.scrollbarH.scrollLeft = val;
+ }
+
+ // Since the delta values reported on mouse wheel events are
+ // unstandardized between browsers and even browser versions, and
+ // generally horribly unpredictable, this code starts by measuring
+ // the scroll effect that the first few mouse wheel events have,
+ // and, from that, detects the way it can convert deltas to pixel
+ // offsets afterwards.
+ //
+ // The reason we want to know the amount a wheel event will scroll
+ // is that it gives us a chance to update the display before the
+ // actual scrolling happens, reducing flickering.
+
+ var wheelSamples = 0, wheelPixelsPerUnit = null;
+ // Fill in a browser-detected starting value on browsers where we
+ // know one. These don't have to be accurate -- the result of them
+ // being wrong would just be a slight flicker on the first wheel
+ // scroll (if it is large enough).
+ if (ie) wheelPixelsPerUnit = -.53;
+ else if (gecko) wheelPixelsPerUnit = 15;
+ else if (chrome) wheelPixelsPerUnit = -.7;
+ else if (safari) wheelPixelsPerUnit = -1/3;
+
+ function onScrollWheel(cm, e) {
+ var dx = e.wheelDeltaX, dy = e.wheelDeltaY;
+ if (dx == null && e.detail && e.axis == e.HORIZONTAL_AXIS) dx = e.detail;
+ if (dy == null && e.detail && e.axis == e.VERTICAL_AXIS) dy = e.detail;
+ else if (dy == null) dy = e.wheelDelta;
+
+ var display = cm.display, scroll = display.scroller;
+ // Quit if there's nothing to scroll here
+ if (!(dx && scroll.scrollWidth > scroll.clientWidth ||
+ dy && scroll.scrollHeight > scroll.clientHeight)) return;
+
+ // Webkit browsers on OS X abort momentum scrolls when the target
+ // of the scroll event is removed from the scrollable element.
+ // This hack (see related code in patchDisplay) makes sure the
+ // element is kept around.
+ if (dy && mac && webkit) {
+ for (var cur = e.target; cur != scroll; cur = cur.parentNode) {
+ if (cur.lineObj) {
+ cm.display.currentWheelTarget = cur;
+ break;
+ }
+ }
+ }
+
+ // On some browsers, horizontal scrolling will cause redraws to
+ // happen before the gutter has been realigned, causing it to
+ // wriggle around in a most unseemly way. When we have an
+ // estimated pixels/delta value, we just handle horizontal
+ // scrolling entirely here. It'll be slightly off from native, but
+ // better than glitching out.
+ if (dx && !gecko && !opera && wheelPixelsPerUnit != null) {
+ if (dy)
+ setScrollTop(cm, Math.max(0, Math.min(scroll.scrollTop + dy * wheelPixelsPerUnit, scroll.scrollHeight - scroll.clientHeight)));
+ setScrollLeft(cm, Math.max(0, Math.min(scroll.scrollLeft + dx * wheelPixelsPerUnit, scroll.scrollWidth - scroll.clientWidth)));
+ e_preventDefault(e);
+ display.wheelStartX = null; // Abort measurement, if in progress
+ return;
+ }
+
+ if (dy && wheelPixelsPerUnit != null) {
+ var pixels = dy * wheelPixelsPerUnit;
+ var top = cm.doc.scrollTop, bot = top + display.wrapper.clientHeight;
+ if (pixels < 0) top = Math.max(0, top + pixels - 50);
+ else bot = Math.min(cm.doc.height, bot + pixels + 50);
+ updateDisplay(cm, [], {top: top, bottom: bot});
+ }
+
+ if (wheelSamples < 20) {
+ if (display.wheelStartX == null) {
+ display.wheelStartX = scroll.scrollLeft; display.wheelStartY = scroll.scrollTop;
+ display.wheelDX = dx; display.wheelDY = dy;
+ setTimeout(function() {
+ if (display.wheelStartX == null) return;
+ var movedX = scroll.scrollLeft - display.wheelStartX;
+ var movedY = scroll.scrollTop - display.wheelStartY;
+ var sample = (movedY && display.wheelDY && movedY / display.wheelDY) ||
+ (movedX && display.wheelDX && movedX / display.wheelDX);
+ display.wheelStartX = display.wheelStartY = null;
+ if (!sample) return;
+ wheelPixelsPerUnit = (wheelPixelsPerUnit * wheelSamples + sample) / (wheelSamples + 1);
+ ++wheelSamples;
+ }, 200);
+ } else {
+ display.wheelDX += dx; display.wheelDY += dy;
+ }
+ }
+ }
+
+ function doHandleBinding(cm, bound, dropShift) {
+ if (typeof bound == "string") {
+ bound = commands[bound];
+ if (!bound) return false;
+ }
+ // Ensure previous input has been read, so that the handler sees a
+ // consistent view of the document
+ if (cm.display.pollingFast && readInput(cm)) cm.display.pollingFast = false;
+ var doc = cm.doc, prevShift = doc.sel.shift, done = false;
+ try {
+ if (isReadOnly(cm)) cm.state.suppressEdits = true;
+ if (dropShift) doc.sel.shift = false;
+ done = bound(cm) != Pass;
+ } finally {
+ doc.sel.shift = prevShift;
+ cm.state.suppressEdits = false;
+ }
+ return done;
+ }
+
+ function allKeyMaps(cm) {
+ var maps = cm.state.keyMaps.slice(0);
+ if (cm.options.extraKeys) maps.push(cm.options.extraKeys);
+ maps.push(cm.options.keyMap);
+ return maps;
+ }
+
+ var maybeTransition;
+ function handleKeyBinding(cm, e) {
+ // Handle auto keymap transitions
+ var startMap = getKeyMap(cm.options.keyMap), next = startMap.auto;
+ clearTimeout(maybeTransition);
+ if (next && !isModifierKey(e)) maybeTransition = setTimeout(function() {
+ if (getKeyMap(cm.options.keyMap) == startMap) {
+ cm.options.keyMap = (next.call ? next.call(null, cm) : next);
+ keyMapChanged(cm);
+ }
+ }, 50);
+
+ var name = keyName(e, true), handled = false;
+ if (!name) return false;
+ var keymaps = allKeyMaps(cm);
+
+ if (e.shiftKey) {
+ // First try to resolve full name (including 'Shift-'). Failing
+ // that, see if there is a cursor-motion command (starting with
+ // 'go') bound to the keyname without 'Shift-'.
+ handled = lookupKey("Shift-" + name, keymaps, function(b) {return doHandleBinding(cm, b, true);})
+ || lookupKey(name, keymaps, function(b) {
+ if (typeof b == "string" ? /^go[A-Z]/.test(b) : b.motion)
+ return doHandleBinding(cm, b);
+ });
+ } else {
+ handled = lookupKey(name, keymaps, function(b) { return doHandleBinding(cm, b); });
+ }
+
+ if (handled) {
+ e_preventDefault(e);
+ restartBlink(cm);
+ if (ie_lt9) { e.oldKeyCode = e.keyCode; e.keyCode = 0; }
+ signalLater(cm, "keyHandled", cm, name, e);
+ }
+ return handled;
+ }
+
+ function handleCharBinding(cm, e, ch) {
+ var handled = lookupKey("'" + ch + "'", allKeyMaps(cm),
+ function(b) { return doHandleBinding(cm, b, true); });
+ if (handled) {
+ e_preventDefault(e);
+ restartBlink(cm);
+ signalLater(cm, "keyHandled", cm, "'" + ch + "'", e);
+ }
+ return handled;
+ }
+
+ var lastStoppedKey = null;
+ function onKeyDown(e) {
+ var cm = this;
+ if (!cm.state.focused) onFocus(cm);
+ if (signalDOMEvent(cm, e) || cm.options.onKeyEvent && cm.options.onKeyEvent(cm, addStop(e))) return;
+ if (ie && e.keyCode == 27) e.returnValue = false;
+ var code = e.keyCode;
+ // IE does strange things with escape.
+ cm.doc.sel.shift = code == 16 || e.shiftKey;
+ // First give onKeyEvent option a chance to handle this.
+ var handled = handleKeyBinding(cm, e);
+ if (opera) {
+ lastStoppedKey = handled ? code : null;
+ // Opera has no cut event... we try to at least catch the key combo
+ if (!handled && code == 88 && !hasCopyEvent && (mac ? e.metaKey : e.ctrlKey))
+ cm.replaceSelection("");
+ }
+ }
+
+ function onKeyPress(e) {
+ var cm = this;
+ if (signalDOMEvent(cm, e) || cm.options.onKeyEvent && cm.options.onKeyEvent(cm, addStop(e))) return;
+ var keyCode = e.keyCode, charCode = e.charCode;
+ if (opera && keyCode == lastStoppedKey) {lastStoppedKey = null; e_preventDefault(e); return;}
+ if (((opera && (!e.which || e.which < 10)) || khtml) && handleKeyBinding(cm, e)) return;
+ var ch = String.fromCharCode(charCode == null ? keyCode : charCode);
+ if (this.options.electricChars && this.doc.mode.electricChars &&
+ this.options.smartIndent && !isReadOnly(this) &&
+ this.doc.mode.electricChars.indexOf(ch) > -1)
+ setTimeout(operation(cm, function() {indentLine(cm, cm.doc.sel.to.line, "smart");}), 75);
+ if (handleCharBinding(cm, e, ch)) return;
+ if (ie && !ie_lt9) cm.display.inputHasSelection = null;
+ fastPoll(cm);
+ }
+
+ function onFocus(cm) {
+ if (cm.options.readOnly == "nocursor") return;
+ if (!cm.state.focused) {
+ signal(cm, "focus", cm);
+ cm.state.focused = true;
+ if (cm.display.wrapper.className.search(/\bCodeMirror-focused\b/) == -1)
+ cm.display.wrapper.className += " CodeMirror-focused";
+ if (!cm.curOp) {
+ resetInput(cm, true);
+ if (webkit) setTimeout(bind(resetInput, cm, true), 0); // Issue #1730
+ }
+ }
+ slowPoll(cm);
+ restartBlink(cm);
+ }
+ function onBlur(cm) {
+ if (cm.state.focused) {
+ signal(cm, "blur", cm);
+ cm.state.focused = false;
+ cm.display.wrapper.className = cm.display.wrapper.className.replace(" CodeMirror-focused", "");
+ }
+ clearInterval(cm.display.blinker);
+ setTimeout(function() {if (!cm.state.focused) cm.doc.sel.shift = false;}, 150);
+ }
+
+ var detectingSelectAll;
+ function onContextMenu(cm, e) {
+ if (signalDOMEvent(cm, e, "contextmenu")) return;
+ var display = cm.display, sel = cm.doc.sel;
+ if (eventInWidget(display, e) || contextMenuInGutter(cm, e)) return;
+
+ var pos = posFromMouse(cm, e), scrollPos = display.scroller.scrollTop;
+ if (!pos || opera) return; // Opera is difficult.
+
+ // Reset the current text selection only if the click is done outside of the selection
+ // and 'resetSelectionOnContextMenu' option is true.
+ var reset = cm.options.resetSelectionOnContextMenu;
+ if (reset && (posEq(sel.from, sel.to) || posLess(pos, sel.from) || !posLess(pos, sel.to)))
+ operation(cm, setSelection)(cm.doc, pos, pos);
+
+ var oldCSS = display.input.style.cssText;
+ display.inputDiv.style.position = "absolute";
+ display.input.style.cssText = "position: fixed; width: 30px; height: 30px; top: " + (e.clientY - 5) +
+ "px; left: " + (e.clientX - 5) + "px; z-index: 1000; background: white; outline: none;" +
+ "border-width: 0; outline: none; overflow: hidden; opacity: .05; -ms-opacity: .05; filter: alpha(opacity=5);";
+ focusInput(cm);
+ resetInput(cm, true);
+ // Adds "Select all" to context menu in FF
+ if (posEq(sel.from, sel.to)) display.input.value = display.prevInput = " ";
+
+ function prepareSelectAllHack() {
+ if (display.input.selectionStart != null) {
+ var extval = display.input.value = "\u200b" + (posEq(sel.from, sel.to) ? "" : display.input.value);
+ display.prevInput = "\u200b";
+ display.input.selectionStart = 1; display.input.selectionEnd = extval.length;
+ }
+ }
+ function rehide() {
+ display.inputDiv.style.position = "relative";
+ display.input.style.cssText = oldCSS;
+ if (ie_lt9) display.scrollbarV.scrollTop = display.scroller.scrollTop = scrollPos;
+ slowPoll(cm);
+
+ // Try to detect the user choosing select-all
+ if (display.input.selectionStart != null) {
+ if (!ie || ie_lt9) prepareSelectAllHack();
+ clearTimeout(detectingSelectAll);
+ var i = 0, poll = function(){
+ if (display.prevInput == " " && display.input.selectionStart == 0)
+ operation(cm, commands.selectAll)(cm);
+ else if (i++ < 10) detectingSelectAll = setTimeout(poll, 500);
+ else resetInput(cm);
+ };
+ detectingSelectAll = setTimeout(poll, 200);
+ }
+ }
+
+ if (ie && !ie_lt9) prepareSelectAllHack();
+ if (captureMiddleClick) {
+ e_stop(e);
+ var mouseup = function() {
+ off(window, "mouseup", mouseup);
+ setTimeout(rehide, 20);
+ };
+ on(window, "mouseup", mouseup);
+ } else {
+ setTimeout(rehide, 50);
+ }
+ }
+
+ // UPDATING
+
+ var changeEnd = CodeMirror.changeEnd = function(change) {
+ if (!change.text) return change.to;
+ return Pos(change.from.line + change.text.length - 1,
+ lst(change.text).length + (change.text.length == 1 ? change.from.ch : 0));
+ };
+
+ // Make sure a position will be valid after the given change.
+ function clipPostChange(doc, change, pos) {
+ if (!posLess(change.from, pos)) return clipPos(doc, pos);
+ var diff = (change.text.length - 1) - (change.to.line - change.from.line);
+ if (pos.line > change.to.line + diff) {
+ var preLine = pos.line - diff, lastLine = doc.first + doc.size - 1;
+ if (preLine > lastLine) return Pos(lastLine, getLine(doc, lastLine).text.length);
+ return clipToLen(pos, getLine(doc, preLine).text.length);
+ }
+ if (pos.line == change.to.line + diff)
+ return clipToLen(pos, lst(change.text).length + (change.text.length == 1 ? change.from.ch : 0) +
+ getLine(doc, change.to.line).text.length - change.to.ch);
+ var inside = pos.line - change.from.line;
+ return clipToLen(pos, change.text[inside].length + (inside ? 0 : change.from.ch));
+ }
+
+ // Hint can be null|"end"|"start"|"around"|{anchor,head}
+ function computeSelAfterChange(doc, change, hint) {
+ if (hint && typeof hint == "object") // Assumed to be {anchor, head} object
+ return {anchor: clipPostChange(doc, change, hint.anchor),
+ head: clipPostChange(doc, change, hint.head)};
+
+ if (hint == "start") return {anchor: change.from, head: change.from};
+
+ var end = changeEnd(change);
+ if (hint == "around") return {anchor: change.from, head: end};
+ if (hint == "end") return {anchor: end, head: end};
+
+ // hint is null, leave the selection alone as much as possible
+ var adjustPos = function(pos) {
+ if (posLess(pos, change.from)) return pos;
+ if (!posLess(change.to, pos)) return end;
+
+ var line = pos.line + change.text.length - (change.to.line - change.from.line) - 1, ch = pos.ch;
+ if (pos.line == change.to.line) ch += end.ch - change.to.ch;
+ return Pos(line, ch);
+ };
+ return {anchor: adjustPos(doc.sel.anchor), head: adjustPos(doc.sel.head)};
+ }
+
+ function filterChange(doc, change, update) {
+ var obj = {
+ canceled: false,
+ from: change.from,
+ to: change.to,
+ text: change.text,
+ origin: change.origin,
+ cancel: function() { this.canceled = true; }
+ };
+ if (update) obj.update = function(from, to, text, origin) {
+ if (from) this.from = clipPos(doc, from);
+ if (to) this.to = clipPos(doc, to);
+ if (text) this.text = text;
+ if (origin !== undefined) this.origin = origin;
+ };
+ signal(doc, "beforeChange", doc, obj);
+ if (doc.cm) signal(doc.cm, "beforeChange", doc.cm, obj);
+
+ if (obj.canceled) return null;
+ return {from: obj.from, to: obj.to, text: obj.text, origin: obj.origin};
+ }
+
+ // Replace the range from from to to by the strings in replacement.
+ // change is a {from, to, text [, origin]} object
+ function makeChange(doc, change, selUpdate, ignoreReadOnly) {
+ if (doc.cm) {
+ if (!doc.cm.curOp) return operation(doc.cm, makeChange)(doc, change, selUpdate, ignoreReadOnly);
+ if (doc.cm.state.suppressEdits) return;
+ }
+
+ if (hasHandler(doc, "beforeChange") || doc.cm && hasHandler(doc.cm, "beforeChange")) {
+ change = filterChange(doc, change, true);
+ if (!change) return;
+ }
+
+ // Possibly split or suppress the update based on the presence
+ // of read-only spans in its range.
+ var split = sawReadOnlySpans && !ignoreReadOnly && removeReadOnlyRanges(doc, change.from, change.to);
+ if (split) {
+ for (var i = split.length - 1; i >= 1; --i)
+ makeChangeNoReadonly(doc, {from: split[i].from, to: split[i].to, text: [""]});
+ if (split.length)
+ makeChangeNoReadonly(doc, {from: split[0].from, to: split[0].to, text: change.text}, selUpdate);
+ } else {
+ makeChangeNoReadonly(doc, change, selUpdate);
+ }
+ }
+
+ function makeChangeNoReadonly(doc, change, selUpdate) {
+ if (change.text.length == 1 && change.text[0] == "" && posEq(change.from, change.to)) return;
+ var selAfter = computeSelAfterChange(doc, change, selUpdate);
+ addToHistory(doc, change, selAfter, doc.cm ? doc.cm.curOp.id : NaN);
+
+ makeChangeSingleDoc(doc, change, selAfter, stretchSpansOverChange(doc, change));
+ var rebased = [];
+
+ linkedDocs(doc, function(doc, sharedHist) {
+ if (!sharedHist && indexOf(rebased, doc.history) == -1) {
+ rebaseHist(doc.history, change);
+ rebased.push(doc.history);
+ }
+ makeChangeSingleDoc(doc, change, null, stretchSpansOverChange(doc, change));
+ });
+ }
+
+ function makeChangeFromHistory(doc, type) {
+ if (doc.cm && doc.cm.state.suppressEdits) return;
+
+ var hist = doc.history;
+ var event = (type == "undo" ? hist.done : hist.undone).pop();
+ if (!event) return;
+
+ var anti = {changes: [], anchorBefore: event.anchorAfter, headBefore: event.headAfter,
+ anchorAfter: event.anchorBefore, headAfter: event.headBefore,
+ generation: hist.generation};
+ (type == "undo" ? hist.undone : hist.done).push(anti);
+ hist.generation = event.generation || ++hist.maxGeneration;
+
+ var filter = hasHandler(doc, "beforeChange") || doc.cm && hasHandler(doc.cm, "beforeChange");
+
+ for (var i = event.changes.length - 1; i >= 0; --i) {
+ var change = event.changes[i];
+ change.origin = type;
+ if (filter && !filterChange(doc, change, false)) {
+ (type == "undo" ? hist.done : hist.undone).length = 0;
+ return;
+ }
+
+ anti.changes.push(historyChangeFromChange(doc, change));
+
+ var after = i ? computeSelAfterChange(doc, change, null)
+ : {anchor: event.anchorBefore, head: event.headBefore};
+ makeChangeSingleDoc(doc, change, after, mergeOldSpans(doc, change));
+ var rebased = [];
+
+ linkedDocs(doc, function(doc, sharedHist) {
+ if (!sharedHist && indexOf(rebased, doc.history) == -1) {
+ rebaseHist(doc.history, change);
+ rebased.push(doc.history);
+ }
+ makeChangeSingleDoc(doc, change, null, mergeOldSpans(doc, change));
+ });
+ }
+ }
+
+ function shiftDoc(doc, distance) {
+ function shiftPos(pos) {return Pos(pos.line + distance, pos.ch);}
+ doc.first += distance;
+ if (doc.cm) regChange(doc.cm, doc.first, doc.first, distance);
+ doc.sel.head = shiftPos(doc.sel.head); doc.sel.anchor = shiftPos(doc.sel.anchor);
+ doc.sel.from = shiftPos(doc.sel.from); doc.sel.to = shiftPos(doc.sel.to);
+ }
+
+ function makeChangeSingleDoc(doc, change, selAfter, spans) {
+ if (doc.cm && !doc.cm.curOp)
+ return operation(doc.cm, makeChangeSingleDoc)(doc, change, selAfter, spans);
+
+ if (change.to.line < doc.first) {
+ shiftDoc(doc, change.text.length - 1 - (change.to.line - change.from.line));
+ return;
+ }
+ if (change.from.line > doc.lastLine()) return;
+
+ // Clip the change to the size of this doc
+ if (change.from.line < doc.first) {
+ var shift = change.text.length - 1 - (doc.first - change.from.line);
+ shiftDoc(doc, shift);
+ change = {from: Pos(doc.first, 0), to: Pos(change.to.line + shift, change.to.ch),
+ text: [lst(change.text)], origin: change.origin};
+ }
+ var last = doc.lastLine();
+ if (change.to.line > last) {
+ change = {from: change.from, to: Pos(last, getLine(doc, last).text.length),
+ text: [change.text[0]], origin: change.origin};
+ }
+
+ change.removed = getBetween(doc, change.from, change.to);
+
+ if (!selAfter) selAfter = computeSelAfterChange(doc, change, null);
+ if (doc.cm) makeChangeSingleDocInEditor(doc.cm, change, spans, selAfter);
+ else updateDoc(doc, change, spans, selAfter);
+ }
+
+ function makeChangeSingleDocInEditor(cm, change, spans, selAfter) {
+ var doc = cm.doc, display = cm.display, from = change.from, to = change.to;
+
+ var recomputeMaxLength = false, checkWidthStart = from.line;
+ if (!cm.options.lineWrapping) {
+ checkWidthStart = lineNo(visualLine(doc, getLine(doc, from.line)));
+ doc.iter(checkWidthStart, to.line + 1, function(line) {
+ if (line == display.maxLine) {
+ recomputeMaxLength = true;
+ return true;
+ }
+ });
+ }
+
+ if (!posLess(doc.sel.head, change.from) && !posLess(change.to, doc.sel.head))
+ cm.curOp.cursorActivity = true;
+
+ updateDoc(doc, change, spans, selAfter, estimateHeight(cm));
+
+ if (!cm.options.lineWrapping) {
+ doc.iter(checkWidthStart, from.line + change.text.length, function(line) {
+ var len = lineLength(doc, line);
+ if (len > display.maxLineLength) {
+ display.maxLine = line;
+ display.maxLineLength = len;
+ display.maxLineChanged = true;
+ recomputeMaxLength = false;
+ }
+ });
+ if (recomputeMaxLength) cm.curOp.updateMaxLine = true;
+ }
+
+ // Adjust frontier, schedule worker
+ doc.frontier = Math.min(doc.frontier, from.line);
+ startWorker(cm, 400);
+
+ var lendiff = change.text.length - (to.line - from.line) - 1;
+ // Remember that these lines changed, for updating the display
+ regChange(cm, from.line, to.line + 1, lendiff);
+
+ if (hasHandler(cm, "change")) {
+ var changeObj = {from: from, to: to,
+ text: change.text,
+ removed: change.removed,
+ origin: change.origin};
+ if (cm.curOp.textChanged) {
+ for (var cur = cm.curOp.textChanged; cur.next; cur = cur.next) {}
+ cur.next = changeObj;
+ } else cm.curOp.textChanged = changeObj;
+ }
+ }
+
+ function replaceRange(doc, code, from, to, origin) {
+ if (!to) to = from;
+ if (posLess(to, from)) { var tmp = to; to = from; from = tmp; }
+ if (typeof code == "string") code = splitLines(code);
+ makeChange(doc, {from: from, to: to, text: code, origin: origin}, null);
+ }
+
+ // POSITION OBJECT
+
+ function Pos(line, ch) {
+ if (!(this instanceof Pos)) return new Pos(line, ch);
+ this.line = line; this.ch = ch;
+ }
+ CodeMirror.Pos = Pos;
+
+ function posEq(a, b) {return a.line == b.line && a.ch == b.ch;}
+ function posLess(a, b) {return a.line < b.line || (a.line == b.line && a.ch < b.ch);}
+ function copyPos(x) {return Pos(x.line, x.ch);}
+
+ // SELECTION
+
+ function clipLine(doc, n) {return Math.max(doc.first, Math.min(n, doc.first + doc.size - 1));}
+ function clipPos(doc, pos) {
+ if (pos.line < doc.first) return Pos(doc.first, 0);
+ var last = doc.first + doc.size - 1;
+ if (pos.line > last) return Pos(last, getLine(doc, last).text.length);
+ return clipToLen(pos, getLine(doc, pos.line).text.length);
+ }
+ function clipToLen(pos, linelen) {
+ var ch = pos.ch;
+ if (ch == null || ch > linelen) return Pos(pos.line, linelen);
+ else if (ch < 0) return Pos(pos.line, 0);
+ else return pos;
+ }
+ function isLine(doc, l) {return l >= doc.first && l < doc.first + doc.size;}
+
+ // If shift is held, this will move the selection anchor. Otherwise,
+ // it'll set the whole selection.
+ function extendSelection(doc, pos, other, bias) {
+ if (doc.sel.shift || doc.sel.extend) {
+ var anchor = doc.sel.anchor;
+ if (other) {
+ var posBefore = posLess(pos, anchor);
+ if (posBefore != posLess(other, anchor)) {
+ anchor = pos;
+ pos = other;
+ } else if (posBefore != posLess(pos, other)) {
+ pos = other;
+ }
+ }
+ setSelection(doc, anchor, pos, bias);
+ } else {
+ setSelection(doc, pos, other || pos, bias);
+ }
+ if (doc.cm) doc.cm.curOp.userSelChange = true;
+ }
+
+ function filterSelectionChange(doc, anchor, head) {
+ var obj = {anchor: anchor, head: head};
+ signal(doc, "beforeSelectionChange", doc, obj);
+ if (doc.cm) signal(doc.cm, "beforeSelectionChange", doc.cm, obj);
+ obj.anchor = clipPos(doc, obj.anchor); obj.head = clipPos(doc, obj.head);
+ return obj;
+ }
+
+ // Update the selection. Last two args are only used by
+ // updateDoc, since they have to be expressed in the line
+ // numbers before the update.
+ function setSelection(doc, anchor, head, bias, checkAtomic) {
+ if (!checkAtomic && hasHandler(doc, "beforeSelectionChange") || doc.cm && hasHandler(doc.cm, "beforeSelectionChange")) {
+ var filtered = filterSelectionChange(doc, anchor, head);
+ head = filtered.head;
+ anchor = filtered.anchor;
+ }
+
+ var sel = doc.sel;
+ sel.goalColumn = null;
+ if (bias == null) bias = posLess(head, sel.head) ? -1 : 1;
+ // Skip over atomic spans.
+ if (checkAtomic || !posEq(anchor, sel.anchor))
+ anchor = skipAtomic(doc, anchor, bias, checkAtomic != "push");
+ if (checkAtomic || !posEq(head, sel.head))
+ head = skipAtomic(doc, head, bias, checkAtomic != "push");
+
+ if (posEq(sel.anchor, anchor) && posEq(sel.head, head)) return;
+
+ sel.anchor = anchor; sel.head = head;
+ var inv = posLess(head, anchor);
+ sel.from = inv ? head : anchor;
+ sel.to = inv ? anchor : head;
+
+ if (doc.cm)
+ doc.cm.curOp.updateInput = doc.cm.curOp.selectionChanged =
+ doc.cm.curOp.cursorActivity = true;
+
+ signalLater(doc, "cursorActivity", doc);
+ }
+
+ function reCheckSelection(cm) {
+ setSelection(cm.doc, cm.doc.sel.from, cm.doc.sel.to, null, "push");
+ }
+
+ function skipAtomic(doc, pos, bias, mayClear) {
+ var flipped = false, curPos = pos;
+ var dir = bias || 1;
+ doc.cantEdit = false;
+ search: for (;;) {
+ var line = getLine(doc, curPos.line);
+ if (line.markedSpans) {
+ for (var i = 0; i < line.markedSpans.length; ++i) {
+ var sp = line.markedSpans[i], m = sp.marker;
+ if ((sp.from == null || (m.inclusiveLeft ? sp.from <= curPos.ch : sp.from < curPos.ch)) &&
+ (sp.to == null || (m.inclusiveRight ? sp.to >= curPos.ch : sp.to > curPos.ch))) {
+ if (mayClear) {
+ signal(m, "beforeCursorEnter");
+ if (m.explicitlyCleared) {
+ if (!line.markedSpans) break;
+ else {--i; continue;}
+ }
+ }
+ if (!m.atomic) continue;
+ var newPos = m.find()[dir < 0 ? "from" : "to"];
+ if (posEq(newPos, curPos)) {
+ newPos.ch += dir;
+ if (newPos.ch < 0) {
+ if (newPos.line > doc.first) newPos = clipPos(doc, Pos(newPos.line - 1));
+ else newPos = null;
+ } else if (newPos.ch > line.text.length) {
+ if (newPos.line < doc.first + doc.size - 1) newPos = Pos(newPos.line + 1, 0);
+ else newPos = null;
+ }
+ if (!newPos) {
+ if (flipped) {
+ // Driven in a corner -- no valid cursor position found at all
+ // -- try again *with* clearing, if we didn't already
+ if (!mayClear) return skipAtomic(doc, pos, bias, true);
+ // Otherwise, turn off editing until further notice, and return the start of the doc
+ doc.cantEdit = true;
+ return Pos(doc.first, 0);
+ }
+ flipped = true; newPos = pos; dir = -dir;
+ }
+ }
+ curPos = newPos;
+ continue search;
+ }
+ }
+ }
+ return curPos;
+ }
+ }
+
+ // SCROLLING
+
+ function scrollCursorIntoView(cm) {
+ var coords = scrollPosIntoView(cm, cm.doc.sel.head, null, cm.options.cursorScrollMargin);
+ if (!cm.state.focused) return;
+ var display = cm.display, box = getRect(display.sizer), doScroll = null;
+ if (coords.top + box.top < 0) doScroll = true;
+ else if (coords.bottom + box.top > (window.innerHeight || document.documentElement.clientHeight)) doScroll = false;
+ if (doScroll != null && !phantom) {
+ var hidden = display.cursor.style.display == "none";
+ if (hidden) {
+ display.cursor.style.display = "";
+ display.cursor.style.left = coords.left + "px";
+ display.cursor.style.top = (coords.top - display.viewOffset) + "px";
+ }
+ display.cursor.scrollIntoView(doScroll);
+ if (hidden) display.cursor.style.display = "none";
+ }
+ }
+
+ function scrollPosIntoView(cm, pos, end, margin) {
+ if (margin == null) margin = 0;
+ for (;;) {
+ var changed = false, coords = cursorCoords(cm, pos);
+ var endCoords = !end || end == pos ? coords : cursorCoords(cm, end);
+ var scrollPos = calculateScrollPos(cm, Math.min(coords.left, endCoords.left),
+ Math.min(coords.top, endCoords.top) - margin,
+ Math.max(coords.left, endCoords.left),
+ Math.max(coords.bottom, endCoords.bottom) + margin);
+ var startTop = cm.doc.scrollTop, startLeft = cm.doc.scrollLeft;
+ if (scrollPos.scrollTop != null) {
+ setScrollTop(cm, scrollPos.scrollTop);
+ if (Math.abs(cm.doc.scrollTop - startTop) > 1) changed = true;
+ }
+ if (scrollPos.scrollLeft != null) {
+ setScrollLeft(cm, scrollPos.scrollLeft);
+ if (Math.abs(cm.doc.scrollLeft - startLeft) > 1) changed = true;
+ }
+ if (!changed) return coords;
+ }
+ }
+
+ function scrollIntoView(cm, x1, y1, x2, y2) {
+ var scrollPos = calculateScrollPos(cm, x1, y1, x2, y2);
+ if (scrollPos.scrollTop != null) setScrollTop(cm, scrollPos.scrollTop);
+ if (scrollPos.scrollLeft != null) setScrollLeft(cm, scrollPos.scrollLeft);
+ }
+
+ function calculateScrollPos(cm, x1, y1, x2, y2) {
+ var display = cm.display, snapMargin = textHeight(cm.display);
+ if (y1 < 0) y1 = 0;
+ var screen = display.scroller.clientHeight - scrollerCutOff, screentop = display.scroller.scrollTop, result = {};
+ var docBottom = cm.doc.height + paddingVert(display);
+ var atTop = y1 < snapMargin, atBottom = y2 > docBottom - snapMargin;
+ if (y1 < screentop) {
+ result.scrollTop = atTop ? 0 : y1;
+ } else if (y2 > screentop + screen) {
+ var newTop = Math.min(y1, (atBottom ? docBottom : y2) - screen);
+ if (newTop != screentop) result.scrollTop = newTop;
+ }
+
+ var screenw = display.scroller.clientWidth - scrollerCutOff, screenleft = display.scroller.scrollLeft;
+ x1 += display.gutters.offsetWidth; x2 += display.gutters.offsetWidth;
+ var gutterw = display.gutters.offsetWidth;
+ var atLeft = x1 < gutterw + 10;
+ if (x1 < screenleft + gutterw || atLeft) {
+ if (atLeft) x1 = 0;
+ result.scrollLeft = Math.max(0, x1 - 10 - gutterw);
+ } else if (x2 > screenw + screenleft - 3) {
+ result.scrollLeft = x2 + 10 - screenw;
+ }
+ return result;
+ }
+
+ function updateScrollPos(cm, left, top) {
+ cm.curOp.updateScrollPos = {scrollLeft: left == null ? cm.doc.scrollLeft : left,
+ scrollTop: top == null ? cm.doc.scrollTop : top};
+ }
+
+ function addToScrollPos(cm, left, top) {
+ var pos = cm.curOp.updateScrollPos || (cm.curOp.updateScrollPos = {scrollLeft: cm.doc.scrollLeft, scrollTop: cm.doc.scrollTop});
+ var scroll = cm.display.scroller;
+ pos.scrollTop = Math.max(0, Math.min(scroll.scrollHeight - scroll.clientHeight, pos.scrollTop + top));
+ pos.scrollLeft = Math.max(0, Math.min(scroll.scrollWidth - scroll.clientWidth, pos.scrollLeft + left));
+ }
+
+ // API UTILITIES
+
+ function indentLine(cm, n, how, aggressive) {
+ var doc = cm.doc;
+ if (how == null) how = "add";
+ if (how == "smart") {
+ if (!cm.doc.mode.indent) how = "prev";
+ else var state = getStateBefore(cm, n);
+ }
+
+ var tabSize = cm.options.tabSize;
+ var line = getLine(doc, n), curSpace = countColumn(line.text, null, tabSize);
+ var curSpaceString = line.text.match(/^\s*/)[0], indentation;
+ if (how == "smart") {
+ indentation = cm.doc.mode.indent(state, line.text.slice(curSpaceString.length), line.text);
+ if (indentation == Pass) {
+ if (!aggressive) return;
+ how = "prev";
+ }
+ }
+ if (how == "prev") {
+ if (n > doc.first) indentation = countColumn(getLine(doc, n-1).text, null, tabSize);
+ else indentation = 0;
+ } else if (how == "add") {
+ indentation = curSpace + cm.options.indentUnit;
+ } else if (how == "subtract") {
+ indentation = curSpace - cm.options.indentUnit;
+ } else if (typeof how == "number") {
+ indentation = curSpace + how;
+ }
+ indentation = Math.max(0, indentation);
+
+ var indentString = "", pos = 0;
+ if (cm.options.indentWithTabs)
+ for (var i = Math.floor(indentation / tabSize); i; --i) {pos += tabSize; indentString += "\t";}
+ if (pos < indentation) indentString += spaceStr(indentation - pos);
+
+ if (indentString != curSpaceString)
+ replaceRange(cm.doc, indentString, Pos(n, 0), Pos(n, curSpaceString.length), "+input");
+ else if (doc.sel.head.line == n && doc.sel.head.ch < curSpaceString.length)
+ setSelection(doc, Pos(n, curSpaceString.length), Pos(n, curSpaceString.length), 1);
+ line.stateAfter = null;
+ }
+
+ function changeLine(cm, handle, op) {
+ var no = handle, line = handle, doc = cm.doc;
+ if (typeof handle == "number") line = getLine(doc, clipLine(doc, handle));
+ else no = lineNo(handle);
+ if (no == null) return null;
+ if (op(line, no)) regChange(cm, no, no + 1);
+ else return null;
+ return line;
+ }
+
+ function findPosH(doc, pos, dir, unit, visually) {
+ var line = pos.line, ch = pos.ch, origDir = dir;
+ var lineObj = getLine(doc, line);
+ var possible = true;
+ function findNextLine() {
+ var l = line + dir;
+ if (l < doc.first || l >= doc.first + doc.size) return (possible = false);
+ line = l;
+ return lineObj = getLine(doc, l);
+ }
+ function moveOnce(boundToLine) {
+ var next = (visually ? moveVisually : moveLogically)(lineObj, ch, dir, true);
+ if (next == null) {
+ if (!boundToLine && findNextLine()) {
+ if (visually) ch = (dir < 0 ? lineRight : lineLeft)(lineObj);
+ else ch = dir < 0 ? lineObj.text.length : 0;
+ } else return (possible = false);
+ } else ch = next;
+ return true;
+ }
+
+ if (unit == "char") moveOnce();
+ else if (unit == "column") moveOnce(true);
+ else if (unit == "word" || unit == "group") {
+ var sawType = null, group = unit == "group";
+ for (var first = true;; first = false) {
+ if (dir < 0 && !moveOnce(!first)) break;
+ var cur = lineObj.text.charAt(ch) || "\n";
+ var type = isWordChar(cur) ? "w"
+ : !group ? null
+ : /\s/.test(cur) ? null
+ : "p";
+ if (sawType && sawType != type) {
+ if (dir < 0) {dir = 1; moveOnce();}
+ break;
+ }
+ if (type) sawType = type;
+ if (dir > 0 && !moveOnce(!first)) break;
+ }
+ }
+ var result = skipAtomic(doc, Pos(line, ch), origDir, true);
+ if (!possible) result.hitSide = true;
+ return result;
+ }
+
+ function findPosV(cm, pos, dir, unit) {
+ var doc = cm.doc, x = pos.left, y;
+ if (unit == "page") {
+ var pageSize = Math.min(cm.display.wrapper.clientHeight, window.innerHeight || document.documentElement.clientHeight);
+ y = pos.top + dir * (pageSize - (dir < 0 ? 1.5 : .5) * textHeight(cm.display));
+ } else if (unit == "line") {
+ y = dir > 0 ? pos.bottom + 3 : pos.top - 3;
+ }
+ for (;;) {
+ var target = coordsChar(cm, x, y);
+ if (!target.outside) break;
+ if (dir < 0 ? y <= 0 : y >= doc.height) { target.hitSide = true; break; }
+ y += dir * 5;
+ }
+ return target;
+ }
+
+ function findWordAt(line, pos) {
+ var start = pos.ch, end = pos.ch;
+ if (line) {
+ if ((pos.xRel < 0 || end == line.length) && start) --start; else ++end;
+ var startChar = line.charAt(start);
+ var check = isWordChar(startChar) ? isWordChar
+ : /\s/.test(startChar) ? function(ch) {return /\s/.test(ch);}
+ : function(ch) {return !/\s/.test(ch) && !isWordChar(ch);};
+ while (start > 0 && check(line.charAt(start - 1))) --start;
+ while (end < line.length && check(line.charAt(end))) ++end;
+ }
+ return {from: Pos(pos.line, start), to: Pos(pos.line, end)};
+ }
+
+ function selectLine(cm, line) {
+ extendSelection(cm.doc, Pos(line, 0), clipPos(cm.doc, Pos(line + 1, 0)));
+ }
+
+ // PROTOTYPE
+
+ // The publicly visible API. Note that operation(null, f) means
+ // 'wrap f in an operation, performed on its `this` parameter'
+
+ CodeMirror.prototype = {
+ constructor: CodeMirror,
+ focus: function(){window.focus(); focusInput(this); fastPoll(this);},
+
+ setOption: function(option, value) {
+ var options = this.options, old = options[option];
+ if (options[option] == value && option != "mode") return;
+ options[option] = value;
+ if (optionHandlers.hasOwnProperty(option))
+ operation(this, optionHandlers[option])(this, value, old);
+ },
+
+ getOption: function(option) {return this.options[option];},
+ getDoc: function() {return this.doc;},
+
+ addKeyMap: function(map, bottom) {
+ this.state.keyMaps[bottom ? "push" : "unshift"](map);
+ },
+ removeKeyMap: function(map) {
+ var maps = this.state.keyMaps;
+ for (var i = 0; i < maps.length; ++i)
+ if (maps[i] == map || (typeof maps[i] != "string" && maps[i].name == map)) {
+ maps.splice(i, 1);
+ return true;
+ }
+ },
+
+ addOverlay: operation(null, function(spec, options) {
+ var mode = spec.token ? spec : CodeMirror.getMode(this.options, spec);
+ if (mode.startState) throw new Error("Overlays may not be stateful.");
+ this.state.overlays.push({mode: mode, modeSpec: spec, opaque: options && options.opaque});
+ this.state.modeGen++;
+ regChange(this);
+ }),
+ removeOverlay: operation(null, function(spec) {
+ var overlays = this.state.overlays;
+ for (var i = 0; i < overlays.length; ++i) {
+ var cur = overlays[i].modeSpec;
+ if (cur == spec || typeof spec == "string" && cur.name == spec) {
+ overlays.splice(i, 1);
+ this.state.modeGen++;
+ regChange(this);
+ return;
+ }
+ }
+ }),
+
+ indentLine: operation(null, function(n, dir, aggressive) {
+ if (typeof dir != "string" && typeof dir != "number") {
+ if (dir == null) dir = this.options.smartIndent ? "smart" : "prev";
+ else dir = dir ? "add" : "subtract";
+ }
+ if (isLine(this.doc, n)) indentLine(this, n, dir, aggressive);
+ }),
+ indentSelection: operation(null, function(how) {
+ var sel = this.doc.sel;
+ if (posEq(sel.from, sel.to)) return indentLine(this, sel.from.line, how);
+ var e = sel.to.line - (sel.to.ch ? 0 : 1);
+ for (var i = sel.from.line; i <= e; ++i) indentLine(this, i, how);
+ }),
+
+ // Fetch the parser token for a given character. Useful for hacks
+ // that want to inspect the mode state (say, for completion).
+ getTokenAt: function(pos, precise) {
+ var doc = this.doc;
+ pos = clipPos(doc, pos);
+ var state = getStateBefore(this, pos.line, precise), mode = this.doc.mode;
+ var line = getLine(doc, pos.line);
+ var stream = new StringStream(line.text, this.options.tabSize);
+ while (stream.pos < pos.ch && !stream.eol()) {
+ stream.start = stream.pos;
+ var style = mode.token(stream, state);
+ }
+ return {start: stream.start,
+ end: stream.pos,
+ string: stream.current(),
+ className: style || null, // Deprecated, use 'type' instead
+ type: style || null,
+ state: state};
+ },
+
+ getTokenTypeAt: function(pos) {
+ pos = clipPos(this.doc, pos);
+ var styles = getLineStyles(this, getLine(this.doc, pos.line));
+ var before = 0, after = (styles.length - 1) / 2, ch = pos.ch;
+ if (ch == 0) return styles[2];
+ for (;;) {
+ var mid = (before + after) >> 1;
+ if ((mid ? styles[mid * 2 - 1] : 0) >= ch) after = mid;
+ else if (styles[mid * 2 + 1] < ch) before = mid + 1;
+ else return styles[mid * 2 + 2];
+ }
+ },
+
+ getModeAt: function(pos) {
+ var mode = this.doc.mode;
+ if (!mode.innerMode) return mode;
+ return CodeMirror.innerMode(mode, this.getTokenAt(pos).state).mode;
+ },
+
+ getHelper: function(pos, type) {
+ if (!helpers.hasOwnProperty(type)) return;
+ var help = helpers[type], mode = this.getModeAt(pos);
+ return mode[type] && help[mode[type]] ||
+ mode.helperType && help[mode.helperType] ||
+ help[mode.name];
+ },
+
+ getStateAfter: function(line, precise) {
+ var doc = this.doc;
+ line = clipLine(doc, line == null ? doc.first + doc.size - 1: line);
+ return getStateBefore(this, line + 1, precise);
+ },
+
+ cursorCoords: function(start, mode) {
+ var pos, sel = this.doc.sel;
+ if (start == null) pos = sel.head;
+ else if (typeof start == "object") pos = clipPos(this.doc, start);
+ else pos = start ? sel.from : sel.to;
+ return cursorCoords(this, pos, mode || "page");
+ },
+
+ charCoords: function(pos, mode) {
+ return charCoords(this, clipPos(this.doc, pos), mode || "page");
+ },
+
+ coordsChar: function(coords, mode) {
+ coords = fromCoordSystem(this, coords, mode || "page");
+ return coordsChar(this, coords.left, coords.top);
+ },
+
+ lineAtHeight: function(height, mode) {
+ height = fromCoordSystem(this, {top: height, left: 0}, mode || "page").top;
+ return lineAtHeight(this.doc, height + this.display.viewOffset);
+ },
+ heightAtLine: function(line, mode) {
+ var end = false, last = this.doc.first + this.doc.size - 1;
+ if (line < this.doc.first) line = this.doc.first;
+ else if (line > last) { line = last; end = true; }
+ var lineObj = getLine(this.doc, line);
+ return intoCoordSystem(this, getLine(this.doc, line), {top: 0, left: 0}, mode || "page").top +
+ (end ? lineObj.height : 0);
+ },
+
+ defaultTextHeight: function() { return textHeight(this.display); },
+ defaultCharWidth: function() { return charWidth(this.display); },
+
+ setGutterMarker: operation(null, function(line, gutterID, value) {
+ return changeLine(this, line, function(line) {
+ var markers = line.gutterMarkers || (line.gutterMarkers = {});
+ markers[gutterID] = value;
+ if (!value && isEmpty(markers)) line.gutterMarkers = null;
+ return true;
+ });
+ }),
+
+ clearGutter: operation(null, function(gutterID) {
+ var cm = this, doc = cm.doc, i = doc.first;
+ doc.iter(function(line) {
+ if (line.gutterMarkers && line.gutterMarkers[gutterID]) {
+ line.gutterMarkers[gutterID] = null;
+ regChange(cm, i, i + 1);
+ if (isEmpty(line.gutterMarkers)) line.gutterMarkers = null;
+ }
+ ++i;
+ });
+ }),
+
+ addLineClass: operation(null, function(handle, where, cls) {
+ return changeLine(this, handle, function(line) {
+ var prop = where == "text" ? "textClass" : where == "background" ? "bgClass" : "wrapClass";
+ if (!line[prop]) line[prop] = cls;
+ else if (new RegExp("(?:^|\\s)" + cls + "(?:$|\\s)").test(line[prop])) return false;
+ else line[prop] += " " + cls;
+ return true;
+ });
+ }),
+
+ removeLineClass: operation(null, function(handle, where, cls) {
+ return changeLine(this, handle, function(line) {
+ var prop = where == "text" ? "textClass" : where == "background" ? "bgClass" : "wrapClass";
+ var cur = line[prop];
+ if (!cur) return false;
+ else if (cls == null) line[prop] = null;
+ else {
+ var found = cur.match(new RegExp("(?:^|\\s+)" + cls + "(?:$|\\s+)"));
+ if (!found) return false;
+ var end = found.index + found[0].length;
+ line[prop] = cur.slice(0, found.index) + (!found.index || end == cur.length ? "" : " ") + cur.slice(end) || null;
+ }
+ return true;
+ });
+ }),
+
+ addLineWidget: operation(null, function(handle, node, options) {
+ return addLineWidget(this, handle, node, options);
+ }),
+
+ removeLineWidget: function(widget) { widget.clear(); },
+
+ lineInfo: function(line) {
+ if (typeof line == "number") {
+ if (!isLine(this.doc, line)) return null;
+ var n = line;
+ line = getLine(this.doc, line);
+ if (!line) return null;
+ } else {
+ var n = lineNo(line);
+ if (n == null) return null;
+ }
+ return {line: n, handle: line, text: line.text, gutterMarkers: line.gutterMarkers,
+ textClass: line.textClass, bgClass: line.bgClass, wrapClass: line.wrapClass,
+ widgets: line.widgets};
+ },
+
+ getViewport: function() { return {from: this.display.showingFrom, to: this.display.showingTo};},
+
+ addWidget: function(pos, node, scroll, vert, horiz) {
+ var display = this.display;
+ pos = cursorCoords(this, clipPos(this.doc, pos));
+ var top = pos.bottom, left = pos.left;
+ node.style.position = "absolute";
+ display.sizer.appendChild(node);
+ if (vert == "over") {
+ top = pos.top;
+ } else if (vert == "above" || vert == "near") {
+ var vspace = Math.max(display.wrapper.clientHeight, this.doc.height),
+ hspace = Math.max(display.sizer.clientWidth, display.lineSpace.clientWidth);
+ // Default to positioning above (if specified and possible); otherwise default to positioning below
+ if ((vert == 'above' || pos.bottom + node.offsetHeight > vspace) && pos.top > node.offsetHeight)
+ top = pos.top - node.offsetHeight;
+ else if (pos.bottom + node.offsetHeight <= vspace)
+ top = pos.bottom;
+ if (left + node.offsetWidth > hspace)
+ left = hspace - node.offsetWidth;
+ }
+ node.style.top = top + "px";
+ node.style.left = node.style.right = "";
+ if (horiz == "right") {
+ left = display.sizer.clientWidth - node.offsetWidth;
+ node.style.right = "0px";
+ } else {
+ if (horiz == "left") left = 0;
+ else if (horiz == "middle") left = (display.sizer.clientWidth - node.offsetWidth) / 2;
+ node.style.left = left + "px";
+ }
+ if (scroll)
+ scrollIntoView(this, left, top, left + node.offsetWidth, top + node.offsetHeight);
+ },
+
+ triggerOnKeyDown: operation(null, onKeyDown),
+
+ execCommand: function(cmd) {return commands[cmd](this);},
+
+ findPosH: function(from, amount, unit, visually) {
+ var dir = 1;
+ if (amount < 0) { dir = -1; amount = -amount; }
+ for (var i = 0, cur = clipPos(this.doc, from); i < amount; ++i) {
+ cur = findPosH(this.doc, cur, dir, unit, visually);
+ if (cur.hitSide) break;
+ }
+ return cur;
+ },
+
+ moveH: operation(null, function(dir, unit) {
+ var sel = this.doc.sel, pos;
+ if (sel.shift || sel.extend || posEq(sel.from, sel.to))
+ pos = findPosH(this.doc, sel.head, dir, unit, this.options.rtlMoveVisually);
+ else
+ pos = dir < 0 ? sel.from : sel.to;
+ extendSelection(this.doc, pos, pos, dir);
+ }),
+
+ deleteH: operation(null, function(dir, unit) {
+ var sel = this.doc.sel;
+ if (!posEq(sel.from, sel.to)) replaceRange(this.doc, "", sel.from, sel.to, "+delete");
+ else replaceRange(this.doc, "", sel.from, findPosH(this.doc, sel.head, dir, unit, false), "+delete");
+ this.curOp.userSelChange = true;
+ }),
+
+ findPosV: function(from, amount, unit, goalColumn) {
+ var dir = 1, x = goalColumn;
+ if (amount < 0) { dir = -1; amount = -amount; }
+ for (var i = 0, cur = clipPos(this.doc, from); i < amount; ++i) {
+ var coords = cursorCoords(this, cur, "div");
+ if (x == null) x = coords.left;
+ else coords.left = x;
+ cur = findPosV(this, coords, dir, unit);
+ if (cur.hitSide) break;
+ }
+ return cur;
+ },
+
+ moveV: operation(null, function(dir, unit) {
+ var sel = this.doc.sel;
+ var pos = cursorCoords(this, sel.head, "div");
+ if (sel.goalColumn != null) pos.left = sel.goalColumn;
+ var target = findPosV(this, pos, dir, unit);
+
+ if (unit == "page") addToScrollPos(this, 0, charCoords(this, target, "div").top - pos.top);
+ extendSelection(this.doc, target, target, dir);
+ sel.goalColumn = pos.left;
+ }),
+
+ toggleOverwrite: function(value) {
+ if (value != null && value == this.state.overwrite) return;
+ if (this.state.overwrite = !this.state.overwrite)
+ this.display.cursor.className += " CodeMirror-overwrite";
+ else
+ this.display.cursor.className = this.display.cursor.className.replace(" CodeMirror-overwrite", "");
+ },
+ hasFocus: function() { return this.state.focused; },
+
+ scrollTo: operation(null, function(x, y) {
+ updateScrollPos(this, x, y);
+ }),
+ getScrollInfo: function() {
+ var scroller = this.display.scroller, co = scrollerCutOff;
+ return {left: scroller.scrollLeft, top: scroller.scrollTop,
+ height: scroller.scrollHeight - co, width: scroller.scrollWidth - co,
+ clientHeight: scroller.clientHeight - co, clientWidth: scroller.clientWidth - co};
+ },
+
+ scrollIntoView: operation(null, function(range, margin) {
+ if (range == null) range = {from: this.doc.sel.head, to: null};
+ else if (typeof range == "number") range = {from: Pos(range, 0), to: null};
+ else if (range.from == null) range = {from: range, to: null};
+ if (!range.to) range.to = range.from;
+ if (!margin) margin = 0;
+
+ var coords = range;
+ if (range.from.line != null) {
+ this.curOp.scrollToPos = {from: range.from, to: range.to, margin: margin};
+ coords = {from: cursorCoords(this, range.from),
+ to: cursorCoords(this, range.to)};
+ }
+ var sPos = calculateScrollPos(this, Math.min(coords.from.left, coords.to.left),
+ Math.min(coords.from.top, coords.to.top) - margin,
+ Math.max(coords.from.right, coords.to.right),
+ Math.max(coords.from.bottom, coords.to.bottom) + margin);
+ updateScrollPos(this, sPos.scrollLeft, sPos.scrollTop);
+ }),
+
+ setSize: operation(null, function(width, height) {
+ function interpret(val) {
+ return typeof val == "number" || /^\d+$/.test(String(val)) ? val + "px" : val;
+ }
+ if (width != null) this.display.wrapper.style.width = interpret(width);
+ if (height != null) this.display.wrapper.style.height = interpret(height);
+ if (this.options.lineWrapping)
+ this.display.measureLineCache.length = this.display.measureLineCachePos = 0;
+ this.curOp.forceUpdate = true;
+ }),
+
+ operation: function(f){return runInOp(this, f);},
+
+ refresh: operation(null, function() {
+ var badHeight = this.display.cachedTextHeight == null;
+ clearCaches(this);
+ updateScrollPos(this, this.doc.scrollLeft, this.doc.scrollTop);
+ regChange(this);
+ if (badHeight) estimateLineHeights(this);
+ }),
+
+ swapDoc: operation(null, function(doc) {
+ var old = this.doc;
+ old.cm = null;
+ attachDoc(this, doc);
+ clearCaches(this);
+ resetInput(this, true);
+ updateScrollPos(this, doc.scrollLeft, doc.scrollTop);
+ signalLater(this, "swapDoc", this, old);
+ return old;
+ }),
+
+ getInputField: function(){return this.display.input;},
+ getWrapperElement: function(){return this.display.wrapper;},
+ getScrollerElement: function(){return this.display.scroller;},
+ getGutterElement: function(){return this.display.gutters;}
+ };
+ eventMixin(CodeMirror);
+
+ // OPTION DEFAULTS
+
+ var optionHandlers = CodeMirror.optionHandlers = {};
+
+ // The default configuration options.
+ var defaults = CodeMirror.defaults = {};
+
+ function option(name, deflt, handle, notOnInit) {
+ CodeMirror.defaults[name] = deflt;
+ if (handle) optionHandlers[name] =
+ notOnInit ? function(cm, val, old) {if (old != Init) handle(cm, val, old);} : handle;
+ }
+
+ var Init = CodeMirror.Init = {toString: function(){return "CodeMirror.Init";}};
+
+ // These two are, on init, called from the constructor because they
+ // have to be initialized before the editor can start at all.
+ option("value", "", function(cm, val) {
+ cm.setValue(val);
+ }, true);
+ option("mode", null, function(cm, val) {
+ cm.doc.modeOption = val;
+ loadMode(cm);
+ }, true);
+
+ option("indentUnit", 2, loadMode, true);
+ option("indentWithTabs", false);
+ option("smartIndent", true);
+ option("tabSize", 4, function(cm) {
+ loadMode(cm);
+ clearCaches(cm);
+ regChange(cm);
+ }, true);
+ option("specialChars", /[\t\u0000-\u0019\u00ad\u200b\u2028\u2029\ufeff]/g, function(cm, val) {
+ cm.options.specialChars = new RegExp(val.source + (val.test("\t") ? "" : "|\t"), "g");
+ cm.refresh();
+ }, true);
+ option("specialCharPlaceholder", defaultSpecialCharPlaceholder, function(cm) {cm.refresh();}, true);
+ option("electricChars", true);
+ option("rtlMoveVisually", !windows);
+ option("wholeLineUpdateBefore", true);
+
+ option("theme", "default", function(cm) {
+ themeChanged(cm);
+ guttersChanged(cm);
+ }, true);
+ option("keyMap", "default", keyMapChanged);
+ option("extraKeys", null);
+
+ option("onKeyEvent", null);
+ option("onDragEvent", null);
+
+ option("lineWrapping", false, wrappingChanged, true);
+ option("gutters", [], function(cm) {
+ setGuttersForLineNumbers(cm.options);
+ guttersChanged(cm);
+ }, true);
+ option("fixedGutter", true, function(cm, val) {
+ cm.display.gutters.style.left = val ? compensateForHScroll(cm.display) + "px" : "0";
+ cm.refresh();
+ }, true);
+ option("coverGutterNextToScrollbar", false, updateScrollbars, true);
+ option("lineNumbers", false, function(cm) {
+ setGuttersForLineNumbers(cm.options);
+ guttersChanged(cm);
+ }, true);
+ option("firstLineNumber", 1, guttersChanged, true);
+ option("lineNumberFormatter", function(integer) {return integer;}, guttersChanged, true);
+ option("showCursorWhenSelecting", false, updateSelection, true);
+
+ option("resetSelectionOnContextMenu", true);
+
+ option("readOnly", false, function(cm, val) {
+ if (val == "nocursor") {
+ onBlur(cm);
+ cm.display.input.blur();
+ cm.display.disabled = true;
+ } else {
+ cm.display.disabled = false;
+ if (!val) resetInput(cm, true);
+ }
+ });
+ option("dragDrop", true);
+
+ option("cursorBlinkRate", 530);
+ option("cursorScrollMargin", 0);
+ option("cursorHeight", 1);
+ option("workTime", 100);
+ option("workDelay", 100);
+ option("flattenSpans", true);
+ option("pollInterval", 100);
+ option("undoDepth", 40, function(cm, val){cm.doc.history.undoDepth = val;});
+ option("historyEventDelay", 500);
+ option("viewportMargin", 10, function(cm){cm.refresh();}, true);
+ option("maxHighlightLength", 10000, function(cm){loadMode(cm); cm.refresh();}, true);
+ option("crudeMeasuringFrom", 10000);
+ option("moveInputWithCursor", true, function(cm, val) {
+ if (!val) cm.display.inputDiv.style.top = cm.display.inputDiv.style.left = 0;
+ });
+
+ option("tabindex", null, function(cm, val) {
+ cm.display.input.tabIndex = val || "";
+ });
+ option("autofocus", null);
+
+ // MODE DEFINITION AND QUERYING
+
+ // Known modes, by name and by MIME
+ var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};
+
+ CodeMirror.defineMode = function(name, mode) {
+ if (!CodeMirror.defaults.mode && name != "null") CodeMirror.defaults.mode = name;
+ if (arguments.length > 2) {
+ mode.dependencies = [];
+ for (var i = 2; i < arguments.length; ++i) mode.dependencies.push(arguments[i]);
+ }
+ modes[name] = mode;
+ };
+
+ CodeMirror.defineMIME = function(mime, spec) {
+ mimeModes[mime] = spec;
+ };
+
+ CodeMirror.resolveMode = function(spec) {
+ if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
+ spec = mimeModes[spec];
+ } else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {
+ var found = mimeModes[spec.name];
+ spec = createObj(found, spec);
+ spec.name = found.name;
+ } else if (typeof spec == "string" && /^[\w\-]+\/[\w\-]+\+xml$/.test(spec)) {
+ return CodeMirror.resolveMode("application/xml");
+ }
+ if (typeof spec == "string") return {name: spec};
+ else return spec || {name: "null"};
+ };
+
+ CodeMirror.getMode = function(options, spec) {
+ var spec = CodeMirror.resolveMode(spec);
+ var mfactory = modes[spec.name];
+ if (!mfactory) return CodeMirror.getMode(options, "text/plain");
+ var modeObj = mfactory(options, spec);
+ if (modeExtensions.hasOwnProperty(spec.name)) {
+ var exts = modeExtensions[spec.name];
+ for (var prop in exts) {
+ if (!exts.hasOwnProperty(prop)) continue;
+ if (modeObj.hasOwnProperty(prop)) modeObj["_" + prop] = modeObj[prop];
+ modeObj[prop] = exts[prop];
+ }
+ }
+ modeObj.name = spec.name;
+
+ return modeObj;
+ };
+
+ CodeMirror.defineMode("null", function() {
+ return {token: function(stream) {stream.skipToEnd();}};
+ });
+ CodeMirror.defineMIME("text/plain", "null");
+
+ var modeExtensions = CodeMirror.modeExtensions = {};
+ CodeMirror.extendMode = function(mode, properties) {
+ var exts = modeExtensions.hasOwnProperty(mode) ? modeExtensions[mode] : (modeExtensions[mode] = {});
+ copyObj(properties, exts);
+ };
+
+ // EXTENSIONS
+
+ CodeMirror.defineExtension = function(name, func) {
+ CodeMirror.prototype[name] = func;
+ };
+ CodeMirror.defineDocExtension = function(name, func) {
+ Doc.prototype[name] = func;
+ };
+ CodeMirror.defineOption = option;
+
+ var initHooks = [];
+ CodeMirror.defineInitHook = function(f) {initHooks.push(f);};
+
+ var helpers = CodeMirror.helpers = {};
+ CodeMirror.registerHelper = function(type, name, value) {
+ if (!helpers.hasOwnProperty(type)) helpers[type] = CodeMirror[type] = {};
+ helpers[type][name] = value;
+ };
+
+ // UTILITIES
+
+ CodeMirror.isWordChar = isWordChar;
+
+ // MODE STATE HANDLING
+
+ // Utility functions for working with state. Exported because modes
+ // sometimes need to do this.
+ function copyState(mode, state) {
+ if (state === true) return state;
+ if (mode.copyState) return mode.copyState(state);
+ var nstate = {};
+ for (var n in state) {
+ var val = state[n];
+ if (val instanceof Array) val = val.concat([]);
+ nstate[n] = val;
+ }
+ return nstate;
+ }
+ CodeMirror.copyState = copyState;
+
+ function startState(mode, a1, a2) {
+ return mode.startState ? mode.startState(a1, a2) : true;
+ }
+ CodeMirror.startState = startState;
+
+ CodeMirror.innerMode = function(mode, state) {
+ while (mode.innerMode) {
+ var info = mode.innerMode(state);
+ if (!info || info.mode == mode) break;
+ state = info.state;
+ mode = info.mode;
+ }
+ return info || {mode: mode, state: state};
+ };
+
+ // STANDARD COMMANDS
+
+ var commands = CodeMirror.commands = {
+ selectAll: function(cm) {cm.setSelection(Pos(cm.firstLine(), 0), Pos(cm.lastLine()));},
+ killLine: function(cm) {
+ var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
+ if (!sel && cm.getLine(from.line).length == from.ch)
+ cm.replaceRange("", from, Pos(from.line + 1, 0), "+delete");
+ else cm.replaceRange("", from, sel ? to : Pos(from.line), "+delete");
+ },
+ deleteLine: function(cm) {
+ var l = cm.getCursor().line;
+ cm.replaceRange("", Pos(l, 0), Pos(l), "+delete");
+ },
+ delLineLeft: function(cm) {
+ var cur = cm.getCursor();
+ cm.replaceRange("", Pos(cur.line, 0), cur, "+delete");
+ },
+ undo: function(cm) {cm.undo();},
+ redo: function(cm) {cm.redo();},
+ goDocStart: function(cm) {cm.extendSelection(Pos(cm.firstLine(), 0));},
+ goDocEnd: function(cm) {cm.extendSelection(Pos(cm.lastLine()));},
+ goLineStart: function(cm) {
+ cm.extendSelection(lineStart(cm, cm.getCursor().line));
+ },
+ goLineStartSmart: function(cm) {
+ var cur = cm.getCursor(), start = lineStart(cm, cur.line);
+ var line = cm.getLineHandle(start.line);
+ var order = getOrder(line);
+ if (!order || order[0].level == 0) {
+ var firstNonWS = Math.max(0, line.text.search(/\S/));
+ var inWS = cur.line == start.line && cur.ch <= firstNonWS && cur.ch;
+ cm.extendSelection(Pos(start.line, inWS ? 0 : firstNonWS));
+ } else cm.extendSelection(start);
+ },
+ goLineEnd: function(cm) {
+ cm.extendSelection(lineEnd(cm, cm.getCursor().line));
+ },
+ goLineRight: function(cm) {
+ var top = cm.charCoords(cm.getCursor(), "div").top + 5;
+ cm.extendSelection(cm.coordsChar({left: cm.display.lineDiv.offsetWidth + 100, top: top}, "div"));
+ },
+ goLineLeft: function(cm) {
+ var top = cm.charCoords(cm.getCursor(), "div").top + 5;
+ cm.extendSelection(cm.coordsChar({left: 0, top: top}, "div"));
+ },
+ goLineUp: function(cm) {cm.moveV(-1, "line");},
+ goLineDown: function(cm) {cm.moveV(1, "line");},
+ goPageUp: function(cm) {cm.moveV(-1, "page");},
+ goPageDown: function(cm) {cm.moveV(1, "page");},
+ goCharLeft: function(cm) {cm.moveH(-1, "char");},
+ goCharRight: function(cm) {cm.moveH(1, "char");},
+ goColumnLeft: function(cm) {cm.moveH(-1, "column");},
+ goColumnRight: function(cm) {cm.moveH(1, "column");},
+ goWordLeft: function(cm) {cm.moveH(-1, "word");},
+ goGroupRight: function(cm) {cm.moveH(1, "group");},
+ goGroupLeft: function(cm) {cm.moveH(-1, "group");},
+ goWordRight: function(cm) {cm.moveH(1, "word");},
+ delCharBefore: function(cm) {cm.deleteH(-1, "char");},
+ delCharAfter: function(cm) {cm.deleteH(1, "char");},
+ delWordBefore: function(cm) {cm.deleteH(-1, "word");},
+ delWordAfter: function(cm) {cm.deleteH(1, "word");},
+ delGroupBefore: function(cm) {cm.deleteH(-1, "group");},
+ delGroupAfter: function(cm) {cm.deleteH(1, "group");},
+ indentAuto: function(cm) {cm.indentSelection("smart");},
+ indentMore: function(cm) {cm.indentSelection("add");},
+ indentLess: function(cm) {cm.indentSelection("subtract");},
+ insertTab: function(cm) {cm.replaceSelection("\t", "end", "+input");},
+ defaultTab: function(cm) {
+ if (cm.somethingSelected()) cm.indentSelection("add");
+ else cm.replaceSelection("\t", "end", "+input");
+ },
+ transposeChars: function(cm) {
+ var cur = cm.getCursor(), line = cm.getLine(cur.line);
+ if (cur.ch > 0 && cur.ch < line.length - 1)
+ cm.replaceRange(line.charAt(cur.ch) + line.charAt(cur.ch - 1),
+ Pos(cur.line, cur.ch - 1), Pos(cur.line, cur.ch + 1));
+ },
+ newlineAndIndent: function(cm) {
+ operation(cm, function() {
+ cm.replaceSelection("\n", "end", "+input");
+ cm.indentLine(cm.getCursor().line, null, true);
+ })();
+ },
+ toggleOverwrite: function(cm) {cm.toggleOverwrite();}
+ };
+
+ // STANDARD KEYMAPS
+
+ var keyMap = CodeMirror.keyMap = {};
+ keyMap.basic = {
+ "Left": "goCharLeft", "Right": "goCharRight", "Up": "goLineUp", "Down": "goLineDown",
+ "End": "goLineEnd", "Home": "goLineStartSmart", "PageUp": "goPageUp", "PageDown": "goPageDown",
+ "Delete": "delCharAfter", "Backspace": "delCharBefore", "Shift-Backspace": "delCharBefore",
+ "Tab": "defaultTab", "Shift-Tab": "indentAuto",
+ "Enter": "newlineAndIndent", "Insert": "toggleOverwrite"
+ };
+ // Note that the save and find-related commands aren't defined by
+ // default. Unknown commands are simply ignored.
+ keyMap.pcDefault = {
+ "Ctrl-A": "selectAll", "Ctrl-D": "deleteLine", "Ctrl-Z": "undo", "Shift-Ctrl-Z": "redo", "Ctrl-Y": "redo",
+ "Ctrl-Home": "goDocStart", "Alt-Up": "goDocStart", "Ctrl-End": "goDocEnd", "Ctrl-Down": "goDocEnd",
+ "Ctrl-Left": "goGroupLeft", "Ctrl-Right": "goGroupRight", "Alt-Left": "goLineStart", "Alt-Right": "goLineEnd",
+ "Ctrl-Backspace": "delGroupBefore", "Ctrl-Delete": "delGroupAfter", "Ctrl-S": "save", "Ctrl-F": "find",
+ "Ctrl-G": "findNext", "Shift-Ctrl-G": "findPrev", "Shift-Ctrl-F": "replace", "Shift-Ctrl-R": "replaceAll",
+ "Ctrl-[": "indentLess", "Ctrl-]": "indentMore",
+ fallthrough: "basic"
+ };
+ keyMap.macDefault = {
+ "Cmd-A": "selectAll", "Cmd-D": "deleteLine", "Cmd-Z": "undo", "Shift-Cmd-Z": "redo", "Cmd-Y": "redo",
+ "Cmd-Up": "goDocStart", "Cmd-End": "goDocEnd", "Cmd-Down": "goDocEnd", "Alt-Left": "goGroupLeft",
+ "Alt-Right": "goGroupRight", "Cmd-Left": "goLineStart", "Cmd-Right": "goLineEnd", "Alt-Backspace": "delGroupBefore",
+ "Ctrl-Alt-Backspace": "delGroupAfter", "Alt-Delete": "delGroupAfter", "Cmd-S": "save", "Cmd-F": "find",
+ "Cmd-G": "findNext", "Shift-Cmd-G": "findPrev", "Cmd-Alt-F": "replace", "Shift-Cmd-Alt-F": "replaceAll",
+ "Cmd-[": "indentLess", "Cmd-]": "indentMore", "Cmd-Backspace": "delLineLeft",
+ fallthrough: ["basic", "emacsy"]
+ };
+ keyMap["default"] = mac ? keyMap.macDefault : keyMap.pcDefault;
+ keyMap.emacsy = {
+ "Ctrl-F": "goCharRight", "Ctrl-B": "goCharLeft", "Ctrl-P": "goLineUp", "Ctrl-N": "goLineDown",
+ "Alt-F": "goWordRight", "Alt-B": "goWordLeft", "Ctrl-A": "goLineStart", "Ctrl-E": "goLineEnd",
+ "Ctrl-V": "goPageDown", "Shift-Ctrl-V": "goPageUp", "Ctrl-D": "delCharAfter", "Ctrl-H": "delCharBefore",
+ "Alt-D": "delWordAfter", "Alt-Backspace": "delWordBefore", "Ctrl-K": "killLine", "Ctrl-T": "transposeChars"
+ };
+
+ // KEYMAP DISPATCH
+
+ function getKeyMap(val) {
+ if (typeof val == "string") return keyMap[val];
+ else return val;
+ }
+
+ function lookupKey(name, maps, handle) {
+ function lookup(map) {
+ map = getKeyMap(map);
+ var found = map[name];
+ if (found === false) return "stop";
+ if (found != null && handle(found)) return true;
+ if (map.nofallthrough) return "stop";
+
+ var fallthrough = map.fallthrough;
+ if (fallthrough == null) return false;
+ if (Object.prototype.toString.call(fallthrough) != "[object Array]")
+ return lookup(fallthrough);
+ for (var i = 0, e = fallthrough.length; i < e; ++i) {
+ var done = lookup(fallthrough[i]);
+ if (done) return done;
+ }
+ return false;
+ }
+
+ for (var i = 0; i < maps.length; ++i) {
+ var done = lookup(maps[i]);
+ if (done) return done != "stop";
+ }
+ }
+ function isModifierKey(event) {
+ var name = keyNames[event.keyCode];
+ return name == "Ctrl" || name == "Alt" || name == "Shift" || name == "Mod";
+ }
+ function keyName(event, noShift) {
+ if (opera && event.keyCode == 34 && event["char"]) return false;
+ var name = keyNames[event.keyCode];
+ if (name == null || event.altGraphKey) return false;
+ if (event.altKey) name = "Alt-" + name;
+ if (flipCtrlCmd ? event.metaKey : event.ctrlKey) name = "Ctrl-" + name;
+ if (flipCtrlCmd ? event.ctrlKey : event.metaKey) name = "Cmd-" + name;
+ if (!noShift && event.shiftKey) name = "Shift-" + name;
+ return name;
+ }
+ CodeMirror.lookupKey = lookupKey;
+ CodeMirror.isModifierKey = isModifierKey;
+ CodeMirror.keyName = keyName;
+
+ // FROMTEXTAREA
+
+ CodeMirror.fromTextArea = function(textarea, options) {
+ if (!options) options = {};
+ options.value = textarea.value;
+ if (!options.tabindex && textarea.tabindex)
+ options.tabindex = textarea.tabindex;
+ if (!options.placeholder && textarea.placeholder)
+ options.placeholder = textarea.placeholder;
+ // Set autofocus to true if this textarea is focused, or if it has
+ // autofocus and no other element is focused.
+ if (options.autofocus == null) {
+ var hasFocus = document.body;
+ // doc.activeElement occasionally throws on IE
+ try { hasFocus = document.activeElement; } catch(e) {}
+ options.autofocus = hasFocus == textarea ||
+ textarea.getAttribute("autofocus") != null && hasFocus == document.body;
+ }
+
+ function save() {textarea.value = cm.getValue();}
+ if (textarea.form) {
+ on(textarea.form, "submit", save);
+ // Deplorable hack to make the submit method do the right thing.
+ if (!options.leaveSubmitMethodAlone) {
+ var form = textarea.form, realSubmit = form.submit;
+ try {
+ var wrappedSubmit = form.submit = function() {
+ save();
+ form.submit = realSubmit;
+ form.submit();
+ form.submit = wrappedSubmit;
+ };
+ } catch(e) {}
+ }
+ }
+
+ textarea.style.display = "none";
+ var cm = CodeMirror(function(node) {
+ textarea.parentNode.insertBefore(node, textarea.nextSibling);
+ }, options);
+ cm.save = save;
+ cm.getTextArea = function() { return textarea; };
+ cm.toTextArea = function() {
+ save();
+ textarea.parentNode.removeChild(cm.getWrapperElement());
+ textarea.style.display = "";
+ if (textarea.form) {
+ off(textarea.form, "submit", save);
+ if (typeof textarea.form.submit == "function")
+ textarea.form.submit = realSubmit;
+ }
+ };
+ return cm;
+ };
+
+ // STRING STREAM
+
+ // Fed to the mode parsers, provides helper functions to make
+ // parsers more succinct.
+
+ // The character stream used by a mode's parser.
+ function StringStream(string, tabSize) {
+ this.pos = this.start = 0;
+ this.string = string;
+ this.tabSize = tabSize || 8;
+ this.lastColumnPos = this.lastColumnValue = 0;
+ }
+
+ StringStream.prototype = {
+ eol: function() {return this.pos >= this.string.length;},
+ sol: function() {return this.pos == 0;},
+ peek: function() {return this.string.charAt(this.pos) || undefined;},
+ next: function() {
+ if (this.pos < this.string.length)
+ return this.string.charAt(this.pos++);
+ },
+ eat: function(match) {
+ var ch = this.string.charAt(this.pos);
+ if (typeof match == "string") var ok = ch == match;
+ else var ok = ch && (match.test ? match.test(ch) : match(ch));
+ if (ok) {++this.pos; return ch;}
+ },
+ eatWhile: function(match) {
+ var start = this.pos;
+ while (this.eat(match)){}
+ return this.pos > start;
+ },
+ eatSpace: function() {
+ var start = this.pos;
+ while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
+ return this.pos > start;
+ },
+ skipToEnd: function() {this.pos = this.string.length;},
+ skipTo: function(ch) {
+ var found = this.string.indexOf(ch, this.pos);
+ if (found > -1) {this.pos = found; return true;}
+ },
+ backUp: function(n) {this.pos -= n;},
+ column: function() {
+ if (this.lastColumnPos < this.start) {
+ this.lastColumnValue = countColumn(this.string, this.start, this.tabSize, this.lastColumnPos, this.lastColumnValue);
+ this.lastColumnPos = this.start;
+ }
+ return this.lastColumnValue;
+ },
+ indentation: function() {return countColumn(this.string, null, this.tabSize);},
+ match: function(pattern, consume, caseInsensitive) {
+ if (typeof pattern == "string") {
+ var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};
+ var substr = this.string.substr(this.pos, pattern.length);
+ if (cased(substr) == cased(pattern)) {
+ if (consume !== false) this.pos += pattern.length;
+ return true;
+ }
+ } else {
+ var match = this.string.slice(this.pos).match(pattern);
+ if (match && match.index > 0) return null;
+ if (match && consume !== false) this.pos += match[0].length;
+ return match;
+ }
+ },
+ current: function(){return this.string.slice(this.start, this.pos);}
+ };
+ CodeMirror.StringStream = StringStream;
+
+ // TEXTMARKERS
+
+ function TextMarker(doc, type) {
+ this.lines = [];
+ this.type = type;
+ this.doc = doc;
+ }
+ CodeMirror.TextMarker = TextMarker;
+ eventMixin(TextMarker);
+
+ TextMarker.prototype.clear = function() {
+ if (this.explicitlyCleared) return;
+ var cm = this.doc.cm, withOp = cm && !cm.curOp;
+ if (withOp) startOperation(cm);
+ if (hasHandler(this, "clear")) {
+ var found = this.find();
+ if (found) signalLater(this, "clear", found.from, found.to);
+ }
+ var min = null, max = null;
+ for (var i = 0; i < this.lines.length; ++i) {
+ var line = this.lines[i];
+ var span = getMarkedSpanFor(line.markedSpans, this);
+ if (span.to != null) max = lineNo(line);
+ line.markedSpans = removeMarkedSpan(line.markedSpans, span);
+ if (span.from != null)
+ min = lineNo(line);
+ else if (this.collapsed && !lineIsHidden(this.doc, line) && cm)
+ updateLineHeight(line, textHeight(cm.display));
+ }
+ if (cm && this.collapsed && !cm.options.lineWrapping) for (var i = 0; i < this.lines.length; ++i) {
+ var visual = visualLine(cm.doc, this.lines[i]), len = lineLength(cm.doc, visual);
+ if (len > cm.display.maxLineLength) {
+ cm.display.maxLine = visual;
+ cm.display.maxLineLength = len;
+ cm.display.maxLineChanged = true;
+ }
+ }
+
+ if (min != null && cm) regChange(cm, min, max + 1);
+ this.lines.length = 0;
+ this.explicitlyCleared = true;
+ if (this.atomic && this.doc.cantEdit) {
+ this.doc.cantEdit = false;
+ if (cm) reCheckSelection(cm);
+ }
+ if (withOp) endOperation(cm);
+ };
+
+ TextMarker.prototype.find = function() {
+ var from, to;
+ for (var i = 0; i < this.lines.length; ++i) {
+ var line = this.lines[i];
+ var span = getMarkedSpanFor(line.markedSpans, this);
+ if (span.from != null || span.to != null) {
+ var found = lineNo(line);
+ if (span.from != null) from = Pos(found, span.from);
+ if (span.to != null) to = Pos(found, span.to);
+ }
+ }
+ if (this.type == "bookmark") return from;
+ return from && {from: from, to: to};
+ };
+
+ TextMarker.prototype.changed = function() {
+ var pos = this.find(), cm = this.doc.cm;
+ if (!pos || !cm) return;
+ if (this.type != "bookmark") pos = pos.from;
+ var line = getLine(this.doc, pos.line);
+ clearCachedMeasurement(cm, line);
+ if (pos.line >= cm.display.showingFrom && pos.line < cm.display.showingTo) {
+ for (var node = cm.display.lineDiv.firstChild; node; node = node.nextSibling) if (node.lineObj == line) {
+ if (node.offsetHeight != line.height) updateLineHeight(line, node.offsetHeight);
+ break;
+ }
+ runInOp(cm, function() {
+ cm.curOp.selectionChanged = cm.curOp.forceUpdate = cm.curOp.updateMaxLine = true;
+ });
+ }
+ };
+
+ TextMarker.prototype.attachLine = function(line) {
+ if (!this.lines.length && this.doc.cm) {
+ var op = this.doc.cm.curOp;
+ if (!op.maybeHiddenMarkers || indexOf(op.maybeHiddenMarkers, this) == -1)
+ (op.maybeUnhiddenMarkers || (op.maybeUnhiddenMarkers = [])).push(this);
+ }
+ this.lines.push(line);
+ };
+ TextMarker.prototype.detachLine = function(line) {
+ this.lines.splice(indexOf(this.lines, line), 1);
+ if (!this.lines.length && this.doc.cm) {
+ var op = this.doc.cm.curOp;
+ (op.maybeHiddenMarkers || (op.maybeHiddenMarkers = [])).push(this);
+ }
+ };
+
+ function markText(doc, from, to, options, type) {
+ if (options && options.shared) return markTextShared(doc, from, to, options, type);
+ if (doc.cm && !doc.cm.curOp) return operation(doc.cm, markText)(doc, from, to, options, type);
+
+ var marker = new TextMarker(doc, type);
+ if (posLess(to, from) || posEq(from, to) && type == "range" &&
+ !(options.inclusiveLeft && options.inclusiveRight))
+ return marker;
+ if (options) copyObj(options, marker);
+ if (marker.replacedWith) {
+ marker.collapsed = true;
+ marker.replacedWith = elt("span", [marker.replacedWith], "CodeMirror-widget");
+ if (!options.handleMouseEvents) marker.replacedWith.ignoreEvents = true;
+ }
+ if (marker.collapsed) sawCollapsedSpans = true;
+
+ if (marker.addToHistory)
+ addToHistory(doc, {from: from, to: to, origin: "markText"},
+ {head: doc.sel.head, anchor: doc.sel.anchor}, NaN);
+
+ var curLine = from.line, size = 0, collapsedAtStart, collapsedAtEnd, cm = doc.cm, updateMaxLine;
+ doc.iter(curLine, to.line + 1, function(line) {
+ if (cm && marker.collapsed && !cm.options.lineWrapping && visualLine(doc, line) == cm.display.maxLine)
+ updateMaxLine = true;
+ var span = {from: null, to: null, marker: marker};
+ size += line.text.length;
+ if (curLine == from.line) {span.from = from.ch; size -= from.ch;}
+ if (curLine == to.line) {span.to = to.ch; size -= line.text.length - to.ch;}
+ if (marker.collapsed) {
+ if (curLine == to.line) collapsedAtEnd = collapsedSpanAt(line, to.ch);
+ if (curLine == from.line) collapsedAtStart = collapsedSpanAt(line, from.ch);
+ else updateLineHeight(line, 0);
+ }
+ addMarkedSpan(line, span);
+ ++curLine;
+ });
+ if (marker.collapsed) doc.iter(from.line, to.line + 1, function(line) {
+ if (lineIsHidden(doc, line)) updateLineHeight(line, 0);
+ });
+
+ if (marker.clearOnEnter) on(marker, "beforeCursorEnter", function() { marker.clear(); });
+
+ if (marker.readOnly) {
+ sawReadOnlySpans = true;
+ if (doc.history.done.length || doc.history.undone.length)
+ doc.clearHistory();
+ }
+ if (marker.collapsed) {
+ if (collapsedAtStart != collapsedAtEnd)
+ throw new Error("Inserting collapsed marker overlapping an existing one");
+ marker.size = size;
+ marker.atomic = true;
+ }
+ if (cm) {
+ if (updateMaxLine) cm.curOp.updateMaxLine = true;
+ if (marker.className || marker.title || marker.startStyle || marker.endStyle || marker.collapsed)
+ regChange(cm, from.line, to.line + 1);
+ if (marker.atomic) reCheckSelection(cm);
+ }
+ return marker;
+ }
+
+ // SHARED TEXTMARKERS
+
+ function SharedTextMarker(markers, primary) {
+ this.markers = markers;
+ this.primary = primary;
+ for (var i = 0, me = this; i < markers.length; ++i) {
+ markers[i].parent = this;
+ on(markers[i], "clear", function(){me.clear();});
+ }
+ }
+ CodeMirror.SharedTextMarker = SharedTextMarker;
+ eventMixin(SharedTextMarker);
+
+ SharedTextMarker.prototype.clear = function() {
+ if (this.explicitlyCleared) return;
+ this.explicitlyCleared = true;
+ for (var i = 0; i < this.markers.length; ++i)
+ this.markers[i].clear();
+ signalLater(this, "clear");
+ };
+ SharedTextMarker.prototype.find = function() {
+ return this.primary.find();
+ };
+
+ function markTextShared(doc, from, to, options, type) {
+ options = copyObj(options);
+ options.shared = false;
+ var markers = [markText(doc, from, to, options, type)], primary = markers[0];
+ var widget = options.replacedWith;
+ linkedDocs(doc, function(doc) {
+ if (widget) options.replacedWith = widget.cloneNode(true);
+ markers.push(markText(doc, clipPos(doc, from), clipPos(doc, to), options, type));
+ for (var i = 0; i < doc.linked.length; ++i)
+ if (doc.linked[i].isParent) return;
+ primary = lst(markers);
+ });
+ return new SharedTextMarker(markers, primary);
+ }
+
+ // TEXTMARKER SPANS
+
+ function getMarkedSpanFor(spans, marker) {
+ if (spans) for (var i = 0; i < spans.length; ++i) {
+ var span = spans[i];
+ if (span.marker == marker) return span;
+ }
+ }
+ function removeMarkedSpan(spans, span) {
+ for (var r, i = 0; i < spans.length; ++i)
+ if (spans[i] != span) (r || (r = [])).push(spans[i]);
+ return r;
+ }
+ function addMarkedSpan(line, span) {
+ line.markedSpans = line.markedSpans ? line.markedSpans.concat([span]) : [span];
+ span.marker.attachLine(line);
+ }
+
+ function markedSpansBefore(old, startCh, isInsert) {
+ if (old) for (var i = 0, nw; i < old.length; ++i) {
+ var span = old[i], marker = span.marker;
+ var startsBefore = span.from == null || (marker.inclusiveLeft ? span.from <= startCh : span.from < startCh);
+ if (startsBefore ||
+ (marker.inclusiveLeft && marker.inclusiveRight || marker.type == "bookmark") &&
+ span.from == startCh && (!isInsert || !span.marker.insertLeft)) {
+ var endsAfter = span.to == null || (marker.inclusiveRight ? span.to >= startCh : span.to > startCh);
+ (nw || (nw = [])).push({from: span.from,
+ to: endsAfter ? null : span.to,
+ marker: marker});
+ }
+ }
+ return nw;
+ }
+
+ function markedSpansAfter(old, endCh, isInsert) {
+ if (old) for (var i = 0, nw; i < old.length; ++i) {
+ var span = old[i], marker = span.marker;
+ var endsAfter = span.to == null || (marker.inclusiveRight ? span.to >= endCh : span.to > endCh);
+ if (endsAfter || marker.type == "bookmark" && span.from == endCh && (!isInsert || span.marker.insertLeft)) {
+ var startsBefore = span.from == null || (marker.inclusiveLeft ? span.from <= endCh : span.from < endCh);
+ (nw || (nw = [])).push({from: startsBefore ? null : span.from - endCh,
+ to: span.to == null ? null : span.to - endCh,
+ marker: marker});
+ }
+ }
+ return nw;
+ }
+
+ function stretchSpansOverChange(doc, change) {
+ var oldFirst = isLine(doc, change.from.line) && getLine(doc, change.from.line).markedSpans;
+ var oldLast = isLine(doc, change.to.line) && getLine(doc, change.to.line).markedSpans;
+ if (!oldFirst && !oldLast) return null;
+
+ var startCh = change.from.ch, endCh = change.to.ch, isInsert = posEq(change.from, change.to);
+ // Get the spans that 'stick out' on both sides
+ var first = markedSpansBefore(oldFirst, startCh, isInsert);
+ var last = markedSpansAfter(oldLast, endCh, isInsert);
+
+ // Next, merge those two ends
+ var sameLine = change.text.length == 1, offset = lst(change.text).length + (sameLine ? startCh : 0);
+ if (first) {
+ // Fix up .to properties of first
+ for (var i = 0; i < first.length; ++i) {
+ var span = first[i];
+ if (span.to == null) {
+ var found = getMarkedSpanFor(last, span.marker);
+ if (!found) span.to = startCh;
+ else if (sameLine) span.to = found.to == null ? null : found.to + offset;
+ }
+ }
+ }
+ if (last) {
+ // Fix up .from in last (or move them into first in case of sameLine)
+ for (var i = 0; i < last.length; ++i) {
+ var span = last[i];
+ if (span.to != null) span.to += offset;
+ if (span.from == null) {
+ var found = getMarkedSpanFor(first, span.marker);
+ if (!found) {
+ span.from = offset;
+ if (sameLine) (first || (first = [])).push(span);
+ }
+ } else {
+ span.from += offset;
+ if (sameLine) (first || (first = [])).push(span);
+ }
+ }
+ }
+ if (sameLine && first) {
+ // Make sure we didn't create any zero-length spans
+ for (var i = 0; i < first.length; ++i)
+ if (first[i].from != null && first[i].from == first[i].to && first[i].marker.type != "bookmark")
+ first.splice(i--, 1);
+ if (!first.length) first = null;
+ }
+
+ var newMarkers = [first];
+ if (!sameLine) {
+ // Fill gap with whole-line-spans
+ var gap = change.text.length - 2, gapMarkers;
+ if (gap > 0 && first)
+ for (var i = 0; i < first.length; ++i)
+ if (first[i].to == null)
+ (gapMarkers || (gapMarkers = [])).push({from: null, to: null, marker: first[i].marker});
+ for (var i = 0; i < gap; ++i)
+ newMarkers.push(gapMarkers);
+ newMarkers.push(last);
+ }
+ return newMarkers;
+ }
+
+ function mergeOldSpans(doc, change) {
+ var old = getOldSpans(doc, change);
+ var stretched = stretchSpansOverChange(doc, change);
+ if (!old) return stretched;
+ if (!stretched) return old;
+
+ for (var i = 0; i < old.length; ++i) {
+ var oldCur = old[i], stretchCur = stretched[i];
+ if (oldCur && stretchCur) {
+ spans: for (var j = 0; j < stretchCur.length; ++j) {
+ var span = stretchCur[j];
+ for (var k = 0; k < oldCur.length; ++k)
+ if (oldCur[k].marker == span.marker) continue spans;
+ oldCur.push(span);
+ }
+ } else if (stretchCur) {
+ old[i] = stretchCur;
+ }
+ }
+ return old;
+ }
+
+ function removeReadOnlyRanges(doc, from, to) {
+ var markers = null;
+ doc.iter(from.line, to.line + 1, function(line) {
+ if (line.markedSpans) for (var i = 0; i < line.markedSpans.length; ++i) {
+ var mark = line.markedSpans[i].marker;
+ if (mark.readOnly && (!markers || indexOf(markers, mark) == -1))
+ (markers || (markers = [])).push(mark);
+ }
+ });
+ if (!markers) return null;
+ var parts = [{from: from, to: to}];
+ for (var i = 0; i < markers.length; ++i) {
+ var mk = markers[i], m = mk.find();
+ for (var j = 0; j < parts.length; ++j) {
+ var p = parts[j];
+ if (posLess(p.to, m.from) || posLess(m.to, p.from)) continue;
+ var newParts = [j, 1];
+ if (posLess(p.from, m.from) || !mk.inclusiveLeft && posEq(p.from, m.from))
+ newParts.push({from: p.from, to: m.from});
+ if (posLess(m.to, p.to) || !mk.inclusiveRight && posEq(p.to, m.to))
+ newParts.push({from: m.to, to: p.to});
+ parts.splice.apply(parts, newParts);
+ j += newParts.length - 1;
+ }
+ }
+ return parts;
+ }
+
+ function collapsedSpanAt(line, ch) {
+ var sps = sawCollapsedSpans && line.markedSpans, found;
+ if (sps) for (var sp, i = 0; i < sps.length; ++i) {
+ sp = sps[i];
+ if (!sp.marker.collapsed) continue;
+ if ((sp.from == null || sp.from < ch) &&
+ (sp.to == null || sp.to > ch) &&
+ (!found || found.width < sp.marker.width))
+ found = sp.marker;
+ }
+ return found;
+ }
+ function collapsedSpanAtStart(line) { return collapsedSpanAt(line, -1); }
+ function collapsedSpanAtEnd(line) { return collapsedSpanAt(line, line.text.length + 1); }
+
+ function visualLine(doc, line) {
+ var merged;
+ while (merged = collapsedSpanAtStart(line))
+ line = getLine(doc, merged.find().from.line);
+ return line;
+ }
+
+ function lineIsHidden(doc, line) {
+ var sps = sawCollapsedSpans && line.markedSpans;
+ if (sps) for (var sp, i = 0; i < sps.length; ++i) {
+ sp = sps[i];
+ if (!sp.marker.collapsed) continue;
+ if (sp.from == null) return true;
+ if (sp.marker.replacedWith) continue;
+ if (sp.from == 0 && sp.marker.inclusiveLeft && lineIsHiddenInner(doc, line, sp))
+ return true;
+ }
+ }
+ function lineIsHiddenInner(doc, line, span) {
+ if (span.to == null) {
+ var end = span.marker.find().to, endLine = getLine(doc, end.line);
+ return lineIsHiddenInner(doc, endLine, getMarkedSpanFor(endLine.markedSpans, span.marker));
+ }
+ if (span.marker.inclusiveRight && span.to == line.text.length)
+ return true;
+ for (var sp, i = 0; i < line.markedSpans.length; ++i) {
+ sp = line.markedSpans[i];
+ if (sp.marker.collapsed && !sp.marker.replacedWith && sp.from == span.to &&
+ (sp.marker.inclusiveLeft || span.marker.inclusiveRight) &&
+ lineIsHiddenInner(doc, line, sp)) return true;
+ }
+ }
+
+ function detachMarkedSpans(line) {
+ var spans = line.markedSpans;
+ if (!spans) return;
+ for (var i = 0; i < spans.length; ++i)
+ spans[i].marker.detachLine(line);
+ line.markedSpans = null;
+ }
+
+ function attachMarkedSpans(line, spans) {
+ if (!spans) return;
+ for (var i = 0; i < spans.length; ++i)
+ spans[i].marker.attachLine(line);
+ line.markedSpans = spans;
+ }
+
+ // LINE WIDGETS
+
+ var LineWidget = CodeMirror.LineWidget = function(cm, node, options) {
+ if (options) for (var opt in options) if (options.hasOwnProperty(opt))
+ this[opt] = options[opt];
+ this.cm = cm;
+ this.node = node;
+ };
+ eventMixin(LineWidget);
+ function widgetOperation(f) {
+ return function() {
+ var withOp = !this.cm.curOp;
+ if (withOp) startOperation(this.cm);
+ try {var result = f.apply(this, arguments);}
+ finally {if (withOp) endOperation(this.cm);}
+ return result;
+ };
+ }
+ LineWidget.prototype.clear = widgetOperation(function() {
+ var ws = this.line.widgets, no = lineNo(this.line);
+ if (no == null || !ws) return;
+ for (var i = 0; i < ws.length; ++i) if (ws[i] == this) ws.splice(i--, 1);
+ if (!ws.length) this.line.widgets = null;
+ var aboveVisible = heightAtLine(this.cm, this.line) < this.cm.doc.scrollTop;
+ updateLineHeight(this.line, Math.max(0, this.line.height - widgetHeight(this)));
+ if (aboveVisible) addToScrollPos(this.cm, 0, -this.height);
+ regChange(this.cm, no, no + 1);
+ });
+ LineWidget.prototype.changed = widgetOperation(function() {
+ var oldH = this.height;
+ this.height = null;
+ var diff = widgetHeight(this) - oldH;
+ if (!diff) return;
+ updateLineHeight(this.line, this.line.height + diff);
+ var no = lineNo(this.line);
+ regChange(this.cm, no, no + 1);
+ });
+
+ function widgetHeight(widget) {
+ if (widget.height != null) return widget.height;
+ if (!widget.node.parentNode || widget.node.parentNode.nodeType != 1)
+ removeChildrenAndAdd(widget.cm.display.measure, elt("div", [widget.node], null, "position: relative"));
+ return widget.height = widget.node.offsetHeight;
+ }
+
+ function addLineWidget(cm, handle, node, options) {
+ var widget = new LineWidget(cm, node, options);
+ if (widget.noHScroll) cm.display.alignWidgets = true;
+ changeLine(cm, handle, function(line) {
+ var widgets = line.widgets || (line.widgets = []);
+ if (widget.insertAt == null) widgets.push(widget);
+ else widgets.splice(Math.min(widgets.length - 1, Math.max(0, widget.insertAt)), 0, widget);
+ widget.line = line;
+ if (!lineIsHidden(cm.doc, line) || widget.showIfHidden) {
+ var aboveVisible = heightAtLine(cm, line) < cm.doc.scrollTop;
+ updateLineHeight(line, line.height + widgetHeight(widget));
+ if (aboveVisible) addToScrollPos(cm, 0, widget.height);
+ }
+ return true;
+ });
+ return widget;
+ }
+
+ // LINE DATA STRUCTURE
+
+ // Line objects. These hold state related to a line, including
+ // highlighting info (the styles array).
+ var Line = CodeMirror.Line = function(text, markedSpans, estimateHeight) {
+ this.text = text;
+ attachMarkedSpans(this, markedSpans);
+ this.height = estimateHeight ? estimateHeight(this) : 1;
+ };
+ eventMixin(Line);
+ Line.prototype.lineNo = function() { return lineNo(this); };
+
+ function updateLine(line, text, markedSpans, estimateHeight) {
+ line.text = text;
+ if (line.stateAfter) line.stateAfter = null;
+ if (line.styles) line.styles = null;
+ if (line.order != null) line.order = null;
+ detachMarkedSpans(line);
+ attachMarkedSpans(line, markedSpans);
+ var estHeight = estimateHeight ? estimateHeight(line) : 1;
+ if (estHeight != line.height) updateLineHeight(line, estHeight);
+ }
+
+ function cleanUpLine(line) {
+ line.parent = null;
+ detachMarkedSpans(line);
+ }
+
+ // Run the given mode's parser over a line, update the styles
+ // array, which contains alternating fragments of text and CSS
+ // classes.
+ function runMode(cm, text, mode, state, f, forceToEnd) {
+ var flattenSpans = mode.flattenSpans;
+ if (flattenSpans == null) flattenSpans = cm.options.flattenSpans;
+ var curStart = 0, curStyle = null;
+ var stream = new StringStream(text, cm.options.tabSize), style;
+ if (text == "" && mode.blankLine) mode.blankLine(state);
+ while (!stream.eol()) {
+ if (stream.pos > cm.options.maxHighlightLength) {
+ flattenSpans = false;
+ if (forceToEnd) processLine(cm, text, state, stream.pos);
+ stream.pos = text.length;
+ style = null;
+ } else {
+ style = mode.token(stream, state);
+ }
+ if (!flattenSpans || curStyle != style) {
+ if (curStart < stream.start) f(stream.start, curStyle);
+ curStart = stream.start; curStyle = style;
+ }
+ stream.start = stream.pos;
+ }
+ while (curStart < stream.pos) {
+ // Webkit seems to refuse to render text nodes longer than 57444 characters
+ var pos = Math.min(stream.pos, curStart + 50000);
+ f(pos, curStyle);
+ curStart = pos;
+ }
+ }
+
+ function highlightLine(cm, line, state, forceToEnd) {
+ // A styles array always starts with a number identifying the
+ // mode/overlays that it is based on (for easy invalidation).
+ var st = [cm.state.modeGen];
+ // Compute the base array of styles
+ runMode(cm, line.text, cm.doc.mode, state, function(end, style) {
+ st.push(end, style);
+ }, forceToEnd);
+
+ // Run overlays, adjust style array.
+ for (var o = 0; o < cm.state.overlays.length; ++o) {
+ var overlay = cm.state.overlays[o], i = 1, at = 0;
+ runMode(cm, line.text, overlay.mode, true, function(end, style) {
+ var start = i;
+ // Ensure there's a token end at the current position, and that i points at it
+ while (at < end) {
+ var i_end = st[i];
+ if (i_end > end)
+ st.splice(i, 1, end, st[i+1], i_end);
+ i += 2;
+ at = Math.min(end, i_end);
+ }
+ if (!style) return;
+ if (overlay.opaque) {
+ st.splice(start, i - start, end, style);
+ i = start + 2;
+ } else {
+ for (; start < i; start += 2) {
+ var cur = st[start+1];
+ st[start+1] = cur ? cur + " " + style : style;
+ }
+ }
+ });
+ }
+
+ return st;
+ }
+
+ function getLineStyles(cm, line) {
+ if (!line.styles || line.styles[0] != cm.state.modeGen)
+ line.styles = highlightLine(cm, line, line.stateAfter = getStateBefore(cm, lineNo(line)));
+ return line.styles;
+ }
+
+ // Lightweight form of highlight -- proceed over this line and
+ // update state, but don't save a style array.
+ function processLine(cm, text, state, startAt) {
+ var mode = cm.doc.mode;
+ var stream = new StringStream(text, cm.options.tabSize);
+ stream.start = stream.pos = startAt || 0;
+ if (text == "" && mode.blankLine) mode.blankLine(state);
+ while (!stream.eol() && stream.pos <= cm.options.maxHighlightLength) {
+ mode.token(stream, state);
+ stream.start = stream.pos;
+ }
+ }
+
+ var styleToClassCache = {};
+ function interpretTokenStyle(style, builder) {
+ if (!style) return null;
+ for (;;) {
+ var lineClass = style.match(/(?:^|\s)line-(background-)?(\S+)/);
+ if (!lineClass) break;
+ style = style.slice(0, lineClass.index) + style.slice(lineClass.index + lineClass[0].length);
+ var prop = lineClass[1] ? "bgClass" : "textClass";
+ if (builder[prop] == null)
+ builder[prop] = lineClass[2];
+ else if (!(new RegExp("(?:^|\s)" + lineClass[2] + "(?:$|\s)")).test(builder[prop]))
+ builder[prop] += " " + lineClass[2];
+ }
+ return styleToClassCache[style] ||
+ (styleToClassCache[style] = "cm-" + style.replace(/ +/g, " cm-"));
+ }
+
+ function buildLineContent(cm, realLine, measure, copyWidgets) {
+ var merged, line = realLine, empty = true;
+ while (merged = collapsedSpanAtStart(line))
+ line = getLine(cm.doc, merged.find().from.line);
+
+ var builder = {pre: elt("pre"), col: 0, pos: 0,
+ measure: null, measuredSomething: false, cm: cm,
+ copyWidgets: copyWidgets};
+
+ do {
+ if (line.text) empty = false;
+ builder.measure = line == realLine && measure;
+ builder.pos = 0;
+ builder.addToken = builder.measure ? buildTokenMeasure : buildToken;
+ if ((ie || webkit) && cm.getOption("lineWrapping"))
+ builder.addToken = buildTokenSplitSpaces(builder.addToken);
+ var next = insertLineContent(line, builder, getLineStyles(cm, line));
+ if (measure && line == realLine && !builder.measuredSomething) {
+ measure[0] = builder.pre.appendChild(zeroWidthElement(cm.display.measure));
+ builder.measuredSomething = true;
+ }
+ if (next) line = getLine(cm.doc, next.to.line);
+ } while (next);
+
+ if (measure && !builder.measuredSomething && !measure[0])
+ measure[0] = builder.pre.appendChild(empty ? elt("span", "\u00a0") : zeroWidthElement(cm.display.measure));
+ if (!builder.pre.firstChild && !lineIsHidden(cm.doc, realLine))
+ builder.pre.appendChild(document.createTextNode("\u00a0"));
+
+ var order;
+ // Work around problem with the reported dimensions of single-char
+ // direction spans on IE (issue #1129). See also the comment in
+ // cursorCoords.
+ if (measure && (ie || ie_gt10) && (order = getOrder(line))) {
+ var l = order.length - 1;
+ if (order[l].from == order[l].to) --l;
+ var last = order[l], prev = order[l - 1];
+ if (last.from + 1 == last.to && prev && last.level < prev.level) {
+ var span = measure[builder.pos - 1];
+ if (span) span.parentNode.insertBefore(span.measureRight = zeroWidthElement(cm.display.measure),
+ span.nextSibling);
+ }
+ }
+
+ var textClass = builder.textClass ? builder.textClass + " " + (realLine.textClass || "") : realLine.textClass;
+ if (textClass) builder.pre.className = textClass;
+
+ signal(cm, "renderLine", cm, realLine, builder.pre);
+ return builder;
+ }
+
+ function defaultSpecialCharPlaceholder(ch) {
+ var token = elt("span", "\u2022", "cm-invalidchar");
+ token.title = "\\u" + ch.charCodeAt(0).toString(16);
+ return token;
+ }
+
+ function buildToken(builder, text, style, startStyle, endStyle, title) {
+ if (!text) return;
+ var special = builder.cm.options.specialChars;
+ if (!special.test(text)) {
+ builder.col += text.length;
+ var content = document.createTextNode(text);
+ } else {
+ var content = document.createDocumentFragment(), pos = 0;
+ while (true) {
+ special.lastIndex = pos;
+ var m = special.exec(text);
+ var skipped = m ? m.index - pos : text.length - pos;
+ if (skipped) {
+ content.appendChild(document.createTextNode(text.slice(pos, pos + skipped)));
+ builder.col += skipped;
+ }
+ if (!m) break;
+ pos += skipped + 1;
+ if (m[0] == "\t") {
+ var tabSize = builder.cm.options.tabSize, tabWidth = tabSize - builder.col % tabSize;
+ content.appendChild(elt("span", spaceStr(tabWidth), "cm-tab"));
+ builder.col += tabWidth;
+ } else {
+ var token = builder.cm.options.specialCharPlaceholder(m[0]);
+ content.appendChild(token);
+ builder.col += 1;
+ }
+ }
+ }
+ if (style || startStyle || endStyle || builder.measure) {
+ var fullStyle = style || "";
+ if (startStyle) fullStyle += startStyle;
+ if (endStyle) fullStyle += endStyle;
+ var token = elt("span", [content], fullStyle);
+ if (title) token.title = title;
+ return builder.pre.appendChild(token);
+ }
+ builder.pre.appendChild(content);
+ }
+
+ function buildTokenMeasure(builder, text, style, startStyle, endStyle) {
+ var wrapping = builder.cm.options.lineWrapping;
+ for (var i = 0; i < text.length; ++i) {
+ var ch = text.charAt(i), start = i == 0;
+ if (ch >= "\ud800" && ch < "\udbff" && i < text.length - 1) {
+ ch = text.slice(i, i + 2);
+ ++i;
+ } else if (i && wrapping && spanAffectsWrapping(text, i)) {
+ builder.pre.appendChild(elt("wbr"));
+ }
+ var old = builder.measure[builder.pos];
+ var span = builder.measure[builder.pos] =
+ buildToken(builder, ch, style,
+ start && startStyle, i == text.length - 1 && endStyle);
+ if (old) span.leftSide = old.leftSide || old;
+ // In IE single-space nodes wrap differently than spaces
+ // embedded in larger text nodes, except when set to
+ // white-space: normal (issue #1268).
+ if (ie && wrapping && ch == " " && i && !/\s/.test(text.charAt(i - 1)) &&
+ i < text.length - 1 && !/\s/.test(text.charAt(i + 1)))
+ span.style.whiteSpace = "normal";
+ builder.pos += ch.length;
+ }
+ if (text.length) builder.measuredSomething = true;
+ }
+
+ function buildTokenSplitSpaces(inner) {
+ function split(old) {
+ var out = " ";
+ for (var i = 0; i < old.length - 2; ++i) out += i % 2 ? " " : "\u00a0";
+ out += " ";
+ return out;
+ }
+ return function(builder, text, style, startStyle, endStyle, title) {
+ return inner(builder, text.replace(/ {3,}/g, split), style, startStyle, endStyle, title);
+ };
+ }
+
+ function buildCollapsedSpan(builder, size, marker, ignoreWidget) {
+ var widget = !ignoreWidget && marker.replacedWith;
+ if (widget) {
+ if (builder.copyWidgets) widget = widget.cloneNode(true);
+ builder.pre.appendChild(widget);
+ if (builder.measure) {
+ if (size) {
+ builder.measure[builder.pos] = widget;
+ } else {
+ var elt = zeroWidthElement(builder.cm.display.measure);
+ if (marker.type == "bookmark" && !marker.insertLeft)
+ builder.measure[builder.pos] = builder.pre.appendChild(elt);
+ else if (builder.measure[builder.pos])
+ return;
+ else
+ builder.measure[builder.pos] = builder.pre.insertBefore(elt, widget);
+ }
+ builder.measuredSomething = true;
+ }
+ }
+ builder.pos += size;
+ }
+
+ // Outputs a number of spans to make up a line, taking highlighting
+ // and marked text into account.
+ function insertLineContent(line, builder, styles) {
+ var spans = line.markedSpans, allText = line.text, at = 0;
+ if (!spans) {
+ for (var i = 1; i < styles.length; i+=2)
+ builder.addToken(builder, allText.slice(at, at = styles[i]), interpretTokenStyle(styles[i+1], builder));
+ return;
+ }
+
+ var len = allText.length, pos = 0, i = 1, text = "", style;
+ var nextChange = 0, spanStyle, spanEndStyle, spanStartStyle, title, collapsed;
+ for (;;) {
+ if (nextChange == pos) { // Update current marker set
+ spanStyle = spanEndStyle = spanStartStyle = title = "";
+ collapsed = null; nextChange = Infinity;
+ var foundBookmarks = [];
+ for (var j = 0; j < spans.length; ++j) {
+ var sp = spans[j], m = sp.marker;
+ if (sp.from <= pos && (sp.to == null || sp.to > pos)) {
+ if (sp.to != null && nextChange > sp.to) { nextChange = sp.to; spanEndStyle = ""; }
+ if (m.className) spanStyle += " " + m.className;
+ if (m.startStyle && sp.from == pos) spanStartStyle += " " + m.startStyle;
+ if (m.endStyle && sp.to == nextChange) spanEndStyle += " " + m.endStyle;
+ if (m.title && !title) title = m.title;
+ if (m.collapsed && (!collapsed || collapsed.marker.size < m.size))
+ collapsed = sp;
+ } else if (sp.from > pos && nextChange > sp.from) {
+ nextChange = sp.from;
+ }
+ if (m.type == "bookmark" && sp.from == pos && m.replacedWith) foundBookmarks.push(m);
+ }
+ if (collapsed && (collapsed.from || 0) == pos) {
+ buildCollapsedSpan(builder, (collapsed.to == null ? len : collapsed.to) - pos,
+ collapsed.marker, collapsed.from == null);
+ if (collapsed.to == null) return collapsed.marker.find();
+ }
+ if (!collapsed && foundBookmarks.length) for (var j = 0; j < foundBookmarks.length; ++j)
+ buildCollapsedSpan(builder, 0, foundBookmarks[j]);
+ }
+ if (pos >= len) break;
+
+ var upto = Math.min(len, nextChange);
+ while (true) {
+ if (text) {
+ var end = pos + text.length;
+ if (!collapsed) {
+ var tokenText = end > upto ? text.slice(0, upto - pos) : text;
+ builder.addToken(builder, tokenText, style ? style + spanStyle : spanStyle,
+ spanStartStyle, pos + tokenText.length == nextChange ? spanEndStyle : "", title);
+ }
+ if (end >= upto) {text = text.slice(upto - pos); pos = upto; break;}
+ pos = end;
+ spanStartStyle = "";
+ }
+ text = allText.slice(at, at = styles[i++]);
+ style = interpretTokenStyle(styles[i++], builder);
+ }
+ }
+ }
+
+ // DOCUMENT DATA STRUCTURE
+
+ function updateDoc(doc, change, markedSpans, selAfter, estimateHeight) {
+ function spansFor(n) {return markedSpans ? markedSpans[n] : null;}
+ function update(line, text, spans) {
+ updateLine(line, text, spans, estimateHeight);
+ signalLater(line, "change", line, change);
+ }
+
+ var from = change.from, to = change.to, text = change.text;
+ var firstLine = getLine(doc, from.line), lastLine = getLine(doc, to.line);
+ var lastText = lst(text), lastSpans = spansFor(text.length - 1), nlines = to.line - from.line;
+
+ // First adjust the line structure
+ if (from.ch == 0 && to.ch == 0 && lastText == "" &&
+ (!doc.cm || doc.cm.options.wholeLineUpdateBefore)) {
+ // This is a whole-line replace. Treated specially to make
+ // sure line objects move the way they are supposed to.
+ for (var i = 0, e = text.length - 1, added = []; i < e; ++i)
+ added.push(new Line(text[i], spansFor(i), estimateHeight));
+ update(lastLine, lastLine.text, lastSpans);
+ if (nlines) doc.remove(from.line, nlines);
+ if (added.length) doc.insert(from.line, added);
+ } else if (firstLine == lastLine) {
+ if (text.length == 1) {
+ update(firstLine, firstLine.text.slice(0, from.ch) + lastText + firstLine.text.slice(to.ch), lastSpans);
+ } else {
+ for (var added = [], i = 1, e = text.length - 1; i < e; ++i)
+ added.push(new Line(text[i], spansFor(i), estimateHeight));
+ added.push(new Line(lastText + firstLine.text.slice(to.ch), lastSpans, estimateHeight));
+ update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));
+ doc.insert(from.line + 1, added);
+ }
+ } else if (text.length == 1) {
+ update(firstLine, firstLine.text.slice(0, from.ch) + text[0] + lastLine.text.slice(to.ch), spansFor(0));
+ doc.remove(from.line + 1, nlines);
+ } else {
+ update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));
+ update(lastLine, lastText + lastLine.text.slice(to.ch), lastSpans);
+ for (var i = 1, e = text.length - 1, added = []; i < e; ++i)
+ added.push(new Line(text[i], spansFor(i), estimateHeight));
+ if (nlines > 1) doc.remove(from.line + 1, nlines - 1);
+ doc.insert(from.line + 1, added);
+ }
+
+ signalLater(doc, "change", doc, change);
+ setSelection(doc, selAfter.anchor, selAfter.head, null, true);
+ }
+
+ function LeafChunk(lines) {
+ this.lines = lines;
+ this.parent = null;
+ for (var i = 0, e = lines.length, height = 0; i < e; ++i) {
+ lines[i].parent = this;
+ height += lines[i].height;
+ }
+ this.height = height;
+ }
+
+ LeafChunk.prototype = {
+ chunkSize: function() { return this.lines.length; },
+ removeInner: function(at, n) {
+ for (var i = at, e = at + n; i < e; ++i) {
+ var line = this.lines[i];
+ this.height -= line.height;
+ cleanUpLine(line);
+ signalLater(line, "delete");
+ }
+ this.lines.splice(at, n);
+ },
+ collapse: function(lines) {
+ lines.splice.apply(lines, [lines.length, 0].concat(this.lines));
+ },
+ insertInner: function(at, lines, height) {
+ this.height += height;
+ this.lines = this.lines.slice(0, at).concat(lines).concat(this.lines.slice(at));
+ for (var i = 0, e = lines.length; i < e; ++i) lines[i].parent = this;
+ },
+ iterN: function(at, n, op) {
+ for (var e = at + n; at < e; ++at)
+ if (op(this.lines[at])) return true;
+ }
+ };
+
+ function BranchChunk(children) {
+ this.children = children;
+ var size = 0, height = 0;
+ for (var i = 0, e = children.length; i < e; ++i) {
+ var ch = children[i];
+ size += ch.chunkSize(); height += ch.height;
+ ch.parent = this;
+ }
+ this.size = size;
+ this.height = height;
+ this.parent = null;
+ }
+
+ BranchChunk.prototype = {
+ chunkSize: function() { return this.size; },
+ removeInner: function(at, n) {
+ this.size -= n;
+ for (var i = 0; i < this.children.length; ++i) {
+ var child = this.children[i], sz = child.chunkSize();
+ if (at < sz) {
+ var rm = Math.min(n, sz - at), oldHeight = child.height;
+ child.removeInner(at, rm);
+ this.height -= oldHeight - child.height;
+ if (sz == rm) { this.children.splice(i--, 1); child.parent = null; }
+ if ((n -= rm) == 0) break;
+ at = 0;
+ } else at -= sz;
+ }
+ if (this.size - n < 25) {
+ var lines = [];
+ this.collapse(lines);
+ this.children = [new LeafChunk(lines)];
+ this.children[0].parent = this;
+ }
+ },
+ collapse: function(lines) {
+ for (var i = 0, e = this.children.length; i < e; ++i) this.children[i].collapse(lines);
+ },
+ insertInner: function(at, lines, height) {
+ this.size += lines.length;
+ this.height += height;
+ for (var i = 0, e = this.children.length; i < e; ++i) {
+ var child = this.children[i], sz = child.chunkSize();
+ if (at <= sz) {
+ child.insertInner(at, lines, height);
+ if (child.lines && child.lines.length > 50) {
+ while (child.lines.length > 50) {
+ var spilled = child.lines.splice(child.lines.length - 25, 25);
+ var newleaf = new LeafChunk(spilled);
+ child.height -= newleaf.height;
+ this.children.splice(i + 1, 0, newleaf);
+ newleaf.parent = this;
+ }
+ this.maybeSpill();
+ }
+ break;
+ }
+ at -= sz;
+ }
+ },
+ maybeSpill: function() {
+ if (this.children.length <= 10) return;
+ var me = this;
+ do {
+ var spilled = me.children.splice(me.children.length - 5, 5);
+ var sibling = new BranchChunk(spilled);
+ if (!me.parent) { // Become the parent node
+ var copy = new BranchChunk(me.children);
+ copy.parent = me;
+ me.children = [copy, sibling];
+ me = copy;
+ } else {
+ me.size -= sibling.size;
+ me.height -= sibling.height;
+ var myIndex = indexOf(me.parent.children, me);
+ me.parent.children.splice(myIndex + 1, 0, sibling);
+ }
+ sibling.parent = me.parent;
+ } while (me.children.length > 10);
+ me.parent.maybeSpill();
+ },
+ iterN: function(at, n, op) {
+ for (var i = 0, e = this.children.length; i < e; ++i) {
+ var child = this.children[i], sz = child.chunkSize();
+ if (at < sz) {
+ var used = Math.min(n, sz - at);
+ if (child.iterN(at, used, op)) return true;
+ if ((n -= used) == 0) break;
+ at = 0;
+ } else at -= sz;
+ }
+ }
+ };
+
+ var nextDocId = 0;
+ var Doc = CodeMirror.Doc = function(text, mode, firstLine) {
+ if (!(this instanceof Doc)) return new Doc(text, mode, firstLine);
+ if (firstLine == null) firstLine = 0;
+
+ BranchChunk.call(this, [new LeafChunk([new Line("", null)])]);
+ this.first = firstLine;
+ this.scrollTop = this.scrollLeft = 0;
+ this.cantEdit = false;
+ this.history = makeHistory();
+ this.cleanGeneration = 1;
+ this.frontier = firstLine;
+ var start = Pos(firstLine, 0);
+ this.sel = {from: start, to: start, head: start, anchor: start, shift: false, extend: false, goalColumn: null};
+ this.id = ++nextDocId;
+ this.modeOption = mode;
+
+ if (typeof text == "string") text = splitLines(text);
+ updateDoc(this, {from: start, to: start, text: text}, null, {head: start, anchor: start});
+ };
+
+ Doc.prototype = createObj(BranchChunk.prototype, {
+ constructor: Doc,
+ iter: function(from, to, op) {
+ if (op) this.iterN(from - this.first, to - from, op);
+ else this.iterN(this.first, this.first + this.size, from);
+ },
+
+ insert: function(at, lines) {
+ var height = 0;
+ for (var i = 0, e = lines.length; i < e; ++i) height += lines[i].height;
+ this.insertInner(at - this.first, lines, height);
+ },
+ remove: function(at, n) { this.removeInner(at - this.first, n); },
+
+ getValue: function(lineSep) {
+ var lines = getLines(this, this.first, this.first + this.size);
+ if (lineSep === false) return lines;
+ return lines.join(lineSep || "\n");
+ },
+ setValue: function(code) {
+ var top = Pos(this.first, 0), last = this.first + this.size - 1;
+ makeChange(this, {from: top, to: Pos(last, getLine(this, last).text.length),
+ text: splitLines(code), origin: "setValue"},
+ {head: top, anchor: top}, true);
+ },
+ replaceRange: function(code, from, to, origin) {
+ from = clipPos(this, from);
+ to = to ? clipPos(this, to) : from;
+ replaceRange(this, code, from, to, origin);
+ },
+ getRange: function(from, to, lineSep) {
+ var lines = getBetween(this, clipPos(this, from), clipPos(this, to));
+ if (lineSep === false) return lines;
+ return lines.join(lineSep || "\n");
+ },
+
+ getLine: function(line) {var l = this.getLineHandle(line); return l && l.text;},
+ setLine: function(line, text) {
+ if (isLine(this, line))
+ replaceRange(this, text, Pos(line, 0), clipPos(this, Pos(line)));
+ },
+ removeLine: function(line) {
+ if (line) replaceRange(this, "", clipPos(this, Pos(line - 1)), clipPos(this, Pos(line)));
+ else replaceRange(this, "", Pos(0, 0), clipPos(this, Pos(1, 0)));
+ },
+
+ getLineHandle: function(line) {if (isLine(this, line)) return getLine(this, line);},
+ getLineNumber: function(line) {return lineNo(line);},
+
+ getLineHandleVisualStart: function(line) {
+ if (typeof line == "number") line = getLine(this, line);
+ return visualLine(this, line);
+ },
+
+ lineCount: function() {return this.size;},
+ firstLine: function() {return this.first;},
+ lastLine: function() {return this.first + this.size - 1;},
+
+ clipPos: function(pos) {return clipPos(this, pos);},
+
+ getCursor: function(start) {
+ var sel = this.sel, pos;
+ if (start == null || start == "head") pos = sel.head;
+ else if (start == "anchor") pos = sel.anchor;
+ else if (start == "end" || start === false) pos = sel.to;
+ else pos = sel.from;
+ return copyPos(pos);
+ },
+ somethingSelected: function() {return !posEq(this.sel.head, this.sel.anchor);},
+
+ setCursor: docOperation(function(line, ch, extend) {
+ var pos = clipPos(this, typeof line == "number" ? Pos(line, ch || 0) : line);
+ if (extend) extendSelection(this, pos);
+ else setSelection(this, pos, pos);
+ }),
+ setSelection: docOperation(function(anchor, head, bias) {
+ setSelection(this, clipPos(this, anchor), clipPos(this, head || anchor), bias);
+ }),
+ extendSelection: docOperation(function(from, to, bias) {
+ extendSelection(this, clipPos(this, from), to && clipPos(this, to), bias);
+ }),
+
+ getSelection: function(lineSep) {return this.getRange(this.sel.from, this.sel.to, lineSep);},
+ replaceSelection: function(code, collapse, origin) {
+ makeChange(this, {from: this.sel.from, to: this.sel.to, text: splitLines(code), origin: origin}, collapse || "around");
+ },
+ undo: docOperation(function() {makeChangeFromHistory(this, "undo");}),
+ redo: docOperation(function() {makeChangeFromHistory(this, "redo");}),
+
+ setExtending: function(val) {this.sel.extend = val;},
+
+ historySize: function() {
+ var hist = this.history;
+ return {undo: hist.done.length, redo: hist.undone.length};
+ },
+ clearHistory: function() {this.history = makeHistory(this.history.maxGeneration);},
+
+ markClean: function() {
+ this.cleanGeneration = this.changeGeneration();
+ },
+ changeGeneration: function() {
+ this.history.lastOp = this.history.lastOrigin = null;
+ return this.history.generation;
+ },
+ isClean: function (gen) {
+ return this.history.generation == (gen || this.cleanGeneration);
+ },
+
+ getHistory: function() {
+ return {done: copyHistoryArray(this.history.done),
+ undone: copyHistoryArray(this.history.undone)};
+ },
+ setHistory: function(histData) {
+ var hist = this.history = makeHistory(this.history.maxGeneration);
+ hist.done = histData.done.slice(0);
+ hist.undone = histData.undone.slice(0);
+ },
+
+ markText: function(from, to, options) {
+ return markText(this, clipPos(this, from), clipPos(this, to), options, "range");
+ },
+ setBookmark: function(pos, options) {
+ var realOpts = {replacedWith: options && (options.nodeType == null ? options.widget : options),
+ insertLeft: options && options.insertLeft};
+ pos = clipPos(this, pos);
+ return markText(this, pos, pos, realOpts, "bookmark");
+ },
+ findMarksAt: function(pos) {
+ pos = clipPos(this, pos);
+ var markers = [], spans = getLine(this, pos.line).markedSpans;
+ if (spans) for (var i = 0; i < spans.length; ++i) {
+ var span = spans[i];
+ if ((span.from == null || span.from <= pos.ch) &&
+ (span.to == null || span.to >= pos.ch))
+ markers.push(span.marker.parent || span.marker);
+ }
+ return markers;
+ },
+ getAllMarks: function() {
+ var markers = [];
+ this.iter(function(line) {
+ var sps = line.markedSpans;
+ if (sps) for (var i = 0; i < sps.length; ++i)
+ if (sps[i].from != null) markers.push(sps[i].marker);
+ });
+ return markers;
+ },
+
+ posFromIndex: function(off) {
+ var ch, lineNo = this.first;
+ this.iter(function(line) {
+ var sz = line.text.length + 1;
+ if (sz > off) { ch = off; return true; }
+ off -= sz;
+ ++lineNo;
+ });
+ return clipPos(this, Pos(lineNo, ch));
+ },
+ indexFromPos: function (coords) {
+ coords = clipPos(this, coords);
+ var index = coords.ch;
+ if (coords.line < this.first || coords.ch < 0) return 0;
+ this.iter(this.first, coords.line, function (line) {
+ index += line.text.length + 1;
+ });
+ return index;
+ },
+
+ copy: function(copyHistory) {
+ var doc = new Doc(getLines(this, this.first, this.first + this.size), this.modeOption, this.first);
+ doc.scrollTop = this.scrollTop; doc.scrollLeft = this.scrollLeft;
+ doc.sel = {from: this.sel.from, to: this.sel.to, head: this.sel.head, anchor: this.sel.anchor,
+ shift: this.sel.shift, extend: false, goalColumn: this.sel.goalColumn};
+ if (copyHistory) {
+ doc.history.undoDepth = this.history.undoDepth;
+ doc.setHistory(this.getHistory());
+ }
+ return doc;
+ },
+
+ linkedDoc: function(options) {
+ if (!options) options = {};
+ var from = this.first, to = this.first + this.size;
+ if (options.from != null && options.from > from) from = options.from;
+ if (options.to != null && options.to < to) to = options.to;
+ var copy = new Doc(getLines(this, from, to), options.mode || this.modeOption, from);
+ if (options.sharedHist) copy.history = this.history;
+ (this.linked || (this.linked = [])).push({doc: copy, sharedHist: options.sharedHist});
+ copy.linked = [{doc: this, isParent: true, sharedHist: options.sharedHist}];
+ return copy;
+ },
+ unlinkDoc: function(other) {
+ if (other instanceof CodeMirror) other = other.doc;
+ if (this.linked) for (var i = 0; i < this.linked.length; ++i) {
+ var link = this.linked[i];
+ if (link.doc != other) continue;
+ this.linked.splice(i, 1);
+ other.unlinkDoc(this);
+ break;
+ }
+ // If the histories were shared, split them again
+ if (other.history == this.history) {
+ var splitIds = [other.id];
+ linkedDocs(other, function(doc) {splitIds.push(doc.id);}, true);
+ other.history = makeHistory();
+ other.history.done = copyHistoryArray(this.history.done, splitIds);
+ other.history.undone = copyHistoryArray(this.history.undone, splitIds);
+ }
+ },
+ iterLinkedDocs: function(f) {linkedDocs(this, f);},
+
+ getMode: function() {return this.mode;},
+ getEditor: function() {return this.cm;}
+ });
+
+ Doc.prototype.eachLine = Doc.prototype.iter;
+
+ // The Doc methods that should be available on CodeMirror instances
+ var dontDelegate = "iter insert remove copy getEditor".split(" ");
+ for (var prop in Doc.prototype) if (Doc.prototype.hasOwnProperty(prop) && indexOf(dontDelegate, prop) < 0)
+ CodeMirror.prototype[prop] = (function(method) {
+ return function() {return method.apply(this.doc, arguments);};
+ })(Doc.prototype[prop]);
+
+ eventMixin(Doc);
+
+ function linkedDocs(doc, f, sharedHistOnly) {
+ function propagate(doc, skip, sharedHist) {
+ if (doc.linked) for (var i = 0; i < doc.linked.length; ++i) {
+ var rel = doc.linked[i];
+ if (rel.doc == skip) continue;
+ var shared = sharedHist && rel.sharedHist;
+ if (sharedHistOnly && !shared) continue;
+ f(rel.doc, shared);
+ propagate(rel.doc, doc, shared);
+ }
+ }
+ propagate(doc, null, true);
+ }
+
+ function attachDoc(cm, doc) {
+ if (doc.cm) throw new Error("This document is already in use.");
+ cm.doc = doc;
+ doc.cm = cm;
+ estimateLineHeights(cm);
+ loadMode(cm);
+ if (!cm.options.lineWrapping) computeMaxLength(cm);
+ cm.options.mode = doc.modeOption;
+ regChange(cm);
+ }
+
+ // LINE UTILITIES
+
+ function getLine(chunk, n) {
+ n -= chunk.first;
+ while (!chunk.lines) {
+ for (var i = 0;; ++i) {
+ var child = chunk.children[i], sz = child.chunkSize();
+ if (n < sz) { chunk = child; break; }
+ n -= sz;
+ }
+ }
+ return chunk.lines[n];
+ }
+
+ function getBetween(doc, start, end) {
+ var out = [], n = start.line;
+ doc.iter(start.line, end.line + 1, function(line) {
+ var text = line.text;
+ if (n == end.line) text = text.slice(0, end.ch);
+ if (n == start.line) text = text.slice(start.ch);
+ out.push(text);
+ ++n;
+ });
+ return out;
+ }
+ function getLines(doc, from, to) {
+ var out = [];
+ doc.iter(from, to, function(line) { out.push(line.text); });
+ return out;
+ }
+
+ function updateLineHeight(line, height) {
+ var diff = height - line.height;
+ for (var n = line; n; n = n.parent) n.height += diff;
+ }
+
+ function lineNo(line) {
+ if (line.parent == null) return null;
+ var cur = line.parent, no = indexOf(cur.lines, line);
+ for (var chunk = cur.parent; chunk; cur = chunk, chunk = chunk.parent) {
+ for (var i = 0;; ++i) {
+ if (chunk.children[i] == cur) break;
+ no += chunk.children[i].chunkSize();
+ }
+ }
+ return no + cur.first;
+ }
+
+ function lineAtHeight(chunk, h) {
+ var n = chunk.first;
+ outer: do {
+ for (var i = 0, e = chunk.children.length; i < e; ++i) {
+ var child = chunk.children[i], ch = child.height;
+ if (h < ch) { chunk = child; continue outer; }
+ h -= ch;
+ n += child.chunkSize();
+ }
+ return n;
+ } while (!chunk.lines);
+ for (var i = 0, e = chunk.lines.length; i < e; ++i) {
+ var line = chunk.lines[i], lh = line.height;
+ if (h < lh) break;
+ h -= lh;
+ }
+ return n + i;
+ }
+
+ function heightAtLine(cm, lineObj) {
+ lineObj = visualLine(cm.doc, lineObj);
+
+ var h = 0, chunk = lineObj.parent;
+ for (var i = 0; i < chunk.lines.length; ++i) {
+ var line = chunk.lines[i];
+ if (line == lineObj) break;
+ else h += line.height;
+ }
+ for (var p = chunk.parent; p; chunk = p, p = chunk.parent) {
+ for (var i = 0; i < p.children.length; ++i) {
+ var cur = p.children[i];
+ if (cur == chunk) break;
+ else h += cur.height;
+ }
+ }
+ return h;
+ }
+
+ function getOrder(line) {
+ var order = line.order;
+ if (order == null) order = line.order = bidiOrdering(line.text);
+ return order;
+ }
+
+ // HISTORY
+
+ function makeHistory(startGen) {
+ return {
+ // Arrays of history events. Doing something adds an event to
+ // done and clears undo. Undoing moves events from done to
+ // undone, redoing moves them in the other direction.
+ done: [], undone: [], undoDepth: Infinity,
+ // Used to track when changes can be merged into a single undo
+ // event
+ lastTime: 0, lastOp: null, lastOrigin: null,
+ // Used by the isClean() method
+ generation: startGen || 1, maxGeneration: startGen || 1
+ };
+ }
+
+ function attachLocalSpans(doc, change, from, to) {
+ var existing = change["spans_" + doc.id], n = 0;
+ doc.iter(Math.max(doc.first, from), Math.min(doc.first + doc.size, to), function(line) {
+ if (line.markedSpans)
+ (existing || (existing = change["spans_" + doc.id] = {}))[n] = line.markedSpans;
+ ++n;
+ });
+ }
+
+ function historyChangeFromChange(doc, change) {
+ var from = { line: change.from.line, ch: change.from.ch };
+ var histChange = {from: from, to: changeEnd(change), text: getBetween(doc, change.from, change.to)};
+ attachLocalSpans(doc, histChange, change.from.line, change.to.line + 1);
+ linkedDocs(doc, function(doc) {attachLocalSpans(doc, histChange, change.from.line, change.to.line + 1);}, true);
+ return histChange;
+ }
+
+ function addToHistory(doc, change, selAfter, opId) {
+ var hist = doc.history;
+ hist.undone.length = 0;
+ var time = +new Date, cur = lst(hist.done);
+
+ if (cur &&
+ (hist.lastOp == opId ||
+ hist.lastOrigin == change.origin && change.origin &&
+ ((change.origin.charAt(0) == "+" && doc.cm && hist.lastTime > time - doc.cm.options.historyEventDelay) ||
+ change.origin.charAt(0) == "*"))) {
+ // Merge this change into the last event
+ var last = lst(cur.changes);
+ if (posEq(change.from, change.to) && posEq(change.from, last.to)) {
+ // Optimized case for simple insertion -- don't want to add
+ // new changesets for every character typed
+ last.to = changeEnd(change);
+ } else {
+ // Add new sub-event
+ cur.changes.push(historyChangeFromChange(doc, change));
+ }
+ cur.anchorAfter = selAfter.anchor; cur.headAfter = selAfter.head;
+ } else {
+ // Can not be merged, start a new event.
+ cur = {changes: [historyChangeFromChange(doc, change)],
+ generation: hist.generation,
+ anchorBefore: doc.sel.anchor, headBefore: doc.sel.head,
+ anchorAfter: selAfter.anchor, headAfter: selAfter.head};
+ hist.done.push(cur);
+ hist.generation = ++hist.maxGeneration;
+ while (hist.done.length > hist.undoDepth)
+ hist.done.shift();
+ }
+ hist.lastTime = time;
+ hist.lastOp = opId;
+ hist.lastOrigin = change.origin;
+ }
+
+ function removeClearedSpans(spans) {
+ if (!spans) return null;
+ for (var i = 0, out; i < spans.length; ++i) {
+ if (spans[i].marker.explicitlyCleared) { if (!out) out = spans.slice(0, i); }
+ else if (out) out.push(spans[i]);
+ }
+ return !out ? spans : out.length ? out : null;
+ }
+
+ function getOldSpans(doc, change) {
+ var found = change["spans_" + doc.id];
+ if (!found) return null;
+ for (var i = 0, nw = []; i < change.text.length; ++i)
+ nw.push(removeClearedSpans(found[i]));
+ return nw;
+ }
+
+ // Used both to provide a JSON-safe object in .getHistory, and, when
+ // detaching a document, to split the history in two
+ function copyHistoryArray(events, newGroup) {
+ for (var i = 0, copy = []; i < events.length; ++i) {
+ var event = events[i], changes = event.changes, newChanges = [];
+ copy.push({changes: newChanges, anchorBefore: event.anchorBefore, headBefore: event.headBefore,
+ anchorAfter: event.anchorAfter, headAfter: event.headAfter});
+ for (var j = 0; j < changes.length; ++j) {
+ var change = changes[j], m;
+ newChanges.push({from: change.from, to: change.to, text: change.text});
+ if (newGroup) for (var prop in change) if (m = prop.match(/^spans_(\d+)$/)) {
+ if (indexOf(newGroup, Number(m[1])) > -1) {
+ lst(newChanges)[prop] = change[prop];
+ delete change[prop];
+ }
+ }
+ }
+ }
+ return copy;
+ }
+
+ // Rebasing/resetting history to deal with externally-sourced changes
+
+ function rebaseHistSel(pos, from, to, diff) {
+ if (to < pos.line) {
+ pos.line += diff;
+ } else if (from < pos.line) {
+ pos.line = from;
+ pos.ch = 0;
+ }
+ }
+
+ // Tries to rebase an array of history events given a change in the
+ // document. If the change touches the same lines as the event, the
+ // event, and everything 'behind' it, is discarded. If the change is
+ // before the event, the event's positions are updated. Uses a
+ // copy-on-write scheme for the positions, to avoid having to
+ // reallocate them all on every rebase, but also avoid problems with
+ // shared position objects being unsafely updated.
+ function rebaseHistArray(array, from, to, diff) {
+ for (var i = 0; i < array.length; ++i) {
+ var sub = array[i], ok = true;
+ for (var j = 0; j < sub.changes.length; ++j) {
+ var cur = sub.changes[j];
+ if (!sub.copied) { cur.from = copyPos(cur.from); cur.to = copyPos(cur.to); }
+ if (to < cur.from.line) {
+ cur.from.line += diff;
+ cur.to.line += diff;
+ } else if (from <= cur.to.line) {
+ ok = false;
+ break;
+ }
+ }
+ if (!sub.copied) {
+ sub.anchorBefore = copyPos(sub.anchorBefore); sub.headBefore = copyPos(sub.headBefore);
+ sub.anchorAfter = copyPos(sub.anchorAfter); sub.readAfter = copyPos(sub.headAfter);
+ sub.copied = true;
+ }
+ if (!ok) {
+ array.splice(0, i + 1);
+ i = 0;
+ } else {
+ rebaseHistSel(sub.anchorBefore); rebaseHistSel(sub.headBefore);
+ rebaseHistSel(sub.anchorAfter); rebaseHistSel(sub.headAfter);
+ }
+ }
+ }
+
+ function rebaseHist(hist, change) {
+ var from = change.from.line, to = change.to.line, diff = change.text.length - (to - from) - 1;
+ rebaseHistArray(hist.done, from, to, diff);
+ rebaseHistArray(hist.undone, from, to, diff);
+ }
+
+ // EVENT OPERATORS
+
+ function stopMethod() {e_stop(this);}
+ // Ensure an event has a stop method.
+ function addStop(event) {
+ if (!event.stop) event.stop = stopMethod;
+ return event;
+ }
+
+ function e_preventDefault(e) {
+ if (e.preventDefault) e.preventDefault();
+ else e.returnValue = false;
+ }
+ function e_stopPropagation(e) {
+ if (e.stopPropagation) e.stopPropagation();
+ else e.cancelBubble = true;
+ }
+ function e_defaultPrevented(e) {
+ return e.defaultPrevented != null ? e.defaultPrevented : e.returnValue == false;
+ }
+ function e_stop(e) {e_preventDefault(e); e_stopPropagation(e);}
+ CodeMirror.e_stop = e_stop;
+ CodeMirror.e_preventDefault = e_preventDefault;
+ CodeMirror.e_stopPropagation = e_stopPropagation;
+
+ function e_target(e) {return e.target || e.srcElement;}
+ function e_button(e) {
+ var b = e.which;
+ if (b == null) {
+ if (e.button & 1) b = 1;
+ else if (e.button & 2) b = 3;
+ else if (e.button & 4) b = 2;
+ }
+ if (mac && e.ctrlKey && b == 1) b = 3;
+ return b;
+ }
+
+ // EVENT HANDLING
+
+ function on(emitter, type, f) {
+ if (emitter.addEventListener)
+ emitter.addEventListener(type, f, false);
+ else if (emitter.attachEvent)
+ emitter.attachEvent("on" + type, f);
+ else {
+ var map = emitter._handlers || (emitter._handlers = {});
+ var arr = map[type] || (map[type] = []);
+ arr.push(f);
+ }
+ }
+
+ function off(emitter, type, f) {
+ if (emitter.removeEventListener)
+ emitter.removeEventListener(type, f, false);
+ else if (emitter.detachEvent)
+ emitter.detachEvent("on" + type, f);
+ else {
+ var arr = emitter._handlers && emitter._handlers[type];
+ if (!arr) return;
+ for (var i = 0; i < arr.length; ++i)
+ if (arr[i] == f) { arr.splice(i, 1); break; }
+ }
+ }
+
+ function signal(emitter, type /*, values...*/) {
+ var arr = emitter._handlers && emitter._handlers[type];
+ if (!arr) return;
+ var args = Array.prototype.slice.call(arguments, 2);
+ for (var i = 0; i < arr.length; ++i) arr[i].apply(null, args);
+ }
+
+ var delayedCallbacks, delayedCallbackDepth = 0;
+ function signalLater(emitter, type /*, values...*/) {
+ var arr = emitter._handlers && emitter._handlers[type];
+ if (!arr) return;
+ var args = Array.prototype.slice.call(arguments, 2);
+ if (!delayedCallbacks) {
+ ++delayedCallbackDepth;
+ delayedCallbacks = [];
+ setTimeout(fireDelayed, 0);
+ }
+ function bnd(f) {return function(){f.apply(null, args);};};
+ for (var i = 0; i < arr.length; ++i)
+ delayedCallbacks.push(bnd(arr[i]));
+ }
+
+ function signalDOMEvent(cm, e, override) {
+ signal(cm, override || e.type, cm, e);
+ return e_defaultPrevented(e) || e.codemirrorIgnore;
+ }
+
+ function fireDelayed() {
+ --delayedCallbackDepth;
+ var delayed = delayedCallbacks;
+ delayedCallbacks = null;
+ for (var i = 0; i < delayed.length; ++i) delayed[i]();
+ }
+
+ function hasHandler(emitter, type) {
+ var arr = emitter._handlers && emitter._handlers[type];
+ return arr && arr.length > 0;
+ }
+
+ CodeMirror.on = on; CodeMirror.off = off; CodeMirror.signal = signal;
+
+ function eventMixin(ctor) {
+ ctor.prototype.on = function(type, f) {on(this, type, f);};
+ ctor.prototype.off = function(type, f) {off(this, type, f);};
+ }
+
+ // MISC UTILITIES
+
+ // Number of pixels added to scroller and sizer to hide scrollbar
+ var scrollerCutOff = 30;
+
+ // Returned or thrown by various protocols to signal 'I'm not
+ // handling this'.
+ var Pass = CodeMirror.Pass = {toString: function(){return "CodeMirror.Pass";}};
+
+ function Delayed() {this.id = null;}
+ Delayed.prototype = {set: function(ms, f) {clearTimeout(this.id); this.id = setTimeout(f, ms);}};
+
+ // Counts the column offset in a string, taking tabs into account.
+ // Used mostly to find indentation.
+ function countColumn(string, end, tabSize, startIndex, startValue) {
+ if (end == null) {
+ end = string.search(/[^\s\u00a0]/);
+ if (end == -1) end = string.length;
+ }
+ for (var i = startIndex || 0, n = startValue || 0; i < end; ++i) {
+ if (string.charAt(i) == "\t") n += tabSize - (n % tabSize);
+ else ++n;
+ }
+ return n;
+ }
+ CodeMirror.countColumn = countColumn;
+
+ var spaceStrs = [""];
+ function spaceStr(n) {
+ while (spaceStrs.length <= n)
+ spaceStrs.push(lst(spaceStrs) + " ");
+ return spaceStrs[n];
+ }
+
+ function lst(arr) { return arr[arr.length-1]; }
+
+ function selectInput(node) {
+ if (ios) { // Mobile Safari apparently has a bug where select() is broken.
+ node.selectionStart = 0;
+ node.selectionEnd = node.value.length;
+ } else {
+ // Suppress mysterious IE10 errors
+ try { node.select(); }
+ catch(_e) {}
+ }
+ }
+
+ function indexOf(collection, elt) {
+ if (collection.indexOf) return collection.indexOf(elt);
+ for (var i = 0, e = collection.length; i < e; ++i)
+ if (collection[i] == elt) return i;
+ return -1;
+ }
+
+ function createObj(base, props) {
+ function Obj() {}
+ Obj.prototype = base;
+ var inst = new Obj();
+ if (props) copyObj(props, inst);
+ return inst;
+ }
+
+ function copyObj(obj, target) {
+ if (!target) target = {};
+ for (var prop in obj) if (obj.hasOwnProperty(prop)) target[prop] = obj[prop];
+ return target;
+ }
+
+ function emptyArray(size) {
+ for (var a = [], i = 0; i < size; ++i) a.push(undefined);
+ return a;
+ }
+
+ function bind(f) {
+ var args = Array.prototype.slice.call(arguments, 1);
+ return function(){return f.apply(null, args);};
+ }
+
+ var nonASCIISingleCaseWordChar = /[\u3040-\u309f\u30a0-\u30ff\u3400-\u4db5\u4e00-\u9fcc\uac00-\ud7af]/;
+ function isWordChar(ch) {
+ return /\w/.test(ch) || ch > "\x80" &&
+ (ch.toUpperCase() != ch.toLowerCase() || nonASCIISingleCaseWordChar.test(ch));
+ }
+
+ function isEmpty(obj) {
+ for (var n in obj) if (obj.hasOwnProperty(n) && obj[n]) return false;
+ return true;
+ }
+
+ var isExtendingChar = /[\u0300-\u036F\u0483-\u0487\u0488-\u0489\u0591-\u05BD\u05BF\u05C1-\u05C2\u05C4-\u05C5\u05C7\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06DC\u06DF-\u06E4\u06E7-\u06E8\u06EA-\u06ED\uA66F\u1DC0–\u1DFF\u20D0–\u20FF\uA670-\uA672\uA674-\uA67D\uA69F\udc00-\udfff\uFE20–\uFE2F]/;
+
+ // DOM UTILITIES
+
+ function elt(tag, content, className, style) {
+ var e = document.createElement(tag);
+ if (className) e.className = className;
+ if (style) e.style.cssText = style;
+ if (typeof content == "string") setTextContent(e, content);
+ else if (content) for (var i = 0; i < content.length; ++i) e.appendChild(content[i]);
+ return e;
+ }
+
+ function removeChildren(e) {
+ for (var count = e.childNodes.length; count > 0; --count)
+ e.removeChild(e.firstChild);
+ return e;
+ }
+
+ function removeChildrenAndAdd(parent, e) {
+ return removeChildren(parent).appendChild(e);
+ }
+
+ function setTextContent(e, str) {
+ if (ie_lt9) {
+ e.innerHTML = "";
+ e.appendChild(document.createTextNode(str));
+ } else e.textContent = str;
+ }
+
+ function getRect(node) {
+ return node.getBoundingClientRect();
+ }
+ CodeMirror.replaceGetRect = function(f) { getRect = f; };
+
+ // FEATURE DETECTION
+
+ // Detect drag-and-drop
+ var dragAndDrop = function() {
+ // There is *some* kind of drag-and-drop support in IE6-8, but I
+ // couldn't get it to work yet.
+ if (ie_lt9) return false;
+ var div = elt('div');
+ return "draggable" in div || "dragDrop" in div;
+ }();
+
+ // For a reason I have yet to figure out, some browsers disallow
+ // word wrapping between certain characters *only* if a new inline
+ // element is started between them. This makes it hard to reliably
+ // measure the position of things, since that requires inserting an
+ // extra span. This terribly fragile set of tests matches the
+ // character combinations that suffer from this phenomenon on the
+ // various browsers.
+ function spanAffectsWrapping() { return false; }
+ if (gecko) // Only for "$'"
+ spanAffectsWrapping = function(str, i) {
+ return str.charCodeAt(i - 1) == 36 && str.charCodeAt(i) == 39;
+ };
+ else if (safari && !/Version\/([6-9]|\d\d)\b/.test(navigator.userAgent))
+ spanAffectsWrapping = function(str, i) {
+ return /\-[^ \-?]|\?[^ !\'\"\),.\-\/:;\?\]\}]/.test(str.slice(i - 1, i + 1));
+ };
+ else if (webkit && /Chrome\/(?:29|[3-9]\d|\d\d\d)\./.test(navigator.userAgent))
+ spanAffectsWrapping = function(str, i) {
+ var code = str.charCodeAt(i - 1);
+ return code >= 8208 && code <= 8212;
+ };
+ else if (webkit)
+ spanAffectsWrapping = function(str, i) {
+ if (i > 1 && str.charCodeAt(i - 1) == 45) {
+ if (/\w/.test(str.charAt(i - 2)) && /[^\-?\.]/.test(str.charAt(i))) return true;
+ if (i > 2 && /[\d\.,]/.test(str.charAt(i - 2)) && /[\d\.,]/.test(str.charAt(i))) return false;
+ }
+ return /[~!#%&*)=+}\]\\|\"\.>,:;][({[<]|-[^\-?\.\u2010-\u201f\u2026]|\?[\w~`@#$%\^&*(_=+{[|><]|…[\w~`@#$%\^&*(_=+{[><]/.test(str.slice(i - 1, i + 1));
+ };
+
+ var knownScrollbarWidth;
+ function scrollbarWidth(measure) {
+ if (knownScrollbarWidth != null) return knownScrollbarWidth;
+ var test = elt("div", null, null, "width: 50px; height: 50px; overflow-x: scroll");
+ removeChildrenAndAdd(measure, test);
+ if (test.offsetWidth)
+ knownScrollbarWidth = test.offsetHeight - test.clientHeight;
+ return knownScrollbarWidth || 0;
+ }
+
+ var zwspSupported;
+ function zeroWidthElement(measure) {
+ if (zwspSupported == null) {
+ var test = elt("span", "\u200b");
+ removeChildrenAndAdd(measure, elt("span", [test, document.createTextNode("x")]));
+ if (measure.firstChild.offsetHeight != 0)
+ zwspSupported = test.offsetWidth <= 1 && test.offsetHeight > 2 && !ie_lt8;
+ }
+ if (zwspSupported) return elt("span", "\u200b");
+ else return elt("span", "\u00a0", null, "display: inline-block; width: 1px; margin-right: -1px");
+ }
+
+ // See if "".split is the broken IE version, if so, provide an
+ // alternative way to split lines.
+ var splitLines = "\n\nb".split(/\n/).length != 3 ? function(string) {
+ var pos = 0, result = [], l = string.length;
+ while (pos <= l) {
+ var nl = string.indexOf("\n", pos);
+ if (nl == -1) nl = string.length;
+ var line = string.slice(pos, string.charAt(nl - 1) == "\r" ? nl - 1 : nl);
+ var rt = line.indexOf("\r");
+ if (rt != -1) {
+ result.push(line.slice(0, rt));
+ pos += rt + 1;
+ } else {
+ result.push(line);
+ pos = nl + 1;
+ }
+ }
+ return result;
+ } : function(string){return string.split(/\r\n?|\n/);};
+ CodeMirror.splitLines = splitLines;
+
+ var hasSelection = window.getSelection ? function(te) {
+ try { return te.selectionStart != te.selectionEnd; }
+ catch(e) { return false; }
+ } : function(te) {
+ try {var range = te.ownerDocument.selection.createRange();}
+ catch(e) {}
+ if (!range || range.parentElement() != te) return false;
+ return range.compareEndPoints("StartToEnd", range) != 0;
+ };
+
+ var hasCopyEvent = (function() {
+ var e = elt("div");
+ if ("oncopy" in e) return true;
+ e.setAttribute("oncopy", "return;");
+ return typeof e.oncopy == 'function';
+ })();
+
+ // KEY NAMING
+
+ var keyNames = {3: "Enter", 8: "Backspace", 9: "Tab", 13: "Enter", 16: "Shift", 17: "Ctrl", 18: "Alt",
+ 19: "Pause", 20: "CapsLock", 27: "Esc", 32: "Space", 33: "PageUp", 34: "PageDown", 35: "End",
+ 36: "Home", 37: "Left", 38: "Up", 39: "Right", 40: "Down", 44: "PrintScrn", 45: "Insert",
+ 46: "Delete", 59: ";", 91: "Mod", 92: "Mod", 93: "Mod", 109: "-", 107: "=", 127: "Delete",
+ 186: ";", 187: "=", 188: ",", 189: "-", 190: ".", 191: "/", 192: "`", 219: "[", 220: "\\",
+ 221: "]", 222: "'", 63276: "PageUp", 63277: "PageDown", 63275: "End", 63273: "Home",
+ 63234: "Left", 63232: "Up", 63235: "Right", 63233: "Down", 63302: "Insert", 63272: "Delete"};
+ CodeMirror.keyNames = keyNames;
+ (function() {
+ // Number keys
+ for (var i = 0; i < 10; i++) keyNames[i + 48] = String(i);
+ // Alphabetic keys
+ for (var i = 65; i <= 90; i++) keyNames[i] = String.fromCharCode(i);
+ // Function keys
+ for (var i = 1; i <= 12; i++) keyNames[i + 111] = keyNames[i + 63235] = "F" + i;
+ })();
+
+ // BIDI HELPERS
+
+ function iterateBidiSections(order, from, to, f) {
+ if (!order) return f(from, to, "ltr");
+ var found = false;
+ for (var i = 0; i < order.length; ++i) {
+ var part = order[i];
+ if (part.from < to && part.to > from || from == to && part.to == from) {
+ f(Math.max(part.from, from), Math.min(part.to, to), part.level == 1 ? "rtl" : "ltr");
+ found = true;
+ }
+ }
+ if (!found) f(from, to, "ltr");
+ }
+
+ function bidiLeft(part) { return part.level % 2 ? part.to : part.from; }
+ function bidiRight(part) { return part.level % 2 ? part.from : part.to; }
+
+ function lineLeft(line) { var order = getOrder(line); return order ? bidiLeft(order[0]) : 0; }
+ function lineRight(line) {
+ var order = getOrder(line);
+ if (!order) return line.text.length;
+ return bidiRight(lst(order));
+ }
+
+ function lineStart(cm, lineN) {
+ var line = getLine(cm.doc, lineN);
+ var visual = visualLine(cm.doc, line);
+ if (visual != line) lineN = lineNo(visual);
+ var order = getOrder(visual);
+ var ch = !order ? 0 : order[0].level % 2 ? lineRight(visual) : lineLeft(visual);
+ return Pos(lineN, ch);
+ }
+ function lineEnd(cm, lineN) {
+ var merged, line;
+ while (merged = collapsedSpanAtEnd(line = getLine(cm.doc, lineN)))
+ lineN = merged.find().to.line;
+ var order = getOrder(line);
+ var ch = !order ? line.text.length : order[0].level % 2 ? lineLeft(line) : lineRight(line);
+ return Pos(lineN, ch);
+ }
+
+ function compareBidiLevel(order, a, b) {
+ var linedir = order[0].level;
+ if (a == linedir) return true;
+ if (b == linedir) return false;
+ return a < b;
+ }
+ var bidiOther;
+ function getBidiPartAt(order, pos) {
+ for (var i = 0, found; i < order.length; ++i) {
+ var cur = order[i];
+ if (cur.from < pos && cur.to > pos) { bidiOther = null; return i; }
+ if (cur.from == pos || cur.to == pos) {
+ if (found == null) {
+ found = i;
+ } else if (compareBidiLevel(order, cur.level, order[found].level)) {
+ bidiOther = found;
+ return i;
+ } else {
+ bidiOther = i;
+ return found;
+ }
+ }
+ }
+ bidiOther = null;
+ return found;
+ }
+
+ function moveInLine(line, pos, dir, byUnit) {
+ if (!byUnit) return pos + dir;
+ do pos += dir;
+ while (pos > 0 && isExtendingChar.test(line.text.charAt(pos)));
+ return pos;
+ }
+
+ // This is somewhat involved. It is needed in order to move
+ // 'visually' through bi-directional text -- i.e., pressing left
+ // should make the cursor go left, even when in RTL text. The
+ // tricky part is the 'jumps', where RTL and LTR text touch each
+ // other. This often requires the cursor offset to move more than
+ // one unit, in order to visually move one unit.
+ function moveVisually(line, start, dir, byUnit) {
+ var bidi = getOrder(line);
+ if (!bidi) return moveLogically(line, start, dir, byUnit);
+ var pos = getBidiPartAt(bidi, start), part = bidi[pos];
+ var target = moveInLine(line, start, part.level % 2 ? -dir : dir, byUnit);
+
+ for (;;) {
+ if (target > part.from && target < part.to) return target;
+ if (target == part.from || target == part.to) {
+ if (getBidiPartAt(bidi, target) == pos) return target;
+ part = bidi[pos += dir];
+ return (dir > 0) == part.level % 2 ? part.to : part.from;
+ } else {
+ part = bidi[pos += dir];
+ if (!part) return null;
+ if ((dir > 0) == part.level % 2)
+ target = moveInLine(line, part.to, -1, byUnit);
+ else
+ target = moveInLine(line, part.from, 1, byUnit);
+ }
+ }
+ }
+
+ function moveLogically(line, start, dir, byUnit) {
+ var target = start + dir;
+ if (byUnit) while (target > 0 && isExtendingChar.test(line.text.charAt(target))) target += dir;
+ return target < 0 || target > line.text.length ? null : target;
+ }
+
+ // Bidirectional ordering algorithm
+ // See http://unicode.org/reports/tr9/tr9-13.html for the algorithm
+ // that this (partially) implements.
+
+ // One-char codes used for character types:
+ // L (L): Left-to-Right
+ // R (R): Right-to-Left
+ // r (AL): Right-to-Left Arabic
+ // 1 (EN): European Number
+ // + (ES): European Number Separator
+ // % (ET): European Number Terminator
+ // n (AN): Arabic Number
+ // , (CS): Common Number Separator
+ // m (NSM): Non-Spacing Mark
+ // b (BN): Boundary Neutral
+ // s (B): Paragraph Separator
+ // t (S): Segment Separator
+ // w (WS): Whitespace
+ // N (ON): Other Neutrals
+
+ // Returns null if characters are ordered as they appear
+ // (left-to-right), or an array of sections ({from, to, level}
+ // objects) in the order in which they occur visually.
+ var bidiOrdering = (function() {
+ // Character types for codepoints 0 to 0xff
+ var lowTypes = "bbbbbbbbbtstwsbbbbbbbbbbbbbbssstwNN%%%NNNNNN,N,N1111111111NNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNbbbbbbsbbbbbbbbbbbbbbbbbbbbbbbbbb,N%%%%NNNNLNNNNN%%11NLNNN1LNNNNNLLLLLLLLLLLLLLLLLLLLLLLNLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLNLLLLLLLL";
+ // Character types for codepoints 0x600 to 0x6ff
+ var arabicTypes = "rrrrrrrrrrrr,rNNmmmmmmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmmmmmmmmrrrrrrrnnnnnnnnnn%nnrrrmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmmmmmmmmmmmmmNmmmmrrrrrrrrrrrrrrrrrr";
+ function charType(code) {
+ if (code <= 0xff) return lowTypes.charAt(code);
+ else if (0x590 <= code && code <= 0x5f4) return "R";
+ else if (0x600 <= code && code <= 0x6ff) return arabicTypes.charAt(code - 0x600);
+ else if (0x700 <= code && code <= 0x8ac) return "r";
+ else return "L";
+ }
+
+ var bidiRE = /[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac]/;
+ var isNeutral = /[stwN]/, isStrong = /[LRr]/, countsAsLeft = /[Lb1n]/, countsAsNum = /[1n]/;
+ // Browsers seem to always treat the boundaries of block elements as being L.
+ var outerType = "L";
+
+ return function(str) {
+ if (!bidiRE.test(str)) return false;
+ var len = str.length, types = [];
+ for (var i = 0, type; i < len; ++i)
+ types.push(type = charType(str.charCodeAt(i)));
+
+ // W1. Examine each non-spacing mark (NSM) in the level run, and
+ // change the type of the NSM to the type of the previous
+ // character. If the NSM is at the start of the level run, it will
+ // get the type of sor.
+ for (var i = 0, prev = outerType; i < len; ++i) {
+ var type = types[i];
+ if (type == "m") types[i] = prev;
+ else prev = type;
+ }
+
+ // W2. Search backwards from each instance of a European number
+ // until the first strong type (R, L, AL, or sor) is found. If an
+ // AL is found, change the type of the European number to Arabic
+ // number.
+ // W3. Change all ALs to R.
+ for (var i = 0, cur = outerType; i < len; ++i) {
+ var type = types[i];
+ if (type == "1" && cur == "r") types[i] = "n";
+ else if (isStrong.test(type)) { cur = type; if (type == "r") types[i] = "R"; }
+ }
+
+ // W4. A single European separator between two European numbers
+ // changes to a European number. A single common separator between
+ // two numbers of the same type changes to that type.
+ for (var i = 1, prev = types[0]; i < len - 1; ++i) {
+ var type = types[i];
+ if (type == "+" && prev == "1" && types[i+1] == "1") types[i] = "1";
+ else if (type == "," && prev == types[i+1] &&
+ (prev == "1" || prev == "n")) types[i] = prev;
+ prev = type;
+ }
+
+ // W5. A sequence of European terminators adjacent to European
+ // numbers changes to all European numbers.
+ // W6. Otherwise, separators and terminators change to Other
+ // Neutral.
+ for (var i = 0; i < len; ++i) {
+ var type = types[i];
+ if (type == ",") types[i] = "N";
+ else if (type == "%") {
+ for (var end = i + 1; end < len && types[end] == "%"; ++end) {}
+ var replace = (i && types[i-1] == "!") || (end < len - 1 && types[end] == "1") ? "1" : "N";
+ for (var j = i; j < end; ++j) types[j] = replace;
+ i = end - 1;
+ }
+ }
+
+ // W7. Search backwards from each instance of a European number
+ // until the first strong type (R, L, or sor) is found. If an L is
+ // found, then change the type of the European number to L.
+ for (var i = 0, cur = outerType; i < len; ++i) {
+ var type = types[i];
+ if (cur == "L" && type == "1") types[i] = "L";
+ else if (isStrong.test(type)) cur = type;
+ }
+
+ // N1. A sequence of neutrals takes the direction of the
+ // surrounding strong text if the text on both sides has the same
+ // direction. European and Arabic numbers act as if they were R in
+ // terms of their influence on neutrals. Start-of-level-run (sor)
+ // and end-of-level-run (eor) are used at level run boundaries.
+ // N2. Any remaining neutrals take the embedding direction.
+ for (var i = 0; i < len; ++i) {
+ if (isNeutral.test(types[i])) {
+ for (var end = i + 1; end < len && isNeutral.test(types[end]); ++end) {}
+ var before = (i ? types[i-1] : outerType) == "L";
+ var after = (end < len - 1 ? types[end] : outerType) == "L";
+ var replace = before || after ? "L" : "R";
+ for (var j = i; j < end; ++j) types[j] = replace;
+ i = end - 1;
+ }
+ }
+
+ // Here we depart from the documented algorithm, in order to avoid
+ // building up an actual levels array. Since there are only three
+ // levels (0, 1, 2) in an implementation that doesn't take
+ // explicit embedding into account, we can build up the order on
+ // the fly, without following the level-based algorithm.
+ var order = [], m;
+ for (var i = 0; i < len;) {
+ if (countsAsLeft.test(types[i])) {
+ var start = i;
+ for (++i; i < len && countsAsLeft.test(types[i]); ++i) {}
+ order.push({from: start, to: i, level: 0});
+ } else {
+ var pos = i, at = order.length;
+ for (++i; i < len && types[i] != "L"; ++i) {}
+ for (var j = pos; j < i;) {
+ if (countsAsNum.test(types[j])) {
+ if (pos < j) order.splice(at, 0, {from: pos, to: j, level: 1});
+ var nstart = j;
+ for (++j; j < i && countsAsNum.test(types[j]); ++j) {}
+ order.splice(at, 0, {from: nstart, to: j, level: 2});
+ pos = j;
+ } else ++j;
+ }
+ if (pos < i) order.splice(at, 0, {from: pos, to: i, level: 1});
+ }
+ }
+ if (order[0].level == 1 && (m = str.match(/^\s+/))) {
+ order[0].from = m[0].length;
+ order.unshift({from: 0, to: m[0].length, level: 0});
+ }
+ if (lst(order).level == 1 && (m = str.match(/\s+$/))) {
+ lst(order).to -= m[0].length;
+ order.push({from: len - m[0].length, to: len, level: 0});
+ }
+ if (order[0].level != lst(order).level)
+ order.push({from: len, to: len, level: order[0].level});
+
+ return order;
+ };
+ })();
+
+ // THE END
+
+ CodeMirror.version = "3.20.0";
+
+ return CodeMirror;
+})();
diff --git a/plugins/codemirror/codemirror/mode/apl/apl.js b/plugins/codemirror/codemirror/mode/apl/apl.js
new file mode 100644
index 0000000..5c23af8
--- /dev/null
+++ b/plugins/codemirror/codemirror/mode/apl/apl.js
@@ -0,0 +1,160 @@
+CodeMirror.defineMode("apl", function() {
+ var builtInOps = {
+ ".": "innerProduct",
+ "\\": "scan",
+ "/": "reduce",
+ "⌿": "reduce1Axis",
+ "⍀": "scan1Axis",
+ "¨": "each",
+ "⍣": "power"
+ };
+ var builtInFuncs = {
+ "+": ["conjugate", "add"],
+ "−": ["negate", "subtract"],
+ "×": ["signOf", "multiply"],
+ "÷": ["reciprocal", "divide"],
+ "⌈": ["ceiling", "greaterOf"],
+ "⌊": ["floor", "lesserOf"],
+ "∣": ["absolute", "residue"],
+ "⍳": ["indexGenerate", "indexOf"],
+ "?": ["roll", "deal"],
+ "⋆": ["exponentiate", "toThePowerOf"],
+ "⍟": ["naturalLog", "logToTheBase"],
+ "○": ["piTimes", "circularFuncs"],
+ "!": ["factorial", "binomial"],
+ "⌹": ["matrixInverse", "matrixDivide"],
+ "<": [null, "lessThan"],
+ "≤": [null, "lessThanOrEqual"],
+ "=": [null, "equals"],
+ ">": [null, "greaterThan"],
+ "≥": [null, "greaterThanOrEqual"],
+ "≠": [null, "notEqual"],
+ "≡": ["depth", "match"],
+ "≢": [null, "notMatch"],
+ "∈": ["enlist", "membership"],
+ "⍷": [null, "find"],
+ "∪": ["unique", "union"],
+ "∩": [null, "intersection"],
+ "∼": ["not", "without"],
+ "∨": [null, "or"],
+ "∧": [null, "and"],
+ "⍱": [null, "nor"],
+ "⍲": [null, "nand"],
+ "⍴": ["shapeOf", "reshape"],
+ ",": ["ravel", "catenate"],
+ "⍪": [null, "firstAxisCatenate"],
+ "⌽": ["reverse", "rotate"],
+ "⊖": ["axis1Reverse", "axis1Rotate"],
+ "⍉": ["transpose", null],
+ "↑": ["first", "take"],
+ "↓": [null, "drop"],
+ "⊂": ["enclose", "partitionWithAxis"],
+ "⊃": ["diclose", "pick"],
+ "⌷": [null, "index"],
+ "⍋": ["gradeUp", null],
+ "⍒": ["gradeDown", null],
+ "⊤": ["encode", null],
+ "⊥": ["decode", null],
+ "⍕": ["format", "formatByExample"],
+ "⍎": ["execute", null],
+ "⊣": ["stop", "left"],
+ "⊢": ["pass", "right"]
+ };
+
+ var isOperator = /[\.\/⌿⍀¨⍣]/;
+ var isNiladic = /⍬/;
+ var isFunction = /[\+−×÷⌈⌊∣⍳\?⋆⍟○!⌹<≤=>≥≠≡≢∈⍷∪∩∼∨∧⍱⍲⍴,⍪⌽⊖⍉↑↓⊂⊃⌷⍋⍒⊤⊥⍕⍎⊣⊢]/;
+ var isArrow = /←/;
+ var isComment = /[⍝#].*$/;
+
+ var stringEater = function(type) {
+ var prev;
+ prev = false;
+ return function(c) {
+ prev = c;
+ if (c === type) {
+ return prev === "\\";
+ }
+ return true;
+ };
+ };
+ return {
+ startState: function() {
+ return {
+ prev: false,
+ func: false,
+ op: false,
+ string: false,
+ escape: false
+ };
+ },
+ token: function(stream, state) {
+ var ch, funcName, word;
+ if (stream.eatSpace()) {
+ return null;
+ }
+ ch = stream.next();
+ if (ch === '"' || ch === "'") {
+ stream.eatWhile(stringEater(ch));
+ stream.next();
+ state.prev = true;
+ return "string";
+ }
+ if (/[\[{\(]/.test(ch)) {
+ state.prev = false;
+ return null;
+ }
+ if (/[\]}\)]/.test(ch)) {
+ state.prev = true;
+ return null;
+ }
+ if (isNiladic.test(ch)) {
+ state.prev = false;
+ return "niladic";
+ }
+ if (/[¯\d]/.test(ch)) {
+ if (state.func) {
+ state.func = false;
+ state.prev = false;
+ } else {
+ state.prev = true;
+ }
+ stream.eatWhile(/[\w\.]/);
+ return "number";
+ }
+ if (isOperator.test(ch)) {
+ return "operator apl-" + builtInOps[ch];
+ }
+ if (isArrow.test(ch)) {
+ return "apl-arrow";
+ }
+ if (isFunction.test(ch)) {
+ funcName = "apl-";
+ if (builtInFuncs[ch] != null) {
+ if (state.prev) {
+ funcName += builtInFuncs[ch][1];
+ } else {
+ funcName += builtInFuncs[ch][0];
+ }
+ }
+ state.func = true;
+ state.prev = false;
+ return "function " + funcName;
+ }
+ if (isComment.test(ch)) {
+ stream.skipToEnd();
+ return "comment";
+ }
+ if (ch === "∘" && stream.peek() === ".") {
+ stream.next();
+ return "function jot-dot";
+ }
+ stream.eatWhile(/[\w\$_]/);
+ word = stream.current();
+ state.prev = true;
+ return "keyword";
+ }
+ };
+});
+
+CodeMirror.defineMIME("text/apl", "apl");
diff --git a/plugins/codemirror/codemirror/mode/apl/index.html b/plugins/codemirror/codemirror/mode/apl/index.html
new file mode 100644
index 0000000..f8282ac
--- /dev/null
+++ b/plugins/codemirror/codemirror/mode/apl/index.html
@@ -0,0 +1,72 @@
+
+
+CodeMirror: APL mode
+
+
+
+
+
+
+
+
+
Simple mode that tries to handle APL as well as it can.
+
It attempts to label functions/operators based upon
+ monadic/dyadic usage (but this is far from fully fleshed out).
+ This means there are meaningful classnames so hover states can
+ have popups etc.
Simple mode that tries to handle C-like languages as well as it
+ can. Takes two configuration parameters: keywords, an
+ object whose property names are the keywords in the language,
+ and useCPP, which determines whether C preprocessor
+ directives are recognized.
Handles AT&T assembler syntax (more specifically this handles
+ the GNU Assembler (gas) syntax.)
+ It takes a single optional configuration parameter:
+ architecture, which can be one of "ARM",
+ "ARMv6" or "x86".
+ Including the parameter adds syntax for the registers and special
+ directives for the supplied architecture.
+
+
MIME types defined:text/x-gas
+
diff --git a/plugins/codemirror/codemirror/mode/gfm/gfm.js b/plugins/codemirror/codemirror/mode/gfm/gfm.js
new file mode 100644
index 0000000..1411a93
--- /dev/null
+++ b/plugins/codemirror/codemirror/mode/gfm/gfm.js
@@ -0,0 +1,97 @@
+CodeMirror.defineMode("gfm", function(config) {
+ var codeDepth = 0;
+ function blankLine(state) {
+ state.code = false;
+ return null;
+ }
+ var gfmOverlay = {
+ startState: function() {
+ return {
+ code: false,
+ codeBlock: false,
+ ateSpace: false
+ };
+ },
+ copyState: function(s) {
+ return {
+ code: s.code,
+ codeBlock: s.codeBlock,
+ ateSpace: s.ateSpace
+ };
+ },
+ token: function(stream, state) {
+ // Hack to prevent formatting override inside code blocks (block and inline)
+ if (state.codeBlock) {
+ if (stream.match(/^```/)) {
+ state.codeBlock = false;
+ return null;
+ }
+ stream.skipToEnd();
+ return null;
+ }
+ if (stream.sol()) {
+ state.code = false;
+ }
+ if (stream.sol() && stream.match(/^```/)) {
+ stream.skipToEnd();
+ state.codeBlock = true;
+ return null;
+ }
+ // If this block is changed, it may need to be updated in Markdown mode
+ if (stream.peek() === '`') {
+ stream.next();
+ var before = stream.pos;
+ stream.eatWhile('`');
+ var difference = 1 + stream.pos - before;
+ if (!state.code) {
+ codeDepth = difference;
+ state.code = true;
+ } else {
+ if (difference === codeDepth) { // Must be exact
+ state.code = false;
+ }
+ }
+ return null;
+ } else if (state.code) {
+ stream.next();
+ return null;
+ }
+ // Check if space. If so, links can be formatted later on
+ if (stream.eatSpace()) {
+ state.ateSpace = true;
+ return null;
+ }
+ if (stream.sol() || state.ateSpace) {
+ state.ateSpace = false;
+ if(stream.match(/^(?:[a-zA-Z0-9\-_]+\/)?(?:[a-zA-Z0-9\-_]+@)?(?:[a-f0-9]{7,40}\b)/)) {
+ // User/Project@SHA
+ // User@SHA
+ // SHA
+ return "link";
+ } else if (stream.match(/^(?:[a-zA-Z0-9\-_]+\/)?(?:[a-zA-Z0-9\-_]+)?#[0-9]+\b/)) {
+ // User/Project#Num
+ // User#Num
+ // #Num
+ return "link";
+ }
+ }
+ if (stream.match(/^((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\([^\s()<>]*\))+(?:\([^\s()<>]*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i) &&
+ stream.string.slice(stream.start - 2, stream.start) != "](") {
+ // URLs
+ // Taken from http://daringfireball.net/2010/07/improved_regex_for_matching_urls
+ // And then (issue #1160) simplified to make it not crash the Chrome Regexp engine
+ return "link";
+ }
+ stream.next();
+ return null;
+ },
+ blankLine: blankLine
+ };
+ CodeMirror.defineMIME("gfmBase", {
+ name: "markdown",
+ underscoresBreakWords: false,
+ taskLists: true,
+ fencedCodeBlocks: true
+ });
+ return CodeMirror.overlayMode(CodeMirror.getMode(config, "gfmBase"), gfmOverlay);
+}, "markdown");
diff --git a/plugins/codemirror/codemirror/mode/gfm/index.html b/plugins/codemirror/codemirror/mode/gfm/index.html
new file mode 100644
index 0000000..b71cd5c
--- /dev/null
+++ b/plugins/codemirror/codemirror/mode/gfm/index.html
@@ -0,0 +1,82 @@
+
+
+CodeMirror: GFM mode
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Mode for html embedded scripts like JSP and ASP.NET. Depends on HtmlMixed which in turn depends on
+ JavaScript, CSS and XML. Other dependancies include those of the scriping language chosen.
The HTML mixed mode depends on the XML, JavaScript, and CSS modes.
+
+
It takes an optional mode configuration
+ option, scriptTypes, which can be used to add custom
+ behavior for specific <script type="..."> tags. If
+ given, it should hold an array of {matches, mode}
+ objects, where matches is a string or regexp that
+ matches the script type, and mode is
+ either null, for script types that should stay in
+ HTML mode, or a mode
+ spec corresponding to the mode that should be used for the
+ script.
+
+
MIME types defined:text/html
+ (redefined, only takes effect if you load this parser after the
+ XML parser).
This is a list of every mode in the distribution. Each mode lives
+in a subdirectory of the mode/ directory, and typically
+defines a single JavaScript file that implements the mode. Loading
+such file will make the language available to CodeMirror, through
+the mode
+option.
Loosely based on Franciszek
+ Wawrzak's CodeMirror
+ 1 mode. One configuration parameter is
+ supported, specials, to which you can provide an
+ array of strings to have those identifiers highlighted with
+ the lua-special style.