if([ $(uname|chomp) = Darwin ]) {
    kanjicode_file_name -m
} else {
    kanjicode_file_name -w
}

def cursor {
    file_name $(cursor_num)
}

def toggle {
    if([ $ARGV[0] = 0 ]) {
        print 1
    } else {
        print 0
    }
}

global VIEW_LIST_MODE=0
global VIEW_LIST_MODE_PERMISSION=0
global VIEW_LIST_MODE_NLINK=0
global VIEW_LIST_MODE_OWNER=0
global VIEW_LIST_MODE_GROUP=0
global VIEW_LIST_MODE_SIZE=0
global VIEW_LIST_MODE_MTIME=0
global VIEW_LIST_MODE_OPTION=0

def toggle_list_mode {
    if ([ $VIEW_LIST_MODE = 0 ]) {
        global VIEW_LIST_MODE=1

        global VIEW_LIST_MODE_PERMISSION=$VIEW_PERMISSION
        global VIEW_LIST_MODE_NLINK=$VIEW_NLINK
        global VIEW_LIST_MODE_OWNER=$VIEW_OWNER
        global VIEW_LIST_MODE_GROUP=$VIEW_GROUP
        global VIEW_LIST_MODE_SIZE=$VIEW_SIZE
        global VIEW_LIST_MODE_MTIME=$VIEW_MTIME
        global VIEW_LIST_MODE_OPTION=$VIEW_OPTION

        export VIEW_PERMISSION=1
        export VIEW_NLINK=1
        export VIEW_OWNER=1
        export VIEW_GROUP=1
        export VIEW_SIZE=1
        export VIEW_MTIME=1
        export VIEW_OPTION=1pain
    } else {
        global VIEW_LIST_MODE=0

        export VIEW_PERMISSION=$VIEW_LIST_MODE_PERMISSION
        export VIEW_NLINK=$VIEW_LIST_MODE_NLINK
        export VIEW_OWNER=$VIEW_LIST_MODE_OWNER
        export VIEW_GROUP=$VIEW_LIST_MODE_GROUP
        export VIEW_SIZE=$VIEW_LIST_MODE_SIZE
        export VIEW_MTIME=$VIEW_LIST_MODE_MTIME
        export VIEW_OPTION=$VIEW_LIST_MODE_OPTION
    }
}

global VIEW_NAMEONLY_MODE=0
global VIEW_NAMEONLY_MODE_PERMISSION=0
global VIEW_NAMEONLY_MODE_NLINK=0
global VIEW_NAMEONLY_MODE_OWNER=0
global VIEW_NAMEONLY_MODE_GROUP=0
global VIEW_NAMEONLY_MODE_SIZE=0
global VIEW_NAMEONLY_MODE_MTIME=0
global VIEW_NAMEONLY_MODE_OPTION=0

def toggle_nameonly_mode {
    if([ $VIEW_NAMEONLY_MODE = 0 ]) {
        global VIEW_NAMEONLY_MODE=1

        global VIEW_NAMEONLY_MODE_PERMISSION=$VIEW_PERMISSION
        global VIEW_NAMEONLY_MODE_NLINK=$VIEW_NLINK
        global VIEW_NAMEONLY_MODE_OWNER=$VIEW_OWNER
        global VIEW_NAMEONLY_MODE_GROUP=$VIEW_GROUP
        global VIEW_NAMEONLY_MODE_SIZE=$VIEW_SIZE
        global VIEW_NAMEONLY_MODE_MTIME=$VIEW_MTIME
        global VIEW_NAMEONLY_MODE_OPTION=$VIEW_OPTION

        export VIEW_PERMISSION=0
        export VIEW_NLINK=0
        export VIEW_OWNER=0
        export VIEW_GROUP=0
        export VIEW_SIZE=0
        export VIEW_MTIME=0
        export VIEW_OPTION=1pain
    } else {
        global VIEW_NAMEONLY_MODE=0

        export VIEW_PERMISSION=$VIEW_NAMEONLY_MODE_PERMISSION
        export VIEW_NLINK=$VIEW_NAMEONLY_MODE_NLINK
        export VIEW_OWNER=$VIEW_NAMEONLY_MODE_OWNER
        export VIEW_GROUP=$VIEW_NAMEONLY_MODE_GROUP
        export VIEW_SIZE=$VIEW_NAMEONLY_MODE_SIZE
        export VIEW_MTIME=$VIEW_NAMEONLY_MODE_MTIME
        export VIEW_OPTION=$VIEW_NAMEONLY_MODE_OPTION
    }
}

global COLOR=0

def toggle_color {
    if([ $COLOR = 0 ]) {
        global COLOR=1

        export COLOR_DIR=$ma_cyan
        export COLOR_EXE=$ma_red
        export COLOR_LINK=$ma_magenta
        export COLOR_MARK=$ma_yellow
    } else {
        global COLOR=0

        export COLOR_DIR=$ma_bold
        export COLOR_EXE=$ma_bold
        export COLOR_LINK=0
        export COLOR_MARK=$ma_bold
    }
}

defmenu view '2dir (a)ll' $key_a 'export VIEW_OPTION=2pain' '1dir(1)' $key_1 'export VIEW_OPTION=1pain' '1dir(2)' $key_2 'export VIEW_OPTION=1pain2' '1dir(3)' $key_3 'export VIEW_OPTION=1pain3' '1dir(5)' $key_5 'export VIEW_OPTION=1pain5' 'toggle (s)ize' $key_s 'export VIEW_SIZE=$(toggle $VIEW_SIZE)' 'toggle m(t)ime' $key_t 'export VIEW_MTIME=$(toggle $VIEW_MTIME)' 'toggle (u)ser' $key_u 'export VIEW_OWNER=$(toggle $VIEW_OWNER)' 'toggle (g)roup' $key_g 'export VIEW_GROUP=$(toggle $VIEW_GROUP)' 'toggle nl(i)nk' $key_i 'export VIEW_NLINK=$(toggle $VIEW_NLINK)' 'toggle (p)ermission' $key_p 'export VIEW_PERMISSION=$(toggle $VIEW_PERMISSION)' 'toggle fname (d)vide extension' $key_d 'export VIEW_DIVIDE_EXTENSION=$(toggle $VIEW_DIVIDE_EXTENSION)' 'toggle fname (f)ocus back' $key_f 'export VIEW_FOCUSBACK=$(toggle $VIEW_FOCUSBACK)' 'toggle (l)ist mode' $key_l 'toggle_list_mode' 'toggle (n)ameonly mode' $key_n 'toggle_nameonly_mode' 'toggle (c)olor' $key_c 'toggle_color' 'toggle (P)age view way' $key_P 'export VIEW_PAGE=$(toggle $VIEW_PAGE)' 'fil(e) size view way Normal' $key_e 'export VIEW_FILE_SIZE=Normal' 'fil(E) size view way Human' $key_E 'export VIEW_FILE_SIZE=Human' 'file (S)ize view way Plane' $key_P 'export VIEW_FILE_SIZE=Plane' 'toggle adding star to e(X)e file' $key_X 'if([ $VIEW_ADD_STAR_EXE = 0 ]) { export VIEW_ADD_STAR_EXE=1; export COLOR_EXE=$ma_red; } else { export VIEW_ADD_STAR_EXE=0; export COLOR_EXE=$ma_white }'

defmenu sort_and_mask '(n)ame' $key_n 'export SORT_KIND=name; reread -d all' '(N)ame reverse' $key_N 'export SORT_KIND=name_rev; reread -d all' '(e)xtension' $key_e 'export SORT_KIND=ext; reread -d all' '(E)xtension reverse' $key_E 'export SORT_KIND=ext_rev; reread -d all' '(s)ize' $key_s 'export SORT_KIND=size; reread -d all' '(S)ize reverse' $key_S 'export SORT_KIND=size_rev; reread -d all' '(t)ime' $key_t 'export SORT_KIND=time; reread -d all' '(T)ime reverse' $key_T 'export SORT_KIND=time_rev; reread -d all' '(u)ser' $key_u 'export SORT_KIND=user; reread -d all' '(U)ser reverse' $key_U 'export SORT_KIND=user_rev; reread -d all' '(g)roup' $key_g 'export SORT_KIND=group; reread -d all' '(G)roup reverse' $key_G 'export SORT_KIND=group_rev; reread -d all' '(p)ermission' $key_p 'export SORT_KIND=perm; reread -d all' '(P)ermission reverse' $key_P 'export SORT_KIND=perm_rev; reread -d all' '(r)andom' $key_r 'export SORT_KIND=random; reread -d all' 'none (0)' $key_0 'export SORT_KIND=none; reread -d all' 'toggle (d)ir up' $key_d 'export SORT_DIR_UP=$(toggle $SORT_DIR_UP); reread -d all' 'toggle (m)ark up' $key_m 'export SORT_MARK_UP=$(toggle $SORT_MARK_UP); reread -d all' '(M)ask' $key_M "cmdline "\""mask -r ''"\"" -2"

defmenu attribute 'ch(m)od (permission)' $key_m 'cmdline "chmod  @@(markfiles); mark -a 0" 6' 'ch(o)wn (owner)' $key_o 'cmdline "chown  @@(markfiles); mark -a 0" 6' 'ch(g)rp (group)' $key_g 'cmdline "chgrp  @@(markfiles); mark -a 0" 6' 'ch(M)od -R (permission recursive)' $key_M 'cmdline "chmod -R  @@(markfiles); mark -a 0" 9' 'ch(O)wn -R (owner recursive)' $key_O 'cmdline "chown -R  @@(markfiles); mark -a 0" 9' 'ch(G)rp -R (group recursive)' $key_G 'cmdline "chgrp -R  @@(markfiles); mark -a 0" 9' 'm(a)ttr (permission and date)' $key_a 'mattr @@(markfiles); mark -a 0; reread -d adir'

