aboutsummaryrefslogtreecommitdiff
path: root/scripts/grep-write.clj
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2026-06-23 00:50:21 +0530
committerAkshay Nair <phenax5@gmail.com>2026-06-23 00:50:21 +0530
commit7502d1e84ae9956356e1578f65d11a8326ce3ec0 (patch)
treed5f9140d2e4ffcd57df971b2b27dc655b7a6294c /scripts/grep-write.clj
parente254ac67272707644be53aa1eb296917651a1771 (diff)
downloadkakoune-config-7502d1e84ae9956356e1578f65d11a8326ce3ec0.tar.gz
kakoune-config-7502d1e84ae9956356e1578f65d11a8326ce3ec0.zip
Refactor fennel scripts to babashka clojure
Diffstat (limited to 'scripts/grep-write.clj')
-rwxr-xr-xscripts/grep-write.clj30
1 files changed, 30 insertions, 0 deletions
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")))