########################################################################
#
# "main_bind" : Tvi, キーバインド設定
#
# 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.
#
########################################################################

bindtags .text { .text . all }
bind .text <Configure> { reconfig_lines ; cursor_put } ;# リサイズした時のため

# 日本語入力起動キー
#catch { bind .text $kanji_start_key1 {kinput_start %W over} }
#catch { bind .text $kanji_start_key2 {kinput_start %W over} }
#catch { bind .text $kanji_start_key3 {kinput_start %W over} }
#
#catch { bind  Text $kanji_start_key1 {kinput_start %W over} }
#catch { bind  Text $kanji_start_key2 {kinput_start %W over} }
#catch { bind  Text $kanji_start_key3 {kinput_start %W over} }
#
#catch { bind  Entry $kanji_start_key1 {kinput_start %W over} }
#catch { bind  Entry $kanji_start_key2 {kinput_start %W over} }
#catch { bind  Entry $kanji_start_key3 {kinput_start %W over} }
#

set num [llength $kanji_start_key]
for {set i 1} {$i <= $num} {incr i} {
    set key [lindex $kanji_start_key [expr $i -1]]
    bind  .text $key {kinput_start %W over}
    bind  Text  $key {kinput_start %W over}
    bind  Entry $key {kinput_start %W over}
}

# セレクション関連
bind Text <ButtonRelease-2> {
    if {!$tkPriv(mouseMoved) || $tk_strictMotif} {
        tkTextPaste %W
    }
}

# カーソル移動
bind .virtual <h> { insert_left  ; cursor_put }
bind .virtual <l> { insert_right ; cursor_put }
bind .virtual <space> { insert_right ; cursor_put }
bind .virtual <BackSpace> { insert_left ; cursor_put }
bind .virtual <j> { insert_down  ; cursor_put }
bind .virtual <k> { insert_up    ; cursor_put }
bind .virtual <Return> { return_on_virtual }

bind .virtual <Left>  { insert_left  ; cursor_put }
bind .virtual <Right> { insert_right ; cursor_put }
bind .virtual <Down>  { insert_down  ; cursor_put }
bind .virtual <Up>    { insert_up    ; cursor_put }

bind .virtual <Control-f> { forward_one_page }
bind .virtual <Control-b> { backward_one_page }
bind .virtual <Control-d> { forward_half_page }
bind .virtual <Control-u> { backward_half_page }

bind .virtual <0> { .text mark set insert "insert linestart" ; \
          set insert_pos_x [ lindex [ .text bbox insert ] 0 ] ; \
                       cursor_put }
bind .virtual <dollar> { goto_lineend_by_dollar }

# 編集関係
bind .virtual <i> { i_on_virtual }
bind .virtual <I> { large_i_on_virtual }
bind .virtual <a> { a_on_virtual }
bind .virtual <A> { large_a_on_virtual }
bind .virtual <o> { o_on_virtual }
bind .virtual <O> { large_o_on_virtual }
bind .virtual <s> { s_on_virtual }
bind .virtual <S> { large_s_on_virtual }
bind .virtual <C> { large_c_on_virtual }
bind .virtual <R> { replace_many }
bind .virtual <r> { replace_one_char }
bind .virtual <x> { delete_by_x }
bind .virtual <X> { focus .garbage ; \
                    update ; delete_by_large_x ; \
                    cursor_put; focus .virtual }
bind .virtual <D> { delete_by_large_d }

bind .navi.edit <x> { delete_many_by_x }
bind .navi.edit <X> { delete_many_by_large_x }

# bind .virtual <KeyRelease-x> { release_x }
# bind .garbage <KeyRelease-x> { release_x }


# bind .virtual <O> { cursor_clear ; insert_by_large_o ; clear_mem_y ; \
#                         clear_edit_entry ; \
#                        .navi.edit insert 0 { [Insert mode]} ; \
#                        .navi.edit configure -state disabled }

bind .virtual <u> { undo_main }
bind .virtual <d>          { focus .virtual_delete ; \
                             clear_edit_entry ; \
                            .navi.edit  insert 0 "1d" }
bind .virtual <y>          { focus .virtual_yank ; \
                  set backup_edit_entry [.navi.edit get] ; \
                             clear_edit_entry ; \
                             .navi.edit  insert 0 "1y" }
