例如,我试图返回当前 (point)
上一行位置的缓冲区字符位置。我的完整功能如下。
我相信 (point)
是字符位置,所以我想减去当前点和当前点上方位置之间的字符数。但是,这取决于当前点上方的线的长度(并不总是等于 frame-char-height
)。
I am trying to mimic the Eclipse comment feature, where when an area is selected, the bottom-most line (the line with the pointer) is not included in the commented region:
(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)))
最佳答案
使用 current-column
的组合获取一行中的当前列,forward-line
导航到另一行,以及 move-to- column
恢复新行上的列:
(let ((column (current-column)))
(forward-line -1)
(move-to-column column))
关于emacs - elisp:如何获得当前点上方一行的点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24968985/