From 77d5c3a67a5f985981e93424b5e35e840a664ee0 Mon Sep 17 00:00:00 2001 From: Fanael Linithien Date: Sat, 22 Nov 2014 18:15:09 +0100 Subject: Don't leave lone closing parens. --- misc/ninja-mode.el | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/misc/ninja-mode.el b/misc/ninja-mode.el index ee5fe48..d3b2248 100644 --- a/misc/ninja-mode.el +++ b/misc/ninja-mode.el @@ -37,16 +37,14 @@ ;; Build Statement - highlight the rule used, ;; allow for escaped $,: in outputs. '("build +\\(?:[^:$\n]\\|$[:$]\\)+ *: *\\([[:alnum:]_.-]+\\)" . - (1 font-lock-function-name-face)) - )) + (1 font-lock-function-name-face)))) ;;;###autoload (define-derived-mode ninja-mode prog-mode "ninja" (setq comment-start "#") ; Pass extra "t" to turn off syntax-based fontification -- we don't want ; quoted strings highlighted. - (setq font-lock-defaults '(ninja-keywords t)) - ) + (setq font-lock-defaults '(ninja-keywords t))) ;; Run ninja-mode for files ending in .ninja. ;;;###autoload -- cgit v0.12 From 6091cfefcd0f283eef17e256db4991cc7c3daac4 Mon Sep 17 00:00:00 2001 From: Fanael Linithien Date: Sat, 22 Nov 2014 18:17:39 +0100 Subject: Use double semicolon comments. --- misc/ninja-mode.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/misc/ninja-mode.el b/misc/ninja-mode.el index d3b2248..0eae72e 100644 --- a/misc/ninja-mode.el +++ b/misc/ninja-mode.el @@ -42,8 +42,8 @@ ;;;###autoload (define-derived-mode ninja-mode prog-mode "ninja" (setq comment-start "#") - ; Pass extra "t" to turn off syntax-based fontification -- we don't want - ; quoted strings highlighted. + ;; Pass extra "t" to turn off syntax-based fontification -- we don't want + ;; quoted strings highlighted. (setq font-lock-defaults '(ninja-keywords t))) ;; Run ninja-mode for files ending in .ninja. -- cgit v0.12 From a13b64746022ab1f5f47c18b0bd1cc3c909e210e Mon Sep 17 00:00:00 2001 From: Fanael Linithien Date: Sat, 22 Nov 2014 18:15:46 +0100 Subject: Reindent ninja-mode.el using Emacs. Emacs knows best how to indent Emacs Lisp. --- misc/ninja-mode.el | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/misc/ninja-mode.el b/misc/ninja-mode.el index 0eae72e..ad1722c 100644 --- a/misc/ninja-mode.el +++ b/misc/ninja-mode.el @@ -22,22 +22,22 @@ ;;; Code: (defvar ninja-keywords - (list - '("^#.*" . font-lock-comment-face) - (cons (concat "^" (regexp-opt '("rule" "build" "subninja" "include" - "pool" "default") - 'words)) - font-lock-keyword-face) - '("\\([[:alnum:]_]+\\) =" . (1 font-lock-variable-name-face)) - ;; Variable expansion. - '("\\($[[:alnum:]_]+\\)" . (1 font-lock-variable-name-face)) - '("\\(${[[:alnum:]._]+}\\)" . (1 font-lock-variable-name-face)) - ;; Rule names - '("rule +\\([[:alnum:]_.-]+\\)" . (1 font-lock-function-name-face)) - ;; Build Statement - highlight the rule used, - ;; allow for escaped $,: in outputs. - '("build +\\(?:[^:$\n]\\|$[:$]\\)+ *: *\\([[:alnum:]_.-]+\\)" . - (1 font-lock-function-name-face)))) + (list + '("^#.*" . font-lock-comment-face) + (cons (concat "^" (regexp-opt '("rule" "build" "subninja" "include" + "pool" "default") + 'words)) + font-lock-keyword-face) + '("\\([[:alnum:]_]+\\) =" . (1 font-lock-variable-name-face)) + ;; Variable expansion. + '("\\($[[:alnum:]_]+\\)" . (1 font-lock-variable-name-face)) + '("\\(${[[:alnum:]._]+}\\)" . (1 font-lock-variable-name-face)) + ;; Rule names + '("rule +\\([[:alnum:]_.-]+\\)" . (1 font-lock-function-name-face)) + ;; Build Statement - highlight the rule used, + ;; allow for escaped $,: in outputs. + '("build +\\(?:[^:$\n]\\|$[:$]\\)+ *: *\\([[:alnum:]_.-]+\\)" . + (1 font-lock-function-name-face)))) ;;;###autoload (define-derived-mode ninja-mode prog-mode "ninja" -- cgit v0.12 From 2f7f4ae945f440744fb5addc39c637e48f5a08f2 Mon Sep 17 00:00:00 2001 From: Fanael Linithien Date: Sat, 22 Nov 2014 18:22:11 +0100 Subject: Use quasi-quoting instead of list+cons. --- misc/ninja-mode.el | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/misc/ninja-mode.el b/misc/ninja-mode.el index ad1722c..48709bc 100644 --- a/misc/ninja-mode.el +++ b/misc/ninja-mode.el @@ -22,21 +22,20 @@ ;;; Code: (defvar ninja-keywords - (list - '("^#.*" . font-lock-comment-face) - (cons (concat "^" (regexp-opt '("rule" "build" "subninja" "include" - "pool" "default") - 'words)) - font-lock-keyword-face) - '("\\([[:alnum:]_]+\\) =" . (1 font-lock-variable-name-face)) - ;; Variable expansion. - '("\\($[[:alnum:]_]+\\)" . (1 font-lock-variable-name-face)) - '("\\(${[[:alnum:]._]+}\\)" . (1 font-lock-variable-name-face)) - ;; Rule names - '("rule +\\([[:alnum:]_.-]+\\)" . (1 font-lock-function-name-face)) - ;; Build Statement - highlight the rule used, - ;; allow for escaped $,: in outputs. - '("build +\\(?:[^:$\n]\\|$[:$]\\)+ *: *\\([[:alnum:]_.-]+\\)" . + `(("^#.*" . font-lock-comment-face) + (,(concat "^" (regexp-opt '("rule" "build" "subninja" "include" + "pool" "default") + 'words)) + . font-lock-keyword-face) + ("\\([[:alnum:]_]+\\) =" . (1 font-lock-variable-name-face)) + ;; Variable expansion. + ("\\($[[:alnum:]_]+\\)" . (1 font-lock-variable-name-face)) + ("\\(${[[:alnum:]._]+}\\)" . (1 font-lock-variable-name-face) + ;; Rule names + ("rule +\\([[:alnum:]_.-]+\\)" . (1 font-lock-function-name-face)) + ;; Build Statement - highlight the rule used, + ;; allow for escaped $,: in outputs. + ("build +\\(?:[^:$\n]\\|$[:$]\\)+ *: *\\([[:alnum:]_.-]+\\)" . (1 font-lock-function-name-face)))) ;;;###autoload -- cgit v0.12 From c600a8258959e7338296423bc05cf47534272ae7 Mon Sep 17 00:00:00 2001 From: Fanael Linithien Date: Sat, 22 Nov 2014 18:23:01 +0100 Subject: Set comment-start buffer-locally, not globally. --- misc/ninja-mode.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/ninja-mode.el b/misc/ninja-mode.el index 48709bc..10cec3e 100644 --- a/misc/ninja-mode.el +++ b/misc/ninja-mode.el @@ -40,7 +40,7 @@ ;;;###autoload (define-derived-mode ninja-mode prog-mode "ninja" - (setq comment-start "#") + (set (make-local-variable 'comment-start) "#") ;; Pass extra "t" to turn off syntax-based fontification -- we don't want ;; quoted strings highlighted. (setq font-lock-defaults '(ninja-keywords t))) -- cgit v0.12 From 327c094596228b8dfdc3715a560ef226cd1403a9 Mon Sep 17 00:00:00 2001 From: Fanael Linithien Date: Sat, 22 Nov 2014 18:35:46 +0100 Subject: Set up a proper syntax table for ninja-mode. Since quotes are not meant to be treated as string delimiters, the syntax table is the place to tell Emacs so. This also means syntactic fontification can be reenabled and the font-lock keyword entry for comments removed. --- misc/ninja-mode.el | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/misc/ninja-mode.el b/misc/ninja-mode.el index 10cec3e..021ede1 100644 --- a/misc/ninja-mode.el +++ b/misc/ninja-mode.el @@ -22,8 +22,7 @@ ;;; Code: (defvar ninja-keywords - `(("^#.*" . font-lock-comment-face) - (,(concat "^" (regexp-opt '("rule" "build" "subninja" "include" + `((,(concat "^" (regexp-opt '("rule" "build" "subninja" "include" "pool" "default") 'words)) . font-lock-keyword-face) @@ -38,12 +37,16 @@ ("build +\\(?:[^:$\n]\\|$[:$]\\)+ *: *\\([[:alnum:]_.-]+\\)" . (1 font-lock-function-name-face)))) +(defvar ninja-mode-syntax-table + (let ((table (make-syntax-table))) + (modify-syntax-entry ?\" "." table) + table) + "Syntax table used in `ninja-mode'.") + ;;;###autoload (define-derived-mode ninja-mode prog-mode "ninja" (set (make-local-variable 'comment-start) "#") - ;; Pass extra "t" to turn off syntax-based fontification -- we don't want - ;; quoted strings highlighted. - (setq font-lock-defaults '(ninja-keywords t))) + (setq font-lock-defaults '(ninja-keywords))) ;; Run ninja-mode for files ending in .ninja. ;;;###autoload -- cgit v0.12 From 931db561cfd2c3fcfe85a5ab8783828137106014 Mon Sep 17 00:00:00 2001 From: Fanael Linithien Date: Sat, 22 Nov 2014 18:57:31 +0100 Subject: Correctly recognize comments. --- misc/ninja-mode.el | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/misc/ninja-mode.el b/misc/ninja-mode.el index 021ede1..fd54b29 100644 --- a/misc/ninja-mode.el +++ b/misc/ninja-mode.el @@ -43,9 +43,26 @@ table) "Syntax table used in `ninja-mode'.") +(defun ninja-syntax-propertize (start end) + (save-match-data + (save-excursion + (goto-char start) + (while (search-forward "#" end t) + (let ((match-pos (match-beginning 0))) + (when (and + ;; Is it the first non-white character on the line? + (eq match-pos (save-excursion (back-to-indentation) (point))) + ;; Are we *not* continuing the previous line? + (not (eq ?$ (char-before (line-end-position 0))))) + (put-text-property match-pos (1+ match-pos) 'syntax-table '(11)) + (let ((line-end (line-end-position))) + (put-text-property line-end (1+ line-end) 'syntax-table '(12))))))))) + ;;;###autoload (define-derived-mode ninja-mode prog-mode "ninja" (set (make-local-variable 'comment-start) "#") + (set (make-local-variable 'parse-sexp-lookup-properties) t) + (set (make-local-variable 'syntax-propertize-function) #'ninja-syntax-propertize) (setq font-lock-defaults '(ninja-keywords))) ;; Run ninja-mode for files ending in .ninja. -- cgit v0.12 From 310532c0b0c4a80f089477e7cdd2fb99b20e7de1 Mon Sep 17 00:00:00 2001 From: Fanael Linithien Date: Sat, 22 Nov 2014 18:59:53 +0100 Subject: Remove unnecessary regexp group. --- misc/ninja-mode.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/misc/ninja-mode.el b/misc/ninja-mode.el index fd54b29..b475edb 100644 --- a/misc/ninja-mode.el +++ b/misc/ninja-mode.el @@ -28,8 +28,8 @@ . font-lock-keyword-face) ("\\([[:alnum:]_]+\\) =" . (1 font-lock-variable-name-face)) ;; Variable expansion. - ("\\($[[:alnum:]_]+\\)" . (1 font-lock-variable-name-face)) - ("\\(${[[:alnum:]._]+}\\)" . (1 font-lock-variable-name-face) + ("$[[:alnum:]_]+" . font-lock-variable-name-face) + ("${[[:alnum:]._]+}" . font-lock-variable-name-face) ;; Rule names ("rule +\\([[:alnum:]_.-]+\\)" . (1 font-lock-function-name-face)) ;; Build Statement - highlight the rule used, -- cgit v0.12 From 14d161080c256ac5e7e877058691172a95acda45 Mon Sep 17 00:00:00 2001 From: Fanael Linithien Date: Sat, 22 Nov 2014 19:03:23 +0100 Subject: Don't use dotted list syntax unless necessary. --- misc/ninja-mode.el | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/misc/ninja-mode.el b/misc/ninja-mode.el index b475edb..20b1237 100644 --- a/misc/ninja-mode.el +++ b/misc/ninja-mode.el @@ -26,16 +26,16 @@ "pool" "default") 'words)) . font-lock-keyword-face) - ("\\([[:alnum:]_]+\\) =" . (1 font-lock-variable-name-face)) + ("\\([[:alnum:]_]+\\) =" 1 font-lock-variable-name-face) ;; Variable expansion. ("$[[:alnum:]_]+" . font-lock-variable-name-face) ("${[[:alnum:]._]+}" . font-lock-variable-name-face) ;; Rule names - ("rule +\\([[:alnum:]_.-]+\\)" . (1 font-lock-function-name-face)) + ("rule +\\([[:alnum:]_.-]+\\)" 1 font-lock-function-name-face) ;; Build Statement - highlight the rule used, ;; allow for escaped $,: in outputs. - ("build +\\(?:[^:$\n]\\|$[:$]\\)+ *: *\\([[:alnum:]_.-]+\\)" . - (1 font-lock-function-name-face)))) + ("build +\\(?:[^:$\n]\\|$[:$]\\)+ *: *\\([[:alnum:]_.-]+\\)" + 1 font-lock-function-name-face))) (defvar ninja-mode-syntax-table (let ((table (make-syntax-table))) -- cgit v0.12 From 73f934e88679fee031f971554a7b79d640fb28f5 Mon Sep 17 00:00:00 2001 From: Fanael Linithien Date: Sat, 22 Nov 2014 19:25:43 +0100 Subject: Avoid putting properties past the end of the buffer. --- misc/ninja-mode.el | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/misc/ninja-mode.el b/misc/ninja-mode.el index 20b1237..330eba7 100644 --- a/misc/ninja-mode.el +++ b/misc/ninja-mode.el @@ -56,7 +56,10 @@ (not (eq ?$ (char-before (line-end-position 0))))) (put-text-property match-pos (1+ match-pos) 'syntax-table '(11)) (let ((line-end (line-end-position))) - (put-text-property line-end (1+ line-end) 'syntax-table '(12))))))))) + ;; Avoid putting properties past the end of the buffer. + ;; Otherwise we get an `args-out-of-range' error. + (unless (= line-end (1+ (buffer-size))) + (put-text-property line-end (1+ line-end) 'syntax-table '(12)))))))))) ;;;###autoload (define-derived-mode ninja-mode prog-mode "ninja" -- cgit v0.12 From 11377a46d103f788982e9a190c50a8526b9a6b80 Mon Sep 17 00:00:00 2001 From: Fanael Linithien Date: Sun, 23 Nov 2014 16:05:48 +0100 Subject: Correctly recognize a comment if the previous line is a comment ending in $. --- misc/ninja-mode.el | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/misc/ninja-mode.el b/misc/ninja-mode.el index 330eba7..4802ef1 100644 --- a/misc/ninja-mode.el +++ b/misc/ninja-mode.el @@ -52,8 +52,15 @@ (when (and ;; Is it the first non-white character on the line? (eq match-pos (save-excursion (back-to-indentation) (point))) - ;; Are we *not* continuing the previous line? - (not (eq ?$ (char-before (line-end-position 0))))) + (save-excursion + (goto-char (line-end-position 0)) + (or + ;; If we're continuting the previous line, it's not a + ;; comment. + (not (eq ?$ (char-before))) + ;; Except if the previous line is a comment as well, as the + ;; continuation dollar is ignored then. + (nth 4 (syntax-ppss))))) (put-text-property match-pos (1+ match-pos) 'syntax-table '(11)) (let ((line-end (line-end-position))) ;; Avoid putting properties past the end of the buffer. -- cgit v0.12 From f15faca3c35881e5f2ada33a63a8a452bc0f3d1e Mon Sep 17 00:00:00 2001 From: Fanael Linithien Date: Sun, 23 Nov 2014 22:26:54 +0100 Subject: Add a Emacs 24 requirement. Only Emacs >= 24 has prog-mode. --- misc/ninja-mode.el | 2 ++ 1 file changed, 2 insertions(+) diff --git a/misc/ninja-mode.el b/misc/ninja-mode.el index 4802ef1..ac5318d 100644 --- a/misc/ninja-mode.el +++ b/misc/ninja-mode.el @@ -1,5 +1,7 @@ ;;; ninja-mode.el --- Major mode for editing .ninja files +;; Package-Requires: ((emacs "24")) + ;; Copyright 2011 Google Inc. All Rights Reserved. ;; ;; Licensed under the Apache License, Version 2.0 (the "License"); -- cgit v0.12 From 40a76d8cd5c77833d31b19311f6a9213c23fd09a Mon Sep 17 00:00:00 2001 From: Fanael Linithien Date: Sun, 23 Nov 2014 22:29:49 +0100 Subject: Use lexical-binding. There's no reason not to use lexical-binding when supporting only Emacs 24+. Its semantics are just that much saner. --- misc/ninja-mode.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/ninja-mode.el b/misc/ninja-mode.el index ac5318d..71825d5 100644 --- a/misc/ninja-mode.el +++ b/misc/ninja-mode.el @@ -1,4 +1,4 @@ -;;; ninja-mode.el --- Major mode for editing .ninja files +;;; ninja-mode.el --- Major mode for editing .ninja files -*- lexical-binding: t -*- ;; Package-Requires: ((emacs "24")) -- cgit v0.12