English 中文(简体)
suppress additional braces in emacs electric mode
原标题:
  • 时间:2009-11-19 04:25:13
  •  标签:
  • ruby
  • emacs

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.

最佳回答

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.





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签