// PADLINES - Pad all lines in a file so that they have the same length. // Implements {EDIT, Convert, All lines same length}. // //-------------------------------------------------------------------------------- // // Originally by: Theodore Green, Greenview Data, Inc. // (from: 02/19/03) // // Modified: 09/14/2004 by Christian Ziemski // // fixed: Scan longest line was one character too long // // added: check for TAB characters and offering Detabbing if any TAB found // // added: N-option and optional -u options (see below) // // added: button: [Squeeze] to remove trailing whitespace // // removed restriction to -N > 8 (what has that been for?) // removed (commented out) restriction to >20 (what has that been for?) // removed/commented out one usage of #104 in loop(what has that been for?) // // some other little modifications // // Last change: 09/15/2004 by Christian Ziemski // // changed: "squeeze" to "trim" // changed: allow to omit parameter -Nn: pad to longest line (same as n=0) // //-------------------------------------------------------------------------------- // // Requires: VEDIT 6.02 or later. // // From VEDIT: Select {EDIT, Convert, All lines same length}. // // Description: Checks for TABs in the file and offers to convert them to spaces. // Prompts for the desired line length, which should be equal // to or greater than the longest line in the file. It then // adds spaces at the end of each line to pad it to the // desired length. All lines will then have the same length. // // The Windows version can "Scan" the current file to // determine the longest line; the suggested value will then // pad all lines to this length. // And it can "Trim" the lines by removing trailing whitespace. // // A warning is given if an existing line is longer than the // desired column number; you are then prompted to ignore it, // truncate the line, or to stop. // // From OS: VPW [-Nn] [-w] -X PADLINES filename [-u [trim] [detab] [truncate]] // // The line length 'n' is specified by the parameter -Nn. // If n=0 or the parameter -Nn is omitted the file is padded // to the longest line length. // // The optional parameters after -u are doing the following: // // trim: trailing whitespace is removed before the padding is done. // // detab: possible existing TAB characters are converted // to spaces before the padding is done. // // truncate: if there are lines longer than 'n' they are silently // truncated to 'n' characters. Otherwise there will // be a message window. // // When the end of file is reached, the file is saved and // Vedit terminates. // // The optional parameter "-w" hides the File Selector window. // //////////////// // // if (Config(F_F_TYPE) >= 8) { if (OS_Type==1) { DI1(103,"`ERROR - Pad lines to same length`,`This function is not available in Binary/Record mode!\nE.g. when {CONFIG, File handling, File type} >= 8`",APP+CENTER,0,0) } else { Get_Key("This function is not available in Binary/Record mode!",STATLINE) } return(-1) } #103 = 80 //Default value is 80 Config( D_DSP_WRAP, 0,LOCAL ) // disable Word_Wrap to avoid EoL-problems // // If run via "-x" invocation option (auto-execution), // look for and process the parameters // if (Is_Auto_Execution && Macro_Num == 100) { #103 = N_Option if (#103 < 0) { #103=0 } #104=Buf_Num Buf_Switch(Buf_Free) Out_Ins() CMD_LINE Out_Ins(CLEAR) BoF Search("|W-u|W", NOERR+ADVANCE) // try to find -u (not within filenames...) if (! Error_Match) { Del_Block(0, Cur_Pos) // delete commandline, only leave -u parameters #106=0 if (search("trim", BEGIN+NOERR) != 0) { #106 = #106 | 1 } if (search("detab", BEGIN+NOERR) != 0) { #106 = #106 | 2 } if (search("truncate", BEGIN+NOERR) != 0) { #106 = #106 | 4 } } Buf_Quit(OK) Buf_Switch(#104) if (#106 & 1) { // remove all trailing whitespace Replace("|W|>","",ALL+NOERR+BEGIN) } if (#106 & 2) { // convert all TABs to spaces Detab_Block(0,File_Size) } if (#103 == 0) { // set padding-length to length of longest line Call("FIND_LONGEST_LINE") } } else { if (OS_Type == 1) { Save_Pos() Begin_Of_File Search("|T", BEGIN+NOERR) if (! Error_Match) { // TAB found Update #117=DI1(121,^`Pad lines to same length`, `There is at least one TAB character in the current file.`, `We suggest converting tab characters to spaces before using this function.`, `[&Detab]`,`[&Not now]`,`[&Cancel]`^,@117,APP+CENTER,0,0) Restore_Pos() if (#117==0 || #117==3) { Return } if (#117==1) { Detab_Block(0,File_Size) } } else { Restore_Pos() } :AGAIN: Update ItoA(#103,117,NOCR) //Convert to string in @117 #117=DI1(121,^`Pad lines to same length`, `This function will add spaces at the end of each line to pad it to the desired length. All lines will then have the same length. [Scan] Scan current file to determine length of longest line; use as default length. (Same as Line length=0 below.) [Pad] Pad lines by adding spaces [Trim] Removes trailing whitespaces (Perhaps useful before [Scan]) [Cancel] Cancel. Don't change file.`, `??Line length:`,`[&Scan]`,`[&Pad]`,`[&Trim]`,`[&Cancel]`^,@117,APP+CENTER,0,0) if (#117==0 || #117==4) { Return } if (#117==1) { Call("FIND_LONGEST_LINE") Goto_Pos(#104) goto AGAIN } if (#117==3) { Save_Pos() Replace("|W|>","",ALL+NOERR+BEGIN) Restore_Pos() goto AGAIN } #103 = AtoI(121) if (#103 < 0) { DI1(103,"`ERROR - Pad lines to same length`,`No negative length allowed. Try again.`",APP+CENTER,0,0) goto AGAIN } if (#103==0) { Call("FIND_LONGEST_LINE") } // if (#103<20) { // DI1(103,"`ERROR - Pad lines to same length`,`Minimum value is 20. Try again.`",APP+CENTER,0,0) // goto AGAIN // } } else { //Else Non-Windows version if (File_Size < 250000) { Call("FIND_LONGEST_LINE") } //:AGAIN2: #103=Get_Num("Enter line length for space padding: ",STATLINE) // if (#103<20) { Get_Key("Minimum value is 20. Try again.",STATLINE) goto AGAIN2 } } } // // Loop to perform the padding. // Message("Processing... (About 2 sec / Megabyte) -- Press to abort ",STATLINE) BOF() // #104 = 0 while (! At_EOF) { EOL() if (Cur_Col > #103+1) { if (Is_Auto_Execution && Macro_Num == 100) { if (#106 & 4) { Goto_Col(#103+1) Del_Block(Cur_Pos,EOL_Pos) } } else { Update() Alert() if (OS_Type==1) { // if (#104==0) { #117 = DI1(104,"`ERROR - Pad lines to same length`, `An existing line is longer than the desired length!!!`, `Suggest [Stop] and trying again with larger line length.\nIf you [Ignore], this line will be longer than others.\nOr you can [Truncate] this line to shorten it.`, `[] Ignore or Truncate all occurrences`, `[&Ignore]`,`[&Truncate]`,`[&Stop]`",APP+CENTER,0,0) if (#117==0 || #117==3) { Return } // } if (#117==2) { Goto_Col(#103+1) Del_Block(Cur_Pos,EOL_Pos) } } else { Get_Key("Current line is too long!!! to continue; to Abort",STATLINE) } } } else { Ins_Char(' ',COUNT,#103-Cur_Col+1) } Line(1,ERRBREAK) } if (Is_Auto_Execution && Macro_Num == 100) { Xall() } return //----------------------------------------------------------------------------- :FIND_LONGEST_LINE: Message("Scanning... (About 2 sec / Megabyte) -- Press to abort ",STATLINE) #103=0 // length of longest line #104=0 // Cursor position of longest line Begin_Of_File() while (! AT_EOF) { EOL() if (Cur_Col>#103) { #103=Cur_Col #104=Cur_Pos} Line(1,ERRBREAK) } #103-- // fix return