From ebc4c30828b1f43d0643defd1eaa651043cbb3c6 Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Sat, 11 May 2019 03:16:37 -0500 Subject: [PATCH] Reload file on change, notifs on new window, goto end of notifs, fix theme on new window --- README.md | 4 +++- plugins/server/file.nim | 38 +++++++++++++++++++++++++++------ plugins/server/filetype.nim | 13 +++++------ plugins/server/window/frame.nim | 1 + 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 5c6adc2..502ab04 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,9 @@ saveAs - save current buffer to new path ``` Drag and drop of files is supported. The current directory changes to the path of the -currently loaded file if `file:fileChdir` is set to `true`. +currently loaded file if `file:fileChdir` is set to `true`. Files are also automatically +reloaded if they changed while working on another file or application and there +are no unsaved modifications. Changing directories: ``` diff --git a/plugins/server/file.nim b/plugins/server/file.nim index dc47b82..986ffa6 100644 --- a/plugins/server/file.nim +++ b/plugins/server/file.nim @@ -1,4 +1,4 @@ -import deques, os, sequtils, sets, strformat, strutils, tables +import deques, os, sequtils, sets, strformat, strutils, tables, times import "../.."/src/pluginapi import "../.."/wrappers/fuzzy @@ -10,6 +10,7 @@ type path: string docptr: pointer cursor: int + syncTime: Time windows: HashSet[int] Docs = ref object @@ -128,7 +129,7 @@ proc switchDoc(plg: var Plugin, docid: int) = currDoc = plg.getDocId() currWindow = plg.getCbIntResult("getCurrentWindow", -1) - if docid < 0 or docid > docs.doclist.len-1 or currWindow < 0 or currDoc < 0 or docid == currDoc: + if docid < 0 or docid > docs.doclist.len-1 or currWindow < 0 or currDoc < 0 or (docid == currDoc and docid != 0): return docs.doclist[currDoc].cursor = plg.ctx.msg(plg.ctx, SCI_GETCURRENTPOS) @@ -155,6 +156,11 @@ proc switchDoc(plg: var Plugin, docid: int) = else: docs.dirHistory.peekFirst().setCurrentDir() + if docid == 0: + let + length = plg.ctx.msg(plg.ctx, SCI_GETLENGTH) + discard plg.ctx.msg(plg.ctx, SCI_GOTOPOS, length) + discard plg.ctx.handleCommand(plg.ctx, "runHook postFileSwitch") proc loadFileContents(plg: var Plugin, path: string) = @@ -315,6 +321,7 @@ proc open(plg: var Plugin) {.feudCallback.} = doc.windows.init() doc.path = path doc.docptr = plg.ctx.msg(plg.ctx, SCI_CREATEDOCUMENT, info.size.toPtr).toPtr + doc.syncTime = path.getLastModificationTime() docs.doclist.add doc @@ -351,6 +358,8 @@ proc save(plg: var Plugin) {.feudCallback.} = f.close() plg.ctx.notify(plg.ctx, &"Saved {doc.path}") + doc.syncTime = doc.path.getLastModificationTime() + discard plg.ctx.msg(plg.ctx, SCI_SETSAVEPOINT) discard plg.ctx.handleCommand(plg.ctx, &"setTitle {doc.path}") except: @@ -485,14 +494,14 @@ proc reload(plg: var Plugin) {.feudCallback.} = var docs = plg.getDocs() docid = plg.getDocId() + doc = docs.doclist[docid] if docid > 0: - let - path = docs.doclist[docid].path + plg.loadFileContents(doc.path) - plg.loadFileContents(path) + doc.syncTime = doc.path.getLastModificationTime() - plg.ctx.notify(plg.ctx, &"Reloaded {path}") + plg.ctx.notify(plg.ctx, &"Reloaded {doc.path}") proc reloadAll(plg: var Plugin) {.feudCallback.} = var @@ -504,6 +513,18 @@ proc reloadAll(plg: var Plugin) {.feudCallback.} = plg.next() plg.reload() +proc reloadIfChanged(plg: var Plugin) {.feudCallback.} = + var + docs = plg.getDocs() + docid = plg.getDocId() + doc = docs.doclist[docid] + + if doc.path.fileExists() and doc.syncTime < doc.path.getLastModificationTime(): + if plg.ctx.msg(plg.ctx, SCI_GETMODIFY) == 0: + plg.reload() + else: + plg.ctx.notify(plg.ctx, &"File '{doc.path.extractFilename()}' with unsaved modifications changed behind the scenes") + proc cd(plg: var Plugin) {.feudCallback.} = var docs = plg.getDocs() @@ -558,4 +579,7 @@ feudPluginLoad: docs.dirHistory = initDeque[string]() docs.dirHistory.addLast(getCurrentDir()) - discard plg.ctx.handleCommand(plg.ctx, "hook preCloseWindow unload") \ No newline at end of file + discard plg.ctx.handleCommand(plg.ctx, "hook preCloseWindow unload") + discard plg.ctx.handleCommand(plg.ctx, "hook onWindowActivate reloadIfChanged") + discard plg.ctx.handleCommand(plg.ctx, "hook postFileSwitch reloadIfChanged") + discard plg.ctx.handleCommand(plg.ctx, "hook postNewWindow open 0") diff --git a/plugins/server/filetype.nim b/plugins/server/filetype.nim index 0ee5ed1..ca4594b 100644 --- a/plugins/server/filetype.nim +++ b/plugins/server/filetype.nim @@ -1,4 +1,4 @@ -import os, parsexml, sequtils, streams, strutils, tables, xmltree +import os, parsexml, sequtils, streams, strformat, strutils, tables, xmltree import "../../src"/pluginapi @@ -130,23 +130,20 @@ proc setLexer(plg: var Plugin) {.feudCallback.} = var lang = plg.getLang() - plg.ctx.cmdParam = @[] if not lang.isNil: - if lang.lexer == plg.getLexer(): - return + if lang.lexer != plg.getLexer(): + plg.ctx.notify(plg.ctx, &"Set language to {lang.name} for '{plg.ctx.cmdParam[0].extractFilename()}'") plg.resetLexer() - discard plg.ctx.msg(plg.ctx, SCI_SETLEXER, lang.lexer) for i in 0 .. lang.keywords.len-1: discard plg.ctx.msg(plg.ctx, SCI_SETKEYWORDS, i, lang.keywords[i].cstring) - plg.ctx.notify(plg.ctx, "Set language to " & lang.name) - plg.ctx.cmdParam = @[lang.lexName] else: plg.resetLexer() - + plg.ctx.cmdParam = @[] + feudPluginDepends(["window"]) feudPluginLoad: diff --git a/plugins/server/window/frame.nim b/plugins/server/window/frame.nim index 43ebc98..d2d2014 100644 --- a/plugins/server/window/frame.nim +++ b/plugins/server/window/frame.nim @@ -33,6 +33,7 @@ proc frameCallback(hwnd: HWND, msg: UINT, wParam: WPARAM, lParam: LPARAM): LRESU of WM_ACTIVATE: hwnd.setFocus() plg.setCurrentWindow(hwnd) + discard plg.ctx.handleCommand(plg.ctx, "runHook onWindowActivate") of WM_CREATE: var pCreate = cast[ptr CREATESTRUCT](lParam)