zsh-users
 help / color / mirror / code / Atom feed
* spell check on the command line
@ 2014-10-24 10:54 Eric Smith
  2014-10-24 15:36 ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Eric Smith @ 2014-10-24 10:54 UTC (permalink / raw)
  To: Zsh Users

Dear zshellers

I make frequent typos (which I like to blame on my apple
keyboard). These are either in commands themselves or in long
filenames concatenated with underscores as arguments to commands.

(Of course autocorrect in a command could be dangerous, but then
so could a typo).

But especially interesting is a spell check for my
long_filenames_with_useful_tags_as_metadata.pdf

How could I implement and autocorrect function that is run when I
press enter and on suspected spell error beeps and
guesses what I meant and would give me a chance to review the
correction?

-- 
Eric Smith


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: spell check on the command line
  2014-10-24 10:54 spell check on the command line Eric Smith
@ 2014-10-24 15:36 ` Bart Schaefer
  2014-10-24 21:19   ` Eric Smith
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2014-10-24 15:36 UTC (permalink / raw)
  To: Zsh Users

On Oct 24, 12:54pm, Eric Smith wrote:
}
} How could I implement and autocorrect function that is run when I
} press enter and on suspected spell error beeps and
} guesses what I meant and would give me a chance to review the
} correction?

Have you tried

	setopt CORRECT_ALL

??  If so, what does it not do that you want done differently?


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: spell check on the command line
  2014-10-24 15:36 ` Bart Schaefer
@ 2014-10-24 21:19   ` Eric Smith
  2014-10-25  7:24     ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Eric Smith @ 2014-10-24 21:19 UTC (permalink / raw)
  To: zsh-users


Thanks Bart

Bart Schaefer wrote on Fri-24-Oct 14  5:36PM
> On Oct 24, 12:54pm, Eric Smith wrote:
> Have you tried
> 
> 	setopt CORRECT_ALL
> 
> ??  If so, what does it not do that you want done differently?

~>  setopt CORRECTALL
~> cat /ettc/issue
zsh: correct '/ettc/issue' to '/etc/issue' [nyae]? y
Ubuntu 14.04.1 LTS \n \l

~> touch this_settting_doess_not_yet_check_the_spelling_of_these_tags_in_a_filename.txt
~> 

-- 
Eric Smith


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: spell check on the command line
  2014-10-24 21:19   ` Eric Smith
@ 2014-10-25  7:24     ` Bart Schaefer
  2014-10-25  8:17       ` Eric Smith
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2014-10-25  7:24 UTC (permalink / raw)
  To: zsh-users

On Oct 24, 11:19pm, Eric Smith wrote:
}
} ~> setopt CORRECTALL
} ~> touch this_settting_doess_not_yet_check_the_spelling_of_these_tags_in_a_filename.txt

Oh, so you want a regular spelling dictionary applied in some way, to
substrings within each command-line argument.  Or perhaps only to
substrings within file names, but since it's not possible to tell in
general which arguments are file names (a shortcoming correctall has
already), it's effectively the same problem.

Let's ignore the complications of shell keywords like "elif", complex
commands like loops, and multi-line buffers.  Your basic choices are:

- override the accept-line widget; or
- create (or add to if already using one) a zle-line-finish widget; or
- attempt to handle it all in prexec.

The simplest one is zle-line-finish so I'll do a quick example of that
here.  If you do it in preexec it's more difficult to implement the
(a)bort and (e)dit cases.

I'll assume that anything matching [[:punct:]] is taken as a word break,
and also that a program or function "suggest" exists that will spit out
pairs of wrong words and their replacments (unlike "spell" which emits
only the wrong words).

    zle-line-finish() {
      local spelt misspelt nyae
      print -R ${BUFFER//[[:punct:][:digit:]]/$'\n'} | suggest |
      while read misspelt spelt
      do
	read -k 1 nyae$'?\n'"correct '$misspelt' to '$spelt' [nyae]? "
	case ${nyae:l} in
	(a) zle send-break;;
	(e) zle push-line; return;;
	(y) BUFFER=${BUFFER/$misspelt/$spelt};;
	esac
        [[ -n $nyae ]] && print -nR $nyae
      done
      [[ -n $nyae ]] && { print; zle redisplay }
    }
    zle -N zle-line-finish

Use at your own risk.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: spell check on the command line
  2014-10-25  7:24     ` Bart Schaefer
