2013-10-07 11:16:38 -04:00
|
|
|
;;; ruby-additional.el --- ruby-mode extensions yet to be merged into Emacs
|
|
|
|
|
|
|
|
;; Authors: Yukihiro Matsumoto, Nobuyoshi Nakada, Akinori MUSHA
|
|
|
|
;; URL: http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/misc/
|
|
|
|
;; Created: 3 Sep 2012
|
|
|
|
;; Package-Requires: ((ruby-mode "1.2"))
|
|
|
|
;; Keywords: ruby, languages
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;
|
|
|
|
;; This package contains ruby-mode extensions yet to be merged into
|
|
|
|
;; the Emacs distribution.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(eval-when-compile
|
|
|
|
(require 'ruby-mode))
|
2012-09-03 05:31:37 -04:00
|
|
|
|
2013-10-02 11:25:34 -04:00
|
|
|
(eval-after-load 'ruby-mode
|
|
|
|
'(progn
|
|
|
|
(define-key ruby-mode-map "\C-c\C-e" 'ruby-insert-end)
|
|
|
|
(define-key ruby-mode-map "\C-c{" 'ruby-toggle-block)
|
2012-09-03 05:31:37 -04:00
|
|
|
|
2013-10-02 11:25:34 -04:00
|
|
|
(defun ruby-insert-end ()
|
|
|
|
(interactive)
|
2013-10-07 10:47:29 -04:00
|
|
|
(if (eq (char-syntax (preceding-char)) ?w)
|
2013-10-02 11:25:34 -04:00
|
|
|
(insert " "))
|
|
|
|
(insert "end")
|
|
|
|
(save-excursion
|
2013-10-07 10:47:29 -04:00
|
|
|
(if (eq (char-syntax (following-char)) ?w)
|
2013-10-02 11:25:34 -04:00
|
|
|
(insert " "))
|
|
|
|
(ruby-indent-line t)
|
|
|
|
(end-of-line)))
|
2012-09-03 05:31:37 -04:00
|
|
|
|
2013-10-02 11:25:34 -04:00
|
|
|
(defun ruby-brace-to-do-end ()
|
|
|
|
(when (looking-at "{")
|
|
|
|
(let ((orig (point)) (end (progn (ruby-forward-sexp) (point))))
|
2013-10-07 10:47:29 -04:00
|
|
|
(when (eq (preceding-char) ?\})
|
2013-10-02 11:25:34 -04:00
|
|
|
(delete-char -1)
|
2013-10-07 10:47:29 -04:00
|
|
|
(if (eq (char-syntax (preceding-char)) ?w)
|
2013-10-02 11:25:34 -04:00
|
|
|
(insert " "))
|
|
|
|
(insert "end")
|
2013-10-07 10:47:29 -04:00
|
|
|
(if (eq (char-syntax (following-char)) ?w)
|
2013-10-02 11:25:34 -04:00
|
|
|
(insert " "))
|
|
|
|
(goto-char orig)
|
|
|
|
(delete-char 1)
|
2013-10-07 10:47:29 -04:00
|
|
|
(if (eq (char-syntax (preceding-char)) ?w)
|
2013-10-02 11:25:34 -04:00
|
|
|
(insert " "))
|
|
|
|
(insert "do")
|
|
|
|
(when (looking-at "\\sw\\||")
|
|
|
|
(insert " ")
|
|
|
|
(backward-char))
|
|
|
|
t))))
|
2012-09-03 05:31:37 -04:00
|
|
|
|
2013-10-02 11:25:34 -04:00
|
|
|
(defun ruby-do-end-to-brace ()
|
|
|
|
(when (and (or (bolp)
|
2013-10-07 10:47:29 -04:00
|
|
|
(not (memq (char-syntax (preceding-char)) '(?w ?_))))
|
2013-10-02 11:25:34 -04:00
|
|
|
(looking-at "\\<do\\(\\s \\|$\\)"))
|
|
|
|
(let ((orig (point)) (end (progn (ruby-forward-sexp) (point))))
|
|
|
|
(backward-char 3)
|
|
|
|
(when (looking-at ruby-block-end-re)
|
|
|
|
(delete-char 3)
|
|
|
|
(insert "}")
|
|
|
|
(goto-char orig)
|
|
|
|
(delete-char 2)
|
|
|
|
(insert "{")
|
|
|
|
(if (looking-at "\\s +|")
|
|
|
|
(delete-char (- (match-end 0) (match-beginning 0) 1)))
|
|
|
|
t))))
|
2012-09-03 05:31:37 -04:00
|
|
|
|
2013-10-02 11:25:34 -04:00
|
|
|
(defun ruby-toggle-block ()
|
|
|
|
(interactive)
|
|
|
|
(or (ruby-brace-to-do-end)
|
|
|
|
(ruby-do-end-to-brace)))
|
2012-11-06 21:48:19 -05:00
|
|
|
|
2013-10-02 11:25:34 -04:00
|
|
|
(defun ruby-mode-set-encoding ()
|
|
|
|
"Insert a magic comment header with the proper encoding always.
|
2012-11-06 21:48:19 -05:00
|
|
|
Now encoding needs to be set always explicitly actually."
|
2013-10-02 11:25:34 -04:00
|
|
|
(save-excursion
|
|
|
|
(let ((coding-system))
|
|
|
|
(widen)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(if (re-search-forward "[^\0-\177]" nil t)
|
|
|
|
(progn
|
|
|
|
(goto-char (point-min))
|
|
|
|
(setq coding-system
|
|
|
|
(or coding-system-for-write
|
|
|
|
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
|
|
|
|
(or (and ruby-use-encoding-map
|
|
|
|
(cdr (assq coding-system ruby-encoding-map)))
|
|
|
|
coding-system))
|
|
|
|
"ascii-8bit")))
|
|
|
|
(setq coding-system "us-ascii"))
|
|
|
|
(if (looking-at "^#!") (beginning-of-line 2))
|
|
|
|
(cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
|
|
|
|
(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
|
|
|
|
(insert "# -*- coding: " coding-system " -*-\n")))))))
|
2012-11-06 21:48:19 -05:00
|
|
|
|
2013-10-02 11:25:34 -04:00
|
|
|
))
|
2013-10-07 11:16:38 -04:00
|
|
|
|
|
|
|
(provide 'ruby-additional)
|
|
|
|
|
|
|
|
;;; ruby-additional.el ends here
|