/
TextEditor.class.st
2464 lines (2042 loc) · 72.8 KB
/
TextEditor.class.st
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
"
See comment in Editor.
My instances edit Text, this is, they support multiple lines and TextAttributes.
They have no specific facilities for editing Smalltalk code. Those are found in SmalltalkEditor.
"
Class {
#name : #TextEditor,
#superclass : #Editor,
#instVars : [
'model',
'editingState',
'selectionShowing'
],
#classVars : [
'CaseSensitiveFinds',
'UseFindReplaceSelection',
'UseSecondarySelection',
'UseSelectionBar',
'WalkAlongDisplayedLine'
],
#classInstVars : [
'cmdActions',
'shiftCmdActions'
],
#category : #'Text-Edition'
}
{ #category : #shortcuts }
TextEditor class >> buildTextEditorShortcutsOn: aBuilder [
<keymap>
(aBuilder shortcut: #toggleCommentOnSelectionOrLine)
category: #TextEditor
default: PharoShortcuts current toggleCommentOnSelectionOrLine
do: [ :target | target handleEdit: [ target toggleCommentOnSelectionOrLine ] ].
(aBuilder shortcut: #selectAll)
category: #TextEditor
default: PharoShortcuts current selectAllShortcut
do: [ :target | target handleEdit: [ target selectAll ] ].
(aBuilder shortcut: #copySelection)
category: #TextEditor
default: PharoShortcuts current copySelectionShortcut
do: [ :target | target copySelection ].
(aBuilder shortcut: #find)
category: #TextEditor
default: PharoShortcuts current findShortcut
do: [ :target | target find ].
(aBuilder shortcut: #findAgain)
category: #TextEditor
default: PharoShortcuts current findAgainShortcut
do: [ :target | target findAgain ].
(aBuilder shortcut: #setSearchString)
category: #TextEditor
default: PharoShortcuts current setSearchStringShortcut
do: [ :target | target setSearchString ].
(aBuilder shortcut: #cancel)
category: #TextEditor
default: PharoShortcuts current cancelShortcut
do: [ :target | target cancelWithoutConfirmation ].
(aBuilder shortcut: #paste)
category: #TextEditor
default: PharoShortcuts current pasteShortcut
do: [ :target | target paste ].
(aBuilder shortcut: #cut)
category: #TextEditor
default: PharoShortcuts current cutShortcut
do: [ :target | target cut ].
(aBuilder shortcut: #undo)
category: #TextEditor
default: PharoShortcuts current undoShortcut
do: [ :target | target undo ].
(aBuilder shortcut: #redo)
category: #TextEditor
default: PharoShortcuts current redoShortcut
do: [ :target | target redo ].
(aBuilder shortcut: #cursorHome)
category: #TextEditor
default: PharoShortcuts current cursorHomeShortcut
do: [ :target :morph :event | target cursorHome: event ].
(aBuilder shortcut: #cursorEnd)
category: #TextEditor
default: PharoShortcuts current cursorEndShortcut
do: [ :target :morph :event | target cursorEnd: event ].
]
{ #category : #shortcuts }
TextEditor class >> buildTextEditorTabKeymapOn: aBuilder [
"This is to mask the tab or shift tab character at the system window level."
<keymap>
(aBuilder shortcut: #doTab)
category: #TextEditorTab
default: Character tab asKeyCombination
do: [ :target | target handleEdit: [ target
handleEdit: [
target replaceSelectionWith: ' '.
target selectInvisiblyFrom: target selectionInterval last + 1 to: target selectionInterval last ] ] ]
]
{ #category : #settings }
TextEditor class >> caseSensitiveFinds [
^ CaseSensitiveFinds ifNil: [CaseSensitiveFinds := false]
]
{ #category : #settings }
TextEditor class >> caseSensitiveFinds: aBoolean [
CaseSensitiveFinds := aBoolean
]
{ #category : #accessing }
TextEditor class >> cmdActions [
^cmdActions
]
{ #category : #'instance creation' }
TextEditor class >> forMorph: aMorph [
^ self new initWithMorph: aMorph
]
{ #category : #'class initialization' }
TextEditor class >> initialize [
"Initialize the keyboard shortcut maps and the shared buffers
for copying text across views and managing again and undo."
"
TextEditor initialize
"
self initializeCmdKeyShortcuts.
self initializeShiftCmdKeyShortcuts.
]
{ #category : #'keyboard shortcut tables' }
TextEditor class >> initializeBasicCmdKeyShortcuts [
"Initialize the (unshifted) command-key (or alt-key) shortcut table.
TextEditor initialize
"
| cmdMap |
cmdMap := Array new: 256 withAll: #noop:. "use temp in case of a crash"
cmdMap at: 1 + 1 put: #cursorHome:. "home key"
cmdMap at: 4 + 1 put: #cursorEnd:. "end key"
cmdMap at: 8 + 1 put: #backspace:. "ctrl-H or delete key"
cmdMap at: 11 + 1 put: #cursorPageUp:. "page up key"
cmdMap at: 12 + 1 put: #cursorPageDown:. "page down key"
cmdMap at: 13 + 1 put: #crWithIndent:. "cmd-Return"
cmdMap at: 27 + 1 put: #escape:. "escape key"
cmdMap at: 28 + 1 put: #cursorLeft:. "left arrow key"
cmdMap at: 29 + 1 put: #cursorRight:. "right arrow key"
cmdMap at: 30 + 1 put: #cursorUp:. "up arrow key"
cmdMap at: 31 + 1 put: #cursorDown:. "down arrow key"
"cmdMap at: 32 + 1 put: #selectWord:." "space bar key"
cmdMap at: 127 + 1 put: #forwardDelete:. "del key"
"cmdMap at: 115 + 1 put:#accept:".
#(
$a #selectAll:
"$c #copySelection:"
$e #exchange:
"$f #find: "
"$g #findAgain: "
"$h #setSearchString: "
"$v #paste: "
$w #backWord:
"$x #cut:"
$y #swapChars:
"$z #undo:"
$j #redo:
"$u #exploreUndoManager: ")
pairsDo: [ :char :symbol|
cmdMap at: char asciiValue + 1 put: symbol].
cmdActions := cmdMap
]
{ #category : #'keyboard shortcut tables' }
TextEditor class >> initializeBasicShiftCmdKeyShortcuts [
"Initialize the shift-command-key (or control-key) shortcut table."
"TextEditor initialize"
| cmdMap |
"shift-command and control shortcuts"
cmdMap := Array new: 256 withAll: #noop:. "use temp in case of a crash"
cmdMap at: ( 1 + 1) put: #cursorHome:. "home key"
cmdMap at: ( 4 + 1) put: #cursorEnd:. "end key"
cmdMap at: ( 8 + 1) put: #forwardDelete:. "ctrl-H or delete key"
cmdMap at: (11 + 1) put: #cursorPageUp:. "page up key"
cmdMap at: (12 + 1) put: #cursorPageDown:. "page down key"
cmdMap at: (13 + 1) put: #crWithIndent:. "ctrl-Return"
cmdMap at: (27 + 1) put: #offerMenuFromEsc:. "escape key"
cmdMap at: (28 + 1) put: #cursorLeft:. "left arrow key"
cmdMap at: (29 + 1) put: #cursorRight:. "right arrow key"
cmdMap at: (30 + 1) put: #cursorUp:. "up arrow key"
cmdMap at: (31 + 1) put: #cursorDown:. "down arrow key"
cmdMap at: (32 + 1) put: #selectWord:. "space bar key"
cmdMap at: (45 + 1) put: #changeEmphasis:. "cmd-sh-minus"
cmdMap at: (61 + 1) put: #changeEmphasis:. "cmd-sh-plus"
cmdMap at: (127 + 1) put: #forwardDelete:. "del key"
#(
$c compareToClipboard:
$d duplicate:
$h cursorTopHome:
$j doAgainMany:
$k changeStyle:
$l outdent:
$m selectCurrentTypeIn:
$r indent:
$s search:
$u changeLfToCr:
$x makeLowercase:
$y makeUppercase:
$z makeCapitalized:
"$g #findAgain:"
) pairsDo: [ :char :symbol|
cmdMap at: char asciiValue + 1 put: symbol. "plain keys"
cmdMap at: char asciiValue - 32 + 1 put: symbol. "shifted keys"
cmdMap at: char asciiValue - 96 + 1 put: symbol. "ctrl keys"
].
shiftCmdActions := cmdMap
]
{ #category : #'keyboard shortcut tables' }
TextEditor class >> initializeCmdKeyShortcuts [
"Initialize the (unshifted) command-key (or alt-key) shortcut table."
"
TextEditor initialize
"
| cmds |
self initializeBasicCmdKeyShortcuts.
'0123456789-='
do: [:char | cmdActions at: char asciiValue + 1 put: #changeEmphasis:].
cmds := #($k #offerFontMenu: $u #align: ).
1 to: cmds size
by: 2
do: [:i | self cmdActions at: (cmds at: i) asciiValue + 1 put: (cmds at: i + 1)]
]
{ #category : #'keyboard shortcut tables' }
TextEditor class >> initializeShiftCmdKeyShortcuts [
"Initialize the shift-command-key (or control-key) shortcut table."
"wod 11/3/1998: Fix setting of cmdMap for shifted keys to actually use the
capitalized versions of the letters.
TPR 2/18/99: add the plain ascii values back in for those VMs that don't return the shifted values."
"TextEditor initialize"
| cmds |
self initializeBasicShiftCmdKeyShortcuts.
self shiftCmdActions at: (45 + 1) put: #changeEmphasis:. "cmd-sh-minus"
self shiftCmdActions at: (61 + 1) put: #changeEmphasis:. "cmd-sh-plus"
cmds := #(
$k changeStyle:
"$u #clearUndoManager: "
).
1 to: cmds size by: 2 do: [ :i |
self shiftCmdActions at: ((cmds at: i) asciiValue + 1) put: (cmds at: i + 1). "plain keys"
self shiftCmdActions at: ((cmds at: i) asciiValue - 32 + 1) put: (cmds at: i + 1). "shifted keys"
self shiftCmdActions at: ((cmds at: i) asciiValue - 96 + 1) put: (cmds at: i + 1). "ctrl keys"
].
]
{ #category : #accessing }
TextEditor class >> shiftCmdActions [
^shiftCmdActions
]
{ #category : #'menu declaration' }
TextEditor class >> textEditorMenuOn: aBuilder [
"Specify the menu used when writing text. Try it with:
(PragmaMenuBuilder
pragmaKeyword: 'textEditorMenu'
model: nil) menu popUpInWorld"
<contextMenu>
<textEditorMenu>
(aBuilder item: #'Find...' translated)
keyText: 'f';
selector: #find;
iconName: #smallFindIcon.
(aBuilder item: #'Find again' translated)
keyText: 'g';
selector: #findAgain;
iconName: #smallFindIcon.
(aBuilder item: #'Set search string' translated)
keyText: 'h';
selector: #setSearchString.
(aBuilder item: #Undo translated)
keyText: 'z';
selector: #undo;
iconName: #smallUndoIcon;
withSeparatorAfter.
(aBuilder item: #Copy translated)
keyText: 'c';
selector: #copySelection;
iconName: #smallCopyIcon.
(aBuilder item: #Cut translated)
keyText: 'x';
selector: #cut;
iconName: #smallCutIcon.
(aBuilder item: #Paste translated)
keyText: 'v';
selector: #paste;
iconName: #smallPasteIcon.
(aBuilder item: #'Paste...' translated)
selector: #pasteRecent;
iconName: #smallCopyIcon;
withSeparatorAfter.
(aBuilder item: #'Set font...' translated)
keyText: 'k';
selector: #offerFontMenu.
(aBuilder item: #'Set style...' translated)
keyText: 'K';
selector: #changeStyle.
(aBuilder item: #'Set alignment...' translated)
selector: #chooseAlignment;
withSeparatorAfter.
(aBuilder item: #Accept translated)
keyText: 's';
selector: #accept;
iconName: #smallOkIcon.
(aBuilder item: #Cancel translated)
keyText: 'l';
selector: #cancel;
iconName: #smallCancelIcon
]
{ #category : #'menu declaration' }
TextEditor class >> textEditorShiftedMenuOn: aBuilder [
"Specify the menu used when writing text. Try it with:
(PragmaMenuBuilder
pragmaKeyword: 'textEditorShiftedMenu'
model: nil) menu popUpInWorld"
<contextMenu>
<textEditorShiftedMenu>
(aBuilder item: #'Set font...' translated)
keyText: 'k';
selector: #offerFontMenu.
(aBuilder item: #'Set style... (K)' translated)
keyText: 'K';
selector: #changeStyle.
(aBuilder item: #'Set alignment...' translated)
selector: #chooseAlignment.
(aBuilder item: #'More...' translated)
selector: #yellowButtonActivity.
]
{ #category : #settings }
TextEditor class >> useFindReplaceSelection [
^ UseFindReplaceSelection ifNil: [UseFindReplaceSelection := true]
]
{ #category : #settings }
TextEditor class >> useFindReplaceSelection: aBoolean [
UseFindReplaceSelection := aBoolean
]
{ #category : #settings }
TextEditor class >> useSecondarySelection [
^ UseSecondarySelection ifNil: [UseSecondarySelection := true]
]
{ #category : #settings }
TextEditor class >> useSecondarySelection: aBoolean [
UseSecondarySelection := aBoolean
]
{ #category : #settings }
TextEditor class >> useSelectionBar [
^ UseSelectionBar ifNil: [UseSelectionBar := true]
]
{ #category : #settings }
TextEditor class >> useSelectionBar: aBoolean [
UseSelectionBar := aBoolean
]
{ #category : #settings }
TextEditor class >> walkAlongDisplayedLine [
^ WalkAlongDisplayedLine ifNil: [WalkAlongDisplayedLine := true]
]
{ #category : #settings }
TextEditor class >> walkAlongDisplayedLine: aBoolean [
WalkAlongDisplayedLine := aBoolean
]
{ #category : #'menu messages' }
TextEditor >> accept [
"Save the current text of the text being edited as the current acceptable version for purposes of canceling. Allow my morph to take appropriate action"
morph canChangeText
ifTrue: [morph acceptContents]
]
{ #category : #'menu messages' }
TextEditor >> accept: aKeyboardEvent [
"Save the current text of the text being edited as the current acceptable version for purposes of canceling. Allow my morph to take appropriate action"
self accept.
^ true
]
{ #category : #'typing support' }
TextEditor >> acceptCR: aKeyboardEvent return: return [
((aKeyboardEvent keyValue = Character cr asciiValue) and: [morph acceptOnCR])
ifTrue: [
self closeTypeIn.
return value: true].
]
{ #category : #'undo-redo private' }
TextEditor >> addDeleteSelectionUndoRecord [
| undoText redoText |
undoText := self selection.
redoText := self nullText.
self editingState
redoArray: { morph. #undoTypeIn:interval:. {redoText. self selectionInterval} }
undoArray: {morph. #redoTypeIn:interval:. {undoText. self selectionInterval first to: self selectionInterval first - 1} }.
]
{ #category : #'typing support' }
TextEditor >> addString: aString [
"Think of a better name"
self zapSelectionWith: (Text string: aString attributes: self emphasisHere)
]
{ #category : #'undo-redo private' }
TextEditor >> addTypeInUndoRecord [
| begin stop undoText redoText |
begin := self startOfTyping min: self stopIndex.
stop := self stopIndex max: self startOfTyping.
self editingState
previousInterval: (begin to: stop - 1)
selection: (self text copyFrom: begin to: stop - 1).
undoText := self nullText.
redoText := stop > begin
ifTrue: [self text copyFrom: begin to: stop - 1]
ifFalse: [self nullText].
((undoText isEmpty and: [redoText isEmpty]) and: [self editingState previousInterval size < 1])
ifFalse: [self editingState
redoArray: { morph. #redoTypeIn:interval:. {redoText. begin to: begin + self selection size - 1} }
undoArray: {morph. #undoTypeIn:interval:. {undoText. begin to: stop - 1} }].
]
{ #category : #'new selection' }
TextEditor >> afterSelectionInsertAndSelect: aString [
self insertAndSelect: aString at: self stopIndex
]
{ #category : #'menu messages' }
TextEditor >> align [
"Align text according to the next greater alignment value,
cycling among leftFlush, rightFlush, center, and justified."
self changeAlignment.
self recomputeInterval
]
{ #category : #'editing keys' }
TextEditor >> align: aKeyboardEvent [
"Triggered by Cmd-u; cycle through alignment alternatives."
self align.
^ true
]
{ #category : #constants }
TextEditor >> alignmentChoices [
"Return the symbols representing the TextAlignment operations to get a text aligned."
^ #(leftFlush centered justified rightFlush)
]
{ #category : #private }
TextEditor >> applyAttribute: aTextAttribute [
"The user selected aTextAttribute via shortcut, menu or other means.
If there is a selection, apply the attribute to the selection.
In any case use the attribute for the user input (emphasisHere)"
self editingState emphasisHere: (Text addAttribute: aTextAttribute toArray: self emphasisHere).
self selection
ifEmpty: [ self paragraph composeAll ]
ifNotEmpty: [:sel | self replaceSelectionWith: (sel addAttribute: aTextAttribute).
self paragraph composeAll.
self recomputeSelection]
]
{ #category : #'typing support' }
TextEditor >> backTo: startIndex [
"Backspace typing"
self stopIndex > startIndex ifTrue: [
self selectFrom: startIndex to: self stopIndex - 1.
self addDeleteSelectionUndoRecord.
self zapSelectionWith: self nullText].
^ false
]
{ #category : #'typing/selecting keys' }
TextEditor >> backspace: aKeyboardEvent [
| result |
self closeTypeIn.
result := super backspace: aKeyboardEvent.
self editingState keystroke: aKeyboardEvent from: morph.
^ result
]
{ #category : #binding }
TextEditor >> bindingOf: aString [
^model bindingOf: aString
]
{ #category : #'editing keys' }
TextEditor >> blankParagraph [
| blankParagraph |
blankParagraph := Paragraph new.
blankParagraph
compose: '' asText
style: TextStyle default copy
from: 1
in: (0@0 extent: 10@10) .
^ blankParagraph
]
{ #category : #parenblinking }
TextEditor >> blinkParen [
| lastParenLocation |
(lastParenLocation := self editingState lastParenLocation) ifNotNil: [
self text string size >= lastParenLocation ifTrue: [
self text
addAttribute: TextEmphasis bold
from: lastParenLocation
to: lastParenLocation]]
]
{ #category : #parenblinking }
TextEditor >> blinkParenAt: parenLocation [
self text
addAttribute: TextEmphasis bold
from: parenLocation
to: parenLocation.
self editingState lastParenLocation: parenLocation.
]
{ #category : #parenblinking }
TextEditor >> blinkPrevParen: aCharacter [
| openDelimiter closeDelimiter level string here hereChar |
string := self string.
here := self editingState pointBlock stringIndex.
openDelimiter := "aKeyboardEvent keyValue." aCharacter.
closeDelimiter := '([{' at: (')]}' indexOf: openDelimiter).
level := 1.
[level > 0 and: [here > 2]]
whileTrue: [hereChar := string at: (here := here - 1).
hereChar = closeDelimiter
ifTrue: [level := level - 1.
level = 0
ifTrue: [^ self blinkParenAt: here]]
ifFalse: [hereChar = openDelimiter
ifTrue: [level := level + 1]]]
]
{ #category : #'menu messages' }
TextEditor >> cancel [
"Cancel the changes made so far to this text"
morph cancelEdits
]
{ #category : #'editing keys' }
TextEditor >> cancel: aKeyboardEvent [
"Cancel unsubmitted changes."
self cancel.
^ true
]
{ #category : #accessing }
TextEditor >> caret [
"Return the index position of the caret"
^ self startBlock stringIndex
]
{ #category : #settings }
TextEditor >> caseSensitiveFinds [
^ self class caseSensitiveFinds
]
{ #category : #'menu messages' }
TextEditor >> changeAlignment [
"Interactively change the alignment of the text currently being edited."
| reply |
reply := UIManager default
chooseFrom: self alignmentChoices
values: self alignmentChoices.
reply ifNil: [^self].
self applyAttribute: (TextAlignment perform: reply).
^ true
]
{ #category : #'menu messages' }
TextEditor >> changeEmphasis [
| reply code align menuList startIndex attribute choices |
choices := self emphasisChoices.
startIndex := self startIndex.
align := self text alignmentAt: startIndex ifAbsent: [ 0 ].
code := self text emphasisAt: startIndex.
menuList := WriteStream on: Array new.
menuList nextPut: (code isZero -> 'normal' translated).
menuList nextPutAll: (choices collect: [:emph|
(code anyMask: (TextEmphasis perform: emph) emphasisCode)
-> emph asString translated ]).
menuList nextPut: ((self text attributesAt: startIndex)
anySatisfy: [ :attr | attr isKern and: [attr kern < 0 ]]) -> 'narrow' translated.
reply := UIManager default chooseFrom: choices values: choices.
reply notNil
ifTrue: [ attribute := TextEmphasis perform: reply.
self applyAttribute: attribute].
^ true
]
{ #category : #'editing keys' }
TextEditor >> changeEmphasis: aKeyboardEvent [
"Change the emphasis of the current selection or prepare to accept characters with the change in emphasis. Emphasis change amounts to a font change. Keeps typeahead."
"control 0..9 -> 0..9"
| keyCode attribute oldAttributes index thisSel colors |
keyCode := ('0123456789-=' indexOf: aKeyboardEvent keyCharacter ifAbsent: [1]) - 1.
oldAttributes := self text attributesAt: self pointIndex.
thisSel := self selection.
"Decipher keyCodes for Command 0-9..."
"
(keyCode between: 1 and: 5)
ifTrue: [attribute := TextFontChange fontNumber: keyCode].
"
keyCode = 6
ifTrue: [
colors := #(#black #magenta #red #yellow #green #blue #cyan #white).
index := UIManager default chooseFrom: colors title: 'choose color...'.
index = 0 ifTrue: [^true].
index <= colors size
ifTrue: [attribute := TextColor color: (Color perform: (colors at: index))]
ifFalse: [
index := index - colors size - 1. "Re-number!!!"
index = 0 ifTrue: [attribute := self chooseColor].
thisSel ifNil: [^true] "Could not figure out what to link to"]].
(keyCode between: 7 and: 11)
ifTrue: [
aKeyboardEvent shiftPressed
ifTrue: [
keyCode = 10 ifTrue: [attribute := TextKern kern: -1].
keyCode = 11 ifTrue: [attribute := TextKern kern: 1]]
ifFalse: [
attribute := TextEmphasis
perform: (#(#bold #italic #narrow #underlined #struckOut) at: keyCode - 6).
oldAttributes
do: [:att | (att dominates: attribute) ifTrue: [attribute turnOff]]]].
keyCode = 0 ifTrue: [ attribute := TextEmphasis normal ].
attribute ifNotNil: [
self applyAttribute: attribute].
^true
]
{ #category : #'menu messages' }
TextEditor >> changeEmphasisOrAlignment [
| aList reply code align menuList startIndex attribute |
startIndex := self startIndex.
align := self text alignmentAt: startIndex ifAbsent: [ 0 ].
code := self text emphasisAt: startIndex.
menuList := WriteStream on: Array new.
menuList nextPut: (code isZero -> 'normal' translated).
menuList nextPutAll: (#(bold italic underlined struckOut) collect: [:emph|
(code anyMask: (TextEmphasis perform: emph) emphasisCode) -> emph asString translated]).
menuList nextPut: ((self text attributesAt: startIndex)
anySatisfy: [ :attr | attr isKern and: [attr kern < 0 ]]) -> 'narrow' translated.
self alignmentChoices collectWithIndex: [ :type :i |
menuList nextPut: (align = (i-1)) -> type asString translated ].
aList := #(normal bold italic underlined struckOut narrow leftFlush centered rightFlush justified).
reply := UIManager default chooseFrom: aList values: aList.
reply notNil ifTrue: [
(self alignmentChoices includes: reply)
ifTrue: [ attribute :=TextAlignment perform: reply]
ifFalse: [ attribute := TextEmphasis perform: reply].
self applyAttribute: attribute].
^ true
]
{ #category : #'editing keys' }
TextEditor >> changeLfToCr: aKeyboardEvent [
"Replace all LFs by CRs.
Triggered by Cmd-U -- useful when getting code from FTP sites
jmv- Modified to als change crlf by cr"
| fixed |
fixed := self selection string.
fixed := fixed copyReplaceAll: String crlf with: String cr.
fixed := fixed copyReplaceAll: String lf with: String cr.
self replaceSelectionWith: (Text fromString: fixed).
^ true
]
{ #category : #'menu messages' }
TextEditor >> changeStyle [
"Let user change styles for the current text pane."
| names reply style current menuList |
current := TextStyle default copy.
names := TextStyle actualTextStyles keys.
menuList := names collect: [ :styleName |
(styleName = current printString) -> styleName ].
reply := UIManager default chooseFrom: menuList values: names.
reply ifNotNil: [
(style := TextStyle named: reply) ifNil: [ ^ true].
self text style: style.
self paragraph textStyle: style.
self paragraph composeAll.
self recomputeSelection].
^ true
]
{ #category : #'typing/selecting keys' }
TextEditor >> changeStyle: aKeyboardEvent [
"Put up the style-change menu"
self closeTypeIn.
self changeStyle.
^ true
]
{ #category : #'menu messages' }
TextEditor >> changeTextFont [
"Present a menu of available fonts, and if one is chosen, apply it to the current selection."
| curFont newFont attr startIndex |
startIndex := self startIndex.
curFont := self text fontAt: startIndex.
newFont := StrikeFont fromUser: curFont.
newFont ifNil: [ ^self ].
attr := TextFontReference toFont: newFont.
self applyAttribute: attr
]
{ #category : #'menu messages' }
TextEditor >> chooseAlignment [
self changeAlignment
]
{ #category : #'editing keys' }
TextEditor >> chooseColor [
"Make a new Text Color Attribute, let the user pick a color, and return the attribute"
| attribute |
attribute := TextColor color: Color black "default".
UIManager default chooseColor
ifNotNil: [:nc | attribute color: nc].
^ attribute
]
{ #category : #parenblinking }
TextEditor >> clearParens [
| lastLocation |
(lastLocation := self editingState lastParenLocation)
ifNil: [^ self].
self text size >= lastLocation
ifTrue: [ self text
removeAttribute: TextEmphasis bold
from: lastLocation
to: lastLocation].
self editingState lastParenLocation: nil
]
{ #category : #'undo-redo private' }
TextEditor >> clearUndoManager: aKeyboardEvent [
^ self editingState clearUndoManager: aKeyboardEvent.
]
{ #category : #'typing support' }
TextEditor >> closeTypeIn [
"See comment in openTypeIn. It is important to call
closeTypeIn before executing any non-typing key, making a new selection, etc. It is
called automatically for menu commands."
self startOfTyping
ifNotNil: [self addTypeInUndoRecord.
self doneTyping]
]
{ #category : #private }
TextEditor >> codeCompletionAround: aBlock textMorph: aTextMorph keyStroke: anEvent [
" No code completion for simple text editor, so nothing special to do here around the block execution"
aBlock value
]
{ #category : #'menu messages' }
TextEditor >> compareToClipboard [
"Check to see if whether the receiver's text is the same as the text currently on the clipboard, and inform the user."
| s1 s2 |
s1 := self clipboardText string.
s2 := self string.
s1 = s2 ifTrue: [^ self inform: 'Exact match'].
UIManager default
longMessage: (TextDiffBuilder buildDisplayPatchFrom: s1 to: s2)
title: 'Comparison to Clipboard Text'
]
{ #category : #'editing keys' }
TextEditor >> compareToClipboard: aKeyboardEvent [
"Compare the receiver to the text on the clipboard."
self compareToClipboard.
^ true
]
{ #category : #'menu messages' }
TextEditor >> copySelection [
"Copy the current selection and store it in the paste buffer, unless a caret. Undoer & Redoer: undoCutCopy"
self lineSelectAndEmptyCheck: [^ self].
self clipboardTextPut: self selection.
self editingState previousInterval: self selectionInterval selection: self selection.
]
{ #category : #'new selection' }
TextEditor >> correctFrom: start to: stop with: aString [
"Make a correction in the model that the user has authorised from somewhere else in the system (such as from the compilier).
The user's selection is not changed, only corrected."
| userSelection delta |
userSelection := self selectionInterval.
self selectInvisiblyFrom: start to: stop.
self replaceSelectionWith: aString asText.
delta := aString size - (stop - start + 1).
self selectInvisiblyFrom:
userSelection first + (userSelection first > start ifFalse: [ 0 ] ifTrue: [ delta ])
to: userSelection last + (userSelection last > start ifFalse: [ 0 ] ifTrue: [ delta ]).
]
{ #category : #'typing/selecting keys' }
TextEditor >> crWithIndent: aKeyboardEvent [
"Replace the current text selection with CR followed by as many tabs
as on the current line (+/- bracket count) -- initiated by Shift-Return."
| char s i tabCount b |
self closeTypeIn.
b := self paragraph characterBlockAtPoint: aKeyboardEvent cursorPoint.
s := self string.
i := self stopIndex.
tabCount := 0.
[(i := i-1) > 0 and: [(char := s at: i) ~= Character cr]]
whileTrue: "Count tabs and brackets (but not a leading bracket)"
[(char = Character tab and: [i < s size and: [(s at: i+1) ~= $[ ]]) ifTrue: [tabCount := tabCount + 1].
char = $[ ifTrue: [tabCount := tabCount + 1].
char = $] ifTrue: [tabCount := tabCount - 1]].
"Now inject CR with tabCount tabs"
self addString: (String streamContents: [ :strm | strm crtab: tabCount ]).
^ false
]
{ #category : #'nonediting/nontyping keys' }
TextEditor >> cursorEnd: aKeyboardEvent [
"Private - Move cursor end of current line."
| string |
self closeTypeIn.
string := self string.
self
moveCursor:
[:position | self walkAlongDisplayedLine
ifTrue:[| targetLine |
targetLine := self paragraph lines at: (self paragraph lineIndexOfCharacterIndex: position).
targetLine = self paragraph lines last
ifTrue:[targetLine last + 1]
ifFalse:[targetLine last]]
ifFalse:[
string
indexOf: Character cr
startingAt: position
ifAbsent:[string size + 1]]]
forward: true
specialBlock:[:dummy | string size + 1]
event: aKeyboardEvent.
^true
]
{ #category : #'nonediting/nontyping keys' }
TextEditor >> cursorHome: aKeyboardEvent [
"Private - Move cursor from position in current line to beginning of
current line. If control key is pressed put cursor at beginning of text"
| string |
self closeTypeIn.
string := self string.
self
moveCursor: [ :position | self walkAlongDisplayedLine
ifTrue: [ (self paragraph lines at:(self paragraph lineIndexOfCharacterIndex: position)) first]
ifFalse: [(string
lastIndexOf: Character cr
startingAt: position - 1
ifAbsent:[0]) + 1]]
forward: false
specialBlock: [:dummy | 1]
event: aKeyboardEvent.
^true
]
{ #category : #private }
TextEditor >> cut [
"Cut out the current selection and redisplay the paragraph if necessary. Undoer & Redoer: undoCutCopy:"
self lineSelectAndEmptyCheck: [^ self].
self clipboardTextPut: self selection.
self replaceSelectionWith: self nullText.
]
{ #category : #'new selection' }
TextEditor >> deselect [
self selectFrom: 1 to: 0
]
{ #category : #'typing support' }
TextEditor >> dispatchCommandOn: aKeyboardEvent return: return [
|asciiValue honorCommandKeys|
asciiValue := aKeyboardEvent keyValue.
honorCommandKeys := self cmdKeysInText.
"Special keys overwrite crtl+key combinations - at least on Windows. To resolve this
conflict, assume that keys other than cursor keys aren't used together with Crtl."
((self class specialShiftCmdKeys includes: asciiValue) and: [ asciiValue < 27])
ifTrue: [
^ self performCmdActionsWith: aKeyboardEvent
shifted: aKeyboardEvent controlKeyPressed
return: return].
"backspace, and escape keys (ascii 8 and 27) are command keys"
((honorCommandKeys and: [aKeyboardEvent commandKeyPressed]) or: [self class specialShiftCmdKeys includes: asciiValue])
ifTrue: [
^ self performCmdActionsWith: aKeyboardEvent
shifted: aKeyboardEvent shiftPressed
return: return].
"the control key can be used to invoke shift-cmd shortcuts"
(honorCommandKeys and: [aKeyboardEvent controlKeyPressed])
ifTrue: [
^ self performCmdActionsWith: aKeyboardEvent shifted: true return: return].
^ false
]
{ #category : #'typing support' }
TextEditor >> dispatchEnterOn: aKeyboardEvent return: aBlock [
aKeyboardEvent keyValue = 13
ifFalse: [ ^ false ].
aKeyboardEvent controlKeyPressed
ifTrue: [ aBlock value: (self normalCharacter: aKeyboardEvent) ].
aKeyboardEvent shiftPressed