From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29683 invoked by alias); 31 May 2012 20:27:34 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 30496 Received: (qmail 6352 invoked from network); 31 May 2012 20:27:32 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED,RCVD_IN_DNSWL_LOW, T_DKIM_INVALID,T_TO_NO_BRKTS_FREEMAIL autolearn=no version=3.3.2 Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.google.com designates 209.85.217.171 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:x-mailer:in-reply-to:references; bh=d4F8eHeFVJBN0Pz7lK/k1TReQUCQCU11OeVOYiCs9DA=; b=L52GzljkPo6hAAnkq1f6mpyh9dTllE+ZMpvYdSDVScUCRJS2KLnO6qZgFiPVw5Lt6f ccL0Nc0MY4AkuyqtGFPfDrm9WRb+HtOBm8jhyDhUnI0H4v1zRuYwEVUJtYwRANzg8slk +Iev2eZKq92ib/zCUaBDAmW0ivpA9uS79uN7PPKHFTn+ri873Stfrk2u/o2SLnGZ00F4 Em5MIug58+3QeTVC0hGHnzWbLl7vXWo7oAHUZPVwDJZP7Gm6TVmbK3F52wnOba+/Dry9 jH5usYOK/k0Vvax1uHHCAvylS5fxRNwXHTCLeCfYz6Fa85EuNpgF64RjTVvTCQEfvlwd BYqA== From: Mikael Magnusson To: zsh-workers@zsh.org Subject: PATCH: Parse argument to %F and %K as prompt sequences Date: Thu, 31 May 2012 21:56:56 +0200 Message-Id: <1338494216-6970-1-git-send-email-mikachu@gmail.com> X-Mailer: git-send-email 1.7.10.GIT In-Reply-To: <1336071602-31699-1-git-send-email-mikachu@gmail.com> References: <1336071602-31699-1-git-send-email-mikachu@gmail.com> Well, nobody objected so here's a version with de-duplicated code. The previous patch broke %K, but it works now (passed 1 for is_fg on both paths). --- Src/prompt.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/Src/prompt.c b/Src/prompt.c index e51ce24..95a7d49 100644 --- a/Src/prompt.c +++ b/Src/prompt.c @@ -232,6 +232,33 @@ promptexpand(char *s, int ns, char *rs, char *Rs, unsigned int *txtchangep) return new_vars.buf; } +/* Parse the argument for %F and %K */ +static int +parsecolorchar(int arg, int is_fg) +{ + if (bv->fm[1] == '{') { + char *ep; + bv->fm += 2; /* skip over F{ */ + if ((ep = strchr(bv->fm, '}'))) { + char oc = *ep, *col, *coll; + *ep = '\0'; + /* expand the contents of the argument so you can use + * %v for example */ + coll = col = promptexpand(bv->fm, 0, NULL, NULL, NULL); + *ep = oc; + arg = match_colour((const char **)&coll, is_fg, 0); + free(col); + bv->fm = ep; + } else { + arg = match_colour((const char **)&bv->fm, is_fg, 0); + if (*bv->fm != '}') + bv->fm--; + } + } else + arg = match_colour(NULL, 1, arg); + return arg; +} + /* Perform %- and !-expansion as required on a section of the prompt. The * * section is ended by an instance of endchar. If doprint is 0, the valid * * % sequences are merely skipped over, and nothing is stored. */ @@ -494,13 +521,7 @@ putpromptchar(int doprint, int endchar, unsigned int *txtchangep) tsetcap(TCUNDERLINEEND, TSC_PROMPT|TSC_DIRTY); break; case 'F': - if (bv->fm[1] == '{') { - bv->fm += 2; - arg = match_colour((const char **)&bv->fm, 1, 0); - if (*bv->fm != '}') - bv->fm--; - } else - arg = match_colour(NULL, 1, arg); + arg = parsecolorchar(arg, 1); if (arg >= 0 && !(arg & TXTNOFGCOLOUR)) { txtchangeset(txtchangep, arg & TXT_ATTR_FG_ON_MASK, TXTNOFGCOLOUR); @@ -515,13 +536,7 @@ putpromptchar(int doprint, int endchar, unsigned int *txtchangep) set_colour_attribute(TXTNOFGCOLOUR, COL_SEQ_FG, TSC_PROMPT); break; case 'K': - if (bv->fm[1] == '{') { - bv->fm += 2; - arg = match_colour((const char **)&bv->fm, 0, 0); - if (*bv->fm != '}') - bv->fm--; - } else - arg = match_colour(NULL, 0, arg); + arg = parsecolorchar(arg, 0); if (arg >= 0 && !(arg & TXTNOBGCOLOUR)) { txtchangeset(txtchangep, arg & TXT_ATTR_BG_ON_MASK, TXTNOBGCOLOUR); -- 1.7.10.GIT