; Insert Clipboard Contents as External Quotes into an Email Editor ; 2005-06-14, 2005-09-18 ; OS=WinXP, AHK=1.0.34, Author=HJS (Hajos) ; ; This program transforms the current clipboard contents to linewise ; quoted paragraphs and inserts it into the current window - ; provided the window title contains a specified string. ; Long paragraphs are broken to specified line length. ; ; Intended for use in email editors such as TB ; ; Improved handling of script´s hotkey (thanks Andreas Borutta) ; ; Reformatting of paragraphs 2005-09-25, 2005-10-09 ; ; ########## Auto-execute Section #SingleInstance force #UseHook xrq_init( "de", "" ) ; "de" = deutsch, anything else: english ; ########## End of Auto-execute Section Return ;------------------------------------------------------------ xrq_init( lang, SeperateFile ) { global ; ########## Script menu xq_Locale( lang ) rq_Locale( lang ) Menu, Tray, Add, %xq_0%, xq_conf Menu, Tray, Add, %rq_0%, rq_conf xrq_ini := MakeIniFileName( SeperateFile, "ExternalQuote_Rewrap_1_2.ini" ) xq_Init( xrq_ini ) rq_Init( xrq_ini ) } ; MakeIniFileName( SeperateFile, Default ) { If ( A_IsCompiled ) Return Default Else If ( SeperateFile ) Return SeperateFile Else Return A_ScriptFullPath } ; xq_Init( inifile ) { global xq_ReadIni( IniFile, "xq_Config" ) Gosub, xq_GuiBuild xq_always := ( xq_titletext = "" ) MakeHotkey( xq_oldhk, xq_hk, "xq_InsertQuotedClipBd" ) If ( xq_Confgd = 0 ) Gosub, xq_conf xq_stdmark := "> " } ; rq_Init( inifile ) { global rq_ReadIni( IniFile, "rq_Config" ) Gosub, rq_GuiBuild rq_always := ( rq_titletext = "" ) MakeHotkey( rq_oldhk, rq_hk, "rq_Reformat" ) If ( rq_Confgd = 0 ) Gosub, rq_conf } ; xq_GuiBuild: Gui, 17:Default Gui, Font, s13 Gui, Add, Text, Section xm , %xq_8% Gui, Font, , Gui, Add, Text, Section xm , %xq_1% Gui, Add, Edit, ys w40 vxq_quotemark, %xq_quotemark% Gui, Add, Text, Section x+13 ys-20 , %xq_2% Gui, Add, Text, Section xm , %xq_5% Gui, Add, Edit, ys w30 Number vxq_charsperline, %xq_charsperline% Gui, Add, Text, Section xm y+20 , %xq_7% Gui, Add, Edit, ys w100 vxq_titletext, %xq_titletext% Gui, Add, Text, Section xm y+20, Hotkey: Gui, Add, Hotkey, ys w200 vxq_hk , %xq_hk% Gui, Add, Button, Default xm w70 gxq_ButtonOK, OK Gui, Add, Button, xp+185 yp wp gxq_ButtonCancel, Cancel GuiControl, Focus, OK Return ; rq_GuiBuild: Gui, 18:Default Gui, Font, s13 Gui, Add, Text, Section xm , %rq_8% Gui, Font, , Gui, Add, Text, Section xm, %rq_1% Gui, Add, Text, Section x+13 ys-20 , %rq_2% Gui, Add, Text, Section xm, %rq_3% Gui, Add, Text, Section xm, %rq_4% Gui, Add, Text, Section xm , %rq_5% Gui, Add, Edit, ys w30 Number vrq_charsperline, %rq_charsperline% Gui, Add, Text, Section xm y+20 , %rq_7% Gui, Add, Edit, ys w100 vrq_titletext, %rq_titletext% Gui, Add, Text, Section xm y+20, Hotkey: Gui, Add, Hotkey, ys w200 vrq_hk , %rq_hk% Gui, Add, Button, Default xm w70 grq_ButtonOK, OK Gui, Add, Button, xp+185 yp wp grq_ButtonCancel, Cancel GuiControl, Focus, OK Return ; ; ---------------------------------------- HotkeyOn( hk, hk_on ) { If ( hk != "" ) If hk_on Hotkey, %hk%, On Else Hotkey, %hk%, Off } ; xq_conf: Gui, 17:Default ; user hotkey off so that it can be changed and ; restored again by user during configuration HotkeyOn( xq_oldhk, 0 ) Gui,Show, ,%xq_IniFile% Return ; rq_conf: Gui, 18:Default HotkeyOn( rq_oldhk, 0 ) Gui,Show, ,%xq_IniFile% Return ; xq_ButtonCancel: 17GuiClose: 17GuiEscape: Gui, Cancel HotkeyOn( xq_oldhk, 1 ) Return ; xq_ButtonOK: Gui, Submit xq_always := ( xq_titletext = "" )^ MakeHotkey( xq_oldhk, xq_hk, "xq_InsertQuotedClipBd" ) xq_WriteIni( xrq_Ini, "xq_Config" ) Return ; rq_ButtonCancel: 18GuiClose: 18GuiEscape: Gui, Cancel HotkeyOn( rq_oldhk, 1 ) Return ; rq_ButtonOK: Gui, Submit rq_always := ( rq_titletext = "" ) MakeHotkey( rq_oldhk, rq_hk, "rq_Reformat" ) rq_WriteIni( xrq_Ini, "rq_Config" ) Return ; Exit: xq_Exit: ExitApp Return ; ---------------------------------------- CurrWinTitle( mode ) { m := A_TitleMatchMode SetTitleMatchMode, %mode% WinGetTitle, Title, A SetTitleMatchMode, %m% Return Title } ; SendThisHotkey() { hk := A_ThisHotkey prefix := "+^!#" p = 0 l := StrLen( hk ) Loop, %l% { StringMid, c, hk, %A_Index%, 1 IfInString, prefix, %c% p ++ Else Break } StringLeft, prefix, hk, p StringRight, hk, hk, l - p StringLower, hk, hk Send, %prefix%{%hk%} } ; MakeHotkey( ByRef oldhk, hk, Label ) ; install user hotkey { If ( hk != "" ) ; variable xq_hk contains the user hotkey { If ( oldhk != hk ) Hotkey, %hk%, %Label% ;xq_InsertQuotedClipBd ; %Label% ; store current hotkey in oldhk for next call of conf oldhk := hk Hotkey %oldhk%, On } } ; ---------------------------------------- xq_InsertQuotedClipBd: Active_title := CurrWinTitle( 2 ) If ( xq_always or InStr(Active_title, xq_titletext) ) { AutoTrim, Off xq_a:=Clipboard xq_InsertQuoted( xq_a ) } Else ; special function of user hotkey not applicable SendThisHotkey() Return ; xq_InsertQuoted( ByRef data ) { global xq_qmlen := StrLen( xq_quotemark ) If ( xq_charsperline > 0 ) xq_BreakLines( data, xq_charsperline - xq_qmlen + 1, xq_quotemark ) Else ; no line break of clipboard contents { StringReplace, xq_b, data, `n,`n%xq_quotemark%, A StringRight, xq_c, xq_b, xq_qmlen If xq_c =%xq_quotemark% StringTrimRight, xq_b, xq_b, xq_qmlen Clipboard=%xq_quotemark%%xq_b% } Send, ^v Clipboard=%xq_a% } ; rq_Reformat: Active_title := CurrWinTitle( 2 ) If ( rq_always or InStr(Active_title, rq_titletext) ) { rq_a := ClipboardAll Send, ^c ; Copy highlighted text to clipboard ClipWait, 0 AutoTrim, On Clipboard := rq_ReformatQuotedText( Clipboard, rq_charsperline ) Send, ^v Clipboard = %rq_a% } Else ; special function of user hotkey not applicable{ SendThisHotkey() Return ; rq_ReformatQuotedText( data, n ) { data = %data%`r`n Loop, Parse, data, `n, `r { c := A_LoopField ; for every line ... l:=StrLen( c ) qml = 0 Loop, %l% { StringMid, ch, c, %A_Index%, 1 If ( ch <> "|" and ch <> ">" and ch <> A_Space ) { qml := A_Index Break } } If ( qml = l or qml = 0 ) { b := rq_BreakaLine( b, n, lastqm . A_Space ) cpb = %cpb%%b%%c%`r`n b = } Else { If qml StringLeft, qm, c, qml-1 StringMid, c, c, qml, l qm = %qm% If ( qm = lastqm ) b = %b%%A_Space%%c% Else { b := rq_BreakaLine( b, n, lastqm . A_Space ) cpb = %cpb%%b% lastqm := qm b := c } } } StringTrimRight, cpb, cpb, 2 StringRight, c, cpb, 2 If c = `r`n StringTrimRight, cpb, cpb, 2 Return cpb } ; xq_BreakLines( ByRef data, n, quotemark ) { qmlen := StrLen( quotemark ) n -= qmlen b = Loop, Parse, data, `n, `r { c := A_LoopField ; for every line ... Loop { l:=StrLen( c ) If ( l < n ) ; no linebreak necessary Break ; search for a point to cut the line StringGetPos, p, c, %A_Space%, R1, l - n If ( ErrorLevel ) { StringGetPos, p, c, %A_Space%, L1 If ( ErrorLevel ) ; no linebreak position found Break ; leave the line as is } StringMid, d, c, 1, p StringTrimLeft, c, c, p +1 b=%b%%quotemark%%d%`r`n ; add quotemark, line, and linefeed } b=%b%%quotemark%%c%`r`n ; add quotemark, line, and linefeed } StringRight, c, b, qmlen + 2 If c =%quotemark%`r`n StringTrimRight, b, b, qmlen + 2 Clipboard=%b% } ; rq_BreakaLine( ByRef c, n, quotemark ) { If ( quotemark = A_Space ) quotemark = qmlen := StrLen( quotemark ) b = Loop { l := StrLen( c ) If ( l < n ) ; no linebreak necessary Break ; search for a point to cut the line StringGetPos, p, c, %A_Space%, R1, l - n If ( ErrorLevel ) { StringGetPos, p, c, %A_Space%, L1 If ( ErrorLevel ) ; no linebreak position found Break ; leave the line as is } StringMid, d, c, 1, p StringTrimLeft, c, c, p +1 b=%b%%quotemark%%d%`r`n ; add quotemark, line, and linefeed } If ( l > 0 ) b=%b%%quotemark%%c%`r`n ; add quotemark, line, and linefeed Return b } ; xq_Locale( lang ) { global If lang = de { xq_0 = Config Externes Zitat xq_1 := "Alle Zeilen markieren mit: " xq_2 = von Hajos xq_3 = ( Absatzenden bleiben erhalten ) xq_5 := "Zeilenumbruch nach Zeichen: `n( 0 = kein Umbruch, 1 = Wortliste ) " xq_6 = ( 0 = kein Umbruch, 1 = Wortliste ) xq_7 = Fenstertitel muss enthalten:`n(Leer: aktiv in allen Fenstern) xq_8 = Zwischenablage als externes`n Zitat einfügen } Else { xq_0 = Config External Quotes xq_1 = Mark each Clipboard line with xq_2 = by Hajos xq_3 = ( Paragraphs will be preserved ) xq_5 = Characters per line: `n ( 0 = no line Break, 1 = word list ) xq_6 = (0 = no line Break, 1 = yields a word List) xq_7 = Text in current window´s title:`n(blank: active in any window) xq_8 = Clipboard Contents: linebreak`n and marking of lines } } rq_Locale( lang ) { global rq_2 := xq_2 rq_5 := xq_5 rq_6 := xq_6 rq_7 := xq_7 If lang = de { rq_0 = Config Neuumbruch rq_1 := "Markierter Text wird neu umbrochen " rq_3 = Dabei gelten "|", ">" sowie beliebige Kombinationen `ndieser Zeichen am Zeilenanfang als Zitatzeichen. rq_4 = Zitatebenenstruktur und Leerzeilen bleiben erhalten rq_8 = Zitate neu umbrechen } Else { rq_0 = Config Rewrap Quoted Text rq_1 := " Rewrap marked text " rq_3 = Any sequences of "|" or ">" at begin of line are `nconsidered as quote marks. rq_4 = Quote level structure will be preserved. rq_8 = Rewrapping of quoted text } } ; xq_ReadIni( IniFile, Section ) { global AutoTrim, Off IniRead, xq_hk, %IniFile%, %Section%, Hotkey, ^+q IniRead, xq_quotemark, %IniFile%, %Section%, Quotemark, |"A_Space" IniRead, xq_Confgd, %IniFile%, %Section%, Confgd, 0 IniRead, xq_charsperline, %IniFile%, %Section%, CharsPerLine, 68 IniRead, xq_titletext, %IniFile%, %Section%, TitleText, Verfassen: StringReplace, xq_quotemark, xq_quotemark, "A_Space",%A_Space%, ALL } ; xq_WriteIni( IniFile, Section ) { global IniWrite, %xq_hk%, %IniFile%, %Section%, Hotkey IniWrite, %xq_hkref%, %IniFile%, %Section%, hkreformat IniWrite, 1, %IniFile%, %Section%, Confgd IniWrite, %xq_charsperline%, %IniFile%, %Section%, CharsPerLine IniWrite, %xq_titletext%, %IniFile%, %Section%, TitleText StringReplace, xq_quotemark2, xq_quotemark,%A_Space%,"A_Space", ALL IniWrite, %xq_quotemark2%,%IniFile%, %Section%, Quotemark } ; rq_ReadIni( IniFile, Section ) { global AutoTrim, Off IniRead, rq_hk, %IniFile%, %Section%, Hotkey, ^u IniRead, rq_Confgd, %IniFile%, %Section%, Confgd, 0 IniRead, rq_charsperline, %IniFile%, %Section%, CharsPerLine, 68 IniRead, rq_titletext, %IniFile%, %Section%, TitleText, Verfassen: } ; rq_WriteIni( IniFile, Section ) { global IniWrite, %rq_hk%, %IniFile%, %Section%, Hotkey IniWrite, 1, %IniFile%, %Section%, Confgd IniWrite, %rq_charsperline%, %IniFile%, %Section%, CharsPerLine IniWrite, %rq_titletext%, %IniFile%, %Section%, TitleText } ; /* [xq_Config] [rq_Config] */