write the script vimsnippet2yasnippet

 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
#!/bin/bash
export TTY

( hs "$(basename "$0")" "$@" "#" "<==" "$(ps -o comm= $PPID)" 0</dev/null ) &>/dev/null

while [ $# -gt 0 ]; do opt="$1"; case "$opt" in
    "") { shift; }; ;;
    -d) {
        dir="$2"
        shift
        shift
    }
    ;;

    *) break;
esac; done

stdin_exists() {
    ! [ -t 0 ]
}

if ! stdin_exists && test -f "$1"; then
    exec < "$1"
fi

# I don't think aatr is the right way to do this. It's kinda functional
# though, so it's acceptable
# aatr -k "\\nsnippet " "cat -n"

if ! test -d "$dir"; then
    dir="$(mktemp -t -d td_snippetsXXXXXX || echo /dev/null)"
fi

is_tty() {
    [[ -t 1 ]]
}

aatr -k "\\nsnippet " "vimsnippetbody2yasnippet -d $(aqf "$dir")" &>/dev/null

exec <&1

is_tty() {
    # If stout is a tty
    [[ -t 1 ]]
}

if is_tty; then
    zcd "$dir"
else
    find "$dir" -type f
fi

write the script vimsnippetbody2yasnippet

 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
#!/bin/bash
export TTY

while [ $# -gt 0 ]; do opt="$1"; case "$opt" in
    "") { shift; }; ;;
    -d) {
        dir="$2"
        shift
        shift
    }
    ;;

    *) break;
esac; done

IFS=$'\n' read -r line
IFS=' ' read -r -a words <<< "$line"

# allwords="${words[@]}"
key="${words[0]}"

key="$(printf -- "%s" "$key" | slugify)"

# desc="$(cmd "${words[@]:1}")" # shift
desc="${words[@]:1}" # shift

: ${desc:="$key"}


IFS= read -rd '' content < <(cat /dev/stdin | sed 's/^\t//')

IFS= read -r -d '' snip <<HEREDOC
# -*- mode: snippet -*-
# name: $desc
# group: vimsnippet
# key: $key
# --
$content
HEREDOC

if test -d "$dir"; then
    cd "$dir"
    printf -- "%s" "$snip" > "vs-$key"
else
    printf -- "%s" "$snip"
fi

Create the awk script aatr (awk-apply-to-record)

  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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
export TTY

( hs "$(basename "$0")" "$@" "#" "<==" "$(ps -o comm= $PPID)" 0</dev/null ) &>/dev/null

# This pipes a section of a document through an external command
# If you want to pipe a column or a match through an external command,
# then see aatc or aatm.

# TODO
# If no filter command is given, and INVERT != y, only the selected record will be printed to output
# If no filter command is given, and INVERT = y, everything except the selected record will be printetd to output
# If a filter command is given, and INVERT != y, only the selected record will be filtered
# If a filter command is given, and INVERT = y, everthing except the selected record will be filtered


# Example
# cat $HOME/source/git/mullikine/codelingo/tenets/codelingo/jenkinsx/intformat/codelingo.yaml | awk-apply-to-record "tenets:" "nl" | v
# Print only ifconfig records that match
# ifconfig 2>&1 | awk-apply-to-record '\n\n' 'sed -z -n /wlp3s0/p' | v


# Something to note is that RECORD == 2 will start with the first match of the record separator

# Example: The record separator for the current record is also filtered
# ifconfig 2>&1 | awk-apply-to-record -R 3 'addr' 'sed -u s/[a-z]/_/g' | v
# Unless you specify -irs
# ifconfig 2>&1 | awk-apply-to-record -irs -R 2 'Link' 'sed -u s/[a-z]/_/g' | v

SELECTED_RECORD_ID=
while [ $# -gt 0 ]; do opt="$1"; case "$opt" in
    -R) {
        SELECTED_RECORD_ID="$2"
        shift
        shift
    }
    ;;

    # TODO
    -v) {
        INVERT=y
        shift
    }
    ;;

    -k|-irs) {
        # Do not put RS into the filter
        IGNORE_RS=y
        shift
    }
    ;;

    -r|-rrs) {
        # Remove the RS at the end
        REMOVE_RS=y
        shift
    }
    ;;

    *) break;
