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

0001 var katescript = {
0002     "name": "Latex",
0003     "author": "Jeroen Vuurens <jbpvuurens@gmail.com>",
0004     "license": "LGPL",
0005     "revision": 2,
0006     "kate-version": "5.1"
0007 }; // kate-script-header, must be at the start of the file without comments, pure json
0008 
0009 /**
0010  * Simple indentation for Latex. This script indents sections within
0011  * \begin{ and \end{ parts, as well as within { and }. Parts after
0012  * an \ escape character are ignored for proper handling of
0013  * \{ \} and \%. In other cases every { is regarded as an extra
0014  * indent, every } as a de-indent, and everything after \% is comment.
0015  */
0016 
0017 // specifies the characters which should trigger indent, beside the default '\n'
0018 var triggerCharacters = "{}";
0019 var lineStartsClose = /^(\s*\}|\s*\\end\{)/;
0020 
0021 function indent(line, indentWidth, character) {
0022   // not necessary to indent the first line
0023   if (line == 0)
0024     return -2;
0025 
0026   var c = document.line(line); // current line
0027 
0028   // Search backwards for first non-space, non-comment line.
0029   var prev = line;
0030   while (prev--) {
0031     if (!document.line(prev).match(/^\s*$|^%/)) {
0032       var previousLine = document.line(prev); // previous non-space line
0033       var prevIndent = document.firstVirtualColumn(prev);
0034       var end = document.lineLength(prev);
0035 
0036       var delta = 0;    // count normal openers/closers
0037 
0038       // Walk over openers and closers in the remainder of the previous line.
0039       if (previousLine.match(lineStartsClose))
0040         delta++; 
0041       var escaped = false;
0042       for (var i = 0; i < end; i++) {
0043          var char = previousLine.charAt(i);
0044          if (char == "\\") { // ignore rest of line as comment
0045             escaped = true;
0046             continue;
0047          }
0048          if (escaped) {
0049             if (char == "b" && end > i + 5 && previousLine.substr(i, 6) == "begin{" ) {
0050                delta++;
0051             } else if (char == "e" && end > i + 3 && previousLine.substr(i, 4) == "end{" ) {
0052                delta--;
0053             }
0054          } else {
0055             if (char == "%") { // ignore rest of line as comment
0056                break;
0057             } if (char == "{") {
0058                delta++;
0059             } else if (char == "}") {
0060                delta--;
0061             }
0062          }
0063          escaped = false;
0064       }
0065 
0066       // now count the number of closers in the beginning of the current line.
0067       if (c.match(lineStartsClose))
0068         delta --;
0069       return Math.max(0, prevIndent + delta * indentWidth);
0070     }
0071   }
0072   return 0;
0073 }
0074 
0075 // kate: space-indent on; indent-width 2; replace-tabs on;