-
Notifications
You must be signed in to change notification settings - Fork 278
/
formatting.py
1055 lines (869 loc) · 34.6 KB
/
formatting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2010-2011 Joshua Harlan Lifton.
# See LICENSE.txt for details.
"""This module converts translations to printable text.
This module defines and implements plover's custom dictionary language.
"""
from enum import Enum
from os.path import commonprefix
from collections import namedtuple
import re
import string
from plover.orthography import add_suffix
from plover.registry import registry
Case = Enum('case', ((c, c.lower()) for c in '''
CAP_FIRST_WORD
LOWER
LOWER_FIRST_CHAR
TITLE
UPPER
UPPER_FIRST_WORD
'''.split()))
SPACE = ' '
META_ATTACH_FLAG = '^'
META_CAPITALIZE = '-|'
META_CARRY_CAPITALIZATION = '~|'
META_COMMAND = 'PLOVER:'
META_COMMAS = (',', ':', ';')
META_CUSTOM = ':'
META_GLUE_FLAG = '&'
META_KEY_COMBINATION = '#'
META_LOWER = '>'
META_MODE = 'MODE:'
META_RETRO_CAPITALIZE = '*-|'
META_RETRO_FORMAT = '*('
META_RETRO_LOWER = '*>'
META_RETRO_UPPER = '*<'
META_STOPS = ('.', '!', '?')
META_UPPER = '<'
MODE_CAMEL = 'CAMEL'
MODE_CAPS = 'CAPS'
MODE_LOWER = 'LOWER'
MODE_RESET = 'RESET'
MODE_RESET_CASE = 'RESET_CASE'
MODE_RESET_SPACE = 'RESET_SPACE'
MODE_SET_SPACE = 'SET_SPACE:'
MODE_SNAKE = 'SNAKE'
MODE_TITLE = 'TITLE'
META_ESCAPE = '\\'
RE_META_ESCAPE = '\\\\'
META_START = '{'
META_END = '}'
META_ESC_START = META_ESCAPE + META_START
META_ESC_END = META_ESCAPE + META_END
ATOM_RE = re.compile(r"""(?:%s%s|%s%s|[^%s%s])+ # One or more of anything
# other than unescaped { or }
#
| # or
#
%s(?:%s%s|%s%s|[^%s%s])*%s # Anything of the form {X}
# where X doesn't contain
# unescaped { or }
""" % (RE_META_ESCAPE, META_START, RE_META_ESCAPE,
META_END, META_START, META_END,
META_START,
RE_META_ESCAPE, META_START, RE_META_ESCAPE,
META_END, META_START, META_END,
META_END),
re.VERBOSE)
# A more human-readable version of the above RE is:
#
# re.compile(r"""(?:\\{|\\}|[^{}])+ # One or more of anything other than
# # unescaped { or }
# #
# | # or
# #
# {(?:\\{|\\}|[^{}])*} # Anything of the form {X} where X
# # doesn't contain unescaped { or }
# """, re.VERBOSE)
WORD_RX = re.compile(r'(?:\d+(?:[.,]\d+)+|[\'\w]+[-\w\']*|[^\w\s]+)\s*', re.UNICODE)
class RetroFormatter:
"""Helper for iterating over the result of previous translations.
Support iterating over previous actions, text, fragments of text, or words:
text : "Something something, blah! Blah: 45.8... (blah: foo42) "
fragments: "__________-----------______------________-------_________"
words : "__________---------__----__----__----____-____--_____----"
"""
FRAGMENT_RX = re.compile(r'\s*[^\s]+\s*|^\s*$')
def __init__(self, previous_translations):
self.previous_translations = previous_translations
def iter_last_actions(self):
"""Iterate over past actions (last first)."""
for translation in reversed(self.previous_translations):
yield from reversed(translation.formatting)
def iter_last_fragments(self):
"""Iterate over last text fragments (last first).
A text fragment is a series of non-whitespace characters
followed by zero or more trailing whitespace characters.
"""
replace = 0
next_action = None
current_fragment = ''
for action in self.iter_last_actions():
part = '' if action.text is None else action.text
if next_action is not None and \
next_action.text is not None and \
not next_action.prev_attach:
part += next_action.space_char
if replace:
# Ignore replaced content.
if len(part) > replace:
part = part[:-replace]
replace = 0
else:
replace -= len(part)
part = ''
if part:
# Find out new complete fragments.
fragments = self.FRAGMENT_RX.findall(part + current_fragment)
yield from reversed(fragments[1:])
current_fragment = fragments[0]
replace += len(action.prev_replace)
next_action = action
# Don't forget to process the current (first) fragment.
if not current_fragment.isspace():
yield current_fragment.lstrip()
def last_fragments(self, count=1):
"""Return the last <count> text fragments."""
fragment_list = []
for fragment in self.iter_last_fragments():
fragment_list.insert(0, fragment)
if len(fragment_list) == count:
break
return fragment_list
def iter_last_words(self, strip=False, rx=WORD_RX):
"""Iterate over last words (last first).
If <strip> is False, then trailing whitespace is included
as part of each word (useful for calculating position).
For <strip> to be properly supported when a custom regexp is
passed as <rx>, then it must include trailing whitespace as
part of each word.
"""
assert not rx.groups
for fragment in self.iter_last_fragments():
# Split each fragment into words.
for word in reversed(rx.findall(fragment)):
yield word.rstrip() if strip else word
def last_words(self, count=1, strip=False, rx=WORD_RX):
"""Return the last <count> words."""
word_list = []
for w in self.iter_last_words(strip=strip, rx=rx):
word_list.insert(0, w)
if len(word_list) == count:
break
return word_list
def last_text(self, size):
"""Return the last <size> characters."""
text = ''
if not size:
return text
for fragment in self.iter_last_fragments():
text = fragment + text
if len(text) >= size:
break
return text[-size:]
class _Context(RetroFormatter):
"""Context for formatting translations to actions.
Keep tracks of previous actions as well as newly translated actions,
offer helpers for creating new actions and convenient access to past
actions/text/words.
"""
def __init__(self, previous_translations, last_action):
super().__init__(previous_translations)
assert last_action is not None
self.last_action = last_action
self.translated_actions = []
def new_action(self):
"""Create a new action, only copying global state."""
return self.last_action.new_state()
def copy_last_action(self):
"""Create a new action, cloning the last action state."""
return self.last_action.copy_state()
def translated(self, action):
"""Mark an action as translated."""
assert action is not None
self.translated_actions.append(action)
self.last_action = action
def iter_last_actions(self):
"""Custom iterator with support for newly translated actions."""
yield from reversed(self.translated_actions)
yield from super().iter_last_actions()
class Formatter:
"""Convert translations into output.
The main entry point for this class is format, which takes in translations
to format. Output is sent via an output class passed in through set_output.
Other than setting the output, the formatter class is stateless.
The output class can define the following functions, which will be called
if available:
send_backspaces -- Takes a number and deletes back that many characters.
send_string -- Takes a string and prints it verbatim.
send_key_combination -- Takes a string the dictionary format for specifying
key combinations and issues them.
send_engine_command -- Takes a string which names the special command to
execute.
"""
output_type = namedtuple(
'output', ['send_backspaces', 'send_string', 'send_key_combination',
'send_engine_command'])
def __init__(self):
self.set_output(None)
self.spaces_after = False
self.last_output_spaces_after = False
self.start_capitalized = False
self.start_attached = False
self._listeners = set()
def add_listener(self, callback):
"""Add a listener for translation outputs.
Arguments:
callback -- A function that takes: a list of translations to undo, a
list of new translations to render, and a translation that is the
context for the new translations.
"""
self._listeners.add(callback)
def remove_listener(self, callback):
"""Remove a listener added by add_listener."""
self._listeners.remove(callback)
def set_output(self, output):
"""Set the output class."""
noop = lambda x: None
output_type = self.output_type
fields = output_type._fields
self._output = output_type(*[getattr(output, f, noop) for f in fields])
def set_space_placement(self, s):
# Set whether spaces will be inserted
# before the output or after the output
self.spaces_after = bool(s == 'After Output')
def format(self, undo, do, prev):
"""Format the given translations.
Arguments:
undo -- A sequence of translations that should be undone. The
formatting parameter of the translations will be used to undo the
actions that were taken, if possible.
do -- The new actions to format. The formatting attribute will be
filled in with the result.
prev -- The last translation before the new actions in do. This
translation's formatting attribute provides the context for the new
rendered translations. If there is no context then this may be None.
"""
assert undo or do
if do:
last_action = None
if prev:
previous_translations = prev
if prev[-1].formatting:
last_action = prev[-1].formatting[-1]
else:
previous_translations = []
if last_action is None:
# Initial output.
next_attach = self.start_attached or self.spaces_after
next_case = Case.CAP_FIRST_WORD if self.start_capitalized else None
last_action = _Action(next_attach=next_attach, next_case=next_case)
ctx = _Context(previous_translations, last_action)
for t in do:
if t.english:
t.formatting = _translation_to_actions(t.english, ctx)
else:
t.formatting = _raw_to_actions(t.rtfcre[0], ctx)
new = ctx.translated_actions
else:
new = []
old = [a for t in undo for a in t.formatting]
# Figure out what really changed.
min_length = min(len(old), len(new))
for i in range(min_length):
if old[i] != new[i]:
break
else:
i = min_length
if i > 0:
optimized_away = old[:i]
old = old[i:]
new = new[i:]
else:
optimized_away = []
# Notify listeners.
for callback in self._listeners:
callback(old, new)
# Render output.
if optimized_away:
last_action = optimized_away[-1]
elif prev and prev[-1].formatting:
last_action = prev[-1].formatting[-1]
else:
last_action = None
OutputHelper(self._output, self.last_output_spaces_after,
self.spaces_after).render(last_action, old, new)
self.last_output_spaces_after = self.spaces_after
class TextFormatter:
"""Format a series of action into text."""
def __init__(self, spaces_after):
self.spaces_after = spaces_after
# Initial replaced text.
self.replaced_text = ''
# New appended text.
self.appended_text = ''
self.trailing_space = ''
def _render_action(self, action):
if self.spaces_after and self.trailing_space:
assert self.appended_text.endswith(self.trailing_space)
self.appended_text = self.appended_text[:-len(self.trailing_space)]
if action.prev_replace:
replaced = len(action.prev_replace)
appended = len(self.appended_text)
if replaced > appended:
assert action.prev_replace.endswith(self.appended_text)
replaced -= appended
if replaced > len(self.replaced_text):
assert action.prev_replace.endswith(self.replaced_text)
self.replaced_text = action.prev_replace[:replaced]
else:
assert self.replaced_text.endswith(action.prev_replace)
self.replaced_text = self.replaced_text[:-replaced]
self.replaced_text += action.prev_replace[:replaced]
self.appended_text = ''
else:
assert self.appended_text.endswith(action.prev_replace)
self.appended_text = self.appended_text[:-replaced]
if not action.prev_attach:
self.appended_text += action.space_char
self.appended_text += action.text
if self.spaces_after and not action.next_attach:
self.appended_text += action.space_char
self.trailing_space = action.space_char
else:
self.trailing_space = ''
def render(self, action_list, last_action):
"""Render a series of action.
Note: the function is a generator that yields non-text
actions (commands, combos, ...) for special processing.
"""
if self.spaces_after and last_action is not None:
self.trailing_space = last_action.trailing_space
self.appended_text = last_action.trailing_space
for action in action_list:
if action.text is None:
yield action
else:
self._render_action(action)
def reset(self, trailing_space):
"""Reset current state (rendered text)."""
self.replaced_text = ''
self.appended_text = trailing_space
class OutputHelper:
"""A helper class for minimizing the amount of change on output.
This class figures out the current state, compares it to the new output and
optimizes away extra backspaces and typing.
"""
def __init__(self, output, before_spaces_after, after_spaces_after):
self.output = output
self.before = TextFormatter(before_spaces_after)
self.after = TextFormatter(after_spaces_after)
def flush(self):
# FIXME:
# - what about things like emoji zwj sequences?
# - normalize strings to better handle combining characters?
#
# >>> u"C\u0327"
# 'Ç'
# >>> len(u"C\u0327")
# 2
# >>> len(unicodedata.normalize('NFC', u"C\u0327"))
# 1
if len(self.before.replaced_text) > len(self.after.replaced_text):
assert self.before.replaced_text.endswith(self.after.replaced_text)
replaced_text = self.before.replaced_text
else:
assert self.after.replaced_text.endswith(self.before.replaced_text)
replaced_text = self.after.replaced_text
before = replaced_text[:len(replaced_text)-len(self.before.replaced_text)] + self.before.appended_text
after = replaced_text[:len(replaced_text)-len(self.after.replaced_text)] + self.after.appended_text
common_length = len(commonprefix([before, after]))
erased = len(before) - common_length
if erased:
self.output.send_backspaces(erased)
appended = after[common_length:]
if appended:
self.output.send_string(appended)
self.before.reset(self.after.trailing_space)
self.after.reset(self.after.trailing_space)
def render(self, last_action, undo, do):
# Render undone actions, ignoring non-text actions.
for action in self.before.render(undo, last_action):
pass
# Render new actions.
for action in self.after.render(do, last_action):
self.flush()
if action.combo:
self.output.send_key_combination(action.combo)
elif action.command:
self.output.send_engine_command(action.command)
self.flush()
class _Action:
"""A hybrid class that stores instructions and resulting state.
A single translation may be formatted into one or more actions. The
instructions are used to render the current action and the state is used as
context to render future translations.
"""
def __init__(self,
# Previous.
prev_attach=False, prev_replace='',
# Current.
glue=False, word=None, orthography=True, space_char=' ',
upper_carry=False, case=None, text=None, trailing_space='',
combo=None, command=None,
# Next.
next_attach=False, next_case=None
):
"""Initialize a new action.
Arguments:
prev_attach -- True if there should be no space between this and the
previous action.
prev_replace -- Text that should be deleted for this action.
glue -- True if there be no space between this and the next action if
the next action also has glue set to True.
word -- The current root word (sans prefix, and un-cased). This is
context for future actions whose behavior depends on it such as
suffixes.
upper_carry -- True if we are uppercasing the current word.
othography -- True if orthography rules should be applies when adding
a suffix to this action.
space_char -- this character will replace spaces after all other
formatting has been applied
case -- an integer to determine which case to output after formatting
text -- The text that should be rendered for this action.
trailing_space -- This the space that would be added when rendering
up to this action with space placement set to
'after output'.
combo -- The key combo, in plover's key combo language, that should be
executed for this action.
command -- The command that should be executed for this action.
next_attach -- True if there should be no space between this and the next
action.
next_case -- Case to apply to next action: capitalize/lower/upper...
"""
# State variables
self.prev_attach = prev_attach
self.glue = glue
self.word = word
self.upper_carry = upper_carry
self.orthography = orthography
self.next_attach = next_attach
self.next_case = next_case
# Persistent state variables
self.space_char = space_char
self.case = case
self.trailing_space = trailing_space
# Instruction variables
self.prev_replace = prev_replace
self.text = text
self.combo = combo
self.command = command
def copy_state(self):
"""Clone this action but only clone the state variables."""
return _Action(
# Previous.
prev_attach=self.next_attach,
# Current.
case=self.case, glue=self.glue, orthography=self.orthography,
space_char=self.space_char, upper_carry=self.upper_carry,
word=self.word, trailing_space=self.trailing_space,
# Next.
next_attach=self.next_attach, next_case=self.next_case,
)
def new_state(self):
return _Action(
# Previous.
prev_attach=self.next_attach,
# Current.
space_char=self.space_char, case=self.case,
trailing_space=self.trailing_space,
# Next.
)
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __ne__(self, other):
return not self == other
def __str__(self):
kwargs = [
'%s=%r' % (k, v)
for k, v in self.__dict__.items()
if v != self.DEFAULT.__dict__[k]
]
return 'Action(%s)' % ', '.join(sorted(kwargs))
def __repr__(self):
return str(self)
_Action.DEFAULT = _Action()
def _translation_to_actions(translation, ctx):
"""Create actions for a translation.
Arguments:
translation -- A string with the translation to render.
last_action -- The action in whose context this translation is formatted.
Returns: A list of actions.
"""
# Reduce the translation to atoms. An atom is an irreducible string that is
# either entirely a single meta command or entirely text containing no meta
# commands.
if translation.isdigit():
# If a translation is only digits then glue it to neighboring digits.
atoms = [_glue_translation(translation)]
else:
atoms = filter(None, (
x.strip(' ') for x in ATOM_RE.findall(translation))
)
action_list = []
for atom in atoms:
action = _atom_to_action(atom, ctx)
action_list.append(action)
ctx.translated(action)
if not action_list:
action = ctx.copy_last_action()
action_list = [action]
ctx.translated(action)
return action_list
def _raw_to_actions(stroke, ctx):
"""Turn a raw stroke into actions.
Arguments:
stroke -- A string representation of the stroke.
last_action -- The context in which the new actions are created
Returns: A list of actions.
"""
# If a raw stroke is composed of digits then remove the dash (if
# present) and glue it to any neighboring digits. Otherwise, just
# output the raw stroke as is.
no_dash = stroke.replace('-', '', 1)
if no_dash.isdigit():
return _translation_to_actions(no_dash, ctx)
action = _Action(text=stroke, word=stroke,
case=ctx.last_action.case,
prev_attach=ctx.last_action.next_attach,
space_char=ctx.last_action.space_char,
trailing_space=ctx.last_action.space_char)
ctx.translated(action)
return [action]
def _atom_to_action(atom, ctx):
"""Convert an atom into an action.
Arguments:
atom -- A string holding an atom. An atom is an irreducible string that is
either entirely a single meta command or entirely text containing no meta
commands.
last_action -- The context in which the new action takes place.
Returns: An action for the atom.
"""
meta = _get_meta(atom)
if meta is not None:
meta = _unescape_atom(meta)
if meta in META_COMMAS:
action = _apply_meta_comma(meta, ctx)
elif meta in META_STOPS:
action = _apply_meta_stop(meta, ctx)
elif meta == META_CAPITALIZE:
action = _apply_meta_case(Case.CAP_FIRST_WORD, ctx)
elif meta == META_LOWER:
action = _apply_meta_case(Case.LOWER_FIRST_CHAR, ctx)
elif meta == META_UPPER:
action = _apply_meta_case(Case.UPPER_FIRST_WORD, ctx)
elif meta == META_RETRO_CAPITALIZE:
action = _apply_meta_retro_case(Case.CAP_FIRST_WORD, ctx)
elif meta == META_RETRO_LOWER:
action = _apply_meta_retro_case(Case.LOWER_FIRST_CHAR, ctx)
elif meta == META_RETRO_UPPER:
action = _apply_meta_retro_case(Case.UPPER_FIRST_WORD, ctx)
elif (meta.startswith(META_CARRY_CAPITALIZATION) or
meta.startswith(META_ATTACH_FLAG + META_CARRY_CAPITALIZATION)):
action = _apply_meta_carry_capitalize(meta, ctx)
elif meta.startswith(META_RETRO_FORMAT):
action = _apply_meta_currency(meta, ctx)
elif meta.startswith(META_COMMAND):
action = _apply_meta_command(meta, ctx)
elif meta.startswith(META_MODE):
action = _apply_meta_mode(meta, ctx)
elif meta.startswith(META_GLUE_FLAG):
action = _apply_meta_glue(meta, ctx)
elif (meta.startswith(META_ATTACH_FLAG) or
meta.endswith(META_ATTACH_FLAG)):
action = _apply_meta_attach(meta, ctx)
elif meta.startswith(META_KEY_COMBINATION):
action = _apply_meta_combo(meta, ctx)
elif meta.startswith(META_CUSTOM):
meta_args = meta[1:].split(':', 1)
meta_fn = registry.get_plugin('meta', meta_args[0]).obj
action = meta_fn(ctx, meta_args[1] if len(meta_args) == 2 else '')
else:
action = ctx.new_action()
else:
action = ctx.new_action()
action.text = _unescape_atom(atom)
# Finalize action's text.
text = action.text
if text is not None:
# Update word.
if action.word is None:
last_word = None
if action.glue and ctx.last_action.glue:
last_word = ctx.last_action.word
action.word = _rightmost_word((last_word or '') + text)
# Apply case.
case = ctx.last_action.next_case
if case is None and action.prev_attach and ctx.last_action.upper_carry:
case = Case.UPPER_FIRST_WORD
text = _apply_case(text, case)
if case == Case.UPPER_FIRST_WORD:
action.upper_carry = not _has_word_boundary(text)
# Apply mode.
action.text = _apply_mode(text, action.case, action.space_char,
action.prev_attach, ctx.last_action)
# Update trailing space.
action.trailing_space = '' if action.next_attach else action.space_char
return action
def _apply_meta_attach(meta, ctx):
action = ctx.new_action()
begin = meta.startswith(META_ATTACH_FLAG)
end = meta.endswith(META_ATTACH_FLAG)
if begin:
meta = meta[len(META_ATTACH_FLAG):]
action.prev_attach = True
if end:
meta = meta[:-len(META_ATTACH_FLAG)]
action.next_attach = True
last_word = ctx.last_action.word or ''
if not meta:
# We use an empty connection to indicate a "break" in the
# application of orthography rules. This allows the
# stenographer to tell Plover not to auto-correct a word.
action.orthography = False
elif (
last_word and
not meta.isspace() and
ctx.last_action.orthography and
begin and (not end or _has_word_boundary(meta))
):
new_word = add_suffix(last_word, meta)
common_len = len(commonprefix([last_word, new_word]))
replaced = last_word[common_len:]
action.prev_replace = ctx.last_text(len(replaced))
assert replaced.lower() == action.prev_replace.lower()
last_word = last_word[:common_len]
meta = new_word[common_len:]
action.text = meta
if action.prev_attach:
action.word = _rightmost_word(last_word + meta)
return action
def _apply_meta_comma(meta, ctx):
action = ctx.new_action()
action.text = meta
action.prev_attach = True
return action
def _apply_meta_stop(meta, ctx):
action = ctx.new_action()
action.prev_attach = True
action.text = meta
action.next_case = Case.CAP_FIRST_WORD
return action
def _apply_meta_case(meta, ctx):
action = ctx.copy_last_action()
action.next_case = meta
return action
def _apply_meta_retro_case(meta, ctx):
action = ctx.copy_last_action()
action.prev_attach = True
last_words = ctx.last_words(count=1)
if last_words:
action.prev_replace = last_words[0]
action.text = _apply_case(last_words[0], meta)
else:
action.text = ''
return action
def _apply_meta_combo(meta, ctx):
action = ctx.copy_last_action()
action.combo = meta[len(META_KEY_COMBINATION):]
return action
def _apply_meta_command(meta, ctx):
action = ctx.copy_last_action()
action.command = meta[len(META_COMMAND):]
return action
def _apply_meta_glue(meta, ctx):
action = ctx.new_action()
action.glue = True
action.text = meta[len(META_GLUE_FLAG):]
if ctx.last_action.glue:
action.prev_attach = True
return action
def _apply_meta_currency(meta, ctx):
action = ctx.copy_last_action()
if not meta.endswith(')'):
return action
dict_format = meta[len(META_RETRO_FORMAT):-len(')')]
last_words = ctx.last_words(count=1)
if not last_words:
return action
for cast, fmt in (
(float, '{:,.2f}'),
(int, '{:,}' ),
):
try:
cast_input = cast(last_words[0])
except ValueError:
pass
else:
currency_format = dict_format.replace('c', fmt)
action.prev_attach = True
action.prev_replace = last_words[0]
action.text = currency_format.format(cast_input)
action.word = None
return action
def _apply_meta_carry_capitalize(meta, ctx):
# Meta format: ^~|content^ (attach flags are optional)
action = ctx.new_action()
if ctx.last_action.next_case == Case.CAP_FIRST_WORD:
action.next_case = Case.CAP_FIRST_WORD
begin = meta.startswith(META_ATTACH_FLAG)
if begin:
meta = meta[len(META_ATTACH_FLAG):]
action.prev_attach = True
meta = meta[len(META_CARRY_CAPITALIZATION):]
end = meta.endswith(META_ATTACH_FLAG)
if end:
meta = meta[:-len(META_ATTACH_FLAG)]
action.next_attach = True
if meta or begin or end:
action.text = meta
return action
def _apply_meta_mode(meta, ctx):
"""
command should be:
CAPS, LOWER, TITLE, CAMEL, SNAKE, RESET_SPACE,
RESET_CASE, SET_SPACE or RESET
CAPS: UPPERCASE
LOWER: lowercase
TITLE: Title Case
CAMEL: titleCase, no space, initial lowercase
SNAKE: Underscore_space
RESET_SPACE: Space resets to ' '
RESET_CASE: Reset to normal case
SET_SPACE:xy: Set space to xy
RESET: Reset to normal case, space resets to ' '
"""
action = ctx.copy_last_action()
command = meta[len(META_MODE):]
if command == MODE_CAPS:
action.case = Case.UPPER
elif command == MODE_TITLE:
action.case = Case.TITLE
elif command == MODE_LOWER:
action.case = Case.LOWER
elif command == MODE_SNAKE:
action.space_char = '_'
elif command == MODE_CAMEL:
action.case = Case.TITLE
action.space_char = ''
action.next_case = Case.LOWER_FIRST_CHAR
elif command == MODE_RESET:
action.space_char = SPACE
action.case = None
elif command == MODE_RESET_SPACE:
action.space_char = SPACE
elif command == MODE_RESET_CASE:
action.case = None
elif command.startswith(MODE_SET_SPACE):
action.space_char = command[len(MODE_SET_SPACE):]
return action
def _apply_case(text, case):
if case is None:
return text
if case == Case.CAP_FIRST_WORD:
return _capitalize_first_word(text)
if case == Case.LOWER_FIRST_CHAR:
return _lower_first_character(text)
if case == Case.UPPER_FIRST_WORD:
return _upper_first_word(text)
raise ValueError('%r is not a valid case' % case)
def _apply_mode(text, case, space_char, begin, last_action):
# Should title case be applied to the beginning of the next string?
lower_title_case = (begin and not
last_action.case in (
Case.CAP_FIRST_WORD,
Case.UPPER_FIRST_WORD,
))
# Apply case, then replace space character
text = _apply_mode_case(text, case, lower_title_case)
text = _apply_mode_space_char(text, space_char)
# Title case is sensitive to lower flag
if (last_action.next_case == Case.LOWER_FIRST_CHAR
and text and case == Case.TITLE):
text = _lower_first_character(text)
return text
def _apply_mode_case(text, case, appended):
if case is None:
return text
if case == Case.LOWER:
return text.lower()
if case == Case.UPPER:
return text.upper()
if case == Case.TITLE:
# Do nothing to appended output
if appended:
return text
return _capitalize_all_words(text)
raise ValueError('%r is not a valid case' % case)
def _apply_mode_space_char(text, space_char):
if space_char == SPACE:
return text
return text.replace(SPACE, space_char)
def _get_meta(atom):
"""Return the meta command, if any, without surrounding meta markups."""
if (atom is not None and
atom.startswith(META_START) and
atom.endswith(META_END)):
return atom[len(META_START):-len(META_END)]
return None
def _glue_translation(s):
"""Mark the given string as a glue stroke."""
return META_START + META_GLUE_FLAG + s + META_END
def _unescape_atom(atom):
"""Replace escaped meta markups with unescaped meta markups."""
atom = atom.replace(META_ESC_START, META_START)
atom = atom.replace(META_ESC_END, META_END)
return atom
def _capitalize_first_word(s):
"""Capitalize the first letter of s.