@@ -55,16 +55,25 @@ func isValidState(state uint32) bool {
5555 return true
5656}
5757
58+ func (e * FcitxBambooEngine ) isPrintableKey (state , keyVal uint32 ) bool {
59+ return isValidState (state ) && e .isValidKeyVal (keyVal )
60+ }
61+
5862func (e * FcitxBambooEngine ) getCommitText (keyVal , state uint32 ) (string , bool ) {
5963 var keyRune = rune (keyVal )
64+ isPrintableKey := e .isPrintableKey (state , keyVal )
6065 oldText := e .getPreeditString ()
6166 // restore key strokes by pressing Shift + Space
6267 if e .shouldRestoreKeyStrokes {
6368 e .shouldRestoreKeyStrokes = false
6469 e .preeditor .RestoreLastWord (! bamboo .HasAnyVietnameseRune (oldText ))
6570 return e .getPreeditString (), false
6671 }
67- if e .preeditor .CanProcessKey (keyRune ) {
72+ var keyS string
73+ if isPrintableKey {
74+ keyS = string (keyRune )
75+ }
76+ if isPrintableKey && e .preeditor .CanProcessKey (keyRune ) {
6877 if state & FcitxLockMask != 0 {
6978 keyRune = e .toUpper (keyRune )
7079 }
@@ -81,7 +90,7 @@ func (e *FcitxBambooEngine) getCommitText(keyVal, state uint32) (string, bool) {
8190 var ret = e .getPreeditString ()
8291 var lastRune = rune (ret [len (ret )- 1 ])
8392 var isWordBreakRune = bamboo .IsWordBreakSymbol (lastRune )
84- // TODO: THIS IS HACKING
93+ // TODO: THIS IS A HACK
8594 if isWordBreakRune {
8695 e .preeditor .RemoveLastChar (false )
8796 e .preeditor .ProcessKey (' ' , bamboo .EnglishMode )
@@ -104,28 +113,46 @@ func (e *FcitxBambooEngine) getCommitText(keyVal, state uint32) (string, bool) {
104113 } else {
105114 return e .getPreeditString (), false
106115 }
107- } else if bamboo . IsWordBreakSymbol ( keyRune ) {
116+ } else if e . macroEnabled {
108117 // macro processing
109- if e .macroEnabled {
110- var keyS = string (keyRune )
111- if keyVal == FcitxSpace && e .macroTable .HasKey (oldText ) {
112- e .preeditor .Reset ()
113- return e .expandMacro (oldText ) + keyS , keyVal == FcitxSpace
114- } else {
115- e .preeditor .ProcessKey (keyRune , e .getBambooInputMode ())
116- return oldText + keyS , keyVal == FcitxSpace
118+ if isPrintableKey && e .macroTable .HasPrefix (oldText + keyS ) {
119+ e .preeditor .ProcessKey (keyRune , bamboo .EnglishMode )
120+ return oldText + keyS , false
121+ }
122+ if e .macroTable .HasKey (oldText ) {
123+ if isPrintableKey {
124+ return e .expandMacro (oldText ) + keyS , true
117125 }
126+ return e .expandMacro (oldText ), true
118127 }
119- if bamboo .HasAnyVietnameseRune (oldText ) && e .mustFallbackToEnglish () {
120- e .preeditor .RestoreLastWord (false )
121- newText := e .preeditor .GetProcessedString (bamboo .EnglishMode ) + string (keyRune )
128+ }
129+ return e .handleNonVnWord (keyVal , state ), true
130+ }
131+
132+ func (e * FcitxBambooEngine ) handleNonVnWord (keyVal , state uint32 ) string {
133+ var (
134+ keyS string
135+ keyRune = rune (keyVal )
136+ isPrintableKey = e .isPrintableKey (state , keyVal )
137+ oldText = e .getPreeditString ()
138+ )
139+ if isPrintableKey {
140+ keyS = string (keyRune )
141+ }
142+ if bamboo .HasAnyVietnameseRune (oldText ) && e .mustFallbackToEnglish () {
143+ e .preeditor .RestoreLastWord (false )
144+ newText := e .preeditor .GetProcessedString (bamboo .PunctuationMode | bamboo .EnglishMode ) + keyS
145+ if isPrintableKey {
122146 e .preeditor .ProcessKey (keyRune , bamboo .EnglishMode )
123- return newText , true
124147 }
148+ return newText
149+ }
150+ if isPrintableKey {
125151 e .preeditor .ProcessKey (keyRune , bamboo .EnglishMode )
126- return oldText + string ( keyRune ), true
152+ return oldText + keyS
127153 }
128- return "" , true
154+ // Ctrl + A is treasted as a WBS
155+ return oldText + keyS
129156}
130157
131158func (e * FcitxBambooEngine ) getMacroText () (bool , string ) {
@@ -138,3 +165,14 @@ func (e *FcitxBambooEngine) getMacroText() (bool, string) {
138165 }
139166 return false , ""
140167}
168+
169+ func (e * FcitxBambooEngine ) isValidKeyVal (keyVal uint32 ) bool {
170+ var keyRune = rune (keyVal )
171+ if keyVal == FcitxBackSpace || bamboo .IsWordBreakSymbol (keyRune ) {
172+ return true
173+ }
174+ if ok , _ := e .getMacroText (); ok && keyVal == FcitxTab {
175+ return true
176+ }
177+ return e .preeditor .CanProcessKey (keyRune )
178+ }
0 commit comments