Fork me on GitHub
#emacs
<
2019-02-16
>
dpsutton21:02:59

trying to insert contents into another buffer and move point to the end of the buffer. not having luck. Create a buffer named random, and then

(with-current-buffer "random"
  (goto-char (point-max))
  (insert "\nbob\n")
  (goto-char (point-max))
  )
and no luck. point stays where it was as if there was an unwind-protect somewhere. anyone know how to do this?

dpsutton21:02:21

(with-selected-window (get-buffer-window ...))

Michael Griffiths21:02:15

Are you possibly inside a save-excursion?

Michael Griffiths21:02:54

FWIW: (with-current-buffer "*scratch*" (goto-char (point-max)) (insert "foo\n")) works for me

dpsutton21:02:14

i think that's because you're currently there. do it with a buffer that is separate from your current view

dpsutton21:02:27

my steps above mimic more closely what we are doing with the repl buffer

Michael Griffiths21:02:18

You’re right, if the buffer isn’t visible it won’t move the point

dpsutton21:02:27

even if its visible it doesn't move the point

dpsutton21:02:31

screenshot incoming

dpsutton21:02:53

note point at the end of "random stuff". using the first form will move point

Michael Griffiths22:02:28

Seems like we do this in a couple of places:

(with-selected-window (or (get-buffer-window ...buffer-name...)
                          (selected-window))
  ...)

Michael Griffiths22:02:02

I guess if the buffer isn’t in any live window we have to leave the point where it is

dpsutton22:02:31

ok. there's a problem if the buffer isn't visible in a window. can't use with-selected-window. how about something like this:

(defun cider-insert-in-repl (form eval)
  "Insert FORM in the REPL buffer and switch to it.
If EVAL is non-nil the form will also be evaluated."
  (while (string-match "\\`[ \t\n\r]+\\|[ \t\n\r]+\\'" form)
    (setq form (replace-match "" t t form)))
  (let ((repl (cider-current-repl)))
    (if-let* ((window (get-buffer-window repl)))
        (with-selected-window window
          (cider--insert-in-repl form eval))
      (with-current-buffer repl
        (cider--insert-in-repl form eval))))
  (when cider-switch-to-repl-after-insert-p
    (cider-switch-to-repl-buffer)))

dpsutton22:02:57

ie, if there's a window, use with-selected-window else fall back to with-current-buffer and it won't scroll, but its not visible so whatever

dpsutton22:02:52

so pull the guts out into cider--insert-in-repl and that is either in the context of a selected window or with-current-buffer

dpsutton22:02:00

goog find @cichli. i've updated the PR