Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Input box with wikipedia and google completion


  • Please log in to reply
2 replies to this topic
keyboardfreak
  • Members
  • 217 posts
  • Last active: Sep 27 2010 07:21 PM
  • Joined: 09 Oct 2004
Here's an other variant of the input box with wikipedia completion script which also shows results from google. Completions can be selected with up/down and enter, and the corresponding wikipedia or google page is opened. Esc cancels.

The display of completions is experimental, currently a [w] before the completion indicates it is from Wikipedia and [g] indicates google. If no completion is used then the input is sent to google.

The variable ibc_max_completions_from_one_source controls how much completions are used from one source (by default 5).

; input box completion with wikipedia

ibc_gui_background_color = 66CCFF
ibc_input_gui_number = 45
ibc_completion_gui_number := ibc_input_gui_number + 1
ibc_input_check_delay = 100
ibc_max_completions_from_one_source = 5
ibc_completion_file := A_Temp . "\ibc_completions"


Gui %ibc_input_gui_number%: +Owner -Caption +AlwaysOnTop 
Gui %ibc_input_gui_number%: font, s20
Gui %ibc_input_gui_number%: Add, Edit, w500 vInput gibc_input_changed
Gui %ibc_input_gui_number%: Color, % ibc_gui_background_color
Gui %ibc_input_gui_number%: +LastFound

ibc_input_gui_id := winexist()

Gui %ibc_completion_gui_number%: +Owner -Caption +AlwaysOnTop 
Gui %ibc_completion_gui_number%: font, s14
Gui %ibc_completion_gui_number%: Add, Listbox, x0 y0 w600 r10 vCompletions AltSubmit HwndIbc_Listbox
Gui %ibc_completion_gui_number%: Color, EEAA99
Gui %ibc_completion_gui_number%: +LastFound
WinSet, TransColor, EEAA99

Gui %ibc_input_gui_number%: Show
ibc_selected_search_provider =

WinGetPos ibc_input_gui_x, ibc_input_gui_y, w , ibc_input_gui_height, A
ibc_completion_gui_x := ibc_input_gui_x
ibc_completion_gui_y := ibc_input_gui_y + ibc_input_gui_height + 5


Hotkey enter, ibc_submit, on
Hotkey esc, ibc_cancel, on
Hotkey down, ibc_next_completion, off
Hotkey up, ibc_previous_completion, off

return

ibc_submit:
  GuiControlGet ibc_input, %ibc_input_gui_number%:, Input
  gosub cleanup
  
  ; fixme: encode query
  if (ibc_selected_search_provider == "w")
    Run http://en.wikipedia.org/wiki/%ibc_input%
  else
    Run http://www.google.com/search?q=%ibc_input%
  
  exitapp


ibc_cancel:
  gosub cleanup
  exitapp
  
  
ibc_input_changed:
  GuiControlGet ibc_input, %ibc_input_gui_number%:, Input
  SetTimer ibc_check_input, %ibc_input_check_delay%
  return
  
  
ibc_check_input:
  if (A_TimeIdlePhysical < ibc_input_check_delay)
    return
  
  SetTimer ibc_check_input, off

  if (ibc_input == ibc_previous_input)
    return
  
  ibc_previous_input := ibc_input
  ibc_input_len := strlen(ibc_input)
  
  if (ibc_input_len == 0)
  {
    gosub ibc_hide_completions
    return
  }
  
  ibc_matches =
  
  ; fixme: encode query
  URLDownloadToFile http://en.wikipedia.org/w/api.php?action=opensearch&search=%ibc_input%, %ibc_completion_file%

  if (ErrorLevel == 0)
  {
    fileread ibc_completions, %ibc_completion_file%
    
    ; thanks to SKAN for this simple approach of parsing the results
    ibc_completions := Deref_Umlauts(RegExReplace(ibc_completions, "\[|\]"))
    ibc_completions_num = 0
    Loop, Parse, ibc_completions, CSV 
    {
      if (A_Index == 1)
        ibc_user_input := A_Loopfield
      else
      {
        ibc_matches := ibc_matches . "|[w] " . A_Loopfield
        ibc_completions_num +=1
        if (ibc_completions_num == ibc_max_completions_from_one_source)
          break
      }
    }
    
    ; if only an emtpy entry is returned then there are no matches
    if (ibc_matches == "|[w] ")
      ibc_matches := ""
  }

  ; fixme: encode query
  URLDownloadToFile http://google.com/complete/search?output=toolbar&q=%ibc_input%, %ibc_completion_file%

  if (ErrorLevel == 0)
  {
    fileread ibc_completions, %ibc_completion_file%

    ibc_completions_num = 0
    pos = 0
    while (pos := RegExMatch(ibc_completions, "<suggestion data=""(.+?)""/>", match, pos + 1))
    {
      ibc_matches := ibc_matches . "|[g] " . match1
      ibc_completions_num +=1
      if (ibc_completions_num == ibc_max_completions_from_one_source)
        break
    }
  }

  if (ibc_matches <> "")
  {
    GuiControl %ibc_completion_gui_number%:,Completions, %ibc_matches%
    Gui %ibc_completion_gui_number%: Show, x%ibc_completion_gui_x% y%ibc_completion_gui_y%
    WinActivate ahk_id %ibc_input_gui_id%
    hotkey down, on
    hotkey up, on
  }
  Else
    gosub ibc_hide_completions

  return
  
  
