Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *F1::commenter.Show()
- *F2::commenter.Hide()
- class commenter {
- #Requires Autohotkey v2.0.18+
- ; === User Methods ===
- ; Show Commenter GUI
- static show() => WinExist('ahk_id ' this.gui.hwnd) ? 0 : this.gui.Show()
- ; Hide Commenter GUI
- static hide() => WinExist('ahk_id ' this.gui.hwnd) ? this.gui.Hide() : 0
- ; === Internal ===
- static title := 'AHK Commenter'
- static __New() => this.make_gui()
- static make_gui() {
- start_w := A_ScreenWidth * 0.4
- start_h := A_ScreenHeight * 0.4
- btn_w := 100
- btn_h := 30
- cb_w := 100
- cb_h := 30
- pad := 5
- min_width := (btn_w + pad) * 5 + pad
- min_height := 300
- bg_color := 0x101010
- min_size := '+MinSize' min_width 'x' min_height
- this.gui := goo := Gui('+Resize ' min_size, this.title, this.events)
- goo.BackColor := bg_color
- goo.MarginX := goo.MarginY := pad
- goo.OnEvent('Size', gui_resize)
- ; Add buttons
- goo.SetFont('bold')
- goo.AddButton('vbtn_com_add', 'Add`nComments').OnEvent('Click', 'add_comments')
- goo.AddButton('vbtn_com_rem', 'Remove`nComments').OnEvent('Click', 'remove_comments')
- goo.AddButton('vbtn_clipboard', 'Save to`nClipboard').OnEvent('Click', 'save_to_clip')
- goo.AddButton('vbtn_clipboard_reddit', 'Reddit Format to`nClipboard').OnEvent('Click', 'save_to_clip_reddit')
- goo.AddButton('vbtn_close', 'Close').OnEvent('Click', (con, *) => con.Gui.Hide())
- goo.SetFont('norm')
- ; Add edits
- con := goo.AddEdit('vedt_left +Multi +Background0 +HScroll', 'Paste Code Here')
- con.SetFont('cWhite', 'Courier New')
- con.SetFont('cWhite', 'Consolas')
- con := goo.AddEdit('vedt_right +Multi +ReadOnly +Background0 +HScroll')
- con.SetFont('cWhite', 'Courier New')
- con.SetFont('cWhite', 'Consolas')
- gui_resize(goo, 0, start_w, start_h)
- goo.Show('w' start_w ' h' start_h)
- goo['edt_left'].Focus()
- return
- gui_resize(goo, MinMax, Width, Height) {
- last_left := last_top := last_right := last_bottom := last_width := last_height := unset
- ; Edit fields
- set('edt_left', pad, pad, (Width - pad * 3) / 2, height - pad * 3 - btn_h)
- set('edt_right',last_right + pad,last_top,last_width,last_height)
- ; Controls
- set('btn_com_add', pad, height - pad - btn_h, btn_w, btn_h)
- set('btn_com_rem', last_right + pad, last_top, last_width, last_height)
- set('btn_close', width - pad - btn_w, last_top, btn_w, btn_h)
- set('btn_clipboard', last_left - pad - btn_w, last_top, last_width, last_height)
- set('btn_clipboard_reddit', last_left - pad - btn_w, last_top, last_width, last_height)
- return
- set(name, x, y, w, h) {
- last_left := x
- last_top := y
- last_width := w
- last_height := h
- last_right := x + w
- last_bottom := y + h
- goo[name].Move(x, y, w, h)
- }
- }
- }
- ; Contains all gui control events
- class events {
- static rgx_comment := '^(.*\S.*)[ \t](;.*?)$'
- static add_comments(btn, *) {
- ; Get length of longest line
- max_line_len := 0
- text := btn.gui['edt_left'].Text
- loop parse text, '`n', '`r' {
- if RegExMatch(A_LoopField, this.rgx_comment, &match)
- len := StrLen(match[1])
- else len := StrLen(A_LoopField)
- if (len > max_line_len)
- max_line_len := len
- }
- ; Apply comments to each line and align previous comments.
- result := ''
- loop parse text, '`n', '`r' {
- ; if blank line, no change
- if RegExMatch(A_LoopField, '^\s*$')
- result .= A_LoopField
- ; if header comment, no change
- else if RegExMatch(A_LoopField, '^[ \t]*;.*$')
- result .= A_LoopField
- ; if comment already exists, fix it
- else if RegExMatch(A_LoopField, this.rgx_comment, &match)
- result .= match[1] make_pad(1 + max_line_len - StrLen(match[1])) match[2]
- ; else add a comment
- else result .= A_LoopField make_pad(max_line_len - StrLen(A_LoopField)) ' `; '
- result .= '`r`n'
- }
- btn.Gui['edt_right'].Text := result
- return
- make_pad(size:=0, char:=' ') {
- pad := ''
- loop size
- pad .= char
- return pad
- }
- }
- static remove_comments(btn, *) {
- text := btn.Gui['edt_left'].Text
- result := ''
- loop parse text, '`n', '`r'
- if RegExMatch(A_LoopField, this.rgx_comment, &match)
- result .= match[1] '`r`n'
- else result .= A_LoopField '`r`n'
- btn.Gui['edt_right'].Text := result
- }
- static save_to_clip(btn, *) => A_Clipboard := btn.Gui['edt_right'].Text
- static save_to_clip_reddit(btn, *) {
- txt := btn.Gui['edt_right'].Text
- A_Clipboard := '`r`n`r`n ' StrReplace(txt, '`r`n', '`r`n ')
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement