1

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?

enter image description here

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 ?

element
  • 27
  • 5
  • The functions `point`, `point-min`, and `point-max` operate on the _current_ buffer. (`what-line` uses `point-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
  • Same result with `(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:25
  • it will always be 1 if you don't move the point. – nega May 14 '22 at 21:07
  • Each buffer has its own current point. That is, what makes nega's comment meaningful: "The functions `point`, `point-min`, and `point-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 of [`switch-to-buffer-preserve-window-point`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Switching-Buffers.html#index-switch_002dto_002dbuffer_002dpreserve_002dwindow_002dpoint) – Tobias May 14 '22 at 21:41
  • Could you explain why you want to create this 'logger'? Of course, the current-line number is printed by default in the mode line. If you want to print the line-number of some other buffer, then you probably should use `with-current-buffer`. Also, you should not use `what-line` with `print` (what line already uses `message)`. You probably want to use `line-number-at-pos`. – dalanicolai May 16 '22 at 06:53
  • It's for debug purpose and variable values monitoring. I want to automatically insert `print(f"Line 42: {}")` and fill it with variable name `print(f"Line 42: var1 = {var1}")` to print var1 value on output. – element May 16 '22 at 19:20
  • Your function do exactly what you want it to do. Place the point in any place in any buffer ad type `M-: (logger) RET`. It will print the line number where the point is. – gigiair May 16 '22 at 19:43
  • I think the `global-set-key` is wrong. Instead of binding `C-x p` to logger, it binds it to the result of the call to `logger`, when `global-set-key` was called. I think it should be `(global-set-key (kbd "C-x p") #'logger)`. – Lindydancer May 16 '22 at 20:04

1 Answers1

1

There are two parts to my answer.

The first is that your code works perfectly fine for me. I pasted your function into my *scratch* buffer, inserted an (interactive) declaration, and evaluated it. Then I typed M-x logger and it put "print("Line 75: ")" into the echo area as expected. If I move the cursor to a different line, it reports a different line number, as expected.

The second part is that based on your comments, you are writing a debug macro and you want to print the line number from the source code where the macro was invoked. Unfortunately I must disappoint you; Emacs does not keep track of that information. It does keep track of the line numbers when it encounters a defun or defvar, but not for arbitrary expressions. Implementing this feature will require you to extend the Emacs Lisp interpreter and compiler, which unfortunately is not an easy task.

db48x
  • 15,741
  • 1
  • 19
  • 23