bind .virtual <KeyPress-1> { \
                  set backup_edit_entry [.navi.edit get] ; \
                  focus .navi.edit  ; clear_edit_entry ; \
                 .navi.edit  insert 0 "1" }
bind .virtual <KeyPress-2> { \
                  set backup_edit_entry [.navi.edit get] ; \
                  focus .navi.edit  ; clear_edit_entry ; \
                 .navi.edit  insert 0 "2" }
bind .virtual <KeyPress-3> { \
                  set backup_edit_entry [.navi.edit get] ; \
                  focus .navi.edit  ; clear_edit_entry ; \
                 .navi.edit  insert 0 "3" }
bind .virtual <KeyPress-4> { \
                  set backup_edit_entry [.navi.edit get] ; \
                  focus .navi.edit  ; clear_edit_entry ; \
                 .navi.edit  insert 0 "4" }
bind .virtual <KeyPress-5> { \
                  set backup_edit_entry [.navi.edit get] ; \
                  focus .navi.edit  ; clear_edit_entry ; \
                 .navi.edit  insert 0 "5" }
bind .virtual <KeyPress-6> { \
                  set backup_edit_entry [.navi.edit get] ; \
                  focus .navi.edit  ; clear_edit_entry ; \
                 .navi.edit  insert 0 "6" }
bind .virtual <KeyPress-7> { \
                  set backup_edit_entry [.navi.edit get] ; \
                  focus .navi.edit  ; clear_edit_entry ; \
                 .navi.edit  insert 0 "7" }
bind .virtual <KeyPress-8> { \
                  set backup_edit_entry [.navi.edit get] ; \
                  focus .navi.edit  ; clear_edit_entry ; \
                 .navi.edit  insert 0 "8" }
bind .virtual <KeyPress-9> { \
                  set backup_edit_entry [.navi.edit get] ; \
                  focus .navi.edit  ; clear_edit_entry ; \
                 .navi.edit  insert 0 "9" }

bind .virtual <p> { put_by_p }
bind .virtual <P> { put_by_large_p }
bind .virtual <J> { connect_by_large_j }
bind .virtual <period> { redoing }

# Escape キーの種々の動作
bind .text    <Escape> { escape_on_text }

bind .virtual              <Escape> { \
                            set line_yank 0 ; \
                                     belll ; \
                        .navi.edit configure -state disabled }
bind .navi.edit            <Escape> { clear_edit_entry ; \
                            set navi_edit $backup_edit_entry ; \
                        .navi.edit configure -state disabled ; \
                                      focus .virtual ; belll }
bind .virtual_delete       <Escape> { clear_edit_entry ; \
                         set navi_edit $backup_edit_entry ; \
                        .navi.edit configure -state disabled ; \
                                      focus .virtual ; belll }
bind .virtual_yank         <Escape> { clear_edit_entry ; \
                           set navi_edit $backup_edit_entry ; \
                          .navi.edit configure -state disabled ; \
                                      focus .virtual ; belll }
bind .navi.main            <Escape> { \
                            .navi.main delete 0 end ; \
                            .navi.main insert 0 $navi_main_backup ; \
                             focus .virtual ; cursor_put }

# 編集モード
bind .navi.edit  <d> { focus .virtual_delete  }
bind .navi.edit  <y> { focus .virtual_yank  }
bind .virtual_delete  <d> { delete_by_dd  ; \
                               cursor_put \
                             ; focus .virtual \
                             ; clear_mem_y ; \
                           .text tag remove cut_and_paste 1.0 end ; \
                           .navi.edit configure -state disabled }

bind .virtual_yank    <y> { yank_by_yy ; \
                            set navi_edit $backup_edit_entry ; \
                           .navi.edit configure -state disabled ; \
                           .text tag remove cut_and_paste 1.0 end ; \
                            focus .virtual } 

bind .virtual_delete <Any-KeyPress> { ; \
                       cursor_put ; \
       .text tag configure cur -background $fore_color -foreground $back_color ; \
       .cursor configure  -bg $fore_color ; \
                       set yank_line_start "none" ; \
                       set yank_char_start "none" ; \
                       set yank_char_or_line 0 ; \
                      .text tag remove cut_and_paste 1.0 end ; \
                      .navi.edit configure -state normal ; \
                       set navi_edit $backup_edit_entry ; \
                      .navi.edit configure -state disabled ; \
                       clear_mem_y ; \
                       focus .virtual ; belll }

