I am writing an Emacs Lisp function which needs to know the character directly under
point in the line below, returning
nil when no such character exists. I thus defined the function
char-below, which seems to work well except that it breaks down in case the line below has tabs.
(defun char-below () (condition-case nil (aref (buffer-substring-no-properties (line-beginning-position 2) (line-end-position 2)) (- (point) (line-beginning-position))) (error nil)))
Is there a built-in elisp function to accomplish this? If not, is there a cleverer way to write
char-below, accounting for the presence of tabs?
EDIT: I tried a solution based on
next-line but there seems to be a problem with using
next-line in Lisp code, as observed in its doc-string:
"This function is for interactive use only; in Lisp code use
A way to illustrate the problem is by defining the following key-binding, using the
char-below function proposed by Erik Sjöstrand, which is supposed to display the character below in the message area and move forward one char.
(global-set-key (kbd "<f12>") (lambda () (interactive) (message (char-below)) (forward-char)))
Curiously, repeated pressed of the key f12 thus defined keep messaging the same character below the position point was at the time of the FIRST invocation of f12.
I think this is because
next-line uses goal columns, and somehow the goal column is not updated when the above key is repeated.
By the way, Erik initially proposed the function
char-below as follows
(defun char-below () (ignore-errors (save-excursion (next-line) (string (following-char)))))
and this is the function that causes the error I mentioned above. Since then Eric has edited his function inserting the command
(setq temporary-goal-column nil) which fixes this issue.