例如,我试图返回当前位置上方一行的缓冲区char位置(point).我的全部功能如下.
我相信(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)))
Run Code Online (Sandbox Code Playgroud)
任何关于如何实现这一目标的建议都将受到赞赏.
UPDATE
感谢下面的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)))
Run Code Online (Sandbox Code Playgroud)
使用组合current-column来获取一行上的当前列,forward-line导航到另一行,并move-to-column恢复新行上的列:
(let ((column (current-column)))
(forward-line -1)
(move-to-column column))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
739 次 |
| 最近记录: |