emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] trunk r115681: Integrate ruby-mode with electric-indent-mo


From: Dmitry Gutov
Subject: [Emacs-diffs] trunk r115681: Integrate ruby-mode with electric-indent-mode better
Date: Sun, 22 Dec 2013 04:57:05 +0000
User-agent: Bazaar (2.6b2)

------------------------------------------------------------
revno: 115681
revision-id: address@hidden
parent: address@hidden
committer: Dmitry Gutov <address@hidden>
branch nick: trunk
timestamp: Sun 2013-12-22 06:57:00 +0200
message:
  Integrate ruby-mode with electric-indent-mode better
  
  * lisp/progmodes/ruby-mode.el (ruby--at-indentation-p): New function,
  extracted from `ruby-smie-rules'.
  (ruby--electric-indent-chars): New variable.
  (ruby--electric-indent-p): New function.
  (ruby-mode): Use `electric-indent-functions' instead of
  `electric-indent-chars'.
modified:
  etc/NEWS                       news-20100311060928-aoit31wvzf25yr1z-1
  lisp/ChangeLog                 changelog-20091113204419-o5vbwnq5f7feedwu-1432
  lisp/progmodes/ruby-mode.el    
rubymode.el-20091113204419-o5vbwnq5f7feedwu-8804
=== modified file 'etc/NEWS'
--- a/etc/NEWS  2013-12-22 04:55:31 +0000
+++ b/etc/NEWS  2013-12-22 04:57:00 +0000
@@ -599,6 +599,8 @@
 
 *** New option `ruby-align-to-stmt-keywords'.
 
+*** New `electric-indent-mode' integration.
+
 ** Search and Replace
 
 *** New global command `M-s .' (`isearch-forward-symbol-at-point')

=== modified file 'lisp/ChangeLog'
--- a/lisp/ChangeLog    2013-12-22 02:31:21 +0000
+++ b/lisp/ChangeLog    2013-12-22 04:57:00 +0000
@@ -1,5 +1,14 @@
 2013-12-22  Dmitry Gutov  <address@hidden>
 
+       * progmodes/ruby-mode.el (ruby--at-indentation-p): New function,
+       extracted from `ruby-smie-rules'.
+       (ruby--electric-indent-chars): New variable.
+       (ruby--electric-indent-p): New function.
+       (ruby-mode): Use `electric-indent-functions' instead of
+       `electric-indent-chars'.
+
+2013-12-22  Dmitry Gutov  <address@hidden>
+
        * progmodes/ruby-mode.el (ruby-align-to-stmt-keywords): Tweak the
        docstring.
        (ruby-smie-rules): Indent plus one level after `=>'.

=== modified file 'lisp/progmodes/ruby-mode.el'
--- a/lisp/progmodes/ruby-mode.el       2013-12-22 02:31:21 +0000
+++ b/lisp/progmodes/ruby-mode.el       2013-12-22 04:57:00 +0000
@@ -631,12 +631,19 @@
           ruby-indent-level))
     (`(:after . ,(or "?" ":")) ruby-indent-level)
     (`(:before . ,(or "if" "while" "unless" "until" "begin" "case" "for"))
-     (when (not (save-excursion (skip-chars-backward " \t") (bolp)))
+     (when (not (ruby--at-indentation-p))
        (if (ruby-smie--indent-to-stmt-p token)
            (ruby-smie--indent-to-stmt)
          (cons 'column (current-column)))))
     ))
 
+(defun ruby--at-indentation-p (&optional point)
+  (save-excursion
+    (unless point (setq point (point)))
+    (forward-line 0)
+    (skip-chars-forward " \t")
+    (eq (point) point)))
+
 (defun ruby-imenu-create-index-in-block (prefix beg end)
   "Create an imenu index of methods inside a block."
   (let ((index-alist '()) (case-fold-search nil)
@@ -767,6 +774,29 @@
           (when (buffer-modified-p)
             (basic-save-buffer-1)))))))
 
+(defvar ruby--electric-indent-chars '(?. ?\) ?} ?\]))
+
+(defun ruby--electric-indent-p (char)
+  (cond
+   ((memq char ruby--electric-indent-chars)
+    ;; Outdent after typing a closing paren.
+    (ruby--at-indentation-p (1- (point))))
+   ((memq (char-after) ruby--electric-indent-chars)
+    ;; Reindent after inserting something before a closing paren.
+    (ruby--at-indentation-p (1- (point))))
+   ((or (memq (char-syntax char) '(?w ?_)))
+    (let ((pt (point)))
+      (save-excursion
+        (skip-syntax-backward "w_")
+        (and (ruby--at-indentation-p)
+             (looking-at (regexp-opt (cons "end" ruby-block-mid-keywords)))
+             ;; Outdent after typing a keyword.
+             (or (eq (match-end 0) pt)
+                 ;; Reindent if it wasn't a keyword after all.
+                 (eq (match-end 0) (1- pt)))))))))
+
+;; FIXME: Remove this?  It's unused here, but some redefinitions of
+;; `ruby-calculate-indent' in user init files still call it.
 (defun ruby-current-indentation ()
   "Return the indentation level of current line."
   (save-excursion
@@ -2081,8 +2111,7 @@
   (setq-local end-of-defun-function 'ruby-end-of-defun)
 
   (add-hook 'after-save-hook 'ruby-mode-set-encoding nil 'local)
-
-  (setq-local electric-indent-chars (append '(?\{ ?\}) electric-indent-chars))
+  (add-hook 'electric-indent-functions 'ruby--electric-indent-p nil 'local)
 
   (setq-local font-lock-defaults '((ruby-font-lock-keywords) nil nil))
   (setq-local font-lock-keywords ruby-font-lock-keywords)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]