home *** CD-ROM | disk | FTP | other *** search
- // $Id: CIndentMode.FPL 1.4 1995/07/21 10:30:50 jskov Exp $
- // $VER: CIndentMode.FPL Version 0.2 (07.03.95) © Jesper Skov
-
- //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CIndentModePrefs() ««
- void export CIndentModePrefs()
- {
- PromptInfo(-1,"CIndentMode preferences",-1,-1,
- "c_usetabs",
- "c-indent-level",
- "c-cont-offset",
- "c-brace-offset",
- "c-label-offset"
- );
- }
-
- //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CIndentBlock() ««
- void export CIndentBlock()
- {
- if (!ReadInfo("block_exist"))
- ReturnStatus("No block marked!"); // Complaint no block marked
- else {
- int start=ReadInfo("block_begin_y");
- int end=ReadInfo("block_end_y");
-
- BlockMark(0); // Remove block mark
- GotoLine(start);
- while (ReadInfo("line")=<end){
- CIndent();
- CursorDown();
- }
- }
- }
-
- //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CIndent() ««
- int export CIndent()
- {
- int thisLine = ReadInfo("line");
- int prevLine;
-
- if ((prevLine=findPrevLine(thisLine)) > 0){
- int indentVal = GetIndent(prevLine, GetLine(prevLine));
- int lineLength, thisBuffer;
- int searchBuffer = DuplicateEntry();
-
- thisBuffer = CurrentBuffer(searchBuffer);
-
- //// Checks on current line
- // c-brace-offset - add if { is first letter on line (not col 0 though)
- GotoLine(thisLine); // beg of line
- lineLength = ReadInfo("line_length");
- if ('{'!=GetChar()){ // check not { on col 0
- CursorLeft(2);
- if (0==Search("\\sN[ \t]*\{","=wf+",lineLength))
- indentVal += ReadInfo("c-brace-offset");
- }
-
- // c-brace-offset - sub if } starts line
- GotoLine(thisLine); // beg of line
- CursorLeft(2);
- if (0==Search("\\sN[ \t]*\}","=wf+",lineLength))
- indentVal -= ReadInfo("c-brace-offset");
-
- // c-label-offset - add if case/default is first on line
- GotoLine(thisLine);
- CursorLeft(2);
- if (0==Search("\\sN[ \t]*((case)|(default))","=wf+", lineLength))
- indentVal += ReadInfo("c-label-offset");
-
-
- //// Checks on previous line
-
- // c-cont-offset - add if prev "if/while" line had no {/;
- GotoLine(prevLine);
- lineLength = ReadInfo("line_length"); // length of prev line + a bit for \n etc
- if (0==Search("(if|while)[ \t]*\\(.*\\)[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*\\sN","=wf+", lineLength))
- indentVal += ReadInfo("c-cont-offset");
- else { // de-indent if prev line was indented
- GotoLine(findPrevLine(prevLine));
- if (0==Search("(if|while)[ \t]*\\(.*\\)[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*\\sN","=wf+", lineLength))
- indentVal -= ReadInfo("c-cont-offset");
- }
-
- // c-indent-level - add if prev line end in {.
- GotoLine(prevLine);
- lineLength -= CursorLeft();
- if (0==Search("\{[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*\\sN","=wf+",lineLength))
- indentVal += ReadInfo("c-indent-level");
-
- // c-indent-level - sub if prev line end in }.
- GotoLine(prevLine);
- if (Isspace(GetChar())){
- CursorRightWord();
- if (GetChar()!='\}'){
- GotoLine(prevLine);
- CursorLeft();
- if (0==Search("\}[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*\\sN","=wf+",lineLength))
- indentVal -= ReadInfo("c-indent-level");
- }
- }
-
- CurrentBuffer(thisBuffer);
- Kill(searchBuffer);
-
- if (GetIndent(thisLine, GetLine(thisLine)) != indentVal){
- GotoLine(thisLine);
- while(Isspace(GetChar())&&!Isnewline(GetChar()))
- Delete();
- if (indentVal>0)
- InsertIndent(indentVal);
- }
- }
- }
-
-
- int findPrevLine(int thisLine)
- {
- int i;
- string s;
- int line=ReadInfo("line");
- int byte=ReadInfo("byte_position");
-
- while(thisLine>0){
- s=GetLine(--thisLine);
-
- for(i=0;(!Isnewline(s[i])&&(Isspace(s[i])));i++);
-
- if (Isnewline(s[i])){
- GotoLine(thisLine);
- DeleteEol();
- } else {
- GotoLine(line, byte);
- return(thisLine);
- }
- }
- GotoLine(line, byte);
- return(0);
- }
-
- void InsertIndent(int indentation)
- {
- int x, tabs, spaces;
- int tabsize = ReadInfo("tab_size");
- string output;
-
- 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, " ");
- }
- }
- Output(output);
- }
-
-
- int GetIndent(int linenum, string inline)
- {
- int linelen = strlen(inline);
- int x;
-
- for (x = 0; x < linelen && ((inline[x] == '\t') || (inline[x] == ' ')); x++);
- return(GetCursor(x, linenum) - 1);
- }
-
- //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» Key bindings ««
- AssignKey("CIndent();", "'tab'", "c_indent_mode");
- AssignKey("Output(\"\t\");", "shift 'tab'", "c_indent_mode");
- AssignKey("CIndent();GotoLine(ReadInfo(\"line\"),ReadInfo(\"line_length\"));Output(\"\n\");CIndent();", "shift 'Enter'", "c_indent_mode");
- AssignKey("Output(\"\n\");", "'enter'", "c_indent_mode");
-
- //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CIndentMode settings ««
- ConstructInfo("c_usetabs", "", "", "GBWH", "", 0, 1, 1);
- ConstructInfo("c-indent-level","","","GIWH","",-32,32,2);
- ConstructInfo("c-cont-offset","","","GIWH","",-32,32,2);
- ConstructInfo("c-brace-offset","","","GIWH","",-32,32,2);
- ConstructInfo("c-label-offset","","","GIWH","",-32,32,-2);
-
- ConstructInfo("c_indent_mode", "", "", "LBH", "", 0, 1, 0);
-
- AddMode(0,"c_indent_mode", "", ""); // Add as minor mode
-
- //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CIndent menu ««
- MenuAdd("s", "CIndentMode...", "CIndentModePrefs();", "", 6,6,-1); // Add to PackageSettings
- MenuBuild();
-