|
| 1 | +from os.path import commonprefix |
| 2 | + |
| 3 | +from plover.formatting import ( |
| 4 | + Case, |
| 5 | + META_ATTACH_FLAG, |
| 6 | + META_CARRY_CAPITALIZATION, |
| 7 | + has_word_boundary, |
| 8 | + rightmost_word, |
| 9 | +) |
| 10 | +from plover.orthography import add_suffix |
| 11 | + |
| 12 | + |
| 13 | +def meta_attach(ctx, meta): |
| 14 | + action = ctx.new_action() |
| 15 | + begin = meta.startswith(META_ATTACH_FLAG) |
| 16 | + end = meta.endswith(META_ATTACH_FLAG) |
| 17 | + if begin: |
| 18 | + meta = meta[len(META_ATTACH_FLAG):] |
| 19 | + action.prev_attach = True |
| 20 | + if end: |
| 21 | + meta = meta[:-len(META_ATTACH_FLAG)] |
| 22 | + action.next_attach = True |
| 23 | + last_word = ctx.last_action.word or '' |
| 24 | + if not meta: |
| 25 | + # We use an empty connection to indicate a "break" in the |
| 26 | + # application of orthography rules. This allows the |
| 27 | + # stenographer to tell Plover not to auto-correct a word. |
| 28 | + action.orthography = False |
| 29 | + elif ( |
| 30 | + last_word and |
| 31 | + not meta.isspace() and |
| 32 | + ctx.last_action.orthography and |
| 33 | + begin and (not end or has_word_boundary(meta)) |
| 34 | + ): |
| 35 | + new_word = add_suffix(last_word, meta) |
| 36 | + common_len = len(commonprefix([last_word, new_word])) |
| 37 | + replaced = last_word[common_len:] |
| 38 | + action.prev_replace = ctx.last_text(len(replaced)) |
| 39 | + assert replaced.lower() == action.prev_replace.lower() |
| 40 | + last_word = last_word[:common_len] |
| 41 | + meta = new_word[common_len:] |
| 42 | + action.text = meta |
| 43 | + if action.prev_attach: |
| 44 | + action.word = rightmost_word(last_word + meta) |
| 45 | + return action |
| 46 | + |
| 47 | +def meta_carry_capitalize(ctx, meta): |
| 48 | + # Meta format: ^~|content^ (attach flags are optional) |
| 49 | + action = ctx.new_action() |
| 50 | + if ctx.last_action.next_case == Case.CAP_FIRST_WORD: |
| 51 | + action.next_case = Case.CAP_FIRST_WORD |
| 52 | + begin = meta.startswith(META_ATTACH_FLAG) |
| 53 | + if begin: |
| 54 | + meta = meta[len(META_ATTACH_FLAG):] |
| 55 | + action.prev_attach = True |
| 56 | + meta = meta[len(META_CARRY_CAPITALIZATION):] |
| 57 | + end = meta.endswith(META_ATTACH_FLAG) |
| 58 | + if end: |
| 59 | + meta = meta[:-len(META_ATTACH_FLAG)] |
| 60 | + action.next_attach = True |
| 61 | + if meta or begin or end: |
| 62 | + action.text = meta |
| 63 | + return action |
0 commit comments