emacs - elisp:如何获得当前点上方一行的点?

标签 emacs lisp elisp

例如,我试图返回当前 (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/

相关文章:

python - 在 python 中处理 org-mode 文件

emacs - emacs 上的自定义评论框

emacs hg 删除函数

emacs - 验证一行中正确的字段数?

emacsclient 不评估颜色主题?

macos - 在Mac OS X上,Vim和Emacs的首选版本是什么?

html - XML、S-Expressions 和重叠作用域……它叫什么?

lisp - 普通口齿不清, "defined but never used"

scheme - 如何在 andmap 中的 lambda 函数中返回值

emacs - 在 elisp 中按值添加两个列表