home *** CD-ROM | disk | FTP | other *** search
- // Various search strings
- string whitespace = "^[ \t]*";
- string any_character = "(\\sW|\\s!)";
- string left_brace_at_eol = "{[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*$";
- string left_or_right_brace = "(({[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*$)|(^[ \t]*}))";
- string case_or_default = "^[ \t]*((case[ \t]+.+)|(default[ \t]*)):";
- string if_or_while = "(if|while)[ \t]*\\(.*\\)[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*$";
- string _else = "else[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*$";
- string _switch = "switch[ \t]*\\(.*\\)[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*$";
- string backward = "=w+f-";
- string forward = "=w+f+";
-
- //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CModePrefs() ««
- void export CModePrefs()
- {
- int intelligence = ReadInfo("c_intelligence");
- string indentkey = ReadInfo("c_indentkey");
- int usetabs = ReadInfo("c_usetabs");
- int indent_level = ReadInfo("c_indent_level");
- int case_level = ReadInfo("c_case_level");
- int brace_offset = ReadInfo("c_brace_offset");
- int cont_offset = ReadInfo("c_cont_offset");
- int delay = ReadInfo("c_delay");
-
- DeleteKey(indentkey);
-
- RequestWindow("C Indent Preferences", 15,
- "Intelligence level", "c", &intelligence, "Stupid|Intelligent|Ninja",
- "Force Indent key", "s", &indentkey,
- "Use TAB character", "b", &usetabs,
- "General indent level", "i", &indent_level,
- "'case' indent level", "i", &case_level,
- "Brace offset", "i", &brace_offset,
- "Continuation offset", "i", &cont_offset,
- "Parenthesis match delay", "i", &delay
- );
-
- AssignKey("ForceIndent();", indentkey, "c_indent_mode");
-
- SetInfo(-1, "c_intelligence", intelligence);
- SetInfo(-1, "c_indentkey", indentkey);
- SetInfo(-1, "c_usetabs", usetabs);
- SetInfo(-1, "c_indent_level", indent_level);
- SetInfo(-1, "c_case_level", case_level);
- SetInfo(-1, "c_brace_offset", brace_offset);
- SetInfo(-1, "c_cont_offset", cont_offset);
- SetInfo(-1, "c_delay", delay);
- }
-
- int PrevLine()
- {
- CursorStack(-1);
- if(0 == Search(any_character, backward))
- {
- int line;
-
- line = ReadInfo("line");
- CursorStack(1);
- return(line);
- }
- else
- {
- CursorStack(1);
- return(1);
- }
- }
-
- export void MatchIt(string paren)
- {
- Output(paren);
- CursorLeft(1);
- if (MatchParen()>=0)
- {
- Delay(ReadInfo("c_delay"));
- MatchParen();
- }
- else
- {
- ReturnStatus("No match!");
- }
- CursorRight(1);
- }
-
- string InsertIndent(string output, int indentation)
- {
- int x;
- int tabsize = ReadInfo("tab_size");
- int tabs = indentation / tabsize;
- int spaces= indentation % tabsize;
-
- if (ReadInfo("c_usetabs"))
- {
- while(x < tabs)
- {
- output += "\t";
- x++;
- }
- for (x = 0; x < spaces; x++)
- {
- output += " ";
- }
- }
- else
- {
- for (x = 0; x < indentation; x++)
- {
- output += " ";
- }
- }
- return (output);
- }
-
- export void HashMark()
- {
- int v = Visible(0);
- int dum = Output("#");
- int line = ReadInfo("line");
- string thisline = GetLine();
- int thislinelen = strlen(thisline) - (line != ReadInfo("lines"));
-
- CursorStack(-1);
- GotoLine(line, 0);
- CursorLeft(); /* Fix! The search cannot find if we are at
- the beginning of the line */
-
- if(0 <= Search("^[ \t]*\\#$",forward, thislinelen))
- {
- GotoLine(line, 0);
- DeleteEol();
- Output("#");
- }
- else
- {
- CursorStack(1);
- }
- Visible(v);
- }
-
- export void Colon()
- {
- int v = Visible(0);
- int dum = Output(":");
- int line = ReadInfo("line");
- string thisline = GetLine();
- int thislinelen = strlen(thisline) - (line != ReadInfo("lines"));
-
- CursorStack(-1);
- GotoLine(line, 0);
- CursorLeft(); /* Fix! The search cannot find if we are at
- the beginning of the line */
-
- if(0 <= Search(case_or_default, forward, thislinelen))
- {
- if (0 <= Search(_switch, backward))
- {
- int x;
- int prevline = ReadInfo("line");
- int linelen = ReadInfo("line_length") - (prevline != ReadInfo("lines"));
- int indentation;
-
- GotoLine(prevline, 0);
-
- indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1 + ReadInfo("c_case_level");
-
- GotoLine(line, 0);
- Replace(2, whitespace, "", forward, linelen);
-
- Output(InsertIndent("", indentation));
- Search(":", forward);
- CursorRight();
- }
- else
- {
- CursorStack(1);
- }
- }
- else
- {
- CursorStack(1);
- }
- Visible(v);
- }
-
- export void LeftBrace()
- {
- int v = Visible(0);
- int line = ReadInfo("line");
- int thislinelen = ReadInfo("line_length") - (line != ReadInfo("lines"));
-
- Output("{");
-
- CursorStack(-1);
- GotoLine(line, 0);
- if (GetChar() != '{' && (0 <= Search("^[ \t]*{", forward, thislinelen)))
- {
- int x;
- int prevline = PrevLine();
- string input = GetLine(prevline);
- int linelen = strlen(input) - (line != ReadInfo("lines"));
- int indentation;
-
- GotoLine(prevline, 0);
- indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1;
-
- if(0 == Search(left_brace_at_eol, forward, linelen))
- {
- indentation += ReadInfo("c_indent_level");
- }
-
- GotoLine(line, 0);
-
- Replace(2, whitespace, "", forward, thislinelen);
-
- Output(InsertIndent("", indentation));
- CursorRight();
- }
- else
- {
- CursorStack(1);
- }
- Visible(v);
- }
-
- export void ForceIndent()
- {
- int v = Visible(0);
- int thisline = ReadInfo("line");
- int thislinelen = ReadInfo("line_length") - (thisline != ReadInfo("lines"));
- int linelen, prevline;
- int indentation, intelligence;
-
- CursorStack(-1);
- GotoLine(ReadInfo("line"), 0);
- prevline = PrevLine();
-
- GotoLine(prevline, 0);
- linelen = ReadInfo("line_length") - (ReadInfo("line") != ReadInfo("lines"));
- indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1;
-
- if (prevline < thisline)
- {
- CursorStack(1);
- CursorStack(-1);
-
- GotoLine(thisline, 0);
-
- if (0 <= Search(case_or_default, forward, thislinelen))
- {
- if (0 <= Search(_switch, backward))
- {
- int x;
- int prevline = ReadInfo("line");
- int linelen = ReadInfo("line_length") - (prevline != ReadInfo("lines"));
-
- GotoLine(prevline, 0);
-
- indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1 + ReadInfo("c_case_level");
- }
- }
- else if (0 <= Search("^[ \t]*\\#", forward, thislinelen))
- {
- indentation = 0;
- }
- else if (GetChar() != '}' && 0 > Search("^[ \t]*}", forward, thislinelen))
- {
- GotoLine(prevline, 0);
- if (0 == Search(left_brace_at_eol, forward, linelen))
- {
- indentation += ReadInfo("c_indent_level");
- }
- else if (intelligence = ReadInfo("c_intelligence"))
- {
- if (0 == Search(case_or_default, forward, linelen))
- {
- indentation += ReadInfo("c_indent_level");
- }
- else if (0 == Search(if_or_while, forward, linelen))
- {
- indentation += ReadInfo("c_cont_offset");
- }
- else if (0 == Search(_else, forward, linelen))
- {
- indentation += ReadInfo("c_cont_offset");
- }
- else if (intelligence > 1)
- {
- prevline = PrevLine();
- GotoLine(prevline, 0);
- linelen = ReadInfo("line_length");
- if (0 == Search(if_or_while, forward, linelen))
- {
- indentation -= ReadInfo("c_cont_offset");
- }
- else if (0 == Search(_else, forward, linelen))
- {
- indentation -= ReadInfo("c_cont_offset");
- }
- }
- }
- }
- else
- {
- int prevline;
- int prevlen;
- string input;
-
- if (ReadInfo("c_intelligence"))
- {
- int end, left, right;
-
- right = 1;
- do
- {
- if (0 == (end = Search(left_or_right_brace, backward)))
- {
- if (GetChar() == '{')
- left++;
- else
- right++;
- }
- }
- while (!end && (left < right));
-
- if (left >= right)
- {
- prevline = ReadInfo("line");
- GotoLine(prevline, 0);
- indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1;
- }
- }
- }
- }
-
- CursorStack(1);
- GotoLine(ReadInfo("line"), 0);
- Replace(2, whitespace, "", forward, linelen);
-
- Output(InsertIndent("", indentation));
- GotoLine(thisline, ReadInfo("line_length"));
- Visible(v);
- }
-
- export void CIndent()
- {
- int v = Visible(0);
- int linelen = Output("\n");
- int prevline = PrevLine();
- int indentation, intelligence;
-
- CursorStack(-1);
- GotoLine(prevline, 0);
- linelen = ReadInfo("line_length") - (ReadInfo("line") != ReadInfo("lines"));
- indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1;
-
- CursorStack(1);
- CursorStack(-1);
- if (GetChar() != '}' || 0 > Search("^[ \t]*}", forward, linelen))
- {
- GotoLine(prevline, 0);
- if (0 == Search(left_brace_at_eol, forward, linelen))
- {
- indentation += ReadInfo("c_indent_level");
- }
- else if (intelligence = ReadInfo("c_intelligence"))
- {
- if (0 == Search(case_or_default, forward, linelen))
- {
- indentation += ReadInfo("c_indent_level");
- }
- else if (0 == Search(if_or_while, forward, linelen))
- {
- indentation += ReadInfo("c_cont_offset");
- }
- else if (0 == Search(_else, forward, linelen))
- {
- indentation += ReadInfo("c_cont_offset");
- }
- else if (intelligence > 1)
- {
- prevline = PrevLine();
- GotoLine(prevline, 0);
- linelen = ReadInfo("line_length");
- if (0 == Search(if_or_while, forward, linelen))
- {
- indentation -= ReadInfo("c_cont_offset");
- }
- else if (0 == Search(_else, forward, linelen))
- {
- indentation -= ReadInfo("c_cont_offset");
- }
- }
- }
- }
- CursorStack(1);
-
- Replace(2, whitespace, "", forward, linelen);
-
- Output(InsertIndent("", indentation));
- Visible(v);
- }
-
- export void RightBrace()
- {
- int line = ReadInfo("line");
- int dum = Output("}");
- int v = Visible(0);
- string thisline = GetLine();
- int thislinelen = strlen(thisline) - (line != ReadInfo("lines"));
- string output;
- int x;
-
- CursorStack(-1);
- GotoLine(line, 0);
- CursorLeft();
- if (0 == Search("^[ \t]*}", forward, thislinelen))
- {
- int prevline;
- int prevlen;
- string input;
-
- if(ReadInfo("c_intelligence"))
- {
- int end, left, right;
-
- CursorStack(1);
- CursorStack(-1);
- do
- {
- if (0 == (end = Search(left_or_right_brace, backward)))
- {
- if (GetChar() == '{')
- left++;
- else
- right++;
- }
- } while (!end && (left != right));
-
- if (end)
- {
- CursorStack(-1);
- Visible(v);
- }
- else
- {
- int indentation;
-
- prevline = ReadInfo("line");
- GotoLine(prevline, 0);
- indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1;
-
- Visible(v);
- Delay(ReadInfo("c_delay"));
- MatchParen();
- v = Visible(0);
- GotoLine(line, 0);
-
- Replace(2, whitespace, "", forward, thislinelen);
-
- Output(InsertIndent("", indentation));
- CursorRight();
- Visible(v);
- }
- }
- else
- {
- CursorStack(1);
- Visible(v);
- }
- }
- else
- {
- CursorStack(1);
- Visible(v);
- }
- }
-
- export void Slash()
- {
- int line = ReadInfo("line");
- int dum = Output("/");
- int pos = ReadInfo("byte_position");
- int v = Visible(0);
- string thisline = GetLine();
- int thislinelen = strlen(thisline) - (line != ReadInfo("lines"));
- string output;
- int x;
-
- CursorStack(-1);
- if(pos > 1)
- {
- if(thisline[pos - 2] == '*')
- {
- if(0 == Search("//(\*)", backward))
- {
- Visible(v);
- Delay(ReadInfo("c_delay"));
- CursorStack(1);
- }
- }
- }
- else
- {
- Visible(v);
- }
- }
-
- ConstructInfo("c_indent_mode", "", "", "LB", "", 0, 1, 0);
- ConstructInfo("c_indent_level", "", "", "LIW", "", 0, 0, 3);
- ConstructInfo("c_brace_offset", "", "", "LIW", "", 0, 0, 0);
- ConstructInfo("c_cont_offset", "", "", "LIW", "", 0, 0, 3);
- ConstructInfo("c_case_level", "", "", "LIW", "", 0, 0, 3);
- ConstructInfo("c_usetabs", "", "", "LBW", "", 0, 1, 1);
- ConstructInfo("c_delay", "", "", "GIW", "", 0, 0, 5);
- ConstructInfo("c_intelligence", "", "", "GCW", "Stupid|Intelligent|Ninja", 0, 0, 2);
- ConstructInfo("c_indentkey", "", "", "GSW", "", 0, 0, "Alt i");
-
- AssignKey("RightBrace();", "}", "c_indent_mode");
- AssignKey("Output(\"\n\");", "Shift 'Return'", "c_indent_mode");
- AssignKey("CIndent();", "'Return'", "c_indent_mode");
- AssignKey("LeftBrace();", "{", "c_indent_mode");
- AssignKey("HashMark();", "#", "c_indent_mode");
- AssignKey("Colon();", ":", "c_indent_mode&c_intelligence");
- AssignKey("Slash();", "/", "c_indent_mode");
- AssignKey("ForceIndent();", ReadInfo("c_indentkey"), "c_indent_mode");
-
- AssignKey("MatchIt(\")\");", ")", "c_indent_mode");
- AssignKey("MatchIt(\"]\");", "]", "c_indent_mode");
-
- AddMode(0,"c_indent_mode", "", ""); // Add as minor mode
-
- //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CIndent menu ««
- MenuAdd("s", "C Indent...", "CModePrefs();", "", 6,6,-1); // Add to PackageSettings
- MenuBuild();
-