Subject: bug#12169: [PATCH] Merge changes from upstream ruby-mode

From: Dmitry Gutov <[email-masked]>
Date: Sat, 11 Aug 2012 02:15:21 +0400

On 10.08.2012 19:54, Glenn Morris wrote:
 > Are you also the upstream author?

Nope. Thanks for the reminder.

On 11.08.2012 0:28, Stefan Monnier wrote:
> I've installed your patch, but please fix the ChangeLog authorship as
> pointed out by Glenn (just include the changes in a subsequent patch).

Included.

>> Here's part 2, in which I'm skipping the next 3 commits:
>> https://github.com/ruby/ruby/commit/85df40e9ab4559cd4ddde3f106ab56c76a808cc8
>> https://github.com/ruby/ruby/commit/6b0dc7fd81b25bc66681548b8b82f38258f7e08c
>> https://github.com/ruby/ruby/commit/862048e28d26112af1e67205cfd1c16564590bce

For completeness, this later commit is directly related to the above 3:
https://github.com/ruby/ruby/commit/d9e6b7d6a5e81afb588d79ef923b70890cdec4ba

I also don't include it, and we can see that the relevant test passes fine.

> I also added that patch.  I presume that you're also trying to convince
> Ruby's maintainers to undo those changes?

Not really, no. I will open an issue on Redmine since you asked, but 
nobody seems to be interested in pulling our changes back anyway, and as 
long as we're doing it in one direction, cherry-picking should be fine.

Here's a recent example: http://bugs.ruby-lang.org/issues/5140
A year-old bug, fixed in Emacs trunk, a user posted that information 
there with a patch 4 months ago, and... nothing.

> Same as before: please provide a subsequent patch which corrects the
> authorship attribution.

In my not-a-lawyer opinion, the example code in tests is not 
copyrightable, but okay, I mentioned the original author there too.

--Dmitry
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 38379cd..2d63ab6 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,17 +1,29 @@
-2012-08-09  Dmitry Gutov  <[email-masked]>
+2012-08-10 Nobuyoshi Nakada  <[email-masked]>
 
-	Merge stuff from upsteam ruby-mode, part 1.
+	* progmodes/ruby-mode.el (ruby-mode-map): Remove unnecessary
+	binding for `newline'.
+	(ruby-move-to-block): When moving backward, stop at block opening,
+	not indentation.
+	* progmodes/ruby-mode.el (ruby-brace-to-do-end)
+	(ruby-do-end-to-brace, ruby-toggle-block): New functions.
+	* progmodes/ruby-mode.el (ruby-mode-map): Add binding for
+	`ruby-toggle-block'.
+
+2012-08-09  Dmitry Gutov  <[email-masked]>
 
-	* progmodes/ruby-mode.el (ruby-mode-map): Remove deprecated
-	binding (use `M-;' instead).
 	(ruby-expr-beg, ruby-parse-partial): ?, _, and : are symbol
 	constituents, ! is not (but kinda should be).
-	(ruby-singleton-class-p): New function.
-	(ruby-expr-beg, ruby-in-here-doc-p)
-	(ruby-syntax-propertize-heredoc): Use it.
+	(ruby-syntax-propertize-heredoc): Use `ruby-singleton-class-p'.
 	(ruby-syntax-propertize-function): Adjust for changes in
 	`ruby-syntax-propertize-heredoc'.
 
+2012-08-09 Nobuyoshi Nakada  <[email-masked]>
+
+	* progmodes/ruby-mode.el (ruby-mode-map): Remove deprecated
+	binding (use `M-;' instead).
+	(ruby-singleton-class-p): New function.
+	(ruby-expr-beg, ruby-in-here-doc-p) Use it.
+
 2012-08-07  Andreas Schwab  <[email-masked]>
 
 	* calc/calc-prog.el (math-do-defmath): Use backquote forms.  Fix
diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
index 68abaff..c430c99 100644
--- a/lisp/progmodes/ruby-mode.el
+++ b/lisp/progmodes/ruby-mode.el
@@ -150,7 +150,7 @@ This should only be called after matching against `ruby-here-doc-beg-re'."
     (define-key map (kbd "M-C-q") 'ruby-indent-exp)
     (define-key map (kbd "C-M-h") 'backward-kill-word)
     (define-key map (kbd "C-j")   'reindent-then-newline-and-indent)
-    (define-key map (kbd "C-m")   'newline)
+    (define-key map (kbd "C-c {") 'ruby-toggle-block)
     map)
   "Keymap used in Ruby mode.")
 
@@ -881,7 +881,7 @@ or blocks containing the current block."
   ;; TODO: Make this work for n > 1,
   ;; make it not loop for n = 0,
   ;; document body
-  (let (start pos done down)
+  (let (start pos done down (orig (point)))
     (setq start (ruby-calculate-indent))
     (setq down (looking-at (if (< n 0) ruby-block-end-re
                              (concat "\\<\\(" ruby-block-beg-re "\\)\\>"))))
@@ -907,8 +907,18 @@ or blocks containing the current block."
           (save-excursion
             (back-to-indentation)
             (if (looking-at (concat "\\<\\(" ruby-block-mid-re "\\)\\>"))
-                (setq done nil))))))
-  (back-to-indentation))
+                (setq done nil)))))
+    (back-to-indentation)
+    (when (< n 0)
+      (let ((eol (point-at-eol)) state next)
+        (if (< orig eol) (setq eol orig))
+        (setq orig (point))
+        (while (and (setq next (apply 'ruby-parse-partial eol state))
+                    (< (point) eol))
+          (setq state next))
+        (when (cdaadr state)
+          (goto-char (cdaadr state)))
+        (backward-word)))))
 
 (defun ruby-beginning-of-block (&optional arg)
   "Move backward to the beginning of the current block.
@@ -1116,6 +1126,47 @@ See `add-log-current-defun-function'."
               (if mlist (concat mlist mname) mname)
             mlist)))))
 
