简体   繁体   中英

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?

Check out documentation for columns and for 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.

(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". 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].

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.

Another possibility for meaning 2 is (- (point) (line-beginning-position))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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