2

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?

Alexander Shukaev
  • 16,674
  • 8
  • 70
  • 85

2 Answers2

5

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)))
user4815162342
  • 141,790
  • 18
  • 296
  • 355
2

One approach is to create a function that returns the :my-region-bol for a point and pass it (region-beginning) and similarly point-at-eol-of-point and use those when region is active.

I quickly tried the following, you can fill in the rest.

(defun :my-region-bol () (interactive) 
  (if (region-active-p)
      (save-excursion
        (goto-char (region-beginning))
        (point-at-bol))
    nil))


(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 (:my-region-bol) end (region-end))
      (setq beg (line-beginning-position) end (line-end-position))
    )
    (comment-or-uncomment-region beg end)
    (next-line)
  )
)

EDIT

The above may not be suitable as it because the first call changes point and mark. I am able to use the following, but it changes the region, which you may or may not want:

(defun :my-bol-at (point) (interactive) 
  (save-excursion
    (goto-char point)
    (point-at-bol)))

(defun :my-eol-at (point) (interactive) 
  (save-excursion
    (goto-char point)
    (point-at-eol)))


(defun :my-expand-region-to-whole-line () (interactive)
  (when (region-active-p)
    (let ((beg (region-beginning)) (end (region-end)))
      (goto-char (:my-bol-at beg))
      (set-mark (point))
      (goto-char (:my-eol-at end)))))
Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
  • Well, I guess the solution of `user4815162342` is the way to go, since it is more concise and works perfectly regardless of the order of mark and point. Thank you for your assistance. – Alexander Shukaev Nov 01 '12 at 23:30
  • I agree...the basic idea is same, to find out the points at bol and eol for region-beginning and region-end respectively – Miserable Variable Nov 02 '12 at 00:02