From 34f09740b6f2ddb785bd886ce40af4a1cfc85439 Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Sun, 12 May 2019 02:01:48 -0500 Subject: [PATCH] Toggle comment feature, special char hotkeys, fix getDocPath bug --- README.md | 5 +++ feud.ini | 2 ++ plugins/server/buffer.nim | 56 ++++++++++++++++++++++++++++++++ plugins/server/file.nim | 2 +- plugins/server/gist.nim | 7 +++- plugins/server/window/hotkey.nim | 16 +++++---- 6 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 plugins/server/buffer.nim diff --git a/README.md b/README.md index 502ab04..7471177 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,11 @@ newWindow - open a new editor window closeWindow - close current window ``` +Buffer actions: +``` +toggleComment - comment / uncomment selection or line +``` + Shell: ``` ! command - Run command and print output to new buffer diff --git a/feud.ini b/feud.ini index ce25967..52cb78f 100644 --- a/feud.ini +++ b/feud.ini @@ -42,6 +42,8 @@ hotkey ^l last hotkey ^r togglePopup ! hotkey ^+r togglePopup | +hotkey ^/ toggleComment + # Tabs and indents # https://www.scintilla.org/ScintillaDoc.html#TabsAndIndentationGuides hook postFileSwitch eMsg SCI_SETUSETABS 0 diff --git a/plugins/server/buffer.nim b/plugins/server/buffer.nim new file mode 100644 index 0000000..0730dbd --- /dev/null +++ b/plugins/server/buffer.nim @@ -0,0 +1,56 @@ +import os, strformat, strutils + +import "../.."/src/pluginapi + +proc toggleComment(plg: var Plugin) {.feudCallback.} = + var + selStart = plg.ctx.msg(plg.ctx, SCI_GETSELECTIONSTART) + selEnd = plg.ctx.msg(plg.ctx, SCI_GETSELECTIONEND) + selStartLine = plg.ctx.msg(plg.ctx, SCI_LINEFROMPOSITION, selStart) + selEndLine = plg.ctx.msg(plg.ctx, SCI_LINEFROMPOSITION, selEnd) + + selStart = plg.ctx.msg(plg.ctx, SCI_POSITIONFROMLINE, selStartLine) + if selEnd == 0: + selEnd = plg.ctx.msg(plg.ctx, SCI_GETLINEENDPOSITION, selStartLine) + else: + let + selEndLineStart = plg.ctx.msg(plg.ctx, SCI_POSITIONFROMLINE, selEndLine) + if selEnd == selEndLineStart: + selEndLine -= 1 + selEnd = plg.ctx.msg(plg.ctx, SCI_GETLINEENDPOSITION, selEndLine) + + if selStart != selEnd: + var + length = selEnd - selStart + data = alloc0(length+1) + sel: seq[string] + defer: data.dealloc() + + discard plg.ctx.msg(plg.ctx, SCI_SETTARGETRANGE, selStart, selEnd.toPtr) + discard plg.ctx.msg(plg.ctx, SCI_GETTARGETTEXT, 0, data) + sel = ($cast[cstring](data)).splitLines(keepEol = true) + + if sel.len != 0: + let + docPath = plg.getCbResult("getDocPath") + + if docPath.len != 0: + let + commentLine = plg.getCbResult(&"getCommentLine {docPath.quoteShell}") + commentLen = commentLine.len + + if commentLen != 0: + for i in 0 .. sel.len-1: + if sel[i].startsWith(commentLine): + sel[i] = sel[i][commentLen .. ^1] + elif sel[i].strip().len != 0: + sel[i] = commentLine & sel[i] + + let + newSel = sel.join("") + discard plg.ctx.msg(plg.ctx, SCI_REPLACETARGET, newSel.len, newSel.cstring) + +feudPluginDepends(["config", "window"]) + +feudPluginLoad() + \ No newline at end of file diff --git a/plugins/server/file.nim b/plugins/server/file.nim index 986ffa6..edf84c9 100644 --- a/plugins/server/file.nim +++ b/plugins/server/file.nim @@ -39,7 +39,7 @@ proc getDocPath(plg: var Plugin) {.feudCallback.} = if docid != -1: plg.ctx.cmdParam = @[docs.doclist[docid].path] else: - plg.ctx.cmdParam = @[""] + plg.ctx.cmdParam = @[] proc setCurrentDir(plg: var Plugin, dir: string) = var diff --git a/plugins/server/gist.nim b/plugins/server/gist.nim index 0c70d4b..3a2cd79 100644 --- a/plugins/server/gist.nim +++ b/plugins/server/gist.nim @@ -79,7 +79,12 @@ proc gist(plg: var Plugin) {.feudCallback.} = var client = newHttpClient(proxy = getProxy()) url = "http://ix.io" - name = plg.getCbResult("getDocPath").extractFilename() + path = plg.getCbResult("getDocPath") + name = + if path.len != 0: + path.extractFilename() + else: + "test.txt" post = "name:1=" & name & "&f:1=" success = false gistUrl = "" diff --git a/plugins/server/window/hotkey.nim b/plugins/server/window/hotkey.nim index d57afc8..8122416 100644 --- a/plugins/server/window/hotkey.nim +++ b/plugins/server/window/hotkey.nim @@ -1,8 +1,12 @@ -const VKTable = { - "F1": 112, "F2": 113, "F3": 114, "F4": 115, "F5": 116, "F6": 117, "F7": 118, "F8": 119, "F9": 120, "F10": 121, - "F11": 122, "F12": 123, "F13": 124, "F14": 125, "F15": 126, "F16": 127, "F17": 128, "F18": 129, "F19": 130, "F20": 131, - "F21": 132, "F22": 133, "F23": 134, "F24": 135, "Tab": 9, "PgDn": 34, "PgUp": 35, "Home": 36, "End": 35 -}.toTable() +const + VKTable = { + "F1": 112, "F2": 113, "F3": 114, "F4": 115, "F5": 116, "F6": 117, "F7": 118, "F8": 119, "F9": 120, "F10": 121, + "F11": 122, "F12": 123, "F13": 124, "F14": 125, "F15": 126, "F16": 127, "F17": 128, "F18": 129, "F19": 130, "F20": 131, + "F21": 132, "F22": 133, "F23": 134, "F24": 135, "Tab": 9, "PgDn": 34, "PgUp": 35, "Home": 36, "End": 35, + + ";": 186, ":": 186, "+": 187, ",": 188, "<": 188, "-": 189, "_": 189, ".": 190, ">": 190, "/": 191, "?": 191, + "~": 192, "`": 192, "[": 219, "{": 219, "\\": 220, "|": 220, "]": 221, "}": 221, "'": 222, "\"": 222 + }.toTable() proc hotkey(plg: var Plugin) {.feudCallback.} = var @@ -41,7 +45,7 @@ proc hotkey(plg: var Plugin) {.feudCallback.} = of '+': fsModifiers = fsModifiers or MOD_SHIFT else: - if spec.len != 0 or i != hotkey.len-1: + if spec.len != 0 or i != hotkey.len-1 or VKTable.hasKey($hotkey[i]): spec &= hotkey[i] else: vk = hotkey[i].toUpperAscii