From a059f5d3aaf52913097b5c400336b384bbbba8ca Mon Sep 17 00:00:00 2001 From: David Florness Date: Thu, 16 May 2019 11:09:04 -0600 Subject: [PATCH] Start JavaScript setup file --- init.el | 1 + lisp/setup-javascript.el | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 lisp/setup-javascript.el diff --git a/init.el b/init.el index 1f5f868..a8a5c1e 100644 --- a/init.el +++ b/init.el @@ -26,6 +26,7 @@ (load "~/.emacs.d/lisp/setup-lisp") (load "~/.emacs.d/lisp/setup-python") (load "~/.emacs.d/lisp/setup-go") +(load "~/.emacs.d/lisp/setup-javascript") ;;; Winner mode: allows for undoing and redoing of windoow configurations diff --git a/lisp/setup-javascript.el b/lisp/setup-javascript.el new file mode 100644 index 0000000..7213095 --- /dev/null +++ b/lisp/setup-javascript.el @@ -0,0 +1,36 @@ +(use-package js2-mode + :mode (("\\.m?js\\'" . js2-mode))) + +(defun react-inside-string-q () + "Returns non-nil if inside string, else nil. +Result depends on syntax table's string quote character." + (let ((result (nth 3 (syntax-ppss)))) + result)) + +(defun react-inside-comment-q () + "Returns non-nil if inside comment, else nil. +Result depends on syntax table's comment character." + (let ((result (nth 4 (syntax-ppss)))) + result)) + +(defun react-inside-string-or-comment-q () + "Return non-nil if point is inside string, documentation string or a comment. +If optional argument P is present, test this instead of point." + (or (react-inside-string-q) + (react-inside-comment-q))) + +(use-package rjsx-mode + :defer t + :init + (progn + ;; enable rjsx mode by using magic-mode-alist + (defun +javascript-jsx-file-p () + (and buffer-file-name + (or (equal (file-name-extension buffer-file-name) "js") + (equal (file-name-extension buffer-file-name) "jsx")) + (re-search-forward "\\(^\\s-*import React\\|\\( from \\|require(\\)[\"']react\\)" + magic-mode-regexp-match-limit t) + (progn (goto-char (match-beginning 1)) + (not (react-inside-string-or-comment-q))))) + + (add-to-list 'magic-mode-alist (cons #'+javascript-jsx-file-p 'rjsx-mode)))) -- 2.38.4