keycommand 0 $key_up        '*' 'cursor_move -1'
keycommand 0 $key_ctrl_p    '*' 'cursor_move -1'
keycommand 0 $key_down      '*' 'cursor_move +1'
keycommand 0 $key_ctrl_n    '*' 'cursor_move +1'
keycommand 0 $key_enter     '.directory' 'mcd $(cursor)'
keycommand 0 $key_enter     '.mark' 'cmdline " @@(markfiles)" 0'
keycommand 0 $key_enter     '.link' 'if([ -d $(cursor) ]){ mcd $(cursor) } else { cmdline " $(cursor)" 0 }'
keycommand 0 $key_enter     '*' 'cmdline " $(cursor)" 0'
keycommand 0 $key_x         '*' 'cmdline " ./$(cursor) " 0'
keycommand 0 $key_x         '.execute' 'cmdline "./$(cursor) "  -1'
keycommand 0 $key_backspace '*' 'mcd ..'
keycommand 0 $key_ctrl_h    '*' 'mcd ..'
keycommand 0 $key_tilda     '*' 'mcd ~'
keycommand 0 $key_backslash '*' 'mcd /'

def rename {
    ext=$(extname $(cursor))

    if([ -z "$ext" ]) {
        cmdline -q "mrename -c -r $(cursor) $(cursor)" -1
    } else {
        m=$(($(length "$(cursor)"|chomp) - $(rindex "$(cursor)" .|chomp) + 1))
        cmdline -q "mrename -c -r $(cursor) $(cursor)" -$m
    }
}

def regex_rename {
    ary_new ARGV2
    sub_option=

    for f in @@ARGV {
        if([ $f = -g ]) {
            sub_option="$suboption -g"
        } elif([ $f = -i ]) {
            sub_option="$suboption -i"
        } else {
            print $f | chomp| ary_add ARGV2
        }
    }

    for file in @@(markfiles) {
        if([ $file -re $ARGV2[0] ]) {
            puts "$file -> $(print $file | sub $sub_option $ARGV2[0] $ARGV2[1])"
        } else {
            puts "$file -> $file (not rename)"
        }
    } | less

    minitscr
    if([ $(mchoise "rename ok?" no yes) = yes ]) {
        mendwin
        mclear_immediately
        for file in @@(markfiles) {
            if([ $file -re $ARGV2[0] ]) {
                puts "mrename $file $(print $file | sub $sub_option $ARGV2[0] $ARGV2[1])"
                mrename $file $(print $file | sub $sub_option $ARGV2[0] $ARGV2[1])
            } else {
                puts "$file (no rename)"
            }
        }
    }

    mclear_immediately
    global ARGV2=0

    reread
}

keycommand 0 $key_r         '*' 'rename'
keycommand 0 $key_r         '.mark' 'cmdline "regex_rename " -1'
keycommand 0 $key_c         '*' 'if([ $VIEW_OPTION = 2pain ]){ mcp $(path -d sdir) } else { cmdline -q "mcp $(path)" -1 }'
keycommand 0 $key_C         '*' 'if([ $VIEW_OPTION = 2pain ]){ cmdline "cp -i -R @@(markfiles) $(path -d sdir)" -1 } else { cmdline "cp -i -R @@(markfiles) $(path)" -1 }'
keycommand 0 $key_m         '*' 'if([ $VIEW_OPTION = 2pain ]){ mmv $(path -d sdir) } else { cmdline -q "mmv $(path)" -1 }'
keycommand 0 $key_M         '*' 'if([ $VIEW_OPTION = 2pain ]){ cmdline "mv -i @@(markfiles) $(path -d sdir)" -1 } else { cmdline "mv -i @@(markfiles) $(path)" -1 }'

keycommand 0 $key_l         '*' 'if([ $VIEW_OPTION = 2pain ]){ mln $(path -d sdir) } else { cmdline -q "mln $(path)" -1 } '

