Author Topic: Vague memory of macro that column formated text based on character  (Read 7796 times)

lhasadad

  • Community Member
  • Posts: 15
  • Hero Points: 3
I vaguely remember there once being a macro that would do a format_columns like behavior but with the difference that rather than aligning at every white space it would align at a character (example would be csv style file,  if the format was done with the ',' all the items would line up after the first, right most matching character,  and then same for second matching character and so on. Am I dreaming or did this macro once exist?

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: Vague memory of macro that column formated text based on character
« Reply #1 on: December 10, 2012, 11:04:31 PM »
Do you think of this one ? This is a related posting.
Should be easy to modify to align on ','.
HS2

lhasadad

  • Community Member
  • Posts: 15
  • Hero Points: 3
Re: Vague memory of macro that column formated text based on character
« Reply #2 on: December 11, 2012, 03:47:56 AM »
Possibly or something derived from the one with ='s that lets you specify the character

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: Vague memory of macro that column formated text based on character
« Reply #3 on: December 11, 2012, 09:22:37 AM »
It's not that elegant but you could use a similar static/global variable like 'gSlideStr' set by the calling wrapper macro for adjusting the character to align on and slightly modify 'find_max_eq_filter' accordingly. See the 'slide_in_prompt' macro in the zip file for details.
Good luck,
HS2

Bamsen

  • Community Member
  • Posts: 66
  • Hero Points: 8
Re: Vague memory of macro that column formated text based on character
« Reply #4 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

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;
}