;;; -*- Mode: Lisp; Package: EDITOR; Last updated: "2010/04/01 23:39:52" -*-
;;;
;;; This file is not part of xyzzy.
;;;
;;;   ruby-refe.l --- 傊側偪傚偙 ReFe 嶲徠梡儌乕僪
;;;
;;;     by HIE Masahiro <madoinux@gmail.com>
#|

仭奣梫

  HATTORI Masashi 偝傫偺 ruby-mode 偐傜丆ReFe2 傪嶲徠偟傑偡丅

    ruby-mode:
      http://www7a.biglobe.ne.jp/~hat/xyzzy/ruby-mode.html

    Ruby儕僼傽儗儞僗儅僯儏傾儖嶞怴寁夋:
      http://redmine.ruby-lang.org/wiki/rurema/ReleasedProducts


仭僀儞僗僩乕儖

  1. ruby-refe.l 傪 site-lisp 偵僐僺乕偡傞丅

  2. 昁梫偵墳偠丆僶僀僩僐儞僷僀儖偡傞丅(擟堄)

       M-x byte-compile-file

  3. Ruby儕僼傽儗儞僗儅僯儏傾儖僷僢働乕僕斉傪擖庤偟丆揥奐偡傞丅
     c:/rubyrefm 偵揥奐偡傞応崌偼丆偙傫側姶偠丅

       c:/rubyrefm/
        劌 bitclust/
        劆 劌 bin/
        劆 劆 劋 refe.rb
        劆 劋 lib/
        劌 db-1_8_7/
        劋 db-1_9_1/

     仸Ruby儕僼傽儗儞僗儅僯儏傾儖傪 c:/rubyrefm/ 埲奜偵僀儞僗僩乕儖
       偡傞応崌偼丆愝掕椺偵廬偄丆*ruby-refe-install-path* 傪愝掕偡傞
       昁梫偑偁傝傑偡丅

  4. .xyzzy 偐 siteinit.l 偵埲壓偺婰弎傪捛壛偡傞丅

       (require "ruby-refe")


仭巊偄曽

  M-x ruby-refe 偟偰丆専嶕偡傞僔儞儃儖偲僆僾僔儑儞傪巜掕偟傑偡丅
  僆僾僔儑儞偵 --help 傪巜掕偡傞偲丆僆僾僔儑儞堦棗偑昞帵偝傟傑偡丅

  幚峴椺偼丆<http://i.loveruby.net/ja/prog/refe.html> 偁偨傝傪嶲徠
  偟偰偔偩偝偄丅(ReFe2 偵側偭偰僆僾僔儑儞偑曄傢偭偰偄傑偡)

  [ruby-refe-mode]

    RET   : 僇乕僜儖埵抲偺僔儞儃儖傪堷偔
    F1    : 僇乕僜儖埵抲偺僔儞儃儖傪堷偔 (堦棗昞帵)
    r     : refe 幚峴
    l     : refe -l 幚峴
    o     : 師偺僂傿儞僪僂偵堏摦
    q     : 廔椆


