0

I use an auto complete tool in emacs, which can complete brackets. But I want to find out that how it determines whether it should complete the bracket. For example, assuming | stands for the point. In the case of | abc, when typing (, it will auto complete to (|) abc. In another case of |abc , when typing (, it will not auto complete just like what sublime does.

  • Possible duplicate of stackoverflow.com/questions/27142996/… – xuhdev Nov 23 '16 at 10:24
  • I had a hard time understanding the question, until I found | stands for the cursor. Not exactly what you want, but you can use M-( to insert a pair of parenthesis. – JeanPierre Nov 23 '16 at 14:11
3

Add this line to your emacs init file

(setq electric-pair-preserve-balance nil)

Reference: https://stackoverflow.com/questions/27142996/electric-pair-mode-dont-pair-if-cursor-precedes-a-non-whitespace-character

  • @xuhdex Thanks, but it didn't work in spacemacs. Should I modify something? – maple Nov 23 '16 at 10:34
  • Can it work under smartparens-mode? – maple Nov 23 '16 at 10:47
  • @maple It is for electric-pair-mode. I don't know how to do it in smartparens-mode, but you can certainly switch to electric-pair-mode (which is builtin). – xuhdev Nov 23 '16 at 19:03
1

A possible way to do that:

(defun jp/insert-parentheses ()
"Insert a pair of parenthesis if next char is space or newline, or end of buffer"
  (interactive)
  (let ((c (char-after (point))))
    (cond
     ((or (not c) (= c 32) (= c 10)) (insert "()"))
     (t (insert "(")))))


(global-set-key "(" #'jp/insert-parentheses)

Where 32 is the code for a space character and 10 for newline. The not thing is for when point is at end of buffer (char-after will return nil, and we want to allow insertion of a pair of parenthesis in this case).

0

The possible solution for this question I found is: 1) exclude the smartparens for it haven't support such a function.

dotspacemacs-excluded-packages '(smartparens)

2) add electric package when init

(electric-pair-mode 1)
(setq electric-pair-preserve-balance nil)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.