File indexing completed on 2024-05-19 04:00:09

0001 var katescript = {
0002     "name": "LISP",
0003     "author": "Matteo Sasso <matteo.sasso@gmail.com>",
0004     "license": "LGPL",
0005     "revision": 3,
0006     "kate-version": "5.1"
0007 }; // kate-script-header, must be at the start of the file without comments, pure json
0008 
0009 // required katepart js libraries
0010 require ("range.js");
0011 
0012 triggerCharacters = ";";
0013 
0014 function cursors_eq(a, b)
0015 {
0016     return (a.line == b.line && a.column == b.column);
0017 }
0018 
0019 function cursors_compare(a, b)
0020 {
0021     if (!a || !b)
0022         return false;
0023     if (a.line == b.line)
0024         return (a.column - b.column);
0025     return (a.line - b.line);
0026 }
0027 
0028 
0029 function nearest_anchor(line, column)
0030 {
0031     anchors = new Array(3);
0032     anchors[0] = document.anchor(line, column, '(');
0033     anchors[0].char = '('
0034     anchors[1] = document.anchor(line, column, '[');
0035     anchors[1].char = '['
0036     anchors[2] = document.anchor(line, column, '{');
0037     anchors[2].char = '{'
0038 
0039     anchors.sort(cursors_compare);
0040 
0041     return (anchors[2].line >= 0) ? anchors[2] : null;
0042 }
0043 
0044 function indent(line, indentWidth, ch)
0045 {
0046     currentLineText = document.line(line);
0047 
0048     /* Handle single-line comments. */
0049     if (currentLineText.match(/^\s*;;;/)) {
0050         return 0;
0051     } else if (currentLineText.match(/^\s*;;/)) {
0052         // try to align with the next line
0053         nextLine = document.nextNonEmptyLine(line + 1);
0054         if (nextLine != -1)
0055             return document.firstVirtualColumn(nextLine);
0056         return -1;
0057     }
0058     if (ch != '\n') return -2;
0059 
0060     newlineAnchor = nearest_anchor(line, 0);
0061     if (!newlineAnchor)
0062         /* Toplevel */
0063         return 0;
0064 
0065     for (ln = line-1; ln > newlineAnchor.line; ln--) {
0066         prevlineAnchor = nearest_anchor(ln, 0);
0067         if (prevlineAnchor && cursors_eq(prevlineAnchor, newlineAnchor))
0068             /* IF the previous line's form is at the same nesting level of the current line...
0069             ...indent the current line to the same column of the previous line. */
0070             return document.firstVirtualColumn(ln);
0071     }
0072     nextlineAnchor = nearest_anchor(line + 1, 0);
0073     nextLineText = document.line(line + 1);
0074     if (nextlineAnchor && cursors_eq(nextlineAnchor, newlineAnchor) &&
0075             nextLineText && nextLineText.search(/\S/) != -1 &&
0076             nextlineAnchor.column < document.firstVirtualColumn(line+1))
0077         /* IF the next line's form is at the same nesting level of the current line,
0078            it's not empty and its indentation column makes sense for the current form...
0079            ...indent the current line to the same column of the next line. */
0080         return document.firstVirtualColumn(line+1);
0081     if (!newlineAnchor && !nearest_anchor(line-1, 0))
0082         /* If the user introduces a blank line the indentation level is not changed. */
0083         return -1;
0084 
0085     if (newlineAnchor.char == '(') {
0086         /* We are inside a form introduced previously, so we find the beginning of that
0087            form and determine the correct indentation column, assuming it's a function. */
0088         anchorText = document.line(newlineAnchor.line).substring(newlineAnchor.column+1);
0089         if (anchorText.match(/^[{[(]/))
0090            /* If the first character of the form is a bracket we assume it's something
0091               like Common Lisp's (let ...) special form (or just data) and we indent by 1. */
0092            return (newlineAnchor.column + 1);
0093         argument_match = anchorText.match(/^\S+\s+./);
0094         indentation = argument_match ? argument_match[0].length : indentWidth;
0095         column = document.toVirtualColumn(newlineAnchor.line, newlineAnchor.column);
0096         return (column + indentation);
0097     } else {
0098         /* This is probably some kind of data. Indent by 1. */
0099         return (newlineAnchor.column + 1);
0100     }
0101 }
0102 
0103 // kate: space-indent on; indent-width 4; replace-tabs on;