1 ;;; ninja-mode.el --- Major mode for editing .ninja files -*- lexical-binding: t -*-
3 ;; Package-Requires: ((emacs "24"))
5 ;; Copyright 2011 Google Inc. All Rights Reserved.
7 ;; Licensed under the Apache License, Version 2.0 (the "License");
8 ;; you may not use this file except in compliance with the License.
9 ;; You may obtain a copy of the License at
11 ;; http://www.apache.org/licenses/LICENSE-2.0
13 ;; Unless required by applicable law or agreed to in writing, software
14 ;; distributed under the License is distributed on an "AS IS" BASIS,
15 ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ;; See the License for the specific language governing permissions and
17 ;; limitations under the License.
21 ;; Simple emacs mode for editing .ninja files.
22 ;; Just some syntax highlighting for now.
26 (defvar ninja-keywords
27 `((,(concat "^" (regexp-opt '("rule" "build" "subninja" "include"
30 . font-lock-keyword-face)
31 ("\\([[:alnum:]_]+\\) =" 1 font-lock-variable-name-face)
32 ;; Variable expansion.
33 ("$[[:alnum:]_]+" . font-lock-variable-name-face)
34 ("${[[:alnum:]._]+}" . font-lock-variable-name-face)
36 ("rule +\\([[:alnum:]_.-]+\\)" 1 font-lock-function-name-face)
37 ;; Build Statement - highlight the rule used,
38 ;; allow for escaped $,: in outputs.
39 ("build +\\(?:[^:$\n]\\|$[:$]\\)+ *: *\\([[:alnum:]_.-]+\\)"
40 1 font-lock-function-name-face)))
42 (defvar ninja-mode-syntax-table
43 (let ((table (make-syntax-table)))
44 (modify-syntax-entry ?\" "." table)
46 "Syntax table used in `ninja-mode'.")
48 (defun ninja-syntax-propertize (start end)
51 (while (search-forward "#" end t)
52 (let ((match-pos (match-beginning 0)))
54 ;; Is it the first non-white character on the line?
55 (eq match-pos (save-excursion (back-to-indentation) (point)))
57 (goto-char (line-end-position 0))
59 ;; If we're continuting the previous line, it's not a
61 (not (eq ?$ (char-before)))
62 ;; Except if the previous line is a comment as well, as the
63 ;; continuation dollar is ignored then.
64 (nth 4 (syntax-ppss)))))
65 (put-text-property match-pos (1+ match-pos) 'syntax-table '(11))
66 (let ((line-end (line-end-position)))
67 ;; Avoid putting properties past the end of the buffer.
68 ;; Otherwise we get an `args-out-of-range' error.
69 (unless (= line-end (1+ (buffer-size)))
70 (put-text-property line-end (1+ line-end) 'syntax-table '(12)))))))))
73 (define-derived-mode ninja-mode prog-mode "ninja"
74 (set (make-local-variable 'comment-start) "#")
75 (set (make-local-variable 'parse-sexp-lookup-properties) t)
76 (set (make-local-variable 'syntax-propertize-function) #'ninja-syntax-propertize)
77 (setq font-lock-defaults '(ninja-keywords)))
79 ;; Run ninja-mode for files ending in .ninja.
81 (add-to-list 'auto-mode-alist '("\\.ninja$" . ninja-mode))
85 ;;; ninja-mode.el ends here