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

add markdown-native-preview-mode to view previewed output within emacs #36

Merged
merged 18 commits into from
Nov 8, 2015

Conversation

cosmicexplorer
Copy link
Contributor

markdown-export-and-preview is great for running glossy live-updating markdown viewer nightmare children and is very idiomatic emacs lisp. markdown-native-preview-mode is an alternative using eww (built in to emacs since 24.4) to view compiled HTML output within emacs, live-updating as the file is saved. The function markdown-export-native-preview will run markdown-export, then open eww to the exported file. If eww is not already displaying a preview of the exported output, it will pop to a new eww buffer displaying it; otherwise, it will update all windows displaying the exported output to their respective points in a new eww buffer rendering the new export. If markdown-native-preview-mode is turned on, rendering occurs synchronously on every save.

The defcustom markdown-preview-window-function can be used to customize the method of displaying the exported output in emacs, allowing the user to choose an alternative html viewer within emacs such as w3m. The defcustom markdown-native-preview-delete-export determines whether to delete the exported file, which is typically not required after rendering within emacs, unlike with external html viewers.

Danny McClanahan added 4 commits November 4, 2015 00:56
@jrblevin
Copy link
Owner

jrblevin commented Nov 4, 2015

Thanks, I really like this idea. I didn't know about eww previously. I didn't have a whole lot of time to test it this morning but I did notice a few issues:

  1. These changes break the test-markdown-hook/after-export test. I didn't inspect further to see why.
  2. When editing a buffer with markdown-native-preview-mode on, when I save the buffer the *eww* preview does indeed update but then the buffer I'm editing gets killed (i.e., it is closed and I'm back to *scratch*). I don't think that's the intention.
  3. markdown-export-native-preview and markdown-native-preview-mode don't open the *eww* buffer (not even a half window). Should they? I'm not sure, just raising it as a possibility to help with "discoverability".
  4. A short description added to the documentation at the top of the file (which is used to produce README.md) would also be helpful. Eventually, a keybinding would be nice.

If you can take a look at 1 and 2 (3 and 4 can wait), then I'd be very happy to merge this.

@cosmicexplorer
Copy link
Contributor Author

For some reason, I don't see the after-export test breaking, but I do see a variety of others failing. I'll investigate this. I believe 2. is a result of some sketchy buffer/window-handling in the export function, which I'll fix. For 3., it should should be calling display-buffer on the *eww* buffer, so I don't know why that's happening (I think it's because of some sketchy things I'm doing in my personal emacs config with eww buffers). 4. is easy, and I'll do that.

@jrblevin
Copy link
Owner

jrblevin commented Nov 5, 2015

Somehow the font lock tests aren't reliable when running interactively. I run make test on the command line instead. One of the conditions checked in the after-export test is that the output buffer remains open, but it is now failing because of the following line added to markdown-export:

(kill-buffer output-buffer)

Also, just to make sure it wasn't due to my local config, I double-checked 2 and 3 above with emacs --no-init-file and nothing changed.

Danny McClanahan added 2 commits November 4, 2015 23:30
make test was complaining that font-lock-fontify-buffer shouldn't be run
in interactive mode. this silences those.
@cosmicexplorer
Copy link
Contributor Author

Gotcha, the tests work now (some obsoleted functions in 25.1 tripped me up but that's just cause I'm running from master). I just added the kill-buffer because I personally don't like having the exported html open in a buffer after I'm done, but that's the job of markdown-export-native-preview, not markdown-export, so I've moved it to my function.

I've also made a few changes to the use of font-lock-fontify-buffer; my version of emacs was yelling about not being allowed to use them non-interactively when running make-test. This shouldn't break anything on your end.

Please tell me if the markdown buffer is still being killed or if the changes I made to test/markdown-test.el break anything. I'll address 3 and 4 presently.

@cosmicexplorer
Copy link
Contributor Author

Hey, so I've checked this out on emacs -Q and my own configuration; 3 should be fixed now (it should use display-buffer to pop up the eww buffer). Since you said you tested it on --no-init-file, I'm confused as to why it's not popping up in your testing. Can you check if the new version fails?

native-preview -> live-preview

keybinding and menu item (with checkbox!) added

documentation in "Markdown and Maintenance Commands" added
@cosmicexplorer
Copy link
Contributor Author

Renamed native-preview -> live-preview, because "native preview" doesn't actually mean anything and C-c C-c n was already taken. Added a bit of documentation, keybinding, and menu item.

Danny McClanahan and others added 2 commits November 5, 2015 17:31
add mention of arg BUF in `markdown-window-redisplay-preview'
@jrblevin
Copy link
Owner

jrblevin commented Nov 6, 2015

This is looking great. I'm still seeing some window issues on Emacs 24, however. Now, when I enable live-preview with C-c C-c l the *eww* buffer flashes very briefly but disappears. A second issue is that when I disable live-preview, by pressing C-c C-c l again, the buffer for the Markdown file I am editing gets closed.

I tested this on a recent build of Emacs 25 and it works as it should. The *eww* buffer is opened as a half window and gets closed when I turn off live-preview. I'm not up to speed on the new developments in Emacs 25, so I don't have any idea why the behavior is different.

@jrblevin
Copy link
Owner

jrblevin commented Nov 6, 2015

For some reason markdown-preview-buffer isn't getting set correctly on Emacs 24. It references the buffer for the Markdown file rather than the *eww* buffer. Perhaps it's a difference in eww-open-file?

@jrblevin
Copy link
Owner

jrblevin commented Nov 6, 2015

I simplified the window handling code and refactored the live-preview-window-function definition so that it works for me now on Emacs 24 and 25. See commit 97cae6e in my feat/live-preview branch.

I admit that I didn't fully understand what the additional window handling code was doing. I'm simply using switch-to-buffer-other-window on the first call, and doing nothing on subsequent updates (leaving it to the user to keep the window around). What do you think?

@cosmicexplorer
Copy link
Contributor Author

Took me a bit, but I figured out that the complex window handling code was because in my own config I change the name of the *eww* buffer after it's created, which was causing part of the confusing window management code. I'm definitely ok with removing that, can't imagine why I didn't think about that when developing this.

I'd prefer it if markdown-window-redisplay-preview was kept, because it keeps the window scrolled to the same spot every time the screen is refreshed. This commit keeps most of your changes, but also ensures that we don't modify the point/start of any windows displaying the output buffer (for me, whenever I refresh the *eww* buffer, it scrolls all the way to the bottom on every save unless I do this).

I also added the export function to after-revert-hook, and killed the attached preview buffer on kill-buffer-hook. Those can be removed or defcustomed as required.

@cosmicexplorer
Copy link
Contributor Author

Removed the after-revert-hook because I forgot that it turns off minor modes / clears buffer-locals anyway. Checked this on a clean emacs 24 and 25, and both seemed to work correctly. Thanks for putting so much time into this.

@jrblevin jrblevin merged commit a43678a into jrblevin:master Nov 8, 2015
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants