awk-mode.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ;;; awk-mode.el --- AWK code editing commands for Emacs
  2. ;; Copyright (C) 1988, 1994, 1996, 2000-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: unix, languages
  5. ;; Obsolete-since: 22.1
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Sets up C-mode with support for awk-style #-comments and a lightly
  19. ;; hacked syntax table.
  20. ;;; Code:
  21. (defvar awk-mode-syntax-table
  22. (let ((st (make-syntax-table)))
  23. (modify-syntax-entry ?\\ "\\" st)
  24. (modify-syntax-entry ?\n "> " st)
  25. (modify-syntax-entry ?\f "> " st)
  26. (modify-syntax-entry ?\# "< " st)
  27. ;; / can delimit regexes or be a division operator. We assume that it is
  28. ;; more commonly used for regexes and fix the remaining cases with
  29. ;; `font-lock-syntactic-keywords'.
  30. (modify-syntax-entry ?/ "\"" st)
  31. (modify-syntax-entry ?* "." st)
  32. (modify-syntax-entry ?+ "." st)
  33. (modify-syntax-entry ?- "." st)
  34. (modify-syntax-entry ?= "." st)
  35. (modify-syntax-entry ?% "." st)
  36. (modify-syntax-entry ?< "." st)
  37. (modify-syntax-entry ?> "." st)
  38. (modify-syntax-entry ?& "." st)
  39. (modify-syntax-entry ?| "." st)
  40. (modify-syntax-entry ?_ "_" st)
  41. (modify-syntax-entry ?\' "\"" st)
  42. st)
  43. "Syntax table in use in `awk-mode' buffers.")
  44. ;; Regexps written with help from Peter Galbraith <galbraith@mixing.qc.dfo.ca>.
  45. (defconst awk-font-lock-keywords
  46. (eval-when-compile
  47. (list
  48. ;;
  49. ;; Function names.
  50. '("^[ \t]*\\(function\\)\\>[ \t]*\\(\\sw+\\)?"
  51. (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
  52. ;;
  53. ;; Variable names.
  54. (cons (regexp-opt
  55. '("ARGC" "ARGIND" "ARGV" "CONVFMT" "ENVIRON" "ERRNO"
  56. "FIELDWIDTHS" "FILENAME" "FNR" "FS" "IGNORECASE" "NF" "NR"
  57. "OFMT" "OFS" "ORS" "RLENGTH" "RS" "RSTART" "SUBSEP") 'words)
  58. 'font-lock-variable-name-face)
  59. ;;
  60. ;; Keywords.
  61. (regexp-opt
  62. '("BEGIN" "END" "break" "continue" "delete" "do" "exit" "else" "for"
  63. "getline" "if" "next" "print" "printf" "return" "while") 'words)
  64. ;;
  65. ;; Builtins.
  66. (list (regexp-opt
  67. '("atan2" "close" "cos" "ctime" "exp" "gsub" "index" "int"
  68. "length" "log" "match" "rand" "sin" "split" "sprintf"
  69. "sqrt" "srand" "sub" "substr" "system" "time"
  70. "tolower" "toupper") 'words)
  71. 1 'font-lock-builtin-face)
  72. ;;
  73. ;; Operators. Is this too much?
  74. (cons (regexp-opt '("&&" "||" "<=" "<" ">=" ">" "==" "!=" "!~" "~"))
  75. 'font-lock-constant-face)
  76. ))
  77. "Default expressions to highlight in AWK mode.")
  78. (require 'syntax)
  79. (defconst awk-font-lock-syntactic-keywords
  80. ;; `/' is mostly used for /.../ regular expressions, but is also
  81. ;; used as a division operator. Distinguishing between the two is
  82. ;; a pain in the youknowwhat.
  83. ;; '(("\\(^\\|[<=>-+*%/!^,~(?:|&]\\)\\s-*\\(/\\)\\([^/\n\\]\\|\\\\.\\)*\\(/\\)"
  84. ;; (2 "\"") (4 "\"")))
  85. '(("[^<=>-+*%/!^,~(?:|& \t\n\f]\\s-*\\(/\\)"
  86. (1 (unless (nth 3 (syntax-ppss (match-beginning 1))) "."))))
  87. "Syntactic keywords for `awk-mode'.")
  88. ;; No longer autoloaded since it might clobber the autoload directive in CC Mode.
  89. (define-derived-mode awk-mode c-mode "AWK"
  90. "Major mode for editing AWK code.
  91. This is much like C mode except for the syntax of comments. Its keymap
  92. inherits from C mode's and it has the same variables for customizing
  93. indentation. It has its own abbrev table and its own syntax table.
  94. Turning on AWK mode runs `awk-mode-hook'."
  95. (set (make-local-variable 'paragraph-start) (concat "$\\|" page-delimiter))
  96. (set (make-local-variable 'paragraph-separate) paragraph-start)
  97. (set (make-local-variable 'comment-start) "# ")
  98. (set (make-local-variable 'comment-end) "")
  99. (set (make-local-variable 'comment-start-skip) "#+ *")
  100. (setq font-lock-defaults '(awk-font-lock-keywords
  101. nil nil ((?_ . "w")) nil
  102. (parse-sexp-lookup-properties . t)
  103. (font-lock-syntactic-keywords
  104. . awk-font-lock-syntactic-keywords))))
  105. (provide 'awk-mode)
  106. ;;; awk-mode.el ends here