@ 2014-10-25  8:17       ` Eric Smith
  2014-10-25 18:06         ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Eric Smith @ 2014-10-25  8:17 UTC (permalink / raw)
  To: zsh-users

Looks great Bart, thank you!

I cannot find the application "suggest" anywhere, what do you
suggest?

Eric

Bart Schaefer wrote on Sat-25-Oct 14  9:24AM
> On Oct 24, 11:19pm, Eric Smith wrote:
> }
> } ~> setopt CORRECTALL
> } ~> touch this_settting_doess_not_yet_check_the_spelling_of_these_tags_in_a_filename.txt
> 
> Oh, so you want a regular spelling dictionary applied in some way, to
> substrings within each command-line argument.  Or perhaps only to
> substrings within file names, but since it's not possible to tell in
> general which arguments are file names (a shortcoming correctall has
> already), it's effectively the same problem.
> 
> Let's ignore the complications of shell keywords like "elif", complex
> commands like loops, and multi-line buffers.  Your basic choices are:
> 
> - override the accept-line widget; or
> - create (or add to if already using one) a zle-line-finish widget; or
> - attempt to handle it all in prexec.
> 
> The simplest one is zle-line-finish so I'll do a quick example of that
> here.  If you do it in preexec it's more difficult to implement the
> (a)bort and (e)dit cases.
> 
> I'll assume that anything matching [[:punct:]] is taken as a word break,
> and also that a program or function "suggest" exists that will spit out
> pairs of wrong words and their replacments (unlike "spell" which emits
> only the wrong words).
> 
>     zle-line-finish() {
>       local spelt misspelt nyae
>       print -R ${BUFFER//[[:punct:][:digit:]]/$'\n'} | suggest |
>       while read misspelt spelt
>       do
> 	read -k 1 nyae$'?\n'"correct '$misspelt' to '$spelt' [nyae]? "
> 	case ${nyae:l} in
> 	(a) zle send-break;;
> 	(e) zle push-line; return;;
> 	(y) BUFFER=${BUFFER/$misspelt/$spelt};;
> 	esac
>         [[ -n $nyae ]] && print -nR $nyae
>       done
>       [[ -n $nyae ]] && { print; zle redisplay }
>     }
>     zle -N zle-line-finish
> 
> Use at your own risk.

-- 
Eric Smith


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: spell check on the command line
  2014-10-25  8:17       ` Eric Smith
@ 2014-10-25 18:06         ` Bart Schaefer
  2014-10-27 11:18           ` Eric Smith
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2014-10-25 18:06 UTC (permalink / raw)
  To: zsh-users

On Oct 25, 10:17am, Eric Smith wrote:
}
} I cannot find the application "suggest" anywhere, what do you
} suggest?

No pun intended?

If you have ispell or aspell you can run them with the -a option and
parse the output.  I'm sure there's a way to do the same with hunspell
but I'm not familiar with the details.

It'd be something like

  suggest() {
    local tag misspelt count offset suggestions
    aspell -a |
    while read tag misspelt count offset suggestions
    do 
      [[ $tag = \& ]] && print -R $misspelt ${${(s:, :)suggestions}[1]}
    done
  }

Beware that aspell might suggest that a word be split into two words,
rather than corrected to a different single word, so you probably want
to adjust the above to filter ${(s:, :)suggestions} before deciding
which one to print.

-- 
Barton E. Schaefer


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: spell check on the command line
  2014-10-25 18:06         ` Bart Schaefer
@ 2014-10-27 11:18           ` Eric Smith
  0 siblings, 0 replies; 7+ messages in thread
From: Eric Smith @ 2014-10-27 11:18 UTC (permalink / raw)
  To: zsh-users

Thanks Bart.

All works first time.
(Perhaps one day I will actually become literate and learn to spell, perhaps not?)

ciao

Eric Smith

Bart Schaefer wrote on Sat-25-Oct 14  8:06PM
> On Oct 25, 10:17am, Eric Smith wrote:
> }
> } I cannot find the application "suggest" anywhere, what do you
> } suggest?
> 
> No pun intended?
> 
> If you have ispell or aspell you can run them with the -a option and
> parse the output.  I'm sure there's a way to do the same with hunspell
> but I'm not familiar with the details.
> 
> It'd be something like
> 
>   suggest() {
>     local tag misspelt count offset suggestions
>     aspell -a |
>     while read tag misspelt count offset suggestions
>     do 
>       [[ $tag = \& ]] && print -R $misspelt ${${(s:, :)suggestions}[1]}
>     done
>   }
> 
> Beware that aspell might suggest that a word be split into two words,
> rather than corrected to a different single word, so you probably want
> to adjust the above to filter ${(s:, :)suggestions} before deciding
> which one to print.
> 
> -- 
> Barton E. Schaefer

-- 
Eric Smith


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-10-27 11:19 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-24 10:54 spell check on the command line Eric Smith
2014-10-24 15:36 ` Bart Schaefer
2014-10-24 21:19   ` Eric Smith
2014-10-25  7:24     ` Bart Schaefer
2014-10-25  8:17       ` Eric Smith
2014-10-25 18:06         ` Bart Schaefer
2014-10-27 11:18           ` Eric Smith

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).