0

Below two screenshots:


The first one before executing eval-buffer on the scratch buffer:

enter image description here


The second one after executing 'eval-buffer`:

enter image description here


As you can see above the commands have inserted the text directly into the text after the code of the commands and not at the location of the current point (i.e. cursor).

Is something wrong with my expectation that if I evaluate the buffer content the inserts should happen at the position of the point placed one line below the code (as shown in the first image)?

13
  • The code is working as designed. In writing Lisp code, one navigates to a desired location with goto-char and then insert; and, to preserve point and go elsewhere temporarily, one uses save-excursion (which wraps around the applicable code) followed by goto-char and then insert.
    – lawlist
    Commented Mar 31, 2023 at 19:49
  • Sorry ... don't understand what you are trying to say. How should I modify the code so that it inserts at position of point at the time of starting evaluation of the code?
    – oOosys
    Commented Mar 31, 2023 at 19:55
  • ((let ((pt (point))) (goto-char pt) (insert (format "hello-world | pt (%s)" pt))) or (let ((pt (point))) (save-excursion (goto-char pt) (insert (format "hello-world | pt (%s)" pt)))) See also commonly used points of reference such as point-min and point-max.
    – lawlist
    Commented Mar 31, 2023 at 20:00
  • Sorry again ... I have trouble to make the code you provided work on the code I have. You introduce in your code additional complexity using format. The point-min and point-max are the start and end of butter if I remember it right. What I need is the current position and a way to tell insert to insert there. You 'hide' the point of insertion within the format. Is there no way to insert at desired position without using format?
    – oOosys
    Commented Mar 31, 2023 at 20:19
  • 1
    Put the code in a progn and evaluate it with C-j in the *scratch* buffer. That will add a newline and put the output on the next line.
    – NickD
    Commented Mar 31, 2023 at 21:30

1 Answer 1

0

Is there something wrong with my expectation that if I evaluate the buffer content the inserts should happen at the position of the point placed one line below the code (as shown in the first image)?

The problem with your expectation is that Emacs works not the way you assume it should work. The current position of the point is changed already at the start of the evaluation of the buffer to the buffer start. Then the evaluation uses the point and changes its current position statement after statement it evaluates. From this perspective the inserts happen at the right position: the position after the just evaluated statement where the evaluation moved the current point to.

In order to achieve the effect you expect you can use the above knowledge and wrap all the statements into one. And knowing that the current position will be the char directly after the last bracket of the statement you need to insert a newline in order to get the output in the next line. See the code below after it was evaluated:

(progn
(insert "\n")
(insert "  " (locate-library "font-lock")) ;; /usr/local/share/emacs/29.0.60/lisp/font-lock.elc
(insert "  " (locate-library "linum"    )) ;; /usr/local/share/emacs/29.0.60/lisp/obsolete/linum.elc
)
  /usr/local/share/emacs/29.0.60/lisp/font-lock.elc  /usr/local/share/emacs/29.0.60/lisp/obsolete/linum.elc

Notice that the evaluation restores the position of the point it has been moving around during its work to the original position in the text. If it was placed in part without insertions to exactly the same position. And if it was placed behind the part with insertions moved further by the total length/size of the insertions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.