Create æ-enclose-region and bind it to M-(

It encloses region in the specified character, or pair of brackets.
This commit is contained in:
Gergely Polonkai 2016-09-23 14:58:35 +02:00
parent 6fa0b51045
commit 2165c8aa78
2 changed files with 37 additions and 0 deletions

View File

@ -117,6 +117,7 @@
(load "zim.el") (load "zim.el")
(load "clearcase.el") (load "clearcase.el")
(load "jekyll.el") (load "jekyll.el")
(load "enclose-string.el")
(add-hook 'c-mode-hook (add-hook 'c-mode-hook
(lambda () (lambda ()
@ -359,3 +360,5 @@ Version 2016-02-16"
(add-hook 'org-mode-hook (add-hook 'org-mode-hook
(lambda () (lambda ()
(if (display-graphic-p) (org-bullets-mode t)))) (if (display-graphic-p) (org-bullets-mode t))))
(global-set-key (kbd "M-(") 'æ-enclose-region)

34
lisp/enclose-string.el Normal file
View File

@ -0,0 +1,34 @@
(defun æ-enclose-region (character &optional start end)
"Enclose region in CHARACTER. If region is empty, simply inserts
CHARACTER two times and moves point between them.
If character is present in `insert-pair-alist', this function
will enclose region in the corresponding pair. In this case,
CHARACTER must be the opening member of the pair."
(interactive "cWhat character? \nr")
(setq open character close character)
(let ((pair (assq character insert-pair-alist)))
(if pair
(if (nth 2 pair)
(setq open (nth 1 pair) close (nth 2 pair))
(setq open (nth 0 pair) close (nth 1 pair)))))
(unless (and open close)
(setq open character)
(setq close character))
(unless (use-region-p)
(setq start (point) end (point)))
(save-excursion
(goto-char end)
(insert-char close)
(goto-char start)
(insert-char open))
(unless (use-region-p)
(forward-char)))