vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 0272907ab291458167c18717d8d0511d77108c69 parent 6b5f9c7ba7c1b9d505b016d79cec0b751910af15 Author: Quentin Rameau <quinq@fifth.space> Date: Wed, 22 Mar 2023 23:54:42 +0100 vis-complete: Fix commandline options handling Diffstat:
| M | man/vis-complete.1 | | | 21 | +++++++++------------ |
| M | vis-complete | | | 31 | +++++++++++++++++++++++++------ |
2 files changed, 34 insertions(+), 18 deletions(-)
diff --git a/man/vis-complete.1 b/man/vis-complete.1 @@ -1,4 +1,4 @@ -.Dd January 15, 2017 +.Dd March 21, 2023 .Dt VIS-COMPLETE 1 .Os Vis VERSION . @@ -8,14 +8,12 @@ . .Sh SYNOPSIS .Nm vis-complete -.Op Fl -file -.Op Fl -word +.Op Fl -file | -word .Op Ar -- -.Op Ar pattern +.Ar pattern .Pp .Nm vis-complete -.Fl h | -.Fl -help +.Fl h | -help . .Sh DESCRIPTION .Nm vis-complete @@ -36,17 +34,16 @@ This passes .Ar pattern to .Nm find -to obtain a list of matching file names. +to obtain a list of matching file names +(this is the default). .It Fl -word This reads standard input to obtain a list of lines matching .Ar pattern . .It Fl - -If this token is encountered before the first non-option argument, -all following arguments will be treated as pattern, -even if they would otherwise be valid command-line options. +An argument following this token will be treated as pattern, +even if it would otherwise be a valid command-line option. .Pp -If encountered after the first non-option argument, -or after a previous instance of +If encountered after a previous instance of .Li -- it is treated as a pattern. .It Ar pattern diff --git a/vis-complete b/vis-complete @@ -1,33 +1,52 @@ #!/bin/sh set -e +usage() { + printf '%s\n' "usage: $(basename "$0") [--file|--word] pattern" \ + " $(basename "$0") -h|--help" +} + basic_regex_quote() { printf "%s" "$1" | sed 's|[\\.*^$[]|\\&|g'; } glob_quote () { printf "%s" "$1" | sed 's|[\\?*[]]|\\&|g'; } -PATTERN="" COMPLETE_WORD=0 FIND_FILE_LIMIT=1000 while [ $# -gt 0 ]; do case "$1" in - -h|--help) - echo "usage: $(basename "$0") [-h] [--file|--word] [pattern]" - exit 0; - ;; --file) + COMPLETE_WORD=0 shift ;; --word) COMPLETE_WORD=1 shift ;; + --) + shift + break + ;; + -h|--help) + usage + exit 0 + ;; + -*) + usage + exit 1 + ;; *) - PATTERN="$1" break ;; esac done +if [ $# -ne 1 ]; then + usage + exit 1 +fi + +PATTERN="$1" + if [ $COMPLETE_WORD = 1 ]; then tr -cs '[:alnum:]_' '\n' | grep "^$(basic_regex_quote "$PATTERN")." |