* Improving spelling correction prompt to generate aliases for future use
@ 2007-10-21 3:39 Gwern Branwen
2007-10-21 18:04 ` Peter Stephenson
0 siblings, 1 reply; 4+ messages in thread
From: Gwern Branwen @ 2007-10-21 3:39 UTC (permalink / raw)
To: zsh-users
[-- Attachment #1: Type: text/plain, Size: 1742 bytes --]
'lo everyone.
My desire is this: I have a function, 'typo', which is defined thusly:
function typo () {
(typo_darcs "$@" &); #Subshell to insulate environment
alias $1=$2; } #now enable the typo for the current shell.
function typo_darcs () {
#So often I make little stupid typos while firing away
#in the shell. So, I whipped up a quick shell script which
#reads the two arguments provided, and constructs the appropriate
#echo command to create an alias that solves that typo.
cd; #we need to be in a repository directory, which is ~/ since .ztypos is there
echo alias $1='"'$2'"' >> ~/.ztypos; #It goes typo wrong-command right-command
tail -n 1 ~/.ztypos & #Verify that .ztypos was written, with the right thing.
darcs record --compress --skip-long-comment --all --patch-name=".ztypos: alias $1="$2"" .ztypos; }
It basically makes an alias to fix a typo I've just made. This was very useful in Bash since there was no spelling correction. Now, I've only just recently switched from using Bash to using Zsh, and a little experience with spelling correction has convinced it that it would be excellent if, when I make a typo, I could get correction but also somehow have 'typo' be automatically run.
Looking into it, though, it is not very clear how to do this. I though one could perhaps modify SPROMPT to execute 'typo %R %r', but there doesn't seem to be any escape to run arbitrary shell commands. Precmd() also doesn't seem to work, since I don't see anyway for it to get access to %R, %r, or to know there is a misspelling going on at all. I asked around on #zsh, but no one seemed to really know and someone suggested emailing this list might help.
--
gwern
digicash RRF bemd Beach HRT GSM Blowpipe INR Zen AC
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Improving spelling correction prompt to generate aliases for future use
2007-10-21 3:39 Improving spelling correction prompt to generate aliases for future use Gwern Branwen
@ 2007-10-21 18:04 ` Peter Stephenson
2007-10-21 21:45 ` Bart Schaefer
0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2007-10-21 18:04 UTC (permalink / raw)
To: Gwern Branwen; +Cc: zsh-users
Gwern Branwen wrote:
> Looking into it, though, it is not very clear how to do this. I though
> one could perhaps modify SPROMPT to execute 'typo %R %r', but there
> doesn't seem to be any escape to run arbitrary shell commands.
You could "setopt promptsubst" and have typo output the prompt (as well as
whatever else it's doing) then set
SPROMPT='`typo`'
--
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Improving spelling correction prompt to generate aliases for future use
2007-10-21 18:04 ` Peter Stephenson
@ 2007-10-21 21:45 ` Bart Schaefer
2007-10-21 22:22 ` Gwern Branwen
0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2007-10-21 21:45 UTC (permalink / raw)
To: zsh-users
On Oct 21, 7:04pm, Peter Stephenson wrote:
}
} You could "setopt promptsubst" and have typo output the prompt (as
} well as whatever else it's doing)
I don't think that'll do what Gwern is after. He wants to feed the
results (original and suggested replacement) of spelling correction
to "typo" as command-line arguments.
E.g. he wants to do
typo ${(%):-%R} ${(%):-%r}
but he can't because prompt substitution in parameters doesn't have
access to the SPROMPT escapes.
The following works for simple commands but not loop constructs or other
compound commands:
accept-line() {
emulate -L zsh
local -a words
words=(${(z)BUFFER})
typeset -g ACCEPTED_CMD=$words[1]
zle .accept-line "$@"
}
zle -N accept-line
preexec() {
emulate -L zsh
local -a words
words=(${(z)1})
if [[ $words[1] != $ACCEPTED_CMD ]]
then
typo $ACCEPTED_CMD $words[1]
fi
}
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Improving spelling correction prompt to generate aliases for future use
2007-10-21 21:45 ` Bart Schaefer
@ 2007-10-21 22:22 ` Gwern Branwen
0 siblings, 0 replies; 4+ messages in thread
From: Gwern Branwen @ 2007-10-21 22:22 UTC (permalink / raw)
To: Bart Schaefer, Zsh-user
[-- Attachment #1: Type: text/plain, Size: 1617 bytes --]
On 2007.10.21 14:45:11 -0700, Bart Schaefer <schaefer@brasslantern.com> scribbled 0 lines:
> On Oct 21, 7:04pm, Peter Stephenson wrote:
> }
> } You could "setopt promptsubst" and have typo output the prompt (as
> } well as whatever else it's doing)
>
> I don't think that'll do what Gwern is after. He wants to feed the
> results (original and suggested replacement) of spelling correction
> to "typo" as command-line arguments.
>
> E.g. he wants to do
>
> typo ${(%):-%R} ${(%):-%r}
>
> but he can't because prompt substitution in parameters doesn't have
> access to the SPROMPT escapes.
Yes, after quite a bit of frustrated and bemused experimentation I discovered I could only get '%R'/'%r' and not their values (which still amazes me just a little).
> The following works for simple commands but not loop constructs or other
> compound commands:
>
> accept-line() {
> emulate -L zsh
> local -a words
> words=(${(z)BUFFER})
> typeset -g ACCEPTED_CMD=$words[1]
> zle .accept-line "$@"
> }
> zle -N accept-line
>
> preexec() {
> emulate -L zsh
> local -a words
> words=(${(z)1})
> if [[ $words[1] != $ACCEPTED_CMD ]]
> then
> typo $ACCEPTED_CMD $words[1]
> fi
> }
Yes, that seems to work fine! Thanks, I hadn't realized that the pre* commands could be helpful in this case. In fact, this actually seems to be an even better solution compared to running out of the prompt, because a 'typo' in the prompt wouldn't know whether the user had answered yes or no - while this preexec thing has a conditional on just that.
--
gwern
Freeh Abbas warfare V fuses crypto AOL Mexico SNT friends
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2007-10-21 22:59 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-21 3:39 Improving spelling correction prompt to generate aliases for future use Gwern Branwen
2007-10-21 18:04 ` Peter Stephenson
2007-10-21 21:45 ` Bart Schaefer
2007-10-21 22:22 ` Gwern Branwen
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).