summaryrefslogtreecommitdiff
path: root/harpoon.kak
blob: e10c2e18bd45c73a04f1624c5f4b566490441f0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
require-module luar

declare-option str-to-str-map harpoon_buffers

define-command harpoon-nav -params 1 -docstring "Navigate to the file at the specified index" %{
    lua %arg{1} %opt{harpoon_buffers} %{
        -- Parse arguments
        local to_index = args()
        local buffers = {}
        for i = 2, #arg do
            local index, bufname = string.match(arg[i], "(%d+)=(.*)")
            index = tonumber(index)
            buffers[index] = bufname
        end

        if buffers[to_index] then
            kak.edit(buffers[to_index])
            kak.echo("Opened file at index "..to_index)
        else
            kak.echo("-markup", "{Error}No file defined at index "..to_index)
        end
    }
}

define-command harpoon-add -params ..1 -docstring "Add the current file to the list at the index or at the end" %{
    lua %arg{1} %val{bufname} %opt{harpoon_buffers} %{
        local index, bufname = args()
        index = tonumber(index) or nil
        -- Index is optional
        if not index then
            -- Find the lowest available index
            local buffers = {}
            for i = 3, #arg do
                local index, bufname = string.match(arg[i], "(%d+)=(.*)")
                index = tonumber(index)
                buffers[index] = bufname
            end
            -- This will handle gaps in the list as well, since # will return the first contiguous section
            index = #buffers + 1
        end

        kak.set_option("-add", "global", "harpoon_buffers", index.."="..bufname)
        kak.echo("Added current file at index "..index)
    }
}

define-command harpoon-remove -params ..1 -docstring "Remove the file at the specified index" %{
    lua %arg{1} %{
        local index = args()
        index = tonumber(index) or nil
        if index then
            kak.set_option("-remove", "global", "harpoon_buffers", index.."=")
        else
            kak.set_option("global", "harpoon_buffers")
        end
    }
}

define-command harpoon-add-or-nav -params 1 -docstring "Add or navigate to the file at the specified index" %{
    lua %arg{1} %opt{harpoon_buffers} %{
        local buffers = {}
        for i = 2, #arg do
            local index, bufname = string.match(arg[i], "(%d+)=(.*)")
            index = tonumber(index)
            buffers[index] = bufname
        end

        local index = args()
        if buffers[index] then
            kak.harpoon_nav(index)
        else
            kak.harpoon_add(index)
        end
    }
}

define-command harpoon-list -docstring "List harpoon files in an infobox" %{
    lua %opt{harpoon_buffers} %{
        if #arg == 0 then
            kak.echo("-markup", "{Error}There are no harpooned files")
        else
            local output = {}
            for i, file in pairs(arg) do
                output[i] = string.gsub(file, "=", ": ")
            end
            kak.info("-title", "harpoons", table.concat(output, "\n"))
        end
    }
}

declare-user-mode harpoon
map global harpoon a ": harpoon-add<ret>" -docstring "Add file to end"
map global harpoon l ": harpoon-list<ret>" -docstring "List files"
map global harpoon 1 ": harpoon-add 1<ret>" -docstring "Add file to 1"
map global harpoon 2 ": harpoon-add 2<ret>" -docstring "Add file to 2"
map global harpoon 3 ": harpoon-add 3<ret>" -docstring "Add file to 3"
map global harpoon 4 ": harpoon-add 4<ret>" -docstring "Add file to 4"
map global harpoon 5 ": harpoon-add 5<ret>" -docstring "Add file to 5"
map global harpoon 6 ": harpoon-add 6<ret>" -docstring "Add file to 6"
map global harpoon 7 ": harpoon-add 7<ret>" -docstring "Add file to 7"
map global harpoon 8 ": harpoon-add 8<ret>" -docstring "Add file to 8"
map global harpoon 9 ": harpoon-add 9<ret>" -docstring "Add file to 9"

define-command harpoon-add-bindings -docstring "Add convenient keybindings for navigating harpoons" %{
    map global normal <c-1> ": harpoon-nav 1<ret>"
    map global normal <c-2> ": harpoon-nav 2<ret>"
    map global normal <c-3> ": harpoon-nav 3<ret>"
    map global normal <c-4> ": harpoon-nav 4<ret>"
    map global normal <c-5> ": harpoon-nav 5<ret>"
    map global normal <c-6> ": harpoon-nav 6<ret>"
    map global normal <c-7> ": harpoon-nav 7<ret>"
    map global normal <c-8> ": harpoon-nav 8<ret>"
    map global normal <c-9> ": harpoon-nav 9<ret>"

    map global user h ": enter-user-mode harpoon<ret>" -docstring "harpoon..."
}