tags:

views:

233

answers:

2

Hi,

I started using ruby-electric-mode. I like it except that I am used to closing open brackets myself (the other pairing are still useful to me). How can I make emacs suppress additional brackets when I type in the closing brackets myself? I now am manually deleting the auto-magically inserted bracket every time.

Thanks in advance, Raghu.

A: 

It is a “customizable” setting. Run M-x customize-variable (ESCx if you do not have a Meta key) and customize ruby-electric-expand-delimiters-list.

Uncheck “Everything” and check only the ones you want to be automatically inserted. Be sure to also “Save for Future Sessions”.

If you decide that you mostly like the automatic insertions but that there are some places where you want to turn it off for a single keystroke, then use C-q (Control-q) before an open paren/bracket/brace/quote to suppress the automatic insertion of the closing mark.

Chris Johnsen
Thanks Chris, that does help me. But, is there no way to have the best of both worlds? I.e., have emacs pair my brackets, but delete the duplicate when I type in the the closing pair myself? This would help me navigate within my code better, since, I can skip-over the emacs inserted characters by typing over them (sort of like the override mode but only for the automatically inserted characters)
Raghu
Ahh, it was not clear to me what you wanted to have happen. It looks like Trey's answer (http://stackoverflow.com/questions/1760859/suppress-additional-braces-in-emacs-electric-mode/1764592#1764592) might work the way you want.
Chris Johnsen
+1  A: 

It sounds like what you want is for the } to either jump to the (already inserted) }, or to simply insert a } and delete the } that was inserted earlier by the electric mode.

This code should do what you want, the choice of what to do on } is toggled by the variable my-ruby-close-brace-goto-close.

;; assuming
;; (require 'ruby)
;; (require 'ruby-electric)
(defvar my-ruby-close-brace-goto-close t
  "Non-nill indicates to move point to the next }, otherwise insert } 
and delete the following }.")

(defun my-ruby-close-brace ()
  "replacement for ruby-electric-brace for the close brace"
  (interactive)
  (let ((p (point)))
    (if my-ruby-close-brace-goto-close
        (unless (search-forward "}" nil t)
          (message "No close brace found")
          (insert "}"))
      (insert "}")
      (save-excursion (if (search-forward "}" nil t)
                           (delete-char -1))))))
(define-key ruby-mode-map "}" 'my-ruby-close-brace)
Trey Jackson
Wow! that was awesome. I changed '}' to ')' in your code (for my case) and it started working right-out-of-the-box. Thanks a ton!!
Raghu
@Raghu FWIW, I used `}` because that's what people generally mean when they say "brace" - see http://en.wikipedia.org/wiki/Bracket .
Trey Jackson