keycommand 0 $key_b         '*' 'cmdline -q "mbackup $(cursor).bak" -5'
keycommand 0 $key_d         '*' 'if([ $(marking) = 1 ] && [ $(mchoise "move file to trashbox ok?" yes no) = yes ]){ mtrashbox -f }'
keycommand 0 $key_D         '*' 'if([ $(marking) = 1 ] && [ $(mchoise "DELETE file ok?" yes no) = yes ]){ mrm }'
keycommand 0 $key_p         '*' 'if([ $VIEW_OPTION = 2pain ]){ m=$(length $(path -d sdir)|chomp); m=$(($m + 9)); cmdline "tar cvfz $(path -d sdir).tgz @@(markfiles)" $m } else { cmdline "tar cvfz .tgz @@(markfiles)" 9 }'
keycommand 0 $key_P         '*' 'cmdline "tar cvfz .tgz @@(markfiles)" 9'
keycommand 0 $key_u         '.tgz' 'if([ $VIEW_OPTION = 2pain ]){ cmdline "tar xvfz $(cursor) -C $(path -d sdir)" -1 } else { cmdline "tar xvfz $(cursor) -C $(path)" -1 }'
keycommand 0 $key_U         '.tgz' 'cmdline "tar xvfz $(cursor) -C $(path -d adir)" -1'
keycommand 0 $key_u         '.tar' 'if([ $VIEW_OPTION = 2pain ]){ cmdline "tar xvf $(cursor) -C $(path -d sdir)" -1 } else { cmdline "tar xvf $(cursor) -C $(path)" -1 }'
keycommand 0 $key_U         '.tar' 'cmdline "tar xvf $(cursor) -C $(path -d adir)" -1'
keycommand 0 $key_u         '.gz' 'if([ $VIEW_OPTION = 2pain ]){ if(cursor | index -q tar.gz) { cmdline "tar xvfz $(cursor) -C $(path -d sdir)" -1 } else { cmdline "gzip -d $(cursor)" -1 } } else { if(cursor | index -q tar.gz) { cmdline "tar xvfz $(cursor) -C $(path)" -1 } else { cmdline "gzip -d $(cursor)" -1 } }'
keycommand 0 $key_U         '.gz' 'if([ $VIEW_OPTION = 2pain ]){ if(cursor | index -q tar.gz) { cmdline "tar xvfz $(cursor) -C $(path)" -1 } else { cmdline "gzip -d $(cursor)" -1 } } else { if(cursor | index -q tar.gz) { cmdline "tar xvfz $(cursor) -C $(path)" -1 } else { cmdline "gzip -d $(cursor)" -1 } }'
keycommand 0 $key_u         '.zip' 'if([ $VIEW_OPTION = 2pain ]){ cmdline "unzip $(cursor) -d $(path -d sdir)" -1 } else { cmdline "unzip $(cursor) -d $(path)" -1 }'
keycommand 0 $key_U         '.zip' 'cmdline "unzip $(cursor) -d $(path -d adir)" -1'
keycommand 0 $key_k         '*' 'cmdline "mkdir -p '\'\''" -2'
keycommand 0 $key_n         '*' 'cmdline "touch '\'\''" -2'
keycommand 0 $key_h         '*' 'cmdline'
keycommand 0 $key_H         '*' 'cmdline -c'
keycommand 0 $key_e         '*' 'external -q "vim $(cursor)"'
keycommand 0 $key_v         '*' 'external -q "less $(cursor)"'
keycommand 0 $key_f         '*' 'isearch'
keycommand 0 $key_F         '*' 'cmdline "find .  | scan '\''./(.+)'\'' | vd" 7'
keycommand 0 $key_F         '.mark' 'cmdline "find  @@(markfiles) | vd" 5'
keycommand 0 $key_g         '*' 'cmdline "sgrep  * .*" 6'
keycommand 0 $key_g         '.mark' 'cmdline "sgrep -R  @@(markfiles)" 9'
keycommand 0 $key_G         '*' 'cmdline "grep  * .* | scan '\''^(.+?):'\'' | uniq | vd" 5'
keycommand 0 $key_G         '.mark' 'cmdline "grep -R  @@(markfiles) | scan '\''^(.+?):'\'' | uniq | vd" 8'
keycommand 0 $key_slash     '*' 'isearch'
keycommand 0 $key_q         '*' 'if([ $(mchoise quit\? yes no) = yes ]){ quit }'
keycommand 0 $key_ctrl_c    '*' 'if([ $(mchoise quit\? yes no) = yes ]){ quit }'
keycommand 0 $key_s         '*' 'menu sort_and_mask'
keycommand 0 $key_a         '*' 'menu attribute'
keycommand 0 $key_V         '*' 'menu view'
keycommand 0 $key_w         '*' 'adir=$(path); sdir=$(path -d sdir); mcd $sdir; mcd -d sdir $adir'
keycommand 0 $key_o         '*' 'mcd $(path -d sdir)'
keycommand 0 $key_O         '*' 'mcd -d sdir $(path)'
keycommand 0 $key_home      '*' 'mark -t -f -a'
keycommand 0 $key_end       '*' 'mark -t -a'
keycommand 0 $key_pageup    '*' 'cursor_move -10'
keycommand 0 $key_ctrl_u    '*' 'cursor_move -10'
keycommand 1 $key_v         '*' 'cursor_move -10'
keycommand 0 $key_pagedown  '*' 'cursor_move +10'
keycommand 0 $key_ctrl_d    '*' 'cursor_move +10'
keycommand 0 $key_ctrl_v    '*' 'cursor_move +10'
keycommand 0 $key_ctrl_a    '*' 'cursor_move 0'
keycommand 0 $key_ctrl_e    '*' 'cursor_move $(file_num)'

