;; lisp file for xyzzy ver0.2.2.233
;;;Jamming-search.l
;;;Jammingの辞書を使えるようにする

;(load-library "Jamming-search")

(provide "Jamming-search")

(in-package "editor")

(export '(Jamming-app
	  Jamming-search-normal-all-selection
	  Jamming-search-complete-all-selection
	  Jamming-search-western-all-selection
	  
	  Jamming-search-normal-selection
	  Jamming-search-complete-selection
	  Jamming-search-western-selection

	  Jamming-search-kanjigen-selection
	  Jamming-search-Koujien-selection
	  Jamming-search-Daijirin-selection
	  Jamming-search-Chujiten-selection
	  Jamming-search-pc_wdic-selection

	  get-current-word-or-selection
	  backward-word-stop
	  ;goto-word-head
	  ;init-Jamming-menu
	  ))

;;;;;;;;;;;;;;;;Jamming
(defun Jamming-app ()
  (interactive)
  (launch-application "C:/Program Files/Jamming/Jamming.exe"))

;;;;;;;;;;;;;;;;コマンドライン引数
;;-n"name" 	：　辞書名・辞書セット名
;;-m"method" 	：　検索方法。(method "語尾補正")
;;-w"word"	：　word を検索

;;-p	：　新規ウィンドウの作成を許可しない
;;-f	：　検索後にフォアグラウンドに出る
;;-s	：　完全一致検索
;;-e	：　欧文モードで検索（渡された語を欧州言語だとみなして検索）

;;-d"path" 	：　初期設定ディレクトリ。初期設定ディレクトリを別にしたい時
;;-#012	：　ウィンドウ番号。 複数のJammingが起動。０は全て（デフォルトは０）、複数は1番上が番号１。
;;-i0 〜 -i5   ： 入力欄の指定（デフォルト：-i0）。条件検索などで入力欄


;;;;;;;;;;;;;;;;すべての辞書
;;通常検索
(defun Jamming-search-normal-all-selection ()
  (interactive)
  (let ((word (get-current-word-or-selection)))
    (launch-application
     (format nil "~A~A" "C:/Program Files/Jamming/Jamming.exe -p -f -#1 -n\"すべての辞書\" -w " word))))

;;完全一致検索
(defun Jamming-search-complete-all-selection ()
  (interactive)
  (let ((word (get-current-word-or-selection)))
    (launch-application
     (format nil "~A~A" "C:/Program Files/Jamming/Jamming.exe -p -f -#1 -n\"すべての辞書\" -s -w " word))))

;;欧文モードで検索
(defun Jamming-search-western-all-selection ()
  (interactive)
  (let ((word (get-current-word-or-selection)))
    (launch-application
     (format nil "~A~A" "C:/Program Files/Jamming/Jamming.exe -p -f -#1 -n\"すべての辞書\" -e -w " word))))


;;;;;;;;;;;;;;;;通常
;;通常検索
(defun Jamming-search-normal-selection ()
  (interactive)
  (let ((word (get-current-word-or-selection)))
    (launch-application
     (format nil "~A~A" "C:/Program Files/Jamming/Jamming.exe -p -f -#1 -n\"通常\" -w " word))))

;;完全一致検索
(defun Jamming-search-complete-selection ()
  (interactive)
  (let ((word (get-current-word-or-selection)))
    (launch-application
     (format nil "~A~A" "C:/Program Files/Jamming/Jamming.exe -p -f -#1 -n\"通常\" -s -w " word))))

;;欧文モードで検索
(defun Jamming-search-western-selection ()
  (interactive)
  (let ((word (get-current-word-or-selection)))
    (launch-application
     (format nil "~A~A" "C:/Program Files/Jamming/Jamming.exe -p -f -#1 -n\"通常\" -e -w " word))))

;;;;;;;;;;;;;;;;漢字源
(defun Jamming-search-kanjigen-selection ()
  (interactive)
  (let ((word (get-current-word-or-selection)))
    (launch-application
     (format nil "~A -p -f -#1 -n\"漢字源\" -w ~A" "C:/Program Files/Jamming/Jamming.exe" word)
     )))

(defun Jamming-search-Koujien-selection ()
  (interactive)
  (let ((word (get-current-word-or-selection)))
    (launch-application
     (format nil "~A -p -f -#1 -n\"広辞苑　第四版\" -w ~A" "C:/Program Files/Jamming/Jamming.exe" word)
     )))

(defun Jamming-search-Daijirin-selection ()
  (interactive)
  (let ((word (get-current-word-or-selection)))
    (launch-application
     (format nil "~A -p -f -#1 -n\"三省堂「大辞林」\" -w ~A" "C:/Program Files/Jamming/Jamming.exe" word)
     )))

(defun Jamming-search-Chujiten-selection ()
  (interactive)
  (let ((word (get-current-word-or-selection)))
    (launch-application
     (format nil "~A -p -f -#1 -n\"研究社　新英和・和英中辞典\" -w ~A" "C:/Program Files/Jamming/Jamming.exe" word)
     )))

(defun Jamming-search-pc_wdic-selection ()
  (interactive)
  (let ((word (get-current-word-or-selection)))
    (launch-application
     (format nil "~A -p -f -#1 -n\"通信用語の基礎知識\" -w ~A" "C:/Program Files/Jamming/Jamming.exe" word)
     )))

