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.
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)
-
Regarding smartparens, please see if the recently added
sp-sublimetext-like
config gives you what you wanted. – Hi-Angel Dec 17 '20 at 11:28
Add this line to your emacs init file
(setq electric-pair-preserve-balance nil)
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).
|
stands for the cursor. Not exactly what you want, but you can useM-(
to insert a pair of parenthesis. – JeanPierre Nov 23 '16 at 14:11