仭愝掕椺

  F1 僉乕偱丆僇乕僜儖埵抲偺扨岅傪 ReFe 偐傜堷偔丅

    (define-key *ruby-mode-map* #\F1 'ruby-refe-line-point)

  ReFe 僐儅儞僪偺僄僀儕傾僗丅M-x refe 偱幚峴偡傞傛偆偵丅

    (setf (symbol-function 'refe) #'ruby-refe)

  ReFe 偺僀儞僗僩乕儖僷僗傪曄峏丅(婛掕抣偼 "c:/rubyrefm")

    (setq *ruby-refe-install-path* "d:/opt/rubyrefm")

  Ruby ReFe 偺僨乕僞儀乕僗柤傪巜掕丅(婛掕抣偼 "db-1_8_7")

    (setq *ruby-refe-dbname* "db-1_9_1")


仭峏怴棜楌

  [Version 2.00] 2010-04-01 (栘)
  丒ReFe2 偵懳墳偟偨(偼偢)丅
  丒ReFe1 旕懳墳偵偟偨丅
  丒僆僾僔儑儞偑巜掕偱偒傞傛偆偵偟偨丅
  丒偦偺懠偄傠偄傠丅

  [Version 1.00] 2008-01-06 (擔)
  丒弶斉嶌惉丅


仭儔僀僙儞僗

  ruby-refe.l 偼MIT儔僀僙儞僗偵婎偯偄偰棙梡壜擻偱偡丅
  <http://www.opensource.org/licenses/mit-license.php>

Copyright (c) 2007-2010 HIE Masahiro

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

|#

(provide "ruby-refe")

(require "ruby")

(in-package "editor")

(export '(ruby-refe
	  ruby-refe-line
	  ruby-refe-point
	  ruby-refe-line-point
	  *ruby-refe-install-path*
	  *ruby-refe-dbname*
	  *ruby-refe-buffer1*
	  *ruby-refe-buffer2*
	  ))

;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;
;;;  仭 曄悢愝掕 - 揔摉偵僇僗僞儅僀僘偡傞偁偨傝
;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;

(defvar *ruby-refe-install-path* "c:/rubyrefm"
  "ruby-refe: Ruby ReFe 僀儞僗僩乕儖僼僅儖僟")

(defvar *ruby-refe-dbname* "db-1_8_7"
  "ruby-refe: Ruby ReFe 僨乕僞儀乕僗柤")

(defvar *ruby-refe-option-default* "-l"
  "ruby-refe: ruby-refe 幚峴帪偵巜掕偡傞僆僾僔儑儞偺婛掕抣")

(defvar *ruby-refe-option-force* "-e sjis"
  "ruby-refe: 嫮惂揑偵巜掕偡傞僆僾僔儑儞")

(defvar *ruby-refe-color-title* '(:keyword 0)
  "ruby-refe: 僞僀僩儖(?)偺怓")

(defvar *ruby-refe-color-head* '(:keyword 2)
  "ruby-refe: 僞僀僩儖(?)埲奜偺僿僢僟(??)偺怓")


;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;
;;;  仭 曄悢愝掕 - 偁傑傝偝傢傜側偔偰椙偄傕偺
;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;

(defvar *ruby-refe-command* "refe.rb"
  "ruby-refe: refe 僐儅儞僪")

(defvar *ruby-refe-bitclust-lib-path* nil
  "ruby-refe: bitclust/lib 僼僅儖僟")

(defvar *ruby-refe-bitclust-bin-path* nil
  "ruby-refe: bitclust/bin 僼僅儖僟")

(defvar *ruby-refe-db-path* nil
  "ruby-refe: Ruby ReFe 僨乕僞儀乕僗僼僅儖僟")

(defvar *ruby-refe-buffer1* " *ReFe1*"
  "ruby-refe: ReFe 僶僢僼傽1")

(defvar *ruby-refe-buffer2* " *ReFe2*"
  "ruby-refe: ReFe 僶僢僼傽2")

(defvar *ruby-refe-winconf* nil
  "ruby-refe: 幚峴慜偺僂傿儞僪僂偺忬懺")

(defvar *ruby-refe-mode-hook* nil
  "ruby-refe: refe 幚峴帪張棟")

(defvar *ruby-refe-mode-map* nil
  "ruby-refe: ReFe僶僢僼傽梡僉乕儅僢僾")

(defvar *ruby-refe-mode-syntax-table* nil
  "ruby-refe: ReFe僶僢僼傽梡僔儞僞僢僋僗僥乕僽儖")

(defvar *ruby-refe-regexp-keyword-list*
  (compile-regexp-keyword-list
   `(
     ("^[A-Za-z0-9_#:.*=!?-]+$" t ((0 . ,*ruby-refe-color-title*)))
     ("^--- .*$"                t ((0 . ,*ruby-refe-color-head*)))
     ("^: .*$"                  t ((0 . ,*ruby-refe-color-head*)))
     ("^require.*$"             t ((0 . ,*ruby-refe-color-head*)))
     ))
  "ruby-refe: 惓婯昞尰偵傛傞怓晅偗")


;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;
;;;  仭 懳榖揑側娭悢
;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;

(defun ruby-refe-point (&optional option)
  "ruby-refe: 僇乕僜儖埵抲偺僔儞儃儖傪堷偔"
  (interactive)
  (let ((pattern (ruby-refe-keyword)))
    (when (string= pattern "")
      (setq pattern nil))
    (ruby-refe pattern option)))

(defun ruby-refe-line-point ()
  "ruby-refe: 僇乕僜儖埵抲偺僔儞儃儖傪 -l 僆僾僔儑儞晅偒偱堷偔"
  (interactive)
  (ruby-refe-point "-l"))

(defun ruby-refe (&optional pattern option)
  "ruby-refe: refe 幚峴"
  (interactive "sReFe: \nsOption: "
    :default0 (ruby-refe-keyword)
    :default1 *ruby-refe-option-default*)
  (let (buffer arg)
    (setq buffer (if (string= (buffer-name (selected-buffer)) *ruby-refe-buffer1*)
		     *ruby-refe-buffer2*
		   *ruby-refe-buffer1*))
    (when (string-match "\\(.+\\)\\.#\\(.+\\)" pattern)
      (setq pattern (string-replace-match pattern "\\1#\\2")))
    (let ((cbuf (selected-buffer))
	  (rbuf (find-buffer buffer)))
      (when rbuf
	(set-buffer rbuf)
	(setq buffer-read-only nil)
	(set-buffer cbuf)))
    (unless *ruby-refe-winconf*
      (setq *ruby-refe-winconf* (current-window-configuration)))
    (ruby-refe-set-pathname)
    (setq arg (concat *ruby-refe-option-force* " " (or option "") " " pattern))
    (execute-shell-command (format nil "@~A -I ~A ~A/~A -d file://~A ~A"
				   *ruby-prog*
				   *ruby-refe-bitclust-lib-path*
				   *ruby-refe-bitclust-bin-path*
				   *ruby-refe-command*
				   *ruby-refe-db-path*
				   arg)
			   :OUTPUT buffer
			   :DIRECTORY *ruby-refe-bitclust-bin-path*)
    (ruby-refe-mode arg)))

(defun ruby-refe-line ()
  "ruby-refe: -l 僆僾僔儑儞晅偒偱幚峴"
  (interactive "sReFe: " :default0 (ruby-refe-keyword))
  (ruby-refe nil "-l"))

(defun ruby-refe-quit ()
  "ruby-refe: 廔椆"
  (interactive)
  (when *ruby-refe-winconf*
    (ignore-errors
     (set-window-configuration *ruby-refe-winconf*))
    (setq *ruby-refe-winconf* nil)))


;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;
;;;  仭 ruby-refe-mode
;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;

(unless *ruby-refe-mode-map*
  (setq *ruby-refe-mode-map* (make-sparse-keymap))
  (define-key *ruby-refe-mode-map* '#\F1  'ruby-refe-line-point)
  (define-key *ruby-refe-mode-map* '#\RET 'ruby-refe-point)
  (define-key *ruby-refe-mode-map* '#\r   'ruby-refe)
  (define-key *ruby-refe-mode-map* '#\l   'ruby-refe-line)
  (define-key *ruby-refe-mode-map* '#\o   'other-window)
  (define-key *ruby-refe-mode-map* '#\q   'ruby-refe-quit)
  )

(unless *ruby-refe-mode-syntax-table*
  (setq *ruby-refe-mode-syntax-table* (make-syntax-table))
  (set-syntax-symbol *ruby-refe-mode-syntax-table* #\_)
  (set-syntax-symbol *ruby-refe-mode-syntax-table* #\?)
  (set-syntax-symbol *ruby-refe-mode-syntax-table* #\!)
  (set-syntax-symbol *ruby-refe-mode-syntax-table* #\=)
  (set-syntax-symbol *ruby-refe-mode-syntax-table* #\-)
  (set-syntax-symbol *ruby-refe-mode-syntax-table* #\*)
  (set-syntax-symbol *ruby-refe-mode-syntax-table* #\.)
  (set-syntax-symbol *ruby-refe-mode-syntax-table* #\:)
  (set-syntax-symbol *ruby-refe-mode-syntax-table* #\#)
  )

(defun ruby-refe-mode (arg)
  "ruby-refe: 儕僼傽儗儞僗昞帵梡儌乕僪"
  (interactive)
  (kill-all-local-variables)
  (setq buffer-mode 'ruby-refe)
  (setq mode-name "ReFe")
  (use-keymap *ruby-refe-mode-map*)
  (use-syntax-table *ruby-refe-mode-syntax-table*)
  (make-local-variable 'regexp-keyword-list)
  (setq regexp-keyword-list *ruby-refe-regexp-keyword-list*)
  (make-local-variable 'mode-line-format)
  (setq mode-line-format (format nil "--%*- %b (%M) [%k:%l] %P dbname: ~A  command: \"~A ~A\""
				 *ruby-refe-dbname*
				 *ruby-refe-command*
				 arg))
  (setq need-not-save t)
  (setq kept-undo-information nil)
  (setq auto-save nil)
  (set-buffer-modified-p nil)
  (setq buffer-read-only t)
  (let ((buf (selected-buffer)))
    (set-local-window-flags buf (+ *window-flag-newline*
				   *window-flag-tab*
				   *window-flag-full-width-space*
				   *window-flag-eof*
				   *window-flag-line-number*
				   *window-flag-half-width-space*
				   *window-flag-scrolling*)
			    nil)
    (set-local-window-flags buf (+ (if (boundp '*window-flag-fold-mark*)
				       *window-flag-fold-mark*
				     *window-flag-fold-line*))
			    t))
  (run-hooks '*ruby-refe-mode-hook*))


;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;
;;;  仭 曗彆娭悢
;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;

(defun ruby-refe-set-pathname()
  "ruby-refe: Ruby ReFe 僷僗愝掕"
  (setf *ruby-refe-bitclust-lib-path*
	(merge-pathnames "bitclust/lib" *ruby-refe-install-path*)
	*ruby-refe-bitclust-bin-path*
	(merge-pathnames "bitclust/bin" *ruby-refe-install-path*)
	*ruby-refe-db-path*
	(merge-pathnames *ruby-refe-dbname* *ruby-refe-install-path*)))

(defun ruby-refe-keyword ()
  "ruby-refe: 僇乕僜儖埵抲偺扨岅傑偨偼僙儗僋僔儑儞偺暥帤楍傪曉偡"
  (or (selection-start-end (start end)
	(buffer-substring start end))
      (if (string-match "\\_s+" (format nil "~C" (char-after (point))))
	  (save-excursion
	    (buffer-substring (progn
				(skip-syntax-spec-forward "w_")
				(point))
			      (progn
				(skip-syntax-spec-backward "w_")
				(point))))
	"")))

;;; ruby-refe.l ends here.
