From 4a5410cbfea937f585a07f0b01c328b44820716b Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Tue, 24 Mar 2015 01:02:59 -0400 Subject: Update toggle-line-or-region-comment These modifications are based off of Drew Adam's comment function [1]. The main change in behavior from my old function is that whole lines of the region are commented even when the beginning or end of the region is not at the beginning or end of the line. [1] http://permalink.gmane.org/gmane.emacs.devel/181816 --- lisp/init-editing.el | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'lisp/init-editing.el') diff --git a/lisp/init-editing.el b/lisp/init-editing.el index eb6adf3..c13df37 100644 --- a/lisp/init-editing.el +++ b/lisp/init-editing.el @@ -115,15 +115,21 @@ and '<<<' mark the bounds of the narrowed region. (outline-mark-subtree) (narrow-to-region (region-beginning) (region-end))))) -(defun km/toggle-line-or-region-comment () - "Comment/uncomment the current line or region." - (interactive) - (let (beg end) - (if (region-active-p) - (setq beg (region-beginning) end (region-end)) - (setq beg (line-beginning-position) end (line-end-position))) - (comment-or-uncomment-region beg end)) - (forward-line)) +(defun km/toggle-line-or-region-comment (beg end) + "Comment or uncomment the current line or region. +If there is an active region, act on all lines that the region +touches." + (interactive "*r") + (unless (use-region-p) + (setq beg (point) + end (point))) + (let ((bol (save-excursion (goto-char beg) + (line-beginning-position))) + (eol (save-excursion (goto-char end) + (line-end-position)))) + (unless (eq bol eol) + (comment-or-uncomment-region bol eol) + (forward-line)))) ;; Modified from http://oremacs.com/2015/01/26/occur-dwim/. (defun km/occur () -- cgit v1.2.3