例如,我试图返回当前(point)
上方一行的缓冲区char位置。我的全部功能如下。
我相信(point)
是字符位置,所以我想减去当前点和当前点上方位置之间的字符数。但是,这取决于当前点之上的线的长度(并不总是等于frame-char-height
)。
我正在尝试模仿Eclipse注释功能,当选择某个区域时,最下面一行(带指针的行)不包含在注释区域中:
(defun comment-eclipse (&optional arg)
(interactive)
(let ((start (line-beginning-position))
(end (line-end-position)))
(when (or (not transient-mark-mode) (region-active-p))
(setq start (save-excursion
(goto-char (region-beginning))
(beginning-of-line)
(point))
end (save-excursion
(goto-char (region-end))
(end-of-line)
(point)))) ;; HERE: I want to return something like (- (point) (line-length))
(comment-or-uncomment-region start end)))
对于如何实现这一目标的任何建议都将不胜感激。
更新
感谢下面的lunaryorn的回答,我改进了我的功能如下:
(defun comment-eclipse (&optional arg)
(interactive)
(let ((start (line-beginning-position))
(end (line-end-position)))
(when (or (not transient-mark-mode) (region-active-p))
(setq start (save-excursion
(goto-char (region-beginning))
(beginning-of-line)
(point))
end (save-excursion
(goto-char (region-end));;move point to region end
(end-of-line);;move point to end of line
(forward-line -1)
(end-of-line)
(point))))
(comment-or-uncomment-region start end)))
答案 0 :(得分:8)
使用current-column
组合获取当前列,forward-line
导航到另一行,move-to-column
恢复新行上的列:
(let ((column (current-column)))
(forward-line -1)
(move-to-column column))