+(defun ruby-brace-to-do-end ()
+  (when (looking-at "{")
+    (let ((orig (point)) (end (progn (ruby-forward-sexp) (point))))
+      (when (eq (char-before) ?\})
+        (delete-char -1)
+        (if (eq (char-syntax (char-before)) ?w)
+            (insert " "))
+        (insert "end")
+        (if (eq (char-syntax (char-after)) ?w)
+            (insert " "))
+        (goto-char orig)
+        (delete-char 1)
+        (if (eq (char-syntax (char-before)) ?w)
+            (insert " "))
+        (insert "do")
+        (when (looking-at "\\sw\\||")
+          (insert " ")
+          (backward-char))
+        t))))
+
+(defun ruby-do-end-to-brace ()
+  (when (and (or (bolp)
+                 (not (memq (char-syntax (char-before)) '(?w ?_))))
+             (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))))
+
+(defun ruby-toggle-block ()
+  (interactive)
+  (or (ruby-brace-to-do-end)
+      (ruby-do-end-to-brace)))
+
 (declare-function ruby-syntax-propertize-heredoc "ruby-mode" (limit))
 (declare-function ruby-syntax-general-delimiters-goto-beg "ruby-mode" ())
 (declare-function ruby-syntax-propertize-general-delimiters "ruby-mode" (limit))
diff --git a/test/ChangeLog b/test/ChangeLog
index c59c3cb..9a60f0e 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -5,6 +5,13 @@
 	(ruby-should-indent-buffer): New function.
 	Add tests for `ruby-deep-indent-paren' behavior.
 	Port all tests from test/misc/test_ruby_mode.rb in Ruby repo.
+	(ruby-move-to-block-stops-at-opening): New test.
+	(ruby-toggle-block-to-do-end): New test.
+	(ruby-toggle-block-to-brace): New test.
+
+2012-08-10 Nobuyoshi Nakada  <[email-masked]>
+
+	Original tests in test_ruby_mode.rb in upstream (author).
 
 2012-08-09  Dmitry Gutov  <[email-masked]>
 
diff --git a/test/automated/ruby-mode-tests.el b/test/automated/ruby-mode-tests.el
index f91b6e4..df51aa0 100644
--- a/test/automated/ruby-mode-tests.el
+++ b/test/automated/ruby-mode-tests.el
@@ -191,6 +191,32 @@ VALUES-PLIST is a list with alternating index and value elements."
    |  end
    |"))
 
+(ert-deftest ruby-move-to-block-stops-at-opening ()
+  (with-temp-buffer
+    (insert "def f\nend")
+    (beginning-of-line)
+    (ruby-mode)
+    (ruby-move-to-block -1)
+    (should (looking-at "f$"))))
+
+(ert-deftest ruby-toggle-block-to-do-end ()
+  (with-temp-buffer
+    (insert "foo {|b|\n}\n")
+    (ruby-mode)
+    (search-backward "{")
+    (ruby-toggle-block)
+    (should (string= "foo do |b|\nend\n" (buffer-substring-no-properties
+                                          (point-min) (point-max))))))
+
+(ert-deftest ruby-toggle-block-to-brace ()
+  (with-temp-buffer
+    (insert "foo do |b|\nend\n")
+    (ruby-mode)
+    (search-backward "do")
+    (ruby-toggle-block)
+    (should (string= "foo {|b|\n}\n" (buffer-substring-no-properties
+                                      (point-min) (point-max))))))
+
 (provide 'ruby-mode-tests)
 
 ;;; ruby-mode-tests.el ends here