home *** CD-ROM | disk | FTP | other *** search
- void MatchIt(string paren)
- {
- Output(paren);
- CursorLeft(1);
- if (MatchParen()>=0)
- {
- Delay(4);
- MatchParen();
- }
- else
- {
- ReturnStatus("No match!");
- }
- CursorRight(1);
- }
-
- int GetIndent(int linenum, string inline)
- {
- int x;
- int linelen = strlen(inline);
-
- for (x = 0; x < linelen && ((inline[x] == '\t') || (inline[x] == ' ')); x++);
- return(GetCursor(x, linenum) - 1);
- }
-
- string InsertIndent(string output, int indentation)
- {
- int x, tabs, spaces;
- int tabsize = ReadInfo("tab_size");
-
- if (ReadInfo("c_usetabs"))
- {
- tabs = indentation / tabsize;
- spaces = indentation - tabs * tabsize;
- for (x = 0; x < tabs; x++)
- {
- output = joinstr(output, "\t");
- }
- for (x = 0; x < spaces; x++)
- {
- output = joinstr(output, " ");
- }
- }
- else
- {
- for (x = 0; x < indentation; x++)
- {
- output = joinstr(output, " ");
- }
- }
- return (output);
- }
-
- export void CIndent()
- {
- int indentation;
- int line = ReadInfo("line");
- int linelen = ReadInfo("line_length") - 1;
- int column = ReadInfo("column") - 1;
- int keywordpos;
- string input = GetLine();
- string output = "\n";
-
- indentation = GetIndent(line, input);
- if(indentation == column && column == linelen)
- {
- GotoLine(line, 0);
- DeleteEol();
- }
-
- if (strstr(input, "}") < 0)
- {
- if ((keywordpos = strstr(input, "{")) >= 0)
- {
- indentation += ReadInfo("c_indent");
- }
- else if ((keywordpos = strstr(input, "case")) >= 0)
- {
- indentation += ReadInfo("c_indent");
- }
- else if ((keywordpos = strstr(input, "default")) >= 0)
- {
- indentation += ReadInfo("c_indent");
- }
- else if (indentation > column)
- {
- indentation -= indentation - column;
- }
- }
-
- if (keywordpos < column)
- {
- output = InsertIndent("\n", indentation);
- }
- Output(output);
- }
-
- export void CAutoMatch()
- {
- int line = ReadInfo("line");
- int col = ReadInfo("byte_position");
- int newline;
- string input = GetLine();
- string output;
- int len = strlen(input);
- int column, maxcol;
- int x = 0;
-
- /* If we aren't on the last line, we subtract one from len */
- if (line != ReadInfo("lines"))
- {
- len -= 1;
- }
-
- if (col == len) /* Only act if we are at the end of the line */
- {
- x = GetIndent(line, input);
- if(x == GetCursor(col) - 1) /* Act if there is no text on the line */
- {
- Output("}");
- CursorLeft(1);
- if (MatchParen() >= 0)
- {
- newline = ReadInfo("line");
- if (newline != line)
- {
- column = GetIndent(newline, GetLine());
- output = InsertIndent("", column);
- }
- output = joinstr(output, "}");
- Delay(10);
- GotoLine(line);
- DeleteEol();
- Output(output);
- }
- else
- {
- ReturnStatus("No match!");
- }
- }
- else
- {
- MatchIt("}");
- }
- }
- else
- {
- MatchIt("}");
- }
- }
-
- export int CIndent_LoadHook(string path, string filename)
- {
- int place;
- string extension;
- int ret = 0;
-
- place = strstr(filename, ".");
- if (place > 0)
- {
- extension = substr(filename, place + 1, -1);
- if (0 == Stricmp(extension, "c"))
- {
- SetInfo(-1, "c_mode", 1);
- }
- else if (0 == Stricmp(extension, "h"))
- {
- SetInfo(-1, "c_mode", 1);
- }
- else if (0 == Stricmp(extension, "fpl"))
- {
- SetInfo(-1, "c_mode", 1);
- }
- else
- {
- SetInfo(-1, "c_mode", 0);
- }
- }
- return (ret);
- }
-
- Hook("GetFile", "CIndent_LoadHook");
- ConstructInfo("c_mode", "", "", "LB", "", 0, 1, 0);
- ConstructInfo("c_indent", "", "", "LIW", "", 0, 50, 3);
- ConstructInfo("c_usetabs", "", "", "LBW", "", 0, 1, 1);
- AssignKey("CAutoMatch();", "}", "c_mode");
- AssignKey("Output(\"\n\");", "Shift 'Enter'", "c_mode");
- AssignKey("CIndent();", "'Enter'", "c_mode");
-
-