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