(import-macros {: g! : exec : map! : augroup!} :hibiscus.vim)
(import-macros {: setup} :macros)

(g! :neo_tree_remove_legacy_commands 1)

(setup :neo-tree
  {:close_if_last_window true
   :window {:width 30
            :mappings {:<cr> "open_with_window_picker"
                       :S    "split_with_window_picker"
                       :s    "vsplit_with_window_picker"}}
   :filesystem {:filtered_items {:visible true}
                :follow_current_file true
                :window {:mappings {:/         "noop"
                                    :g/        "fuzzy_finder"
                                    :f         "noop"
                                    :ff        (fn [] (exec [[:Telescope "find_files"]]))
                                    :fg        (fn [] (exec [[:Telescope "live_grep"]]))
                                    :<CR>      "custom_open"
                                    :e         "open"
                                    :<S-Right> (fn [])
                                    :<S-Left>  (fn [])}}
                :commands {:custom_open (fn [state]
                                          (let [node        (state.tree:get_node)
                                                path        (node:get_id)
                                                quoted_path (.. "'" (.. path "'"))]
                                            (match (path:match "^.+%.(.+)$")
                                              "png" (let [bufnr          (vim.api.nvim_create_buf false true)
                                                          image          (: (require :hologram.image) :new path {})
                                                          max_width      (- vim.o.columns 4)
                                                          max_height     (- vim.o.lines 2)
                                                          tmp_width      (math.min image.cols max_width)
                                                          tmp_height     (* image.rows (/ tmp_width image.cols))
                                                          height         (math.min tmp_height max_height)
                                                          width          (* image.cols (/ height image.rows))
                                                          winnr          (vim.api.nvim_open_win bufnr true {:relative "editor"
                                                                                                            :width (math.ceil width)
                                                                                                            :height (+ (math.ceil height) 1)
                                                                                                            :row 0
                                                                                                            :col 2})]
                                                      (exec [[:setlocal "nonumber"]])
                                                      (vim.api.nvim_buf_attach bufnr false {:on_detach (fn [_ bufnr]
                                                                                                         (image:delete bufnr {:free true}))})
                                                      (image:display 1 0 bufnr {}))
                                              _     (do
                                                      (exec [[:silent
                                                              "!file -bL --mime" quoted_path
                                                              "| grep -qv '^text\\|^inode\\|^application/json'"]])
                                                      (if (= vim.v.shell_error 1)
                                                            ((. (require :neo-tree.sources.filesystem.commands) :open_with_window_picker) state)
                                                          (exec [[:silent
                                                                  "!xdg-open" quoted_path
                                                                  "&"]]))))))}}
   :renderers {:directory [[:indent]
                           [:icon]
                           [:current_filter]
                           [:name]
                           [:clipboard]
                           {1 :diagnostics :errors_only true}]
               :file [[:indent]
                      [:icon]
                      {1 :name :use_git_status_colors true :zindex 10}
                      [:clipboard]
                      [:bufnr]
                      [:modified]
                      [:diagnostics]
                      [:git_status]]}})

(map! [n] :f ":Neotree focus<CR>")
(map! [n] :F (fn [] (exec [[:Neotree "toggle"] [:Neotree "toggle" "action=show"]])))

(augroup! :neo-tree [[FileType] [qf] "set nobuflisted|call feedkeys(\"F\")"])

; Startup
(if (or (= (vim.fn.expand "%") "") (not= (vim.fn.isdirectory (vim.fn.expand "%")) 0))
      ((. (require :neo-tree.command) :execute) {:action "focus" :dir (vim.fn.getcwd)})
    ((. (require :neo-tree.command) :execute) {:action "show" :dir (vim.fn.getcwd)}))
