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.
goto-char
and theninsert
; and, to preserve point and go elsewhere temporarily, one usessave-excursion
(which wraps around the applicable code) followed bygoto-char
and theninsert
.(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 aspoint-min
andpoint-max
.progn
and evaluate it withC-j
in the*scratch*
buffer. That will add a newline and put the output on the next line.