;;	 (format nil "~A~A" "C:/Program Files/Jamming/Jamming.exe -f -w " word)
;;	 (concat "C:/Program Files/Jamming/Jamming.exe -p -f -n " name)
;;	 (concat "C:/Program Files/Jamming/Jamming.exe -f -w " word)



;;;;;;;;;;;;;;;;文字列の取得
(defun get-current-word-or-selection ()
  (interactive)
  (let (word)
    (if (pre-selection-p)
	(let ((start (selection-point))
	      (end (selection-mark)))
	  (setq word (buffer-substring start end)))
      (save-excursion ;;カーソル位置、インサーションポイントを戻してやる
	(setq word
	      (buffer-substring
	       ;(progn (goto-word-head) (point))
	       ;(progn (backward-word) (point))
	       ;(point) ;;上手く頭が取れないのでカーソル位置に頭
	       (progn (backward-word-stop) (point)) ;;単語の頭で移動を止める
	       (progn (forward-word) (point)))
	      )
	))
    word))

;;;;;;;;;;;;;;;;単語の頭で移動を止める
;(char= (preceding-char) #\LFD) ;#\SPC ;#\^ ;#\TAB
;(char= (following-char) (character "^")
;(setq pchar (preceding-char))
;(when (/= 0 (point))
;(forward-char 1)
;(looking-back " ")

;;(forward-word -1) 記号などはとばしてしまうので
;;(unless (looking-at "\\<");;単語の行頭にマッチは英数字のみなので
;;漢字とカタカナは単語、ひらがなはとばすみたいなので
;;前のキャラが空白などの時は止まるようにする
;;でも後のキャラが同じく空白などの時は移動する

;;動的なものは捨てて、確実に止まる形で固定
;;英数字の単語の頭なら止める
;;前が空白で、後ろが空白でないなら止める
(defun backward-word-stop ()
  (interactive)
  (when (forward-char -1)
    (setq bchar (looking-at "\\([ \t　\n]\\)"))
    (forward-char 1)
    (setq fchar (looking-at "\\([ \t　\n]\\)"))
    (unless (or (looking-at "\\<")
		(and bchar
		     (null fchar)))
      (forward-word -1))
    )
  )

#|
;;;;;;;;;;;;;;;;
;; 単語の先頭
(defun goto-word-head ()
  (cond ((looking-at " ")
	 (backward-word 1))
	((looking-at "\\<"))
	(t
	 (backward-word 1))))
|#

;;;;;;;;;;;;;;;; 挿入メニュー６　insert menu tools ツール
;;ツールの頭に挿入 menu item 0
(defvar *Jamming-popup-menu* nil)

(add-hook '*init-app-menus-hook*
	  #'(lambda ()
	      (init-Jamming-menu)
	      (when (setq menu (get-menu *app-menu* 'ed::tools))
		(insert-popup-menu menu 0 *Jamming-popup-menu* "辞書Jamming(&Q)")
		(insert-menu-separator menu 1 'tool-Jamming-sep)
		)
	      )
	  )

(defun init-Jamming-menu ()
  (or *Jamming-popup-menu*
      (setq *Jamming-popup-menu* (create-popup-menu)))

  (setq *Jamming-popup-menu*
	(define-popup-menu
	  (:item nil "Jamming起動(&P)"
	   'Jamming-app)
	  :sep
	  (:item nil "すべての辞書_通常検索(&A)"
	   'Jamming-search-normal-all-selection)
	  (:item nil "すべての辞書_完全一致検索(&C)"
	   'Jamming-search-complete-all-selection)
	  (:item nil "すべての辞書_欧文モードで検索(&E)"
	   'Jamming-search-western-all-selection)
	  :sep
	  (:item nil "通常_通常検索(&S)"
	   'Jamming-search-normal-selection)
	  (:item nil "通常_完全一致検索(&C)"
	   'Jamming-search-complete-selection)
	  (:item nil "通常_欧文モードで検索(&E)"
	   'Jamming-search-western-selection)
	  :sep
	  (:item nil "漢字源(&J)"
	   'Jamming-search-kanjigen-selection)
	  (:item nil "広辞苑　第四版(&K)"
	   'Jamming-search-Koujien-selection)
	  (:item nil "三省堂「大辞林」(&D)"
	   'Jamming-search-Daijirin-selection)
	  (:item nil "研究社　新英和・和英中辞典(&C)"
	   'Jamming-search-Chujiten-selection)
	  (:item nil "通信用語の基礎知識(&W)"
	   'Jamming-search-pc_wdic-selection)
	  )
	)
  )

;;;;;;;;;;;;;;;; 挿入メニュー　右クリック　バッファー
(add-hook '*init-app-menus-hook*
	  #'(lambda ()
	      (init-Jamming-menu)
	      (when *app-popup-menu*
		(setq ip -1)
		(while (get-menu *app-popup-menu* (incf ip) t))
		(insert-menu-separator *app-popup-menu* ip 'Jamming-sep)
		(insert-popup-menu *app-popup-menu* (incf ip) *Jamming-popup-menu* "辞書Jamming(&Q)")
		)
	      )
	  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#|
;;;これを呼び出す
(defun launch-application (app)
  (interactive "e% " :history0 'execute)
  (call-process app :no-std-handles t :show :show
		:exec-directory *launch-app-directory*))
|#

;;;Jammingのパス
;"C:/Program Files/Jamming/Jamming.exe"

;;;検索文字
;word
