Skip to content

Commit

Permalink
Add iskeyword chars automatically from buffer's syntax table.
Browse files Browse the repository at this point in the history
  • Loading branch information
fukamachi committed Aug 19, 2023
1 parent 0666d2f commit c792671
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 30 deletions.
2 changes: 1 addition & 1 deletion extensions/vi-mode/lem-vi-mode.asd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"split-sequence")
:serial t
:components ((:file "core")
(:file "options")
(:file "word")
(:file "visual")
(:file "jump-motions")
Expand All @@ -17,7 +18,6 @@
:components
((:file "utils")))
(:file "commands")
(:file "options")
(:file "ex-core")
(:file "ex-parser")
(:file "ex-command")
Expand Down
83 changes: 54 additions & 29 deletions extensions/vi-mode/options.lisp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(defpackage :lem-vi-mode/options
(:use :cl
:lem
:split-sequence)
(:import-from :lem-vi-mode/core
:change-directory)
Expand All @@ -10,10 +11,10 @@
:register-groups-bind)
(:import-from :alexandria
:if-let
:when-let
:once-only
:with-gensyms
:disjoin
:mappend
:copy-hash-table)
(:export :define-vi-option
:get-option
Expand Down Expand Up @@ -41,6 +42,7 @@
(getter nil :type (or function null))
(setter nil :type (or function null))
(set-hook nil :type (or function null))
(initializer nil :type (or function null))
(documentation nil :type (or string null)))

(define-condition vi-option-error (simple-error) ())
Expand All @@ -54,12 +56,20 @@
(or (gethash name *option-aliases*)
name))

(defun new-buffer-options ()
(copy-hash-table *default-buffer-options*
:key (lambda (option)
(let ((new-option (copy-structure option)))
(when (vi-option-initializer new-option)
(funcall (vi-option-initializer new-option) new-option))
new-option))))

(defun get-buffer-options (&optional (buffer (lem:current-buffer)))
(or (gethash "vi-mode-options"
(lem-base::buffer-variables buffer))
(setf (gethash "vi-mode-options"
(lem-base::buffer-variables buffer))
(copy-hash-table *default-buffer-options*))))
(new-buffer-options))))

(defun get-option (name &optional (error-if-not-exists t))
(check-type name string)
Expand Down Expand Up @@ -223,32 +233,36 @@
(check-type scope (member :global :buffer))
(once-only (default scope)
(with-gensyms (option alias)
`(progn
(check-type ,default ,type)
(dolist (,alias ',aliases)
(setf (gethash ,alias *option-aliases*) ,name))
(let ((,option
(make-vi-option :name ,name
:%value ,default
:default ,default
:type ',type
:aliases ',aliases
:getter ,(when-let (getter-arg (find :getter others :key #'car))
`(lambda ,@(rest getter-arg)))
:setter ,(when-let (setter-arg (find :setter others :key #'car))
`(lambda ,@(rest setter-arg)))
:set-hook ,(when-let (set-hook-arg (find :set-hook others :key #'car))
`(lambda ,@(rest set-hook-arg)))
:documentation ,(when-let (doc-arg (find :documentation others :key #'car))
(second doc-arg)))))
(setf (gethash
,name
(ecase ,scope
(:global *global-options*)
(:buffer *default-buffer-options*)))
,option)
(setf (gethash ,name *option-scope*) ,scope)
',name)))))
(destructuring-bind (&key getter setter set-hook initializer documentation)
(mappend (lambda (other-arg)
(list (car other-arg) (cdr other-arg)))
others)
`(progn
(check-type ,default ,type)
(dolist (,alias ',aliases)
(setf (gethash ,alias *option-aliases*) ,name))
(let ((,option
(make-vi-option :name ,name
:%value ,default
:default ,default
:type ',type
:aliases ',aliases
:getter ,(and getter
`(lambda ,@getter))
:setter ,(and setter `(lambda ,@setter))
:set-hook ,(and set-hook `(lambda ,@set-hook))
:initializer ,(and initializer
`(lambda ,@initializer))
:documentation ,(and documentation
(first documentation)))))
(setf (gethash
,name
(ecase ,scope
(:global *global-options*)
(:buffer *default-buffer-options*)))
,option)
(setf (gethash ,name *option-scope*) ,scope)
',name))))))

(defun auto-change-directory (buffer-or-window)
(change-directory (etypecase buffer-or-window
Expand Down Expand Up @@ -328,4 +342,15 @@
(:setter (new-value option)
(setf (vi-option-%value option)
(cons new-value
(compile-iskeyword new-value)))))
(compile-iskeyword new-value))))
(:initializer (option)
(let ((syntax-table (lem:mode-syntax-table (lem:buffer-major-mode (lem:current-buffer)))))
(setf (vi-option-value option)
(delete-duplicates
(nconc (mapcar (lambda (c)
(if (char= c #\@)
"@-@"
(string c)))
(lem-base::syntax-table-symbol-chars syntax-table))
(vi-option-value option))
:test 'equal)))))

0 comments on commit c792671

Please sign in to comment.