#!/bin/fish set -q XDG_CONFIG_HOME; or set XDG_CONFIG_HOME ~/.config set -q BOOKMARK_PATH; or set BOOKMARK_PATH $XDG_CONFIG_HOME/bookmarks.md set LINK_PATTERN '\[(.*?)\]\((.*?)\)' function _help echo "Usage bm ...args" echo "commands:" echo ' open [-c ]' end function list argparse -n open c/category= -- $argv _filter_category $_flag_category < $BOOKMARK_PATH | _filter_name $argv end function open if test 0 -eq (count $argv) echo search mode else list $argv | _open_urls end end function _filter_category -a category if not count $argv >/dev/null cat - return end set -e output_indent while read LINE if set heading (string match -r '^(#+) (.*)$' $LINE) set heading_indent (string length $heading[2]) set heading_title $heading[-1] if test -z "$output_indent" if test -z "$output_indent" && string match "*$heading_title*" $category >/dev/null set output_indent $heading_indent end else if test "$heading_indent" -le "$output_indent" set -e output_indent else echo $LINE end end else if test -n "$output_indent" echo $LINE end end end function _filter_name if not count $argv >/dev/null cat - return end while read LINE if set link (string match -r '\[(.*?)\]\((.*?)\)' $LINE) if count $argv >/dev/null && not contains $link[2] $argv continue end echo $LINE end end end function _open_urls set -q BROWSER; or set BROWSER xdg-open while read LINE if set link (string match -r '\[(.*?)\]\((.*?)\)' $LINE) $BROWSER $link[3] & disown end end end switch $argv[1] case open list $argv[1..-1] case '*' _help end