###############################################################################
# ruby library search path
###############################################################################
#$:.push(".")
#$:.push("/usr/local/lib/ruby/1.8")
#$:.push("/usr/local/lib/ruby/1.8/i386-freebsd5")

#require "nkf"

###############################################################################
# check Env
###############################################################################
if ENV['PAGER'] == nil || ENV['PAGER'] == "" then
    if File.exist?("/usr/bin/lv") || File.exist?("/usr/local/bin/lv") then
        ENV['PAGER'] = "lv"
    elsif File.exist?("/usr/bin/less") || File.exist?("/usr/local/bin/less") then
        ENV['PAGER'] = "less"
    else
        ENV['PAGER'] = "more"
    end
end

if ENV['EDITOR'] == nil || ENV['EDITOR'] == "" then
    ENV['EDITOR'] = "vi"
end

if ENV['SHELL'] == nil || ENV['SHELL'] == "" then
    if File.exist?("/bin/zsh") || File.exist?("/usr/bin/zsh") || File.exist?("/usr/local/bin/zsh") then
        ENV['SHELL'] = "zsh"
    elsif File.exist?("/bin/bash") || File.exist?("/usr/bin/bash") || File.exist?("/usr/local/bin/bash") then
        ENV['SHELL'] = "bash"
    elsif File.exist?("/bin/tcsh") || File.exist?("/usr/bin/tcsh") || File.exist?("/usr/local/bin/tcsh") then
        ENV['SHELL'] = "tcsh"
     else
        ENV['SHELL'] = "sh"
    end
end

###############################################################################
# function definitions
###############################################################################
def is_one_dir()
    vo = view_option();

    return vo == "one_dir" || vo == "one_dir2" || vo == "one_dir3" || vo == "one_dir5";
end

def key_left()
    if is_one_dir() then
        if cursor_x() > 0 then
            cursor_move(cursor()-cursor_maxy())
        end
    else
        if is_adir_left() then
            dir_move('..');
        else
            cursor_left();
        end
    end
end

def key_right()
    if is_one_dir() then
        if cursor_x() < (cursor_maxx()-1)
            cursor_move(cursor()+cursor_maxy())
        end
    else
        if is_adir_right() then
            dir_move('..');
        else
            cursor_right();
        end
    end
end

def key_up()
    cursor_move(cursor()-1)
end

def key_down()
    cursor_move(cursor()+1)
end

def fdclone_type_pageup()
    if is_one_dir() then
        cursor_move(cursor()-5)
    else
        if cursor_max() <= cursor_maxy() then
            cursor_move(cursor()-5)
        else 
            if cursor() >= cursor_maxy() then
                cursor_move(cursor()-cursor_maxy())
            end
        end
    end
end

def afu_type_pageup()
    if is_one_dir() then
        cursor_move(cursor()-5)
    else
        if cursor_max() <= cursor_maxy() then
            cursor_move(cursor()-5)
        else    
            cursor_move(cursor()-cursor_maxy()+2)
        end
    end
end

def fdclone_type_pagedown()
    if is_one_dir() then
        cursor_move(cursor()+5)
    else
        if cursor_max() <= cursor_maxy() then
            cursor_move(cursor()+5)
        else
            n = (cursor_max()/cursor_maxy())*cursor_maxy()
            if cursor() < n then
                cursor_move(cursor()+cursor_maxy())
            end
        end
    end
end

def afu_type_pagedown()
    if is_one_dir() then
        cursor_move(cursor()+5)
    else
        if cursor_max() <= cursor_maxy() then
            cursor_move(cursor()+5)
        else
            cursor_move(cursor()+cursor_maxy()-2)
        end
    end
end

def key_pageup()
    afu_type_pageup()
end

def key_pagedown()
    afu_type_pagedown()
end

###############################################################################
# compile
###############################################################################
def compile(command)
    if !command.kind_of?(String)
        return 
    end

    flg_initscr = false
    if !mis_curses 
        minitscr()
        flg_initscr = true
    end

    ### make ###
    mclear_immediately
    mclear()
    mmove_immediately(0,0)
    mrefresh()
    
    output = `#{command} 2>&1 1>/dev/null`

    ### split ###
    lines = []
    i = 0
    tmp = ""
    x = 0
    while i < output.size()
        if output[i] == ?\n
            if tmp != ""
                lines.push(tmp)
            end
            tmp = ""
            x = 0
        elsif x == mgetmaxx()-1
            tmp = tmp + output[i].chr

            if tmp != ""       
                lines.push(tmp)
            end
            tmp = ""
            x = 0
        else
            tmp = tmp + output[i].chr
            x = x + 1
        end

        i = i + 1
    end

    ### loop ###
    if lines.size > 0 
        scroll_top = 0
        cursor = 0
        key = [0, 0]    
        while key[1] != ?q && key[1] != 27 && key[1] != 3
            ### view ###
            mclear()
            n = scroll_top
            while n < lines.size
                if cursor == n
                    mattron(MA_REVERSE)
                end
                
                mmvprintw(n-scroll_top, 0, lines[n])
                
                if cursor == n
                    mattroff()
                end
                
                n = n + 1
            end
            
            mrefresh()
    
            ### input ###
            key = mgetch()

            if key[1] == 14 || key[1] == KEY_DOWN 
                cursor = cursor + 1
            end
            if key[1] == 16 || key[1] == KEY_UP 
                cursor = cursor -1
            end
            if key[1] == 4 || key[1] == KEY_PAGEDOWN
                cursor = cursor + 5
            end
            if key[1] == 21 || key[1] == KEY_PAGEUP
                cursor = cursor - 5
            end
            if key[1] == 12
                mclear_immediately()
                mclear
                mrefresh
            end
            if key[1] == ?z
                xterm_next
            end
            
            if key[1] == 10 || key[1] == 13
                array = lines[cursor].split(':')

                if !File.exist?(array[0])
                    output = `find . -name #{array[0]}`
                    array2 = output.split
                    
                    if output.size != 0 && array2.size == 1
                        array[0] = output.chomp
                    end
                    if array2.size >  1
                        mclear()                    
                        mmvprintw(0, 0, "find two or more " + array[0])
                        mrefresh()
                        mgetch()
                        
                        #array[0] = array2[0].chomp
                    end
                end

                if File.exist?(array[0]) && array.size == 4
                     # array[1] == line number
                     # array[0] == file name

#                    shell("%q emacs +" + array[1] + " -nw " + array[0], 'array[0]')
                     shell("%q vi +" + array[1] + " " + array[0], 'array[0]')
                end
            end

            ### modification ###
            if cursor < 0
                cursor = 0
            end
            if cursor >= lines.size
                cursor = lines.size - 1
            end
            if cursor < scroll_top
                 scroll_top = cursor
            end
            if cursor > scroll_top + mgetmaxy() - 1
                scroll_top = scroll_top + (cursor-scrolltop-mgetmaxy()) + 1
            end
        end
    end

    if flg_initscr 
        mendwin()
    end

    refresh()
end

###############################################################################
# hook
###############################################################################
def prompt()
    "the Minnu's Filer2 " + username + "@" + hostname + " "
end

def completion_mask(editing, reg, sqort, dqort)
     array = []

     Dir.foreach(adir_path()) { |file|
         reg = Regexp.new(reg)
           
         if File.stat(file).directory? && file != "." && file != ".." then
             array.push(file + "/")
         elsif reg.match(file) then
             array.push(file)
         end
    }

    array.sort
    completion(array, editing, sqort, dqort)

    return true
end

def completion_hook(editing, editing_dir, editing_file, cmd, editing_position, sqort, dqort)
    if cmd == "ls" && editing[0] == "-"[0] then
        array = [
            ["-1", "single column output" ], 
            ["-A", "list all except . and .." ],
            ["-B", "don't list entries ending with ~"],
            ["-C", "list entries in columns sorted vertically"],
            ["-D", "generate output designed for Emacs' dired mode"],
            ["-F", "append file type indicators"],
            ["-G", "inhibit display of group information"],
            ["-H", "sizes in human readable form; powers of 1000"],
            ["-I", "don't list entire matching pattern"],
            ["-L", "list referenced file for sym link"],
            ["-N", "print raw characters"],
            ["-Q", "quote names"],
            ["-R", "list subdirectories recursively"],
            ["-S", "sort by size"],
            ["-T", "specify tab size"],
            ["-U", "unsorted"],
            ["-X", "sort by extension"],
            ["-a", "list entries starting with ."],
            ["-b", "print octal escapes for control characters"],
            ["-c", "status change time"],
            ["-d", "list directory entries instead of contents"],
            ["-f", "unsorted, all, short list"],
            ["-g", "long listing but without owner information"],
            ["-h", "print sizes in human readable form"],
            ["-i", "print file inode numbers"],
            ["-k", "use block size of 1k"],
            ["-l", "long listing"],
            ["-m", "comma separated"],
            ["-n", "numeric uid, gid"],
            ["-o", "no group, long"],
            ["-p", "append file type indicators except *"],
            ["-q", "hide control chars"],
            ["-r", "reverse sort order"],
            ["-s", "display size of each file in blocks"],
            ["-t", "sort by modification time"],
            ["-u", "access time"],
            ["-v", "sort by version (filename treated numerically)"],
            ["-w", "specify screen width"],
            ["-x", "sort horizontally" ]
        ]
        completion2(array, editing, sqort, dqort)

        return true
        
    elsif cmd == "lpr" && editing_dir == "" then
        completion_mask(editing, '.*\.ps', sqort, dqort)
        return true

    elsif cmd == "ssh" then
        if editing.rindex('@') == nil then
            array = ["s1090202@"]
            completion(array, editing, sqort, dqort)

            return true
        else
            user = editing[0..editing.rindex('@')]
            array = [ user + "sshgate.u-aizu.ac.jp"]
            completion(array, editing, sqort, dqort)

            return true
        end
    end

    return nil    
end

def fdclone_type_cursor_move_hook()
    vo = view_option()
    maxy = cursor_maxy()
    
    if vo == "all" || vo == "one_dir" then
         if cursor() >= scrolltop() + maxy then
             set_scrolltop( (cursor()/maxy)*maxy )
         end
         if cursor() < scrolltop() then
             set_scrolltop( (cursor()/maxy)*maxy )
         end
    elsif vo == "one_dir2" then
        if cursor() >= scrolltop() + maxy*2 then
             set_scrolltop( (cursor()/(maxy*2))*(maxy*2) )
        end
        if cursor() < scrolltop() then
            set_scrolltop( (cursor()/(maxy*2))*(maxy*2) )
        end
    elsif vo == "one_dir3" then
        if cursor() >= scrolltop() + maxy*3 then
             set_scrolltop( (cursor()/(maxy*3))*(maxy*3) )
        end
        if cursor() < scrolltop() then
            set_scrolltop( (cursor()/(maxy*3))*(maxy*3) )
        end
    elsif vo == "one_dir5" then
        if cursor() >= scrolltop() + maxy*5 then
             set_scrolltop( (cursor()/(maxy*5))*(maxy*5) )
        end
        if cursor() < scrolltop() then
            set_scrolltop( (cursor()/(maxy*5))*(maxy*5) )
        end
    end
end

def afu_type_cursor_move_hook()
    vo = view_option()
    maxy = cursor_maxy()
    
    if vo == "all" || vo == "one_dir" then
         if cursor() >= scrolltop() + maxy-2 then
             set_scrolltop(cursor() -maxy+2)
         end
         if cursor() < scrolltop() then
             set_scrolltop(cursor())
         end
    elsif vo == "one_dir2" then
        if cursor() >= scrolltop() + maxy*2 then
             set_scrolltop( (cursor()/(maxy*2))*(maxy*2) )
        end
        if cursor() < scrolltop() then
            set_scrolltop( (cursor()/(maxy*2))*(maxy*2) )
        end
    elsif vo == "one_dir3" then
        if cursor() >= scrolltop() + maxy*3 then
             set_scrolltop( (cursor()/(maxy*3))*(maxy*3) )
        end
        if cursor() < scrolltop() then
            set_scrolltop( (cursor()/(maxy*3))*(maxy*3) )
        end
    elsif vo == "one_dir5" then
        if cursor() >= scrolltop() + maxy*5 then
             set_scrolltop( (cursor()/(maxy*5))*(maxy*5) )
        end
        if cursor() < scrolltop() then
            set_scrolltop( (cursor()/(maxy*5))*(maxy*5) )
        end
    end
end

def cursor_move_hook(i)
    afu_type_cursor_move_hook()
end

###############################################################################
# key settings
###############################################################################
keycommand NOMETA, KEY_UP,           "*",        "key_up()"
keycommand NOMETA, KEY_DOWN,         "*",        "key_down()"
keycommand NOMETA, KEY_LEFT,         "*",        "key_left()"
keycommand NOMETA, KEY_RIGHT,        "*",        "key_right()"
keycommand NOMETA, KEY_PAGEUP,       "*",        "key_pageup()"
keycommand NOMETA, KEY_PAGEDOWN,     "*",        "key_pagedown()"
keycommand NOMETA, KEY_CTRL_P,       "*",        "key_up()"
keycommand NOMETA, KEY_CTRL_N,       "*",        "key_down()"
keycommand NOMETA, KEY_CTRL_B,       "*",        "key_left()"
keycommand NOMETA, KEY_CTRL_F,       "*",        "key_right()"
keycommand NOMETA, KEY_CTRL_D,       "*",        "key_pagedown()"
keycommand NOMETA, KEY_CTRL_U,       "*",        "key_pageup()"
keycommand NOMETA, KEY_CTRL_A,       "*",        "cursor_move(0)"
keycommand NOMETA, KEY_CTRL_E,       "*",        "cursor_move(cursor_max()-1)"
keycommand NOMETA, KEY_LSS,          "*",        "cursor_move(0)"
keycommand NOMETA, KEY_GTR,          "*",        "cursor_move(cursor_max()-1)"
keycommand NOMETA, KEY_CTRL_H,       "*",        "dir_move('..')"
keycommand NOMETA, KEY_BACKSPACE,    "*",        "dir_move('..')"
keycommand NOMETA, 127,              "*",        "dir_move('..')"
keycommand NOMETA, KEY_DELETE,       "*",        "dir_move('..')"
keycommand META,   KEY_LEFT,         "*",        "dir_back()"
keycommand NOMETA, KEY_BACKSLASH,    "*",        "dir_move('/')"
keycommand NOMETA, KEY_TILDA,        "*",        "dir_move(\"#{ENV['HOME']}\")"
keycommand NOMETA, KEY_SPACE,        "*",        "mark()"
keycommand NOMETA, KEY_HOME,         "*",        "mark_all_files()"
keycommand NOMETA, KEY_END,          "*",        "mark_all()"
keycommand NOMETA, KEY_CTRL_L,       "*",        "refresh()"
keycommand NOMETA, KEY_CTRL_I,       "*",        "dir_up()"
keycommand NOMETA, KEY_CTRL_T,       "*",        "dir_new()"
keycommand NOMETA, KEY_CTRL_W,       "*",        "dir_close()"
keycommand NOMETA, KEY_SLASH,        "*",        "isearch()"
keycommand NOMETA, KEY_QMARK,        "*",        "help()"
keycommand NOMETA, KEY_ESCAPE,       "*",        "mf_exit()"

keycommand NOMETA, KEY_c,      "*",     "if is_one_dir then cmdline('%Q%r copy(adir_path(), adir_mark(), %D1s)', -2) else cmdline('%Q%r copy(adir_path(), adir_mark(), %D2s)', -2) end"
keycommand NOMETA, KEY_m,      "*",     "if is_one_dir then cmdline('%Q%r move(adir_path(), adir_mark(), %D1s)', -2) else cmdline('%Q%r move(adir_path(), adir_mark(), %D2s)', -2) end"
keycommand NOMETA, KEY_d,      "*",     "trashbox(adir_path(), adir_mark())"
keycommand NOMETA, KEY_e,      "*",     "shell('%q $EDITOR %f', '%f')"
keycommand NOMETA, KEY_v,      "*",     "shell('%q $PAGER %f', '%f')"
keycommand NOMETA, KEY_r,      "*",     "cmdline('%Q mv %f %f', -1)"
keycommand NOMETA, KEY_b,      "*",     "cmdline('%Q%r copy(adir_path(), adir_mark(), %f)', -2)"
keycommand NOMETA, KEY_p,      "*",     "cmdline('tar cvfz %X.tgz %m1', -9)"
keycommand NOMETA, KEY_k,      "*",     "cmdline(\"%Q mkdir ''\", -1)"
keycommand NOMETA, KEY_n,      "*",     "cmdline(\"%Q touch ''\", -1)"
keycommand NOMETA, KEY_g,      "*",     "cmdline(\"%q grep '' * -n 2> /dev/null | $PAGER\", 10)"
keycommand NOMETA, KEY_f,      "*",     "cmdline(\"%Q%r set_mask('{*,.*}')\", -2)"
keycommand NOMETA, KEY_i,      ".dir",  "cmdline('du -sk %f', 7)"
keycommand NOMETA, KEY_i,      "*",     "cmdline('file %f', 0)"
keycommand NOMETA, KEY_l,      "*",     "cmdline(\"%r%Q dir_move(%D1s)\", -2)"
keycommand NOMETA, KEY_o,      "*",     "if !is_one_dir then dir_move(sdir_path() + '/') end"
keycommand NOMETA, KEY_w,      "*",     "if !is_one_dir then tmp = adir_path(); dir_move(sdir_path() + '/'); sdir_move(tmp + '/') end"
keycommand NOMETA, KEY_q,      "*",     "mf_exit()"

defmenu("jump",
   "(1) $HOME/mp3", KEY_1, "dir_move(\"#{ENV['HOME']}/mp3\")",
   "(2) $HOME/public_html", KEY_2, "dir_move(\"#{ENV['HOME']}/public_html\")",
   "(3) /usr/include", KEY_3, "dir_move('/usr/include')"
);

defmenu("sort",
   "sort (n)ame", KEY_n, "sort_name()",
   "sort (N)ame reverse", KEY_N, "sort_name_reverse()",
   "sort (e)xtension", KEY_e, "sort_ext()",
   "sort (E)xtension reverse", KEY_E, "sort_ext_reverse()",
   "sort (s)ize", KEY_s, "sort_size()",
   "sort (S)ize reverse", KEY_S, "sort_size_reverse()",
   "sort (t)ime", KEY_t, "sort_time()",
   "sort (T)ime reverse", KEY_T, "sort_time_reverse()",
   "sort toggle (d)ir up", KEY_d, "sort_toggle_dir_up()",
   "view 2dir(a)ll", KEY_a, "view_all()",
   "view 1dir(1)", KEY_1, "view_onedir()",
   "view 1dir(2)", KEY_2, "view_onedir2()",
   "view 1dir(3)", KEY_3, "view_onedir3()",
   "view 1dir(5)", KEY_5, "view_onedir5()",
   "view toggle name(o)nly", KEY_o, "view_nameonly()"
);

defmenu("file_attr",
   "ch(m)od (permission)", KEY_m, "cmdline('%Q chmod -R  %m1', 13)",
   "ch(o)wn (owner)", KEY_o, "cmdline('%Q chown -R  %m1', 13)",
   "ch(g)rp (group)", KEY_g, "cmdline('%Q chgrp -R  %m1', 13)",
   "m(a)ttr (permission and date)", KEY_a, "shell('%q mattr %m1', 'mattr')"
);

defmenu("context-menu",
   "(d)elete", KEY_d, "remove(adir_path(), adir_mark())",
    "(f)ind", KEY_f, "cmdline(\"%q find . -name '' 2> /dev/null | $PAGER\", 18)", 
   "new symbolic (l)ink", KEY_l, "if is_one_dir() then cmdline('%Q ln -s %F %D1s', -1) else cmdline('%Q ln -s %F %D2s', -1) end",   
   "new (s)hell script", KEY_s, "cmdline(\"%Q echo '#!/bin/sh' > /tmp/sscript; chmod 755 /tmp/sscript; mv /tmp/sscript '.sh'\", -4)",
   "(c)p",  KEY_c, "if is_one_dir() then cmdline('cp -Rpf %m1 %D1s', -1) else cmdline('cp -Rpf %m1 %D2s', -1) end",
   "(m)v",  KEY_m, "if is_one_dir() then cmdline('mv -f %m1 %D1s', -1) else cmdline('mv -f %m1 %D2s', -1) end",
   "(r)m",  KEY_r, "cmdline('rm -R %m1', 0)",
   "re(h)ash",  KEY_h, "rehash",
   "c(o)mpile", KEY_o, "cmdline(\"%r%q compile('make -k ')\", -2)",
   "m(a)rk_all_files", KEY_a, "mark_all_files()",
   "m(A)rk_all", KEY_A, "mark_all()",
   "(b)ack to previous direcotry", KEY_b, "dir_back()"
);

keycommand NOMETA, KEY_j,      "*",     "menu('jump')"
keycommand NOMETA, KEY_s,      "*",     "menu('sort')"
keycommand NOMETA, KEY_a,      "*",     "menu('file_attr')"
keycommand NOMETA, KEY_ATMARK, "*",     "menu('context-menu')"

keycommand NOMETA, KEY_h,            "*",        "cmdline('', 0)"
keycommand NOMETA, KEY_x,            "*",        "cmdline(' ./%f', 1)"
keycommand NOMETA, KEY_x,            ".mark",    "cmdline(' %m1', 1)"
keycommand NOMETA, KEY_COLON,        "*",        "shell('%q cd %D1; echo \"-+- %D1 -+-\"; ls -aF --color; $SHELL', \"#{ENV['SHELL']}\")"
keycommand NOMETA, KEY_z,            "*",        "xterm_next()"

keycommand NOMETA, KEY_CTRL_C,       "*",        "copy2()"
keycommand NOMETA, KEY_CTRL_X,       "*",        "cut()"
keycommand NOMETA, KEY_CTRL_V,       "*",        "past()"

###############################################################################
# extension settings
###############################################################################
defmenu("mp3-menu",
   "(m)pg123", KEY_m, "shell('mpg123 %f', '%f')",
   "m(p)layer", KEY_p, "shell('%q mplayer -zoom -framedrop %f', '%f')"
);

defmenu("gz-menu",
    "(t)ar xvfz", KEY_t, "shell('tar xvfz %f', '%f')",
    "(g)zip -d", KEY_g, "shell('gzip -d %f', '%f')"
);

defmenu("bz2-menu",
    "(t)ar xvfj", KEY_t, "shell('tar xvfj %f', '%f')",
    "(b)zip2 -d", KEY_b, "shell('bzip2d -d %f', '%f')"
);

