Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column of buffer position in Emacs Lisp?

Tags:

emacs

lisp

elisp

In Emacs Lisp, if you have a buffer position stored in a variable, how do you find what column it is in?

like image 436
compman Avatar asked Jul 10 '11 02:07

compman


3 Answers

Check out documentation for columns and for save-excursion.

(save-excursion (goto-char pos) (current-column))
like image 110
Trey Jackson Avatar answered Oct 22 '22 02:10

Trey Jackson


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))))
like image 24
compman Avatar answered Oct 22 '22 01:10

compman


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))

like image 1
Kalman Reti Avatar answered Oct 22 '22 01:10

Kalman Reti