home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / mutt / isearch.mut < prev    next >
Lisp/Scheme  |  1988-09-08  |  4KB  |  145 lines

  1.   ;; isearch.mut: Incremental searching.
  2.  
  3.   ;;   Searching as you type - shows you where the string you have typed so
  4.   ;; far would be found.
  5.   
  6.   ;; Keys:
  7.   ;;  ^H (Backspace):  Remove the last character from the search pattern.
  8.   ;;     Don't move.
  9.   ;;  ^S:  Search forward for existing pattern.  If haven't entered a
  10.   ;;     pattern, the old pattern is used.
  11.   ;;  ^R:  Search reverse for pattern.
  12.   ;;  ^Q:  Quote the next character into the search pattern.  Usefull for
  13.   ;;     searching for control characters.
  14.   ;;  ^W:  Add the rest of the word to the search pattern.
  15.   ;;  ^G:  Exit search.
  16.   ;;  ^M (Enter): Drop to non incremental search if haven't done anything
  17.   ;;     yet.
  18.   ;;  Control characters and combo keys (eg cursor motion, C-X=, etc)
  19.   ;;    terminate the search.  The key is then executed.  For example,
  20.   ;;    searching for text and then pressing C-P will cause the cursor to
  21.   ;;    move to the line above the matched text.
  22.  
  23. (defun chr$ (int n)(array byte str 2) HIDDEN { (str 0 n)(str 1 0) str })
  24.  
  25. (string oldpat 80)
  26.  
  27. (defun isearch (bool forward-search) HIDDEN
  28. {
  29.   (string ipat 80 hoho 40 tmp 3 direction 10)
  30.   (int sc len)
  31.   (bool firsttime forward)
  32.  
  33.   (direction (if (forward forward-search) "forward" "reverse"))
  34.   (firsttime TRUE)(ipat (hoho ""))
  35.   (while TRUE
  36.   {
  37.     (msg "I-search-" direction " [" ipat "] " hoho ' (^S ^R ^W ^Q)')
  38.     (if (not (key-waiting)) (update))
  39.     (hoho "")
  40.     (switch (sc (get-key))
  41.       0x148    ; backspace
  42.     (ipat (substr ipat 0 -1))
  43.       0x153    ; ^S
  44.       {
  45.     (forward TRUE)(direction "forward")
  46.     (if firsttime
  47.     {
  48.       (firsttime FALSE)
  49.       (if (== oldpat "")(continue))
  50.       (ipat oldpat)
  51.     })
  52.     (if (not (search-forward ipat))(hoho "Not found."))
  53.       }
  54.       0x152    ; ^R: reverse the search
  55.       {
  56.     (forward FALSE)(direction "reverse")
  57.     (if firsttime
  58.       {
  59.         (firsttime FALSE)
  60.         (if (== oldpat "")(continue))
  61.         (ipat oldpat)
  62.         (if (search-reverse ipat)
  63.           (search-forward "")    ; put dot at end of pattern
  64.           (hoho "Not found.")
  65.         )
  66.       }
  67.       {
  68.         (arg-prefix (strlen ipat))(previous-character)
  69.         (if (search-reverse ipat)
  70.           (search-forward "")        ; put dot at end of pattern
  71.           {
  72.         (arg-prefix (strlen ipat))(next-character)
  73.         (hoho "Not found.")
  74.           }
  75.         )
  76.       }
  77.     )
  78.       }
  79.       0x151    ; ^Q: quote next character
  80.       {
  81.     (len (strlen ipat))
  82.     (ipat (concat ipat (getchar)))
  83.     (goto ack)
  84.       }
  85.       0x157    ; ^W: grab word
  86.       {
  87.     (if (looking-at '\w+')
  88.     {
  89.       (len (strlen ipat))
  90.       (ipat (concat ipat (get-matched '&')))
  91.       (goto ack)    ; we know the search will succeed
  92.     })
  93.     (hoho "Not looking at a word.")
  94.       }
  95.       0x14D    ; ^M: drop down into non incremental search
  96.       {
  97.     (if firsttime
  98.     {
  99.       (ask-user)
  100.       (if forward (search-forward) (search-reverse))
  101.       (done)
  102.     })
  103.       }
  104.       0x147    ; ^G: quit
  105.       {
  106.     (msg "Done.")
  107.       (label quit)
  108.     (if (!= "" ipat) (oldpat ipat))    ; save search pattern (if interesting).
  109.     (done)
  110.       }
  111.       default
  112.       {
  113.     (if (> sc 0xFF) { (msg "")(exe-key sc)(goto quit) })
  114.     (len (strlen ipat))
  115.     (ipat (concat ipat (chr$ sc tmp)))
  116.       (label ack)
  117.     (firsttime FALSE)
  118.     (if forward
  119.       {
  120.         (arg-prefix len)(previous-character)  ; move to start of pattern
  121.         (if (not (search-forward ipat))
  122.         {
  123.           (arg-prefix len)(next-character)
  124.         (label gack)
  125.           (ipat (substr ipat 0 -1))(hoho "Not found.")
  126.         })
  127.       }
  128.       {
  129.         (if (search-reverse ipat)
  130.           (search-forward "")
  131.           (goto gack)
  132.         )
  133.       }
  134.     )
  135.       }
  136.     )
  137.   })
  138. })
  139.  
  140.  
  141. (defun
  142.   isearch-forward { (isearch TRUE)  }    ; Normally bound to C-S
  143.   isearch-reverse { (isearch FALSE) }    ; Normally bound to C-R
  144. )
  145.