browse.py: allow port override via environment variable
[platform/upstream/ninja.git] / misc / ninja-mode.el
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 ;; Just some syntax highlighting for now.
23
24 ;;; Code:
25
26 (defvar ninja-keywords
27   `((,(concat "^" (regexp-opt '("rule" "build" "subninja" "include"
28                                 "pool" "default")
29                               'words))
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)
35     ;; Rule names
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)))
41
42 (defvar ninja-mode-syntax-table
43   (let ((table (make-syntax-table)))
44     (modify-syntax-entry ?\" "." table)
45     table)
46   "Syntax table used in `ninja-mode'.")
47
48 (defun ninja-syntax-propertize (start end)
49   (save-match-data
50     (goto-char start)
51     (while (search-forward "#" end t)
52       (let ((match-pos (match-beginning 0)))
53         (when (and
54                ;; Is it the first non-white character on the line?
55                (eq match-pos (save-excursion (back-to-indentation) (point)))
56                (save-excursion
57                  (goto-char (line-end-position 0))
58                  (or
59                   ;; If we're continuting the previous line, it's not a
60                   ;; comment.
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)))))))))
71
72 ;;;###autoload
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)))
78
79 ;; Run ninja-mode for files ending in .ninja.
80 ;;;###autoload
81 (add-to-list 'auto-mode-alist '("\\.ninja$" . ninja-mode))
82
83 (provide 'ninja-mode)
84
85 ;;; ninja-mode.el ends here