suppress additional braces in emacs electric mode
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 brack开发者_如何学运维ets myself? I now am manually deleting the auto-magically inserted bracket every time.
Thanks in advance, Raghu.
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)
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.
Ran into the same issue. The solution I found is to:
- Use autopair, which does exactly what you want. Make sure you install it.
- Enable
ruby-electric-mode
but only for|
because the rest is already taken care of.
This leads to the following code in your .emacs
file:
(use-package autopair
:config (autopair-global-mode)
)
(use-package ruby-electric-mode
:init
(setq ruby-electric-expand-delimiters-list (quote (124)))
)
(add-hook 'ruby-mode-hook 'ruby-electric-mode)
This code uses use-package package, make sure you installed it (M-X list-packages
, then find use-package
, then i
on the line, then x
and restart emacs).
Also, that might interest people visiting this thread. I added this code to skip closing delimiters with TAB
, it helps jumping over them. Comment out the while
lines (and adjust )
) to have a single TAB
jump over all closing delimiters (taken from emacs board discussion):
(use-package bind-key)
(defun special-tab ()
"Wrapper for tab key invocation.
If point is just before a close delimiter, skip forward until
there is no closed delimiter in front of point. Otherwise, invoke
normal tab command for current mode.
Must be bound to <tab> using bind-key* macro from bind-key package.
Note, this function will not be called if `override-global-mode' is
turned off."
(interactive)
(defun next-char-delims (delims)
(let ((char (and (not (equal (point) (point-max)))
(string (char-after (point))))))
(when char (member char delims))))
(let ((open '("'" "\"" ")" "]" "}" "|")))
(if (next-char-delims open)
(progn (forward-char 1))
;;(while (next-char-delims open)
;; (forward-char 1)))
(call-interactively (key-binding (kbd "TAB"))))))
(if (macrop 'bind-key*)
(bind-key* (kbd "<tab>") 'special-tab)
(user-error "Must have bind-key from use-package.el to use special-tab function"))
This time, you need the bind-key
package for this snippet to work.
精彩评论