-
Notifications
You must be signed in to change notification settings - Fork 8
/
nushell-mode.el
150 lines (119 loc) · 4.1 KB
/
nushell-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
;;; nushell-mode.el --- Major mode for Nushell scripts -*- lexical-binding: t; -*-
;; Copyright (C) 2023 Mark Karpov
;; Copyright (C) 2022 Azzam S.A
;; Author: Azzam S.A <vcs@azzamsa.com>
;; Homepage: https://github.com/mrkkrp/nushell-mode
;; Keywords: languages, unix
;; Package-Version: 0.1.0
;; Package-Requires: ((emacs "24.4"))
;; SPDX-License-Identifier: GPL-3.0-or-later
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; A very basic version of major mode for Nushell shell scripts.
;;; Code:
(require 'cl-lib)
(eval-when-compile
(require 'subr-x))
(defgroup nushell nil
"Nushell shell support."
:group 'languages
:tag "Nushell"
:prefix "nushell-"
:link '(url-link :tag "GitHub" "https://github.com/mrkkrp/nushell-mode"))
(defcustom nushell-indent-offset 4
"Default indentation offset for Nushell."
:group 'nushell
:type 'integer
:safe 'integerp)
(defvar nushell-enable-auto-indent nil
"Controls auto-indent feature.
If the value of this variable is non-nil, whenever a word in
`nushell-auto-indent-trigger-keywords' is typed, it is indented instantly.")
(defconst nushell-builtins
'("bit-and"
"bit-or"
"bit-shl"
"bit-shr"
"bit-xor"
"ends-with"
"false"
"fdiv"
"mod"
"not-in"
"null"
"starts-with"
"true"
"xor")
"Nushell built-ins.")
(defconst nushell-keywords
'("alias"
"and"
"def"
"def-env"
"else"
"export"
"extern"
"for"
"if"
"let"
"let-env"
"mut"
"not"
"or"
"while")
"Nushell keywords.
It is difficult to tell whether a certain word should be
considered a built-in or a keyword in Nushell. We follow the
principle that keywords define structure and organization of
program to certain extent and thus deserve special highlighting
in order to aid reading. Highlighting every built-in command as
a keyword would result in visual noise.")
(defface nushell-pay-attention-face
'((t :inverse-video t :inherit font-lock-preprocessor-face))
"Face used to display elements that should attract attention.")
(defconst nushell-font-lock-keywords
`(
;; Builtins
(,(regexp-opt nushell-builtins 'symbols) . font-lock-builtin-face)
;; Keywords
(,(regexp-opt nushell-keywords 'symbols) . font-lock-keyword-face)
;; Variables
("\\$[[:word:]_\\.-]+" . font-lock-variable-name-face)
;; String interpolation (beginning of string)
("\\$" . 'nushell-pay-attention-face)
;; Redirections
("\\(out\\|err\\|out\\+err\\|err\\+out\\)>" . 'nushell-pay-attention-face)
;; Using system binaries instead of Nushell built-ins
("\\(\\^\\)\\(?:[[:word:]_-]+\\)" 1 'nushell-pay-attention-face)))
(defvar nushell-mode-syntax-table
(let ((table (make-syntax-table text-mode-syntax-table)))
(modify-syntax-entry ?\# "<" table)
(modify-syntax-entry ?\n ">" table)
(modify-syntax-entry ?\" "\"" table)
(modify-syntax-entry ?\' "\"'" table)
(modify-syntax-entry ?\\ "\\" table)
(modify-syntax-entry ?$ "'" table)
table)
"Syntax table for `nushell-mode'.")
;;;###autoload
(define-derived-mode nushell-mode prog-mode "Nushell"
"Major mode for editing nushell shell files."
:syntax-table nushell-mode-syntax-table
(setq-local font-lock-defaults '(nushell-font-lock-keywords))
(setq-local comment-start "# ")
(setq-local comment-start-skip "#+[\t ]*"))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.nu\\'" . nushell-mode))
;;;###autoload
(add-to-list 'interpreter-mode-alist '("nu" . nushell-mode))
(provide 'nushell-mode)
;;; nushell-mode.el ends here