#
# zaw-src-memo
#
# zaw source for my memo archives
#
# zaw.zsh: https://github.com/nakamuray/zaw
#

MEMO_DIR="${HOME}/memo/archives"

function zaw-src-memo() {
    local fname title

    (cd "${MEMO_DIR}" && awk '/^=+$/ { getline; if ($0) print FILENAME "\t" $0; nextfile }' **/*.rst(On)) | \
        while read fname title; do
            candidates+=("${fname}")
            cand_descriptions+=("${(r:30:::::)fname} -- ${title}")
        done

    actions=("zaw-callback-view-memo" "zaw-callback-edit-memo" "zaw-callback-append-to-buffer")
    act_descriptions=("view memo" "edit memo" "append to edit buffer")
}

zaw-register-src -n memo zaw-src-memo

function zaw-callback-view-memo() {
    local f
    local -a args
    if (( $# > 1 )); then
        args=("${(@q)@}")
        f="{${(j:,:)args}}"
    else
        f="${(q)1}"
    fi
    BUFFER="vim -c ':cd ${MEMO_DIR}' -R ${MEMO_DIR}/${f}"
    accept-line
}

function zaw-callback-edit-memo() {
    local f
    local -a args
    if (( $# > 1 )); then
        args=("${(@q)@}")
        f="{${(j:,:)args}}"
    else
        f="${(q)1}"
    fi
    BUFFER="vim -c ':cd ${MEMO_DIR}' ${MEMO_DIR}/${f}"
    accept-line
}
