aboutsummaryrefslogtreecommitdiff
path: root/elisp/mfl-mode.el
blob: 5c66edc605d9e88599f8d285d42aee38cafb9688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
;;; mfl-mode.el --- major mode for editing MFL sources

;; Authors: 2007 Sergey Poznyakoff
;; Version: 0.1
;; Keywords: mailfromd, MFL
;; $Id$

;; This file is part of mailfromd
;; Copyright (C) 2007 Sergey Poznyakoff
 
;; Mailfromd is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2 of the License, or
;; (at your option) any later version.
 
;; Mailfromd is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with mailfromd; if not, write to the Free Software Foundation,
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

;; Installation:
;; You may wish to use precompiled version of the module. To create it
;; run:
;;    emacs -batch -f batch-byte-compile mfl-mode.el
;; Install the file mfl-mode.elc (and, optionally, mfl-mode.el) to
;; any directory in your Emacs load-path.

(eval-when-compile
  ;; We use functions from these modules
  (mapcar 'require '(font-lock)))

(defvar mfl-mode-syntax-table nil
  "Syntax table used in mfl-mode buffers.")

(unless mfl-mode-syntax-table
  (setq mfl-mode-syntax-table (make-syntax-table))
;  (modify-syntax-entry ?\# "<" mfl-mode-syntax-table)
;  (modify-syntax-entry ?\n ">" mfl-mode-syntax-table)
  (modify-syntax-entry ?\t "-" mfl-mode-syntax-table)
  (modify-syntax-entry ?\( "(" mfl-mode-syntax-table)
  (modify-syntax-entry ?\) ")" mfl-mode-syntax-table)
;  (modify-syntax-entry ?\` "(" mfl-mode-syntax-table)
;  (modify-syntax-entry ?\' ")\"" mfl-mode-syntax-table)
  (modify-syntax-entry ?\' "\"" mfl-mode-syntax-table)
  (modify-syntax-entry ?\" "\"" mfl-mode-syntax-table)
  (modify-syntax-entry ?\\ "\\" mfl-mode-syntax-table)
  (modify-syntax-entry ?\/ ". 14" mfl-mode-syntax-table)
  (modify-syntax-entry ?\* ". 23" mfl-mode-syntax-table))

(defvar mfl-mode-map (make-sparse-keymap)
  "Keymap used in MFL mode.")

;(define-key mfl-mode-map [menu-bar] (make-sparse-keymap))
;(define-key cflow-mode-map [menu-bar MFL]
;  (cons "MFL" (make-sparse-keymap "MFL")))


(defconst mfl-keywords
  (eval-when-compile
    (regexp-opt '("add" "and" "begin" "break" "case"
		  "catch" "const" "continue" "default"
		  "delete" "discard" "do" "done"
		  "echo" "end" "elif" "else"
		  "fi" "fnmatches" "for" "func"
		  "if" "loop" "matches" "next"
		  "not" "on" "or" "pass"
		  "prog" "reject" "replace" "return"
		  "returns" "set"  "switch" "tempfail"
		  "throw" "when" "while"
		  ;; FIXME: These are context-dependent
		  "as" "from" "host" "poll"))))

(defconst mfl-constants
  (eval-when-compile
    (regexp-opt '("__file__" "__function__" "__line__" "__major__"
		  "__minor__" "__package__" "__patch__" "__preproc__"
		  "__version__"))))

(defconst mfl-type-names
  (eval-when-compile
    (regexp-opt '("number" "string"))))

(defconst mfl-preprocessor-directives
  (eval-when-compile
    (regexp-opt '("include" "include_once" "error" "line"
		  "pragma" "require" "warning"))))

(defconst mfl-m4-keywords
  (eval-when-compile
    (regexp-opt '("m4_define" "m4_defn" "m4_undefconst" "m4_builtin"
		  "m4_changecom" "m4_changequote" "m4_debugfile" "m4_debugmode"
		  "m4_decr" "m4_divert" "m4_divnum" "m4_dumpdef"
		  "m4_errprint" "m4_esyscmd" "m4_eval" "m4_format"
		  "m4_ifdef" "m4_ifelse" "m4_include" "m4_incr"
		  "m4_index" "m4_indir" "m4_len" "m4_exit"
		  "m4_wrap" "m4_maketemp" "m4_patsubst" "m4_popdef"
		  "m4_pushdef" "m4_regexp" "m4_shift" "m4_sinclude"
		  "m4_substr" "m4_symbols" "m4_syscmd" "m4_sysval"
		  "m4_traceoff" "m4_traceon" "m4_translit" "m4_undivert"
		  "m4_dnl" "m4___line__" "m4___file__"))))

(defconst mfl-macros
  (eval-when-compile
    (regexp-opt '("defined" "printf" "_" "N_"))))


;; Font-lock stuff
(defconst mfl-font-lock-keywords
  (eval-when-compile
    (list
     ;; Fontify error and warning directives.
     '("^#[ \t]*error[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
     '("^#[ \t]*warning[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
     ;;
     ;; Fontify filenames in #include <...> preprocessor directives as strings.
     '("^#[ \t]*\\(include\\|include_once\\)[ \t]*\\(<[^>\"\n]*>?\\)"
       2 font-lock-string-face)
     ;; Fontify module names in #require directives as strings.
     '("^#[ \t]*\\(require\\)[ \t]*\\(.[^\n]*\\)"
       2 font-lock-string-face)
     ;; Fontify otherwise as symbol names, and the preprocessor directive
     ;; names.
    (list
     (concat "^#[ \t]*\\(" mfl-preprocessor-directives
	      "\\)\\>[ \t!]*\\(\\sw+\\)?")
      '(1 font-lock-builtin-face))
     ;; Otherwise, fontify #...\n as comments:
    (list
     "^#\\(.*\\)\n" 1 font-lock-comment-face)
    
     ;; Fontify all type names.
     `(eval .
       (cons (concat "\\<\\(" ,mfl-type-names "\\)\\>") 'font-lock-type-face))

     ;;
     ;; Fontify all builtin keywords 
     (concat "\\<\\(" mfl-keywords "\\)\\>")

     ;; Fontify m4 keywords and macros
     (list
      (concat "\\<\\(" mfl-m4-keywords "\\)\\>")
      1 font-lock-builtin-face)

     ;; Fontify variable names
     (list
      "%\\([a-zA-Z0-9_]+\\)"
      1 font-lock-variable-name-face)
     (list
      "set[ \t]+\\([a-zA-Z0-9_]+\\)"
      1 font-lock-variable-name-face)
     (list
      "defined[ \t]*([ \t]*\\([a-zA-Z0-9_]+\\)[ \t]*)"
      1 font-lock-variable-name-face)
     (list
      (concat "\\<\\(" mfl-type-names "\\)\\>[ \t]+\\([a-zA-Z0-9_]+\\)")
      2 font-lock-variable-name-face)

     ;; Fontify macro names
     (list
      "${?\\([a-zA-Z0-9_]+\\)}?"
      1 font-lock-constant-face)
     )))
     
;;;###autoload
(defun mfl-mode ()
  "Major mode for viewing cflow output files

Key bindings are:
\\{mfl-mode-map}
"
  (interactive)
  (kill-all-local-variables)
  (use-local-map mfl-mode-map)
  (setq major-mode 'mfl-mode
	mode-name "MFL")

  (set-syntax-table mfl-mode-syntax-table)

  (make-local-variable 'font-lock-defaults)
  (setq font-lock-defaults
	'((mfl-font-lock-keywords) nil nil
	  nil
	  nil)))

(provide 'mfl-mode)
;;; mfl-mode ends

Return to:

Send suggestions and report system problems to the System administrator.