; 候选项翻页
MoreWait:
	If (waitnum*ListNum+ListNum<Max(jichu_for_select_Array.Length(),Function_for_select.Length())){
		waitnum+=1
		Gosub srf_tooltip
	}
Return
lessWait:
	If (waitnum>0){
		waitnum-=1
		Gosub srf_tooltip
	}
Return

; 查字典功能跳转
srf_tooltip:
	; RegExMatch(srf_all_Input, "O)^" func_key "?(.+)" func_key, modeMatch)
	; If (srf_func_table[modeMatch.Value[1]]="pinyin"){
	; ; If (srf_all_Input~="^" func_key "?" srf_func_table["pinyin"] func_key){
	; 	If (Inputscheme~="sp$|pinyin"){
	; 		Goto srf_value_off
	; 	}
	; 	; srf_all_Input_tr:=RegExReplace(srf_all_Input, "^" func_key "?" srf_func_table["pinyin"] func_key)
	; 	srf_all_Input_tr:=StrReplace(srf_all_Input, modeMatch.Value[0])
	; 	, srf_for_select:="", hotstring_for_select:=[], Function_for_select:=[]
	; 	If (srf_all_Input_history=srf_all_Input_tr){
	; 		Goto srf_tooltip_fanye
	; 	} Else {
	; 		; If (srf_all_Input~="^" func_key "?" srf_func_table["pinyin"] func_key "$")
	; 		If (srf_all_Input=modeMatch.Value[0])
	; 			jichu_for_select_Array:=[]
	; 		srf_all_Input_history:=srf_all_Input_tr
	; 		srf_for_select:=""
	; 	}
	; 	If (srf_all_Input){
	; 		pinyinmethod(srf_all_Input_tr, "pinyin")
	; 		Gosub houchuli
	; 		If (ShowCode){
	; 			mabiao:="", pinyinfancha:={}
	; 			Loop % jichu_for_select_Array.Length()
	; 				If InStr(jichu_for_select_Array[A_Index,0], "pinyin")
	; 					mabiao .= "'" jichu_for_select_Array[A_Index,2] "',"
	; 			If (mabiao:=Trim(mabiao, ",")){
	; 				DB.GetTable("SELECT value,key FROM " Inputscheme " WHERE value IN (" mabiao ") ORDER BY key", Result)
	; 				Loop % Result.RowCount
	; 					pinyinfancha[Result.Rows[A_Index,1]]:=Result.Rows[A_Index,2]
	; 			}
	; 		}
	; 	} Else
	; 		srf_all_Input_tooltip:=LTrim(srf_all_Input,func_key)
	; 	srf_all_Input_tooltip:=RegExReplace(srf_all_Input_tooltip, "'\\'?", " ")
	; 	srf_all_Input_tooltip:=srf_all_Input_tooltip?srf_all_Input_tooltip:modeMatch.Value[0]
	; 	; srf_all_Input_tooltip:=srf_all_Input_tooltip?srf_all_Input_tooltip:srf_func_table["pinyin"] func_key
	; 	Gosub srf_tooltip_fanye
	; 	Return
	; } Else If InStr(srf_all_Input, func_key){
	If InStr(srf_all_Input, func_key){
		RegExMatch(srf_all_Input, "O)^" func_key "?([a-zA-Z]+)" func_key, modeMatch)
		If (srf_all_Input_history!=srf_all_Input)
			srf_all_Input_history:=srf_all_Input
		srf_for_select:="", jichu_for_select_Array:=[], hotstring_for_select:=[], Function_for_select:=[]
		If (srf_FuncSelect(srf_all_Input)=1)
			Gosub srf_tooltip_fanye
		Else If (srf_FuncSelect(srf_all_Input)=0)
			Gosub showhouxuankuang
		Else
			Gosub srf_value_off
		Return
	} Else {
		field_Num:=0, loop_num:=0
		If (srf_all_Input_history=srf_all_Input)
			Goto srf_tooltip_fanye
		Else {
			srf_all_Input_history:=srf_all_Input
		}
		Function_for_select:=[]
		ttt:=A_TickCount
		customs_for_select:=get_word(DB, srf_all_Input, "customs")
		OutputDebug, % A_TickCount-ttt
		If (customs_for_select[1,3]=0){
			If InStr(customs_for_select[1,0],"Func"){
				srf_Func_Params:=StrSplit(RTrim(customs_for_select[1,2],func_key),func_key)
				If (srf_func_table[srf_Func_Params[1]]){
					value:=srf_Func_Params[1], srf_Func_Params.RemoveAt(1)
					customs_for_select[1,2]:=FuncCall(srf_func_table[value],srf_Func_Params*)
				}
				customs_for_select[1,0]:=RegExReplace(customs_for_select[1,0],"i)\{Func\}")
			}
			SendInput(customs_for_select[1,2],customs_for_select[1,0])
			Goto srf_value_off
		}
		%shurulei%method(srf_all_Input, Inputscheme)
		; re zi fu he gong neng

		If (magicstring)
			hotstring_for_select:=get_word_lianxiang(DB, srf_all_Input, "hotstrings")
		If (superrun)
			Function_for_select:=get_word_lianxiang(DB, srf_all_Input, "functions")
		RunTime.Push(["Search",A_TickCount-RunTime[1,2]])
		If (customs_for_select.Length()=0&&fzm&&(tvar:=restoreinput(jichu_for_select_Array[1,1],fzm))){
			customs_for_select:=get_word(DB, tvar, "customs")
			Loop % customs_for_select.Length()
				customs_for_select[A_Index,1]:=jichu_for_select_Array[1,1]
		}
		Loop % (tvar:=customs_for_select.Length()){
			If InStr(customs_for_select[tvar-A_Index+1,0],"Func"){
				srf_Func_Params:=StrSplit(RTrim(customs_for_select[tvar-A_Index+1,2],func_key),func_key)
				If (srf_func_table[srf_Func_Params[1]]){
					value:=srf_Func_Params[1], srf_Func_Params.RemoveAt(1)
					customs_for_select[tvar-A_Index+1,valueindex]:=FuncCall(srf_func_table[value],srf_Func_Params*)
				}
				customs_for_select[tvar-A_Index+1,0]:=RegExReplace(customs_for_select[tvar-A_Index+1,0],"i)\{Func\}")
			}
			If (customs_for_select[tvar-A_Index+1,valueindex])
				jichu_for_select_Array.InsertAt(Min(Max(customs_for_select[tvar-A_Index+1,3]-(tvar-A_Index),1),jichu_for_select_Array.Length()+1),customs_for_select[tvar-A_Index+1])
		}
			; jichu_for_select_Array.InsertAt(Min(customs_for_select[A_Index,3],jichu_for_select_Array.Length()+1),customs_for_select[A_Index])
		Gosub houchuli
		RunTime.Push(["Qucong",A_TickCount-RunTime[1,2]])
	}
	Gosub srf_tooltip_fanye
	Timelist:=""
	Loop % RunTime.Length()-1
		Timelist .= RunTime[A_Index+1,1] " " RunTime[A_Index+1,2] "|"
	OutputDebug % Timelist "Final " A_TickCount-RunTime[1,2]
Return
; 去重 简转繁
houchuli:
	If (Traditional){
		Index:=0
		Loop {
			Index++
			If ((Index>jichu_for_select_Array.Length())||(jichu_for_select_Array[Index,2]=""))
				Break
			If tradstr:=Trim(simp2trad(jichu_for_select_Array[Index,2]), Chr(2)){
				If InStr(tradstr, Chr(2)){
					tradstr:=StrSplit(tradstr, Chr(2))
					tarr:={}
					For key,value In jichu_for_select_Array[Index]
						tarr[key]:=value
					jichu_for_select_Array[Index,5]:=tradstr[1]
					Loop % tradstr.Length()-1
						Index++, jichu_for_select_Array.InsertAt(Index, tarr), jichu_for_select_Array[Index,5]:=tradstr[A_Index+1]
				} Else
					jichu_for_select_Array[Index,5]:=tradstr
			} Else 
				jichu_for_select_Array[Index,5]:=jichu_for_select_Array[Index,2]
		}
	}
	;去重
	Loopindex:=1, qucongtable:={}
	Loop % jichu_for_select_Array.Length()
	{
		If (jichu_for_select_Array[Loopindex, valueindex]="")
			Break
		Else If qucongtable[jichu_for_select_Array[Loopindex, valueindex]]
			jichu_for_select_Array.RemoveAt(Loopindex)
		Else If jichu_for_select_Array[Loopindex, valueindex]
			qucongtable[jichu_for_select_Array[Loopindex, valueindex]]:=1, Loopindex++
	}
	; qucongtable:=""
Return

; 显示前处理
srf_tooltip_fanye:
	srf_for_select:=""
	If !InStr(srf_all_Input,func_key){
		If (jichu_for_select_Array[1,valueindex]="")
			jichu_for_select_Array[1,1]:="", jichu_for_select_Array[1,valueindex]:=srf_all_Input
		jichu_for_select_string:=""
		If (Inputscheme~="sp$|pinyin"){
			If (Textdirection="Horizontal")
				Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
					jichu_for_select_string.=A_Space A_Space Mod(A_Index,10) "." jichu_for_select_Array[tvar:=ListNum*waitnum+A_Index, valueindex] (jichu_for_select_Array[tvar, 6])
			Else
				Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
					jichu_for_select_string.="`n" Mod(A_Index,10) "." jichu_for_select_Array[tvar:=ListNum*waitnum+A_Index, valueindex] (jichu_for_select_Array[tvar, 6])
		} Else If (Inputscheme="sanpin"){
			If (ShowCode){
				If (Textdirection="Horizontal")
					Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
						jichu_for_select_string.=A_Space A_Space Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex] (srf_all_Input~=StrReplace(jichu_for_select_Array[ListNum*waitnum+A_Index, 1]," "," ?")?"":jichu_for_select_Array[ListNum*waitnum+A_Index, 1])
				Else
					Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
						jichu_for_select_string.="`n" Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex] (srf_all_Input~=StrReplace(jichu_for_select_Array[ListNum*waitnum+A_Index, 1]," "," ?")?"":jichu_for_select_Array[ListNum*waitnum+A_Index, 1])
			} Else {
				If (Textdirection="Horizontal")
					Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
						jichu_for_select_string.=A_Space A_Space Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex]
				Else
					Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
						jichu_for_select_string.="`n" Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex]
			}
		} Else {
			If (ShowCode){
				If (Textdirection="Horizontal")
					Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
						jichu_for_select_string.=A_Space A_Space Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex] (InStr(jichu_for_select_Array[ListNum*waitnum+A_Index, 0],"pinyin")?pinyinfancha[jichu_for_select_Array[ListNum*waitnum+A_Index, 2]]:(Imagine&&!InStr(jichu_for_select_Array[ListNum*waitnum+A_Index, 0],"<Cloud>")?((var:=RegExReplace(jichu_for_select_Array[ListNum*waitnum+A_Index, 1], "^" srf_all_Input))?var:xingmazigen(jichu_for_select_Array[ListNum*waitnum+A_Index, 2], Inputscheme)):xingmazigen(jichu_for_select_Array[ListNum*waitnum+A_Index, 2], Inputscheme)))
				Else
					Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
						jichu_for_select_string.="`n" Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex] (InStr(jichu_for_select_Array[ListNum*waitnum+A_Index, 0],"pinyin")?pinyinfancha[jichu_for_select_Array[ListNum*waitnum+A_Index, 2]]:(Imagine&&!InStr(jichu_for_select_Array[ListNum*waitnum+A_Index, 0],"<Cloud>")?((var:=RegExReplace(jichu_for_select_Array[ListNum*waitnum+A_Index, 1], "^" srf_all_Input))?var:xingmazigen(jichu_for_select_Array[ListNum*waitnum+A_Index, 2], Inputscheme)):xingmazigen(jichu_for_select_Array[ListNum*waitnum+A_Index, 2], Inputscheme)))
			} Else {
				If (Textdirection="Horizontal")
					Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
						jichu_for_select_string.=A_Space A_Space Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex]
				Else
					Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
						jichu_for_select_string.="`n" Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex]
			}
		}
		srf_for_select:=Trim(jichu_for_select_string," `n")
		If (hotstring_for_select[1, 1])
			srf_for_select .= "`n(，)" hotstring_for_select[1, 3]
		If (Function_for_select[1, 1])
			srf_for_select .= "`n(。)" Function_for_select[1, 3]

		If (Inputscheme~="sp$")||InStr(srf_all_Input,func_key){
			ToolTipText:=StrReplace(srf_all_Input, "\", " ")
			If (Showquanpin&&!InStr(srf_all_Input,func_key))
				ToolTipText .= "`n[ " StrReplace(srf_all_Input_tooltip,"''"," ") " ]"
		} Else If (Inputscheme="pinyin"){
			ToolTipText:=srf_all_Input_tooltip (SubStr(srf_all_Input,0)="'"?"'":SubStr(srf_all_Input,0)="\"?" ":"")
			ToolTipText:=RegExReplace(ToolTipText, "''", " ")
		} Else
			ToolTipText:=srf_all_Input_tooltip (SubStr(srf_all_Input,0)="'"?"'":SubStr(srf_all_Input,0)="\"?" ":"")
	} Else If (srf_func_table[modeMatch.Value[1], "Name"]="get_hotstring"||(srf_all_Input~="^" func_key "[a-z]+$")){
		Loop % Min(Function_for_select.Length()-ListNum*waitnum, ListNum)
		{
			If (modeMatch.Value[1]){
				If (Textdirection="Horizontal")
					srf_for_select .= A_Space A_Space Mod(A_Index,10) "." Function_for_select[ListNum*waitnum+A_Index, 3]
				Else
					srf_for_select .= "`n" Mod(A_Index,10) ". " Function_for_select[ListNum*waitnum+A_Index, 1]
			} Else {
				If (Textdirection="Horizontal")
					srf_for_select .= A_Space A_Space Mod(A_Index,10) "." Function_for_select[ListNum*waitnum+A_Index, 1]
				Else
					srf_for_select .= "`n" Mod(A_Index,10) ". " Function_for_select[ListNum*waitnum+A_Index, 1]
			}
		}
		srf_for_select:=Trim(srf_for_select, "`n "), ToolTipText:=srf_all_Input
	} Else {
		Loop % Min(Function_for_select.Length()-ListNum*waitnum, ListNum)
			If (Textdirection="Horizontal")
				srf_for_select .= A_Space A_Space Mod(A_Index,10) "." Function_for_select[ListNum*waitnum+A_Index, 3]
			Else
				srf_for_select .= "`n" Mod(A_Index,10) "." Function_for_select[ListNum*waitnum+A_Index, 3]
		srf_for_select:=Trim(srf_for_select, "`n "), ToolTipText:=srf_all_Input
	}
	
; 显示候选项
showhouxuankuang:
	If (waitnum=0)&&(jichu_for_select_Array[2,0]=="<Cloud>|-1")
			srf_for_select:=RegExReplace(srf_for_select, "2\." jichu_for_select_Array[2,valueindex] "☁?", "2." jichu_for_select_Array[2,valueindex] "☁")
	If (ToolTipText=""){
		srf_for_select:=""
		If (ToolTipStyle=1)
			ToolTip(1,"") ;, ToolTip(2,"")
		; Else
		; 	Gui, houxuankuang:Hide
		Return
	}
	If (Lockedposition){
		Caret:={}
		Caret.X:=A_ScreenWidth//2-A_ScreenWidth//8, Caret.Y:=A_ScreenHeight-20-Height*(InStr(ToolTipText,"`n")?2.5:4)
	} Else If (!hasCaretPos)
		Caret:=GetCaretPos(), hasCaretPos:=1
	If (ToolTipStyle=1){
		If (Lockedposition){
			ToolTip(1, ToolTipText "`n" srf_for_select, "x" A_ScreenWidth//2-A_ScreenWidth//8 " y" A_ScreenHeight-20-Height*(InStr(ToolTipText,"`n")?2.5:4))
			; WinGetPos, x, y, , Height, % "ahk_id" Tip1hWnd
			; ToolTip(2, srf_for_select, "x" x " y" y+Height+5)
		} Else {
			ToolTip(1, ToolTipText "`n" srf_for_select, "x" Caret.x " y" Caret.y+Caret.h)
			; WinGetPos, x, y, , Height, % "ahk_id" Tip1hWnd
			; ToolTip(2, srf_for_select, "x" x " y" y+Height+5)
		}
		; ToolTip, %ToolTipText%, , , 1
		; ToolTip, %srf_for_select%, , , 2
	; } Else If (ToolTipStyle=2){
	; 	Gui, measure:Destroy
	; 	Gui, measure:+ToolWindow -Caption -DPIScale +AlwaysOnTop
	; 	Gui, measure:Font, s%FontSize%, %FontType%
	; 	Try {
	; 		Gui, measure:Add, Text, vmeasureedit1, %ToolTipText%
	; 		GuiControlGet, measure_P1, measure:Pos, measureedit1
	; 		Gui, measure:Add, Text, x0 yp+20 vmeasureedit2, % srf_for_select
	; 		GuiControlGet, measure_P2, measure:Pos, measureedit2
	; 	}
		
	; 	If (InStr(srf_all_Input,"·"))
	; 		guihouxuankuang_width:=20+Max(180,measure_P2w,measure_P1w)
	; 	Else If (Inputscheme~="sp$"){
	; 		guihouxuankuang_width:=20+Max(measure_P1w, measure_P2w
	; 			, measure_P1w*StrLen(srf_all_Input)/StrLen("[ " srf_all_Input_tooltip " ]")+30)
	; 	} Else
	; 		guihouxuankuang_width:=Max(measure_P1w, measure_P2w)+20
	; 	guihouxuankuang_height:=measure_P1h+measure_P2h+15
	; 	If (Lockedposition)
	; 		Gui, houxuankuang:Show, % "NA w" guihouxuankuang_width " h" guihouxuankuang_height
	; 	Else {
	; 		guihouxuankuang_x:=Max(Min(A_ScreenWidth-guihouxuankuang_width, Caret.X+20), 0)
	; 		, guihouxuankuang_y:=Max(Min(A_ScreenHeight-guihouxuankuang_height, Caret.Y+30), 0)
	; 		Gui, houxuankuang:Show, % "NA w" guihouxuankuang_width " h" guihouxuankuang_height " x" guihouxuankuang_x " y" guihouxuankuang_y
	; 	}
	; 	; WinSet, Region, % "0-0 w" guihouxuankuang_width+3 " h" guihouxuankuang_height+3 " R20-20", ahk_id%guihouxuankuang%
	; 	GuiControl, houxuankuang:Move, myedit1, % "w" measure_P1w " h" measure_P1h
	; 	GuiControl, houxuankuang:Move, fengefu, % "y" measure_P1h+7
	; 	GuiControl, houxuankuang:Move, myedit2, % "w" measure_P2w " h" measure_P2h " y" measure_P1h+10
	; 	GuiControl, houxuankuang:Text, myedit1, %ToolTipText%
	; 	GuiControl, houxuankuang:Text, myedit2, %srf_for_select%
	} Else {
		GdipText(ToolTipText, srf_for_select, "x" Caret.X " y" Caret.Y+30 " c" TextColor " s" FontSize, BackgroundColor, FontType)
	}
Return

; 关闭候选框
srf_value_off:
	If (ToolTipStyle=1)
		ToolTip(1) ;, ToolTip(2)
	; Else If (ToolTipStyle=2)
	; 	Gui, houxuankuang:Hide
	Else
		GdipText("")
	If (Learnfg+InStr(srf_all_Input,"\")>1&&StrLen(learn_words[2])<9) {
		DB.GetTable("SELECT key FROM pinyin WHERE jp=""" RegExReplace(learn_words[1],"([a-z])[a-z]*","$1") """ AND key=""" learn_words[1] """ AND value='" learn_words[2] "' LIMIT 1", Result)
		If !Result.RowCount {
			If !DB.Exec("INSERT INTO pinyin VALUES (""" RegExReplace(learn_words[1],"([a-z])[a-z]*","$1") """,""" learn_words[1] """,'" learn_words[2] "',ifnull((SELECT max(weight) FROM 'pinyin' WHERE jp=""" RegExReplace(learn_words[1],"([a-z])[a-z]*","$1") """ AND key=""" learn_words[1] """),0)+1);")
				MsgBox % DB.ErrorMsg
		} Else
			DB.Exec("UPDATE 'pinyin' SET weight = weight+1 WHERE jp=""" RegExReplace(learn_words[1],"([a-z])[a-z]*","$1") """ AND key = """ learn_words[1] """ AND value = '" learn_words[2] "';")
	}
	learn_words:=[], Learnfg:=0, output_words:=srf_all_Input_tooltip:=srf_all_Input_tr:=ToolTipText:=srf_all_Input_history:=""
	srf_all_Input:=srf_for_select:=output_words:="", hasCaretPos:=waitnum:=IsPinyin:=0
	Function_for_select:=[], hotstring_for_select:=[], history_field_array:=[], jichu_for_select_Array:=[], modeMatch:=[], SQL_buffer:=[]
Return
; 云输入
BDCloudInput:
	If (srf_all_Input_tooltip="")
		Return
	BDCloudInput(StrReplace(srf_all_Input_tooltip,"\"))
	; BDCloudInput(StrSplit(StrReplace(srf_all_Input_tooltip,"\"),"''")[1])
Return