2

我的初始化文件中有这个简洁的功能:

(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)
  )
)

但是,我不喜欢的是以下情况:

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.

注意: | 表示区域的点或标记。

评论该区域将导致:

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.

相反,我希望它很简单:

// 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.

换句话说,我希望该区域扩展/延伸以包含整条线(第一条和最后一条),而不管标记和点位于第一条和最后一条线的中间。

有什么方法可以修改这个函数,使其行为相应?

4

2 回答 2

5

调用line-beginning-positionline-end-position在实际将点移动到所需位置后:

(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)))
于 2012-11-01T23:11:20.133 回答
2

一种方法是创建一个function,它返回:my-region-bol一个点并传递它(region-beginning),类似地point-at-eol-of-point并在区域处于活动状态时使用它们。

我赶紧试了下,剩下的你可以填写。

(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)
  )
)

编辑

以上可能不适合,因为第一次调用改变了点和标记。我可以使用以下内容,但它会更改您可能想要也可能不想要的区域:

(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)))))
于 2012-11-01T23:06:10.580 回答