From owner-dev-commits-src-all@freebsd.org Wed Sep 22 20:25:40 2021 Return-Path: <owner-dev-commits-src-all@freebsd.org> Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 771DA671348; Wed, 22 Sep 2021 20:25:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4HF8tr2cpxz3HsD; Wed, 22 Sep 2021 20:25:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 361311DE38; Wed, 22 Sep 2021 20:25:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 18MKPeWA098593; Wed, 22 Sep 2021 20:25:40 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 18MKPe1i098592; Wed, 22 Sep 2021 20:25:40 GMT (envelope-from git) Date: Wed, 22 Sep 2021 20:25:40 GMT Message-Id: <202109222025.18MKPe1i098592@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Piotr Pawel Stefaniak <pstef@FreeBSD.org> Subject: git: c866d0c798a2 - main - sh: improve command completion MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: pstef X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: c866d0c798a20b8f0a92df524f4ddd0d81511c88 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository <dev-commits-src-all.freebsd.org> List-Unsubscribe: <https://lists.freebsd.org/mailman/options/dev-commits-src-all>, <mailto:dev-commits-src-all-request@freebsd.org?subject=unsubscribe> List-Archive: <http://lists.freebsd.org/pipermail/dev-commits-src-all/> List-Post: <mailto:dev-commits-src-all@freebsd.org> List-Help: <mailto:dev-commits-src-all-request@freebsd.org?subject=help> List-Subscribe: <https://lists.freebsd.org/mailman/listinfo/dev-commits-src-all>, <mailto:dev-commits-src-all-request@freebsd.org?subject=subscribe> X-List-Received-Date: Wed, 22 Sep 2021 20:25:40 -0000 The branch main has been updated by pstef: URL: https://cgit.FreeBSD.org/src/commit/?id=c866d0c798a20b8f0a92df524f4ddd0d81511c88 commit c866d0c798a20b8f0a92df524f4ddd0d81511c88 Author: Piotr Pawel Stefaniak <pstef@FreeBSD.org> AuthorDate: 2021-09-22 16:23:29 +0000 Commit: Piotr Pawel Stefaniak <pstef@FreeBSD.org> CommitDate: 2021-09-22 20:23:32 +0000 sh: improve command completion When there are many matches, find the longest common substring starting from the beginning of each command and use that to replace input. As an example: on my system, llv<tab> will be autocompleted to llvm- and another <tab> will print all matching llvm commands. --- bin/sh/histedit.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/bin/sh/histedit.c b/bin/sh/histedit.c index 37b62cef5746..4e82f7497850 100644 --- a/bin/sh/histedit.c +++ b/bin/sh/histedit.c @@ -596,7 +596,7 @@ static char const char *dirname; char **matches = NULL; size_t i = 0, size = 16, uniq; - size_t curpos = end - start; + size_t curpos = end - start, lcstring = -1; if (start > 0 || memchr("/.~", text[0], 3) != NULL) return (NULL); @@ -652,11 +652,20 @@ out: if (i > 1) { qsort_s(matches + 1, i, sizeof(matches[0]), comparator, (void *)(intptr_t)curpos); - for (size_t k = 2; k <= i; k++) - if (strcmp(matches[uniq] + curpos, matches[k] + curpos) == 0) + for (size_t k = 2; k <= i; k++) { + const char *l = matches[uniq] + curpos; + const char *r = matches[k] + curpos; + size_t common = 0; + + while (*l != '\0' && *r != '\0' && *l == *r) + (void)l++, r++, common++; + if (common < lcstring) + lcstring = common; + if (*l == *r) free(matches[k]); else matches[++uniq] = matches[k]; + } } matches[uniq + 1] = NULL; /* @@ -668,7 +677,12 @@ out: * string in matches[0] which is the reason to copy the full name of the * only match. */ - matches[0] = strdup(uniq == 1 ? matches[1] : text); + if (uniq == 1) + matches[0] = strdup(matches[1]); + else if (lcstring != (size_t)-1) + matches[0] = strndup(matches[1], curpos + lcstring); + else + matches[0] = strdup(text); if (matches[0] == NULL) { for (size_t k = 1; k <= uniq; k++) free(matches[k]);