about summary refs log tree commit diff
path: root/fnl/nvrc/utils.fnl
diff options
context:
space:
mode:
authorsefidel <contact@sefidel.net>2022-02-10 00:24:03 +0900
committersefidel <contact@sefidel.net>2022-02-10 00:24:03 +0900
commit72d448e384249103748ee83b587c45924e4bc44d (patch)
tree2aa05a6aaf8c7aa37a8c278fd2fede6e62ff2218 /fnl/nvrc/utils.fnl
downloadnvimrc-72d448e384249103748ee83b587c45924e4bc44d.tar.gz
nvimrc-72d448e384249103748ee83b587c45924e4bc44d.zip
Initial commit
Diffstat (limited to 'fnl/nvrc/utils.fnl')
-rw-r--r--fnl/nvrc/utils.fnl127
1 files changed, 127 insertions, 0 deletions
diff --git a/fnl/nvrc/utils.fnl b/fnl/nvrc/utils.fnl
new file mode 100644
index 0000000..6812c0c
--- /dev/null
+++ b/fnl/nvrc/utils.fnl
@@ -0,0 +1,127 @@
+(fn tbl? [x]
+  (= :table (type x)))
+
+(fn count [xs]
+  (if (tbl? xs) (table.maxn xs)
+      (not xs) 0
+      (length xs)))
+
+(fn run! [f xs]
+  "Execute the function (for side effects) for every xs."
+  (when xs
+    (let [nxs (count xs)]
+      (when (> nxs 0)
+        (for [i 1 nxs]
+          (f (. xs i)))))))
+
+(fn reduce [f init xs]
+  "Reduce xs into a result by passing each subsequent value into the fn with
+  the previous value as the first arg. Starting with init."
+  (var result init)
+  (run! (fn [x]
+                (set result (f result x))) xs)
+  result)
+
+(fn merge! [base ...]
+  (reduce (fn [acc m]
+                  (when m
+                    (each [k v (pairs m)]
+                      (tset acc k v)))
+                  acc) (or base {}) [...]))
+
+(fn merge [...]
+  (merge! {} ...))
+
+(fn lsp_fidget []
+  "Simple implementation of LSP fidget. Returns current LSP status with a spinner"
+  (let [lsp (. (vim.lsp.util.get_progress_messages) 1)]
+    (when lsp
+      (let [msg (or lsp.message "")
+          percentage (or lsp.percentage 0)
+          title (or lsp.title "")
+          spinners {1"⠋" 2 "⠙" 3 "⠹" 4 "⠸" 5 "⠼" 6 "⠴" 7 "⠦" 8 "⠧" 9 "⠇" 10 "⠏"}
+          success-icon "^.^!"
+          ms (/ (vim.loop.hrtime) 1000000)
+          frame (% (math.floor (/ ms 120)) (length spinners))]
+      (when (>= percentage 70)
+        (let [ertn [(string.format " %%<%s %s %s (%s%%%%) "
+                                   success-icon
+                                   title msg
+                                   percentage)]]
+          (lua "return (table.unpack or _G.unpack)(ertn)")))
+      (let [ertn [(string.format " %%<%s %s %s (%s%%%%) "
+                                 (. spinners
+                                    (+ frame 1))
+                                 title msg
+                                 percentage)]]
+        (lua "return (table.unpack or _G.unpack)(ertn)"))))) "")
+
+(fn will_it_fit [width winid]
+  "Returns whether this module will fit in the given width"
+  (> (vim.api.nvim_win_get_width (or (tonumber winid) 0)) width))
+
+(fn close_buf [force]
+  "ojroques/nvim-bufdel 'BSD-2'"
+  (let [opts {:next :cycle :quit false}]
+    (fn switch-buffer [windows buf]
+      (let [cur-win (vim.fn.winnr)]
+        (each [_ winid (ipairs windows)]
+          (set-forcibly! winid (or (tonumber winid) 0))
+          (vim.cmd (string.format "%d wincmd w" (vim.fn.win_id2win winid)))
+          (vim.cmd (string.format "buffer %d" buf)))
+        (vim.cmd (string.format "%d wincmd w" cur-win))))
+
+    (fn get-next-buf [buf]
+      (var next (vim.fn.bufnr "#"))
+      (when (and (= opts.next :alternate) (= (vim.fn.buflisted next) 1))
+        (lua "return next"))
+      (for [i 0 (- (vim.fn.bufnr "$") 1) 1]
+        (set next (+ (% (+ buf i) (vim.fn.bufnr "$")) 1))
+        (when (= (vim.fn.buflisted next) 1)
+          (lua "return next"))))
+
+    (local buf (vim.fn.bufnr))
+    (when (= (vim.fn.buflisted buf) 0)
+      (vim.cmd :close)
+      (lua "return "))
+    (when (< (length (vim.fn.getbufinfo {:buflisted 1})) 2)
+      (when opts.quit
+        (if force (vim.cmd :qall!) (vim.cmd "confirm qall"))
+        (lua "return "))
+      (local (term _)
+             (pcall (fn []
+                      (vim.api.nvim_buf_get_var buf :term_type))))
+      (when term
+        (vim.cmd (string.format "setlocal nobl" buf))
+        (vim.cmd :enew)
+        (lua "return "))
+      (vim.cmd :enew)
+      (vim.cmd :bp))
+    (local next-buf (get-next-buf buf))
+    (local windows (. (. (vim.fn.getbufinfo buf) 1) :windows))
+    (if (or force (= (vim.fn.getbufvar buf :&buftype) :terminal))
+        (let [(term type) (pcall (fn []
+                                   (vim.api.nvim_buf_get_var buf :term_type)))]
+          (if term
+              (if (= type :wind)
+                  (do
+                    (vim.cmd (string.format "%d bufdo setlocal nobl" buf))
+                    (vim.cmd :BufferLineCycleNext))
+                  (let [cur-win (vim.fn.winnr)]
+                    (vim.cmd (string.format "%d wincmd c" cur-win))
+                    (lua "return ")))
+              (do
+                (switch-buffer windows next-buf)
+                (vim.cmd (string.format "bd! %d" buf)))))
+        (do
+          (switch-buffer windows next-buf)
+          (vim.cmd (string.format "silent! confirm bd %d" buf))))
+    (when (= (vim.fn.buflisted buf) 1)
+      (switch-buffer windows buf))))
+
+(fn defer_unpack [pack after]
+  (when pack
+    (set-forcibly! after (or after 0))
+    (vim.defer_fn (fn [] ((. (require :packer) :loader) pack)) after)))
+
+{: merge : lsp_fidget : will_it_fit : close_buf : defer_unpack}