From 271319297d56891e4db849c84fa330e107d1e981 Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Sat, 31 Dec 2022 18:18:49 -0800 Subject: [PATCH] A bunch of changes --- fnl/macros.fnl | 30 +++++++++++++++++------------- init.fnl | 8 +++++--- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/fnl/macros.fnl b/fnl/macros.fnl index fdf3a2a..f634bb1 100644 --- a/fnl/macros.fnl +++ b/fnl/macros.fnl @@ -33,15 +33,8 @@ :pattern ,pattern_table :callback ,callback }))) ;; Nicer macro for use -;; (lambda use! [repo ...] -;; (local output [ repo ]) -;; (var last_key nil) -;; (each [_ val (ipairs [...])] -;; (if last_key -;; (do (tset output last_key val) -;; (set last_key nil)) -;; (set last_key val))) -;; `(use ,output)) +;; :config acts like use-package's :init and :config options, +;; taking in the following lisp expressions as its arguments (lambda use! [repo ...] (local output [ repo ]) (var last_key nil) @@ -64,8 +57,7 @@ (tset output :config config_entries)) `(use ,output)) -;; Can be passed to :config of `use!' -;; to call a plugin's `setup' function +;; Call a plugin's `setup function' (lambda setup! [pkg ...] (local output [ ]) (var last_key nil) (each [_ val (ipairs [...])] @@ -79,13 +71,25 @@ (lambda module-call! [mod func ...] `((. (require ,mod) ,func) ,...)) -;; Return module function +;; Return module function (lambda module-fn! [mod func] `(. (require ,mod) ,func)) +;; Basically does what the Emacs function of the same name does. +;; Wraps the code inside with a `winsaveview' and `winrestview' +(lambda save-excursion! [...] + (let [args [...] + len (# args)] + (tset args len `(let [retvar# ,(. args len)] + (vim.fn.winrestview save#) + retvar#)) + `(let [save# (vim.fn.winsaveview)] + ,(unpack args)))) + {: bind! : hook! : use! : setup! : module-call! - : module-fn!} + : module-fn! + : save-excursion!} diff --git a/init.fnl b/init.fnl index 2e07c1b..78dc6ad 100644 --- a/init.fnl +++ b/init.fnl @@ -1,6 +1,7 @@ ;;; init.fnl - primary init file -(import-macros {: bind! : module-call! : hook!} :macros) +(import-macros {: bind! : module-call! : hook! + : save-excursion! } :macros) ;; Make space leader (set vim.g.mapleader " ") @@ -61,8 +62,9 @@ ;; Remove trailing whitespace (fn remove-trailing-whitespace [] "Remove trailing whitespace from the whole file." - (pcall vim.cmd "%s/ \\+$//") - (vim.cmd.nohlsearch)) + (save-excursion! + (vim.api.nvim_exec "keepjumps keeppatterns silent! %s/ \\+$//" false) + (vim.api.nvim_exec "keepjumps keeppatterns silent! 0;/^\\%(\\n*.\\)\\@!/,$d_" false))) (vim.api.nvim_create_user_command :RemoveTrailingWhitespace remove-trailing-whitespace {}) (hook! [ :BufWritePre ] "*" remove-trailing-whitespace)