@@ -799,6 +799,9 @@ By default the input-method will not handle DEL, so we need this command."
799
799
" If ascii-mode is enabled."
800
800
(rime-lib-get-option " ascii_mode" ))
801
801
802
+ (defconst rime-release-modifier 1073741824
803
+ " Key release modifier (1 << 30)." )
804
+
802
805
(defun rime--inline-ascii ()
803
806
" Toggle inline ascii."
804
807
(let ((key-code
@@ -810,7 +813,7 @@ By default the input-method will not handle DEL, so we need this command."
810
813
(alt-l 65513 )
811
814
(alt-r 65514 ))))
812
815
(rime-lib-process-key key-code 0 )
813
- (rime-lib-process-key key-code 1073741824 )))
816
+ (rime-lib-process-key key-code rime-release-modifier )))
814
817
815
818
(defun rime-inline-ascii ()
816
819
" Toggle inline ascii and redisplay."
@@ -825,6 +828,80 @@ By default the input-method will not handle DEL, so we need this command."
825
828
(not (or inhibit-read-only
826
829
(get-pos-property (point ) 'inhibit-read-only )))))
827
830
831
+ (defun rime--chord-typing-p ()
832
+ " Return t if the rime input session supports chord typing."
833
+ (rime-lib-get-option " _chord_typing" ))
834
+
835
+ (defvar rime-chord-duration 0.2
836
+ " The duration of a key chord.
837
+
838
+ User should press and release all keys in the chord within the defined duration.
839
+ The default value works for an average chord typist using Combo Pinyin. Tune the
840
+ value if the user's stroke rate is higher or lower." )
841
+
842
+ (defun rime--chording-key-p (key )
843
+ " Return t if KEY is used for chorded input."
844
+ (cond
845
+ ((= key 32 ) t ) ; space
846
+ ((= key 39 ) t ) ; [']
847
+ ((and (>= key 44 ) (<= key 57 )) t ) ; [-,./0-9]
848
+ ((= key 59 ) t ) ; [;]
849
+ ((= key 61 ) t ) ; [=]
850
+ ((and (>= key 91 ) (<= key 93 )) t ) ; [\[\\ \]]
851
+ ((and (>= key 96 ) (<= key 122 )) t ) ; [`a-z]
852
+ (t nil )))
853
+
854
+ (defvar rime--chording-keys nil
855
+ " The list of keys being pressed at the same time." )
856
+
857
+ (defun rime--release-chording-keys ()
858
+ " Simulate key release events for the pressed keys in the chord."
859
+ (dolist (key rime--chording-keys)
860
+ (rime-lib-process-key key rime-release-modifier))
861
+ (rime--clear-chord))
862
+
863
+ (defvar rime--chord-timer nil
864
+ " The timer which, when triggered, concludes the chord." )
865
+
866
+ (defun rime--on-chord-timer ()
867
+ " The callback function invoked when a key chord is time up.
868
+
869
+ Assume all pressed keys are released and process the chord as input."
870
+ (if (and rime--chording-keys rime-active-mode)
871
+ (progn
872
+ (rime--release-chording-keys)
873
+ (rime--update-chorded-input-result))
874
+ (rime--clear-chord)))
875
+
876
+ (defun rime--update-chorded-input-result ()
877
+ " Update candidates, status and commit text after processing chorded input."
878
+ (unwind-protect
879
+ (let ((commit (rime-lib-get-commit)))
880
+ (if commit
881
+ (progn
882
+ (rime--clear-overlay)
883
+ (insert commit)))
884
+ (rime--redisplay))
885
+ (rime--refresh-mode-state)))
886
+
887
+ (defun rime--update-chord (key )
888
+ " Add KEY to the chord.
889
+
890
+ Absent are key up events in Emacs input method, a timer is used to conclude a
891
+ chord after `rime-chord-duration' seconds. When the timer is triggered, key up
892
+ events of the recorded keys are sent to librime."
893
+ (add-to-list 'rime--chording-keys key)
894
+ (unless rime--chord-timer
895
+ (setq rime--chord-timer
896
+ (run-with-timer rime-chord-duration nil #'rime--on-chord-timer ))))
897
+
898
+ (defun rime--clear-chord ()
899
+ " Clears the key chord."
900
+ (if rime--chord-timer
901
+ (cancel-timer rime--chord-timer))
902
+ (setq rime--chording-keys nil
903
+ rime--chord-timer nil ))
904
+
828
905
(defun rime-input-method (key )
829
906
" Process KEY with input method."
830
907
(setq rime--current-input-key key)
@@ -848,6 +925,10 @@ By default the input-method will not handle DEL, so we need this command."
848
925
(rime--inline-ascii)
849
926
(setq inline-ascii-prefix t ))
850
927
(let ((handled (rime-lib-process-key key 0 )))
928
+ (if (and (rime--chording-key-p key) (rime--chord-typing-p))
929
+ (if handled
930
+ (rime--update-chord key)
931
+ (rime--clear-chord)))
851
932
(with-silent-modifications
852
933
(let* ((context (rime-lib-get-context))
853
934
(commit-text-preview (alist-get 'commit-text-preview context))
0 commit comments