home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 March / PCWELT_3_2006.ISO / base / 05_common.mo / usr / share / elvis-2.2_0 / scripts / align.ex next >
Encoding:
Text File  |  2004-02-22  |  2.7 KB  |  102 lines

  1. " Defines an :align alias for aligning = signs, or some other delimiter.  This
  2. " can be handy when you're trying to make a Makefile look pretty.
  3. " Contributed by Ian Utley (iu@apertus.uk.com)
  4.  
  5. alias align {
  6.     "Align any = signs (or other given text) in selected line
  7.     local f=0 i=0 k report=0 nosaveregexp magic magicchar=^$.[* noignorecase
  8.     "
  9.     " The following if tests to see if we have visually highlighted lines.
  10.     "
  11.     if ( !> !!= "" )
  12.     then {
  13.         !< mark a
  14.         !> mark b
  15.         let f=1
  16.     }
  17.     if ( f == 1)
  18.     then {
  19.         "
  20.         " Initialise i which will store the alignment column.
  21.         " Mark the current line to return the cursor at the end.
  22.         "
  23.         set i=0
  24.         mark z
  25.         "
  26.         " Remove any whitespace before the alignment character.
  27.         "
  28.         'a,'b s/[     ]*!(=)\$/!(=)\$/
  29.         "
  30.         " We could be aligning != <= or >= so we want to keep this letter
  31.         " near. Of course we may not be aligning an equals but we commonly do.
  32.         "
  33.         if ( "!(=)\$" == "=" )
  34.         then {
  35.             'a,'b s/[     ]*\([!!<>]*\)!(=)\$[     ]*/ \1!(=)\$ /
  36.         } 
  37.         "
  38.         "
  39.         let f=0
  40.         'a,'bglobal /!(=)\$/ {
  41.             " 
  42.             " Special case for the top line as -1 will not work.
  43.             "
  44.             if ( current("line") == 1 )
  45.             then {
  46.                 1 insert ""
  47.                 let f=1
  48.             }
  49.             -1
  50.             /!(=)\$
  51.             "
  52.             "
  53.             " Remember the largest column number for alignment.
  54.             "
  55.             if (current("column")>i)
  56.             then let i=current("column")
  57.             "
  58.             " Special case removal
  59.             "
  60.             if ( current("line") > 1 && f == 1)
  61.             then {
  62.                 1 delete
  63.                 let f=0
  64.             }
  65.         }
  66.         "
  67.         " Do the alignment.
  68.         "
  69.         let f=0
  70.         'a,'bglobal /!(=)\$/ {
  71.             " 
  72.             " Special case for the top line as -1 will not work.
  73.             "
  74.             if ( current("line") == 1 )
  75.             then {
  76.                 1i ""
  77.                 let f=1
  78.             }
  79.             -1
  80.             /!(=)\$
  81.             "
  82.             " Not sure why I need to add +1
  83.             "
  84.             let k=i-current("column")+1
  85.             s/\([!!<>]*\)!(=)\$/                                                                                \1!(=)\$/
  86.             eval s/ *\\\( \{(k)\}[!!<>]*!(=)\$\\\)/\1
  87.             "
  88.             " Special case removal
  89.             "
  90.             if ( current("line") > 1 && f == 1 )
  91.             then {
  92.                 1 delete
  93.                 let f=0
  94.             }
  95.         }
  96.         "
  97.         " Return the cursor to the line it was previously on.
  98.         "
  99.         'z
  100.     }
  101. }
  102.