I have this neat function in my init-file:
(defun comment-or-uncomment-region-or-line()
"Comments or uncomments the region or the current line if there's no active 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)
(next-line)
)
)
But, what I don't like about it is the following situation:
Lorem ipsum dolor sit amet, consec|tetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proi|dent, sunt in culpa qui officia deserunt
mollit anim id est laborum.
NOTE: | denotes either point or mark of the region.
Commenting this region will result in:
Lorem ipsum dolor sit amet, consec// tetur adipisicing elit, sed do eiusmod tempor
// incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
// exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
// dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
// Excepteur sint occaecat cupidatat non proi
// dent, sunt in culpa qui officia deserunt
// mollit anim id est laborum.
Instead, I want it to be simply:
// Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
// incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
// exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
// dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
// Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
// mollit anim id est laborum.
In other words, I would like the region to expand/extend to embrace the whole lines (first and last) regardless of mark and point being in the middle of the first and last line.
Is there any way to modify this function so that it behaves accordingly?
Call line-beginning-position
and line-end-position
after actually moving the point to the desired locations:
(defun comment-or-uncomment-region-or-line()
"Comments or uncomments the region or the current line if there's no active region."
(interactive)
(let (beg end)
(if (region-active-p)
(progn
(setq beg (region-beginning) end (region-end))
(save-excursion
(setq beg (progn (goto-char beg) (line-beginning-position))
end (progn (goto-char end) (line-end-position)))))
(setq beg (line-beginning-position)
end (line-end-position)))
(comment-or-uncomment-region beg end)
(next-line)))