;;; Code:
-(eval-when-compile (require 'cl))
-
(defgroup ruby nil
"Major mode for editing Ruby code."
:prefix "ruby-"
:group 'ruby)
(defcustom ruby-insert-encoding-magic-comment t
- "Insert a magic Emacs 'coding' comment upon save if this is non-nil."
+ "Insert a magic Ruby encoding comment upon save if this is non-nil.
+The encoding will be auto-detected. The format of the encoding comment
+is customizable via `ruby-encoding-magic-comment-style'.
+
+When set to `always-utf8' an utf-8 comment will always be added,
+even if it's not required."
:type 'boolean :group 'ruby)
(defcustom ruby-encoding-magic-comment-style 'ruby
("unless" insts "end")
("if" if-body "end")
("case" cases "end"))
- (formal-params ("opening-|" exp "|"))
+ (formal-params ("opening-|" exp "closing-|"))
(for-body (for-head ";" insts))
(for-head (id "in" exp))
- (cases (exp "then" insts) ;; FIXME: Ruby also allows (exp ":" insts).
+ (cases (exp "then" insts)
(cases "when" cases) (insts "else" insts))
(expseq (exp) );;(expseq "," expseq)
(hashvals (id "=>" exp1) (hashvals "," hashvals))
(left ".." "...")
(left "+" "-")
(left "*" "/" "%" "**")
- ;; (left "|") ; FIXME: Conflicts with | after block parameters.
(left "&&" "||")
- (left "^" "&")
+ (left "^" "&" "|")
(nonassoc "<=>")
(nonassoc ">" ">=" "<" "<=")
(nonassoc "==" "===" "!=")
(skip-chars-backward " \t")
(not (or (bolp)
(and (memq (char-before)
- '(?\; ?- ?+ ?* ?/ ?: ?. ?, ?\[ ?\( ?\{ ?\\ ?& ?> ?< ?% ?~ ?^))
+ '(?\; ?- ?+ ?* ?/ ?: ?. ?, ?\[ ?\( ?\{ ?\\ ?& ?> ?< ?%
+ ?~ ?^))
;; Make sure it's not the end of a regexp.
(not (eq (car (syntax-after (1- (point)))) 7)))
(and (eq (char-before) ?\?)
(and (eq (char-before) ?=)
(string-match "\\`\\s." (save-excursion
(ruby-smie--backward-token))))
+ (and (eq (char-before) ?|)
+ (member (save-excursion (ruby-smie--backward-token))
+ '("|" "||")))
(and (eq (car (syntax-after (1- (point)))) 2)
(member (save-excursion (ruby-smie--backward-token))
'("iuwu-mod" "and" "or")))
(or (eq ?\{ (char-before))
(looking-back "\\_<do" (- (point) 2)))))
+(defun ruby-smie--closing-pipe-p ()
+ (save-excursion
+ (if (eq ?| (char-before)) (forward-char -1))
+ (and (re-search-backward "|" (line-beginning-position) t)
+ (ruby-smie--opening-pipe-p))))
+
(defun ruby-smie--args-separator-p (pos)
(and
(< pos (line-end-position))
(save-excursion
(goto-char pos)
(or (and (eq (char-syntax (char-after)) ?w)
- (not (looking-at (regexp-opt '("unless" "if" "while" "until"
- "else" "elsif" "do" "end" "and" "or")
+ (not (looking-at (regexp-opt '("unless" "if" "while" "until" "or"
+ "else" "elsif" "do" "end" "and")
'symbols))))
(memq (syntax-after pos) '(7 15))
(looking-at "[([]\\|[-+!~:]\\sw")))))
((string-match-p "\\`|[*&]?\\'" tok)
(forward-char (- 1 (length tok)))
(setq tok "|")
- (if (ruby-smie--opening-pipe-p) "opening-|" tok))
+ (cond
+ ((ruby-smie--opening-pipe-p) "opening-|")
+ ((ruby-smie--closing-pipe-p) "closing-|")
+ (t tok)))
((and (equal tok "") (looking-at "\\\\\n"))
(goto-char (match-end 0)) (ruby-smie--forward-token))
((equal tok "do")
(if (ruby-smie--bosp)
tok "iuwu-mod"))
((equal tok "|")
- (if (ruby-smie--opening-pipe-p) "opening-|" tok))
+ (cond
+ ((ruby-smie--opening-pipe-p) "opening-|")
+ ((ruby-smie--closing-pipe-p) "closing-|")
+ (t tok)))
((string-match-p "\\`|[*&]\\'" tok)
(forward-char 1)
(substring tok 1))
(if (not (smie-rule-sibling-p)) 0)) ;; ruby-indent-level
(`(:after . ,(or "=" "iuwu-mod" "+" "-" "*" "/" "&&" "||" "%" "**" "^" "&"
"<=>" ">" "<" ">=" "<=" "==" "===" "!=" "<<" ">>"
- "+=" "-=" "*=" "/=" "%=" "**=" "&=" "|=" "^="
+ "+=" "-=" "*=" "/=" "%=" "**=" "&=" "|=" "^=" "|"
"<<=" ">>=" "&&=" "||=" "and" "or"))
(if (smie-rule-parent-p ";" nil) ruby-indent-level))
+ (`(:before . "begin")
+ (unless (save-excursion (skip-chars-backward " \t") (bolp))
+ (smie-rule-parent)))
))
(defun ruby-imenu-create-index-in-block (prefix beg end)
(setq-local paragraph-separate paragraph-start)
(setq-local paragraph-ignore-fill-prefix t))
+(defun ruby--insert-coding-comment (encoding)
+ "Insert a magic coding comment for ENCODING.
+The style of the comment is controlled by `ruby-encoding-magic-comment-style'."
+ (let ((encoding-magic-comment-template
+ (pcase ruby-encoding-magic-comment-style
+ (`ruby "# coding: %s")
+ (`emacs "# -*- coding: %s -*-")
+ (`custom
+ ruby-custom-encoding-magic-comment-template))))
+ (insert
+ (format encoding-magic-comment-template encoding)
+ "\n")))
+
+(defun ruby--detect-encoding ()
+ (if (eq ruby-insert-encoding-magic-comment 'always-utf8)
+ "utf-8"
+ (let ((coding-system
+ (or save-buffer-coding-system
+ buffer-file-coding-system)))
+ (if coding-system
+ (setq coding-system
+ (or (coding-system-get coding-system 'mime-charset)
+ (coding-system-change-eol-conversion coding-system nil))))
+ (if coding-system
+ (symbol-name
+ (if ruby-use-encoding-map
+ (let ((elt (assq coding-system ruby-encoding-map)))
+ (if elt (cdr elt) coding-system))
+ coding-system))
+ "ascii-8bit"))))
+
+(defun ruby--encoding-comment-required-p ()
+ (or (eq ruby-insert-encoding-magic-comment 'always-utf8)
+ (re-search-forward "[^\0-\177]" nil t)))
+
(defun ruby-mode-set-encoding ()
"Insert a magic comment header with the proper encoding if necessary."
(save-excursion
(widen)
(goto-char (point-min))
- (when (re-search-forward "[^\0-\177]" nil t)
+ (when (ruby--encoding-comment-required-p)
(goto-char (point-min))
- (let ((coding-system
- (or save-buffer-coding-system
- buffer-file-coding-system)))
- (if coding-system
- (setq coding-system
- (or (coding-system-get coding-system 'mime-charset)
- (coding-system-change-eol-conversion coding-system nil))))
- (setq coding-system
- (if coding-system
- (symbol-name
- (if ruby-use-encoding-map
- (let ((elt (assq coding-system ruby-encoding-map)))
- (if elt (cdr elt) coding-system))
- coding-system))
- "ascii-8bit"))
+ (let ((coding-system (ruby--detect-encoding)))
(when coding-system
(if (looking-at "^#!") (beginning-of-line 2))
- (cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
+ (cond ((looking-at "\\s *#\\s *.*\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)")
+ ;; update existing encoding comment if necessary
(unless (string= (match-string 2) coding-system)
(goto-char (match-beginning 2))
(delete-region (point) (match-end 2))
- (and (looking-at "-\*-")
- (let ((n (skip-chars-backward " ")))
- (cond ((= n 0) (insert " ") (backward-char))
- ((= n -1) (insert " "))
- ((forward-char)))))
(insert coding-system)))
((looking-at "\\s *#.*coding\\s *[:=]"))
(t (when ruby-insert-encoding-magic-comment
- (let ((encoding-magic-comment-template
- (case ruby-encoding-magic-comment-style
- ('ruby "# coding: %s")
- ('emacs "# -*- coding: %s -*-")
- ('custom ruby-custom-encoding-magic-comment-template))))
- (insert
- (format encoding-magic-comment-template coding-system)
- "\n")))))
+ (ruby--insert-coding-comment coding-system))))
(when (buffer-modified-p)
(basic-save-buffer-1)))))))
((looking-at "[\"`]") ;skip string
(cond
((and (not (eobp))
- (ruby-forward-string (buffer-substring (point) (1+ (point))) end t t))
+ (ruby-forward-string (buffer-substring (point) (1+ (point)))
+ end t t))
nil)
(t
(setq in-string (point))
(while (and (re-search-forward "#" pos t)
(setq end (1- (point)))
(or (ruby-special-char-p end)
- (and (setq state (ruby-parse-region parse-start end))
+ (and (setq state (ruby-parse-region
+ parse-start end))
(nth 0 state))))
(setq end nil))
(goto-char (or end pos))
(and
(or (and (looking-at ruby-symbol-re)
(skip-chars-backward ruby-symbol-chars)
- (looking-at (concat "\\<\\(" ruby-block-hanging-re "\\)\\>"))
+ (looking-at (concat "\\<\\(" ruby-block-hanging-re
+ "\\)\\>"))
(not (eq (point) (nth 3 state)))
(save-excursion
(goto-char (match-end 0))
(cond
((and
(null op-end)
- (not (looking-at (concat "\\<\\(" ruby-block-hanging-re "\\)\\>")))
+ (not (looking-at (concat "\\<\\(" ruby-block-hanging-re
+ "\\)\\>")))
(eq (ruby-deep-indent-paren-p t) 'space)
(not (bobp)))
(widen)
(skip-chars-forward ",.:;|&^~=!?\\+\\-\\*")
(looking-at "\\s("))
(goto-char (scan-sexps (point) 1)))
- ((and (looking-at (concat "\\<\\(" ruby-block-beg-re "\\)\\>"))
+ ((and (looking-at (concat "\\<\\(" ruby-block-beg-re
+ "\\)\\>"))
(not (eq (char-before (point)) ?.))
(not (eq (char-before (point)) ?:)))
(ruby-end-of-block)
(progn
(setq expr (or expr (ruby-expr-beg)
(looking-at "%\\sw?\\Sw\\|[\"'`/]")))
- (nth 1 (setq state (apply 'ruby-parse-partial nil state))))
+ (nth 1 (setq state (apply #'ruby-parse-partial
+ nil state))))
(setq expr t)
(skip-chars-forward "<"))
(not expr))))
(forward-char -1)
(cond ((looking-at "\\s)")
(goto-char (scan-sexps (1+ (point)) -1))
- (case (char-before)
- (?% (forward-char -1))
- ((?q ?Q ?w ?W ?r ?x)
- (if (eq (char-before (1- (point))) ?%) (forward-char -2))))
+ (pcase (char-before)
+ (`?% (forward-char -1))
+ ((or `?q `?Q `?w `?W `?r `?x)
+ (if (eq (char-before (1- (point))) ?%)
+ (forward-char -2))))
nil)
((looking-at "\\s\"\\|\\\\\\S_")
(let ((c (char-to-string (char-before (match-end 0)))))
(t
(forward-char 1)
(while (progn (forward-word -1)
- (case (char-before)
- (?_ t)
- (?. (forward-char -1) t)
- ((?$ ?@)
+ (pcase (char-before)
+ (`?_ t)
+ (`?. (forward-char -1) t)
+ ((or `?$ `?@)
(forward-char -1)
- (and (eq (char-before) (char-after)) (forward-char -1)))
- (?:
+ (and (eq (char-before) (char-after))
+ (forward-char -1)))
+ (`?:
(forward-char -1)
(eq (char-before) :)))))
(if (looking-at ruby-block-end-re)
(let ((start (point)) beg end)
(end-of-line)
(unless
- (if (and (re-search-backward "\\({\\)\\|\\_<do\\(\\s \\|$\\||\\)")
+ (if (and (re-search-backward "\\(?:[^#]\\)\\({\\)\\|\\(\\_<do\\_>\\)")
(progn
+ (goto-char (or (match-beginning 1) (match-beginning 2)))
(setq beg (point))
(save-match-data (ruby-forward-sexp))
(setq end (point))
(defconst ruby-font-lock-keyword-beg-re "\\(?:^\\|[^.@$]\\|\\.\\.\\)")
(defconst ruby-font-lock-keywords
- (list
- ;; functions
- '("^\\s *def\\s +\\(?:[^( \t\n.]*\\.\\)?\\([^( \t\n]+\\)"
+ `(;; Functions.
+ ("^\\s *def\\s +\\(?:[^( \t\n.]*\\.\\)?\\([^( \t\n]+\\)"
1 font-lock-function-name-face)
- ;; keywords
- (list (concat
- ruby-font-lock-keyword-beg-re
- (regexp-opt
- '("alias"
- "and"
- "begin"
- "break"
- "case"
- "class"
- "def"
- "defined?"
- "do"
- "elsif"
- "else"
- "fail"
- "ensure"
- "for"
- "end"
- "if"
- "in"
- "module"
- "next"
- "not"
- "or"
- "redo"
- "rescue"
- "retry"
- "return"
- "then"
- "super"
- "unless"
- "undef"
- "until"
- "when"
- "while"
- "yield")
- 'symbols))
- 1 'font-lock-keyword-face)
- ;; some core methods
- (list (concat
- ruby-font-lock-keyword-beg-re
- (regexp-opt
- '(;; built-in methods on Kernel
- "__callee__"
- "__dir__"
- "__method__"
- "abort"
- "at_exit"
- "autoload"
- "autoload?"
- "binding"
- "block_given?"
- "caller"
- "catch"
- "eval"
- "exec"
- "exit"
- "exit!"
- "fail"
- "fork"
- "format"
- "lambda"
- "load"
- "loop"
- "open"
- "p"
- "print"
- "printf"
- "proc"
- "putc"
- "puts"
- "raise"
- "rand"
- "readline"
- "readlines"
- "require"
- "require_relative"
- "sleep"
- "spawn"
- "sprintf"
- "srand"
- "syscall"
- "system"
- "throw"
- "trap"
- "warn"
- ;; keyword-like private methods on Module
- "alias_method"
- "attr"
- "attr_accessor"
- "attr_reader"
- "attr_writer"
- "define_method"
- "extend"
- "include"
- "module_function"
- "prepend"
- "private"
- "protected"
- "public"
- "refine"
- "using")
- 'symbols))
- 1 'font-lock-builtin-face)
- ;; here-doc beginnings
- `(,ruby-here-doc-beg-re 0 (unless (ruby-singleton-class-p (match-beginning 0))
- 'font-lock-string-face))
- ;; Perl-ish keywords
- "\\_<\\(?:BEGIN\\|END\\)\\_>\\|^__END__$"
- ;; variables
- `(,(concat ruby-font-lock-keyword-beg-re
- "\\_<\\(nil\\|self\\|true\\|false\\)\\>")
+ ;; Keywords.
+ (,(concat
+ ruby-font-lock-keyword-beg-re
+ (regexp-opt
+ '("alias"
+ "and"
+ "begin"
+ "break"
+ "case"
+ "class"
+ "def"
+ "defined?"
+ "do"
+ "elsif"
+ "else"
+ "fail"
+ "ensure"
+ "for"
+ "end"
+ "if"
+ "in"
+ "module"
+ "next"
+ "not"
+ "or"
+ "redo"
+ "rescue"
+ "retry"
+ "return"
+ "then"
+ "super"
+ "unless"
+ "undef"
+ "until"
+ "when"
+ "while"
+ "yield")
+ 'symbols))
+ (1 font-lock-keyword-face))
+ ;; Some core methods.
+ (,(concat
+ ruby-font-lock-keyword-beg-re
+ (regexp-opt
+ '( ;; built-in methods on Kernel
+ "__callee__"
+ "__dir__"
+ "__method__"
+ "abort"
+ "at_exit"
+ "autoload"
+ "autoload?"
+ "binding"
+ "block_given?"
+ "caller"
+ "catch"
+ "eval"
+ "exec"
+ "exit"
+ "exit!"
+ "fail"
+ "fork"
+ "format"
+ "lambda"
+ "load"
+ "loop"
+ "open"
+ "p"
+ "print"
+ "printf"
+ "proc"
+ "putc"
+ "puts"
+ "raise"
+ "rand"
+ "readline"
+ "readlines"
+ "require"
+ "require_relative"
+ "sleep"
+ "spawn"
+ "sprintf"
+ "srand"
+ "syscall"
+ "system"
+ "throw"
+ "trap"
+ "warn"
+ ;; keyword-like private methods on Module
+ "alias_method"
+ "attr"
+ "attr_accessor"
+ "attr_reader"
+ "attr_writer"
+ "define_method"
+ "extend"
+ "include"
+ "module_function"
+ "prepend"
+ "private"
+ "protected"
+ "public"
+ "refine"
+ "using")
+ 'symbols))
+ (1 font-lock-builtin-face))
+ ;; Here-doc beginnings.
+ (,ruby-here-doc-beg-re
+ (0 (unless (ruby-singleton-class-p (match-beginning 0))
+ 'font-lock-string-face)))
+ ;; Perl-ish keywords.
+ "\\_<\\(?:BEGIN\\|END\\)\\_>\\|^__END__$"
+ ;; Variables.
+ (,(concat ruby-font-lock-keyword-beg-re
+ "\\_<\\(nil\\|self\\|true\\|false\\)\\_>")
1 font-lock-variable-name-face)
- ;; keywords that evaluate to certain values
- '("\\_<__\\(?:LINE\\|ENCODING\\|FILE\\)__\\_>" 0 font-lock-variable-name-face)
- ;; symbols
- '("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\|@?\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)"
+ ;; Keywords that evaluate to certain values.
+ ("\\_<__\\(?:LINE\\|ENCODING\\|FILE\\)__\\_>"
+ (0 font-lock-variable-name-face))
+ ;; Symbols.
+ ("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\|@?\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\\\\.[^}\n\\\\]*\\)*}\\)\\)"
2 font-lock-constant-face)
- ;; variables
- '("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
+ ;; Variables.
+ ("\\(\\$\\([^a-zA-Z0-9 \n]\\|[0-9]\\)\\)\\W"
1 font-lock-variable-name-face)
- '("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
+ ("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+"
0 font-lock-variable-name-face)
- ;; constants
- '("\\(?:\\_<\\|::\\)\\([A-Z]+\\(\\w\\|_\\)*\\)"
+ ;; Constants.
+ ("\\(?:\\_<\\|::\\)\\([A-Z]+\\(\\w\\|_\\)*\\)"
1 (unless (eq ?\( (char-after)) font-lock-type-face))
- '("\\(^\\s *\\|[\[\{\(,]\\s *\\|\\sw\\s +\\)\\(\\(\\sw\\|_\\)+\\):[^:]" 2 font-lock-constant-face)
- ;; conversion methods on Kernel
- (list (concat ruby-font-lock-keyword-beg-re
- (regexp-opt '("Array" "Complex" "Float" "Hash"
- "Integer" "Rational" "String") 'symbols))
- 1 font-lock-builtin-face)
- ;; expression expansion
- '(ruby-match-expression-expansion
+ ("\\(^\\s *\\|[\[\{\(,]\\s *\\|\\sw\\s +\\)\\(\\(\\sw\\|_\\)+\\):[^:]"
+ (2 font-lock-constant-face))
+ ;; Conversion methods on Kernel.
+ (,(concat ruby-font-lock-keyword-beg-re
+ (regexp-opt '("Array" "Complex" "Float" "Hash"
+ "Integer" "Rational" "String") 'symbols))
+ (1 font-lock-builtin-face))
+ ;; Expression expansion.
+ (ruby-match-expression-expansion
2 font-lock-variable-name-face t)
- ;; negation char
- '("[^[:alnum:]_]\\(!\\)[^=]"
+ ;; Negation char.
+ ("[^[:alnum:]_]\\(!\\)[^=]"
1 font-lock-negation-char-face)
- ;; character literals
- ;; FIXME: Support longer escape sequences.
- '("\\_<\\?\\\\?\\S " 0 font-lock-string-face)
- )
+ ;; Character literals.
+ ;; FIXME: Support longer escape sequences.
+ ("\\_<\\?\\\\?\\S " 0 font-lock-string-face)
+ )
"Additional expressions to highlight in Ruby mode.")
(defun ruby-match-expression-expansion (limit)