Skip to content

Commit

Permalink
Merge pull request #1587 from lem-project/nix-mode
Browse files Browse the repository at this point in the history
add nix-mode
  • Loading branch information
cxxxr authored Nov 4, 2024
2 parents 89c64fe + 66180df commit 2ea5519
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
22 changes: 22 additions & 0 deletions extensions/nix-mode/indent.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(defpackage :lem-nix-mode/indent
(:use :cl
:lem
:lem/language-mode)
(:export :beginning-of-defun
:end-of-defun
:calc-indent))
(in-package :lem-nix-mode/indent)

(defun beginning-of-defun (point n)
(loop :repeat n :do (search-backward-regexp point "^\\{")))

(defun end-of-defun (point n)
(if (minusp n)
(beginning-of-defun point (- n))
(search-forward-regexp point "^\\}")))

(defun calc-indent (point)
(with-point ((point point))
(let ((tab-width (variable-value 'tab-width :default point))
(column (point-column point)))
(+ column (- tab-width (rem column tab-width))))))
5 changes: 5 additions & 0 deletions extensions/nix-mode/lem-nix-mode.asd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(defsystem "lem-nix-mode"
:depends-on ("lem")
:serial t
:components ((:file "indent")
(:file "nix-mode")))
59 changes: 59 additions & 0 deletions extensions/nix-mode/nix-mode.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(defpackage :lem-nix-mode
(:use :cl
:lem
:lem/language-mode
:lem/language-mode-tools)
(:local-nicknames (:indent :lem-nix-mode/indent))
(:export :nix-mode))
(in-package :lem-nix-mode)

(defun tokens (boundary strings)
(let ((alternation
`(:alternation ,@(sort (copy-list strings) #'> :key #'length))))
(if boundary
`(:sequence ,boundary ,alternation ,boundary)
alternation)))

(defun make-tmlanguage-nix ()
(let ((patterns (make-tm-patterns
(make-tm-region '(:sequence "#") "$" :name 'syntax-comment-attribute)
(make-tm-region '(:sequence "''")
'(:sequence "''")
:name 'syntax-string-attribute)
(make-tm-match (tokens :word-boundary
'("true"
"false"
"null"))
:name 'syntax-keyword-attribute)
(make-tm-string-region "''")
(make-tm-string-region "\""))))
(make-tmlanguage :patterns patterns)))

(defvar *syntax-table*
(let ((table (make-syntax-table
:space-chars '(#\space #\tab #\newline)
:symbol-chars '(#\_)
:paren-pairs '((#\( . #\))
(#\{ . #\})
(#\[ . #\]))
:string-quote-chars '(#\")
:block-string-pairs '(("''" . "''"))
:line-comment-string "#")))
(set-syntax-parser table (make-tmlanguage-nix))
table))

(define-major-mode nix-mode language-mode
(:name "Nix"
:keymap *nix-mode-keymap*
:syntax-table *syntax-table*
:mode-hook *nix-mode-hook*)
(setf (variable-value 'enable-syntax-highlight) t
(variable-value 'indent-tabs-mode) nil
(variable-value 'tab-width) 2
(variable-value 'calc-indent-function) 'indent:calc-indent
(variable-value 'line-comment) "#"
(variable-value 'beginning-of-defun-function) 'indent:beginning-of-defun
(variable-value 'end-of-defun-function) 'indent:end-of-defun
))

(define-file-type ("nix") nix-mode)
1 change: 1 addition & 0 deletions lem.asd
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@
"lem-documentation-mode"
"lem-elisp-mode"
"lem-terraform-mode"
"lem-nix-mode"
"lem-markdown-mode"
"lem-color-preview"
"lem-lua-mode"
Expand Down

0 comments on commit 2ea5519

Please sign in to comment.