help-emacs-windows
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[h-e-w] Bidirectional search for word at cursor


From: David Gallucci
Subject: [h-e-w] Bidirectional search for word at cursor
Date: Mon, 19 Nov 2001 22:50:44 -0500

Thanks to all who replied on previous posts. After digging a bit, here's
what I came up with searching for the word at the cursor. The explanation
below is self-explanatory. I map this to <f2> and then use the "," and "."
keys to control search direction. I find it pretty useful. The directional
control reminds me of undo-redo, like when you move too far past where you
want to be.

Constructive criticisms appreciated.

Dave Gallucci



;; start code
(defun dg-find-word-at-cursor ()
  "Finds word at cursor in either direction. Once this function is
executing,
you can search in either direction for the word under the cursor. You can
also
change the direction of the search mid-stream. Search direction is
controlled
by the ',' (comma) and the '.' (period) keys. The comma key searches
backwards
and the period key searches forwards. You can change the direction of the
search by simply hitting the key opposite to your current search
direction. The search is terminated when the last occurence in the current
direction is found, or when a key other than the comma or period has been
hit."
  (interactive)
  ;; get user's choice of forward search or backward search
  (setq char (read-event " "))
  (setq word (current-word))
  (if (looking-at (regexp-quote word))
      (forward-char))
  (while (or (eq char ?,) (eq char ?.))
    (progn
      ;; do search in selected direction...
      ;; this one checks for forward
      (if (eq char ?.)
          (progn
            (while (eq char ?.)
              (progn
                (if (not (search-forward word nil t))
                    (progn
                      (message (format "\"%s\" not found." word))
                      ;; not found - break out of while
                      (setq char ?q))
                      ;; else we did have a match
                  (progn
                    (highlight-regexp word "hi-yellow")
                    (setq char (read-event " "))
                    (if (looking-at (regexp-quote word))
                        (forward-char)))
                  (unhighlight-regexp word))))))
      ;; this one checks for backward
      (if (eq char ?,)
          (progn
            (while (eq char ?,)
              (progn
                (if (not (search-backward word nil t))
                    (progn
                      (message (format "\"%s\" not found." word))
                      ;; not found - break out of while
                      (setq char ?q))
                      ;; else we did have a match
                  (progn
                    (highlight-regexp word "hi-yellow")
                    (setq char (read-event " "))
                    (if (looking-at (regexp-quote word))
                        (forward-char)))
                  (unhighlight-regexp word))))))))
  ;; make sure to remove any highlight!
  (unhighlight-regexp word))

;; end code




reply via email to

[Prev in Thread] Current Thread [Next in Thread]