Skip to content

Commit

Permalink
feat: add set-local! macro (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
datwaft committed Jul 17, 2021
1 parent 40c9e34 commit cd59b41
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
12 changes: 7 additions & 5 deletions fnl/core/autocmd.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
; Restore cursor on exit
(augroup! restore-cursor-on-exit
(autocmd! VimLeave *
"set guicursor=a:ver100-blinkon0"))
#(set! guicursor ["a:ver100-blinkon0"])))

; Automatically resize splits when window is resized
(augroup! resize-splits-on-resize
Expand Down Expand Up @@ -32,7 +32,7 @@
; Disable spell in certain filetypes
(augroup! disable-spell-on-filetypes
(autocmd! FileType [help packer]
"setlocal nospell"))
#(set-local! nospell)))

; Set terminal options
(augroup! terminal-options
Expand All @@ -41,10 +41,12 @@
"startinsert")
; Disables line number on terminal buffers
(autocmd! TermOpen *
"setlocal nonumber norelativenumber")
#(do
(set-local! nonumber)
(set-local! norelativenumber)))
; Disables spell on terminal buffers
(autocmd! TermOpen *
"setlocal nospell")
#(set-local! nospell))
; Disables sign column on terminal buffers
(autocmd! TermOpen *
"setlocal signcolumn=\"no\""))
#(set-local! signcolumn :no)))
39 changes: 39 additions & 0 deletions fnl/core/macros.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
`(let [(ok?# value#) (pcall #(: (. vim.opt ,name) :get))]
(if ok?# value# nil))))

(fn get-local? [name]
(let [name (tostring name)
name (if (= (name:sub 1 2) "no") (name:sub 3) name)]
`(let [(ok?# value#) (pcall #(: (. vim.opt_local ,name) :get))]
(if ok?# value# nil))))

(fn set! [name value]
"Set vim option with vim.opt"
(let [name (tostring name)
Expand Down Expand Up @@ -45,6 +51,37 @@
"^" `(: (. vim.opt ,name-without-last-character) :prepend ,value)
_ `(tset vim.opt ,name ,value)))))

(fn set-local! [name value]
"Set vim option with vim.opt_local"
(let [name (tostring name)
value-is-nil? (nil? value)
name-begins-with-no? (= (name:sub 1 2) "no")
name (if (and value-is-nil? name-begins-with-no?)
(name:sub 3)
name)
value (if value-is-nil?
(not name-begins-with-no?)
value)
name-last-character (name:sub -1)
name-without-last-character (name:sub 1 -2)]
(if (and
(list? value)
(or
(= (tostring (head value)) :hashfn)
(= (tostring (head value)) :fn)))
(let [symbol (gensym-fn!)]
`(tset vim.opt_local ,name (do
(global ,(sym symbol) ,value)
,(.. "v:lua." symbol "()"))))
(match name-last-character
"?" `(get? ,name-without-last-character)
"!" `(tset vim.opt_local ,name-without-last-character
(not (get? ,name-without-last-character)))
"+" `(: (. vim.opt_local ,name-without-last-character) :append ,value)
"-" `(: (. vim.opt_local ,name-without-last-character) :remove ,value)
"^" `(: (. vim.opt_local ,name-without-last-character) :prepend ,value)
_ `(tset vim.opt_local ,name ,value)))))

(fn let! [name value]
"Set vim variable with vim.[g b w t]"
(let [name (tostring name)
Expand Down Expand Up @@ -80,7 +117,9 @@

{: gensym-fn!
: get?
: get-local?
: set!
: set-local!
: let!
: augroup!
: autocmd!}

0 comments on commit cd59b41

Please sign in to comment.