Avoid putting properties past the end of the buffer.
[platform/upstream/ninja.git] / misc / ninja-mode.el
1 ;;; ninja-mode.el --- Major mode for editing .ninja files
2
3 ;; Copyright 2011 Google Inc. All Rights Reserved.
4 ;;
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
8 ;;
9 ;;     http://www.apache.org/licenses/LICENSE-2.0
10 ;;
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.
16
17 ;;; Commentary:
18
19 ;; Simple emacs mode for editing .ninja files.
20 ;; Just some syntax highlighting for now.
21
22 ;;; Code:
23
24 (defvar ninja-keywords
25   `((,(concat "^" (regexp-opt '("rule" "build" "subninja" "include"
26                                 "pool" "default")
27                               'words))
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)
33     ;; Rule names
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)))
39
40 (defvar ninja-mode-syntax-table
41   (let ((table (make-syntax-table)))
42     (modify-syntax-entry ?\" "." table)
43     table)
44   "Syntax table used in `ninja-mode'.")
45
46 (defun ninja-syntax-propertize (start end)
47   (save-match-data
48     (save-excursion
49       (goto-char start)
50       (while (search-forward "#" end t)
51         (let ((match-pos (match-beginning 0)))
52           (when (and
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))))))))))
63
64 ;;;###autoload
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)))
70
71 ;; Run ninja-mode for files ending in .ninja.
72 ;;;###autoload
73 (add-to-list 'auto-mode-alist '("\\.ninja$" . ninja-mode))
74
75 (provide 'ninja-mode)
76
77 ;;; ninja-mode.el ends here