From 2165c8aa782761384258afee642e74bda92011d9 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 23 Sep 2016 14:58:35 +0200 Subject: [PATCH] =?UTF-8?q?Create=20=C3=A6-enclose-region=20and=20bind=20i?= =?UTF-8?q?t=20to=20M-(?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It encloses region in the specified character, or pair of brackets. --- init.el | 3 +++ lisp/enclose-string.el | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 lisp/enclose-string.el diff --git a/init.el b/init.el index 4e04b30..c4ca281 100644 --- a/init.el +++ b/init.el @@ -117,6 +117,7 @@ (load "zim.el") (load "clearcase.el") (load "jekyll.el") +(load "enclose-string.el") (add-hook 'c-mode-hook (lambda () @@ -359,3 +360,5 @@ Version 2016-02-16" (add-hook 'org-mode-hook (lambda () (if (display-graphic-p) (org-bullets-mode t)))) + +(global-set-key (kbd "M-(") 'æ-enclose-region) diff --git a/lisp/enclose-string.el b/lisp/enclose-string.el new file mode 100644 index 0000000..ee59d6c --- /dev/null +++ b/lisp/enclose-string.el @@ -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)))