Posted by: Bamsen
« on: December 11, 2012, 11:05:02 AM »I made this macro which opens a dialog box asking for the old (with regex) and the new delimiter, and then formats all columns in file or only in selection.
---
Morten
---
Morten
Code: [Select]
/**
* Prosedyre: typeless MAS_Format_Column()
*
* @key Alt + B + F
*
* @author Bamse (26.06.2012)
*
* @return typeless
*/
_command MAS_Format_Column() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL)
{
int cur_mark = _alloc_selection('B');
_select_char(cur_mark);
int left_edge = p_left_edge;
int cursor_y = p_cursor_y;
_str search_options = "lh@";
int SearchStatus = 0;
_str Linje=aMatch='';
int dlgResult = textBoxDialog ('Format Colomn',0,5000,'Perl regular expressions','','','Eksisterende skilletegn:\t','Nye skilletegn: | ');
_param2=stranslate(_param2,' ','\t','I');
int Start=0;
boolean Selected=false;
typeless Dummy;
if (_get_selinfo(Start,Dummy,Dummy)) { // Sjekk om det er en selection og finn første kolonne
top();
} else {
search_options = search_options :+ "m";
Selected=true;
_begin_select();
}
int KolonneBredde[];
_str KolonneData[];
int i=0;
for (;;) { // Løp gjennom alle linjer for å finne maks kolonnebredde.
get_line(Linje);
KolonneData=MST_Split_To_StrArray(Linje,_param1);
for (i=0;i<KolonneData._length();i++) { // Sjekk alle kolonner
if (i == KolonneBredde._length()) { // Første gang må array elementet initialiseres
KolonneBredde[i]=KolonneData[i]._length();
} else { // har kolonnedata
if (KolonneData[i]._length() > KolonneBredde[i] ) { // Denne er større
KolonneBredde[i]=KolonneData[i]._length();
}
}
}
if (down()) break;
if (Selected) {
if (Start == 1) {
if (_end_select_compare() >= 0) break; // Ikke ta med cursor linje hvis kolonne 1
} else {
if (_end_select_compare() > 0) break;
}
}
}
if (select_active()) { // Sjekk om det er en selection, og begynn på nytt.
_begin_select();
} else {
top();
}
_str NewLine='';
int Delta=0;
for (;;) { // Løp gjennom alle linjer for å justere kolonner
get_line(Linje);
KolonneData=MST_Split_To_StrArray(Linje,_param1);
NewLine='';
for (i=0;i<KolonneData._length()-1;i++) { // Sjekk alle kolonner
Delta=MST_Count_HighBit(KolonneData[i]); // MÃ¥ ta hensyn til norske tegn.
// Hvert tegn må ha +1 til bredde
NewLine = NewLine :+ _pad(KolonneData[i],KolonneBredde[i]+Delta,' ') :+ _param2;
}
NewLine = NewLine :+ KolonneData[i];
replace_line(NewLine);
if (down()) break;
if (Selected) {
if (Start == 1) {
if (_end_select_compare() >= 0) break; // Ikke ta med cursor linje hvis kolonne 1
} else {
if (_end_select_compare() > 0) break;
}
}
}
_begin_select(cur_mark);
set_scroll_pos(left_edge, cursor_y);
_free_selection(cur_mark);
}
/**
* Prosedyre: STRARRAY MST_Split_To_StrArray(_str String, _str Delimiter)
*
* @author Bamse (26.06.2012)
*
* @param String
* @param Delimiter
*
* @return STRARRAY
*/
STRARRAY MST_Split_To_StrArray(_str String, _str Delimiter)
{
_str Array[];
int i=1;
while (i) {
i=pos(Delimiter,String,1,'li');
if (i) {
Array[Array._length()]=substr(String,1,i-1);
String=substr(String,i+pos(''));
if (String == '') {
break;
}
} else {
Array[Array._length()]=String;
String='';
}
}
return Array;
}
/**
* Prosedyre: int MST_Count_HighBit(_str In)
*
* Telle alle tegn med høyeste bit satt. ex. øæå
*
* @author Bamse (07.07.2012)
*
* @param In
*
* @return int
*/
int MST_Count_HighBit(_str In)
{
return MST_Count_Occurence('[\x80-\xFF]',In);
}
/**
* Prosedyre: int MST_Count_Occurence(_str Find, _str In)
*
* Telle hvor mange ganger Find finnes i In
*
* @author a792247 (2012-06-27)
*
* @param Find
* @param In
*
* @return int
**/
int MST_Count_Occurence(_str Find, _str In)
{
int i=1,j=0;
while (i) {
i=pos(Find,In,1,'li');
if (i) {
j++;
In=substr(In,i+pos(''));
if (In == '') {
break;
}
}
}
return j;
}