-;;; coffee-mode.el --- Major mode to edit CoffeeScript files in Emacs
+;;; coffee-mode.el --- Major mode for CoffeeScript files
-;; Copyright (C) 2010 Chris Wanstrath
+;; Copyright (C) 2010-2013 Free Software Foundation, Inc.
-;; Version: 0.4.1
+;; Version: 0.4.1.1
;; Keywords: CoffeeScript major mode
;; Author: Chris Wanstrath <chris@ozmm.org>
;; URL: http://github.com/defunkt/coffee-mode
-;; This file is not part of GNU Emacs.
+;; This file is part of GNU Emacs.
-;; 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 2, or (at your option)
-;; any later version.
+;; GNU Emacs 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.
+;; GNU Emacs 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, write to the Free Software
-;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary
-;; For commentary please see the README.md or
-;; http://github.com/defunkt/coffee-mode#readme
+;; CoffeeScript mode is an Emacs major mode for [CoffeeScript][cs],
+;; unfancy JavaScript. It provides syntax highlighting, indentation
+;; support, imenu support, a menu bar, and a few cute commands.
-;;; Installation
+;; Installing this package enables CoffeeScript mode for file named
+;; *.coffee and Cakefile.
-;; In your shell:
+;; Commands:
-;; $ cd ~/.emacs.d/vendor
-;; $ git clone git://github.com/defunkt/coffee-mode.git
-
-;; In your emacs config:
-
-;; (add-to-list 'load-path "~/.emacs.d/vendor/coffee-mode")
-;; (require 'coffee-mode)
+;; M-x coffee-compile-file compiles the current file as a JavaScript
+;; file. Operating on "basic.coffee" and running this command will
+;; save a "basic.js" in the same directory. Subsequent runs will
+;; overwrite the file.
+;;
+;; If there are compilation errors and we the compiler have returned a
+;; line number to us for the first error, the point is moved to that
+;; line, so you can investigate. If this annoys you, you can set
+;; `coffee-compile-jump-to-error` to `nil`.
+;;
+;; M-x coffee-compile-buffer compiles the current buffer to JavaScript
+;; using the command specified by the `coffee-command` variable, and
+;; opens the contents in a new buffer using the mode configured for
+;; ".js" files.
+;;
+;; M-x coffee-compile-region compiles the selected region to
+;; JavaScript using the same configuration variables as
+;; `coffee-compile-buffer`.
+;;
+;; `C-c C-o C-s' (coffee-cos-mode) toggles a minor mode implementing
+;; "compile-on-save" behavior.
+;;
+;; M-x coffee-repl starts a repl via `coffee-command` in a new buffer.
-;;; Thanks
+;; Options:
+;;
+;; `coffee-tab-width' - Tab width to use when indenting.
+;; `coffee-command' - CoffeeScript command for evaluating code.
+;; Must be in your path.
+;; `coffee-args-repl' - Command line arguments for `coffee-command'
+;; when starting a REPL.
+;; `coffee-args-compile' - Arguments for `coffee-command'
+;; when compiling a file.
+;; `coffee-compiled-buffer-name' - Name of the scratch buffer used
+;; when compiling CoffeeScript.
+;; `coffee-compile-jump-to-error' - Whether to jump to the first error
+;; if compilation fails.
+
+;; Please file bugs at <http://github.com/defunkt/coffee-mode/issues>
+
+;; Thanks:
;; Major thanks to http://xahlee.org/emacs/elisp_syntax_coloring.html
;; the instructions.
:type 'hook
:group 'coffee)
-(defvar coffee-mode-map (make-keymap)
+(defvar coffee-mode-map
+ (let ((map (make-sparse-keymap)))
+ (define-key map (kbd "A-r") 'coffee-compile-buffer)
+ (define-key map (kbd "A-R") 'coffee-compile-region)
+ (define-key map (kbd "A-M-r") 'coffee-repl)
+ (define-key map "\C-m" 'coffee-newline-and-indent)
+ (define-key map "\C-c\C-o\C-s" 'coffee-cos-mode)
+ map)
"Keymap for CoffeeScript major mode.")
+(defvar coffee-mode-syntax-table
+ (let ((st (make-syntax-table)))
+ (modify-syntax-entry ?# "< b" st)
+ (modify-syntax-entry ?\n "> b" st)
+ (modify-syntax-entry ?' "\"" st)
+ st))
+
;;
;; Commands
;;
;; Helper Functions
;;
-(defun coffee-comment-dwim (arg)
- "Comment or uncomment current line or region in a smart way.
-For details, see `comment-dwim'."
- (interactive "*P")
- (require 'newcomment)
- (let ((deactivate-mark nil) (comment-start "#") (comment-end ""))
- (comment-dwim arg)))
-
(defun coffee-command-compile (file-name)
"Run `coffee-command' to compile FILE."
(let ((full-file-name (expand-file-name file-name)))
"Run `coffee-command' with the given arguments, and display the
output in a compilation buffer."
(interactive "sArguments: ")
- (let ((compilation-buffer-name-function (lambda (this-mode)
- (generate-new-buffer-name coffee-compiled-buffer-name))))
+ (let ((compilation-buffer-name-function
+ (lambda (_this-mode)
+ (generate-new-buffer-name coffee-compiled-buffer-name))))
(compile (concat coffee-command " " args))))
;;
(if (= (point) (point-at-bol))
(insert-tab)
(save-excursion
- (let ((prev-indent (coffee-previous-indent))
- (cur-indent (current-indentation)))
+ (let ((prev-indent (coffee-previous-indent)))
;; Shift one column to the left
(beginning-of-line)
(insert-tab)
;; Remember the current line indentation level,
;; insert a newline, and indent the newline to the same
;; level as the previous line.
- (let ((prev-indent (current-indentation)) (indent-next nil))
+ (let ((prev-indent (current-indentation)))
(delete-horizontal-space t)
(newline)
(insert-tab (/ prev-indent coffee-tab-width))
;; Define Major Mode
;;
+(unless (fboundp 'prog-mode) (defalias 'prog-mode 'fundamental-mode))
+
+(defvar electric-indent-inhibit)
+
;;;###autoload
-(define-derived-mode coffee-mode fundamental-mode
+(define-derived-mode coffee-mode prog-mode
"Coffee"
"Major mode for editing CoffeeScript."
- ;; key bindings
- (define-key coffee-mode-map (kbd "A-r") 'coffee-compile-buffer)
- (define-key coffee-mode-map (kbd "A-R") 'coffee-compile-region)
- (define-key coffee-mode-map (kbd "A-M-r") 'coffee-repl)
- (define-key coffee-mode-map [remap comment-dwim] 'coffee-comment-dwim)
- (define-key coffee-mode-map "\C-m" 'coffee-newline-and-indent)
- (define-key coffee-mode-map "\C-c\C-o\C-s" 'coffee-cos-mode)
-
;; code for syntax highlighting
(setq font-lock-defaults '((coffee-font-lock-keywords)))
;; perl style comment: "# ..."
- (modify-syntax-entry ?# "< b" coffee-mode-syntax-table)
- (modify-syntax-entry ?\n "> b" coffee-mode-syntax-table)
- (make-local-variable 'comment-start)
- (setq comment-start "#")
-
- ;; single quote strings
- (modify-syntax-entry ?' "\"" coffee-mode-syntax-table)
+ (set (make-local-variable 'comment-start) "#")
- ;; indentation
- (make-local-variable 'indent-line-function)
- (setq indent-line-function 'coffee-indent-line)
+ ;; Indentation.
+ (set (make-local-variable 'indent-line-function) 'coffee-indent-line)
(set (make-local-variable 'tab-width) coffee-tab-width)
+ ;; Because indentation is not redundant, we cannot safely reindent code.
+ (setq-local electric-indent-inhibit t)
;; imenu
(make-local-variable 'imenu-create-index-function)
(t
(remove-hook 'after-save-hook 'coffee-compile-file t))))
-(provide 'coffee-mode)
-
;;
;; On Load
;;
;; Run coffee-mode for files ending in .coffee.
;;;###autoload
-(add-to-list 'auto-mode-alist '("\\.coffee$" . coffee-mode))
+(add-to-list 'auto-mode-alist '("\\.coffee\\'" . coffee-mode))
;;;###autoload
(add-to-list 'auto-mode-alist '("Cakefile" . coffee-mode))
+
+(provide 'coffee-mode)
;;; coffee-mode.el ends here