简体   繁体   English

Emacs Lisp 中的缓冲区 position 的列?

[英]Column of buffer position in Emacs Lisp?

In Emacs Lisp, if you have a buffer position stored in a variable, how do you find what column it is in?在 Emacs Lisp 中,如果你有一个缓冲区 position 存储在一个变量中,你如何找到它在哪一列?

Check out documentation for columns and for save-excursion .查看save-excursion的文档。

(save-excursion (goto-char pos) (current-column))

Trey already nailed it (though I haven't personally tried it), but here is something that I wrote to do it. Trey 已经搞定了(虽然我没有亲自尝试过),但这是我写的东西。

(defun calculate-column (point)
  (save-excursion
    (goto-char point)
    (beginning-of-line)
    (- point (point))))

There is an ambiguity in the question: "what column is it in" could mean either 1) "what character column does it appear to be in" or 2) "how many characters does one have to advance from the beginning of the line to get to the current position".这个问题有一个模棱两可的地方:“它在哪一列”可能意味着 1)“它出现在哪个字符列中”或 2)“一个人必须从行首前进多少个字符到到达当前位置”。 These are normally the same, but can be different in the presence of characters with a text property like '(display (space:width 7)) [which says to display the characters having that text property as if they were 7 spaces wide].这些通常是相同的,但在存在具有诸如 '(display (space:width 7)) 之类的文本属性的字符时可能会有所不同[它表示将具有该文本属性的字符显示为 7 个空格宽]。

The elisp function current-column returns meaning 1 but computing on values of (point) returns meaning 2. Trey Jackson's answer will return a different value than compman's because of this in some circumstances. elisp function 当前列返回含义 1,但计算 (point) 的值返回含义 2。因此在某些情况下,Trey Jackson 的答案将返回与 compman 不同的值。

Another possibility for meaning 2 is (- (point) (line-beginning-position))含义 2 的另一种可能性是 (- (point) (line-beginning-position))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM