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

if ENV['EDITOR'] == nil || ENV['EDITOR'] == ""
    if File.exist?("/usr/bin/vim") || File.exist?("/usr/local/bin/vim") 
        ENV['EDITOR'] = "vim"
    else
        ENV['EDITOR'] = "vi"
    end
end

if ENV['SHELL'] == nil || ENV['SHELL'] == "" 
    if File.exist?("/bin/zsh") || File.exist?("/usr/bin/zsh") || File.exist?("/usr/local/bin/zsh") 
        ENV['SHELL'] = "zsh"
    elsif File.exist?("/bin/bash") || File.exist?("/usr/bin/bash") || File.exist?("/usr/local/bin/bash") 
        ENV['SHELL'] = "bash"
    elsif File.exist?("/bin/tcsh") || File.exist?("/usr/bin/tcsh") || File.exist?("/usr/local/bin/tcsh") 
        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 glob_mark(glob)
    garray = Dir[glob]

    i = 0
    while i < cursor_max
        if file_name(i) != "." && file_name(i) != ".."
            garray.each {|item|
                if item == file_name(i)
                    mark_on(i)
                    break
                end
            }
        end

        i = i + 1
    end
end

def mark_all_clear()
    i = 0
    while i < cursor_max
        mark_off(i)
        i = i + 1
    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 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

###############################################################################
# hook
###############################################################################
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
###############################################################################
if `uname -s`.chomp == "FreeBSD"
    CP_OPTION = "-Rp"
else                        # cygwin || Linux
    CP_OPTION = "-Rpd"
end

keycommand NOMETA, KEY_UP,           "*",        "cursor_move(cursor()-1)"
keycommand NOMETA, KEY_DOWN,         "*",        "cursor_move(cursor()+1)"

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
            if $option_left_right_dir_up
                dir_move('..')
            end
        else
            cursor_left()
        end
    end
end

keycommand NOMETA, KEY_LEFT,         "*",        "key_left()"

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
            if $option_left_right_dir_up
                dir_move('..')
            end
        else
            cursor_right()
        end
    end
end

keycommand NOMETA, KEY_RIGHT,        "*",        "key_right()"

keycommand NOMETA, KEY_PAGEUP,       "*",        "afu_type_pageup()"
keycommand NOMETA, KEY_PAGEDOWN,     "*",        "afu_type_pagedown()"
keycommand NOMETA, KEY_CTRL_P,       "*",        "cursor_move(cursor()-1)"
keycommand NOMETA, KEY_CTRL_N,       "*",        "cursor_move(cursor()+1)"
keycommand NOMETA, KEY_CTRL_B,       "*",        "key_left()"
keycommand NOMETA, KEY_CTRL_F,       "*",        "key_right()"
keycommand NOMETA, KEY_CTRL_D,       "*",        "afu_type_pagedown()"
keycommand NOMETA, KEY_CTRL_U,       "*",        "afu_type_pageup()"
keycommand NOMETA, KEY_CTRL_A,       "*",        "cursor_move(0)"
keycommand NOMETA, KEY_CTRL_E,       "*",        "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_META_LEFT,    "*",        "dir_back()"
keycommand NOMETA, KEY_BACKSLASH,    "*",        "dir_move('/')"
keycommand NOMETA, KEY_TILDA,        "*",        "dir_move('~')"
keycommand NOMETA, KEY_SPACE,        "*",        "mark2(cursor); cursor_move(cursor + 1)"
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_new2()"
keycommand NOMETA, KEY_CTRL_W,       "*",        "dir_close()"
keycommand NOMETA, KEY_QMARK,        "*",        "help()"
keycommand NOMETA, KEY_ESCAPE,       "*",        "mf_exit()"
keycommand NOMETA, KEY_SLASH,        "*",        "isearch()"

def key_copy_p()
    if is_one_dir
        cmdline('%Q%r copy_p(adir_path2(), adir_mark(), %D1s)', -2)
    else 
        if $option_afu_copy
            if marking
                copy_p(adir_path2(), adir_mark(), sdir_path2())
            end
        else
            cmdline('%Q%r copy_p(adir_path2(), adir_mark(), %D2s)', -2)
        end
    end
end

keycommand NOMETA, KEY_c,      "*",     "key_copy_p"


keycommand META, KEY_c,      "*",     "if is_one_dir() then cmdline('cp #{CP_OPTION} %m1 %D1s', -1) else cmdline('cp #{CP_OPTION} %m1 %D2s', -1) end"

def key_move()
    if is_one_dir 
        cmdline('%Q%r move(adir_path2(), adir_mark(), %D1s)', -2)
    else 
        if $option_afu_copy
            if marking
                move(adir_path2(), adir_mark(), sdir_path2())
            end
        else
            cmdline('%Q%r move(adir_path2(), adir_mark(), %D2s)', -2)
        end
    end
end

keycommand NOMETA, KEY_m,      "*",     "key_move"
keycommand META, KEY_m,        "*",     "if is_one_dir() then cmdline('mv %m1 %D1s', -1) else cmdline('mv %m1 %D2s', -1) end"

def key_delete()
    if !$option_afu_copy || $option_afu_copy && marking
        remove(adir_path2(), adir_mark())
    end
end

keycommand NOMETA, KEY_d,      "*",     "key_delete"

def key_trashbox()
    if !$option_afu_copy || $option_afu_copy && marking
        trashbox(adir_path2(), adir_mark())
    end
end

keycommand NOMETA, KEY_D,      "*",     "key_trashbox"

keycommand META, KEY_d,      "*",     "cmdline('rm -R %m1', 0)"

keycommand NOMETA, KEY_e,      "*",     "shell('%q $EDITOR %f', '%f')"
keycommand NOMETA, KEY_E,      "*",     "shell('%q%t $EDITOR %f', '%f')"
keycommand NOMETA, KEY_v,      "*",     "shell('%q $PAGER %f', '%f')"
keycommand NOMETA, KEY_V,      "*",     "shell('%q%t $PAGER %f', '%f')"

def key_rename(fname)
    if File.exist?(adir_path2 + fname)
        message("file exists")
        return
    end

    begin
        File.rename(cursor_name, fname)
    rescue
        message("rename failed")
    end
end

keycommand NOMETA, KEY_r,      "*",     "if !marking then cmdline('%Q%r key_rename %f', -1) end"
keycommand NOMETA, KEY_b,      ".mark", "cmdline('for i in %m1; do cp #{CP_OPTION} $i $i; done', -6)"
keycommand NOMETA, KEY_b,      "*",     "cmdline('%Q%r copy(adir_path2(), adir_mark(), %f)', -2)"
keycommand NOMETA, KEY_p,      "*",     "if is_one_dir then cmdline('tar cvfz %X.tgz %m1', -9) else cmdline('tar cvfz %D2s%X.tgz %m1', -9) end"
keycommand NOMETA, KEY_P,      "*",     "cmdline('tar cvfz %X.tgz %m1', -9)"

def make_dir(fname)
    if File.exist?(adir_path2 + fname)
        message("file exists")
        return
    end

    begin
        Dir.mkdir(adir_path2 + fname, 0755)
    rescue
        message("make_dir() failed")
    end
end

keycommand NOMETA, KEY_k,      "*",     "cmdline('%r%Q make_dir \\'\\'', -1)"

def make_new_file(fname)
    if File.exist?(adir_path2 + fname)
        message("file exists")
        return
    end

    begin 
        f = File.new(adir_path2 + fname,File::CREAT|File::TRUNC|File::WRONLY)
        f.chmod(0644)
        f.close
    rescue
        message("make_new_file() failed")
    end
end

keycommand NOMETA, KEY_n,      "*",     "cmdline('%r%Q make_new_file \\'\\'', -1)"

def make_new_link(src, dest)
    if File.exist?(dest)
        message("file exists")
        return
    end

    begin 
        File.symlink(src, dest)
    rescue
        message("make_new_link() failed")
    end
end

def make_new_link_mark(src, dest)
    if !File.stat(dest).directory?
        message("distination is not directory")
        return
    end

    begin
        src.each{|path|
            fname = File.basename(path)
            File.symlink(path, dest + fname)
        }
    rescue
        message("make_new_link() failed")
    end
end

def key_link()
    if is_one_dir 
        if !marking
            cmdline('%r%Qmake_new_link(%F, \'\')', -2)
        end
    else 
        if $option_afu_copy
            if marking
                make_new_link_mark(adir_mark_fullpath(), sdir_path2())
                reread
                mark_clear
            end
        else
            if marking
               cmdline('%r%Qmake_new_link_mark(adir_mark_fullpath(), %D2s)', -2)
            else
                cmdline('%r%Qmake_new_link(%F, %D2s + %f)', -2)
            end
        end
    end
end

keycommand NOMETA, KEY_l,      "*",     "key_link"
keycommand NOMETA, KEY_L,      "*",     "if !marking then cmdline('%r%Qmake_new_link(%F, \\'\\')', -2) end"


defmenu("gz-menu",
    "(t)ar xvfz", KEY_t, "if is_one_dir then cmdline('tar xvfz %f -C %D1s', -1) else cmdline('tar xvfz %f -C %D2s', -1) end",
    "(g)zip -d", KEY_g, "shell('gzip -d %F', '%f')");

defmenu("gz-menu2",
    "(t)ar xvfz", KEY_t, "cmdline('tar xvfz %f -C %D1s', -1)",
    "(g)zip -d", KEY_g, "shell('gzip -d %F', '%f')");

defmenu("bz2-menu",
    "(t)ar xvfj", KEY_t, "if is_one_dir then cmdline('tar xvfj %f -C %D1s', -1) else cmdline('tar xvfj %f -C %D2s', -1) end",
    "(b)zip2 -d", KEY_b, "shell(bzip2 -d %f', '%f')");

defmenu("bz2-menu2",
    "(t)ar xvfj", KEY_t, "cmdline('tar xvfj %f -C %D1s', -1)",
    "(b)zip2 -d", KEY_b, "shell('bzip2 -d %f', '%f')");

keycommand NOMETA, KEY_u, ".gz",      "menu('gz-menu')"
keycommand NOMETA, KEY_U, ".gz",      "menu('gz-menu2')"
keycommand NOMETA, KEY_u, ".bz2",     "menu('bz2-menu')"
keycommand NOMETA, KEY_U, ".bz2",     "menu('bz2-menu2')"
keycommand NOMETA, KEY_u, ".tar",     "if is_one_dir then cmdline('tar xvf %f -C %D1s', -1) else cmdline('tar xvf %f -C %D2s', -1) end"
keycommand NOMETA, KEY_U, ".tar",     "cmdline('tar xvf %f -C %D1s', -1)"
keycommand NOMETA, KEY_u, ".tgz",     "if is_one_dir then cmdline('tar xvfz %f -C %D1s', -1) else cmdline('tar xvfz %f -C %D2s', -1) end"
keycommand NOMETA, KEY_U, ".tgz",     "cmdline('tar xvfz %f -C %D1s', -1)"
keycommand NOMETA, KEY_u, ".lzh",     "if is_one_dir then cmdline('cd %D1s; lha x %F', adir_path2.size() + 5) else cmdline('cd %D2s; lha x %F', sdir_path2.size() + 5) end"
keycommand NOMETA, KEY_U, ".lzh",     "cmdline('cd %D1s; lha x %F', adir_path2.size() + 5)"
keycommand NOMETA, KEY_u, ".zip",     "if is_one_dir then cmdline('unzip %f -d %D1s', -1) else cmdline('unzip %f -d %D2s', -1) end"
keycommand NOMETA, KEY_U, ".zip",     "cmdline('unzip %f -d %D1s', -1)"

