/.emacs.d/el-get/nxhtml/alts/javascript-mozlab.el

https://bitbucket.org/shuangxinyu/emacspack · Lisp · 712 lines · 510 code · 114 blank · 88 comment · 18 complexity · 4ae779ed6d47333797bf97babdd76a3a MD5 · raw file

  1. ;;; javascript.el --- Major mode for editing JavaScript source text
  2. ;; Copyright (C) 2006 Karl Landstrรถm
  3. ;; Author: Karl Landstrรถm <kland@comhem.se>
  4. ;; Maintainer: Karl Landstrรถm <kland@comhem.se>
  5. ;; Version: 2.0 Beta 8
  6. ;; Date: 2006-12-26
  7. ;; Keywords: languages, oop
  8. ;; This file 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 2, or (at your option)
  11. ;; any later version.
  12. ;; This file 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; see the file COPYING. If not, write to
  18. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. ;; Boston, MA 02111-1307, USA.
  20. ;;; Commentary:
  21. ;;
  22. ;; The main features of this JavaScript mode are syntactic
  23. ;; highlighting (enabled with `font-lock-mode' or
  24. ;; `global-font-lock-mode'), automatic indentation and filling of
  25. ;; comments.
  26. ;;
  27. ;; This package has (only) been tested with GNU Emacs 21.4 (the latest
  28. ;; stable release).
  29. ;;
  30. ;; Installation:
  31. ;;
  32. ;; Put this file in a directory where Emacs can find it (`C-h v
  33. ;; load-path' for more info). Then add the following lines to your
  34. ;; Emacs initialization file:
  35. ;;
  36. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . javascript-mode))
  37. ;; (autoload 'javascript-mode "javascript" nil t)
  38. ;;
  39. ;; General Remarks:
  40. ;;
  41. ;; This mode assumes that block comments are not nested inside block
  42. ;; comments and that strings do not contain line breaks.
  43. ;;
  44. ;; Exported names start with "javascript-" whereas private names start
  45. ;; with "js-".
  46. ;;
  47. ;; Changes:
  48. ;;
  49. ;; See javascript.el.changelog.
  50. ;;; Code:
  51. (require 'cc-mode)
  52. (require 'font-lock)
  53. (require 'newcomment)
  54. (defgroup javascript nil
  55. "Customization variables for `javascript-mode'."
  56. :tag "JavaScript"
  57. :group 'languages)
  58. (defcustom javascript-indent-level 4
  59. "Number of spaces for each indentation step."
  60. :type 'integer
  61. :group 'javascript)
  62. (defcustom javascript-auto-indent-flag t
  63. "Automatic indentation with punctuation characters. If non-nil, the
  64. current line is indented when certain punctuations are inserted."
  65. :type 'boolean
  66. :group 'javascript)
  67. ;; --- Keymap ---
  68. (defvar javascript-mode-map nil
  69. "Keymap used in JavaScript mode.")
  70. (unless javascript-mode-map
  71. (setq javascript-mode-map (make-sparse-keymap)))
  72. (when javascript-auto-indent-flag
  73. (mapc (lambda (key)
  74. (define-key javascript-mode-map key 'javascript-insert-and-indent))
  75. '("{" "}" "(" ")" ":" ";" ",")))
  76. (defun javascript-insert-and-indent (key)
  77. "Run command bound to key and indent current line. Runs the command
  78. bound to KEY in the global keymap and indents the current line."
  79. (interactive (list (this-command-keys)))
  80. (call-interactively (lookup-key (current-global-map) key))
  81. (indent-according-to-mode))
  82. ;; --- Syntax Table And Parsing ---
  83. (defvar javascript-mode-syntax-table
  84. (let ((table (make-syntax-table)))
  85. (c-populate-syntax-table table)
  86. ;; The syntax class of underscore should really be `symbol' ("_")
  87. ;; but that makes matching of tokens much more complex as e.g.
  88. ;; "\\<xyz\\>" matches part of e.g. "_xyz" and "xyz_abc". Defines
  89. ;; it as word constituent for now.
  90. (modify-syntax-entry ?_ "w" table)
  91. table)
  92. "Syntax table used in JavaScript mode.")
  93. (defun js-re-search-forward-inner (regexp &optional bound count)
  94. "Auxiliary function for `js-re-search-forward'."
  95. (let ((parse)
  96. (saved-point (point-min)))
  97. (while (> count 0)
  98. (re-search-forward regexp bound)
  99. (setq parse (parse-partial-sexp saved-point (point)))
  100. (cond ((nth 3 parse)
  101. (re-search-forward
  102. (concat "\\([^\\]\\|^\\)" (string (nth 3 parse)))
  103. (save-excursion (end-of-line) (point)) t))
  104. ((nth 7 parse)
  105. (forward-line))
  106. ((or (nth 4 parse)
  107. (and (eq (char-before) ?\/) (eq (char-after) ?\*)))
  108. (re-search-forward "\\*/"))
  109. (t
  110. (setq count (1- count))))
  111. (setq saved-point (point))))
  112. (point))
  113. (defun js-re-search-forward (regexp &optional bound noerror count)
  114. "Search forward but ignore strings and comments. Invokes
  115. `re-search-forward' but treats the buffer as if strings and
  116. comments have been removed."
  117. (let ((saved-point (point))
  118. (search-expr
  119. (cond ((null count)
  120. '(js-re-search-forward-inner regexp bound 1))
  121. ((< count 0)
  122. '(js-re-search-backward-inner regexp bound (- count)))
  123. ((> count 0)
  124. '(js-re-search-forward-inner regexp bound count)))))
  125. (condition-case err
  126. (eval search-expr)
  127. (search-failed
  128. (goto-char saved-point)
  129. (unless noerror
  130. (error (error-message-string err)))))))
  131. (defun js-re-search-backward-inner (regexp &optional bound count)
  132. "Auxiliary function for `js-re-search-backward'."
  133. (let ((parse)
  134. (saved-point (point-min)))
  135. (while (> count 0)
  136. (re-search-backward regexp bound)
  137. (when (and (> (point) (point-min))
  138. (save-excursion (backward-char) (looking-at "/[/*]")))
  139. (forward-char))
  140. (setq parse (parse-partial-sexp saved-point (point)))
  141. (cond ((nth 3 parse)
  142. (re-search-backward
  143. (concat "\\([^\\]\\|^\\)" (string (nth 3 parse)))
  144. (save-excursion (beginning-of-line) (point)) t))
  145. ((nth 7 parse)
  146. (goto-char (nth 8 parse)))
  147. ((or (nth 4 parse)
  148. (and (eq (char-before) ?/) (eq (char-after) ?*)))
  149. (re-search-backward "/\\*"))
  150. (t
  151. (setq count (1- count))))))
  152. (point))
  153. (defun js-re-search-backward (regexp &optional bound noerror count)
  154. "Search backward but ignore strings and comments. Invokes
  155. `re-search-backward' but treats the buffer as if strings and
  156. comments have been removed."
  157. (let ((saved-point (point))
  158. (search-expr
  159. (cond ((null count)
  160. '(js-re-search-backward-inner regexp bound 1))
  161. ((< count 0)
  162. '(js-re-search-forward-inner regexp bound (- count)))
  163. ((> count 0)
  164. '(js-re-search-backward-inner regexp bound count)))))
  165. (condition-case err
  166. (eval search-expr)
  167. (search-failed
  168. (goto-char saved-point)
  169. (unless noerror
  170. (error (error-message-string err)))))))
  171. (defun js-continued-var-decl-list-p ()
  172. "Return non-nil if point is inside a continued variable declaration
  173. list."
  174. (interactive)
  175. (let ((start (save-excursion (js-re-search-backward "\\<var\\>" nil t))))
  176. (and start
  177. (save-excursion (re-search-backward "\n" start t))
  178. (not (save-excursion
  179. (js-re-search-backward
  180. ";\\|[^, \t][ \t]*\\(/[/*]\\|$\\)" start t))))))
  181. ;; --- Font Lock ---
  182. (defun js-inside-param-list-p ()
  183. "Return non-nil if point is inside a function parameter list."
  184. (condition-case err
  185. (save-excursion
  186. (up-list -1)
  187. (and (looking-at "(")
  188. (progn (backward-word 1)
  189. (or (looking-at "function")
  190. (progn (backward-word 1) (looking-at "function"))))))
  191. (error nil)))
  192. (defconst js-function-heading-1-re
  193. "^[ \t]*function[ \t]+\\(\\w+\\)"
  194. "Regular expression matching the start of a function header.")
  195. (defconst js-function-heading-2-re
  196. "^[ \t]*\\(\\w+\\)[ \t]*:[ \t]*function\\>"
  197. "Regular expression matching the start of a function entry in
  198. an associative array.")
  199. (defconst js-keyword-re
  200. (regexp-opt '("abstract" "break" "case" "catch" "class" "const"
  201. "continue" "debugger" "default" "delete" "do" "else"
  202. "enum" "export" "extends" "final" "finally" "for"
  203. "function" "goto" "if" "implements" "import" "in"
  204. "instanceof" "interface" "native" "new" "package"
  205. "private" "protected" "public" "return" "static"
  206. "super" "switch" "synchronized" "this" "throw"
  207. "throws" "transient" "try" "typeof" "var" "void"
  208. "volatile" "while" "with"
  209. "let") 'words)
  210. "Regular expression matching any JavaScript keyword.")
  211. (defconst js-basic-type-re
  212. (regexp-opt '("boolean" "byte" "char" "double" "float" "int" "long"
  213. "short" "void") 'words)
  214. "Regular expression matching any predefined type in JavaScript.")
  215. (defconst js-constant-re
  216. (regexp-opt '("false" "null" "true") 'words)
  217. "Regular expression matching any future reserved words in JavaScript.")
  218. (defconst js-font-lock-keywords-1
  219. (list
  220. "\\<import\\>"
  221. (list js-function-heading-1-re 1 font-lock-function-name-face)
  222. (list js-function-heading-2-re 1 font-lock-function-name-face)
  223. (list "[=(][ \t]*\\(/.*?[^\\]/\\w*\\)" 1 font-lock-string-face))
  224. "Level one font lock.")
  225. (defconst js-font-lock-keywords-2
  226. (append js-font-lock-keywords-1
  227. (list (list js-keyword-re 1 font-lock-keyword-face)
  228. (cons js-basic-type-re font-lock-type-face)
  229. (cons js-constant-re font-lock-constant-face)))
  230. "Level two font lock.")
  231. ;; Limitations with variable declarations: There seems to be no
  232. ;; sensible way to highlight variables occuring after an initialized
  233. ;; variable in a variable list. For instance, in
  234. ;;
  235. ;; var x, y = f(a, b), z
  236. ;;
  237. ;; z will not be highlighted.
  238. (defconst js-font-lock-keywords-3
  239. (append
  240. js-font-lock-keywords-2
  241. (list
  242. ;; variable declarations
  243. (list
  244. (concat "\\<\\(const\\|var\\)\\>\\|" js-basic-type-re)
  245. (list "\\(\\w+\\)[ \t]*\\([=;].*\\|,\\|/[/*]\\|$\\)"
  246. nil
  247. nil
  248. '(1 font-lock-variable-name-face)))
  249. ;; continued variable declaration list
  250. (list
  251. (concat "^[ \t]*\\w+[ \t]*\\([,;=]\\|/[/*]\\|$\\)")
  252. (list "\\(\\w+\\)[ \t]*\\([=;].*\\|,\\|/[/*]\\|$\\)"
  253. '(if (save-excursion (backward-char) (js-continued-var-decl-list-p))
  254. (backward-word 1)
  255. (end-of-line))
  256. '(end-of-line)
  257. '(1 font-lock-variable-name-face)))
  258. ;; formal parameters
  259. (list
  260. (concat "\\<function\\>\\([ \t]+\\w+\\)?[ \t]*([ \t]*\\w")
  261. (list "\\(\\w+\\)\\([ \t]*).*\\)?"
  262. '(backward-char)
  263. '(end-of-line)
  264. '(1 font-lock-variable-name-face)))
  265. ;; continued formal parameter list
  266. (list
  267. (concat "^[ \t]*\\w+[ \t]*[,)]")
  268. (list "\\w+"
  269. '(if (save-excursion (backward-char) (js-inside-param-list-p))
  270. (backward-word 1)
  271. (end-of-line))
  272. '(end-of-line)
  273. '(0 font-lock-variable-name-face)))))
  274. "Level three font lock.")
  275. (defconst js-font-lock-keywords
  276. '(js-font-lock-keywords-3 js-font-lock-keywords-1 js-font-lock-keywords-2
  277. js-font-lock-keywords-3)
  278. "See `font-lock-keywords'.")
  279. ;; --- Indentation ---
  280. (defconst js-possibly-braceless-keyword-re
  281. (regexp-opt
  282. '("catch" "do" "else" "finally" "for" "if" "try" "while" "with" "let")
  283. 'words)
  284. "Regular expression matching keywords that are optionally
  285. followed by an opening brace.")
  286. (defconst js-indent-operator-re
  287. (concat "[-+*/%<>=&^|?:.]\\([^-+*/]\\|$\\)\\|"
  288. (regexp-opt '("in" "instanceof") 'words))
  289. "Regular expression matching operators that affect indentation
  290. of continued expressions.")
  291. (defun js-looking-at-operator-p ()
  292. "Return non-nil if text after point is an operator (that is not
  293. a comma)."
  294. (save-match-data
  295. (and (looking-at js-indent-operator-re)
  296. (or (not (looking-at ":"))
  297. (save-excursion
  298. (and (js-re-search-backward "[?:{]\\|\\<case\\>" nil t)
  299. (looking-at "?")))))))
  300. (defun js-continued-expression-p ()
  301. "Returns non-nil if the current line continues an expression."
  302. (save-excursion
  303. (back-to-indentation)
  304. (or (js-looking-at-operator-p)
  305. (and (js-re-search-backward "\n" nil t)
  306. (progn
  307. (skip-chars-backward " \t")
  308. (backward-char)
  309. (and (> (point) (point-min))
  310. (save-excursion (backward-char) (not (looking-at "[/*]/")))
  311. (js-looking-at-operator-p)
  312. (and (progn (backward-char)
  313. (not (looking-at "++\\|--\\|/[/*]"))))))))))
  314. (defun js-end-of-do-while-loop-p ()
  315. "Returns non-nil if word after point is `while' of a do-while
  316. statement, else returns nil. A braceless do-while statement
  317. spanning several lines requires that the start of the loop is
  318. indented to the same column as the current line."
  319. (interactive)
  320. (save-excursion
  321. (save-match-data
  322. (when (looking-at "\\s-*\\<while\\>")
  323. (if (save-excursion
  324. (skip-chars-backward "[ \t\n]*}")
  325. (looking-at "[ \t\n]*}"))
  326. (save-excursion
  327. (backward-list) (backward-word 1) (looking-at "\\<do\\>"))
  328. (js-re-search-backward "\\<do\\>" (point-at-bol) t)
  329. (or (looking-at "\\<do\\>")
  330. (let ((saved-indent (current-indentation)))
  331. (while (and (js-re-search-backward "^[ \t]*\\<" nil t)
  332. (/= (current-indentation) saved-indent)))
  333. (and (looking-at "[ \t]*\\<do\\>")
  334. (not (js-re-search-forward
  335. "\\<while\\>" (point-at-eol) t))
  336. (= (current-indentation) saved-indent)))))))))
  337. (defun js-ctrl-statement-indentation ()
  338. "Returns the proper indentation of the current line if it
  339. starts the body of a control statement without braces, else
  340. returns nil."
  341. (save-excursion
  342. (back-to-indentation)
  343. (when (save-excursion
  344. (and (not (looking-at "[{]"))
  345. (progn
  346. (js-re-search-backward "[[:graph:]]" nil t)
  347. (forward-char)
  348. (when (= (char-before) ?\)) (backward-list))
  349. (skip-syntax-backward " ")
  350. (skip-syntax-backward "w")
  351. (looking-at js-possibly-braceless-keyword-re))
  352. (not (js-end-of-do-while-loop-p))))
  353. (save-excursion
  354. (goto-char (match-beginning 0))
  355. (+ (current-indentation) javascript-indent-level)))))
  356. (defun js-proper-indentation (parse-status)
  357. "Return the proper indentation for the current line."
  358. (save-excursion
  359. (back-to-indentation)
  360. (let ((ctrl-stmt-indent (js-ctrl-statement-indentation))
  361. (same-indent-p (looking-at "[]})]\\|\\<case\\>\\|\\<default\\>"))
  362. (continued-expr-p (js-continued-expression-p)))
  363. (cond (ctrl-stmt-indent)
  364. ((js-continued-var-decl-list-p)
  365. (js-re-search-backward "\\<var\\>" nil t)
  366. (+ (current-indentation) javascript-indent-level))
  367. ((nth 1 parse-status)
  368. (goto-char (nth 1 parse-status))
  369. (if (looking-at "[({[][ \t]*\\(/[/*]\\|$\\)")
  370. (progn
  371. (skip-syntax-backward " ")
  372. (when (= (char-before) ?\)) (backward-list))
  373. (back-to-indentation)
  374. (cond (same-indent-p
  375. (current-column))
  376. (continued-expr-p
  377. (+ (current-column) (* 2 javascript-indent-level)))
  378. (t
  379. (+ (current-column) javascript-indent-level))))
  380. (unless same-indent-p
  381. (forward-char)
  382. (skip-chars-forward " \t"))
  383. (current-column)))
  384. (continued-expr-p javascript-indent-level)
  385. (t 0)))))
  386. (defun javascript-indent-line ()
  387. "Indent the current line as JavaScript source text."
  388. (interactive)
  389. (let ((parse-status
  390. (save-excursion (parse-partial-sexp (point-min) (point-at-bol))))
  391. (offset (- (current-column) (current-indentation))))
  392. (when (not (nth 8 parse-status))
  393. (indent-line-to (js-proper-indentation parse-status))
  394. (when (> offset 0) (forward-char offset)))))
  395. ;; --- Filling ---
  396. ;; FIXME: It should be possible to use the more sofisticated function
  397. ;; `c-fill-paragraph' in `cc-cmds.el' instead. However, just setting
  398. ;; `fill-paragraph-function' to `c-fill-paragraph' does not work;
  399. ;; inside `c-fill-paragraph', `fill-paragraph-function' evaluates to
  400. ;; nil!?
  401. (defun js-backward-paragraph ()
  402. "Move backward to start of paragraph. Postcondition: Point is at
  403. beginning of buffer or the previous line contains only whitespace."
  404. (forward-line -1)
  405. (while (not (or (bobp) (looking-at "^[ \t]*$")))
  406. (forward-line -1))
  407. (when (not (bobp)) (forward-line 1)))
  408. (defun js-forward-paragraph ()
  409. "Move forward to end of paragraph. Postcondition: Point is at
  410. end of buffer or the next line contains only whitespace."
  411. (forward-line 1)
  412. (while (not (or (eobp) (looking-at "^[ \t]*$")))
  413. (forward-line 1))
  414. (when (not (eobp)) (backward-char 1)))
  415. (defun js-fill-block-comment-paragraph (parse-status justify)
  416. "Fill current paragraph as a block comment. PARSE-STATUS is the
  417. result of `parse-partial-regexp' from beginning of buffer to
  418. point. JUSTIFY has the same meaning as in `fill-paragraph'."
  419. (let ((offset (save-excursion
  420. (goto-char (nth 8 parse-status)) (current-indentation))))
  421. (save-excursion
  422. (save-restriction
  423. (narrow-to-region (save-excursion
  424. (goto-char (nth 8 parse-status)) (point-at-bol))
  425. (save-excursion
  426. (goto-char (nth 8 parse-status))
  427. (re-search-forward "*/")))
  428. (narrow-to-region (save-excursion
  429. (js-backward-paragraph)
  430. (when (looking-at "^[ \t]*$") (forward-line 1))
  431. (point))
  432. (save-excursion
  433. (js-forward-paragraph)
  434. (when (looking-at "^[ \t]*$") (backward-char))
  435. (point)))
  436. (goto-char (point-min))
  437. (while (not (eobp))
  438. (delete-horizontal-space)
  439. (forward-line 1))
  440. (let ((fill-column (- fill-column offset))
  441. (fill-paragraph-function nil))
  442. (fill-paragraph justify))
  443. ;; In Emacs 21.4 as opposed to CVS Emacs 22,
  444. ;; `fill-paragraph' seems toadd a newline at the end of the
  445. ;; paragraph. Remove it!
  446. (goto-char (point-max))
  447. (when (looking-at "^$") (backward-delete-char 1))
  448. (goto-char (point-min))
  449. (while (not (eobp))
  450. (indent-to offset)
  451. (forward-line 1))))))
  452. (defun js-sline-comment-par-start ()
  453. "Return point at the beginning of the line where the current
  454. single-line comment paragraph starts."
  455. (save-excursion
  456. (beginning-of-line)
  457. (while (and (not (bobp))
  458. (looking-at "^[ \t]*//[ \t]*[[:graph:]]"))
  459. (forward-line -1))
  460. (unless (bobp) (forward-line 1))
  461. (point)))
  462. (defun js-sline-comment-par-end ()
  463. "Return point at end of current single-line comment paragraph."
  464. (save-excursion
  465. (beginning-of-line)
  466. (while (and (not (eobp))
  467. (looking-at "^[ \t]*//[ \t]*[[:graph:]]"))
  468. (forward-line 1))
  469. (unless (bobp) (backward-char))
  470. (point)))
  471. (defun js-sline-comment-offset (line)
  472. "Return the column at the start of the current single-line
  473. comment paragraph."
  474. (save-excursion
  475. (goto-line line)
  476. (re-search-forward "//" (point-at-eol))
  477. (goto-char (match-beginning 0))
  478. (current-column)))
  479. (defun js-sline-comment-text-offset (line)
  480. "Return the column at the start of the text of the current
  481. single-line comment paragraph."
  482. (save-excursion
  483. (goto-line line)
  484. (re-search-forward "//[ \t]*" (point-at-eol))
  485. (current-column)))
  486. (defun js-at-empty-sline-comment-p ()
  487. "Return non-nil if inside an empty single-line comment."
  488. (and (save-excursion
  489. (beginning-of-line)
  490. (not (looking-at "^.*//.*[[:graph:]]")))
  491. (save-excursion
  492. (re-search-backward "//" (point-at-bol) t))))
  493. (defun js-fill-sline-comments (parse-status justify)
  494. "Fill current paragraph as a sequence of single-line comments.
  495. PARSE-STATUS is the result of `parse-partial-regexp' from
  496. beginning of buffer to point. JUSTIFY has the same meaning as in
  497. `fill-paragraph'."
  498. (when (not (js-at-empty-sline-comment-p))
  499. (let* ((start (js-sline-comment-par-start))
  500. (start-line (1+ (count-lines (point-min) start)))
  501. (end (js-sline-comment-par-end))
  502. (offset (js-sline-comment-offset start-line))
  503. (text-offset (js-sline-comment-text-offset start-line)))
  504. (save-excursion
  505. (save-restriction
  506. (narrow-to-region start end)
  507. (goto-char (point-min))
  508. (while (re-search-forward "^[ \t]*//[ \t]*" nil t)
  509. (replace-match "")
  510. (forward-line 1))
  511. (let ((fill-paragraph-function nil)
  512. (fill-column (- fill-column text-offset)))
  513. (fill-paragraph justify))
  514. ;; In Emacs 21.4 as opposed to CVS Emacs 22,
  515. ;; `fill-paragraph' seems toadd a newline at the end of the
  516. ;; paragraph. Remove it!
  517. (goto-char (point-max))
  518. (when (looking-at "^$") (backward-delete-char 1))
  519. (goto-char (point-min))
  520. (while (not (eobp))
  521. (indent-to offset)
  522. (insert "//")
  523. (indent-to text-offset)
  524. (forward-line 1)))))))
  525. (defun js-trailing-comment-p (parse-status)
  526. "Return non-nil if inside a trailing comment. PARSE-STATUS is
  527. the result of `parse-partial-regexp' from beginning of buffer to
  528. point."
  529. (save-excursion
  530. (when (nth 4 parse-status)
  531. (goto-char (nth 8 parse-status))
  532. (skip-chars-backward " \t")
  533. (not (bolp)))))
  534. (defun js-block-comment-p (parse-status)
  535. "Return non-nil if inside a block comment. PARSE-STATUS is the
  536. result of `parse-partial-regexp' from beginning of buffer to
  537. point."
  538. (save-excursion
  539. (save-match-data
  540. (when (nth 4 parse-status)
  541. (goto-char (nth 8 parse-status))
  542. (looking-at "/\\*")))))
  543. (defun javascript-fill-paragraph (&optional justify)
  544. "If inside a comment, fill the current comment paragraph.
  545. Trailing comments are ignored."
  546. (interactive)
  547. (let ((parse-status (parse-partial-sexp (point-min) (point))))
  548. (when (and (nth 4 parse-status)
  549. (not (js-trailing-comment-p parse-status)))
  550. (if (js-block-comment-p parse-status)
  551. (js-fill-block-comment-paragraph parse-status justify)
  552. (js-fill-sline-comments parse-status justify))))
  553. t)
  554. ;; --- Imenu ---
  555. (defconst js-imenu-generic-expression
  556. (list
  557. (list
  558. nil
  559. "function\\s-+\\(\\w+\\)\\s-*("
  560. 1))
  561. "Regular expression matching top level procedures. Used by imenu.")
  562. ;; --- Main Function ---
  563. ;;;###autoload
  564. (defun javascript-mode ()
  565. "Major mode for editing JavaScript source text.
  566. Key bindings:
  567. \\{javascript-mode-map}"
  568. (interactive)
  569. (kill-all-local-variables)
  570. (use-local-map javascript-mode-map)
  571. (set-syntax-table javascript-mode-syntax-table)
  572. (set (make-local-variable 'indent-line-function) 'javascript-indent-line)
  573. (set (make-local-variable 'font-lock-defaults) (list js-font-lock-keywords))
  574. (set (make-local-variable 'parse-sexp-ignore-comments) t)
  575. ;; Comments
  576. (setq comment-start "// ")
  577. (setq comment-end "")
  578. (set (make-local-variable 'fill-paragraph-function)
  579. 'javascript-fill-paragraph)
  580. ;; Make c-mark-function work
  581. (setq c-nonsymbol-token-regexp "!=\\|%=\\|&[&=]\\|\\*[/=]\\|\\+[+=]\\|-[=-]\\|/[*/=]\\|<\\(?:<=\\|[<=]\\)\\|==\\|>\\(?:>\\(?:>=\\|[=>]\\)\\|[=>]\\)\\|\\^=\\||[=|]\\|[]!%&(-,./:-?[{-~^-]"
  582. c-stmt-delim-chars "^;{}?:"
  583. c-syntactic-ws-end "[ \n
  584. \f/]"
  585. c-syntactic-eol "\\(\\s \\|/\\*\\([^*\n
  586. ]\\|\\*[^/\n
  587. ]\\)*\\*/\\)*\\(\\(/\\*\\([^*\n
  588. ]\\|\\*[^/\n
  589. ]\\)*\\|\\\\\\)?$\\|//\\)")
  590. ;; Imenu
  591. (setq imenu-case-fold-search nil)
  592. (set (make-local-variable 'imenu-generic-expression)
  593. js-imenu-generic-expression)
  594. (setq major-mode 'javascript-mode)
  595. (setq mode-name "JavaScript")
  596. (run-hooks 'javascript-mode-hook))
  597. (provide 'javascript-mode)
  598. ;;; javascript.el ends here