-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1587 from lem-project/nix-mode
add nix-mode
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters