Skip to content

Commit

Permalink
Fuzzy open hotkey, window specific current doc, case-sensitive highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
genotrance committed Mar 15, 2019
1 parent 9603a5c commit 9187edf
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
1 change: 1 addition & 0 deletions feud.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ hotkey ^k closeWindow

hotkey ^+n newDoc
hotkey ^o togglePopup open
hotkey ^+o togglePopup open -f
hotkey ^s save
hotkey ^+s togglePopup saveAs
hotkey ^w close
Expand Down
49 changes: 31 additions & 18 deletions plugins/server/file.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@ type
cursor: int

Docs = ref object
current: int
doclist: seq[Doc]

proc getDocs(plg: var Plugin): Docs =
return getCtxData[Docs](plg)

proc getDocId(plg: var Plugin): int =
result = -1
if plg.ctx.handleCommand(plg.ctx, "getDocId"):
try:
result = plg.ctx.cmdParam[0].parseInt()
except:
discard

proc setDocId(plg: var Plugin, docid: int) =
discard plg.ctx.handleCommand(plg.ctx, &"setDocId {docid}")

proc findDocFromString(plg: var Plugin, srch: string): int =
result = -1
var
Expand Down Expand Up @@ -72,7 +82,7 @@ proc findDocFromParam(plg: var Plugin, param: string): int =

result =
if param.len == 0:
docs.current
plg.getDocId()
else:
plg.findDocFromString(param)

Expand All @@ -88,17 +98,18 @@ proc findDocFromParam(plg: var Plugin, param: string): int =
proc switchDoc(plg: var Plugin, docid: int) =
var
docs = plg.getDocs()
current = plg.getDocId()

if docid < 0 or docid > docs.doclist.len-1 or docid == docs.current:
if docid < 0 or docid > docs.doclist.len-1 or current < 0 or docid == current:
return

docs.doclist[docs.current].cursor = plg.ctx.msg(plg.ctx, SCI_GETCURRENTPOS)
discard plg.ctx.msg(plg.ctx, SCI_ADDREFDOCUMENT, 0, docs.doclist[docs.current].docptr)
docs.doclist[current].cursor = plg.ctx.msg(plg.ctx, SCI_GETCURRENTPOS)
discard plg.ctx.msg(plg.ctx, SCI_ADDREFDOCUMENT, 0, docs.doclist[current].docptr)
discard plg.ctx.msg(plg.ctx, SCI_SETDOCPOINTER, 0, docs.doclist[docid].docptr)
discard plg.ctx.msg(plg.ctx, SCI_RELEASEDOCUMENT, 0, docs.doclist[docid].docptr)
discard plg.ctx.msg(plg.ctx, SCI_GOTOPOS, docs.doclist[docid].cursor)

docs.current = docid
plg.setDocId(docid)

discard plg.ctx.handleCommand(plg.ctx, &"setTitle {docid}: {docs.doclist[docid].path}")

Expand Down Expand Up @@ -242,10 +253,11 @@ proc open(plg: var Plugin) {.feudCallback.} =
proc save(plg: var Plugin) {.feudCallback.} =
var
docs = plg.getDocs()
current = plg.getDocId()

if docs.doclist.len != 0 and docs.current != 0:
if docs.doclist.len != 0 and current > 0:
let
doc = docs.doclist[docs.current]
doc = docs.doclist[current]

if doc.path == "New document":
plg.ctx.notify(plg.ctx, &"Save new document using saveAs <fullpath>")
Expand All @@ -265,15 +277,15 @@ proc save(plg: var Plugin) {.feudCallback.} =
f.close()
plg.ctx.notify(plg.ctx, &"Saved {doc.path}")

discard plg.ctx.handleCommand(plg.ctx, &"setTitle {docs.current}: {doc.path}")
discard plg.ctx.handleCommand(plg.ctx, &"setTitle {current}: {doc.path}")
except:
plg.ctx.notify(plg.ctx, &"Failed to save {doc.path}")

