[前][次][番号順一覧][スレッド一覧]

ruby-changes:28114

From: knu <ko1@a...>
Date: Sun, 7 Apr 2013 01:43:43 +0900 (JST)
Subject: [ruby-changes:28114] knu:r40166 (trunk): Improve ruby-electric-mode.

knu	2013-04-07 01:43:32 +0900 (Sun, 07 Apr 2013)

  New Revision: 40166

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40166

  Log:
    Improve ruby-electric-mode.
    
    * misc/ruby-electric.el (ruby-electric-hash): New electric
      function that expands a hash sign inside a string or regexp to
      "#{}".
    
    * misc/ruby-electric.el (ruby-electric-curlies): Do not insert
      spaces inside when the curly brace is a delimiter of %r, %w,
      etc.
    
    * misc/ruby-electric.el (ruby-electric-curlies): Insert another
      space before a closing curly brace when
      ruby-electric-newline-before-closing-bracket is nil.

  Modified files:
    trunk/ChangeLog
    trunk/misc/ruby-electric.el

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 40165)
+++ ChangeLog	(revision 40166)
@@ -1,3 +1,17 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Apr  7 01:40:39 2013  Akinori MUSHA  <knu@i...>
+
+	* misc/ruby-electric.el (ruby-electric-hash): New electric
+	  function that expands a hash sign inside a string or regexp to
+	  "#{}".
+
+	* misc/ruby-electric.el (ruby-electric-curlies): Do not insert
+	  spaces inside when the curly brace is a delimiter of %r, %w,
+	  etc.
+
+	* misc/ruby-electric.el (ruby-electric-curlies): Insert another
+	  space before a closing curly brace when
+	  ruby-electric-newline-before-closing-bracket is nil.
+
 Sun Apr  7 01:01:26 2013  Tanaka Akira  <akr@f...>
 
 	* strftime.c (rb_strftime_with_timespec): Test yday range.
Index: misc/ruby-electric.el
===================================================================
--- misc/ruby-electric.el	(revision 40165)
+++ misc/ruby-electric.el	(revision 40166)
@@ -84,7 +84,8 @@ inserted. The word 'all' will do all ins https://github.com/ruby/ruby/blob/trunk/misc/ruby-electric.el#L84
               (const :tag "Quote" ?\' )
               (const :tag "Double quote" ?\" )
               (const :tag "Back quote" ?\` )
-              (const :tag "Vertical bar" ?\| ))
+              (const :tag "Vertical bar" ?\| )
+              (const :tag "Hash" ?\# ))
   :group 'ruby-electric)
 
 (defcustom ruby-electric-newline-before-closing-bracket nil
@@ -119,7 +120,8 @@ strings. Note that you must have Font Lo https://github.com/ruby/ruby/blob/trunk/misc/ruby-electric.el#L120
   (define-key ruby-mode-map "[" 'ruby-electric-matching-char)
   (define-key ruby-mode-map "\"" 'ruby-electric-matching-char)
   (define-key ruby-mode-map "\'" 'ruby-electric-matching-char)
-  (define-key ruby-mode-map "|" 'ruby-electric-bar))
+  (define-key ruby-mode-map "|" 'ruby-electric-bar)
+  (define-key ruby-mode-map "#" 'ruby-electric-hash))
 
 (defun ruby-electric-space (arg)
   (interactive "P")
@@ -140,6 +142,18 @@ strings. Note that you must have Font Lo https://github.com/ruby/ruby/blob/trunk/misc/ruby-electric.el#L142
   (and ruby-electric-mode
        (consp (memq 'font-lock-string-face (text-properties-at (point))))))
 
+(defun ruby-electric-escaped-p()
+  (let ((f nil))
+    (save-excursion
+      (while (char-equal ?\\ (preceding-char))
+        (backward-char 1)
+        (setq f (not f))))
+    f))
+
+(defun ruby-electric-command-char-expandable-punct-p(char)
+  (or (memq 'all ruby-electric-expand-delimiters-list)
+      (memq char ruby-electric-expand-delimiters-list)))
+
 (defun ruby-electric-is-last-command-char-expandable-punct-p()
   (or (memq 'all ruby-electric-expand-delimiters-list)
       (memq last-command-event ruby-electric-expand-delimiters-list)))
@@ -178,25 +192,57 @@ strings. Note that you must have Font Lo https://github.com/ruby/ruby/blob/trunk/misc/ruby-electric.el#L192
 
 (defun ruby-electric-curlies(arg)
   (interactive "P")
-  (ruby-electric-insert arg
-    (if (ruby-electric-is-last-command-char-expandable-punct-p)
-        (cond ((ruby-electric-code-at-point-p)
-               (insert " ")
-               (save-excursion
-                 (if ruby-electric-newline-before-closing-bracket
-                     (progn
-                       (newline)
-                       (insert "}")
-                       (ruby-indent-line t))
-                   (insert "}"))))
-              ((ruby-electric-string-at-point-p)
-               (if (eq last-command-event ?{)
-                   (save-excursion
-                     (backward-char 1)
-                     (or (char-equal ?\# (preceding-char))
-                         (insert "#"))
-                     (forward-char 1)
-                     (insert "}"))))))))
+  (ruby-electric-insert
+   arg
+   (if (ruby-electric-is-last-command-char-expandable-punct-p)
+       (cond
+        ((ruby-electric-code-at-point-p)
+         (insert "}")
+         (backward-char 1)
+         (redisplay)
+         (cond
+          ((ruby-electric-string-at-point-p) ; %w{}, %r{}, etc.
+           t)
+          (ruby-electric-newline-before-closing-bracket
+           (insert " ")
+           (save-excursion
+             (newline)
+             (ruby-indent-line t)))
+          (t
+           (insert "  ")
+           (backward-char 1))))
+        ((ruby-electric-string-at-point-p)
+         (save-excursion
+           (backward-char 1)
+           (cond
+            ((char-equal ?\# (preceding-char))
+             (unless (save-excursion
+                       (backward-char 1)
+                       (ruby-electric-escaped-p))
+               (forward-char 1)
+               (insert "}")))
+            ((ruby-electric-command-char-expandable-punct-p ?\#)
+             t) ; no need for the following if ruby-electric-hash is enabled
+            ((ruby-electric-escaped-p)
+             t)
+            (t
+             (insert "#")
+             (forward-char 1)
+             (insert "}")))))))))
+
+(defun ruby-electric-hash(arg)
+  (interactive "P")
+  (ruby-electric-insert
+   arg
+   (and (ruby-electric-is-last-command-char-expandable-punct-p)
+        (ruby-electric-string-at-point-p)
+        (or (looking-at "'") ; likely to be in ''
+            (save-excursion
+              (backward-char 1)
+              (ruby-electric-escaped-p))
+            (progn
+              (insert "{}")
+              (backward-char 1))))))
 
 (defun ruby-electric-matching-char(arg)
   (interactive "P")

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]