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 / html.ex < prev    next >
Encoding:
Text File  |  2004-02-22  |  2.0 KB  |  81 lines

  1. "This file contains some maps that are useful when editing XML or HTML code.
  2.  
  3. " When a > is input at the end of a tag, automatically add the closing tag
  4. map input > noremap >mmF<ye`mpa>bi/`mli
  5.  
  6. " Make the % command match XML tags.  This doesn't work as the destination of
  7. " operator commands or during visual selections, though, so we only map it for
  8. " commands.
  9. map command % noremap :xmlmatch
  10.  
  11. " Add <> to matchchar
  12. set matchchar="{}()[]<>"
  13.  
  14. alias xmlmatch {
  15.     "Move the cursor to the matching HTML or XML tag name
  16.  
  17.     "If not on tag name, then do the normal % character match
  18.     if current(/\i/) == ""
  19.     then normal %
  20.     else {
  21.     " d is the direction to search
  22.     " i counts nested tag pairs
  23.     " n is the tag name without any punctuation
  24.     " t is origin tag name without args or >
  25.     local d i n t
  26.  
  27.     "Configure search parameters to be "normal"
  28.     local nowrapscan magic magicchar=^$.[* magicname noincsearch ignorecase nosmartcase
  29.  
  30.     "HTML ignores case, but XML is case sensitive
  31.     if (tolower(dirext(filename)) << 4) == ".htm"
  32.     then set ignorecase
  33.     else set noignorecase
  34.  
  35.     "This particular alias doesn't really change the file, but it uses
  36.     "the :normal command which *can* change the file and hence is not
  37.     "allowed on locked buffers.  Temporarily turn off locking.
  38.     local nolocked
  39.  
  40.     "Get the current tag name
  41.     let t=current(/<\/\i\+/)
  42.     if t
  43.     then {
  44.         let d="backward"
  45.         let n=t[,3...]
  46.     }
  47.     else {
  48.         let t=current(/<\i\+/)
  49.         if t
  50.         then {
  51.         let d="forward"
  52.         let n=t[,2...]
  53.         }
  54.         else error cursor isn't on a tag name
  55.     }
  56.  
  57.     " move to the start of this tag, so we don't immediately find it in
  58.     " the following search loop and mistake it for a nested tag.
  59.     normal F<
  60.  
  61.     "search for the tag, for nested tags.  Stop on the matching one
  62.     normal mx
  63.     try {
  64.         set i=1
  65.         while i > 0
  66.         do {
  67.         "find the next opening or closing tag, in the proper direction
  68.         switch d
  69.         case forward normal /<\/\?\=$n\>
  70.         case backward normal ?</*\=$n\>
  71.  
  72.         "count nested tag levels
  73.         if current(/<\/\?$n/) == t
  74.         then let i = i + 1
  75.         else let i = i - 1
  76.         }
  77.     }
  78.     else normal `x
  79.     }
  80. }
  81.