home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / me34exe.zip / mutt / contrib / autparen.mut < prev    next >
Text File  |  1995-01-14  |  3KB  |  80 lines

  1. ; autparen.mut    --    Show matching open parens while typing
  2. ;
  3. ; Author: Tim Brengle
  4. ;   largely derived from pmatch.mut by Craig Durland
  5. ;
  6. ; This rebinds the ")", "}", and "]" keys so that typing one of them causes
  7. ; the corresponding opening delimiter to be shown briefly.  If there is no
  8. ; matching instance, you will hear a beep (if the sysvar (beeper) is non-zero)
  9. ; or see a message.  The DELAY const has been chosen empirically so that I can
  10. ; keep typing at my normal speed on my PC-AT clone and just listen for the
  11. ; beep.  Your mileage may vary.  There should be no impact as long as you have
  12. ; sufficient type-ahead capacity.
  13. ;
  14. ; Problems and gotchas:
  15. ;    In order for the regular expression to work properly with square brackets
  16. ;    ("[" and "]"), the right bracket must appear first.  Since it makes no
  17. ;    difference for the others, I made it that way for all.
  18. ;
  19. ;    Instances of the delimiters appearing as parts of strings are not handled
  20. ;    any differently.  Try removing and replacing the last ")" in this file.
  21. ;    At least GNU Emacs doesn't handle it any better...
  22. ;
  23. ;    Adding new matching delimiter pairs should be obvious, but they must be
  24. ;    different characters; this won't work on strings, for instance.
  25.  
  26. (const DELAY 1)        ; Number of seconds to delay
  27.  
  28. (defun
  29.   insert-and-match (open-paren close-paren) HIDDEN
  30.   {
  31.     (string m)(int c mark)
  32.  
  33.     (m (concat '[' close-paren open-paren ']'))    ;form regular expression
  34.     (insert-text close-paren)            ;insert delimiter
  35.     (previous-character)            ;so match won't end too soon
  36.     (set-mark (mark (create-mark)))
  37.     (c 1)
  38.     (while (!= c 0)                ;while not-balanced
  39.     {
  40.       (if (re-search-reverse m)            ;find either open- or close-?
  41.     (switch (get-matched '&')        ;yes: update balance count
  42.       close-paren  (+= c 1)
  43.       open-paren (c (- c 1))
  44.     )
  45.     {                    ;no: must be unbalanced
  46.       (if (== (beep -1) 0)
  47.         (msg 'No matching "' open-paren '"')
  48.         (beep)
  49.       )
  50.       (goto-mark mark)            ;back to insertion point
  51.           (next-character)
  52.       FALSE
  53.       (done)                ;quit
  54.     }
  55.       )
  56.     })
  57.     (update)                    ;all balanced so show
  58.     (key-waiting DELAY)
  59.     (goto-mark mark)                ;back to insertion point
  60.     (next-character)
  61.     (msg "")
  62.     TRUE
  63.   }
  64. )
  65.  
  66. (defun
  67.   insert-close-paren
  68.   { (insert-and-match '(' ')') }
  69.   insert-close-brace
  70.   { (insert-and-match '{' '}') }
  71.   insert-close-bracket
  72.   { (insert-and-match '[' ']') }
  73.   MAIN
  74.   {
  75.     (bind-to-key 'insert-close-paren'   ')')
  76.     (bind-to-key 'insert-close-brace'   '}')
  77.     (bind-to-key 'insert-close-bracket' ']')
  78.   }
  79. )
  80.