-
Notifications
You must be signed in to change notification settings - Fork 17
/
sis.el
1615 lines (1381 loc) · 55.8 KB
/
sis.el
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
;;; sis.el --- Minimize manual input source (input method) switching -*- lexical-binding: t; -*-
;; URL: https://github.com/laishulu/emacs-smart-input-source
;; Created: March 27th, 2020
;; Keywords: convenience
;; Package-Requires: ((emacs "27.1"))
;; Version: 1.0
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package Minimize manual input source (input method) switching.
;;
;;For more information see the README in the GitHub repo.
;;; Code:
(require 'subr-x)
(defvar sis-external-ism "macism"
"Path of external ism.")
(defvar sis-do-get nil
"Function to get the current input source.
Should return a string which is the id of the input source.")
(defvar sis-do-set nil
"Function to set the input source.
Should accept a string which is the id of the input source.")
(defvar sis-english-pattern "[a-zA-Z]"
"Pattern to identify a character as english.")
(defvar sis-english-source "com.apple.keylayout.US"
"Input source for english.")
(defvar sis-other-pattern "\\cC"
"Pattern to identify a character as other lang.")
(defvar sis-other-source "com.sogou.inputmethod.sogou.pinyin"
"Input source for other lang.")
(defvar sis-blank-pattern "[:blank:]"
"Pattern to identify a character as blank.")
(defvar sis-auto-refresh-seconds 0.2
"Idle timer interval to auto refresh input source status from OS.
Emacs-nativ input method don't need it. nil to disable the timer.
Set after the modes may have no effect.")
(defvar sis-change-hook nil
"Hook to run when input source changes.")
(defvar sis-default-cursor-color nil
"Default cursor color, used for English.
nil means obtained from the envrionment.")
(defvar sis-other-cursor-color "green"
"Cursor color for other language.")
(defvar sis-respect-start 'english
"Switch to specific input source when the /respect mode/ is enabled.")
(defvar sis-respect-evil-normal-escape t
"<escape> to english in normal state when the /respect mode/ is enabled.")
(defvar sis-respect-minibuffer-triggers (list)
"Commands trigger to set input source in minibuffer.
Each trigger should be a cons cell: (cons FN DETECTOR).
- FN: function to trigger the context following.
- DETECTOR:
- args: nil
- return:
- nil: left the determination to later detectors.
- \\='english: english context.
- \\='other: other language context.
Example of adding a trigger:
(add-to-list \\='sis-respect-minibuffer-triggers
(cons \\='org-roam-node-find (lambda () \\='other)))
If no trigger returns a none-nil result, english will be used as default.")
(defvar sis-respect-prefix-and-buffer t
"Preserve buffer input source when the /respect mode/ is enabled.")
(defvar sis-respect-go-english-triggers nil
"Triggers to save input source to buffer and then go to english.
Set after the modes may have no effect.")
(defvar sis-respect-restore-triggers nil
"Triggers to restore the input source from buffer.
Set after the modes may have no effect.")
(defvar sis-prefix-override-keys
(list "C-c" "C-x" "C-h")
"Prefix keys to be overrided.")
(defvar sis-prefix-override-recap-triggers
(list 'evil-local-mode 'yas-minor-mode)
"Commands trigger the recap of the prefix override.
Some functions take precedence of the override, need to recap after.
Set after the modes may have no effect.")
(defvar sis-context-fixed nil
"Context is fixed to a specific language in the /follow context mode/.
Possible values:
nil: dynamic context
\\='english: English context
\\='other: other language context.")
(defvar sis-context-detectors
(list (lambda (&rest _) sis-context-fixed)
(lambda (back-detect fore-detect)
(when (sis--context-english-p back-detect fore-detect)
'english))
(lambda (back-detect fore-detect)
(when (sis--context-other-p back-detect fore-detect)
'other)))
"Detectors to detect the context.
Each detector should:
- have two arguments:
- back-detect: which is the result of (sis--back-detect-chars).
- fore-detect: which is the result of (sis--fore-detect-chars).
- return one of the following values:
- nil: left the determination to later detectors.
- \\='english: English context.
- \\='other: other language context.")
(defvar sis-context-aggressive-line t
"Aggressively detect context across blank lines.")
(defvar sis-context-hooks
'(evil-insert-state-entry-hook)
"Hooks trigger the set of input source following context.")
(defvar sis-context-triggers
(list '('+org/insert-item-below 'sis--context-line nil)
'('+org/insert-item-above 'sis--context-line nil))
"Commands trigger the set of input source following context.
Each trigger should be a list: (FN PRE-FN-DETECTOR POST-FN-DETECTOR).
- FN: function to trigger the context following.
- PRE-FN-DETECTOR:
- args: none
- return:
- nil: left the determination to later detectors.
- \\='english: English context.
- \\='other: other language context.
- POST-FN-DETECTOR:
- args: none
- return:
- nil: left the determination to later detectors.
- \\='english: English context.
- \\='other: other language context.
Input source will be switched to (or (PRE-FN-DETECTOR) (POST-FN-DETECTOR)) after
FN is invoked.")
(defvar sis--context-triggers-adviced nil "Context triggers adviced.")
(defvar sis-inline-english-activated-hook nil
"Hook to run after inline english region activated.")
(defvar sis-inline-english-deactivated-hook nil
"Hook to run after inline english region deactivated.")
(defvar sis-inline-other-activated-hook nil
"Hook to run after inline other language region activated.")
(defvar sis-inline-other-deactivated-hook nil
"Hook to run after inline other language region deactivated.")
(defface sis-inline-face
'()
"Face of the inline region overlay."
:group 'sis)
(set-face-attribute
'sis-inline-face nil
:foreground (face-attribute 'font-lock-constant-face :foreground)
:inverse-video t)
(defvar sis-inline-not-max-point t
"Make sure there are other characters after inline region.
Insert new line when the whole buffer ends with the region, to avoid
autocomplete rendering a large area with the region background.")
(defvar sis-inline-tighten-head-rule 'one
"Rule to delete head spaces.
Possible values:
0: don't delete space
1: delete 1 space if exists
\\='zero: always ensure no space
\\='one: always ensure one space
custom function: the cursor will be moved to the beginning of the inline region,
and the function will be called with an argument which is the
end position of the leading whitespaces in the inline region.")
(defvar sis-inline-tighten-tail-rule 'one
"Rule to delete tail spaces.
Possible values:
0: don't delete space
1: delete 1 space if exists
\\='zero: always ensure no space
\\='one: always ensure one space
custom function: the cursor will be moved to the end of the inline region, and
the function will be called with an argument which is the
beginning of the tailing whitespaces in the inline region.")
(defvar sis-inline-single-space-close nil
"Single space closes the inline region.")
(defvar sis-inline-with-english t
"With the inline region.")
(defvar sis-inline-with-other nil
"With the inline other lang region.")
;;;###autoload
(define-minor-mode sis-log-mode
"Log the execution of this package."
:global t
:init-value nil)
;;
;; Following symbols are not supposed to be used directly by end user.
;;
(declare-function evil-normal-state-p "ext:evil-states.el" (&optional state) t)
(declare-function evil-visual-state-p "ext:evil-states.el" (&optional state) t)
(declare-function evil-motion-state-p "ext:evil-states.el" (&optional state) t)
(declare-function evil-operator-state-p
"ext:evil-states.el" (&optional state) t)
(declare-function company--active-p "ext:company.el" () t)
(declare-function company-complete-selection "ext:company.el" () t)
(declare-function mac-input-source "ext:macfns.c" (&optional SOURCE FORMAT) t)
(declare-function mac-select-input-source "ext:macfns.c"
(SOURCE &optional SET-KEYBOARD-LAYOUT-OVERRIDE-P) t)
(declare-function w32-get-ime-open-status "ext:w32fns.c" () t)
(declare-function w32-set-ime-open-status "ext:w32fns.c" (status) t)
(defvar transient--showp)
(defvar evil-normal-state-map)
(defun sis--do-nothing-advice (&rest _)
"Advice to make existing function do nothing.")
(defun sis--original-advice (fn &rest args)
"Advice to override existing advice on FN with ARGS."
(apply fn args))
(defsubst sis--string-match-p (regexp str &optional start)
"Robust wrapper of `string-match-p'.
Works when REGEXP or STR is not a string REGEXP, STR, START all has the same
meanings as `string-match-p'."
(and (stringp regexp)
(stringp str)
(string-match-p regexp str start)))
;;
;; Following codes are mainly about input source manager
;;
(defvar sis--ism nil "The input source manager.")
(defvar sis--ism-inited nil "Input source manager initialized.")
(defvar sis--current nil
"Current input source.")
(defvar sis--previous nil
"Previous input source.")
(defvar sis--for-buffer nil
"Saved buffer input source.")
(make-variable-buffer-local 'sis--for-buffer)
(defvar sis--for-buffer-locked nil
"Buffer input source is locked.")
(make-variable-buffer-local 'sis--for-buffer-locked)
(defun sis--init-ism ()
"Init input source manager."
;; `sis-do-get' and `sis-do-set' takes the first precedence.
;; external ism
(when (stringp sis-external-ism)
(let ((ism-path (executable-find sis-external-ism)))
(when ism-path (setq sis--ism ism-path))))
;; try EMP/w32 when do-get or do-set is missing
(unless (and (functionp sis-do-get)
(functionp sis-do-set))
(cond
((and (member (window-system) (list 'ns 'mac))
(fboundp 'mac-input-source))
;; EMP
(setq sis--ism 'emp))
((and (equal (window-system) 'w32)
(fboundp 'w32-get-ime-open-status))
;; w32, input sources are fixed
(setq sis-english-source nil)
(setq sis-other-source t)
(setq sis--ism 'w32))))
;; make `sis-do-set' and `sis-do-get'
(when sis--ism
;; avoid override user customized sis-do-get
(unless (functionp sis-do-get)
(setq sis-do-get (sis--mk-get-fn)))
;; avoid override user customized sis-do-set
(unless (functionp sis-do-set)
(setq sis-do-set (sis--mk-set-fn))))
;; successfully inited
(when (and (functionp sis-do-get)
(functionp sis-do-set))
;; a t `sis--ism' means customized by `sis-do-get' and `sis-do-set'
(unless sis--ism (setq sis--ism t)))
;; just inited, successfully or not
(setq sis--ism-inited t))
(defmacro sis--ensure-ism (&rest body)
"Only run BODY with valid ism."
`(progn
(unless sis--ism-inited
(sis--init-ism))
(when sis--ism
,@body)))
(defmacro sis--ensure-dir (&rest body)
"Ensure BODY run in home directory."
`(let ((default-directory "~"))
,@body))
(defsubst sis--normalize-to-lang (lang)
"Normalize LANG in the form of source id or lang to lang."
(cond
(; english
(member lang (list 'english sis-english-source))
'english)
(; other
(member lang (list 'other sis-other-source))
'other)))
(defsubst sis--normalize-to-source (source)
"Normalize SOURCE in the form of source id or lang to source."
(cond
(; english
(member source (list 'english sis-english-source))
sis-english-source)
(; other
(member source (list 'other sis-other-source))
sis-other-source)))
(defun sis--mk-get-fn ()
"Make a function to be bound to `sis-do-get'."
(cond
(; EMP
(equal sis--ism 'emp)
#'mac-input-source)
(;w32
(equal sis--ism 'w32)
#'w32-get-ime-open-status)
(; external ism
sis--ism
(lambda ()
(sis--ensure-dir
(string-trim (shell-command-to-string sis--ism)))))))
(defun sis--mk-set-fn ()
"Make a function to be bound to `sis-do-set'."
(cond
(; EMP
(equal sis--ism 'emp)
(lambda (source) (mac-select-input-source source)))
(;w32
(equal sis--ism 'w32)
#'w32-set-ime-open-status)
(; external ism
sis--ism
(lambda (source)
(sis--ensure-dir
(start-process "set-input-source" nil sis--ism source))))))
(defun sis--update-state (source)
"Update input source state.
SOURCE should be \\='english or \\='other."
(setq sis--previous sis--current)
(setq sis--current source)
(unless sis--for-buffer-locked
(setq sis--for-buffer source))
(when (not (eq sis--previous sis--current))
(run-hooks 'sis-change-hook)))
(defsubst sis--get ()
"Get the input source id."
(sis--ensure-ism
(sis--update-state (sis--normalize-to-lang (funcall sis-do-get)))))
(defsubst sis--set (source)
"Set the input source according to source SOURCE."
(sis--ensure-ism
(sis--update-state (sis--normalize-to-lang source))
(funcall sis-do-set (sis--normalize-to-source source))
(when sis-log-mode
(message "Do set input source: [%s]@%s, for-buffer: %s, locked: %s"
source (current-buffer)
sis--for-buffer sis--for-buffer-locked))))
;;;###autoload
(defun sis-get ()
"Get input source."
(interactive)
(sis--get)
(message (sis--normalize-to-source sis--current)))
(defsubst sis--save-to-buffer ()
"Save buffer input source."
(sis--get))
(defsubst sis--restore-from-buffer ()
"Restore buffer input source."
(setq sis--for-buffer-locked nil)
(sis--set (or sis--for-buffer 'english)))
(defun sis--set-english ()
"Function to set input source to \\='english."
(sis--set 'english))
(defun sis--set-other ()
"Function to set input source to \\='other."
(sis--set 'other))
;;;###autoload
(defun sis-set-english ()
"Command to set input source to \\='english."
(interactive)
(setq sis--for-buffer-locked nil)
(sis--set-english))
;;;###autoload
(defun sis-set-other ()
"Command to set input source to \\='other."
(interactive)
(setq sis--for-buffer-locked nil)
(sis--set-other))
;;;###autoload
(defun sis-switch ()
"Switch input source between \\='english and \\='other."
(interactive)
(setq sis--for-buffer-locked nil)
(cond
(; current is \\='english
(eq sis--current 'english)
(sis--set-other))
(; current is \\='other
(eq sis--current 'other)
(sis--set-english))))
;;;###autoload
(defun sis-ism-lazyman-config (english-source other-source &optional ism-type)
"Config ism for lazy man.
Run after the modes may have no effect.
ENGLISH-SOURCE: ENGLISH input source, nil means default,
ignored by ISM-TYPE of \\='fcitx, \\='fcitx5, \\='native,
\\='w32.
OTHER-SOURCE: OTHER language input source, nil means default,
ignored by ISM-TYPE of \\='fcitx, \\='fcitx5, \\='w32.
TYPE: TYPE can be \\='native, \\='w32, \\='emp, \\='macism, \\='im-select,
\\='fcitx, \\='fcitx5,\\='ibus.
nil TYPE fits both \\='emp and \\='macism."
(when english-source
(setq sis-english-source english-source))
(when other-source
(setq sis-other-source other-source))
(when ism-type
(setq sis-external-ism (pcase ism-type
('native 'native)
('emp 'emp)
('w32 'w32)
('macism "macism")
('im-select "im-select.exe")
('fcitx "fcitx-remote")
('fcitx5 "fcitx5-remote")
('ibus "ibus"))))
(cond
(; Emacs native input method, set do-get and do-set
(eq ism-type 'native)
(setq default-input-method other-source)
(setq sis-english-source nil)
;; Don't use `input-method-activate-hook',
;; because evil will make a buffer local one
(advice-add 'activate-input-method :filter-return
(lambda (res)
(sis--update-state (sis--normalize-to-lang
current-input-method))
res))
;; Don't use `input-method-deactivate-hook',
;; because evil will make a buffer local one
(advice-add 'deactivate-input-method :filter-return
(lambda (res)
(sis--update-state (sis--normalize-to-lang
current-input-method))
res))
(setq sis-do-get (lambda() current-input-method))
(setq sis-do-set #'activate-input-method))
(; for builtin supoort, use the default do-get and do-set
(memq ism-type (list nil 'emp 'w32 'macism 'im-select))
(; for WSL/Windows Subsystem for Linux, use the default do-get, set do-set
if (eq system-type 'gnu/linux)
(setq sis-do-set
(lambda(source)
(sis--ensure-dir
(make-process :name "set-input-source"
:command (list sis--ism source)
:connection-type 'pipe ))))
t))
(; fcitx and fcitx5, use the default do-get, set do-set
(memq ism-type (list 'fcitx 'fcitx5))
(unless sis-english-source
(setq sis-english-source "1"))
(unless sis-other-source
(setq sis-other-source "2"))
(setq sis-do-set (lambda(source)
(sis--ensure-dir
(pcase source
("1" (start-process "set-input-source"
nil sis--ism "-c"))
("2" (start-process "set-input-source"
nil sis--ism "-o")))))))
(; ibus, set do-get and do-set
(eq ism-type 'ibus)
(setq sis-do-get (lambda ()
(sis--ensure-dir
(string-trim
(shell-command-to-string
(format "%s engine" sis--ism))))))
(setq sis-do-set (lambda(source)
(sis--ensure-dir
(start-process "set-input-source"
nil sis--ism "engine" source)))))))
;;
;; Following codes are mainly about auto update mode
;;
(defvar sis--auto-refresh-timer nil
"Timer for `sis--auto-refresh-timer-function'.")
(defvar sis--auto-refresh-manager-timer nil
"Timer to manage `sis--auto-refresh-timer'.")
(defvar sis--auto-refresh-timer-scale 1
"Interval scale during this idle period.")
(defun sis--auto-refresh-timer-function ()
"Auto refresh input source on idle timer."
(when sis--auto-refresh-timer
(cancel-timer sis--auto-refresh-timer))
(sis--save-to-buffer)
(setq sis--auto-refresh-timer
(run-with-idle-timer
;; every time the wait period increases by auto-refresh-seconds
(time-add (current-idle-time)
(* sis-auto-refresh-seconds sis--auto-refresh-timer-scale))
nil
#'sis--auto-refresh-timer-function))
(setq sis--auto-refresh-timer-scale
(* 1.05 sis--auto-refresh-timer-scale)))
;;;###autoload
(define-minor-mode sis-auto-refresh-mode
"Automaticly refresh input source."
:global t
:init-value nil
(cond
(; turn on the mode
sis-auto-refresh-mode
(when sis-auto-refresh-seconds
(when sis--auto-refresh-manager-timer
(cancel-timer sis--auto-refresh-manager-timer))
(setq sis--auto-refresh-manager-timer
(run-with-idle-timer sis-auto-refresh-seconds t
#'sis--auto-refresh-timer-restart))))
(; turn off the mode
(not sis-auto-refresh-mode)
(when sis--auto-refresh-manager-timer
(cancel-timer sis--auto-refresh-manager-timer))
(when sis--auto-refresh-timer (cancel-timer sis--auto-refresh-timer)))))
(defun sis--auto-refresh-timer-restart ()
"Restart `sis--auto-refresh-timer'."
(when (and sis-auto-refresh-seconds sis-auto-refresh-mode)
(setq sis--auto-refresh-timer-scale 1)
(sis--auto-refresh-timer-function)))
(defun sis--try-enable-auto-refresh-mode ()
"Try to enable auto refresh mode."
(when sis-auto-refresh-seconds
(sis-auto-refresh-mode t)))
;;
;; Following codes are mainly about cursor color mode
;;
(defun sis--reset-default-cursor-color (&rest _)
"Reset default cursor color to nil."
(setq sis-default-cursor-color nil))
(defun sis--set-cursor-color-advice (color)
"Advice for FN of `set-cursor-color' with COLOR.
The advice is needed, because other packages may set cursor color in their own
way."
(pcase sis--current
('english
(list sis-default-cursor-color))
('other
(list sis-other-cursor-color))
(_
color)))
(defun sis--update-cursor-color()
"Update cursor color according to input source."
;; save original cursor color
(unless sis-default-cursor-color
(setq sis-default-cursor-color
(or (when (display-graphic-p)
(or (cdr (assq 'cursor-color default-frame-alist))
(face-background 'cursor)))
"white")))
;; for GUI
(when (display-graphic-p)
;;
;;actually which color passed to the function does not matter,
;; the advice will take care of it.
(set-cursor-color sis-default-cursor-color))
;; for TUI
(unless (display-graphic-p)
(pcase sis--current
('english
(send-string-to-terminal
(format "\e]12;%s\a" sis-default-cursor-color)))
('other
(send-string-to-terminal
(format "\e]12;%s\a" sis-other-cursor-color))))))
;;;###autoload
(define-minor-mode sis-global-cursor-color-mode
"Automaticly change cursor color according to input source."
:global t
:init-value nil
(cond
(; turn on the mode
sis-global-cursor-color-mode
;; auto refresh input source
(unless (eq sis-external-ism 'native)
(sis--try-enable-auto-refresh-mode))
(add-hook 'enable-theme-functions #'sis--reset-default-cursor-color)
(add-hook 'disable-theme-functions #'sis--reset-default-cursor-color)
(advice-add 'set-cursor-color :filter-args #'sis--set-cursor-color-advice)
(add-hook 'sis-change-hook #'sis--update-cursor-color))
(; turn off the mode
(not sis-global-cursor-color-mode)
(sis--try-disable-auto-refresh-mode)
(remove-hook 'enable-theme-functions #'sis--reset-default-cursor-color)
(remove-hook 'disable-theme-functions #'sis--reset-default-cursor-color)
(advice-remove 'set-cursor-color #'sis--set-cursor-color-advice)
(remove-hook 'sis-change-hook #'sis--update-cursor-color))))
;;
;; Following codes are mainly about respect mode
;;
(defvar sis--prefix-override-map-alist nil
"Map alist for override.")
(defvar sis--prefix-handle-stage 'normal
"Processing state of the prefix key.
Possible values: \\='normal, \\='prefix, \\='sequence.")
(defvar sis--buffer-before-prefix nil
"Current buffer before prefix.")
(defvar sis--buffer-before-command nil
"Current buffer before prefix.")
(defvar sis--real-this-command nil
"Real this command. Some commands overwrite it.")
(defvar sis--respect-post-cmd-timer nil
"Timer to run after returning to command loop.")
(defvar sis--respect-go-english nil
"Go english.")
(defvar sis--respect-force-restore nil
"Force restore after command finishes.")
(defvar sis--prefix-override-order -1000
"Order of the prefix override in `emulation-mode-map-alists'.")
(defun sis--respect-go-english-advice (&rest _)
"Advice for `sis-respect-go-english-triggers'."
(sis--save-to-buffer)
(when sis-log-mode
(message "go-english-advice: %s@%s, %s@locked"
sis--for-buffer (current-buffer)
sis--for-buffer-locked))
(setq sis--for-buffer-locked t)
(sis--set-english)
(setq sis--respect-go-english t))
(defun sis--respect-restore-advice (fn &rest args)
"Advice for FN in `sis-respect-restore-triggers' with ARGS args."
(unwind-protect (apply fn args)
(when sis-log-mode
(message "restore-advice: %s@%s, %s@locked"
sis--for-buffer (current-buffer)
sis--for-buffer-locked))
(setq sis--respect-go-english nil)
(setq sis--respect-force-restore t)))
(defvar sis--prefix-override-map-enable nil
"Enabe the override keymap.")
;;;###autoload
(defun sis-prefix-override-buffer-disable ()
"Disable prefix override in current buffer."
(interactive)
(make-local-variable
'sis--prefix-override-map-enable)
(setq sis--prefix-override-map-enable nil))
;;;###autoload
(defun sis-prefix-override-buffer-enable ()
"Disable prefix override in current buffer."
(interactive)
(when (local-variable-p 'sis--prefix-override-map-enable)
(kill-local-variable 'sis--prefix-override-map-enable)))
(defun sis--prefix-override-recap-do ()
"Recap prefix key override."
(add-to-ordered-list
'emulation-mode-map-alists
'sis--prefix-override-map-alist
sis--prefix-override-order))
(defun sis--prefix-override-recap-advice (fn &rest args)
"Advice for FN of `prefix-override-recap-triggers' with ARGS."
(unwind-protect (apply fn args)
(sis--prefix-override-recap-do)))
(defun sis--prefix-override-handler (arg)
"Prefix key handler with ARG."
(interactive "P")
;; Restore the prefix arg
(setq prefix-arg arg)
(prefix-command-preserve-state)
;; Push the key back on the event queue
(setq unread-command-events
(append (mapcar (lambda (e) (cons t e))
(listify-key-sequence (this-command-keys)))
unread-command-events)))
(defun sis--respect-focus-change-advice ()
"Advice for `after-focus-change-function'."
(if (frame-focus-state)
(sis--respect-focus-in-handler)
(sis--respect-focus-out-handler)))
(defun sis--respect-focus-out-handler ()
"Handler for `focus-out-hook'."
;; `mouse-drag-region' causes lots of noise.
(unless (eq this-command 'mouse-drag-region)
;; can't use `sis--save-to-buffer' directly
;; because OS may has already changed input source
;; when other windows get focus.
;; so, don't get the current OS input source
(setq sis--for-buffer-locked t)
(sis--set-english))
(when sis-log-mode
(message "Handle save hook, save [%s] to [%s]."
sis--for-buffer (current-buffer))))
(defun sis--respect-focus-in-handler ()
"Handler for `focus-in-hook'."
(when sis-log-mode
(message "Handle restore hook, restore [%s] from [%s] ."
sis--for-buffer (current-buffer)))
(sis--restore-from-buffer))
(defun sis--respect-pre-command-handler ()
"Handler for `pre-command-hook' to preserve input source."
(setq sis--buffer-before-command (current-buffer))
(setq sis--real-this-command this-command)
(when sis-log-mode
(message "pre@[%s]: [%s]@key [%s]@cmd [%s]@buf [%s]@override."
sis--prefix-handle-stage
(this-command-keys)
sis--real-this-command
(current-buffer)
sis--prefix-override-map-enable))
(pcase sis--prefix-handle-stage
(; current is normal stage
'normal
(cond
(; not prefix key
(not (eq sis--real-this-command #'sis--prefix-override-handler))
t)
(; for prefix key
(eq sis--real-this-command #'sis--prefix-override-handler)
;; go to pre@[prefix] directly
(when sis-log-mode
(message
"[%s] is a prefix key, short circuit to prefix phase."
(this-command-keys)))
(setq sis--prefix-handle-stage 'prefix)
(sis--respect-pre-command-handler))))
(; current is prefix stage
'prefix
(setq sis--prefix-override-map-enable nil)
(setq sis--buffer-before-prefix (current-buffer))
(sis--save-to-buffer)
(setq sis--for-buffer-locked t)
(sis--set-english)
(when sis-log-mode
(message "Input source: [%s] (saved) => [%s]."
sis--for-buffer sis-english-source)))
(; current is sequence stage
'sequence t)))
(defvar sis-prefix-override-buffer-disable-predicates
(list 'minibufferp
(;; read only buffer
lambda ()
buffer-read-only)
(;; magit
lambda ()
(sis--string-match-p "^magit.*:" (buffer-name)))
(;; special buffer
lambda ()
(let ((normalized-buffer-name
(downcase (string-trim (buffer-name)))))
(and (sis--string-match-p "^\*" normalized-buffer-name)
(not (sis--string-match-p "^\*new\*" normalized-buffer-name))
(not (sis--string-match-p "^\*dashboard\*"
normalized-buffer-name))
(not (sis--string-match-p "^\*scratch\*"
normalized-buffer-name))))))
"Predicates on buffers to disable prefix overriding.")
(defsubst sis--prefix-override-buffer-disable-p ()
"Final predicate on disabling prefix override in BUFFER."
(let ((value nil))
(dolist (p sis-prefix-override-buffer-disable-predicates)
(setq value (or value (funcall p))))
value))
(defun sis--respect-post-cmd-timer-fn ()
"Function for `sis--respect-post-cmd-timer'."
(when sis-log-mode
(message "timer@[%s]: [%s]@key [%s]@cmd [%s]@buf [%s]@override."
sis--prefix-handle-stage
(this-command-keys)
sis--real-this-command
(current-buffer)
sis--prefix-override-map-enable))
;; determine input source
(cond
(; go english, nothing need to do
sis--respect-go-english
t)
(; transient buffer shows
(and (boundp 'transient--showp) transient--showp)
(setq sis--for-buffer-locked t)
(sis--set-english))
(; restore
(or sis--respect-force-restore
(not (eq sis--buffer-before-command (current-buffer))))
;; entering minibuffer is handled separately.
;; some functions like `exit-minibuffer' won't trigger post-command-hook
(unless (minibufferp)
(when sis-log-mode
(message "restore: [%s]@[%s]" sis--for-buffer (current-buffer)))
(sis--restore-from-buffer)
(setq sis--respect-force-restore nil))))
;; disable prefix override for current buffer
(when (and (not (local-variable-p 'sis--prefix-override-map-enable))
(sis--prefix-override-buffer-disable-p))
(sis-prefix-override-buffer-disable))
;; re-enable if prefix override is disabled temporarily
(unless (local-variable-p 'sis--prefix-override-map-enable)
(setq sis--prefix-override-map-enable t))
(setq sis--prefix-handle-stage 'normal)
(setq sis--respect-post-cmd-timer nil))
(defsubst sis--to-normal-stage()
"Transite to normal stage."
;; for some command, after the command end,
;; the command loop may change the current buffer,
;; so delay the real processing.
(unless sis--respect-post-cmd-timer
(setq sis--respect-post-cmd-timer
(run-with-timer 0 nil #'sis--respect-post-cmd-timer-fn))))
(defun sis--respect-post-command-handler ()
"Handler for `post-command-hook' to preserve input source."
;; (setq this-command sis--real-this-command)
(when sis-log-mode
(message "post@[%s]: [%s]@key [%s]@cmd [%s]@buf [%s]@override."
sis--prefix-handle-stage
(this-command-keys)
sis--real-this-command
(current-buffer)
sis--prefix-override-map-enable))
(pcase sis--prefix-handle-stage
(; current is prefix stage
'prefix
(setq sis--prefix-handle-stage 'sequence))
(; current is sequence stage
'sequence
(cond
(; still in progress
(minibufferp)
(setq sis--prefix-handle-stage 'sequence))
(; key sequence is canceled
(not sis--real-this-command)
(when sis-log-mode (message "Key sequence canceled."))
(setq sis--respect-force-restore t)
(sis--to-normal-stage))
(; end key sequence
t
(when sis-log-mode (message "Key sequence ended."))
(setq sis--respect-force-restore t)
(sis--to-normal-stage))))
(; current is normal stage
'normal
(sis--to-normal-stage))))
(defun sis--minibuffer-setup-handler ()
"Handler for `minibuffer-setup-hook'."
(when sis-log-mode
(message "enter minibuffer: [%s]@current [%s]@last [%s]@command"
(current-buffer)
sis--buffer-before-command
this-command))
(let ((res nil))
(dolist (trigger sis-respect-minibuffer-triggers)
(let ((cmd (car trigger))
(detector (cdr trigger)))
(if (and (not res) (eq this-command cmd))
(setq res (funcall detector)))))
(sis--set (or res 'english))))