aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to '')
-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
5 files changed, 94 insertions, 148 deletions
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)