あめだまふぁくとりー

Boost.Graphとかできますん

vfiler.vimの乗り換えました

Neovimの設定をLuaで一新したので,使用していたプラグインも見直しを行いました. ファイラーはこれまでDefxを使用していましたが,以下の理由でvfiler.vimを採用しました.

  • Luaで設定が書けること(Luaの設定ファイル内でVimScriptを書く方法がよくわかっていないので).
  • 依存関係がNeovim内で閉じていること(Defxはpynvimが必要だったりして,新しく環境を構築するときにいろいろと面倒だったため).
  • 2画面ファイラーとして使えること(VimFilerの使いやすさが忘れられないため).

機能的に足りないところもあるのですが,view:get_item()を使うと現在のカーソルのエンティティの情報が取れるので,これでいろいろマッピングを追加しました(ドキュメントには書いていないのでもしかしらた非推奨の使い方かもしれません).

ファイル名の表示

サイドバーとして表示している場合,横幅の関係上ファイル名が省略されて表示されてしまいます.なので,Ctrl+gで現在のカーソルのエンティティの名前を表示するようにしました.

            ["<C-g>"] = function(vfiler, context, view)
                local item = view:get_item()
                print(item.name)
            end,

ファイルかディレクトリかでの処理の分岐

デフォルトだと,ディレクトリにしか効果のないアクションもあるので,カーソルのエンティティがファイルかディレクトリかを判断してアクションを変えるようにしました.

            ["o"] = function(vfiler, context, view)
                local vfiler_action = require("vfiler/action")
                local item = view:get_item()
                if item.type == "directory" then
                    if item.opened == true then
                        vfiler_action.close_tree_or_cd(vfiler, context, view)
                    else
                        vfiler_action.open_tree(vfiler, context, view)
                        -- 開いたツリーの中にカーソルが移動するので元に戻す
                        vfiler_action.move_cursor_up(vfiler, context, view)
                    end
                else
                    vfiler_action.open_by_vsplit(vfiler, context, view)
                end
            end,

ターミナルとの連携

vfiler.viimからターミナルを開いて,選択中のエンティティを入力済みにするようにしました. これにはview:get_item()の代わりにview:selected_items()を使用しています.

local open_vfiler_terminal = function(dirpath, args)
    local termname = "term://" .. vim.fn.getbufinfo("%")[1].name
    local termbufs = vim.fn.getbufinfo(termname)
    local job_id = nil
    if next(termbufs) == nil then
        vim.cmd [[
            botright new
            resize 15
        ]]
        job_id = vim.fn.termopen({vim.opt.shell:get()}, { cwd = dirpath })
        vim.cmd("keepalt file " .. termname)
    else
        local termbufinfo = termbufs[1]
        if termbufinfo.hidden == 0 then
            -- 表示済みの場合は表示中のWindowにフォーカスする
            local wids = vim.fn.win_findbuf(termbufinfo.bufnr)
            if next(wids) ~= nil then
                vim.fn.win_gotoid(wids[1])
            end
        else
            -- 非表示の場合は画面分割で開く
            vim.cmd("botright sbuffer " .. termbufinfo.bufnr)
            vim.cmd [[resize 15]]
        end
        job_id = termbufinfo.variables.terminal_job_id
        -- VFilerで開いているディレクトリに移動する
        vim.fn.chansend(
            job_id,
            vim.api.nvim_replace_termcodes("<C-U>", true, true, true)
            .. " cd " .. vim.fn.shellescape(dirpath)
            .. vim.api.nvim_replace_termcodes("<CR>", true, true, true))
    end
    if args ~= "" then
        vim.fn.chansend(
            job_id,
            args .. vim.api.nvim_replace_termcodes("<C-A>", true, true, true))
    end
    vim.cmd [[startinsert]]
end
            ["H"] = function(vfiler, context, view)
                local selected_items = view:selected_items()
                local args = ''
                for _, item in pairs(selected_items) do
                    if item.selected then
                        args = args .. ' ' .. vim.fn.shellescape(item.path)
                    end
                end
                vfiler_action.clear_selected_all(vfiler, context, view)
                open_vfiler_terminal(selected_items[1].parent.path, args)
            end,