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

ruby-changes:31104

From: knu <ko1@a...>
Date: Mon, 7 Oct 2013 23:47:54 +0900 (JST)
Subject: [ruby-changes:31104] knu:r43183 (trunk): Make ruby-electric play nicely with smartparens-mode.

knu	2013-10-07 23:47:32 +0900 (Mon, 07 Oct 2013)

  New Revision: 43183

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

  Log:
    Make ruby-electric play nicely with smartparens-mode.
    
    * misc/ruby-electric.el (ruby-electric-space-can-be-expanded-p):
      Return nil to avoid "end" insertion when in smartparens-mode
      that is configured to insert "end" for the same keyword.
    
    * misc/ruby-electric.el (ruby-electric-keywords): New custom
      variable to replace `ruby-electric-simple-keywords-re` with.

  Modified files:
    trunk/ChangeLog
    trunk/misc/ruby-electric.el
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43182)
+++ ChangeLog	(revision 43183)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Oct  7 22:52:45 2013  Akinori MUSHA  <knu@i...>
+
+	* misc/ruby-electric.el (ruby-electric-space-can-be-expanded-p):
+	  Return nil to avoid "end" insertion when in smartparens-mode
+	  that is configured to insert "end" for the same keyword.
+
+	* misc/ruby-electric.el (ruby-electric-keywords): New custom
+	  variable to replace `ruby-electric-simple-keywords-re` with.
+
 Mon Oct  7 22:52:16 2013  Akinori MUSHA  <knu@i...>
 
 	* misc/ruby-additional.el: Use preceding-char/following-char
Index: misc/ruby-electric.el
===================================================================
--- misc/ruby-electric.el	(revision 43182)
+++ misc/ruby-electric.el	(revision 43183)
@@ -54,9 +54,6 @@ https://github.com/ruby/ruby/blob/trunk/misc/ruby-electric.el#L54
   "Minor mode providing electric editing commands for ruby files"
   :group 'ruby)
 
-(defconst ruby-electric-expandable-do-re
-  "do\\s-$")
-
 (defconst ruby-electric-expandable-bar
   "\\s-\\(do\\|{\\)\\s-+|")
 
@@ -67,10 +64,39 @@ https://github.com/ruby/ruby/blob/trunk/misc/ruby-electric.el#L64
     (?\` . ?\`)
     (?\" . ?\")))
 
-(defcustom ruby-electric-simple-keywords-re
-  (regexp-opt '("def" "if" "class" "module" "unless" "case" "while" "do" "until" "for" "begin") t)
-  "*Regular expresion matching keywords for which closing 'end'
-is to be inserted."
+(defvar ruby-electric-expandable-do-re)
+
+(defvar ruby-electric-expandable-keyword-re)
+
+(defcustom ruby-electric-keywords
+  '("begin"
+    "case"
+    "class"
+    "def"
+    "do"
+    "for"
+    "if"
+    "module"
+    "unless"
+    "until"
+    "while")
+  "List of keywords for which closing 'end' is to be inserted
+after typing a space."
+  :type '(repeat string)
+  :set (lambda (sym val)
+         (set sym val)
+         (setq ruby-electric-expandable-do-re
+               (and (member "do" val)
+                    "\\S-\\s-+\\(do\\)\\s-?$")
+               ruby-electric-expandable-keyword-re
+               (concat "^\\s-*"
+                       (regexp-opt (remove "do" val) t)
+                       "\\s-?$")))
+  :group 'ruby-electric)
+
+(defcustom ruby-electric-simple-keywords-re nil
+  "Obsolete and ignored.  Customize `ruby-electric-keywords'
+instead."
   :type 'regexp :group 'ruby-electric)
 
 (defcustom ruby-electric-expand-delimiters-list '(all)
@@ -103,9 +129,10 @@ mode. https://github.com/ruby/ruby/blob/trunk/misc/ruby-electric.el#L129
 When Ruby Electric mode is enabled, an indented 'end' is
 heuristicaly inserted whenever typing a word like 'module',
 'class', 'def', 'if', 'unless', 'case', 'until', 'for', 'begin',
-'do'. Simple, double and back quotes as well as braces are paired
-auto-magically. Expansion does not occur inside comments and
-strings. Note that you must have Font Lock enabled."
+'do' followed by a space.  Single, double and back quotes as well
+as braces are paired auto-magically.  Expansion does not occur
+inside comments and strings. Note that you must have Font Lock
+enabled."
   ;; initial value.
   nil
   ;;indicator for the mode line.
@@ -166,18 +193,27 @@ strings. Note that you must have Font Lo https://github.com/ruby/ruby/blob/trunk/misc/ruby-electric.el#L193
 
 (defun ruby-electric-space-can-be-expanded-p()
   (if (ruby-electric-code-at-point-p)
-      (let* ((ruby-electric-keywords-re
-              (concat ruby-electric-simple-keywords-re "\\s-$"))
-             (ruby-electric-single-keyword-in-line-re
-              (concat "\\s-*" ruby-electric-keywords-re)))
-        (save-excursion
-          (backward-word 1)
-          (or (looking-at ruby-electric-expandable-do-re)
-              (and (looking-at ruby-electric-keywords-re)
-                   (not (string= "do" (match-string 1)))
-                   (progn
-                     (beginning-of-line)
-                     (looking-at ruby-electric-single-keyword-in-line-re))))))))
+      (cond ((and ruby-electric-expandable-do-re
+                  (looking-back ruby-electric-expandable-do-re))
+             (not (ruby-electric-space--sp-has-pair-p "do")))
+            ((looking-back ruby-electric-expandable-keyword-re)
+             (not (ruby-electric-space--sp-has-pair-p (match-string 1)))))))
+
+(defun ruby-electric-space--sp-has-pair-p(keyword)
+  (and (boundp 'smartparens-mode)
+       smartparens-mode
+       (let ((plist (sp-get-pair keyword)))
+         (and plist
+              ;; Check for :actions '(insert)
+              (memq 'insert (plist-get plist :actions))
+              ;; Check for :when '(("SPC" "RET" "<evil-ret>"))
+              (let ((x (plist-get plist :when)) when-space)
+                (while (and x
+                            (not (let ((it (car x)))
+                                   (setq when-space (and (listp it)
+                                                         (member "SPC" it))))))
+                  (setq x (cdr x)))
+                when-space)))))
 
 (defun ruby-electric-cua-replace-region-maybe()
   (let ((func (key-binding [remap self-insert-command])))

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

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