-
-w_refuri="$(HTML "$REQUEST_URI")"
-
-w_tags="$(find "$_DATA/" -path '*/.index/meta' -print0 \
- | xargs -r0 cut -f4 \
- | cut -d= -f2- |tr , '\n' | sort -u)"
-w_tags="$(UNSTRING "$w_tags")"
-w_tagcategories="$(printf %s "$w_tags" |cut -sd: -f1 |sort -u)"
-
-CHECK(){ [ "$1" = "$2" ] && printf checked; }
+. "$_EXEC/db_meta.sh"
+
+w_refuri="$(URL "$PATH_INFO")?$(HTML "$QUERY_STRING")"
+
+w_str_s="$(STRING "$SEARCH")"
+w_str_f="$(STRING "$FILTER")"
+
+c_tags="$_DATA/.index/tags.cache"; c_tagcategories="$_DATA/.index/tagcategories.cache"
+if [ ! -s "$c_tags" -o ! -s "$c_tagcategories" ] \
+ || [ "$(find "$_DATA/" -path '*/.index/meta' -newer "$c_tags")" ]
+ then
+ w_tags="$(
+ list_meta |sed -E '
+ s;^.*\ttags=([^\t]*)\t.*$;\1;;
+ s;,;\n;g;
+ ' \
+ | sort |uniq -c |sort -rn |sed -E 's;^ *[0-9]+ ;;;' \
+ | UNSTRING | HTML \
+ | sed -E 's;
\;;\n;g; s;\n+;\n;g;'
+ )"
+ w_tagcategories="$(printf %s "$w_tags" | sed -rn '/:/s;^-?([^:]+):.*$;\1;p' |sort -u )"
+ printf %s "$w_tags" >"$c_tags"
+ printf %s "$w_tagcategories" >"$c_tagcategories"
+else
+ w_tags="$(cat "$c_tags")"
+ w_tagcategories="$(cat "$c_tagcategories")"
+fi
+
+
+[ "$ORDER" = Name ] && w_coname=checked
+[ "$ORDER" = Date ] && w_codate=checked
+[ "$ORDER" = Length ] && w_colength=checked
+[ "$ORDER" = Group ] && w_cogroup=checked
+
+w_bmname=
+w_bmname(){
+ [ "$w_bmname" ] || w_bmname="$(
+ bm="$_DATA/.index/bookmarks"
+ name="$(grep -m1 -aF " search=${w_str_s} filter=${w_str_f}${CR}" "$bm" 2>&-)"
+
+ if [ "$name" ]; then
+ printf '%s' "$name" |cut -f1 |UNSTRING |HTML
+ else
+ printf '%s\t%s' "$SEARCH" "$FILTER" \
+ | sed -r '/^\t$/{ s;\t;All;; q;}
+ /.*\t$/{ s;\t$;;; q;}
+ /^\t.*/{ s;^\t;;;
+ :x; s;(^|[~^|])([^|^~:]+):;\1;; tx;
+ s;\^; and ;g; s;\|;,;g; s;~;not ;g; q;}' \
+ | HTML
+ fi
+ )"
+ printf '%s' "$w_bmname"
+}