########################################################################
#
# "tvi_text" : Tvi, .text ウィジェットの bind
#
# Copyright (c) 1998,1999  Motomu Nakashima
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option) 
# any later version.
#
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
# GNU General Public License for more details.
#
# このファイルはオリジナルの Tk4.2 distribution に含まれる text.tcl に
# 変更を加えて作成されました．
#
########################################################################

# Standard Motif bindings:

bind .text <1> {
    tkTextButton1 %W %x %y
    %W tag remove sel 0.0 end
}
bind .text <B1-Motion> {
    set tkPriv(x) %x
    set tkPriv(y) %y
    tkTextSelectTo %W %x %y
}
bind .text <Double-1> {
    set tkPriv(selectMode) word
    tkTextSelectTo %W %x %y
    catch {%W mark set insert sel.first}
}
bind .text <Triple-1> {
    set tkPriv(selectMode) line
    tkTextSelectTo %W %x %y
    catch {%W mark set insert sel.first}
}
bind .text <Shift-1> {
    tkTextResetAnchor %W @%x,%y
    set tkPriv(selectMode) char
    tkTextSelectTo %W %x %y
}
bind .text <Double-Shift-1>	{
    set tkPriv(selectMode) word
    tkTextSelectTo %W %x %y
}
bind .text <Triple-Shift-1>	{
    set tkPriv(selectMode) line
    tkTextSelectTo %W %x %y
}
bind .text <B1-Leave> {
    set tkPriv(x) %x
    set tkPriv(y) %y
    tkTextAutoScan %W
}
bind .text <B1-Enter> {
    tkCancelRepeat
}
bind .text <ButtonRelease-1> {
    tkCancelRepeat
}
bind .text <Control-1> {
    %W mark set insert @%x,%y
}
bind .text <ButtonRelease-2> {
    if {!$tkPriv(mouseMoved) || $tk_strictMotif} {
	# tkTextPaste %W %x %y
	tkTextPaste %W
    }
}
bind .text <Left> {
    belll
    # tkTextSetCursor %W insert-1c
}
bind .text <Right> {
    belll
    # tkTextSetCursor %W insert+1c
}
bind .text <Up> {
    belll
    # tkTextSetCursor %W [tkTextUpDownLine %W -1]
}
bind .text <Down> {
    belll
    # tkTextSetCursor %W [tkTextUpDownLine %W 1]
}
bind .text <Shift-Left> {
    tkTextKeySelect %W [%W index {insert - 1c}]
}
bind .text <Shift-Right> {
    tkTextKeySelect %W [%W index {insert + 1c}]
}
bind .text <Shift-Up> {
    tkTextKeySelect %W [tkTextUpDownLine %W -1]
}
bind .text <Shift-Down> {
    tkTextKeySelect %W [tkTextUpDownLine %W 1]
}
bind .text <Control-Left> {
    tkTextSetCursor %W [%W index {insert - 1c wordstart}]
}
bind .text <Control-Right> {
    tkTextSetCursor %W [%W index {insert wordend}]
}
bind .text <Control-Up> {
    tkTextSetCursor %W [tkTextPrevPara %W insert]
}
bind .text <Control-Down> {
    tkTextSetCursor %W [tkTextNextPara %W insert]
}
bind .text <Shift-Control-Left> {
    tkTextKeySelect %W [%W index {insert - 1c wordstart}]
}
bind .text <Shift-Control-Right> {
    tkTextKeySelect %W [%W index {insert wordend}]
}
bind .text <Shift-Control-Up> {
    tkTextKeySelect %W [tkTextPrevPara %W insert]
}
bind .text <Shift-Control-Down> {
    tkTextKeySelect %W [tkTextNextPara %W insert]
}
bind .text <Prior> {
    tkTextSetCursor %W [tkTextScrollPages %W -1]
}
bind .text <Shift-Prior> {
    tkTextKeySelect %W [tkTextScrollPages %W -1]
}
bind .text <Next> {
    tkTextSetCursor %W [tkTextScrollPages %W 1]
}
bind .text <Shift-Next> {
    tkTextKeySelect %W [tkTextScrollPages %W 1]
}
bind .text <Control-Prior> {
    %W xview scroll -1 page
}
bind .text <Control-Next> {
    %W xview scroll 1 page
}

bind .text <Home> {
    tkTextSetCursor %W {insert linestart}
}
bind .text <Shift-Home> {
    tkTextKeySelect %W {insert linestart}
}
bind .text <End> {
    tkTextSetCursor %W {insert lineend}
}
bind .text <Shift-End> {
    tkTextKeySelect %W {insert lineend}
}
bind .text <Control-Home> {
    tkTextSetCursor %W 1.0
}
bind .text <Control-Shift-Home> {
    tkTextKeySelect %W 1.0
}
bind .text <Control-End> {
    tkTextSetCursor %W {end - 1 char}
}
bind .text <Control-Shift-End> {
    tkTextKeySelect %W {end - 1 char}
}

