zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Felix Rosencrantz <f_rosencrantz@yahoo.com>, zsh-workers@sunsite.dk
Subject: Re: Working with the historywords special parameter
Date: Tue, 28 Aug 2001 15:16:49 +0000	[thread overview]
Message-ID: <1010828151649.ZM2643@candle.brasslantern.com> (raw)
In-Reply-To: <20010828061920.57606.qmail@web10406.mail.yahoo.com>

On Aug 27, 11:19pm, Felix Rosencrantz wrote:
}
} --- Bart Schaefer <schaefer@brasslantern.com> wrote:
} >Have you tried using $history instead of $historywords?  You want the
} >word that comes immediately after $words[CURRENT-1] in every history
} >line that contains $word[CURRENT-1], right?
} >
} >    local w p h r
} >    w=${(q)words[CURRENT-1]}
} >    p=$'\0'$w$'\0'
} >    h=$'\0'${(pj:\0:)${(z)history[(R)*$w*]}}
} >    r=( ${${(ps:\1:)h//$~p/$'\1'}%%$'\0'*} )
} >    compadd -a r
} >
} >This assumes there are no literal NUL or ctrl-A characters in the
} >history, but that seems a pretty safe assumption.
} 
} The other problem is that it didn't know about line boundaries. So if
} the searched for word was at the end of the line, then it would return
} a match for the previous command, which is wrong.

Stick a $'\1' on the end of each line so that $'\0'$w$'\0' won't match
the last word:

    local w p h r
    w=${(q)words[CURRENT-1]}
    p=$'\0'$w$'\0'
    #
    h=( ${^history[(R)*$w*]}$'\1' )
    h=$'\0'${(pj:\0:)${(z)h}}
    r=( ${${(ps:\1:)h//$~p/$'\1'}%%$'\0'*} )
    #
    compadd -a r

To see which is faster, you can replace the three lines between the `#'
with:

    r=()
    for h in $history[(R)*$w*]
    do
	h=$'\0'${(pj:\0:)${(z)h}}
	r[$#r+1]=( ${${(ps:\1:)h//$~p/$'\1'}%%$'\0'*} )
    done

I suspect the first way is faster because it does fewer, though larger,
memory allocations.

The loop body could be one line if not for the need to prepend $'\0' to
the join.

} Also, I was wondering if the (z) modifier applied to the elements of
} history would always return the same results as found by historywords?

No, it won't.  $historywords is more like $=history, but it isn't quite
that either -- the rules for what is a "word" in $historywords are a bit
odd.  E.g., quoted strings are broken up at whitespace, but =(...) are
kept as single words.  Also, it appears that $historywords has the words
from the current input line, whereas $history doesn't get that line added
until after the command has been parsed -- but I'm not sure about that.

I think ${(z)history} is more accurate for purposes of completion.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


  reply	other threads:[~2001-08-28 15:20 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-24  6:54 Felix Rosencrantz
2001-08-24  8:16 ` Sven Wischnowsky
2001-08-24 17:05   ` Bart Schaefer
2001-08-28  6:19     ` Felix Rosencrantz
2001-08-28 15:16       ` Bart Schaefer [this message]
2001-08-28  6:24     ` Seg Fault in paramsubst() Felix Rosencrantz
2001-08-28  8:35       ` PATCH: " Sven Wischnowsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1010828151649.ZM2643@candle.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=f_rosencrantz@yahoo.com \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).