For example the wordlist is:
Code: Select all
New
Open
Save As
Page Setup
Print
Exit
Find
Find Next
Replace
Go To
View Help
About Notepad
Status Bar
Word Wrap
Font
Undo
Paste
Cut
Copy
Delete
Select All
Time/Date
Code: Select all
SetBatchLines, -1
#SingleInstance Force
SetTitleMatchMode, 2
; Create a timer that checks if the popup is no longer the active window. If so, close the gui! (gosub esc to the gui)
; AUTO EXECUTE SECTION ----------------------------------------------------------------
FileRead, FileContents, wordlist.txt ; file read one time at script launch for best efficiency
Sort, FileContents ; sort alphabetically allows better autocomplete behavior
Gui +LastFound ; Make the GUI window the last found window for use by the line below.
Gui, font, s12, Arial
Gui, Add, Edit, vneedle grefresh w400, ; whenever user types in this editbox, the grefresh g-label runs the refresh subroutine
Gui, Add, ListBox, vresults r6 w400 ; shows the contents of %results% in a listbox
listboxvisible := true
Gui, Show, x500 y500, Suggestions ;show the edit box for the user to type in
GuiControl, Focus, needle
Return
Refresh:
Gui, Submit, NoHide ; Save the user's input without hiding the window
choicelimit := 0 ; resets the listbox choices counter every time the user adds or removes a letter from the edit box
GuiControl,, results, | ; this empties the previous listbox entries
listbox:="" ; this empties variable that controls the listbox entries
if (needle != "") ; if the user's entry isn't empty (if something typed into editbox)
{
Guicontrol, show, results ; show the listbox
listboxvisible := true
Loop, parse, FileContents, `n, `r ; parse the wordlist.txt in memory to find matches
{
haystack:=A_LoopField ; cycles each line 1 by 1 matching 1 word at a time
position := instr(haystack, needle) ; finds out where the user's entry matches the word at (left, middle, or end of word etc.) or if not at all
if (position = 1) ;if it matches from the left
{
listbox.="|" . haystack ; add it to the listbox variable
choicelimit := choicelimit + 1 ; increase the counter which counts how many results are going to be in the listbox
}
}
Loop, parse, FileContents, `n, `r ; parse the wordlist.txt in memory again for a different kind of matching (midstring match)
{
haystack:=A_LoopField ; cycles each line 1 by 1 matching 1 word at a time
position := instr(haystack, needle) ; finds out where the user's entry matches the word at (left, middle, or end of word etc.) or if not at all
if (position > 1) ; if it matches anywhere but from the left
{
listbox.="|" . haystack ; add it to the listbox variable, importantly AFTER the above matches
choicelimit := choicelimit + 1 ; increase the counter which counts how many results are going to be in the listbox
}
}
if (listbox = "" || listbox = "|") {
listboxvisible := false
return
}
GuiControl,, results, %listbox% ; add the matched words to the listbox entries
GuiControl, choose, results, 1 ; choose the first one by default (highlight it)
resultchosen := 1 ; store which choice is highlighted in a variable
}
return
; HOTKEYS ----------------------------------------------------------------
1::
2::
3::
4::
5::
6::
7::
8::
9::
0::
a::
b::
c::
d::
e::
f::
g::
h::
i::
j::
k::
l::
m::
n::
o::
p::
q::
r::
s::
t::
u::
v::
w::
x::
y::
z::
controlsend, Edit1, %A_ThisHotkey%, Suggestions ahk_class AutoHotkeyGUI
Return
^Esc::
Reload
Return
A good test case is typing the letter 'p', right now it will match "Page Setup" as the first result (screenshot below). I would like to make the script prioritize the exact first-letter of the word match for Paste to be at the top, with Print coming in second (because the second letter is alphabetically ordered), and page setup coming in third. But this 'first letter of each word' functionality eludes me
A good second proof the functionality is working would be to type "ps" and have the only result be "Page Setup", as it is now nothing appears.
Do I need a match-scoring system to incorporate this new first letter of each word search functionality as well or can I do without it?