Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ClipHistory
- {
- static _data := []
- , _skip := false
- , _histSize := 0
- , _filePath := ""
- , _snippets := ""
- , _prevClip := ""
- __New(conf)
- {
- if (!FileExist(conf))
- {
- MsgBox, 0x10, > ClipHistory, % conf " not found"
- return
- }
- IniRead, filePath, % conf, CLIPBOARD, path
- if (!InStr(FileExist(filePath), "D"))
- {
- MsgBox, 0x10, > ClipHistory, % "Bad path, check " conf
- return
- }
- this._filePath := filePath
- IniRead, key1, % conf, CLIPBOARD, key1, 0
- if (key1)
- {
- menuHist := ObjBindMethod(this, "_histMenu")
- Hotkey, % key1, % menuHist
- OnClipboardChange(ObjBindMethod(this, "_monitor"))
- }
- else
- {
- return
- }
- IniRead, histSize, % conf, CLIPBOARD, size, 49
- if histSize is digit
- {
- this._histSize := (histSize > 99 ? 99 : histSize)
- }
- else
- {
- this._histSize := 49
- }
- IniRead, key2, % conf, CLIPBOARD, key2, 0
- if (key2)
- {
- snipsMenu := ObjBindMethod(this, "_snipsMenu")
- Hotkey, % key2, % snipsMenu
- IniRead, snippets, % conf, SNIPPETS
- loop, Parse, snippets, `n, `r
- {
- if (A_Index > 9)
- {
- break
- }
- this._snippets .= A_LoopField "`n"
- }
- }
- }
- restore(secs := 0)
- {
- if (secs)
- {
- fn := ObjBindMethod(this, "restore")
- SetTimer, % fn, % secs * -1000
- }
- else
- {
- this._skip := true
- Clipboard := this._prevClip
- this._prevClip := ""
- }
- }
- skip(copy := false)
- {
- this._skip := true
- this._prevClip := ClipboardAll
- Clipboard := ""
- if (copy = true)
- {
- SendInput, ^{Insert} ; Sadly Chr(3) doesn't work
- ClipWait
- }
- else if (copy)
- {
- Clipboard := copy
- ClipWait
- }
- return (copy ? Clipboard : true)
- }
- _cancel()
- {
- }
- _clear()
- {
- FileDelete, % this._filePath "\*.clip"
- }
- _histMenu()
- {
- clipFiles := ""
- loop, Files, % this._filePath "\*.clip"
- {
- clipFiles .= A_LoopFileFullPath "`n"
- }
- ; No History
- if (!clipFiles)
- {
- return
- }
- Sort, clipFiles, R
- ; (Re)Set
- this._filePaths := []
- , this._data["hist"] := []
- , fnClear := ObjBindMethod(this, "_clear")
- , fnCancel := ObjBindMethod(this, "_cancel")
- , fnAction := ObjBindMethod(this, "_historyPaste")
- loop, Parse, % RTrim(clipFiles, "`n"), `n
- {
- if (A_Index > this._histSize)
- {
- FileDelete, % A_LoopField
- continue
- } ; Max History, FIFO mode.
- FileRead, txtConts, % "*P1200 " A_LoopField
- index := Format("{:02}", A_Index)
- , clipTitle := SubStr(txtConts, 1, 60)
- , this._filePaths[A_Index] := A_LoopField
- , this._data["hist"][A_Index] := txtConts
- , this._histMenuBuild(index, clipTitle)
- }
- Menu, clipMenu, Add
- Menu, clipMenu, Add, C&lear, % fnClear
- Menu, clipMenu, Add, &Cancel, % fnCancel
- Menu, clipMenu, Show
- ; Cleanup
- Menu, clipMenu, DeleteAll
- loop, 9
- {
- Menu, % "clipSub"A_Index, Add
- Menu, % "clipSub"A_Index, DeleteAll
- }
- }
- _histMenuBuild(index, title)
- {
- lDigit := SubStr(index, 1, 1)
- , rDigit := SubStr(index, 0, 1)
- , title := StrReplace(title, "&", "&&")
- , fnPaste := ObjBindMethod(this, "_paste", "hist", "dummy", index)
- if (lDigit = "0")
- {
- Menu, clipMenu, Add, % "&" rDigit ") " title, % fnPaste
- }
- else
- {
- Menu, % "clipSub" lDigit, Add, % index ") " title, % fnPaste
- }
- if (rDigit = "0")
- {
- Menu, clipMenu, Add, % "(" lDigit "0-" lDigit "9)", % ":clipSub" lDigit
- }
- }
- _monitor(type)
- {
- if (this._skip)
- {
- this._skip := false
- }
- else if (Clipboard && type = 1) ; 1=text, 2=image
- {
- ; Dupes
- loop, Files, % this._filePath "\*.clip"
- {
- FileRead, data, % "*P1200 " A_LoopFileFullPath
- if (Clipboard = data)
- {
- FileDelete, % A_LoopFileFullPath
- break
- }
- }
- FileAppend, % Clipboard, % this._filePath "\" A_now ".clip", CP1200
- }
- }
- _paste(type, dummy, index)
- {
- if (type = "snip")
- {
- this._skip := true
- }
- Clipboard := ""
- Clipboard := this._data[type][index]
- ClipWait
- SendInput, % Chr(22) ; || +{Insert}
- }
- _snipsMenu()
- {
- static built := false
- if (!built)
- {
- this._data["snip"] := []
- , snippets := RTrim(this._snippets, "`n")
- , fnCancel := ObjBindMethod(this, "_cancel")
- , fnPaste := ObjBindMethod(this, "_paste", "snip")
- loop, Parse, snippets, `n
- {
- snip := StrSplit(A_LoopField, "=",, 2)
- , this._data["snip"][A_Index] := snip[2]
- Menu, clipSnips, Add, % "(&" A_Index ") " snip[1], % fnPaste
- }
- Menu, clipSnips, Add
- Menu, clipSnips, Add, &Cancel, % fnCancel
- built := true
- }
- Menu, clipSnips, Show
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement