Skip to content

Instantly share code, notes, and snippets.

@sudarshang
Created July 8, 2017 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudarshang/f37d22d4c580080f5c103e2f6e4c8a31 to your computer and use it in GitHub Desktop.
Save sudarshang/f37d22d4c580080f5c103e2f6e4c8a31 to your computer and use it in GitHub Desktop.
-- typescript-mode-org.el 2017-07-08 07:28:40.804849539 -0500
;;; js-mode.el --- Major mode for editing js
;; -----------------------------------------------------------------------------------
;; Js support for Emacs
;; Unmodified original sourve available at http://www.karllandstrom.se/downloads/emacs/javascript.el
;; Copyright (c) 2008 Free Software Foundation
;; Portions Copyright (C) Microsoft Open Technologies, Inc. All rights reserved.
;;
;; This program 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 3 of the License, or
;; (at your option) any later version.
;;
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;; -------------------------------------------------------------------------------------------
;; Version: 0.1
;; Keywords: js languages
;; Package-Requires: ()
;; This file is not part of GNU Emacs.
;;; Commentary:
;; This is based on Karl Landstrom's barebones js-mode. This
;; The modifications to the original javascript.el mode mainly consisted in
;; replacing "javascript" with "js"
;; The main features of this js mode are syntactic
;; comments and C preprocessor fontification
;;
(require 'ido)
(require 'cl))
"Regexp matching the start of a js identifier, without grouping.")
"Regexp matching a js identifier, without grouping.")
"Regexp matching the start of a js object field.")
"Regexp matching a dot-separated sequence of js names.")
"Regexp matching an explicit js prototype \"method\" declaration.
"Regexp matching a js explicit prototype \"class\" declaration.
;; Parent class name(s) (yes, multiple inheritance in js) are
:framework js)
"List of js class definition styles.
"List of available js frameworks symbols.")
"Regexp matching the start of a js function header.
"Regexp matching a line in the js form \"var MUMBLE = function\".
'("any" "bool" "boolean" "break" "case" "catch" "class" "constructor"
"continue" "declare" "default" "delete" "do" "else"
"enum" "export" "extends" "extern" "false" "finally" "for"
"instanceof" "interface" "module" "new" "null" "number"
"private" "public" "return" "static" "string"
"super" "switch" "this" "throw" "true"
"try" "typeof" "var" "void"
"while" ))
"Regexp matching any js keyword.")
'("bool" "boolean" "string" "number" "any" "void"))
"Regular expression matching any predefined type in js.")
"Regular expression matching any future reserved words in js.")
"Customization variables for js mode."
:tag "js"
in js mode."
(defcustom js-mode-hook nil
"*Hook called by `js-mode'."
:type 'hook
(when (memq 'js js-enabled-frameworks)
"Move forward over a whole js expression.
"Move forward over a js function declaration.
"Return the start of the js function prologue containing POS.
"Split a js name into its dot-separated parts.
"Guess the name of the js function at POSITION.
"Move forward over a js destructuring spec.
"Parse the js program state at point.
"Return the js syntactic context corresponding to PSTATE."
"Return the js syntactic context at point.
;; XXX: js can continue a regexp literal across lines so long
"[=(,:]\\(?:\\s-\\|\n\\)*\\(/\\)\\(?:\\\\/\\|[^/*]\\)\\(?:\\\\/\\|[^/]\\)*\\(/\\)"
"Regexp matching a js regular expression literal.
"Syntactic font lock keywords matching regexps in js.
"Return non-nil if point is on a js operator, other than a comma."
"Indent the current line as js."
;; Dynamically replace functions using the lexically scoped cl-letf.
;; See below for more details:
;; http://endlessparentheses.com/understanding-letf-and-how-it-replaces-flet.html
(cl-letf (((symbol-function 'c-forward-sws)
(lambda (&optional limit)
(js--forward-syntactic-ws limit)))
((symbol-function 'c-backward-sws)
(lambda (&optional limit)
(js--backward-syntactic-ws limit)))
((symbol-function 'c-beginning-of-macro)
(lambda (&optional limit)
(js--beginning-of-macro limit))))
;;; Main Function
(defalias 'js-parent-mode
(if (fboundp 'prog-mode) 'prog-mode 'fundamental-mode))
(define-derived-mode js-mode js-parent-mode "js"
"Major mode for editing js.
(setq mode-name "js")
(let (font-lock-keywords) ; leaves syntactic keywords intact
;; Avoid byte-compilation errors. `font-lock-fontify-buffer' is
;; marked as interactive only in Emacs 25.
(with-no-warnings
(font-lock-fontify-buffer)))
(run-mode-hooks 'js-mode-hook))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.ts$" . js-mode))
(provide 'js-mode)
;;; js-mode.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment