~ubuntu-branches/ubuntu/quantal/kate/quantal-proposed

« back to all changes in this revision

Viewing changes to part/script/data/lisp.js

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-14 13:28:06 UTC
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: package-import@ubuntu.com-20111214132806-aa2uf6ri5w2p8ak3
Tags: upstream-4.7.90
ImportĀ upstreamĀ versionĀ 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/** kate-script
2
2
 * name: LISP
3
3
 * license: LGPL
4
 
 * author: Dominik Haumann <dhdev@gmx.de>
5
 
 * revision: 2
6
 
 * kate-version: 3.4
 
4
 * author: Matteo Sasso <matteo.sasso@gmail.com>
 
5
 * version: 1.1
 
6
 * kate-version: 3.0
7
7
 * type: indentation
8
8
 *
9
 
 * This file is part of the Kate Project.
10
 
 *
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.
14
 
 *
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.
19
 
 *
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.
24
9
 */
25
10
 
26
11
triggerCharacters = ";";
27
12
 
28
 
/**
29
 
 * Process a newline character.
30
 
 * This function is called whenever the user hits <return/enter>.
31
 
 */
 
13
function cursors_eq(a, b)
 
14
{
 
15
    return (a.line == b.line && a.column == b.column);
 
16
}
 
17
 
 
18
function cursors_compare(a, b)
 
19
{
 
20
    if (!a || !b)
 
21
        return false;
 
22
    if (a.line == b.line)
 
23
        return (a.column - b.column);
 
24
    return (a.line - b.line);
 
25
}
 
26
 
 
27
 
 
28
function nearest_anchor(line, column)
 
29
{
 
30
    anchors = new Array(3);
 
31
    anchors[0] = document.anchor(line, column, '(');
 
32
    anchors[0].char = '('
 
33
    anchors[1] = document.anchor(line, column, '[');
 
34
    anchors[1].char = '['
 
35
    anchors[2] = document.anchor(line, column, '{');
 
36
    anchors[2].char = '{'
 
37
 
 
38
    anchors.sort(cursors_compare);
 
39
 
 
40
    return (anchors[2].line >= 0) ? anchors[2] : null;
 
41
}
 
42
 
32
43
function indent(line, indentWidth, ch)
33
44
{
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);
37
46
 
38
 
    textLine = document.line(line);
39
 
    if (textLine.search(/^\s*;;;/) != -1) {
 
47
    /* Handle single-line comments. */
 
48
    if (currentLineText.match(/^\s*;;;/)) {
40
49
        return 0;
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);
44
 
        if (nextLine != -1) {
 
53
        if (nextLine != -1)
45
54
            return document.firstVirtualColumn(nextLine);
46
 
        }
47
 
    }
48
 
 
49
 
    cursor = document.anchor(line, 0, '(');
50
 
    if (cursor.isValid()) {
51
 
        return document.toVirtualColumn(cursor) + indentWidth;
 
55
        return -1;
 
56
    }
 
57
    if (ch != '\n') return -2;
 
58
 
 
59
    newlineAnchor = nearest_anchor(line, 0);
 
60
    if (!newlineAnchor)
 
61
        /* Toplevel */
 
62
        return 0;
 
63
 
 
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);
 
70
    }
 
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. */
 
82
        return -1;
 
83
 
 
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);
52
96
    } else {
53
 
        return 0;
 
97
        /* This is probably some kind of data. Indent by 1. */
 
98
        return (newlineAnchor.column + 1);
54
99
    }
55
100
}
56
101