@@ -11,25 +11,11 @@ const Hints = Module("hints", {
11
11
requires : [ "config" ] ,
12
12
13
13
init : function ( ) {
14
-
15
14
this . _hintMode = null ;
16
- this . _submode = "" ; // used for extended mode, can be "o", "t", "y", etc.
17
- this . _hintString = "" ; // the typed string part of the hint is in this string
18
- this . _hintNumber = 0 ; // only the numerical part of the hint
19
- this . _usedTabKey = false ; // when we used <Tab> to select an element
20
- this . _prevInput = "" ; // record previous user input type, "text" || "number"
15
+ this . _submode = "" ; // used for extended mode, can be "o", "t", "y", etc.
21
16
this . _extendedhintCount = null ; // for the count argument of Mode#action (extended hint only)
22
17
23
- this . _pageHints = [ ] ;
24
- this . _validHints = [ ] ; // store the indices of the "hints" array with valid elements
25
- this . _tabNavigation = { } ; // for navigating between valid hints when TAB key is pressed
26
-
27
- this . _activeTimeout = null ; // needed for hinttimeout > 0
28
- this . _canUpdate = false ;
29
-
30
- // keep track of the documents which we generated the hints for
31
- // this._docs = { doc: document, start: start_index in hints[], end: end_index in hints[] }
32
- this . _docs = [ ] ;
18
+ this . _reset ( ) ;
33
19
34
20
const Mode = Hints . Mode ;
35
21
Mode . defaultValue ( "tags" , function ( ) function ( ) options . hinttags ) ;
@@ -88,18 +74,30 @@ const Hints = Module("hints", {
88
74
* Reset hints, so that they can be cleanly used again.
89
75
*/
90
76
_reset : function ( ) {
91
- statusline . updateField ( "input" , "" ) ;
92
- this . _hintString = "" ;
93
- this . _hintNumber = 0 ;
94
- this . _usedTabKey = false ;
95
- this . _prevInput = "" ;
77
+ this . _hintString = "" ; // the typed string part of the hint is in this string
96
78
this . _pageHints = [ ] ;
97
- this . _validHints = [ ] ;
98
- this . _tabNavigation = { } ;
99
- this . _canUpdate = false ;
79
+ this . _validHints = [ ] ; // store the indices of the "hints" array with valid elements
80
+ this . _tabNavigation = { } ; // for navigating between valid hints when TAB key is pressed
81
+
82
+ this . _resetInput ( ) ;
83
+
84
+ // keep track of the documents which we generated the hints for
85
+ // this._docs = { doc: document, start: start_index in hints[], end: end_index in hints[] }
100
86
this . _docs = [ ] ;
101
- hints . escNumbers = false ;
102
87
88
+ this . _clearTimeout ( ) ;
89
+ } ,
90
+
91
+ _resetInput : function ( ) {
92
+ this . _hintNumber = null ; // only the numerical part of the hint
93
+ this . _usedTabKey = false ; // when we used <Tab> to select an element
94
+ this . _prevInput = "" ; // record previous user input type, "text" || "number"
95
+ } ,
96
+
97
+ /**
98
+ * Clear _activeTimeout, a handle to delayed event (setTimeout)
99
+ */
100
+ _clearTimeout : function ( ) {
103
101
if ( this . _activeTimeout )
104
102
clearTimeout ( this . _activeTimeout ) ;
105
103
this . _activeTimeout = null ;
@@ -109,7 +107,11 @@ const Hints = Module("hints", {
109
107
* Display the current status to the user.
110
108
*/
111
109
_updateStatusline : function ( ) {
112
- statusline . updateField ( "input" , ( hints . escNumbers ? mappings . getMapLeader ( ) : "" ) + ( this . _hintNumber ? this . _num2chars ( this . _hintNumber ) : "" ) ) ;
110
+ statusline . updateField (
111
+ "input" ,
112
+ ( hints . escNumbers ? mappings . getMapLeader ( ) : "" ) +
113
+ ( this . _hintNumber !== null ? this . _num2chars ( this . _hintNumber ) : "" )
114
+ ) ;
113
115
} ,
114
116
115
117
/**
@@ -450,7 +452,7 @@ const Hints = Module("hints", {
450
452
let valid = validHint ( hint . text ) ;
451
453
let hintnumchars = this . _num2chars ( hintnum ) ;
452
454
let display = valid && (
453
- this . _hintNumber == 0 ||
455
+ this . _hintNumber === null ||
454
456
hintnumchars . indexOf ( String ( activeHintChars ) ) == 0
455
457
) ;
456
458
@@ -563,6 +565,8 @@ const Hints = Module("hints", {
563
565
styles . removeSheet ( true , "hint-positions" ) ;
564
566
565
567
this . _reset ( ) ;
568
+ statusline . updateField ( "input" , "" ) ;
569
+ hints . escNumbers = false ;
566
570
} ,
567
571
568
572
_num2chars : function ( num ) {
@@ -644,9 +648,13 @@ const Hints = Module("hints", {
644
648
} ,
645
649
646
650
_checkUnique : function ( ) {
647
- if ( this . _hintNumber == 0 )
651
+ if (
652
+ this . _hintNumber === null ||
653
+ this . _hintNumber === this . _chars2num ( options . hintchars [ 0 ] ) ||
654
+ this . _hintNumber > this . _validHints . length
655
+ ) {
648
656
return ;
649
- liberator . assert ( this . _hintNumber <= this . _validHints . length ) ;
657
+ }
650
658
651
659
// if we write a numeric part like 3, but we have 45 hints, only follow
652
660
// the hint after a timeout, as the user might have wanted to follow link 34
@@ -671,12 +679,9 @@ const Hints = Module("hints", {
671
679
this . _prevInput = "text" ;
672
680
673
681
// clear any timeout which might be active after pressing a number
674
- if ( this . _activeTimeout ) {
675
- clearTimeout ( this . _activeTimeout ) ;
676
- this . _activeTimeout = null ;
677
- }
682
+ this . _clearTimeout ( ) ;
678
683
679
- this . _hintNumber = 0 ;
684
+ this . _hintNumber = null ;
680
685
this . _hintString = commandline . command ;
681
686
this . _updateStatusline ( ) ;
682
687
this . _showHints ( ) ;
@@ -926,17 +931,19 @@ const Hints = Module("hints", {
926
931
927
932
this . _submode = minor ;
928
933
this . _hintString = filter || "" ;
929
- this . _hintNumber = 0 ;
930
- this . _usedTabKey = false ;
931
- this . _prevInput = "" ;
932
- this . _canUpdate = false ;
934
+ this . _resetInput ( ) ;
933
935
934
- this . _generate ( win ) ;
936
+ try {
937
+ this . _generate ( win ) ;
938
+ } catch ( e ) {
939
+ setTimeout ( function ( ) {
940
+ hints . _generate ( win ) ;
941
+ } , 0 )
942
+ }
935
943
936
944
// get all keys from the input queue
937
945
liberator . threadYield ( true ) ;
938
946
939
- this . _canUpdate = true ;
940
947
this . _showHints ( ) ;
941
948
942
949
if ( this . _validHints . length == 0 ) {
@@ -983,14 +990,28 @@ const Hints = Module("hints", {
983
990
this . _removeHints ( 0 ) ;
984
991
} ,
985
992
993
+ /**
994
+ * Return true if key is a valid entry point for hints handling
995
+ */
996
+ canHandleKey : function ( key ) {
997
+ return (
998
+ [ "<Return>" , "<Tab>" , "<S-Tab>" , mappings . getMapLeader ( ) ] . indexOf ( key ) > - 1 ||
999
+ ( key == "<BS>" && hints . _prevInput === "number" ) ||
1000
+ (
1001
+ hints . _isHintNumber ( key ) &&
1002
+ ! hints . escNumbers &&
1003
+ ( key !== options . hintchars [ 0 ] || this . _prevInput === "number" )
1004
+ )
1005
+ ) ;
1006
+ } ,
1007
+
986
1008
/**
987
1009
* Handle a hint mode event.
988
1010
*
989
1011
* @param {Event } event The event to handle.
990
1012
*/
991
1013
onEvent : function ( event ) {
992
1014
let key = events . toString ( event ) ;
993
- let followFirst = false ;
994
1015
995
1016
// clear any timeout which might be active after pressing a number
996
1017
if ( this . _activeTimeout ) {
@@ -1000,13 +1021,12 @@ const Hints = Module("hints", {
1000
1021
1001
1022
switch ( key ) {
1002
1023
case "< Return > ":
1003
- followFirst = true ;
1004
- break ;
1005
-
1024
+ this . _processHints ( true ) ;
1025
+ return ;
1006
1026
case "<Tab>" :
1007
1027
case "< S - Tab > ":
1008
1028
this . _usedTabKey = true ;
1009
- if ( this . _hintNumber == 0 )
1029
+ if ( this . _hintNumber === null )
1010
1030
this . _hintNumber = 1 ;
1011
1031
1012
1032
let oldId = this . _hintNumber ;
@@ -1020,14 +1040,14 @@ const Hints = Module("hints", {
1020
1040
return ;
1021
1041
1022
1042
case "<BS>" :
1023
- if ( this . _hintNumber > 0 && ! this . _usedTabKey ) {
1043
+ if ( this . _hintNumber !== null && ! this . _usedTabKey ) {
1024
1044
this . _hintNumber = Math . floor ( this . _hintNumber / 10 ) ;
1025
- if ( this . _hintNumber == 0 )
1026
- this . _prevInput = "text" ;
1045
+ if ( this . _hintNumber === 0 )
1046
+ this . _resetInput ( ) ;
1027
1047
}
1028
1048
else {
1029
1049
this . _usedTabKey = false ;
1030
- this . _hintNumber = 0 ;
1050
+ this . _hintNumber = null ;
1031
1051
liberator . beep ( ) ;
1032
1052
return ;
1033
1053
}
@@ -1036,49 +1056,35 @@ const Hints = Module("hints", {
1036
1056
case mappings . getMapLeader ( ) :
1037
1057
hints . escNumbers = ! hints . escNumbers ;
1038
1058
if ( hints . escNumbers && this . _usedTabKey ) // this._hintNumber not used normally, but someone may wants to toggle
1039
- this . _hintNumber = 0 ; // <tab>s ? this._reset. Prevent to show numbers not entered.
1059
+ this . _hintNumber = null ; // <tab>s ? this._reset. Prevent to show numbers not entered.
1040
1060
1041
1061
this . _updateStatusline ( ) ;
1042
1062
return ;
1043
1063
1044
1064
default :
1045
- if ( this . _isHintNumber ( key ) ) {
1046
- this . _prevInput = "number" ;
1047
-
1048
- let oldHintNumber = this . _hintNumber ;
1049
- if ( this . _hintNumber == 0 || this . _usedTabKey ) {
1050
- this . _usedTabKey = false ;
1051
- this . _hintNumber = this . _chars2num ( key ) ;
1052
- }
1053
- else
1054
- this . _hintNumber = this . _chars2num ( this . _num2chars ( this . _hintNumber ) + key ) ;
1055
-
1056
- this . _updateStatusline ( ) ;
1057
-
1058
- if ( ! this . _canUpdate )
1059
- return ;
1065
+ if ( this . _hintNumber !== null ) {
1066
+ this . _hintNumber = this . _chars2num ( this . _num2chars ( this . _hintNumber ) + key ) ;
1067
+ }
1068
+ else {
1069
+ this . _usedTabKey = false ;
1070
+ this . _hintNumber = this . _chars2num ( key ) ;
1071
+ }
1072
+ this . _prevInput = "number" ;
1060
1073
1061
- if ( this . _docs . length == 0 ) {
1062
- this . _generate ( ) ;
1063
- this . _showHints ( ) ;
1064
- }
1065
- this . _showActiveHint ( this . _hintNumber , oldHintNumber || 1 ) ;
1074
+ let oldHintNumber = this . _hintNumber ;
1066
1075
1067
- liberator . assert ( this . _hintNumber != 0 ) ;
1076
+ this . _showActiveHint ( this . _hintNumber , oldHintNumber || 1 ) ;
1068
1077
1069
- this . _checkUnique ( ) ;
1070
- }
1078
+ this . _checkUnique ( ) ;
1071
1079
}
1072
1080
1073
1081
this . _updateStatusline ( ) ;
1074
1082
1075
- if ( this . _canUpdate ) {
1076
- if ( this . _docs . length == 0 && this . _hintString . length > 0 )
1077
- this . _generate ( ) ;
1078
-
1079
- this . _showHints ( ) ;
1080
- this . _processHints ( followFirst ) ;
1083
+ if ( this . _docs . length == 0 && this . _hintString . length > 0 ) {
1084
+ this . _generate ( ) ;
1081
1085
}
1086
+ this . _showHints ( ) ;
1087
+ this . _processHints ( false ) ;
1082
1088
}
1083
1089
1084
1090
// FIXME: add resize support
0 commit comments