esac; done

rsep="$1"
# rsepe="$rsep"
rsepe="$(p "$rsep" | esc \\)"
filter_cmd="$2"


sn="$(basename "$0")"
test "$sn" = "awk-apply-to-records" && SELECTED_RECORD_ID=
test "$sn" = "atrs" && SELECTED_RECORD_ID=



# BEGIN { PROCINFO[cmd, "pty"] = 1 }
# fflush(cmd);  system("");
# system("sleep 1");
# sub(/a/, "_", $0);


# equivalent
# printf "%s", RS |& cmd; printf "%s", $0 |& cmd;
# printf "%s%s", RS, $0 |& cmd;


read -r -d '' awkscript <<'HEREDOC'
{
    # print NR
    if (r == "" || NR == r) {
        if (NR == 1) {
            printf "%s", $0 |& cmd;
        } else {
            if (irs == "y") {
                printf "%s", RS
                printf "%s", $0 |& cmd;
            } else {
                printf "%s%s", RS, $0 |& cmd;
            }
        }

        close(cmd, "to");

        # This is needed because getline will not overwrite if nothing is read
        $0 = "";

        cmd |& getline $0;

        # if (NR == 1) {
        #     sub(/tenets:/, "", $0);
        # } else {
        #     sub(/tenets:/, RS, $0);
        # }

        fflush(cmd);
        close(cmd);

        # TODO: If the returned cmd is empty, the record must've been
        # cleared. Consider then not printing the record at all. By
        # default, awk adds a record separator.

    } else {
        if (NR > 1) {
            print RS
        }
    }

    #if (length($0) == 0) {
    #    print NR
    #    RS="";
    #    $0 = "";
    #    next;
    #} else {
    #    print "good"
    #    print NR
    #    print; system("");
    #}

    print; system("");
}
HEREDOC

# { printf "%s", $0; system(""); }

# lit "$awkscript"
# exit 0


# echo "sed -z \"s/__RS_PLACEHOLDER__/$rsep/\""
# exit 0

gawk -v irs="$IGNORE_RS" -v r="$SELECTED_RECORD_ID" -v RS="$rsep" -v ORS="" -v cmd="$filter_cmd | sed -z 's/$rsepe/__RS_PLACEHOLDER__/'" -v FS=" " -v OFS=" " "$awkscript" | sed -z "s/__RS_PLACEHOLDER__/$sep/g" | {
    if test "$REMOVE_RS" = "y"; then
        sed -z "s/$rsep//g"
    else
        cat
    fi
}

# works
# gawk -F '\n' -v RS="\n- name" -v cmd="nl" '{ printf "%s%s", RS, $0 |& cmd; close(cmd, "to"); cmd |& getline $0; fflush(cmd); close(cmd) } { print; system(""); }'

# placeholder version
# gawk -v RS="$1" -v ORS="" -v cmd="$2" -v FS=" " -v OFS=" " '{ print "__RS_PLACEHOLDER___" |& cmd; printf "%s", $0 |& cmd; close(cmd, "to"); cmd |& getline $0; fflush(cmd); close(cmd) } { print; system(""); }' | sed "s/__RS_PLACEHOLDER___/$1/"
# one-liner -- good
# gawk -v RS="$1" -v ORS="$1" -v cmd="$2" -v FS=" " -v OFS=" " '{ printf "%s", $0 |& cmd; close(cmd, "to"); cmd |& getline $0; fflush(cmd); close(cmd) } { print; system(""); }'

Demonstration

1
vimsnippet2yasnippet $MYGIT/honza/vim-snippets/snippets/vim.snippets | v

Lets use some of those snippets

It’s not a perfect snippet translation, but it’s a start.

The pipeline works, though.

1
2
3
4
let a="hi"
for 1 in 2
VISUAL
endfor

asciinema recording