keycommand NOMETA, KEY_g,      "*",     "cmdline(\"%q grep '' * -n 2> /dev/null | $PAGER\", 10)"
keycommand NOMETA, KEY_g,      ".mark",     "cmdline(\"%q grep '' %m1 -n 2> /dev/null | $PAGER\", 10)"

if `uname -s`.chomp() == "Linux"
    keycommand NOMETA, KEY_G,      "*",     "cmdline(\"%q grep -R '' * -n 2> /dev/null | $PAGER\", 13)"
    keycommand NOMETA, KEY_G,      ".mark",     "cmdline(\"%q grep -R '' %m1 -n 2> /dev/null | $PAGER\", 13)"
else
    keycommand NOMETA, KEY_G,      "*",     "cmdline(\"%q find . | xargs grep -n '' 2> /dev/null | $PAGER\", 28)"
end

keycommand META, KEY_g,      "*",     "cmdline(\"%q mgrep \\\"grep -n '' *\\\"\", 20)"
keycommand NOMETA, KEY_STAR,   "*",     "cmdline(\"%r%Q%M glob_mark('')\", -2)"
keycommand NOMETA, KEY_i,      ".dir",  "cmdline('du -sk %f', 7)"
keycommand NOMETA, KEY_i,      "*",     "cmdline('file %m1', 0)"
keycommand NOMETA, KEY_j,      "*",     "cmdline(\"%r%Q dir_move %D1s\", -1)"
keycommand NOMETA, KEY_J,      "*",     "cmdline(\"%r%Q dir_move ''\", -1)"
keycommand NOMETA, KEY_o,      "*",     "if !is_one_dir then dir_move(sdir_path2()) end"
keycommand NOMETA, KEY_O,      "*",     "if !is_one_dir then sdir_move(adir_path2()) end"
keycommand NOMETA, KEY_w,      "*",     "if !is_one_dir then tmp = adir_path2(); dir_move(sdir_path2()); sdir_move(tmp) end"
keycommand NOMETA, KEY_q,      "*",     "mf_exit()"

keycommand NOMETA, KEY_f,            "*",        "isearch()"
keycommand NOMETA, KEY_F,            "*",        "cmdline(\"%q find . -name '' -follow 2> /dev/null | $PAGER\", 18)"

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 (u)ser", KEY_u, "sort_user()",
   "sort (U)ser reverse", KEY_U, "sort_user_reverse()",
   "sort (g)roup", KEY_g, "sort_group()",
   "sort (G)roup reverse", KEY_G, "sort_group_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()",
   "view toggle (c)olor", KEY_c, "view_color()",
   "view (m)ask", KEY_m, "if is_remain_marks() then cmdline(\"%Q%r set_mask('{*,.*}')\", -2); else cmdline(\"%Q%r%M set_mask('{*,.*}')\", -2); end");

keycommand NOMETA, KEY_s,      "*",     "menu('sort')"

defmenu("file_attr",
   "ch(m)od (permission)", KEY_m, "cmdline('chmod  %m1', 7)",
   "ch(o)wn (owner)", KEY_o, "cmdline('chown  %m1', 7)",
   "ch(g)rp (group)", KEY_g, "cmdline('chgrp  %m1', 7)",
   "ch(M)od -R (permission)", KEY_M, "cmdline('chmod -R  %m1', 10)",
   "ch(O)wn -R (owner)", KEY_O, "cmdline('chown -R  %m1', 10)",
   "ch(G)rp -R (group)", KEY_G, "cmdline('chgrp -R  %m1', 10)",
   "m(a)ttr (permission and date)", KEY_a, "shell('%q mattr %m1', 'mattr')");

keycommand NOMETA, KEY_a,      "*",     "menu('file_attr')"

