diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-10-26 01:12:36 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-10-26 01:12:36 +0530 |
| commit | a699f41c228b573ff980927d6807ea2b538c0be0 (patch) | |
| tree | 9690227ac84f8b54b52ab28750b497ba219aa133 /scripts/marks.fnl | |
| parent | 5d823bbd19dc8589cfacdbe51ee5260042dacab2 (diff) | |
| download | kakoune-config-a699f41c228b573ff980927d6807ea2b538c0be0.tar.gz kakoune-config-a699f41c228b573ff980927d6807ea2b538c0be0.zip | |
Move marks into fnl script
Diffstat (limited to '')
| -rwxr-xr-x | scripts/marks.fnl | 73 |
1 files changed, 73 insertions, 0 deletions
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) |
