;;; lisp file for xyzzy ver0.2.2.234
;;; scan-reference.l ver.0.0.1.0 // crayonzen [2006-05-03]01:36:58(+0900)

;; how-to: load
;;       : write in .xyzzy or siteinit.l
;;
;; (load-library "scan-reference")

;;"■reference2006-03-29.txt"ファイルを
;;"xyzzy.exe"と同じ階層・ディレクトリ（同じフォルダ）に置きます。
;;選択範囲またはカーソル上の単語を F1 キーで検索します。

(provide "scan-reference")

(in-package "editor")

(export '(scan-reference
	  ))

;;;;;;;;;;;;;;;;
;;scan-reference

(defvar *target-file* "■reference2006-03-29.txt")

(defun scan-reference ()
  (interactive "*")
  (let ((target (scan-target))
	(file *target-file*))
    (setq currentwin (selected-window))
    (setq currentbuf (selected-buffer))
    (if (null (equal target ""))
	(progn
	  ;(setq *last-search-string* target)
	  (add-history target '*minibuffer-search-string-history*)
	  (or (setq buffer (find-buffer-string-equal file))
	      (setq buffer (open-target-file file)))
	  (if buffer
	      (progn
		(if (setq window (get-buffer-window buffer))
		    (set-window window))
		(set-buffer buffer)
		(setq targetcurpoint (point))
		;;	  (if (null (equal currentbuf buffer))
		;;	      (goto-char (point-min)))
		(goto-char (point-min))
		(setq target (format nil "~A~A" "■" target))
		(if (scan-buffer target
				 :regexp nil
				 :case-fold t
				 :left-bound t
				 :right-bound t
				 :no-dup nil
				 :tail nil)
		    (progn
		      (forward-char 1)
		      (recenter 0)
		      (message "target: ~A" target))
		  (progn
		    (goto-char targetcurpoint)
		    (set-window currentwin)
		    (set-buffer currentbuf)
		    (message "non target: ~A" target))
		  )
		)
	    (message "non file: ~S" file)
	    )
	  )
      (message "empty target: ~A" target)
      )
    )
  )

(defun scan-target ()
  (if (pre-selection-p)
      (buffer-substring
       (selection-mark) (selection-point))
    (save-excursion
      (buffer-substring (progn
			  (skip-syntax-spec-forward "w_")
			  (point))
			(progn
			  (skip-syntax-spec-backward "w_")
			  (point)))))
  )

(defun open-target-file (file)
  (let (path
	filepath)
    (setq path (directory-namestring (merge-pathnames "xyzzy.exe" (si:system-root))))
    (setq filepath (merge-pathnames file path))
    (if (file-exist-p filepath)
	(ed::find-file-internal filepath))))
;(find-file filepath)

(global-set-key #\F1 'scan-reference)

;;monkey.l:10:(set-function-bar-label #\F1 "VCヘルプ")
;;winhelp.l:111:(global-set-key #\F1 'find-winhelp)
;;monkey.l:29:(set-function-bar-label #\C-F1 "ヘルプ")
;;winhelp.l:112:(global-set-key #\C-F1 'show-winhelp)
;;monkey.l:34:(set-function-bar-label #\S-C-F1 "S-C-F1")
;;#\S-F1
