Skip to content

Commit d00bcf3

Browse files
committed
Backspace: Restore key strokes while typing
1 parent eccf182 commit d00bcf3

File tree

2 files changed

+44
-43
lines changed

2 files changed

+44
-43
lines changed

src/ibus-bamboo/engine_backspace.go

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (e *IBusBambooEngine) bsProcessKeyEvent(keyVal uint32, keyCode uint32, stat
4141
return false, nil
4242
}
4343
var keyRune = rune(keyVal)
44-
// don't use ForwardKeyEvent api in XTestFakeKeyEvent and SurroundingText mode
44+
// Caution: don't use ForwardKeyEvent api in XTestFakeKeyEvent and SurroundingText mode
4545
if e.checkInputMode(xTestFakeKeyEventIM) || e.checkInputMode(surroundingTextIM) {
4646
if keyVal == IBusLeft && state&IBusShiftMask != 0 {
4747
return false, nil
@@ -104,23 +104,21 @@ func (e *IBusBambooEngine) keyPressHandler(keyVal, keyCode, state uint32) {
104104
return
105105
}
106106
var keyRune = rune(keyVal)
107-
oldText := e.preeditor.GetProcessedString(bamboo.VietnameseMode | bamboo.WithEffectKeys)
107+
oldText := e.getPreeditString()
108108
if keyVal == IBusBackSpace {
109-
if e.config.IBflags&IBautoNonVnRestore == 0 || e.checkInputMode(shiftLeftForwardingIM) {
110-
if e.getRawKeyLen() > 0 {
111-
e.preeditor.RemoveLastChar(false)
112-
}
113-
e.ForwardKeyEvent(keyVal, keyCode, state)
114-
return
115-
}
116-
if e.getRawKeyLen() > 0 {
109+
if e.getRawKeyLen() > 0 && oldText != "" {
110+
if e.config.IBflags&IBautoNonVnRestore == 0 {
111+
e.preeditor.RemoveLastChar(false)
112+
e.ForwardKeyEvent(keyVal, keyCode, state)
113+
return
114+
}
117115
e.preeditor.RemoveLastChar(true)
118-
if oldText == "" {
119-
e.ForwardKeyEvent(keyVal, keyCode, state)
120-
return
121-
}
122-
e.updatePreviousText(e.preeditor.GetProcessedString(bamboo.VietnameseMode|bamboo.WithEffectKeys), oldText)
123-
return
116+
var newText = e.getPreeditString()
117+
var offset = e.getPreeditOffset([]rune(newText), []rune(oldText))
118+
if offset != len([]rune(newText)) {
119+
e.updatePreviousText(newText, oldText)
120+
return
121+
}
124122
}
125123
e.ForwardKeyEvent(keyVal, keyCode, state)
126124
return
@@ -142,13 +140,19 @@ func (e *IBusBambooEngine) keyPressHandler(keyVal, keyCode, state uint32) {
142140
if state&IBusLockMask != 0 {
143141
keyRune = e.toUpper(keyRune)
144142
}
143+
var outputMode = bamboo.VietnameseMode
144+
if e.shouldFallbackToEnglish(true) {
145+
outputMode = bamboo.EnglishMode
146+
}
147+
var t =e.getInputMethod()
145148
e.preeditor.ProcessKey(keyRune, e.getInputMethod())
146-
var vnSeq = e.preeditor.GetProcessedString(bamboo.VietnameseMode | bamboo.WithEffectKeys)
149+
var vnSeq = e.preeditor.GetProcessedString(outputMode | bamboo.WithEffectKeys)
150+
log.Println("vnsql", vnSeq, t , outputMode)
147151
if len(vnSeq) > 0 && rune(vnSeq[len(vnSeq)-1]) == keyRune && bamboo.IsWordBreakSymbol(keyRune) {
148152
e.updatePreviousText(vnSeq, oldText)
149153
e.preeditor.Reset()
150154
} else {
151-
e.updatePreviousText(vnSeq, oldText)
155+
e.updatePreviousText(e.getPreeditString(), oldText)
152156
}
153157
return
154158
} else if bamboo.IsWordBreakSymbol(keyRune) {
@@ -188,30 +192,30 @@ func (e *IBusBambooEngine) keyPressHandler(keyVal, keyCode, state uint32) {
188192
e.ForwardKeyEvent(keyVal, keyCode, state)
189193
}
190194

191-
func (e *IBusBambooEngine) updatePreviousText(newText, oldText string) {
192-
var oldRunes = []rune(oldText)
193-
var newRunes = []rune(newText)
194-
oldLen := len(oldRunes)
195-
newLen := len(newRunes)
196-
minLen := oldLen
197-
if newLen < minLen {
198-
minLen = newLen
195+
func (e *IBusBambooEngine) getPreeditOffset(newRunes, oldRunes []rune) int {
196+
var minLen = len(oldRunes)
197+
if len(newRunes) < minLen {
198+
minLen = len(newRunes)
199199
}
200-
201-
sameTo := -1
202200
for i := 0; i < minLen; i++ {
203-
if oldRunes[i] == newRunes[i] {
204-
sameTo = i
205-
} else {
206-
break
201+
if oldRunes[i] != newRunes[i] {
202+
return i
207203
}
208204
}
209-
diffFrom := sameTo + 1
210-
log.Printf("Updating Previous Text %s ---> %s\n", string(oldRunes), string(newRunes))
205+
return minLen
206+
}
207+
208+
func (e *IBusBambooEngine) updatePreviousText(newText, oldText string) {
209+
var oldRunes = []rune(oldText)
210+
var newRunes = []rune(newText)
211+
var nBackSpace = 0
212+
var offset = e.getPreeditOffset(newRunes, oldRunes)
213+
if offset < len(oldRunes) {
214+
nBackSpace += len(oldRunes) - offset
215+
}
211216

212-
nBackSpace := 0
213217
// workaround for chrome and firefox's address bar
214-
if e.isFirstTimeSendingBS && diffFrom < newLen && diffFrom < oldLen && e.inBrowserList() &&
218+
if e.isFirstTimeSendingBS && offset < len(newRunes) && offset < len(oldRunes) && e.inBrowserList() &&
215219
!e.checkInputMode(shiftLeftForwardingIM) {
216220
fmt.Println("Append a deadkey")
217221
e.SendText([]rune(" "))
@@ -220,11 +224,8 @@ func (e *IBusBambooEngine) updatePreviousText(newText, oldText string) {
220224
e.isFirstTimeSendingBS = false
221225
}
222226

223-
if diffFrom < oldLen {
224-
nBackSpace += oldLen - diffFrom
225-
}
226-
227-
e.sendBackspaceAndNewRunes(nBackSpace, newRunes[diffFrom:])
227+
log.Printf("Updating Previous Text %s ---> %s\n", oldText, newText)
228+
e.sendBackspaceAndNewRunes(nBackSpace, newRunes[offset:])
228229
}
229230

230231
func (e *IBusBambooEngine) sendBackspaceAndNewRunes(nBackSpace int, newRunes []rune) {
@@ -307,8 +308,8 @@ func (e *IBusBambooEngine) SendText(rs []rune) {
307308
}
308309
e.ForwardKeyEvent(keyVal, 0, 0)
309310
e.ForwardKeyEvent(keyVal, 0, IBusReleaseMask)
310-
time.Sleep(5 * time.Millisecond)
311311
}
312+
time.Sleep(time.Duration(len(rs)) * 5 * time.Millisecond)
312313
return
313314
}
314315
e.commitText(string(rs))

src/ibus-bamboo/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919

2020
package main
2121

22-
const Version = "v0.6.2"
22+
const Version = "v0.6.3"

0 commit comments

Comments
 (0)