1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-27 14:14:19 +03:00
vimr/docs/notes-on-cocoa-text-input.md
2021-10-31 17:41:30 +08:00

6.8 KiB

Some Notes on Cocoa's Text Input

To use Cocoa's text input system, e.g. the 2-Set Korean input, your view has to implement the NSTextInputClient protocol. Apple's documentation is very scarce, so we're writing down some of our findings.

Simple Case

For simple cases like ü, which can be entered by Opt-u + u, it's quite straightforward:

  1. Enter Opt-u.
  2. hasMarkedText() is called to check whether we already have marked text.
  3. setMarkedText("¨", selectedRange NSRange(1, 0), replacementRange: NSRange(NSNotFound, 0)) is called. In this case the first argument is an NSString, selectedRange tells us where to put the cursor relative to the string: in this case after ¨. The range replacemenRange tells us whether the string should replace some of the existing text. In this case no replacement is required.
  4. Enter u.
  5. hasMarkedText() is called again.
  6. insertText("ü", replacementRange: NSRange(NSNotFound, 0)) is called to finalize the input. It seems that for the replacement range (NSNotFound, 0) we should replace the previously marked text with the final string. So in this case we must first delete ¨ and insert ü.

Korean (Hangul, 한글)

Let's move to a bit more complicated case: Korean. In this case more methods are involved:

  • selectedRange(): all other additional methods seem to rely on this method. Ideally we should return NSRange(CursorPosition, 0) when nothing is selected or NSRange(SelectionBegin, SelectionLength) when there's a selection.
  • attributedSubstringForProposedRange(_:actualRange:): for entering only Hangul, this method can be ignored.

Let's assume we want to enter 하태원: (hasMarkedText() is called here and there...)

  1. selectedRange() is called multiple times when changing the input method from US to Korean. This is also the case when starting the app with Korean input selected.
  2. Enter .
  3. setMarkedText("ㅎ", selectedRange: NSRange(1, 0) replacementRange:NSRange(NotFound, 0)) is called.
  4. Enter .
  5. attributedSubstringForProposedRange(_:actualRange:) and selectedRange() are called multiple times: again, for only Hangul, ignorable.
  6. setMarkedText("하", selectedRange: NSRange(1, 0), replacementRange: NSRange(NotFound, 0)) is called: delete and insert ; not yet finalized.
  7. Enter
  8. attributedSubstringForProposedRange(_:actualRange:) and selectedRange() are called multiple times: ignore.
  9. setMarkedText("핱", selectedRange: NSRange(1, 0), replacementRange: NSRange(NotFound, 0)) is called: delete and insert ; not yet finalized.
  10. Enter
  11. attributedSubstringForProposedRange(_:actualRange:) and selectedRange() are called multiple times: ignore.
  12. setMarkedText("하", selectedRange: NSRange(1, 0), replacementRange: NSRange(NotFound, 0)) is called: delete and insert ; not yet finalized.
  13. insertText("하", replacementRange: NSRange(NotFound, 0)) is called to finalize the input of .
  14. attributedSubstringForProposedRange(_:actualRange:) and selectedRange() are called multiple times: ignore.
  15. setMarkedText("태", selectedRange: NSRange(1, 0), replacementRange: NSRange(NotFound, 0)) is called: Since the replacement range is NotFound, append the marked text to the freshly finalized .
  16. ...

Hanja (한자)

Let's consider the even more complicated case: Hanja in Korean. In this case the selectedRange() and attributedSubstringForProposedRange(_:actualRange:) play a vital role and also

  • firstRectForCharacterRange(_:actualRange): this method is used to determine where to show the Hanja popup. The character range is determined by selectedRange().

Let's assume we want to enter : (again hasMarkedText() is called here and there...)

  1. Enter .
  2. setMarkedText("ㅎ", selectedRange: NSRange(1, 0) replacementRange:NSRange(NotFound, 0)) is called.
  3. Enter .
  4. attributedSubstringForProposedRange(_:actualRange:), selectedRange() and hasMarkedText() are called multiple times: again, for only Hangul, ignorable.
  5. setMarkedText("하", selectedRange: NSRange(1, 0), replacementRange: NSRange(NotFound, 0)) is called: delete and insert ; not yet finalized.
  6. Enter Opt-Return.
  7. setMarkedText("하", selectedRange: NSRange(1, 0), replacementRange: NSRange(NotFound, 0)) is called again.
  8. selectedRange() is called: here we should return a range which can be consistently used by attributedSubstringForProposedRange(_:actualRange) and firstRectForCharacterRange(_:actualRange).
  9. insertText("하", replacementRange: NSRange(NotFound, 0)) is called even we are not done yet... So our view thinks we finalized the input of .
  10. attributedSubstringForProposedRange(_:actualRange) is called multiple times to get the Hangul syllable to replace with Hanja. The proposed range can be very different in each call.
  11. Only if the range from selectedRange() could be somehow consistently used in attributedSubstringForProposedRange(_:actualRange), then the Hanja popup is displayed. Otherwise we get the selector insertNewlineIgnoringFieldEditor in doCommandBySelector().
  12. setMarkedText("下" , selectedRange: NSRange(1, 0), replacementRange: NSRange(1, 1)) is called: the replacement range is not NotFound which means that we first have to delete the text in the given range, in this case the finalized and then append the marked text.
  13. Selecting different Hanja calls the usual setMarkedText(_:selectedRange:actualRange) and Return finalizes the input of .

Chinese Pinyin

suppose we want to enter 中国

  1. we should enter the pinyin zhongguo, then <Space> to confirm it.
  2. each char input triggers: setMarkedText, markedRange, firstRect, attributedSubstringForProposedRange
  3. finally setMarkedText("zhong guo", selectedRange: NSRange(10, 0), replacementRange: NSRange(NotFound, 0)) iscalled:
  4. then after <Space> enter, insertText("中国", replacementRange: NSRange(NotFound, 0)) is called
  5. many selectedRange and attributedSubstring(forProposedRange:actualRange:) calls.

this seems right simple. but when in markedtext state(before confirming it),

  1. we can use number to select other candidates
  2. we can use =, -, <UP>, <DOWN>, <Left>, <Right> to choose candidate, and vim shouldn't handle it.
  3. we can use <Left>, <Right> to move in marked text, and insert char in middle of markedText. even complicate, the move is not by char, but by word.

each marked text or marked cursor changes, setMarkedText will called, with selectedRange point to the marked cursor position(may be middle, not the text end)

so these key shouldn't be handle by vim directly when in marked text state.

and finally we confirmed all markedtext, then a insertText will be called

Other Writing System

Not a clue, since I only know Latin alphabet and Korean (+Hanja)...