5 <title>CodeMirror: Any Word Completion Demo</title>
6 <link rel="stylesheet" href="../lib/codemirror.css">
7 <script src="../lib/codemirror.js"></script>
8 <script src="../addon/hint/show-hint.js"></script>
9 <link rel="stylesheet" href="../addon/hint/show-hint.css">
10 <script src="../addon/hint/anyword-hint.js"></script>
11 <script src="../mode/javascript/javascript.js"></script>
12 <link rel="stylesheet" href="../doc/docs.css">
15 <h1>CodeMirror: Any Word Completion Demo</h1>
17 <form><textarea id="code" name="code">
21 var WORD = /[\w$]+/g, RANGE = 500;
23 CodeMirror.registerHelper("hint", "anyword", function(editor, options) {
24 var word = options && options.word || WORD;
25 var range = options && options.range || RANGE;
26 var cur = editor.getCursor(), curLine = editor.getLine(cur.line);
27 var start = cur.ch, end = start;
28 while (end < curLine.length && word.test(curLine.charAt(end))) ++end;
29 while (start && word.test(curLine.charAt(start - 1))) --start;
30 var curWord = start != end && curLine.slice(start, end);
32 var list = [], seen = {};
34 var line = cur.line, end = Math.min(Math.max(line + dir * range, editor.firstLine()), editor.lastLine()) + dir;
35 for (; line != end; line += dir) {
36 var text = editor.getLine(line), m;
38 while (m = word.exec(text)) {
39 if ((!curWord || m[0].indexOf(curWord) == 0) && !seen.hasOwnProperty(m[0])) {
48 return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};
53 <p>Press <strong>ctrl-space</strong> to activate autocompletion. The
55 the <a href="../doc/manual.html#addon_anyword-hint">anyword-hint.js</a>
56 module, which simply looks at nearby words in the buffer and completes
60 CodeMirror.commands.autocomplete = function(cm) {
61 CodeMirror.showHint(cm, CodeMirror.hint.anyword);
63 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
65 extraKeys: {"Ctrl-Space": "autocomplete"}