bind .virtual_yank <Any-KeyPress> { ; \
                       cursor_put ; \
       .text tag configure cur -background $fore_color -foreground $back_color ; \
       .cursor configure  -bg $fore_color ; \
                       set yank_line_start "none" ; \
                       set yank_char_start "none" ; \
                       set yank_char_or_line 0 ; \
                      .text tag remove cut_and_paste 1.0 end ; \
                      .navi.edit configure -state normal ; \
                       set navi_edit $backup_edit_entry ; \
                      .navi.edit configure -state disabled ; \
                       clear_mem_y ; \
                       focus .virtual ; belll }

bind .navi.edit  <G> { jump_by_large_g } 
bind .virtual    <G> { jump_by_large_g_from_virtual } 

# 検索
bind .virtual <slash>    { search_forward }
bind .virtual <question> { search_backward }
bind .replace.search <Escape> { set navi_search $backup_navi_search ; \
                                navi_search_cond_back ; \
                                focus .virtual ; cursor_put }
bind .replace.search <Return>  { search_return }
bind .replace.search <BackSpace>  { ; \
                 if {[string compare $navi_search ""] == 0 } { \
                        set navi_search $backup_navi_search ; \
                        navi_search_cond_back ; \
                        focus .virtual ; cursor_put } else { \
                  .replace.search delete insert } }

bind .virtual <n>  { search_by_small_n }
bind .virtual <N>  { search_by_large_n }

# その他

bind .virtual <Alt-c> { compiling }
bind .virtual <Alt-x> { xdvi_popup }
bind .virtual <Alt-m> { equation_panel_onoff }
#bind .virtual <Alt-o> { set new_or_insert 0 ; openfile }
#bind .virtual <Alt-i> { set new_or_insert 1 ; openfile }
bind .virtual <Alt-s> { savefile }
bind .virtual <Alt-t> { terminal_popup }
bind .virtual <Alt-b> { bookmark_popup }
bind .virtual <Alt-l> { after 0 { uncolorize ; set colored 1 ; colorize 1.0 end } }
bind .virtual <Alt-i> { after 0 { spellcheck } }
bind .virtual <Alt-n> { set colored 0 ; uncolorize }
bind .virtual <Alt-p> { print_window }
bind .virtual <Alt-r> { replace_popup }
bind .virtual <Alt-h> { help_popup }

bind .virtual <colon> { cursor_clear ; \
                     set navi_main_backup [.navi.main get] ; \
                     .navi.main configure -state normal ; \
                     .navi.main delete  0 end ; \
                     .navi.main insert 0 ":" ; \
                         focus .navi.main }
bind .navi.main <Return> { do_navi_main }
bind .navi.main <space> { space_navi_main }

bind .navi.main <BackSpace> { ; \
                 if {[string compare $navi_main ":"] == 0 } { \
                     .navi.main insert 0 $navi_main_backup ; \
                     .replace.search delete insert ; \
                     focus .virtual ; cursor_put ; \
                 } else { .replace.search delete insert } }

bind .text <parenright>   { checkbrace "("  ")"  ; tkTextInsert %W %A}
bind .text <braceright>   { checkbrace "\{" "\}" ; tkTextInsert %W %A}
bind .text <bracketright> { checkbrace  {[} {]}  ; tkTextInsert %W %A}

# Tab によるコマンド補完
bind .text <Tab> { tab_in_text }

# マークによるヤンク用
bind .virtual <Control-space> { ; \
                        set yank_line_start "none" ; \
                        set yank_char_start "none" ; \
                        set yank_char_or_line "0" ; \
                        set backup_edit_entry [.navi.edit get] ; \
                        focus .line_yank ; start_line_yank }
bind .virtual <v> { ; \
                        set yank_line_start "none" ; \
                        set yank_char_start "none" ; \
                        set yank_char_or_line "0" ; \
                        set backup_edit_entry [.navi.edit get] ; \
                        focus .line_yank ; start_line_yank }
bind .line_yank <d> { .text mark set insert cut_and_paste.first ; \
                       cursor_put ; \
       .text tag configure cur -background $fore_color -foreground $back_color ; \
       .cursor configure  -bg $fore_color ; \
       .navi.edit configure -state normal ; \
       .navi.edit insert end "d" ; \
                       set yank_line_start "none" ; \
                       set yank_char_start "none" ; \
                       set yank_char_or_line "0" ; \
                        focus .virtual_delete }

