1;;; ninja-mode.el --- Major mode for editing .ninja files -*- lexical-binding: t -*- 2 3;; Package-Requires: ((emacs "24")) 4 5;; Copyright 2011 Google Inc. All Rights Reserved. 6;; 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 10;; 11;; http://www.apache.org/licenses/LICENSE-2.0 12;; 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. 18 19;;; Commentary: 20 21;; Simple emacs mode for editing .ninja files. 22 23;;; Code: 24 25(defcustom ninja-indent-offset 2 26 "*Amount of offset per level of indentation." 27 :type 'integer 28 :safe 'natnump 29 :group 'ninja) 30 31(defconst ninja-keywords-re 32 (concat "^" (regexp-opt '("rule" "build" "subninja" "include" "pool" "default") 33 'words))) 34 35(defvar ninja-keywords 36 `((,ninja-keywords-re . font-lock-keyword-face) 37 ("^[[:space:]]*\\([[:alnum:]_]+\\)[[:space:]]*=" 1 font-lock-variable-name-face) 38 ;; Variable expansion. 39 ("$[[:alnum:]_]+" . font-lock-variable-name-face) 40 ("${[[:alnum:]._]+}" . font-lock-variable-name-face) 41 ;; Rule names 42 ("rule +\\([[:alnum:]_.-]+\\)" 1 font-lock-function-name-face) 43 ;; Build Statement - highlight the rule used, 44 ;; allow for escaped $,: in outputs. 45 ("build +\\(?:[^:$\n]\\|$[:$]\\)+ *: *\\([[:alnum:]_.-]+\\)" 46 1 font-lock-function-name-face))) 47 48(defvar ninja-mode-syntax-table 49 (let ((table (make-syntax-table))) 50 (modify-syntax-entry ?\" "." table) 51 table) 52 "Syntax table used in `ninja-mode'.") 53 54(defun ninja-syntax-propertize (start end) 55 (save-match-data 56 (goto-char start) 57 (while (search-forward "#" end t) 58 (let ((match-pos (match-beginning 0))) 59 (when (and 60 ;; Is it the first non-white character on the line? 61 (eq match-pos (save-excursion (back-to-indentation) (point))) 62 (save-excursion 63 (goto-char (line-end-position 0)) 64 (or 65 ;; If we're continuing the previous line, it's not a 66 ;; comment. 67 (not (eq ?$ (char-before))) 68 ;; Except if the previous line is a comment as well, as the 69 ;; continuation dollar is ignored then. 70 (nth 4 (syntax-ppss))))) 71 (put-text-property match-pos (1+ match-pos) 'syntax-table '(11)) 72 (let ((line-end (line-end-position))) 73 ;; Avoid putting properties past the end of the buffer. 74 ;; Otherwise we get an `args-out-of-range' error. 75 (unless (= line-end (1+ (buffer-size))) 76 (put-text-property line-end (1+ line-end) 'syntax-table '(12))))))))) 77 78(defun ninja-compute-indentation () 79 "Calculate indentation for the current line." 80 (save-excursion 81 (beginning-of-line) 82 (if (or (looking-at ninja-keywords-re) 83 (= (line-number-at-pos) 1)) 84 0 85 (forward-line -1) 86 (if (looking-at ninja-keywords-re) 87 ninja-indent-offset 88 (current-indentation))))) 89 90(defun ninja-indent-line () 91 "Indent the current line. Uses previous indentation level if 92 available or `ninja-indent-offset'" 93 (interactive "*") 94 (indent-line-to (ninja-compute-indentation))) 95 96;;;###autoload 97(define-derived-mode ninja-mode prog-mode "ninja" 98 (set (make-local-variable 'comment-start) "#") 99 (set (make-local-variable 'parse-sexp-lookup-properties) t) 100 (set (make-local-variable 'syntax-propertize-function) #'ninja-syntax-propertize) 101 (set (make-local-variable 'indent-line-function) 'ninja-indent-line) 102 (setq font-lock-defaults '(ninja-keywords))) 103 104;; Run ninja-mode for files ending in .ninja. 105;;;###autoload 106(add-to-list 'auto-mode-alist '("\\.ninja$" . ninja-mode)) 107 108(provide 'ninja-mode) 109 110;;; ninja-mode.el ends here 111