.emacs.d/ecmascript-mode.el
author Adam Gomaa <adam@gomaa.us>
Sun Dec 18 13:13:02 2011 -0500
changeset 521 67f37d330ad0
permissions -rw-r--r--
Remove project & _list_projects, I don't use them anymore.
     1 ;;; ecmascript-mode.el --- major mode for editing ECMAScript code
     2 
     3 ;; Copyright (c) 2004-2005 David Lindquist <david.lindquist@gmail.com>
     4 
     5 ;; Author: David Lindquist <david.lindquist@gmail.com>
     6 ;; Keywords: languages ecmascript javascript
     7 
     8 ;; This is free software; you can redistribute it and/or modify it
     9 ;; under the terms of the GNU General Public License as published by
    10 ;; the Free Software Foundation; either version 2, or (at your option)
    11 ;; any later version.
    12 
    13 ;; This is distributed in the hope that it will be useful, but WITHOUT
    14 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    15 ;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
    16 ;; License for more details.
    17 
    18 ;; You should have received a copy of the GNU General Public License
    19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
    20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    21 ;; Boston, MA 02111-1307, USA.
    22 
    23 ;;; Commentary:
    24 
    25 ;; ecmascript-mode is a basic major mode for editing code conforming
    26 ;; to the ECMA-262 standard (3rd edition). The most notable languages
    27 ;; employing all or most of the standard are JavaScript (Netscape),
    28 ;; JScript (Microsoft), and ActionScript (Macromedia).
    29 
    30 ;; See also:
    31 ;; http://www.ecma-international.org/publications/files/ecma-st/ECMA-262.pdf
    32 
    33 ;;; History:
    34 
    35 ;; 2005-11-24 david Fixed void function error and corrected font-lock
    36 ;;                  problems that appeared in emacs version 21.3.50.1
    37 ;;                  (probably due to changes in java-mode).
    38 ;; 2004-??-?? david Initial release.
    39 
    40 ;;; Code:
    41 
    42 (require 'font-lock)
    43 (require 'cc-mode)
    44 (eval-when-compile
    45   (require 'regexp-opt))
    46 
    47 (defconst ecmascript-mode-version "1.1"
    48   "ECMAScript Mode version number.")
    49 
    50 (defgroup ecmascript nil
    51   "Major mode for editing ECMAScript code."
    52   :group 'languages
    53   :prefix "ecmascript-")
    54 
    55 (defcustom ecmascript-mode-hook nil
    56   "Hook for customizing `ecmascript-mode'."
    57   :group 'ecmascript
    58   :type 'hook)
    59 
    60 (defvar ecmascript-mode-map (c-make-inherited-keymap)
    61   "Keymap used in `ecmascript-mode' buffers.")
    62 
    63 ;;;###autoload
    64 (define-derived-mode ecmascript-mode java-mode "ECMAScript"
    65   "Major mode for editing ECMAScript code.
    66 
    67 This mode is derived from `java-mode'; see its documentation for further
    68 information.
    69 
    70 \\{ecmascript-mode-map}"
    71   (make-local-variable 'font-lock-defaults)
    72   (setq font-lock-defaults
    73         '((;; comment out the lines below to adjust
    74            ;; syntax highlighting gaudiness
    75            ecmascript-font-lock-keywords-1
    76            ecmascript-font-lock-keywords-2
    77            ecmascript-font-lock-keywords-3
    78            )
    79           nil nil ((?_ . "w") (?$ . "w")) nil))
    80 
    81   (easy-menu-define c-ecmascript-menu ecmascript-mode-map
    82     "ECMAScript Mode Commands" (c-mode-menu "ECMAScript"))
    83   )
    84 
    85 (defvar ecmascript-font-lock-default-face 'ecmascript-font-lock-default-face)
    86 
    87 (defconst ecmascript-font-lock-keywords-1
    88   (append
    89    java-font-lock-keywords-1
    90    (list
    91 
    92     '("\\<\\(function\\)\\>\\(?:\\s-+\\(\\sw+\\)\\)?"
    93       (1 font-lock-keyword-face t)
    94       (2 font-lock-function-name-face nil t))
    95 
    96     ;; need to fix this to handle: var a, b;
    97     '("\\<\\(var\\)\\>\\(?:\\s-+\\(\\sw+\\)\\)?"
    98       (1 font-lock-keyword-face t)
    99       (2 font-lock-variable-name-face nil t))
   100     ))
   101   "Subdued level highlighting for ECMAScript mode.")
   102 
   103 (defconst ecmascript-font-lock-keywords-2
   104   (append
   105    java-font-lock-keywords-2
   106    ecmascript-font-lock-keywords-1
   107    (list
   108 
   109     '("\\<\\(debugger\\|delete\\|export\\|in\\|typeof\\|with\\)\\>"
   110       (1 font-lock-keyword-face t))
   111 
   112     (list (concat
   113            "\\<\\("
   114            (mapconcat 'identity java-font-lock-extra-types nil)
   115            "\\)\\>\\.")
   116           '(1 font-lock-type-face nil t))
   117 
   118     ;; In Java, `void' is a type. In ECMAScript, it is an operator.
   119     ;; This overrides the inherited notion of keyword `void'.
   120     '("\\<\\(void\\)\\>\\(?:\\s-+\\(\\sw+\\)\\)?"
   121       (1 font-lock-keyword-face t)
   122       (2 ecmascript-font-lock-default-face t t))
   123 
   124     ;; Value properties of the global object
   125     '("\\<\\(Infinity\\|NaN\\|undefined\\)\\>" 0 font-lock-constant-face t)
   126 
   127     ;; Properties of the Number constructor
   128     (list (concat
   129            "\\<Number\\."
   130            (regexp-opt
   131             '("MAX_VALUE" "MIN_VALUE" "NaN" "NEGATIVE_INFINITY"
   132               "POSITIVE_INFINITY") t)
   133            "\\>")
   134           '(1 font-lock-constant-face))
   135 
   136     ;; Value properties of the Math object
   137     (list (concat
   138            "\\<Math\\."
   139            (regexp-opt
   140             '("E" "LN10" "LN2" "LOG2E" "LOG10E" "PI" "SQRT1_2" "SQRT2") t)
   141            "\\>")
   142           '(1 font-lock-constant-face))
   143     ))
   144   "Medium level highlighting for ECMAScript mode.")
   145 
   146 (defconst ecmascript-font-lock-keywords-3
   147   (append
   148    java-font-lock-keywords-3
   149    ecmascript-font-lock-keywords-2
   150    (list
   151 
   152     ;; Properties of the Date constructor
   153     '("\\<Date\\.\\(parse\\|UTC\\)\\>" 1 font-lock-builtin-face)
   154 
   155     ;; Function properties of the Math object
   156     (list (concat
   157            "\\<Math\\."
   158            (regexp-opt
   159             '("abs" "acos" "asin" "atan" "atan2" "ceil" "cos" "exp" "floor"
   160               "log" "max" "min" "pow" "random" "round" "sin" "sqrt" "tan") t)
   161            "\\>")
   162           '(1 font-lock-builtin-face))
   163 
   164     (list (regexp-opt
   165            '(;; URI handling function properties
   166              "decodeURI" "decodeURIComponent" "encodeURI" "encodeURIComponent"
   167              ;; Function properties of the global object
   168              "eval" "isFinite" "isNaN" "parseFloat" "parseInt") 'words)
   169           '(0 font-lock-builtin-face))
   170 
   171     (list (concat
   172            "\\."
   173            (regexp-opt
   174             '(;; Properties of the Object prototype object
   175               "hasOwnProperty" "isPrototypeOf" "propertyIsEnumerable"
   176               "toLocaleString" "toString" "valueOf"
   177               ;; Properties of the Function prototype object
   178               "apply" "call"
   179               ;; Properties of the Array prototype object
   180               "concat" "join" "pop" "push" "reverse" "shift" "slice" "sort"
   181               "splice" "unshift"
   182               ;; Properties of the String prototype object
   183               "charAt" "charCodeAt" "fromCharCode" "indexOf" "lastIndexOf"
   184               "localeCompare" "match" "replace" "search" "split" "substring"
   185               "toLocaleLowerCase" "toLocaleUpperCase" "toLowerCase"
   186               "toUpperCase"
   187               ;; Properties of the Number prototype object
   188               "toExponential" "toFixed" "toPrecision"
   189               ;; Properties of the Date prototype object
   190               "getDate" "getDay" "getFullYear" "getHours" "getMilliseconds"
   191               "getMinutes" "getMonth" "getSeconds" "getTime"
   192               "getTimezoneOffset" "getUTCDate" "getUTCDay" "getUTCFullYear"
   193               "getUTCHours" "getUTCMilliseconds" "getUTCMinutes" "getUTCMonth"
   194               "getUTCSeconds" "setDate" "setFullYear" "setHours"
   195               "setMilliseconds" "setMinutes" "setMonth" "setSeconds" "setTime"
   196               "setUTCDate" "setUTCFullYear" "setUTCHours" "setUTCMilliseconds"
   197               "setUTCMinutes" "setUTCMonth" "setUTCSeconds" "toDateString"
   198               "toLocaleDateString" "toLocaleString" "toLocaleTimeString"
   199               "toTimeString" "toUTCString"
   200               ;; Properties of the RegExp prototype object
   201               "exec" "test"
   202               ) t)
   203            "\\>")
   204           '(1 font-lock-builtin-face))
   205     ))
   206   "Gaudy level highlighting for ECMAScript mode.")
   207 
   208 (provide 'ecmascript-mode)
   209 
   210 ;;; ecmascript-mode.el ends here