defmenu("file_attr_sudo",
   "sudo ch(m)od (permission)", KEY_m, "cmdline('sudo chmod  %m1', 12)",
   "sudo ch(o)wn (owner)", KEY_o, "cmdline('sudo chown  %m1', 12)",
   "sudo ch(g)rp (group)", KEY_g, "cmdline('sudo chgrp  %m1', 12)",
   "sudo ch(M)od -R (permission)", KEY_M, "cmdline('sudo chmod -R  %m1', 15)",
   "sudo ch(O)wn -R (owner)", KEY_O, "cmdline('sudo chown -R  %m1', 15)",
   "sudo ch(G)rp -R (group)", KEY_G, "cmdline('sudo chgrp -R  %m1', 15)",
   "sudo m(a)ttr (permission and date)", KEY_a, "shell('sudo mattr %m1', 'mattr')");

defmenu("sudo-gz-menu",
    "(t)ar xvfz", KEY_t, "if is_one_dir then cmdline('sudo tar xvfz %f -C %D1s', -1) else cmdline('sudo tar xvfz %f -C %D2s', -1) end",
    "(g)zip -d", KEY_g, "shell('sudo gzip -d %F', '%f')");

defmenu("sudo-gz-menu2",
    "(t)ar xvfz", KEY_t, "cmdline('sudo tar xvfz %f -C %D1s', -1)",
    "(g)zip -d", KEY_g, "shell('sudo gzip -d %F', '%f')");

defmenu("sudo-bz2-menu",
    "(t)ar xvfj", KEY_t, "if is_one_dir then cmdline('sudo tar xvfj %f -C %D1s', -1) else cmdline('sudo tar xvfj %f -C %D2s', -1) end",
    "(b)zip2 -d", KEY_b, "shell('sudo bzip2 -d %f', '%f')");

defmenu("sudo-bz2-menu2",
    "(t)ar xvfj", KEY_t, "cmdline('sudo tar xvfj %f -C %D1s', -1)",
    "(b)zip2 -d", KEY_b, "shell('sudo bzip2 -d %f', '%f')");


def key_sudo_unpack()
    ext = cursor_ext()
    if ext == "gz" 
        menu('sudo-gz-menu')
    elsif ext == "bz2"
        menu('sudo-bz2-menu')
    elsif ext == "tar"
        if is_one_dir 
            cmdline('sudo tar xvf %f -C %D1s', -1) 
        else 
            cmdline('sudo tar xvf %f -C %D2s', -1)
        end
    elsif ext == "tgz"
        if is_one_dir
            cmdline('sudo tar xvfz %f -C %D1s', -1)
        else
            cmdline('sudo tar xvfz %f -C %D2s', -1)
        end
    elsif ext == "lzh"
        if is_one_dir
            cmdline('cd %D1s; sudo lha x %F', adir_path2.size() + 5)
        else 
            cmdline('cd %D2s; sudo lha x %F', sdir_path2.size() + 5)
        end
    elsif ext == "zip"
        if is_one_dir
            cmdline('sudo unzip %f -d %D1s', -1)
        else
            cmdline('sudo unzip %f -d %D2s', -1)
        end
    end
end

def key_sudo_Unpack()
    ext = cursor_ext()
    
    if ext == "gz"
        menu('sudo-gz-menu2')
    elsif ext == "bz2"
        menu('sudo-bz2-menu2')
    elsif ext == "tar"
        cmdline('sudo tar xvf %f -C %D1s', -1)
    elsif ext == "tgz"
        cmdline('sudo tar xvfz %f -C %D1s', -1)
    elsif ext == "zip"
        cmdline('sudo unzip %f -d %D1s', -1)
    elsif ext == "lzh"
        cmdline('cd %D1s; sudo lha x %F', adir_path2.size() + 5)
    end
end

