aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-10-26 01:12:36 +0530
committerAkshay Nair <phenax5@gmail.com>2025-10-26 01:12:36 +0530
commita699f41c228b573ff980927d6807ea2b538c0be0 (patch)
tree9690227ac84f8b54b52ab28750b497ba219aa133 /scripts
parent5d823bbd19dc8589cfacdbe51ee5260042dacab2 (diff)
downloadkakoune-config-a699f41c228b573ff980927d6807ea2b538c0be0.tar.gz
kakoune-config-a699f41c228b573ff980927d6807ea2b538c0be0.zip
Move marks into fnl script
Diffstat (limited to '')
-rw-r--r--scripts/fnl/lib.fnl32
-rwxr-xr-xscripts/marks.fnl73
2 files changed, 105 insertions, 0 deletions
diff --git a/scripts/fnl/lib.fnl b/scripts/fnl/lib.fnl
new file mode 100644
index 0000000..100b447
--- /dev/null
+++ b/scripts/fnl/lib.fnl
@@ -0,0 +1,32 @@
+(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/marks.fnl b/scripts/marks.fnl
new file mode 100755
index 0000000..d0ad3db
--- /dev/null
+++ b/scripts/marks.fnl
@@ -0,0 +1,73 @@
+#!/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)