def scroll_left {
    if([ $(row) != 0 ]) {
        cursor_move -$(line_max)
    }
}

def scroll_right {
    if([ $(row) != $(($(row_max) -1)) ]) {
        cursor_move +$(line_max)
    }
}

keycommand 0 $key_J '*' 'cmdline "mcd $PWD" -1'

keycommand 0 $key_left      '*' 'if([ $VIEW_OPTION = 2pain ]){ activate 0 } else { scroll_left }'
keycommand 0 $key_ctrl_b    '*' 'if([ $VIEW_OPTION = 2pain ]){ activate 0 } else { scroll_left }'
keycommand 0 $key_right     '*' 'if([ $VIEW_OPTION = 2pain ]){ activate 1 } else { scroll_right }'
keycommand 0 $key_ctrl_f    '*' 'if([ $VIEW_OPTION = 2pain ]){ activate 1 } else { scroll_right }'
keycommand 0 $key_space     '*' 'mark -t -n $(cursor_num); cursor_move +1'
keycommand 0 $key_ctrl_l    '*' 'mclear_immediately; reread -d 0; reread -d 1'
keycommand 0 $key_colon     '*' 'external "bash"'

def run_file_manager {
    if(uname -s | index -q CYGWIN) {
        cygstart . &
    } elif(uname -s | index -q Darwin) {
        open . &
    } else {
        if (ps axw | grep -v grep | index -q nautilus) {
            nautilus "$PWD" 2>/dev/null
        } elif(ps axw | grep -v grep | index -q kdeinit) {
            konqueror "$PWD" 2>/dev/null &
        } elif(ps axw | grep -v grep | index -q xfdesktop) {
            thunar "$PWD" 2>/dev/null
        }
    }
}

keycommand 0 $key_exclam    '*' 'run_file_manager'

global DOT_MASK=0
keycommand 0 $key_dot       '*' 'if([ $DOT_MASK = 0 ]){ global DOT_MASK=1; mask -dotdir 1; mask -r "^[^\.].*"; } else { global DOT_MASK=0; mask -dotdir 0; mask -r ".+"; }'

keycommand 0 $key_lbrack    '*' 'MARK_RANGE_BEGIN=$(cursor_num)'
keycommand 0 $key_rbrack    '*' 'if([ $MARK_RANGE_BEGIN != -1 ]){ i=$MARK_RANGE_BEGIN; while([ $i -le $(cursor_num) ]){ mark -t -n $i; i=$(($i + 1)) } }'

def glob_mark {
    for file in @@ARGV {
        mark -t $file
    }
}

keycommand 0 $key_star      '*' 'cmdline "glob_mark *" -1'

def regex_mark {
    for file in * .* {
        if([ $file -re $ARGV[0] ]) {
            mark -t $file
        }
    }
}

keycommand 0 $key_plus      '*' 'cmdline "regex_mark '\'\''" -2'

keycommand 0 $key_meta_left  '*' 'mcd -'
keycommand 0 $key_meta_right '*' 'mcd +'
keycommand 1 $key_left       '*' 'mcd -'
keycommand 1 $key_right      '*' 'mcd +'

keycommand 0 $key_ctrl_t    '*' 'a=$(dir_num); new_dir ~ -m "^[^\.].*" -dotdir 1; activate $a'
keycommand 0 $key_ctrl_w    '*' 'if([ $(dir_num) -gt 2 ]){ activate $(($(dir_num) - 1)); del_dir -d $(($(dir_num) -1)) }'
keycommand 0 $key_tab       '*' 'if([ $(adir) = 0 ]){ activate 1 } else { activate 0 }'

keycommand 0 $key_1         '*' 'external -q "fg 1"'
keycommand 0 $key_2         '*' 'external -q "fg 2"'
keycommand 0 $key_3         '*' 'external -q "fg 3"'
keycommand 0 $key_4         '*' 'external -q "fg 4"'
keycommand 0 $key_5         '*' 'external -q "fg 5"'
keycommand 0 $key_6         '*' 'external -q "fg 6"'
keycommand 0 $key_7         '*' 'external -q "fg 7"'
keycommand 0 $key_8         '*' 'external -q "fg 8"'
keycommand 0 $key_9         '*' 'external -q "fg 9"'

keycommand 1 $key_0         '*' 'activate 0'
keycommand 1 $key_1         '*' 'activate 1'
keycommand 1 $key_2         '*' 'activate 2'
keycommand 1 $key_3         '*' 'activate 3'
keycommand 1 $key_4         '*' 'activate 4'
keycommand 1 $key_5         '*' 'activate 5'
keycommand 1 $key_6         '*' 'activate 6'
keycommand 1 $key_7         '*' 'activate 7'
keycommand 1 $key_8         '*' 'activate 8'
keycommand 1 $key_9         '*' 'activate 9'


