Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164;; Set to false if nerd font is missing/broken;; Icon reference: https://www.nerdfonts.com/cheat-sheet(local icons-enabled true)
(macro inv [group] ;; inverse of syntax groups (swap fg <-> bg) `(let [hl# (vim.api.nvim_get_hl 0 {:name ,group :link false})] {:fg hl#.bg :bg hl#.fg}))
(macro hl [group opts] `(vim.api.nvim_set_hl 0 ,group ,opts))
(hl :StlNormal {:link :ModeMsg :default true})(hl :StlInsert {:link :special :default true})(hl :StlVisual {:link :Visual :default true})(hl :StlReplace {:link :Number :default true})(hl :StlCommand {:link :Keyword :default true})
; (->> {:callback (fn [])}; ;; (hl :StlNormal (link :StlNormal :ModeMsg)); ;; (hl :StlInsert (inv :Special)); ;; (hl :StlReplace (inv :Number)); ;; (hl :StlCommand (inv :Comment)))}; (vim.api.nvim_create_autocmd :ColorScheme))
(fn get-hl-bg [group] (. (vim.api.nvim_get_hl 0 {:name group :link false}) :bg))
(fn current-mode-info [] (let [m (: (. (vim.api.nvim_get_mode) :mode) :sub 1 1) mode-map {:n [:NORMAL :StlNormal] :i [:INSERT :StlInsert] :wv [:VISUAL :StlVisual] :V [:V-LINE :StlVisual] "\022" [:V-BLOCK :StlVisual] :R [:REPLACE :StlReplace] :c [:COMMAND :StlCommand] :t [:TERMINAL :StlInsert] :s [:SELECT :StlVisual] :S [:S-LINE :StlVisual]}] (or (. mode-map m) [:NORMAL :StlNormal])))
(fn mode-component [] (let [info (current-mode-info) hl-name (. info 2)] (vim.api.nvim_set_hl 0 :StlModeSep {:fg (get-hl-bg hl-name) :bg (get-hl-bg :StatusLine)}) (string.format "%%#%s# %s %%#StlModeSep#" hl-name (. info 1))))
(local buf-display-cache {})
(fn refresh-display-cache [] (let [bufs (vim.fn.getbufinfo {:buflisted 1})] (each [k _ (pairs buf-display-cache)] (tset buf-display-cache k nil)) (each [_ buf (ipairs bufs)] (let [name (vim.fn.fnamemodify buf.name ":t")] (when (not= name "") (var dup? false) (each [_ other (ipairs bufs)] (when (and (not dup?) (not= other.name buf.name) (= (vim.fn.fnamemodify other.name ":t") name)) (set dup? true))) (tset buf-display-cache buf.bufnr (if dup? (.. (vim.fn.fnamemodify buf.name ":h:t") "/" name) name)))))))
(refresh-display-cache)(vim.api.nvim_create_autocmd [:BufAdd :BufDelete :BufFilePost] {:callback refresh-display-cache})
(fn filename-component [] (let [display (. buf-display-cache (vim.api.nvim_get_current_buf))] (if display (.. "%#StlFile# " display " %m%r") "%#StlFile# [No Name] %m%r")))
(fn sep [] ;; Filler component "%#StlFill#%=")
(fn location [] (let [hl-name (. (current-mode-info) 2)] (vim.api.nvim_set_hl 0 :StlPosSep {:fg (get-hl-bg hl-name) :bg (get-hl-bg :StatusLine)}) (.. "%#StlPosSep#%#" hl-name "# %l:%c ")))
(macro get-diagnostic-count [severity] `(length (vim.diagnostic.get buf {:severity (. vim.diagnostic.severity ,severity)})))
(local diagnostic-icons (if icons-enabled ;; TODO: maybe define style sets, fill/outline, set those with condition? {:error " " :warn " " :info " " :hint " "} {:error "E:" :warn "W:" :info "I:" :hint "H:"}))
(macro diag-part [parts severity hl icon-key] `(let [n# (get-diagnostic-count ,severity)] (when (> n# 0) (table.insert ,parts (.. "%#" ,hl "#" (. diagnostic-icons ,icon-key) n#)))))
(fn diagnostics-component [] (let [buf 0 parts {}] (diag-part parts :ERROR :DiagnosticError :error) (diag-part parts :WARN :DiagnosticWarn :warn) (diag-part parts :INFO :DiagnosticInfo :info) (diag-part parts :HINT :DiagnosticHint :hint) (if (> (length parts) 0) (.. "%#StlDiag#" (table.concat parts " ") " ") "")))
(macro diff-part [parts s field hl prefix] `(when (and ,s (> (or (. ,s ,field) 0) 0)) (table.insert ,parts (.. "%#" ,hl "#" ,prefix (. ,s ,field)))))
(fn diff-component [] (if (= _G.MiniDiff nil) "" (let [buf-data (_G.MiniDiff.get_buf_data 0)] (if (not buf-data) "" (let [s buf-data.summary parts []] (diff-part parts s :add :Added "+") (diff-part parts s :change :Changed "~") (diff-part parts s :delete :Removed "-") (if (> (length parts) 0) (.. "%#StlDiff# " (table.concat parts " ") " ") ""))))))
(var jj-info "")
(fn update-jj-info [] (vim.system [:jj :log :-r "@" :--no-graph :--ignore-working-copy :-T "if(bookmarks, bookmarks.join(\", \"), change_id.short(8))"] {:text true} (fn [result] (if (= result.code 0) (set jj-info (vim.trim result.stdout)) (set jj-info "")))))
(vim.api.nvim_create_autocmd [:BufEnter :BufWritePost :FocusGained] {:callback update-jj-info})
(fn jj-component [] (if (= jj-info "") "" (.. "%#StlJJ# " jj-info " ")))
(fn _G.Statusline [] (table.concat [(mode-component) (filename-component) (sep) (diagnostics-component) (diff-component) (jj-component) (location)]))
(set vim.o.statusline "%!v:lua.Statusline()")