Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cursor moves to beginning of buffer on save #181

Open
mandarm opened this issue Oct 5, 2021 · 2 comments
Open

Cursor moves to beginning of buffer on save #181

mandarm opened this issue Oct 5, 2021 · 2 comments
Labels

Comments

@mandarm
Copy link

mandarm commented Oct 5, 2021

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.

@stsquad stsquad added the elisp label Oct 5, 2021
@stsquad
Copy link
Owner

stsquad commented Oct 5, 2021

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?

@mandarm
Copy link
Author

mandarm commented Oct 5, 2021

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants