EMACS:扩展/扩展区域,使其包含整条线

时间:2012-11-01 23:00:22

标签: emacs line extend expand region

我的init文件中有这个简洁的功能:

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

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

有没有办法修改这个功能,以便它的行为相应?

2 个答案:

答案 0 :(得分: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)))

答案 1 :(得分: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)))))