keycommand NOMETA, KEY_ENTER, ".txt",     "shell('%q $PAGER %f', '%f')"
keycommand NOMETA, KEY_ENTER, ".html",    "shell('%q w3m %f', '%f')"
keycommand NOMETA, KEY_ENTER, ".gz",      "menu('gz-menu')"
keycommand NOMETA, KEY_ENTER, ".bz2",     "menu('bz2-menu')"
keycommand NOMETA, KEY_ENTER, ".tar",     "cmdline('tar xvf %f', 0)"
keycommand NOMETA, KEY_ENTER, ".tgz",     "cmdline('tar xvfz %f', 0)"
keycommand NOMETA, KEY_ENTER, ".lzh",     "cmdline('lha x %f', 0)"
keycommand NOMETA, KEY_ENTER, ".zip",     "cmdline('unzip %f', 0)"
keycommand NOMETA, KEY_ENTER, ".jpg",     "shell('%q display %f &', '%f')"
keycommand NOMETA, KEY_ENTER, ".jpeg",    "shell('%q display %f &', '%f')"
keycommand NOMETA, KEY_ENTER, ".gif",     "shell('%q display %f &', '%f')"
keycommand NOMETA, KEY_ENTER, ".bmp",     "shell('%q display %f &', '%f')"
keycommand NOMETA, KEY_ENTER, ".pnm",     "shell('%q display %f &', '%f')"
keycommand NOMETA, KEY_ENTER, ".png",     "shell('%q display %f &', '%f')"
keycommand NOMETA, KEY_ENTER, ".tif",     "shell('%q display %f &', '%f')"
keycommand NOMETA, KEY_ENTER, ".eps",     "shell('%q display %f &', '%f')"
keycommand NOMETA, KEY_ENTER, ".mp3",     "menu('mp3-menu')"
keycommand NOMETA, KEY_ENTER, ".wav",     "shell('%q esdplay %f', '%f')"
keycommand NOMETA, KEY_ENTER, ".WAV",     "shell('%q esdplay %f', '%f')"
keycommand NOMETA, KEY_ENTER, ".mid",     "shell('timidity %f', '%f')"
keycommand NOMETA, KEY_ENTER, ".mpg",     "shell('%q mplayer -zoom -framedrop %f', '%f')"
keycommand NOMETA, KEY_ENTER, ".mpeg",    "shell('%q mplayer -zoom -framedrop %f', '%f')"
keycommand NOMETA, KEY_ENTER, ".avi",     "shell('%q mplayer -zoom -framedrop %f', '%f')"
keycommand NOMETA, KEY_ENTER, ".MID",     "shell('timidity %f', '%f')"
keycommand NOMETA, KEY_ENTER, ".sh",      "cmdline('./%f', 0)"
keycommand NOMETA, KEY_ENTER, ".pl",      "cmdline('perl %f', 0)"
keycommand NOMETA, KEY_ENTER, ".py",      "cmdline('python %f', 0)"
keycommand NOMETA, KEY_ENTER, ".rb",      "cmdline('ruby %f', 0)"
keycommand NOMETA, KEY_ENTER, ".java",    "cmdline('%q javac %f 2>&1 | $PAGER', 0)"
keycommand NOMETA, KEY_ENTER, ".class",   "cmdline('java %X', 0)"
keycommand NOMETA, KEY_ENTER, ".c",       "cmdline('gcc -o %X %f; ./%X; rm -f %X', 0)"
keycommand NOMETA, KEY_ENTER, ".cpp",     "cmdline('g++ -o %X %f; ./%X; rm -f %X', 0)"
keycommand NOMETA, KEY_ENTER, ".asm",     "cmdline('%q nasm -f elf %f 2>&1 | $PAGER', 0)"
keycommand NOMETA, KEY_ENTER, "makefile", "cmdline('%q make -f %f  2>&1| $PAGER', -13)"
keycommand NOMETA, KEY_ENTER, "Makefile", "cmdline('%q make -f %f  2>&1| $PAGER', -13)"
keycommand NOMETA, KEY_ENTER, ".pdf",     "cmdline('%q acroread %f &', 0)"
keycommand NOMETA, KEY_ENTER, ".tex",     "cmdline('%q platex %f 2>&1 | $PAGER', 0)"
keycommand NOMETA, KEY_ENTER, ".dvi",     "cmdline('xdvi %f &', 0)"
keycommand NOMETA, KEY_ENTER, ".ps",      "cmdline('%q lpr %f -P', 0)"
keycommand NOMETA, KEY_ENTER, ".diff",    "cmdline('patch < %f', 0)"
keycommand NOMETA, KEY_ENTER, ".deb",     "cmdline('dpkg -i %f', 0)"
keycommand NOMETA, KEY_ENTER, ".nb",      "cmdline('mathematica %f &', 0)"
keycommand NOMETA, KEY_ENTER, ".s",       "cmdline('%q as -o %X.o %f | $PAGER', 0)"
keycommand NOMETA, KEY_ENTER, ".a",       "cmdline('%q nm %f 2>&1 | $PAGER', 0)"
keycommand NOMETA, KEY_ENTER, ".so",      "cmdline('%q nm -D %f 2>&1 | $PAGER', 0)"
keycommand NOMETA, KEY_ENTER, ".o",       "cmdline('%q nm %f 2>&1 | $PAGER', 0)"
keycommand NOMETA, KEY_ENTER, "core",     "cmdline('%Q rm %f', 0)"

keycommand NOMETA, KEY_ENTER, ".mark",    "cmdline('for i in %m1; do  ./$i; done', 18)"
keycommand NOMETA, KEY_ENTER, ".dir",     "dir_move(cursor_name())"
keycommand NOMETA, KEY_ENTER, ".execute", "cmdline('./%f ', 0)"
keycommand NOMETA, KEY_ENTER, "*",        "shell('%q $PAGER %f', '%f')"

###############################################################################
# alias
###############################################################################

malias "HOME", 0, "%r%Q dir_move(ENV['HOME'])"
malias "L", 0, "| $PAGER"
malias "CD", 1, "%r%Q dir_move('#0')"
malias "&", 0, "& %s"
malias "PSG", 1, "ps aux | grep #0 | grep -v grep"

###############################################################################
# option settings
###############################################################################
sort_name
#sort_name_reverse
#sort_ext
#sort_ext_reverse
#sort_size
#sort_size_reverse
#sort_time
#sort_time_reverse

#view_all
#view_onedir
#view_onedir2
view_onedir3
#view_onedir5

#view_nameonly

