[comp.emacs] Bi-directional search for GNU Emacs

spencer@ttidca.TTI.COM (David Spencer) (06/09/87)

	Below is source code for a bi-directional search routine for
	GNU Emacs (tested under 18.36.1).  It searches both forwards
	and backwards for a given string and then moves to the closest
	occurrence.  The code is fairly simple, but I welcome
	any comments on lisp coding style and whether there is an easier
	way to do this within GNU Emacs.  Installation instructions are
	at the beginning of the code.


			David Spencer

			spencer@ttidca	
  ...!{philabs, trwrb, csun, mtxinu}!ttidca!spencer




--------- CUT HERE AND PUT IN ~/Emacs/bi-search.el -------------------


;
;===========================================================================
;
;	bi-search.el
;
;===========================================================================
;


;
;
;
;	To map search-back-and-forth to M-? uncomment the next
;	four commented lines and put them in your .emacs file.
;	Put this file into ~/Emacs/bi-search.el and then run
;	byte-compile on it.

; (autoload 'search-back-and-forth  "~/Emacs/bi-search.elc"
;	  "Search back and forth for STRING, moving to the closest occurance." 
;		t nil)
; (global-set-key "\e?" 'search-back-and-forth)



;
;---------------------------------------------------------------------------
;
;	search-back-and-forth
;
;		This routine uses search-backward and search-forward to
;		search back and forth for a string, and then moves to the
;		closest match.
;
;---------------------------------------------------------------------------
;

(defun search-back-and-forth (string)
  "Search back and forth for STRING and move to the closest match."
  (interactive "sBidirectional search for: ")
  (setq start (point))

  ; try to go backwards for the string
  (if (setq back (search-backward string (point-min) t)) ; found it
      (progn
	(setq back-ch (point))		; remember where
	(goto-char start)))		; go back to the start

  ; now try to go forward
  (if (setq fwd (search-forward string (point-max) t)) ; found it
      (setq fwd-ch (point)))	; remember where, and leave the point

  (if back   		; we found the string going backward
      (if fwd		; we found it going forwards
	  (progn	; which is closer?
	    (setq d-back (- start back-ch))
	    (setq d-fwd (- fwd-ch start))
	    (if (< d-fwd d-back)	; going forward is closer
		(message "Went forward %d characters instead of back %d."
			 d-fwd d-back)
		(progn			; else going backward is closer
		  (goto-char back-ch)
		  (message "Went backward %d characters instead of forward %d."
			   d-back d-fwd))))
	; else can only go back
	    (progn
	      (goto-char back-ch)
	      (message "Had to go back %d characters." (- start back-ch))))

    ; else can not go back
	(if fwd		; can only go forward
	    (message "Had to go forward %d characters." (- fwd-ch start))
	  ; can not go back or forward
	(progn
	  (goto-char start)
	  (ding)
	  (message "Couldn't find it in either direction!")))))

-- 
-----------------------------------------------------------------------
			spencer@ttidca	
  ...!{philabs, trwrb, csun, mtxinu}!ttidca!spencer