I want to make a logger that display line number of the point.
In init.el I wrote the following function:
(defun logger ()
"Print logger"
(print (concat "print(\"" (what-line) ": \")")))
When I call the function from an other buffer it always prints Line 1
.
However when I do M-x what-line
the correct line number is displayed.
What can I do to make the function printing the right line number?
Edit: I found that the issue was C-x p
.
If I do M-x logger
it works like expected.
Why have we two different behaviors ?
point
,point-min
, andpoint-max
operate on the current buffer. (what-line
usespoint-min
). See https://www.gnu.org/software/emacs/manual/html_node/elisp/Point.html, https://www.gnu.org/software/emacs/manual/html_node/eintr/what_002dline.html,simple.el
and https://emacs.stackexchange.com/questions/3821/a-faster-method-to-obtain-line-number-at-pos-in-large-buffers might be interesting – nega May 14 '22 at 18:24(print (number-to-string (point))))
in init.el. On the current buffer 1 is always printed. Where is this value come from ? – element May 14 '22 at 19:25point
,point-min
, andpoint-max
operate on the current buffer." Note, that even that is a bit simplified, since windows for the same buffer also hold their separate values for point. If you change the selected window you also change current point. (See the doc ofswitch-to-buffer-preserve-window-point
– Tobias May 14 '22 at 21:41with-current-buffer
. Also, you should not usewhat-line
withprint
(what line already usesmessage)
. You probably want to useline-number-at-pos
. – dalanicolai May 16 '22 at 06:53print(f"Line 42: {}")
and fill it with variable nameprint(f"Line 42: var1 = {var1}")
to print var1 value on output. – element May 16 '22 at 19:20M-: (logger) RET
. It will print the line number where the point is. – gigiair May 16 '22 at 19:43global-set-key
is wrong. Instead of bindingC-x p
to logger, it binds it to the result of the call tologger
, whenglobal-set-key
was called. I think it should be(global-set-key (kbd "C-x p") #'logger)
. – Lindydancer May 16 '22 at 20:04