4
* author: Dominik Haumann <dhdev@gmx.de>
4
* author: Matteo Sasso <matteo.sasso@gmail.com>
9
* This file is part of the Kate Project.
11
* This library is free software; you can redistribute it and/or
12
* modify it under the terms of the GNU Library General Public
13
* License version 2 as published by the Free Software Foundation.
15
* This library is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
* Library General Public License for more details.
20
* You should have received a copy of the GNU Library General Public License
21
* along with this library; see the file COPYING.LIB. If not, write to
22
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23
* Boston, MA 02110-1301, USA.
26
11
triggerCharacters = ";";
29
* Process a newline character.
30
* This function is called whenever the user hits <return/enter>.
13
function cursors_eq(a, b)
15
return (a.line == b.line && a.column == b.column);
18
function cursors_compare(a, b)
23
return (a.column - b.column);
24
return (a.line - b.line);
28
function nearest_anchor(line, column)
30
anchors = new Array(3);
31
anchors[0] = document.anchor(line, column, '(');
33
anchors[1] = document.anchor(line, column, '[');
35
anchors[2] = document.anchor(line, column, '{');
38
anchors.sort(cursors_compare);
40
return (anchors[2].line >= 0) ? anchors[2] : null;
32
43
function indent(line, indentWidth, ch)
34
// special rules: ;;; -> indent 0
35
// ;; -> align with next line, if possible
36
// ; -> usually on the same line as code -> ignore
45
currentLineText = document.line(line);
38
textLine = document.line(line);
39
if (textLine.search(/^\s*;;;/) != -1) {
47
/* Handle single-line comments. */
48
if (currentLineText.match(/^\s*;;;/)) {
41
} else if (textLine.search(/^\s*;;/) != -1) {
50
} else if (currentLineText.match(/^\s*;;/)) {
42
51
// try to align with the next line
43
52
nextLine = document.nextNonEmptyLine(line + 1);
45
54
return document.firstVirtualColumn(nextLine);
49
cursor = document.anchor(line, 0, '(');
50
if (cursor.isValid()) {
51
return document.toVirtualColumn(cursor) + indentWidth;
57
if (ch != '\n') return -2;
59
newlineAnchor = nearest_anchor(line, 0);
64
for (ln = line-1; ln > newlineAnchor.line; ln--) {
65
prevlineAnchor = nearest_anchor(ln, 0);
66
if (prevlineAnchor && cursors_eq(prevlineAnchor, newlineAnchor))
67
/* IF the previous line's form is at the same nesting level of the current line...
68
...indent the current line to the same column of the previous line. */
69
return document.firstVirtualColumn(ln);
71
nextlineAnchor = nearest_anchor(line + 1, 0);
72
nextLineText = document.line(line + 1);
73
if (nextlineAnchor && cursors_eq(nextlineAnchor, newlineAnchor) &&
74
nextLineText && nextLineText.search(/\S/) != -1 &&
75
nextlineAnchor.column < document.firstVirtualColumn(line+1))
76
/* IF the next line's form is at the same nesting level of the current line,
77
it's not empty and its indentation column makes sense for the current form...
78
...indent the current line to the same column of the next line. */
79
return document.firstVirtualColumn(line+1);
80
if (!newlineAnchor && !nearest_anchor(line-1, 0))
81
/* If the user introduces a blank line the indentation level is not changed. */
84
if (newlineAnchor.char == '(') {
85
/* We are inside a form introduced previously, so we find the beginning of that
86
form and determine the correct indentation column, assuming it's a function. */
87
anchorText = document.line(newlineAnchor.line).substring(newlineAnchor.column+1);
88
if (anchorText.match(/^[{[(]/))
89
/* If the first character of the form is a bracket we assume it's something
90
like Common Lisp's (let ...) special form (or just data) and we indent by 1. */
91
return (newlineAnchor.column + 1);
92
argument_match = anchorText.match(/^\S+\s+./);
93
indentation = argument_match ? argument_match[0].length : indentWidth;
94
column = document.toVirtualColumn(newlineAnchor.line, newlineAnchor.column);
95
return (column + indentation);
97
/* This is probably some kind of data. Indent by 1. */
98
return (newlineAnchor.column + 1);