emacs_chrome icon indicating copy to clipboard operation
emacs_chrome copied to clipboard

Cursor moves to beginning of buffer on save

Open mandarm opened this issue 4 years ago • 2 comments

Not sure if this is related to #159... whenever I type C-x C-s, point moves to the beginning of buffer. Can this be fixed so that point remains where it was when I run edit-server-save ?

I'm just guessing that the last line of edit-server-done (edit-server-kill-client proc) is the cause? Is it the case that every time I do edit-server-save, the current client is killed, and a fresh connection with the updated content is started? That would explain why point moves to the beginning of the buffer, but don't know if this is the correct explanation.

mandarm avatar Oct 05 '21 10:10 mandarm

Your analysis is correct - this is due to the way edit-server-save is implemented. You are effectively saving the old session and then triggering an immediate second editing session (at which point regenerate the buffer contents and reset point). Ideally we would move to a persistent websocket based approach which would do away with the need for the transactional approach of using a HTTP request with reply that we currently have. However that is a fair amount of work so unlikely to be done anytime soon.

That said we do take care to preserve the buffer for iterative editing (see x-file/edit-server-file). Perhaps the easiest approach would be to save a buffer local value (say edit-server-point-on-save) which we can restore if it exists on a new edit?

stsquad avatar Oct 05 '21 11:10 stsquad

Great, thanks for the confirmation! Unfortunately, I have no idea about what needs to be done on the non-emacs-lisp side, so I've just provided my guesses about what will have to be added to edit-server-filter

  ;; look for "x-saved-point" header
  (save-excursion
    (goto-char (point-min))
    (when (re-search-forward "^x-saved-point: \\([[:digit:]]+\\)" nil t)
       (edit-server-log proc "Found x-saved-point: %s" (match-string 1))
       (setq edit-server-saved-point (match-string 1))))

And the following just before "Mode magic":

  (defvar edit-server-saved-point nil
    "The value gotten from the HTTP `x-saved-point' header.")
  (make-variable-buffer-local 'edit-server-saved-point)
  (put 'edit-server-saved-point 'permanent-local t)

And finally, some code in edit-server-edit-mode involving posn-set-point, edit-server-saved-point, and possibly a customisable variable like edit-server-restore-point?

mandarm avatar Oct 05 '21 12:10 mandarm