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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With