Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^4: word auto-corrector in Curses::UI

by shmem (Chancellor)
on Jan 09, 2021 at 13:06 UTC ( #11126654=note: print w/replies, xml ) Need Help??


in reply to Re^3: word auto-corrector in Curses::UI
in thread word auto-corrector in Curses::UI

The problem is that after the first <Tab> Curses::UI gives control of the terminal to Term::Complete. It acts directly on the terminal, and Curses::UI is not aware of what it does. It is not another perlio layer. Curses::UI is not aware of any keystrokes whilst in Term::Complete::Complete(). From the POD of Term::Complete:

The tty driver is put into raw mode and restored using an operating system specific command, in UNIX-like environments "stty".

So, Curses::UI and Term::Complete are not aware of each other. Term::Complete doesn't define up/down keys. It is tricky to integrate both...

With the snippet as provided, <Tab> must be pressed before attempting a completion; then you type the chars of a word you intend to complete. Subseqent <Tab> presses advance the completion until it is unambiguous. Then you press <Return> - completion done. Refresh the screen with ^L.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
  • Comment on Re^4: word auto-corrector in Curses::UI

Replies are listed 'Best First'.
Re^5: word auto-corrector in Curses::UI
by Bpl (Scribe) on Jan 09, 2021 at 13:16 UTC
    Oh, that's unexpected,
    Initially I thought that Curses::UI used a custom perlio layer for its purposes, but effectively in the source code there is no XS ( || perlio library ), So the question remain the same,
    How I could create an auto-correcting text-editor application?
    There is something better or similar than Ncurses || Curses::UI on CPAN?
    regards,
    Edoardo Mantovani
      How I could create an auto-correcting text-editor application?

      You tried to integrate Term::Complete into your curses UI, but that's not about auto-correction.

      First thing you have to do is get the snippet of text which has to be examined as e.g. $candidate:

      $cui->add_callback( 'editor', \&current_word); my $candidate; # word to build a complete list from, or to spell-check sub current_word { my ($x, $y); Curses::UI::TextEditor::getsyx($y, $x); # not $editor->getsyx($foo +), this would overwrite $editor. BUG! $y--; # there's 1 padding line chomp (my $line = $editor->getline_at_ypos($y-1)); # first line is + 0 my $part = substr $line, 0, $x; ($candidate) = $part =~ /\b(\w+)$/; # caveat locale, utf8 warn $fh "x: $x y: $y '$line' - '$part' candidate: '$candidate'\n" +; }

      This will give you information to STDERR about the current word under the cursor at every keystroke.

      You can build a list of completion candidates based on that word, or check the spelling with e.g. the aspell library.

      Based on that word, you could decide wether adding a <Tab> to the text, or attempt completion.

      You could mark misspelled words in terms of Term::ANSIColor or such, do completion with either Term::Complete or Term::ReadLine::Gnu (with both you have to shoehorn the results into your $editor widget) - or just do your own completion routine with Curses::UI using Curses::UI::Listbox to display possible completions.

      Way to go yet...

      PS: why? are you trying to re-implement vi or emacs in perl? ;)

      PS2: "Find and Replace" comes first in functionality, then more sophisticated things like (auto-)completion and spellcheck.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        Hi,
        Sorry for the late reply, in those days I'll try to do it.
        Still many thanks for everything!
        Edoardo Mantovani

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11126654]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2021-03-01 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?