summaryrefslogtreecommitdiff
path: root/harpoon.kak
blob: eba256b20d484795bb97295e7474cc8de6d75591 (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
declare-option str-to-str-map harpoon_buffers

define-command harpoon-nav \
-params 1..2 \
-docstring "harpoon-nav <index> [<add-if-empty>]: Navigate to the harpoon at <index>, or optionally add it if it's empty" \
%{
    evaluate-commands %sh{
        selected=$1
        add=$2
        eval "set -- $kak_quoted_opt_harpoon_buffers"
        while [ $# -gt 0 ]; do
            index=${1%%=*}
            if [ "$index" = "$selected" ]; then
                echo "edit ${1#*=}"
                echo "echo 'Opened harpoon at index $selected'"
                return
            fi
            shift
        done

        if [ "$add" = true ]; then
            echo "harpoon-add $selected"
        else
            echo "fail 'No harpoon defined at index $selected'"
        fi
    }
}

define-command harpoon-add \
-params 1 \
-docstring "harpoon-add <index>: Harpoon the current file at <index>" \
%{
    evaluate-commands %sh{
        bufname=$(echo "$kak_bufname" | sed "s/@/@@/g")
        echo "set-option -add global harpoon_buffers %@$1=$bufname@"
        echo "echo 'Added current file at index $1'"
    }
}

define-command harpoon-remove \
-params ..1 \
-docstring "harpoon-remove [<index>]: Remove the harpoon at <index>, or all harpoons" \
%{
    evaluate-commands %sh{
        if [ -n "$1" ]; then
            echo "set-option -remove global harpoon_buffers '$1='"
            echo "echo 'Removed harpoon at index $1'"
        else
            echo "set-option global harpoon_buffers"
            echo "echo 'Removed all harpoons'"
        fi
    }
}

define-command harpoon-list \
-docstring "List harpoons in an infobox" \
%{
    evaluate-commands %sh{
        output=""
        eval "set -- $kak_quoted_opt_harpoon_buffers"
        while [ $# -gt 0 ]; do
            index=${1%%=*}
            filename=${1#*=}
            # Add newline if it's not the first one
            if [ -n "$output" ]; then
                output="$output
$index: $filename"
            else
                output="$index: $filename"
            fi
            shift
        done
        output=$(echo "$output" | sed "s/@/@@/g" | sort)

        if [ -n "$output" ]; then
            echo "info -title harpoons %@$output@"
        else
            echo "fail 'There are no harpoons'"
        fi
    }
}

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

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

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