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

Save buffer before executing markdown-open-command #248

Closed
saf-dmitry opened this issue Sep 1, 2017 · 7 comments
Closed

Save buffer before executing markdown-open-command #248

saf-dmitry opened this issue Sep 1, 2017 · 7 comments

Comments

@saf-dmitry
Copy link
Contributor

I would suggest some small improvements to the markdown-open command. The version below will first save the buffer content using (save-buffer (buffer-file-name)) and then run the markdown-open-command on the file.

(defun markdown-open ()
  "Open file for the current buffer with `markdown-open-command'."
  (interactive)
  (if (not markdown-open-command)
      (user-error "Variable `markdown-open-command' must be set")
    (if (not buffer-file-name)
        (user-error "Must be visiting a file")
      (save-buffer (buffer-file-name))
      (call-process markdown-open-command
                    nil nil nil buffer-file-name))))

For the time being you could advise the markdown-open function:

(defadvice markdown-open (before my-markdown-save-and-open activate)
  "Save buffer before open file for the current buffer."
  (unless (buffer-file-name) (user-error "Must be visiting a file"))
  (save-buffer (buffer-file-name)))
@jrblevin
Copy link
Owner

jrblevin commented Sep 1, 2017

Nice idea, thanks. I'll add this.

@saf-dmitry
Copy link
Contributor Author

Sorry for being obtrusive @jrblevin: Any news on implementing this, or did you change your mind?

@jrblevin
Copy link
Owner

I didn't change my mind, I'm just behind. Thanks for reminding me about this.

From my reading of the save-buffer docstring, it looks like we don't need the (buffer-file-name) argument. It only takes numerical arguments. I'll add (save-buffer) before the (call-process ... line.

@saf-dmitry
Copy link
Contributor Author

By the way, why the markdown-open-command is called synchronously using call-process? Wouldn't it be more appropriate to use start-process-shell-command for an asynchronous call? This way we can open the current file in a viewer, e.g. Marked, and continue editing text in Emacs buffer while Marked (or other similar viewer) will run in parallel with Emacs updating every time the buffer is saved.

@jrblevin
Copy link
Owner

That's exactly how I use markdown-open-command, with Marked (see this post for details). After reading the call-process docstring carefully, when the third argument (destination) is nil it does seem as if it should wait, but when used with open command on macOS the script returns. I think we can safely change the third argument to 0 instead to make it asynchronous in all cases. Any thoughts?

@jrblevin jrblevin reopened this Oct 17, 2017
@saf-dmitry
Copy link
Contributor Author

Yes, this works when used with open command on macOS. Actually I ask because I'm now working on a cross-platform Markdown viewer, conceptually similar to the Marked app for macOS, and it does not work asynchronously on Linux with Markdown mode v2.3. Maybe changing the third argument of call-process to 0 will help (according to docstring it should), but if not, I would consider start-process-shell-command as truly asynchronous solution.

@jrblevin
Copy link
Owner

Modifying call-process as discussed above works for me in my testing. Good luck with your Markdown viewer, and let me know if for some reason this doesn't work for you.

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

No branches or pull requests

2 participants