bind .line_yank <y> { .text mark set insert cut_and_paste.first ; \
                       cursor_put ; \
       .text tag configure cur -background $fore_color -foreground $back_color ; \
       .cursor configure  -bg $fore_color ; \
       .navi.edit configure -state normal ; \
       .navi.edit insert end "y" ; \
                       set yank_line_start "none" ; \
                       set yank_char_start "none" ; \
                       set yank_char_or_line "0" ; \
                       clear_mem_y ; \
                        focus .virtual_yank }

bind .line_yank <x> { delete_by_x_in_line_yank } 

bind .line_yank <Escape> { ; \
                       cursor_put ; \
       .text tag configure cur -background $fore_color -foreground $back_color ; \
       .cursor configure  -bg $fore_color ; \
                       set yank_line_start "none" ; \
                       set yank_char_start "none" ; \
                       set yank_char_or_line 0 ; \
                      .text tag remove cut_and_paste 1.0 end ; \
                      .navi.edit configure -state normal ; \
                       set navi_edit $backup_edit_entry ; \
                      .navi.edit configure -state disabled ; \
                       focus .virtual }

# マークを付ける際のカーソル移動
#
# (1) 上下
bind .line_yank <j> { insert_down  ; cursor_put ; \
                        set yank_char_or_line 1 ; \
                        trace_for_yank }
bind .line_yank <k> { insert_up    ; cursor_put ; \
                        set yank_char_or_line 1 ; \
                        trace_for_yank }

bind .line_yank <Return> { return_on_virtual ; \
                        set yank_char_or_line 1 ; \
                         trace_for_yank }

bind .line_yank <Down> { insert_down ; \
                         cursor_put ; \
                         set yank_char_or_line 1 ; \
                         trace_for_yank }

bind .line_yank <Up> { insert_up  ; \
                         cursor_put ; \
                        set yank_char_or_line 1 ; \
                         trace_for_yank }

bind .line_yank <Control-f> { forward_one_page ; \
                        set yank_char_or_line 1 ; \
                         trace_for_yank }

bind .line_yank <Control-b> { backward_one_page ; \
                        set yank_char_or_line 1 ; \
                         trace_for_yank }

# (2) 左右
bind .line_yank <h> { insert_left  ; cursor_put ; \
                         trace_for_yank }
bind .line_yank <BackSpace> { insert_left  ; cursor_put ; \
                         trace_for_yank }
bind .line_yank <l> { insert_right ; cursor_put ; \
                         trace_for_yank }
bind .line_yank <space> { insert_right ; cursor_put ; \
                         trace_for_yank }

bind .line_yank <Left> { insert_left  ; cursor_put ; \
                         trace_for_yank }
bind .line_yank <Right> { insert_right ; cursor_put ; \
                         trace_for_yank }


bind .line_yank <0> { .text mark set insert "insert linestart" ; \
          set insert_pos_x [ lindex [ .text bbox insert ] 0 ] ; \
                       cursor_put ; \
                         trace_for_yank }
bind .line_yank <dollar> { goto_lineend_by_dollar ; \
                         trace_for_yank }

# (3) スクロールバー
bind .yscroll   <Any-B1-Motion> { ; \
                        if {$yank_line_start !="none"} {
                        set yank_char_or_line 1 ; \
                        trace_for_yank }}

bind .yscroll   <Any-ButtonRelease> { ; \
                        if {$yank_line_start !="none"} {
                        set yank_char_or_line 1 ; \
                        trace_for_yank }}

bind .yscroll   <Any-ButtonPress> { ; \
                        if {$yank_line_start !="none"} {
                        set yank_char_or_line 1 ; \
                        trace_for_yank }}

# どんなイベントが起こっても行番号を合わせる
# after_id は "x" による削除のため
bind all <Any-KeyPress>    { auto_scroll_linenumber ; \
                           auto_show_column_and_line ; \
                           if {"%K"!="x"} { set after_id 0 } }
bind all <Any-ButtonPress> { auto_scroll_linenumber ; \
                           auto_show_column_and_line }
bind all <Any-Motion>      { auto_scroll_linenumber ; \
                           auto_show_column_and_line }

#bind .text <FocusIn>  { .text insert 1.0 "Focus!!"}

# 置換ウィンドウ
bind .replace_window.text <1> { focus .replace_window.text }

bind .virtual <Alt-d> { initialize_digest }