ibc_hide_completions:
  Gui %ibc_completion_gui_number%: Hide
  Hotkey down, off
  Hotkey up, off
  return
  
  
ibc_next_completion:
  GuiControlGet ibc_current_completion, %ibc_completion_gui_number%:, Completions
  if (ibc_current_completion == "")
    ibc_current_completion = 0
  ibc_current_completion := ibc_current_completion + 1
  GuiControl %ibc_completion_gui_number%:Choose ,Completions, %ibc_current_completion%
  gosub ibc_copy_completion_to_input
  return
  
  
ibc_previous_completion:
  GuiControlGet ibc_current_completion, %ibc_completion_gui_number%:, Completions
  if (ibc_current_completion > 0)
  {
    ibc_current_completion := ibc_current_completion - 1
    if (ibc_current_completion == 0)
    {
      PostMessage, 0x186, -1, 0,, ahk_id %Ibc_Listbox%
      copy_string_to_input(ibc_user_input)
      ibc_selected_search_provider = g
    }
    else
    {
      GuiControl %ibc_completion_gui_number%:Choose ,Completions, %ibc_current_completion%
      Gosub ibc_copy_completion_to_input
    }
  }
  return
  
  
ibc_copy_completion_to_input:
  ibc_wanted_completion := ibc_current_completion + 1
  Loop, Parse, ibc_matches, |
  {
    if (A_Index == ibc_wanted_completion)
    {
      RegExMatch(A_Loopfield, "^\[(.)\] (.+)", match)
      ibc_selected_search_provider := match1
      copy_string_to_input(match2)
      Break
    }
  }
  
  return
  
  
copy_string_to_input(str)
{
  global
  
  GuiControl %ibc_input_gui_number%:, Input, %str%
  ibc_input := str
  ibc_previous_input := ibc_input
  SendInput {end}
  SetTimer ibc_check_input, off  
}

  
cleanup:
  Gosub ibc_hide_completions
  Gui %ibc_input_gui_number%: Hide
  Hotkey enter, ibc_submit, off
  Hotkey esc, ibc_cancel, off
  Hotkey down, off
  Hotkey up, off
  return
  
  
; code by SKAN  
Deref_Umlauts( w, n=1 )
{ 
  stringreplace, w, w, \u0171, % chr("0xfc")
  stringreplace, w, w, \u0151, % chr("0xf6")
  While n := instr( w, "\u",1,n ) 
    StringReplace, w, w, % ww := substr( w,n,6 ), % chr( "0x" substr( ww,3 ) ), all 
  Return w 
}


Madroaster
  • Members
  • 25 posts
  • Last active: Mar 20 2014 06:20 PM
  • Joined: 25 Feb 2010
I've been using this for a few days now and have noticed that it frequently drops typed characters from the input box.

Essentially, as I'm typing my search string, 5-10% of my characters are dropped. Is there a way to fix this?

Cragaha
  • Members
  • 265 posts
  • Last active: Jan 04 2016 02:24 AM
  • Joined: 19 Nov 2010
It will search for each word in the search string separately :(