defmenu("sudo-menu",
   "sudo (c)opy", KEY_c,   "if is_one_dir() then cmdline('sudo cp #{CP_OPTION} %m1 %D1s', -1) else cmdline('sudo cp #{CP_OPTION} %m1 %D2s', -1) end",
   "sudo (m)v", KEY_m,      "if is_one_dir() then cmdline('sudo mv %m1 %D1s', -1) else cmdline('sudo mv %m1 %D2s', -1) end",
   "sudo (d)elete",  KEY_d, "cmdline('sudo rm -R %m1', 0)",
   "sudo (r)ename", KEY_r, "cmdline('sudo mv %f %f', -1)",
   "sudo (a)ttr menu",  KEY_a, "menu('file_attr_sudo')",
   "sudo (e)dit", KEY_e,   "shell('%q sudo $EDITOR %f', '%f')",
   "sudo (E)dit", KEY_E,   "shell('%t%q sudo $EDITOR %f', '%f')",
   "sudo (v)iewer", KEY_v,     "shell('%q sudo $PAGER %f', '%f')",
   "sudo (V)iewer", KEY_V,     "shell('%t%q sudo $PAGER %f', '%f')",
   "sudo (p)ack", KEY_p, "if is_one_dir then cmdline('sudo tar cvfz %X.tgz %m1', -9) else cmdline('sudo tar cvfz %D2s%X.tgz %m1', -9) end", 
   "sudo (P)ack", KEY_P,     "cmdline('sudo tar cvfz %X.tgz %m1', -9)",
   "sudo (u)npack", KEY_u,     "key_sudo_unpack()",
   "sudo (U)npack", KEY_U,     "key_sudo_Unpack()",
   "sudo ma(k)e dir", KEY_k,   "cmdline(\"sudo mkdir -p ''\", -1)", 
   "sudo (n)ew file", KEY_n,   "cmdline(\"sudo touch ''\", -1)",
   "sudo make (l)ink", KEY_l,  "if is_one_dir() then cmdline('sudo ln -s %M1 %D1s', -1) else cmdline('sudo ln -s %M1 %D2s', -1) end",
   "sudo make (L)ink", KEY_L,  "cmdline('sudo ln -s %F %D1s', -1)",
   "sudo (g)rep", KEY_g,       "cmdline(\"%q sudo grep '' * -n 2> /dev/null | $PAGER\", 15)",
   "sudo (G)rep", KEY_G,       "cmdline(\"%q%t sudo grep '' * -n 2> /dev/null | $PAGER\", 17)",
   "sudo e(x)ecute", KEY_x, "cmdline('sudo  ./%f', 6)",
   "sudo e(X)ecute", KEY_X, "cmdline('%t sudo  ./%f', 9)",
   "sudo (b)ackup", KEY_b, "cmdline('sudo cp #{CP_OPTION} %f %f', -1)",
   "sudo run shell(:)", KEY_COLON, "shell('%q echo \"-+- \"%D1\" -+-\"; ls -aFG; sudo $SHELL', \"#{ENV['SHELL']}\")", 
   "sudo s(h)ell", KEY_h,   "cmdline('sudo ', 0)",
   "sudo s(H)ell with xterminal", KEY_H,   "cmdline('%t sudo ', 0)");

keycommand NOMETA, KEY_ATMARK,       "*",     "menu('sudo-menu')"

defmenu("extra-menu",
   "re(h)ash",  KEY_h, "rehash", 
   "c(o)mpile", KEY_o, "cmdline(\"%q mcompile 'make -k '\", -1)",
   "(k)ill pty", KEY_k, "kill_pty(select_pty('kill_pty'))",
   "m(a)rk_all_files", KEY_a, "mark_all_files()",
   "m(A)rk_all", KEY_A, "mark_all()",
   "mark_all_cl(e)ar", KEY_e, "mark_all_clear()",
   "(b)ack to previous direcotry", KEY_b, "dir_back()");

keycommand NOMETA, KEY_UNDERBAR,     "*",     "menu('extra-menu')"

keycommand NOMETA, KEY_h,            "*",        "cmdline('', 0)"
keycommand NOMETA, KEY_H,            "*",        "cmdline('%t ', 0)"
keycommand NOMETA, KEY_x,            ".mark",    "cmdline('for i in %m1; do  ./$i; done', 18)"
keycommand NOMETA, KEY_X,            ".mark",    "cmdline('%t for i in %m1; do  ./$i; done', 21)"
keycommand NOMETA, KEY_x,            "*",        "cmdline(' ./%f', 1)"
keycommand NOMETA, KEY_X,            "*",        "cmdline('%t  ./%f', 4)"
keycommand NOMETA, KEY_COLON,        "*",        "shell('%q echo \"-+- \"%D1\" -+-\"; ls -aFG; $SHELL', \"#{ENV['SHELL']}\")"
keycommand NOMETA, KEY_z,            "*",        "xterm_next()"
keycommand NOMETA, KEY_Z,            "*",        "xterm_toggle()"

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

