Merge branch 'master' of git://factorcode.org/git/factor
[factor/jcg.git] / misc / fuel / fuel-font-lock.el
blob4b3607b96deaca32ed2008d0dd4319b25a05e454
1 ;;; fuel-font-lock.el -- font lock for factor code
3 ;; Copyright (C) 2008, 2009 Jose Antonio Ortega Ruiz
4 ;; See http://factorcode.org/license.txt for BSD license.
6 ;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
7 ;; Keywords: languages, fuel, factor
8 ;; Start date: Wed Dec 03, 2008 21:40
10 ;;; Comentary:
12 ;; Font lock setup for highlighting Factor code.
14 ;;; Code:
16 (require 'fuel-syntax)
17 (require 'fuel-base)
19 (require 'font-lock)
22 ;;; Faces:
24 (defgroup fuel-faces nil
25 "Faces used by FUEL."
26 :group 'fuel
27 :group 'faces)
29 (defmacro fuel-font-lock--defface (face def group doc)
30 `(defface ,face (face-default-spec ,def)
31 ,(format "Face for %s." doc)
32 :group ',group
33 :group 'fuel-faces
34 :group 'faces))
36 (put 'fuel-font-lock--defface 'lisp-indent-function 1)
38 (defmacro fuel-font-lock--make-face (prefix def-prefix group face def doc)
39 (let ((face (intern (format "%s-%s" prefix face)))
40 (def (intern (format "%s-%s-face" def-prefix def))))
41 `(fuel-font-lock--defface ,face ,def ,group ,doc)))
43 (defmacro fuel-font-lock--define-faces (prefix def-prefix group faces)
44 (let ((setup (make-symbol (format "%s--faces-setup" prefix))))
45 `(progn
46 (defmacro ,setup ()
47 (cons 'progn
48 (mapcar (lambda (f) (append '(fuel-font-lock--make-face
49 ,prefix ,def-prefix ,group) f))
50 ',faces)))
51 (,setup))))
53 (fuel-font-lock--define-faces
54 factor-font-lock font-lock factor-mode
55 ((comment comment "comments")
56 (constructor type "constructors (<foo>)")
57 (constant constant "constants and literal values")
58 (number constant "integers and floats")
59 (ratio constant "ratios")
60 (declaration keyword "declaration words")
61 (parsing-word keyword "parsing words")
62 (setter-word function-name "setter words (>>foo)")
63 (getter-word function-name "getter words (foo>>)")
64 (stack-effect comment "stack effect specifications")
65 (string string "strings")
66 (symbol variable-name "name of symbol being defined")
67 (type-name type "type names")
68 (vocabulary-name constant "vocabulary names")
69 (word function-name "word, generic or method being defined")
70 (invalid-syntax warning "syntactically invalid constructs")))
73 ;;; Font lock:
75 (defun fuel-font-lock--syntactic-face (state)
76 (if (nth 3 state) 'factor-font-lock-string
77 (let ((c (char-after (nth 8 state))))
78 (cond ((or (char-equal c ?\ ) (char-equal c ?\n))
79 (save-excursion
80 (goto-char (nth 8 state))
81 (beginning-of-line)
82 (cond ((looking-at "USING: ") 'factor-font-lock-vocabulary-name)
83 ((looking-at "\\(TUPLE\\|SYMBOLS\\|VARS\\): ")
84 'factor-font-lock-symbol)
85 (t 'default))))
86 ((char-equal c ?U) 'factor-font-lock-parsing-word)
87 ((char-equal c ?\() 'factor-font-lock-stack-effect)
88 ((char-equal c ?\") 'factor-font-lock-string)
89 (t 'factor-font-lock-comment)))))
91 (defconst fuel-font-lock--font-lock-keywords
92 `((,fuel-syntax--stack-effect-regex . 'factor-font-lock-stack-effect)
93 (,fuel-syntax--brace-words-regex 1 'factor-font-lock-parsing-word)
94 (,fuel-syntax--vocab-ref-regexp 2 'factor-font-lock-vocabulary-name)
95 (,fuel-syntax--constructor-decl-regex (1 'factor-font-lock-word)
96 (2 'factor-font-lock-type-name)
97 (3 'factor-font-lock-invalid-syntax nil t))
98 (,fuel-syntax--typedef-regex (1 'factor-font-lock-type-name)
99 (2 'factor-font-lock-type-name)
100 (3 'factor-font-lock-invalid-syntax nil t))
101 (,fuel-syntax--rename-regex (1 'factor-font-lock-word)
102 (2 'factor-font-lock-vocabulary-name)
103 (3 'factor-font-lock-word)
104 (4 'factor-font-lock-invalid-syntax nil t))
105 (,fuel-syntax--declaration-words-regex . 'factor-font-lock-declaration)
106 (,fuel-syntax--word-definition-regex 2 'factor-font-lock-word)
107 (,fuel-syntax--alias-definition-regex (1 'factor-font-lock-word)
108 (2 'factor-font-lock-word))
109 (,fuel-syntax--int-constant-def-regex 2 'factor-font-lock-constant)
110 (,fuel-syntax--integer-regex . 'factor-font-lock-number)
111 (,fuel-syntax--float-regex . 'factor-font-lock-number)
112 (,fuel-syntax--ratio-regex . 'factor-font-lock-ratio)
113 (,fuel-syntax--type-definition-regex 2 'factor-font-lock-type-name)
114 (,fuel-syntax--method-definition-regex (1 'factor-font-lock-type-name)
115 (2 'factor-font-lock-word))
116 (,fuel-syntax--tuple-decl-regex 2 'factor-font-lock-type-name)
117 (,fuel-syntax--constructor-regex . 'factor-font-lock-constructor)
118 (,fuel-syntax--setter-regex . 'factor-font-lock-setter-word)
119 (,fuel-syntax--getter-regex . 'factor-font-lock-getter-word)
120 (,fuel-syntax--symbol-definition-regex 2 'factor-font-lock-symbol)
121 (,fuel-syntax--bad-string-regex . 'factor-font-lock-invalid-syntax)
122 ("\\(P\\|SBUF\\)\"" 1 'factor-font-lock-parsing-word)
123 (,fuel-syntax--parsing-words-regex . 'factor-font-lock-parsing-word)))
125 (defun fuel-font-lock--font-lock-setup (&optional keywords no-syntax)
126 (set (make-local-variable 'comment-start) "! ")
127 (set (make-local-variable 'parse-sexp-lookup-properties) t)
128 (set (make-local-variable 'font-lock-defaults)
129 `(,(or keywords 'fuel-font-lock--font-lock-keywords)
130 nil nil nil nil
131 ,@(if no-syntax nil
132 (list (cons 'font-lock-syntactic-keywords
133 fuel-syntax--syntactic-keywords)
134 (cons 'font-lock-syntactic-face-function
135 'fuel-font-lock--syntactic-face))))))
138 ;;; Fontify strings as Factor code:
140 (defun fuel-font-lock--font-lock-buffer ()
141 (let ((name " *fuel font lock*"))
142 (or (get-buffer name)
143 (let ((buffer (get-buffer-create name)))
144 (set-buffer buffer)
145 (set-syntax-table fuel-syntax--syntax-table)
146 (fuel-font-lock--font-lock-setup)
147 buffer))))
149 (defun fuel-font-lock--factor-str (str)
150 (save-current-buffer
151 (set-buffer (fuel-font-lock--font-lock-buffer))
152 (erase-buffer)
153 (insert str)
154 (let ((font-lock-verbose nil)) (font-lock-fontify-buffer))
155 (buffer-string)))
158 (provide 'fuel-font-lock)
159 ;;; fuel-font-lock.el ends here