awk-mode.el 4.5 KB

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