keycommand NOMETA, KEY_0,            "*",        "restore_pty(select_pty('select pty'))"
keycommand NOMETA, KEY_1,            "*",        "restore_pty(1)"
keycommand NOMETA, KEY_2,            "*",        "restore_pty(2)"
keycommand NOMETA, KEY_3,            "*",        "restore_pty(3)"
keycommand NOMETA, KEY_4,            "*",        "restore_pty(4)"
keycommand NOMETA, KEY_5,            "*",        "restore_pty(5)"
keycommand NOMETA, KEY_6,            "*",        "restore_pty(6)"
keycommand NOMETA, KEY_7,            "*",        "restore_pty(7)"
keycommand NOMETA, KEY_8,            "*",        "restore_pty(8)"
keycommand NOMETA, KEY_9,            "*",        "restore_pty(9)"

keycommand META, KEY_0,            "*",        "dir_up()"
keycommand META, KEY_1,            "*",        "tab_up(0)"
keycommand META, KEY_2,            "*",        "tab_up(1)"
keycommand META, KEY_3,            "*",        "tab_up(2)"
keycommand META, KEY_4,            "*",        "tab_up(3)"
keycommand META, KEY_5,            "*",        "tab_up(4)"
keycommand META, KEY_6,            "*",        "tab_up(5)"
keycommand META, KEY_7,            "*",        "tab_up(6)"
keycommand META, KEY_8,            "*",        "tab_up(7)"
keycommand META, KEY_9,            "*",        "tab_up(8)"

###############################################################################
# keymap
###############################################################################
keymap   KEY_UP,        27,   91,   65,   -1,      -1,     -1, -1,-1,-1,-1
keymap   KEY_DOWN,      27,   91,   66,   -1,      -1,     -1, -1,-1,-1,-1
keymap   KEY_RIGHT,     27,   91,   67,   -1,      -1,     -1, -1,-1,-1,-1
keymap   KEY_LEFT,      27,   91,   68,   -1,      -1,     -1, -1,-1,-1,-1
keymap   KEY_INSERT,    27,   91,   50,   126,     -1,     -1, -1,-1,-1,-1
keymap   KEY_DELETE,    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_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_PAGEUP,    27,   91,   53,   126,     -1,     -1, -1,-1,-1,-1
keymap   KEY_PAGEDOWN,  27,   91,   54,   126,     -1,     -1, -1,-1,-1,-1
keymap   KEY_F1,        27,   91,   49,   49,      126,    -1, -1,-1,-1,-1
keymap   KEY_F2,        27,   91,   49,   50,      126,    -1, -1,-1,-1,-1
keymap   KEY_F3,        27,   91,   49,   51,      126,    -1, -1,-1,-1,-1
keymap   KEY_F4,        27,   91,   49,   52,      126,    -1, -1,-1,-1,-1
keymap   KEY_F5,        27,   91,   49,   53,      126,    -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_UP,        27,   79,   65,   -1,       -1,    -1, -1,-1,-1,-1
keymap   KEY_DOWN,      27,   79,   66,   -1,       -1,    -1, -1,-1,-1,-1
keymap   KEY_RIGHT,     27,   79,   67,   -1,       -1,    -1, -1,-1,-1,-1
keymap   KEY_LEFT,      27,   79,   68,   -1,       -1,    -1, -1,-1,-1,-1
keymap   KEY_HOME,      27,   91,   49,   126,      -1,    -1, -1,-1,-1,-1
keymap   KEY_END,       27,   91,   52,   126,      -1,    -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

###############################################################################
# alias
###############################################################################
malias "&", 0, "&%s"
