branch: externals/ruby-end
commit 8cc9faf605aae7a1364a9e8ffcdbff9e1272b0c0
Author: Roman Sokolov <sokolov....@gmail.com>
Commit: Roman Sokolov <sokolov....@gmail.com>

    Allow to toggle on/off expansion with statement modifiers
    
    Introduce variable that controls behavior of expansions.
    When set to nil (default), always perform expansion, even if
    there is some code before statement.
    When set to true, do not perform expansion if there is meaningful
    code before statement.
    Expansion for do-blocks always performed.
---
 ruby-end.el | 40 +++++++++++++++++++++++++++++++++-------
 1 file changed, 33 insertions(+), 7 deletions(-)

diff --git a/ruby-end.el b/ruby-end.el
index a2cee7366e..adcb03e4b6 100644
--- a/ruby-end.el
+++ b/ruby-end.el
@@ -56,9 +56,27 @@
     map)
   "Keymap for `ruby-end-mode'.")
 
-(defconst ruby-end-expand-before-re
-  
"^\\s-*\\(?:def\\|if\\|class\\|module\\|unless\\|case\\|while\\|do\\|until\\|for\\|begin\\)"
-  "Regular expression matching before point.")
+(defcustom ruby-end-check-statement-modifiers nil
+  "*Disable or enable expansion (insertion of end) for statement modifiers"
+  :type 'boolean
+  :group 'ruby)
+
+(defconst ruby-end-expand-postfix-statement-before-re
+  
"\\(?:def\\|if\\|class\\|module\\|unless\\|case\\|while\\|until\\|for\\|begin\\)"
+  "Regular expression matching statements before point.")
+
+(defconst ruby-end-expand-prefix-check-modifiers-re
+  "^\\s-*"
+  "Prefix for regular expression to prevent expansion with statement 
modifiers")
+
+(defconst ruby-end-expand-prefix-re
+  "\\(?:^\\|\\s-+\\)"
+  "Prefix for regular expression")
+
+(defconst ruby-end-expand-block-before-re
+  "\\(?:^\\|\\s-+\\)do"
+  "Regular expression matching blocks before point.")
+
 
 (defconst ruby-end-expand-after-re
   "\\s-*$"
@@ -92,10 +110,18 @@
 
 (defun ruby-end-expand-p ()
   "Checks if expansion (insertion of end) should be done."
-  (and
-   (ruby-end-code-at-point-p)
-   (looking-back ruby-end-expand-before-re)
-   (looking-at ruby-end-expand-after-re)))
+  (let ((ruby-end-expand-statement-before-re
+        (concat
+         (if ruby-end-check-statement-modifiers
+             ruby-end-expand-prefix-check-modifiers-re
+             ruby-end-expand-prefix-re)
+         ruby-end-expand-postfix-statement-before-re)))
+    (and
+     (ruby-end-code-at-point-p)
+     (or
+      (looking-back ruby-end-expand-statement-before-re)
+      (looking-back ruby-end-expand-block-before-re))
+     (looking-at ruby-end-expand-after-re))))
 
 (defun ruby-end-code-at-point-p ()
   "Checks if point is code, or comment or string."

Reply via email to