vim-marked icon indicating copy to clipboard operation
vim-marked copied to clipboard

`:MarkedPreview` docs are not auto-closed

Open itspriddle opened this issue 1 year ago • 1 comments

With g:marked_auto_quit set, Marked documents that are opened with :MarkedPreview are not closed.

itspriddle avatar Jun 08 '24 20:06 itspriddle

Someday, I'll get back to this. I ran into an issue where Marked hangs for some reason when closing windows that were opened from the clipboard. I need to be a good citizen and report it, but I haven't had time 🙃


 plugin/marked.vim | 53 +++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 49 insertions(+), 4 deletions(-)

diff --git a/plugin/marked.vim b/plugin/marked.vim
index 56eabb6..91baa02 100644
--- a/plugin/marked.vim
+++ b/plugin/marked.vim
@@ -8,7 +8,6 @@
 if &cp || (exists("g:marked_loaded") && g:marked_loaded) || !executable("osascript")
   finish
 endif
-
 let g:marked_loaded = 1
 let s:save_cpo = &cpo
 set cpo&vim
@@ -24,6 +23,32 @@ function! s:AddDocument(path) abort
   endif
 endfunction
 
+function! MarkedPreviews()
+  return split(s:RunApplescript("listPreviews"), ",")
+endfunction
+
+function! s:DocumentFindPath(path) abort
+  if filereadable(a:path)
+    echom "DocumentFindPath - file exists " . a:path
+    return a:path
+  endif
+
+  echom "DocumentFindPath - fetching previews"
+  let previews = s:RunApplescript("listPreviews")
+
+  for preview in split(previews, ",")
+    echom "DocumentFindPath - checking preview " . preview
+    let last_line = readfile(preview, '', -1)[0]
+
+    if last_line =~ "^<!-- " . a:path . " -->$"
+      echom "DocumentFindPath - found preview " . preview
+      return preview
+    endif
+  endfor
+
+  return ""
+endfunction
+
 function! s:RemoveDocument(path) abort
   unlet! s:open_documents[index(s:open_documents, a:path)]
 endfunction
@@ -48,7 +73,9 @@ function! s:MarkedOpen(background, path) abort
 
   redraw!
 endfunction
-
+function! MarkedDocs()
+  echo join(s:open_documents, "\n")
+endfunction
 function! s:MarkedQuit(force, path) abort
   if s:MarkedVersionCheck() == 0
     return
@@ -59,6 +86,7 @@ function! s:MarkedQuit(force, path) abort
     call s:RunApplescript("quit")
   else
     call s:RemoveDocument(a:path)
+    echom "MarkedQuit - calling closeDocument " . a:path
     call s:RunApplescript("closeDocument", a:path)
   endif
 
@@ -67,7 +95,11 @@ endfunction
 
 function! s:MarkedPreview(background, line1, line2) abort
   let lines = getline(a:line1, a:line2)
+  let id = "vim-marked-preview:" . localtime()
 
+  call add(lines, "<!-- " . id . " -->")
+
+  call s:AddDocument(id)
   call s:MarkedOpenURI(a:background, "preview", { "text": join(lines, "\n") })
 endfunction
 
@@ -87,6 +119,9 @@ let s:js =<< trim JS
 
     if (action == "quit") {
       app.quit();
+    } else if (action == "listPreviews") {
+      return app.documents().filter((d) => d.path().match(/Clipboard Preview/))
+        .map((d) => d.path()).join(",");
     } else if (action == "closeDocument") {
       let doc = app.documents().find((d) => d.path() == path)
 
@@ -122,9 +157,17 @@ endfunction
 
 function! s:MarkedQuitVimLeave() abort
   if get(g:, "marked_auto_quit", 1)
-    for document in s:open_documents
-      call s:MarkedQuit(0, document)
+    for name in s:open_documents
+      echom "checking s:open_documents " . name
+
+      let document = s:DocumentFindPath(name)
+
+      if document != ""
+        echom "quitting " . document
+        call s:MarkedQuit(0, document)
+      endif
     endfor
+    " sleep 15
   endif
 endfunction
 
@@ -133,6 +176,8 @@ function! s:MarkedVersionCheck() abort
     let s:marked_version = s:RunApplescript("version")
   endif
 
+  echom "checking marked version"
+
   if s:marked_version =~ "^1"
     echohl WarningMsg
     echomsg "marked.vim requires Marked 2 but you've configured it to use Marked 1."

itspriddle avatar Feb 07 '25 02:02 itspriddle