Initial Commit
[packages] / xemacs-packages / prog-modes / awk-mode.el
1 ;;; awk-mode.el --- AWK code editing commands for Emacs
2
3 ;; Copyright (C) 1988,94,96,2000  Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: unix, languages
7
8 ;; This file is part of XEmacs.
9
10 ;; XEmacs is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; XEmacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING.  If not, write to the Free
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 ;; 02111-1307, USA.
24
25 ;;; Synched up with: GNU Emacs 21.2 except not derived from c-mode and we
26 ;;;                  have a bit different regexp-opt syntax.
27
28 ;;; Commentary:
29
30 ;; Sets up C-mode with support for awk-style #-comments and a lightly
31 ;; hacked syntax table.
32
33 ;;; Code:
34
35 (defvar awk-mode-syntax-table nil
36   "Syntax table in use in Awk-mode buffers.")
37
38 (if awk-mode-syntax-table
39     ()
40   (setq awk-mode-syntax-table (make-syntax-table))
41   (modify-syntax-entry ?\\ "\\" awk-mode-syntax-table)
42   (modify-syntax-entry ?\n ">   " awk-mode-syntax-table)
43   (modify-syntax-entry ?\f ">   " awk-mode-syntax-table)
44   (modify-syntax-entry ?\# "<   " awk-mode-syntax-table)
45   (modify-syntax-entry ?/ "." awk-mode-syntax-table)
46   (modify-syntax-entry ?* "." awk-mode-syntax-table)
47   (modify-syntax-entry ?+ "." awk-mode-syntax-table)
48   (modify-syntax-entry ?- "." awk-mode-syntax-table)
49   (modify-syntax-entry ?= "." awk-mode-syntax-table)
50   (modify-syntax-entry ?% "." awk-mode-syntax-table)
51   (modify-syntax-entry ?< "." awk-mode-syntax-table)
52   (modify-syntax-entry ?> "." awk-mode-syntax-table)
53   (modify-syntax-entry ?& "." awk-mode-syntax-table)
54   (modify-syntax-entry ?| "." awk-mode-syntax-table)
55   (modify-syntax-entry ?_ "_" awk-mode-syntax-table)
56   (modify-syntax-entry ?\' "\"" awk-mode-syntax-table))
57
58 (defvar awk-mode-abbrev-table nil
59   "Abbrev table in use in Awk-mode buffers.")
60 (define-abbrev-table 'awk-mode-abbrev-table ())
61
62 ;; Regexps written with help from Peter Galbraith <galbraith@mixing.qc.dfo.ca>.
63 (defconst awk-font-lock-keywords
64   (eval-when-compile
65     (list
66      ;;
67      ;; Regular expressions. npak@ispras.ru
68      ;; We shoul do it before any other fontification
69      ;; Split into two cases for Speed god.
70      (cons "^[ \t]*/\\(\\(\\([\\].\\)?[^/\\]*\\)+\\)"
71            '(1 'font-lock-string-face))
72      (cons "[-!~+*=&<>|(][ \t]*/\\(\\(\\([\\].\\)?[^/\\]*\\)+\\)"
73            '(1 font-lock-string-face))
74      ;;
75      ;; Function names.
76      '("^[ \t]*\\(function\\)\\>[ \t]*\\(\\sw+\\)?"
77        (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
78      ;;
79      ;; Variable names.
80      ;; "RT", field identifiers $0, $1 etc added by npak@ispras.ru
81      (cons (concat
82             "\\<\\(?:"
83             (regexp-opt
84              '("ARGC" "ARGIND" "ARGV" "CONVFMT" "ENVIRON" "ERRNO"
85                "FIELDWIDTHS" "FILENAME" "FNR" "FS" "IGNORECASE" "NF" "NR"
86                "OFMT" "OFS" "ORS" "RLENGTH" "RS" "RT" "RSTART" "SUBSEP"))
87             "\\|\\$\\(?:[0-9]+\\|\\sw+\\)\\)\\>")
88            'font-lock-variable-name-face)
89      ;;
90      ;; Keywords.
91      ;; "do", "in", "nextfile" added by npak@ispras.ru
92      (concat
93       "\\<\\(?:"
94       (regexp-opt
95        '("BEGIN" "END" "break" "continue" "delete" "do" "else" "exit" "for"
96          "getline" "if" "in" "next" "nextfile" "print" "printf" "return"
97          "while"))
98       "\\)\\>")
99      ;;
100      ;; Builtins.
101      ;; "fflush" added by npak@ispras.ru
102      (list (concat
103             "\\<\\("
104             (regexp-opt
105              '("atan2" "close" "cos" "ctime" "exp" "gsub" "index" "int"
106                "length" "log" "match" "rand" "sin" "split" "sprintf"
107                "sqrt" "srand" "sub" "substr" "system" "time" "fflush"
108                "tolower" "toupper"))
109             "\\)(")
110            1 'font-lock-reference-face)
111      ;;
112      ;; Function call, variables. npak@ispras.ru
113      (list "\\<\\([A-Za-z_]\\sw*\\)("
114            1 'font-lock-function-name-face)
115      (list "\\<\\([A-Za-z_]\\sw*\\)\\($\\|[^(]\\)"
116            1 'font-lock-variable-name-face)
117      ;;
118      ;; Operators.  Is this too much?
119      ;; Yes, it is. npak@ispras.ru
120      ;(cons (regexp-opt '("&&" "||" "<=" "<" ">=" ">" "==" "!=" "!~" "~"))
121      ;      'font-lock-reference-face)
122      ))
123  "Default expressions to highlight in AWK mode.")
124
125 ;; npak@ispras.ru
126 (put 'awk-mode 'font-lock-defaults
127      '(awk-font-lock-keywords nil nil ((?_ . "w"))))
128
129 ;;;###autoload
130 (defun awk-mode ()
131   "Major mode for editing AWK code.
132 This is much like C mode except for the syntax of comments.  It uses
133 the same keymap as C mode and has the same variables for customizing
134 indentation.  It has its own abbrev table and its own syntax table.
135
136 Turning on AWK mode calls the value of the variable `awk-mode-hook'
137 with no args, if that value is non-nil."
138   (interactive)
139   (kill-all-local-variables)
140   (require 'cc-mode)
141   (c-initialize-cc-mode)
142   (use-local-map c-mode-map)
143   (c-common-init)
144   (setq major-mode 'awk-mode)
145   (setq mode-name "AWK")
146   (setq local-abbrev-table awk-mode-abbrev-table)
147   (set-syntax-table awk-mode-syntax-table)
148   (make-local-variable 'paragraph-start)
149   (setq paragraph-start (concat "$\\|" page-delimiter))
150   (make-local-variable 'paragraph-separate)
151   (setq paragraph-separate paragraph-start)
152   (make-local-variable 'paragraph-ignore-fill-prefix)
153   (setq paragraph-ignore-fill-prefix t)
154   (make-local-variable 'indent-line-function)
155   (setq indent-line-function 'c-indent-line)
156   (make-local-variable 'require-final-newline)
157   (setq require-final-newline t)
158   (make-local-variable 'comment-start)
159   (setq comment-start "# ")
160   (make-local-variable 'comment-end)
161   (setq comment-end "")
162   (make-local-variable 'comment-column)
163   (setq comment-column 32)
164   (make-local-variable 'comment-start-skip)
165   (setq comment-start-skip "#+ *")
166   (make-local-variable 'comment-indent-function)
167   (setq comment-indent-function 'c-comment-indent)
168   (make-local-variable 'parse-sexp-ignore-comments)
169   (setq parse-sexp-ignore-comments t)
170   ;; No need in XEmacs
171   ;(make-local-variable 'font-lock-defaults)
172   ;(setq font-lock-defaults '(awk-font-lock-keywords nil nil ((?_ . "w"))))
173   (run-hooks 'awk-mode-hook))
174
175 ;; XEmacs additions
176 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.awk\\'" . awk-mode))
177 ;;;###autoload(add-to-list 'interpreter-mode-alist '("awk\\b" . awk-mode))
178
179 (provide 'awk-mode)
180
181 ;;; awk-mode.el ends here