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