bind .text <Tab> {
    #tkTextInsert %W \t
    #focus %W
    #break
}
bind .text <Shift-Tab> {
    # Needed only to keep <Tab> binding from triggering;  doesn't
    # have to actually do anything.
}
bind .text <Control-Tab> {
    focus [tk_focusNext %W]
}
bind .text <Control-Shift-Tab> {
    focus [tk_focusPrev %W]
}
bind .text <Control-i> {
    tkTextInsert %W \t
}
bind .text <Return> {

     # kinput2 のロケーションを追従させるため
     # ダミーの改行コードを入れる
     .text insert insert "\n"
     catch { kinput_send_spot .text } 
     .text mark set insert "insert - 1 c"
     .text delete insert "insert + 1 c"

    tkTextInsert %W \n
}
bind .text <Delete> {
    if {[%W tag nextrange sel 1.0 end] != ""} {
	%W delete sel.first sel.last
    } else {
	%W delete insert
	%W see insert
    }
}
bind .text <BackSpace> {
    global back_at_first_position

    # 行頭の場合はベルを鳴らして上の行には行かないようにする
    if {$back_at_first_position=="0"} { 
        if {[string compare [.text index insert ] \
            [.text index "insert linestart"  ] ] == 0 } {
            belll   
            break
        } 
    } 

    # 挿入時に isi より前に行かないようにする
    if {[.text index insert]==$isi} {
       belll   
       break
    }

    if {[%W tag nextrange sel 1.0 end] != ""} {
	%W delete sel.first sel.last
    } elseif [%W compare insert != 1.0] {
	%W delete insert-1c
	%W see insert
    }
}

bind .text <Control-space> {
    %W mark set anchor insert
}
bind .text <Select> {
    %W mark set anchor insert
}
bind .text <Control-Shift-space> {
    set tkPriv(selectMode) char
    tkTextKeyExtend %W insert
}
bind .text <Shift-Select> {
    set tkPriv(selectMode) char
    tkTextKeyExtend %W insert
}
bind .text <Control-slash> {
    %W tag add sel 1.0 end
}
bind .text <Control-backslash> {
    %W tag remove sel 1.0 end
}
bind .text <<Cut>> {
    tk_textCut %W
}
bind .text <<Copy>> {
    tk_textCopy %W
}
bind .text <<Paste>> {
    tk_textPaste %W
}
bind .text <<Clear>> {
    %W delete sel.first sel.last
}
bind .text <Insert> {
    catch {tkTextInsert %W [selection get -displayof %W]}
}
bind .text <KeyPress> {
    tkTextInsert %W %A
}

# Ignore all Alt, Meta, and Control keypresses unless explicitly bound.
# Otherwise, if a widget binding for one of these is defined, the
# <KeyPress> class binding will also fire and insert the character,
# which is wrong.  Ditto for <Escape>.

bind .text <Alt-KeyPress> {# nothing }
bind .text <Meta-KeyPress> {# nothing}
bind .text <Control-KeyPress> {# nothing}
# bind .text <Escape> {# nothing}
bind .text <KP_Enter> {# nothing}

# Additional emacs-like bindings:

bind .text <Control-a> {
    if !$tk_strictMotif {
	tkTextSetCursor %W {insert linestart}
    }
}
bind .text <Control-b> {
    if !$tk_strictMotif {
	tkTextSetCursor %W insert-1c
    }
}
bind .text <Control-d> {
    if !$tk_strictMotif {
	%W delete insert
    }
}
bind .text <Control-e> {
    if !$tk_strictMotif {
	tkTextSetCursor %W {insert lineend}
    }
}
bind .text <Control-f> {
    if !$tk_strictMotif {
	tkTextSetCursor %W insert+1c
    }
}
bind .text <Control-k> {
    if !$tk_strictMotif {
	if [%W compare insert == {insert lineend}] {
	    %W delete insert
	} else {
	    %W delete insert {insert lineend}
	}
    }
}
bind .text <Control-n> {
    if !$tk_strictMotif {
	tkTextSetCursor %W [tkTextUpDownLine %W 1]
    }
}
bind .text <Control-o> {
    if !$tk_strictMotif {
	%W insert insert \n
	%W mark set insert insert-1c
    }
}
bind .text <Control-p> {
    if !$tk_strictMotif {
	tkTextSetCursor %W [tkTextUpDownLine %W -1]
    }
}
bind .text <Control-t> {
    if !$tk_strictMotif {
	tkTextTranspose %W
    }
}
bind .text <Control-v> {
    if !$tk_strictMotif {
	tkTextScrollPages %W 1
    }
}
bind .text <Meta-b> {
    if !$tk_strictMotif {
	tkTextSetCursor %W {insert - 1c wordstart}
    }
}
bind .text <Meta-d> {
    if !$tk_strictMotif {
	%W delete insert {insert wordend}
    }
}
bind .text <Meta-f> {
    if !$tk_strictMotif {
	tkTextSetCursor %W {insert wordend}
    }
}
bind .text <Meta-less> {
    if !$tk_strictMotif {
	tkTextSetCursor %W 1.0
    }
}
bind .text <Meta-greater> {
    if !$tk_strictMotif {
	tkTextSetCursor %W end-1c
    }
}
bind .text <Meta-BackSpace> {
    if !$tk_strictMotif {
	%W delete {insert -1c wordstart} insert
    }
}
bind .text <Meta-Delete> {
    if !$tk_strictMotif {
	%W delete {insert -1c wordstart} insert
    }
}

# A few additional bindings of my own.

bind .text <Control-h> {
    if !$tk_strictMotif {
	if [%W compare insert != 1.0] {
	    %W delete insert-1c
	    %W see insert
	}
    }
}
bind .text <2> {
    if !$tk_strictMotif {
	%W scan mark %x %y
	set tkPriv(x) %x
	set tkPriv(y) %y
	set tkPriv(mouseMoved) 0
    }
}
bind .text <B2-Motion> {
    if !$tk_strictMotif {
	if {(%x != $tkPriv(x)) || (%y != $tkPriv(y))} {
	    set tkPriv(mouseMoved) 1
	}
	if $tkPriv(mouseMoved) {
	    %W scan dragto %x %y
	}
    }
}
set tkPriv(prevPos) {}

