Skip to content

Commit

Permalink
Add option to fall back to case-insensitive matches.
Browse files Browse the repository at this point in the history
  • Loading branch information
samhocevar committed Apr 24, 2014
1 parent 677738f commit e78fa37
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
62 changes: 35 additions & 27 deletions src/main.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ global S := { typing: false ; Is the user typing something?
, selected_seq: "" } ; The sequence currently selected

; Runtime configuration, imported from the config files
global R := { sequences: {}
, seq_count: 0
, prefixes: {}
, descriptions: {}
, keynames: {}
, compose_key: C.keys.default
, reset_delay: C.delays.valid
, opt_case: false
, opt_discard: false
, opt_beep: false }
global R := { sequences: {} ; List of valid sequences
, sequences_alt: {} ; Alt table for case insensitive
, seq_count: 0
, prefixes: {} ; List of valid prefixes
, prefixes_alt: {} ; Alt table for case insensitive
, descriptions: {}
, keynames: {}
, compose_key: C.keys.default
, reset_delay: C.delays.valid
, opt_case: false
, opt_discard: false
, opt_beep: false }

main()
return
Expand Down Expand Up @@ -169,25 +171,14 @@ send_keystroke(keystroke)

info := "Sequence: [ " sequence " ]"

valid_sequence := has_sequence(sequence)
valid_prefix := valid_sequence ? true : has_prefix(sequence)

; If sequence is a dead-end, try a case-insensitive match
if (!valid_sequence && !valid_prefix && R.opt_case)
{
if (toupper(sequence) != tolower(sequence))
{
}
}

if (valid_sequence)
if (has_sequence(sequence))
{
info .= " -> [ " get_sequence(sequence) " ]"
send_unicode(get_sequence(sequence))
S.typing := false
sequence := ""
}
else if (valid_prefix)
else if (has_prefix(sequence))
{
if (R.reset_delay > 0)
settimer on_delay_expired, % R.reset_delay
Expand Down Expand Up @@ -556,10 +547,15 @@ add_sequence(seq, char, desc)

; Insert into our [sequence → character] lookup table
R.sequences.insert(string_to_hex(seq), [seq, char])
R.sequences_alt.insert(seq, seq)

; Insert into the prefix lookup table
loop % strlen(seq) - 1
R.prefixes.insert(string_to_hex(substr(seq, 1, a_index)), true)
{
prefix := substr(seq, 1, a_index)
R.prefixes.insert(string_to_hex(prefix), true)
R.prefixes_alt.insert(prefix, prefix)
}

; Insert into Unicode description list
R.descriptions.insert(string_to_hex(seq), desc)
Expand Down Expand Up @@ -609,12 +605,20 @@ fill_sequences(filter)

has_sequence(seq)
{
return R.sequences.haskey(string_to_hex(seq))
ret := R.sequences.haskey(string_to_hex(seq))
; Try to match case-insensitive, but only if it is not a valid prefix
if (!ret && R.opt_case && !R.prefixes_alt.haskey(seq))
ret := R.sequences_alt.haskey(seq)
return ret
}

get_sequence(seq)
{
return R.sequences[string_to_hex(seq)][2]
ret := R.sequences[string_to_hex(seq)][2]
; Try to match case-insensitive
if (!ret && R.opt_case)
ret := R.sequences[string_to_hex(R.sequences_alt[seq])][2]
return ret
}

get_description(seq)
Expand All @@ -624,6 +628,10 @@ get_description(seq)

has_prefix(seq)
{
return R.prefixes.haskey(string_to_hex(seq))
ret := R.prefixes.haskey(string_to_hex(seq))
; Try to match case-insensitive
if (!ret && R.opt_case)
ret := R.prefixes_alt.haskey(seq)
return ret
}

22 changes: 13 additions & 9 deletions src/ui.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ global ui_listbox, ui_edit_filter, ui_button
global ui_text_filter, ui_text_filterw, ui_text_bigchar, ui_text_desc
global ui_text_composekey, ui_dropdown_composekey
global ui_text_delay, ui_dropdown_delay
global ui_text_separator1
global ui_checkbox_case, ui_checkbox_discard, ui_checkbox_beep
global ui_keycap_0
global ui_keycap_1, ui_keycap_2, ui_keycap_3, ui_keycap_4, ui_keycap_5, ui_keycap_6, ui_keycap_7, ui_keycap_8, ui_keycap_9
Expand Down Expand Up @@ -143,7 +144,7 @@ on_exit:
create_app_win()
{
; Build the main window
gui +resize +minsize720x450
gui +resize +minsize720x450 +labelon_app_win_
gui margin, 8, 8

gui add, tab2, vui_tab, % _("Sequences") "|" _("Options")
Expand Down Expand Up @@ -209,14 +210,15 @@ create_app_win()
gui add, text, vui_text_delay, % _("Delay:")
gui add, dropdownlist, vui_dropdown_delay gon_set_delay, %delaylist%

gui add, checkbox, vui_checkbox_case gon_toggle_case, % _("If a sequence is invalid, try to match an existing case-insensitive sequence.")
gui add, text, vui_text_separator1 0x10

gui add, checkbox, vui_checkbox_case gon_toggle_case, % _("Fall back to case insensitive matches on invalid sequences")
guicontrol ,, ui_checkbox_case, % R.opt_case ? 1 : 0
guicontrol disable, ui_checkbox_case

gui add, checkbox, vui_checkbox_discard gon_toggle_discard, % _("Discard characters from invalid sequences instead of printing them.")
gui add, checkbox, vui_checkbox_discard gon_toggle_discard, % _("Discard characters from invalid sequences")
guicontrol ,, ui_checkbox_discard, % R.opt_discard ? 1 : 0

gui add, checkbox, vui_checkbox_beep gon_toggle_beep, % _("Beep on invalid sequences.")
gui add, checkbox, vui_checkbox_beep gon_toggle_beep, % _("Beep on invalid sequences")
guicontrol ,, ui_checkbox_beep, % R.opt_beep ? 1 : 0

; Build the rest of the window
Expand All @@ -229,7 +231,7 @@ create_app_win()

return

guisize:
on_app_win_size:
if (a_eventinfo != 1) ; Ignore minimising
{
UI.app_win.width := a_guiwidth
Expand All @@ -238,7 +240,7 @@ guisize:
}
return

guicontextmenu:
on_app_win_contextmenu:
if (a_guicontrol == "ui_listbox")
{
if (a_eventinfo > 0)
Expand Down Expand Up @@ -315,8 +317,8 @@ on_select_sequence:
return

buttonclose:
guiclose:
guiescape:
on_app_win_close:
on_app_win_escape:
hide_app_win()
return
}
Expand Down Expand Up @@ -346,6 +348,8 @@ refresh_gui()
guicontrol move, ui_text_delay, % "x" 3 * m " y" (40 + 32 + m)
guicontrol move, ui_dropdown_delay, % "x" (120) " y" (40 + 32 - 4 + m)

guicontrol move, ui_text_separator1, % "x" (3 * m) " y" (40 + 64 + m) " w" (t_w - 4 * m)

guicontrol move, ui_checkbox_case, % "x" 3 * m " y" 120 + m
guicontrol move, ui_checkbox_discard, % "x" 3 * m " y" 120 + 25 + m
guicontrol move, ui_checkbox_beep, % "x" 3 * m " y" 120 + 50 + m
Expand Down

0 comments on commit e78fa37

Please sign in to comment.