aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--README.md4
-rw-r--r--autoload/grep.kak11
-rw-r--r--autoload/marks.kak14
-rwxr-xr-xscripts/apply_vimgrep_updates.fnl43
-rw-r--r--scripts/fnl/lib.fnl32
-rwxr-xr-xscripts/grep-write.clj30
-rwxr-xr-xscripts/marks.clj64
-rwxr-xr-xscripts/marks.fnl73
9 files changed, 111 insertions, 162 deletions
diff --git a/.gitignore b/.gitignore
index 3f1b426..9c1a31e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,3 @@
dummy/
+.lsp/
+.clj-kondo/
diff --git a/README.md b/README.md
index 1d2b8ee..51f198e 100644
--- a/README.md
+++ b/README.md
@@ -7,9 +7,9 @@ My kakoune configuration
- [gitu](https://github.com/altsem/gitu): Git ui
- [fd](https://github.com/sharkdp/fd): Find files
- [rg](https://github.com/BurntSushi/ripgrep): Grep command
-- [./scripts/apply_vimgrep_updates.fnl](./scripts/apply_vimgrep_updates.fnl): Apply changes made to grep buffer
+- [./scripts/grep-write.clj](./scripts/grep-write.clj): Apply changes made to grep buffer
- [kakoune-lsp](https://github.com/kakoune-lsp/kakoune-lsp): LSP client
- [kak-tree-sitter](https://git.sr.ht/~hadronized/kak-tree-sitter): Treesitter syntax highlighting
- [kakoune.cr](https://github.com/alexherbo2/kakoune.cr): Connecting with kakoune session
-- [./scripts/marks.fnl](./scripts/marks.fnl): Marking files root per-directory
+- [./scripts/marks.clj](./scripts/marks.clj): Marking files root per-directory
diff --git a/autoload/grep.kak b/autoload/grep.kak
index c753ab2..5da657f 100644
--- a/autoload/grep.kak
+++ b/autoload/grep.kak
@@ -4,17 +4,18 @@ map global file g ':grep ''''<left>' -docstring 'Grep'
map global file * ': grep --case-sensitive %val{selection}<ret>' -docstring 'Grep selection'
define-command grep-write %{
- execute-keys '%' # Consider selecting manually
- evaluate-commands %sh{
- echo "$kak_selections" | "$kak_config/scripts/apply_vimgrep_updates.fnl" | xargs -i echo "info '{}'"
+ evaluate-commands -draft %{
+ execute-keys '%'
+ set-register c %sh{ echo "$kak_selections" | "$kak_config/scripts/grep-write.clj" }
}
+ info %val{reg_c}
}
-hook global -always BufOpenFifo '\*grep\*' %{
+hook global BufOpenFifo '\*grep\*' %{
map global file [ ': grep-previous-match<ret>'
map global file ] ': grep-next-match<ret>'
}
-hook global -always BufOpenFifo '\*make\*' %{
+hook global BufOpenFifo '\*make\*' %{
map global file [ ': make-previous-error<ret>'
map global file ] ': make-next-error<ret>'
}
diff --git a/autoload/marks.kak b/autoload/marks.kak
index 92f45a2..1621308 100644
--- a/autoload/marks.kak
+++ b/autoload/marks.kak
@@ -1,31 +1,31 @@
define-command marks-select -params 1 %{
evaluate-commands %sh{
- mark=$("$kak_config/scripts/marks.fnl" get "${1:-0}")
+ mark=$("$kak_config/scripts/marks.clj" get "${1:-0}")
[ -z "$mark" ] || echo "edit $mark"
}
marks-show
}
define-command marks-add -params 1..2 %{
- nop %sh{ "$kak_config/scripts/marks.fnl" add "$1" "$2" }
+ nop %sh{ "$kak_config/scripts/marks.clj" add "$1" "$2" }
marks-show
}
define-command marks-delete -params 1 %{
- nop %sh{ "$kak_config/scripts/marks.fnl" delete "$1" }
+ nop %sh{ "$kak_config/scripts/marks.clj" delete "$1" }
delete-buffer %arg{1}
marks-show
}
define-command marks-clear %{
- nop %sh{ "$kak_config/scripts/marks.fnl" clear }
+ nop %sh{ "$kak_config/scripts/marks.clj" clear }
}
define-command marks-show %{
info -title 'marks' -markup %sh{
path="$kak_opt_marks_path/$kak_opt_marks_name"
echo -n "{Default}"
- marks=$("$kak_config/scripts/marks.fnl" show)
+ marks=$("$kak_config/scripts/marks.clj" show)
if [ -z "$marks" ]; then
echo "{comment}<empty>" && exit 0;
fi
@@ -38,14 +38,14 @@ define-command marks-show %{
}
define-command marks-edit-all %{
- edit %sh{ "$kak_config/scripts/marks.fnl" show-path }
+ edit %sh{ "$kak_config/scripts/marks.clj" show-path }
}
define-command marks-add-all-buffers %{
eval %sh{
echo "$kak_quoted_buflist" | xargs -n1 | while IFS= read file; do
if [ -f "$file" ]; then
- "$kak_config/scripts/marks.fnl" add "$(realpath "$file")"
+ "$kak_config/scripts/marks.clj" add "$(realpath "$file")"
fi
done
}
diff --git a/scripts/apply_vimgrep_updates.fnl b/scripts/apply_vimgrep_updates.fnl
deleted file mode 100755
index 5fada6c..0000000
--- a/scripts/apply_vimgrep_updates.fnl
+++ /dev/null
@@ -1,43 +0,0 @@
-#!/usr/bin/env -S fennel --lua luajit
-
-;; TODO: Support -A -B outputs for context
-
-(local M {})
-
-(fn M.main []
- (local write-count (M.apply-vimgrep-changes))
- (print (.. "Applied " write-count " changes")))
-
-(fn M.apply-vimgrep-changes []
- (var count 0)
- (each [line (io.lines)]
- (local (filepath linenr _ text) (M.parse-vimgrep line))
- (when filepath
- (local applied? (M.update-line filepath linenr text))
- (when applied?
- (set count (+ count 1)))))
- count)
-
-(fn M.parse-vimgrep [line]
- (local (filepath linenr col text) (string.match line "(.*):(%d+):(%d+):(.*)"))
- (values filepath (tonumber linenr) col text))
-
-(fn M.update-line [filepath linenr text]
- (local lines (M.read-lines filepath))
- (var written? false)
- (when (and (> (length lines) linenr) (not= (. lines linenr) text))
- (set (. lines linenr) text)
- (local file (io.open filepath :w))
- (file:write (.. (table.concat lines "\n") "\n"))
- (file:close)
- (set written? true))
- written?)
-
-(fn M.read-lines [filepath]
- (local file (io.open filepath :r))
- (local lines [])
- (each [line (file:lines)] (table.insert lines line))
- (file:close)
- lines)
-
-(M.main)
diff --git a/scripts/fnl/lib.fnl b/scripts/fnl/lib.fnl
deleted file mode 100644
index 100b447..0000000
--- a/scripts/fnl/lib.fnl
+++ /dev/null
@@ -1,32 +0,0 @@
-(local M {})
-
-(lambda M.contains? [tbl elem]
- (not= nil (table.index-of tbl elem)))
-
-(lambda M.index-of [tbl elem]
- (each [idx value (pairs tbl)]
- (when (= value elem) (lua "return idx")))
- nil)
-
-(lambda M.exec [cmd args]
- (var argstr "")
- (each [_ arg (ipairs args)]
- (set argstr (.. argstr " \"" (string.gsub arg "\"" "\\\"") "\"")))
- (local code (os.execute (.. cmd argstr))))
-
-(lambda M.read-lines [filepath]
- (local file (io.open filepath :r))
- (if file
- (do
- (local lines [])
- (each [line (file:lines)] (table.insert lines line))
- (file:close)
- lines)
- []))
-
-(lambda M.write-lines [filepath lines]
- (local file (io.open filepath :w))
- (file:write (table.concat lines "\n"))
- (file:close))
-
-M
diff --git a/scripts/grep-write.clj b/scripts/grep-write.clj
new file mode 100755
index 0000000..e730dae
--- /dev/null
+++ b/scripts/grep-write.clj
@@ -0,0 +1,30 @@
+#!/usr/bin/env bb
+
+(require '[clojure.string :as str])
+
+(defn read-file-lines [filepath]
+ (-> filepath slurp (str/split #"\r?\n" -1)))
+
+(defn parse-vimgrep [line]
+ (when-let [[_ filepath linenr col text] (re-matches #"(.*):(\d+):(\d+):(.*)" line)]
+ [filepath (parse-long linenr) col text]))
+
+(defn update-line [filepath linenr text]
+ (let [lines (read-file-lines filepath)
+ line-changed? #(not= (nth lines (dec linenr)) text)]
+ (when (and (> (count lines) linenr) (line-changed?))
+ (let [new-lines (assoc lines (dec linenr) text)]
+ (spit filepath (str/join "\n" new-lines))
+ true))))
+
+(defn apply-vimgrep-change [vimgrep-line]
+ (when-let [[filepath linenr _ text] (parse-vimgrep vimgrep-line)]
+ (update-line filepath linenr text)))
+
+(defn grep-write []
+ (->> (line-seq (java.io.BufferedReader. *in*))
+ (keep apply-vimgrep-change)
+ count))
+
+(let [change-count (grep-write)]
+ (println (str "Applied " change-count " changes")))
diff --git a/scripts/marks.clj b/scripts/marks.clj
new file mode 100755
index 0000000..49f7cb8
--- /dev/null
+++ b/scripts/marks.clj
@@ -0,0 +1,64 @@
+#!/usr/bin/env bb
+
+(require '[clojure.string :as str])
+(require '[clojure.java.io :as io])
+
+(def xdg-data-home (or (System/getenv "XDG_DATA_HOME")
+ (str (System/getenv "HOME") "/.local/share")))
+(def marks-root-dir (str xdg-data-home "/kak/marks"))
+(def path-key (str/replace (System/getenv "PWD") #"[^A-Za-z0-9._-]" "-"))
+(def marks-file-path (str marks-root-dir "/" path-key))
+
+(defn get-marks []
+ (let [file (io/file marks-file-path)]
+ (if (.exists file)
+ (str/split-lines (str/trim (slurp file)))
+ [])))
+
+(defn set-marks [marks]
+ (.mkdirs (io/file marks-root-dir))
+ (spit marks-file-path (str/join "\n" marks)))
+
+(defn vec-push-at-pos [vector index new-value]
+ (vec (concat (subvec vector 0 index) [new-value] (subvec vector index))))
+
+(defn cmd-add [[new-mark posstr]]
+ (let [pos (some-> posstr parse-long)
+ marks (vec (remove #(= % new-mark) (get-marks)))
+ pos-valid? (or (not (pos-int? pos)) (> pos (count marks)))]
+ (set-marks (if pos-valid?
+ (conj marks new-mark)
+ (vec-push-at-pos marks (dec pos) new-mark)))))
+
+(defn cmd-get [[posstr]]
+ (let [pos (some-> posstr parse-long)
+ marks (get-marks)]
+ (when-let [mark (and pos (nth marks (dec pos) nil))]
+ (println mark))))
+
+(defn cmd-delete [[mark]]
+ (->> (get-marks) (remove #(= % mark)) set-marks))
+
+(defn cmd-clear [_]
+ (set-marks []))
+
+(defn cmd-show [_]
+ (->> (get-marks) (str/join "\n") println))
+
+(defn cmd-show-path [_]
+ (println marks-file-path))
+
+(def commands
+ {"add" cmd-add
+ "get" cmd-get
+ "delete" cmd-delete
+ "clear" cmd-clear
+ "show" cmd-show
+ "show-path" cmd-show-path})
+
+(let [[cmd & args] *command-line-args*]
+ (if-let [command-fn (commands cmd)]
+ (command-fn args)
+ (binding [*out* *err*]
+ (println (str "invalid command: " (or cmd "")))
+ (println (str "Valid commands: " (keys commands))))))
diff --git a/scripts/marks.fnl b/scripts/marks.fnl
deleted file mode 100755
index d0ad3db..0000000
--- a/scripts/marks.fnl
+++ /dev/null
@@ -1,73 +0,0 @@
-#!/usr/bin/env -S fennel --lua luajit
-
-(lambda fnl-require [path]
- (local fnl (require :fennel))
- (local scriptpath (string.gsub (. arg 0) "[^/]*$" ""))
- (fnl.dofile (.. scriptpath "/" path)))
-
-(local {: index-of : exec : read-lines : write-lines}
- (fnl-require :fnl/lib.fnl))
-
-(local xdg_data_home
- (do
- (local path (os.getenv :XDG_DATA_HOME))
- (or path (.. (os.getenv :HOME) :/.local/share))))
-
-(local marks_path (.. xdg_data_home :/kak/marks))
-
-(local command {})
-(local M {})
-
-(lambda command.add [[new-mark ?posstr]]
- (local pos (and ?posstr (tonumber ?posstr)))
- (local markpaths (M.get-marks))
- (local existing-idx (index-of markpaths new-mark))
- (when (not= nil existing-idx)
- (table.remove markpaths existing-idx))
- (if (or (= pos nil) (= pos 0))
- (table.insert markpaths new-mark)
- (table.insert markpaths pos new-mark))
- (M.set-marks markpaths))
-
-(lambda command.get [[posstr]]
- (local pos (and posstr (tonumber posstr)))
- (local markpaths (M.get-marks))
- (local existing-idx (. markpaths pos))
- (when (not= nil existing-idx) (print existing-idx)))
-
-(lambda command.delete [[mark]]
- (local markpaths (M.get-marks))
- (local existing-idx (index-of markpaths mark))
- (when (not= nil existing-idx)
- (table.remove markpaths existing-idx))
- (M.set-marks markpaths))
-
-(fn command.clear [] (M.set-marks []))
-
-(fn command.show []
- (print (table.concat (M.get-marks) "\n")))
-
-(fn command.show-path []
- (print (.. marks_path "/" (M.path-key))))
-
-;; -----
-
-(fn M.path-key []
- (string.gsub (os.getenv :PWD) "[^A-Za-z0-9._-]" "-"))
-
-(fn M.get-marks [?key]
- (local path (.. marks_path "/" (or ?key (M.path-key))))
- (read-lines path))
-
-(lambda M.set-marks [marks ?key]
- (exec :mkdir [:-p marks_path]) ; Create marks path dir if not exists
- (local path (.. marks_path "/" (or ?key (M.path-key))))
- (write-lines path marks))
-
-(fn M.main []
- (local [cmd & cmdargs] arg)
- (if (and cmd (. command cmd))
- ((. command cmd) cmdargs)
- (error (.. "invalid command: " (or cmd "")))))
-
-(M.main)