def help {
    if(which lv > /dev/null && index -q "$LANG" ja || index -q "$LC_ALL" ja) {
        lv $MF3DOCDIR/USAGE.ja.txt 
    } else {
        less $MF3DOCDIR/USAGE.en.txt
    }
}

keycommand 0 $key_qmark     '*' 'external -q help'

#########################################################################
# キーマップ
##########################################################################
keymap   $key_up         27 91 65 -1 -1 -1 -1 -1 -1 -1
keymap   $key_up         27 79 65 -1 -1 -1 -1 -1 -1 -1
keymap   $key_down       27 91 66 -1 -1 -1 -1 -1 -1 -1
keymap   $key_down       27 79 66 -1 -1 -1 -1 -1 -1 -1
keymap   $key_right      27 91 67 -1 -1 -1 -1 -1 -1 -1
keymap   $key_right      27 79 67 -1 -1 -1 -1 -1 -1 -1
keymap   $key_left       27 91 68 -1 -1 -1 -1 -1 -1 -1
keymap   $key_left       27 79 68 -1 -1 -1 -1 -1 -1 -1
keymap   $key_insert     27 91 50 126 -1 -1 -1 -1 -1 -1
keymap   $key_delete     27 91 51 126 -1 -1 -1 -1 -1 -1
keymap   $key_backspace  127 -1 -1 -1 -1 -1 -1 -1 -1 -1
keymap   $key_home       27 79 72  -1  -1 -1 -1 -1 -1 -1
keymap   $key_home       27 91 72  -1  -1 -1 -1 -1 -1 -1
keymap   $key_home       27 91 55  126 -1 -1  -1 -1 -1 -1
keymap   $key_home       27 91 0   -1  -1 -1  -1 -1 -1 -1
keymap   $key_home       27 91 49  126 -1 -1  -1 -1 -1 -1
keymap   $key_end        27 79 70  -1  -1 -1  -1 -1 -1 -1
keymap   $key_end        27 91 70  -1  -1 -1  -1 -1 -1 -1
keymap   $key_end        27 91 56  126 -1 -1  -1 -1 -1 -1
keymap   $key_end        27 91 101 -1  -1 -1  -1 -1 -1 -1
keymap   $key_end        27 91 52  126 -1 -1  -1 -1 -1 -1
keymap   $key_pageup     27 91 53  126 -1 -1  -1 -1 -1 -1
keymap   $key_pageup     27 91 73  -1  -1 -1  -1 -1 -1 -1
keymap   $key_pagedown   27 91 54  126 -1 -1  -1 -1 -1 -1
keymap   $key_pagedown   27 91 71  -1  -1 -1  -1 -1 -1 -1
keymap   $key_f1         27 91 49  49  126 -1 -1 -1 -1 -1
keymap   $key_f1         27 79 80  -1  -1  -1 -1 -1 -1 -1
keymap   $key_f1         27 91 91  65  -1  -1 -1 -1 -1 -1
keymap   $key_f2         27 91 49  50  126 -1 -1 -1 -1 -1
keymap   $key_f2         27 79 81  -1  -1  -1 -1 -1 -1 -1
keymap   $key_f2         27 91 91  66  -1  -1 -1 -1 -1 -1
keymap   $key_f3         27 91 49  51  126 -1 -1 -1 -1 -1
keymap   $key_f3         27 79 82  -1  -1  -1 -1 -1 -1 -1
keymap   $key_f3         27 91 91  67  -1  -1 -1 -1 -1 -1
keymap   $key_f4         27 91 49  52  126 -1 -1 -1 -1 -1
keymap   $key_f4         27 79 83  -1  -1  -1 -1 -1 -1 -1
keymap   $key_f4         27 91 91  68  -1  -1 -1 -1 -1 -1
keymap   $key_f5         27 91 49  53  126 -1 -1 -1 -1 -1
keymap   $key_f5         27 91 91  69  -1  -1 -1 -1 -1 -1
keymap   $key_f6         27 91 49  55  126 -1 -1 -1 -1 -1
keymap   $key_f7         27 91 49  56  126 -1 -1 -1 -1 -1
keymap   $key_f8         27 91 49  57  126 -1 -1 -1 -1 -1
keymap   $key_f9         27 91 50  48  126 -1 -1 -1 -1 -1
keymap   $key_f10        27 91 50  49  126 -1 -1 -1 -1 -1
keymap   $key_f11        27 91 50  51  126 -1 -1 -1 -1 -1
keymap   $key_f12        27 91 50  52  126 -1 -1 -1 -1 -1
keymap   $key_meta_left  27 91 51   68  -1 -1 -1 -1 -1 -1 # gnome-terminal
keymap   $key_meta_left  27 91 49   59  51 68 -1 -1 -1 -1 # xterm
keymap   $key_meta_left  27 91 49   59  57 68 -1 -1 -1 -1 # mlterm
keymap   $key_meta_left  27 79 49   59  51 68 -1 -1 -1 -1 # screen
keymap   $key_meta_right 27 91 51   67  -1 -1 -1 -1 -1 -1 # gnome-terminal
keymap   $key_meta_right 27 91 49   59  51 67 -1 -1 -1 -1 # xterm
keymap   $key_meta_right 27 91 49   59  57 67 -1 -1 -1 -1 # mlterm
keymap   $key_meta_right 27 79 49   59  51 67 -1 -1 -1 -1 # screen
keymap   $key_meta_up    27 91 51   65  -1 -1 -1 -1 -1 -1 # gnome-terminal
keymap   $key_meta_up    27 91 49   59  51 65 -1 -1 -1 -1 # xterm
keymap   $key_meta_up    27 91 49   59  57 65 -1 -1 -1 -1 # mlterm
keymap   $key_meta_up    27 79 49   59  51 65 -1 -1 -1 -1 # screen
keymap   $key_meta_down  27 91 51   66  -1  -1  -1 -1 -1 -1 # gnome-terminal
keymap   $key_meta_down  27 91 49   59  51  66  -1 -1 -1 -1 # xterm
keymap   $key_meta_down  27 91 49   59  57  66  -1 -1 -1 -1 # mlterm
keymap   $key_meta_down  27 79 49   59  51  66  -1 -1 -1 -1 # screen

#########################################################################
# ジャンプメニュー
##########################################################################
ary_new JTITLE_LIST
ary_new JKEY_LIST
ary_new JDIR_LIST

def add_jump_menu {
    if([ $JTITLE_LIST_size -le 9 ]) {
        print "($JTITLE_LIST_size) $ARGV[0]" | ary_add JTITLE_LIST
        print "${key_$(($JTITLE_LIST_size-1))}" | ary_add JKEY_LIST
    } else {
        print "(0) $ARGV[0]" | ary_add JTITLE_LIST
        print $key_0 | ary_add JKEY_LIST
    }
       
    print $ARGV[0] | ary_add JDIR_LIST

    make_jump_menu
}

def clear_jump_menu {
    ary_clear JDIR_LIST
    ary_clear JKEY_LIST
    ary_clear JTITLE_LIST

    ary_new JTITLE_LIST
    ary_new JKEY_LIST
    ary_new JDIR_LIST

    make_jump_menu
}

def make_jump_menu {
    defmenu jump

    i=0
    while([ $i -lt $JDIR_LIST_size ]) {
        addmenu jump $JTITLE_LIST[$i] $JKEY_LIST[$i] "mcd $JDIR_LIST[$i]"
        i=$(($i+1))
    }

    addmenu jump ----------------- $key_a "external 'puts dummy'"
    addmenu jump '(A)dd current directory to jump menu' $key_A 'cmdline "add_jump_menu $(path -d adir)" -1'
    addmenu jump '(C)lear jump menu' $key_C 'clear_jump_menu'
}

def save_jump_menu {
    print > $MF3HOME/jmenu

    i=0
    while([ $i -lt $JTITLE_LIST_size ]) {
        puts $JTITLE_LIST[$i] >> $MF3HOME/jmenu
        puts $JKEY_LIST[$i] >> $MF3HOME/jmenu
        puts $JDIR_LIST[$i] >> $MF3HOME/jmenu

        ++ i
    }
}

def load_jump_menu {
    NR=0
    cat $MF3HOME/jmenu | each {
        if([ $(( $NR % 3 )) = 0 ]) {
            | ary_add JTITLE_LIST
        } elif([ $(( $NR % 3 )) = 1 ]) {
            | ary_add JKEY_LIST
        } elif([ $(( $NR % 3 )) = 2 ]) {
            | ary_add JDIR_LIST
        }

        ++ NR
    }

    make_jump_menu
}

### 初期化 ###
if ([ -e $MF3HOME/jmenu ]){
    load_jump_menu
} else {
    make_jump_menu
}

keycommand 0 $key_j '*' 'menu jump'

##########################################################################
# フック
##########################################################################
# ディレクトリが移動するとき
MARK_RANGE_BEGIN=-1
def mcd_hook {
    # ARGV[0] に移動したディレクトリ番号が入ってます
    MARK_RANGE_BEGIN=-1
}

# ディレクトリがアクティブになったっとき
def activate_hook {
    # ARGV[0] にアクティブになったディレクトリ番号が入ってます
    true
}

# カーソルが移動したときに
def cursor_move_hook {
    # ARGV[0] にアクティブになったディレクトリ番号が入ってます
    true
}

# コマンドラインに移行したとき
def cmdline_hook {
    true
}

# 終了するとき
def atexit_hook {
    save_jump_menu
    save_mfiler3_stat
}

##########################################################################
# 設定
##########################################################################
export HISTORY_FILE=$MF3HOME/history   # ヒストリファイルの名前
export HISTORY_SIZE=1000               # コマンドラインヒストリの最大サイズ
export TRASHBOX_DIR=$MF3HOME/trashbox/ # dによるゴミ箱への移動ディレクトリ
export CMDLINE_COLOR=1

export PROMPT='print "mfiler3 $VERSION with saphire $SAPHIRE_VERSION (c)Minato Daisuke 2004-2011 hit <?> for help"' # プロンプト。表示時にevalされます

export CMDLINE_ESCAPE_KEY_ENABLE=1     # コマンドラインでEscapeキーを効かすかどうか
export VIEW_ADD_STAR_EXE=1             # 実行ファイルにスターマークをつけるかどうか

# 初期化
def init_mfiler3_stat() {
    export VIEW_SIZE=0
    export VIEW_OPTION=2pain
    export VIEW_MTIME=0
    export VIEW_OWNER=0
    export VIEW_GROUP=0
    export VIEW_NLINK=0
    export VIEW_PERMISSION=0
    export VIEW_DIVIDE_EXTENSION=0
    export VIEW_FOCUSBACK=0
    export VIEW_PAGE=0
    export VIEW_FILE_SIZE=Human

    export COLOR_DIR=$ma_bold
    export COLOR_EXE=$ma_bold
    export COLOR_LINK=0
    export COLOR_MARK=$ma_bold

    export SORT_KIND=name
    export SORT_DIR_UP=1
    export SORT_MARK_UP=0

    new_dir $(pwd|chomp)/  # 初期ディレクトリ
    new_dir $(pwd|chomp)/  # 初期ディレクトリ
}

# 終了時に保存
def save_mfiler3_stat() {
    puts $VIEW_SIZE > $MF3HOME/stat
    puts $VIEW_OPTION >> $MF3HOME/stat
    puts $VIEW_MTIME >> $MF3HOME/stat
    puts $VIEW_OWNER >> $MF3HOME/stat
    puts $VIEW_GROUP >> $MF3HOME/stat
    puts $VIEW_NLINK >> $MF3HOME/stat
    puts $VIEW_PERMISSION >> $MF3HOME/stat
    puts $VIEW_DIVIDE_EXTENSION >> $MF3HOME/stat
    puts $VIEW_FOCUSBACK >> $MF3HOME/stat
    puts $VIEW_PAGE >> $MF3HOME/stat
    puts $VIEW_FILE_SIZE >> $MF3HOME/stat

    puts $COLOR_DIR >> $MF3HOME/stat
    puts $COLOR_EXE >> $MF3HOME/stat
    puts $COLOR_LINK >> $MF3HOME/stat
    puts $COLOR_MARK >> $MF3HOME/stat

    puts $SORT_KIND >> $MF3HOME/stat
    puts $SORT_DIR_UP >> $MF3HOME/stat
    puts $SORT_MARK_UP >> $MF3HOME/stat

    dir_num | pomch >> $MF3HOME/stat

    i=0
    while([ $i -lt $(dir_num) ]) {
        path -d $i | pomch >> $MF3HOME/stat
        mask -d $i | pomch >> $MF3HOME/stat
        mask -d $i -dotdir | pomch >> $MF3HOME/stat

        ++ i
    }

    puts $VIEW_ADD_STAR_EXE >> $MF3HOME/stat
}

# 起動時に復帰
def load_mfiler3_stat() {
    read $MF3HOME/stat | export VIEW_SIZE
    read $MF3HOME/stat | export VIEW_OPTION
    read $MF3HOME/stat | export VIEW_MTIME
    read $MF3HOME/stat | export VIEW_OWNER
    read $MF3HOME/stat | export VIEW_GROUP
    read $MF3HOME/stat | export VIEW_NLINK
    read $MF3HOME/stat | export VIEW_PERMISSION
    read $MF3HOME/stat | export VIEW_DIVIDE_EXTENSION
    read $MF3HOME/stat | export VIEW_FOCUSBACK
    read $MF3HOME/stat | export VIEW_PAGE
    read $MF3HOME/stat | export VIEW_FILE_SIZE

    read $MF3HOME/stat | export COLOR_DIR
    read $MF3HOME/stat | export COLOR_EXE
    read $MF3HOME/stat | export COLOR_LINK
    read $MF3HOME/stat | export COLOR_MARK

    read $MF3HOME/stat | export SORT_KIND
    read $MF3HOME/stat | export SORT_DIR_UP
    read $MF3HOME/stat | export SORT_MARK_UP

    read $MF3HOME/stat | var dir_num

    i=0
    while([ $i -lt $dir_num ]) {
        read $MF3HOME/stat | var path
        read $MF3HOME/stat | var mask
        read $MF3HOME/stat | var mask_dotdir

        new_dir $path -m "$$mask" -dotdir "$mask_dotdir"
        
        ++ i
    }

    read $MF3HOME/stat | export VIEW_ADD_STAR_EXE
}

if([ -e $MF3HOME/stat ]) {
    load_mfiler3_stat
} else {
    init_mfiler3_stat
}