proc saveAs(plg: var Plugin) {.feudCallback.} =
if plg.ctx.cmdParam.len != 0:
var
docs = plg.getDocs()
doc = docs.doclist[docs.current]
doc = docs.doclist[plg.getDocId()]

doc.path = plg.ctx.cmdParam[0].expandFilename()

Expand All @@ -300,16 +312,17 @@ proc close(plg: var Plugin) {.feudCallback.} =

var
docid = plg.findDocFromParam(param)
current = plg.getDocId()

if docid > 0:
if docid == docs.current:
if docid > 0 and current > -1:
if docid == current:
if docid == docs.doclist.len-1:
plg.switchDoc(docid-1)
else:
plg.switchDoc(docid+1)
else:
if docid < docs.current:
docs.current -= 1
if docid < current:
plg.setDocId(current-1)

discard plg.ctx.msg(plg.ctx, SCI_RELEASEDOCUMENT, 0, docs.doclist[docid].docptr)
docs.doclist.del(docid)
Expand All @@ -325,7 +338,7 @@ proc closeAll(plg: var Plugin) {.feudCallback.} =
proc reload(plg: var Plugin) {.feudCallback.} =
var
docs = plg.getDocs()
docid = docs.current
docid = plg.getDocId()

if docid > 0:
let
Expand All @@ -341,7 +354,7 @@ proc next(plg: var Plugin) {.feudCallback.} =

if docs.doclist.len != 1:
var
docid = docs.current
docid = plg.getDocId()
docid += 1
if docid == docs.doclist.len:
docid = 0
Expand All @@ -354,7 +367,7 @@ proc prev(plg: var Plugin) {.feudCallback.} =

if docs.doclist.len != 1:
var
docid = docs.current
docid = plg.getDocId()
docid -= 1
if docid < 0:
docid = docs.doclist.len-1
Expand All @@ -375,4 +388,4 @@ feudPluginLoad:
discard plg.ctx.msg(plg.ctx, SCI_SETDOCPOINTER, 0, notif.docptr, windowID=0)

docs.doclist.add notif
docs.current = 0
plg.setDocId(0)
3 changes: 2 additions & 1 deletion plugins/server/search.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ proc search(plg: var Plugin) {.feudCallback.} =
discard plg.ctx.msg(plg.ctx, SCI_TARGETWHOLEDOCUMENT)
discard plg.ctx.msg(plg.ctx, SCI_SETTARGETSTART, curpos+1)
pos = plg.ctx.msg(plg.ctx, SCI_SEARCHINTARGET, search.needle.len, search.needle.cstring)

if pos != -1:
discard plg.ctx.msg(plg.ctx, SCI_GOTOPOS, pos)
else:
Expand All @@ -102,6 +102,7 @@ proc highlight(plg: var Plugin) {.feudCallback.} =
pos = 0
while pos != -1:
discard plg.ctx.msg(plg.ctx, SCI_TARGETWHOLEDOCUMENT)
discard plg.ctx.msg(plg.ctx, SCI_SETSEARCHFLAGS, SCFIND_MATCHCASE)
discard plg.ctx.msg(plg.ctx, SCI_SETTARGETSTART, pos)
pos = plg.ctx.msg(plg.ctx, SCI_SEARCHINTARGET, search.len, search.cstring)
if pos != -1:
Expand Down
20 changes: 20 additions & 0 deletions plugins/server/window.nim
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,26 @@ proc execPopup(plg: var Plugin) =
plg.addHistory()
discard plg.ctx.handleCommand(plg.ctx, cmd)

proc getDocId(plg: var Plugin) {.feudCallback.} =
var
windows = plg.getWindows()
winid = windows.current

plg.ctx.cmdParam = @[$(windows.editors[winid].docid)]

proc setDocId(plg: var Plugin) {.feudCallback.} =
if plg.ctx.cmdParam.len != 0:
var
windows = plg.getWindows()
winid = windows.current
docid = -1

try:
docid = plg.ctx.cmdParam[0].parseInt()
windows.editors[winid].docid = docid
except:
discard

feudPluginDepends(["config"])

feudPluginLoad:
Expand Down

0 comments on commit 9187edf

Please sign in to comment.