option_color false
option_individual_cursor true
#option_isearch_decision_way "mismatch_finish"
#option_isearch_decision_way "enter_decision"
option_isearch_decision_way "slash_toggle"
option_check_delete_file true
option_check_copy_file false
option_check_exit true
option_shift_isearch true
option_trashbox_name "mtrashbox"
option_gnu_screen false
option_pty false
option_remain_cursor true
option_auto_rehash false
option_select true
option_remain_marks true

option_bold_exe true
option_bold_dir true
option_color_mark MA_YELLOW
option_color_dir MA_CYAN
option_color_exe MA_RED
option_color_link MA_MAGENTA

# MA_WHITE, MA_BLUE, MA_CYAN, MA_GREEN, MA_YELLOW, MA_MAGENTA, MA_RED

option_kanjicode "euc"
# option_kanjicode "sjis"
# option_kanjicode "utf"

option_xterm false
set_xterm("xterm", "-T", "-e", "")

#set_xterm("Eterm", "-T", "-e", "--mencoding eucj --scrollbar false -F 7x14 -M k14 --cmod 100 --trans")

set_mask("{*,.*}")
set_mask_sdir("{*,.*}")

set_shell(ENV['SHELL'], `which #{ENV['SHELL']}`.chomp, "-c")

###############################################################################
# keymap
###############################################################################
keymap_up        27,   91,   65,   0,       0,     0
keymap_down      27,   91,   66,   0,       0,     0
keymap_right     27,   91,   67,   0,       0,     0
keymap_left      27,   91,   68,   0,       0,     0 
keymap_insert    27,   91,   50,   126,     0,     0
keymap_delete    127,  0,    0,    0,       0,     0
keymap_home      27,   79,   72,   0,       0,     0
keymap_end       27,   79,   70,   0,       0,     0
keymap_page_up   27,   91,   53,   126,     0,     0
keymap_page_down 27,   91,   54,   126,     0,     0
keymap_f1        27,   91,   49,   49,      126,   0
keymap_f2        27,   91,   49,   50,      126,   0
keymap_f3        27,   91,   49,   51,      126,   0
keymap_f4        27,   91,   49,   52,      126,   0
keymap_f5        27,   91,   49,   53,      126,   0
keymap_f6        27,   91,   49,   55,      126,   0
keymap_f7        27,   91,   49,   56,      126,   0
keymap_f8        27,   91,   49,   57,      126,   0
keymap_f9        27,   91,   50,   48,      126,   0
keymap_f10       27,   91,   50,   49,      126,   0
keymap_f11       27,   91,   50,   51,      126,   0
keymap_f12       27,   91,   50,   52,      126,   0

###############################################################################
# user definition
###############################################################################
defmenu("iso-menu",
   "d(v)d", KEY_v, "cmdline('growisofs -Z /dev/dvd=%f', 0)",
   "(c)d", KEY_c, "cmdline('cdrecord -v dev=0,0,0 speed=8 -pad -dao %f', 0)"
);

keycommand NOMETA, KEY_ENTER, ".iso",     "menu('iso-menu')"

defmenu("user_commands",
   "g(e)dit",  KEY_e, "shell('%q gedit %f &', '%f')",
   "mk(i)sofs", KEY_i, "cmdline('mkisofs -R -J -o %f.iso %f', 0)",
   "(c)dparanoia", KEY_c, "cmdline('cdparanoia -B 1-; for i in *; do gogo $i; done; rm *wav', 0)",
   "(r)eadcd to iso", KEY_r, "cmdline('readcd dev=0,0 f=', 0)",
   "dvd (b)ackup", KEY_b, "cmdline('dvdbackup -M -i/dev/dvd -o', 0)",
   "(f)tp", KEY_f, "cmdline(\"cd /home/daisuke/public_html/geocities; lftp -u daisuke530221jp ftp.geocities.jp -e 'mirror -R .'\", 0)",
   "(m)ail", KEY_m, "cmdline('cat %f | nkf -j | mail -s ', 0)",
   "c(d)rom mount", KEY_d, "cmdline('mount -t iso9660 /dev/sr0 %f', 0)",
   "/var/(l)og/message", KEY_l, "cmdline('tail -f /var/log/messages', 0)",
   "(k)abegami", KEY_k, "cmdline('Esetroot -scale %f', 0)"
);

keycommand NOMETA, KEY_u, "*", "menu('user_commands')"

