Render Markdown in Emacs buffer - emacs

Is it possible to present Markdown rendered in an Emacs buffer using Emacs' own buffer text formatting capabilities? Emacs in graphical environments has rich text presentation capabilities (font styles, colors, links and even images) so it should be quite possible. Are there any existing implementations?
Note that the idea is to have the rendered Markdown be native Emacs formatted text that can be navigated and operated on as any other text in Emacs. Therefore solutions that render to an image that is embedded in an Emacs buffer are not desirable here.
Also note that this is not about a mode for editing Markdown, but for presenting rendered Markdown in an Emacs buffer. It should preferably be a pure Emacs Lisp solution for portability.

Personally, I use the following workflow:
run on a C-c C-c m to run Mark­down on the cur­rent buffer and pre­view the out­put in an­other buffer.
move to html-mode on this other buffer (M-x html-mode)
hide the html tags to display something close to the output (M-x sgml-tags-invisible)
Then every time you want to refresh the rendering, simply run again C-c C-c m on the markdown buffer.
Yet I confess until now for Markdown editing/previewing, nothing beats for me Textmate and its markdown preview panel. Actually, from a personal perspective, the only case where I prefer to run Textmate rather than Emacs is when I want to edit markdown files. Yet the path to have the same quality of preview on emacs is not so difficult and probably I should investigate it. As I see it, it's simply:
get the internal css used by Textmate for rendering the preview
use w3 or w3m to preview the markdown output using this css

I guess you can use the source code of latex-preview for inspiration or pretty-lambda(a much simpler piece of software).
Alternatively you can convert the markdown to html in a background process and preview the html.
Everything is possible in Emacs, but not everything is easily achieved :-)

I have this in my .emacs file:
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(markdown-header-delimiter-face ((t (:inherit font-lock-function-name-face :underline t :weight bold))) t)
'(markdown-header-face-1 ((t (:inherit markdown-header-face :height 1.5))) t)
'(markdown-header-face-2 ((t (:inherit markdown-header-face :height 1.3))) t)
'(markdown-header-face-3 ((t (:inherit markdown-header-face :underline t :height 1.2))) t)
'(markdown-header-face-4 ((t (:inherit markdown-header-face :underline t :height 1.1))) t)
'(markdown-header-face-5 ((t (:inherit markdown-header-face :underline t))) t)
'(markdown-header-face-6 ((t (:inherit markdown-header-face :underline t))) t))
(put 'set-goal-column 'disabled nil)
which enlarges all the headlines. Markdown-mode itself will italicize starred text and boldface double-starred text, so this will get you there. However, it will not make the control characters invisible.
If you want that, you should probably look into pretty-lambda for examples (as Bozhidar Batsov suggested)

Depending on the context where you'd like to see the rendered text, you might be able to get pretty close to what you need by just tweaking markdown-mode's font-lock rules.

Apart from pretty-lambda (which has already been suggested multiple times), you could look into org-mode and its syntax highliting rules. In particular, the org-hide-emphasis-markers variable does more or less what you want (see e.g. how org-do-emphasis-faces hide markup characters).

As I don't know about any emacs-based Markdown parser, I'd have to say that you have to code one from scratch. Maybe this SO question can throw some pointers at you.
If you opt to go through the Emacs-only road, then Semantic is an excellent API for that job (it offers you a lexer, parser-generator, and parser; it's been around for more than a decade, and it has documentation!). After having the language parser, you'll have to make some rendering functions for each token type. And this way you can customize everything.
Although this would be an enlightening trip, for sure, I'd still opt for using an existing Markdown->html converter on a separate background process and then use w3(m) for emacs preview (as Bozhidar suggested). It gets the job done and it's much simpler to do. No major performance issues, neither - you should run this tool rather scarcely, so you can easily spare some extra milliseconds).
A mixed solution would be to make the Markdown parser generate HTML directly, and preview it on a w3(m) buffer (it takes away the rendering weight from your shoulders, you only need to transliterate the markdown into html, and that seems pretty straight forward with Semantic).

If it is only about the rendering, go with Bozhidar's suggestion and do a Markdown to HTML conversion, then display the HTML in a W3 buffer. markdown-mode has code to call the external Markdown command with a few goodies.
But if you really want to do everything within Emacs Lisp, you'll have to write a Markdown parser first.

Me too, I've been looking for something like this for a very long time. The best I could find, though, is not am Emacs solution, it is an independent great piece of software called ReText.

Related

How to disable current line "highlighting" in emacs (doom emacs)

I just installed Doom Emacs, and I get this screen:
picture link
I'd like for this "highlighting" feature to go away.
I thought it was hl-line-mode or global-hl-line-mode, but disabling those have no effect.
Using Emacs 27.2 on macOS 10.15.7 on Doom Emacs commit 2731685 (the current head).
Doom sets up a variable to control which modes it shows hl-line
global-hl-line-modes ;; What modes to enable hl-line-mode in.
I like to use hl-line mode at times, but disliked it appearing as a default so:
(setq global-hl-line-modes nil)
It should either be a list of modes, or nil.
;; for example...
(setq global-hl-line-modes '(prog-modes markdown-mode))
But for the love of Pete... don't fix this problem with a color change uurgh!!.
Forcibly change the background color:
(custom-set-faces
'(default ((t (:background "#000000"))))
'(hl-line ((t (:background "#000000"))))
)
add this to the end of ~/.doomd.d/config.el.

emacs doc-view new frame

I am new to Emacs and presently I am using it heavily for LaTeXing.
Please help me out with the following customizations:
How to scroll continuously in doc-view-mode? I have
(setq doc-view-continuous t)
in my .emacs file. This enables scrolling through the pages, however, the pages "jump" to the next one. I do not like reading to the bottom of the screen. Is it possible to resolve it?
I invoke doc-view using C-c C-c and the PDF loads into a new window. Is it possible to load it in a new frame?
I have used
(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)
in my .emacs file. This works fine. However, the first line is just below the top screen. Can I create some margin ONLY on top?
How do I copy/paste from Emacs to other application, like a browser? I couldn't copy the code above using C-w in Emacs and then Ctrl-v in Iceweasel (browser). I had to use Kate, sadly. (This I realized while typing this question!)
Regards,
Saurav Agarwal
You should be able to scroll "line by line" with C-n and C-p.
I do not know that mode (I use tex-mode), but what you probably want is to find out how C-c C-c is invoking doc-view and use it with other-window, for example:
(defun new-frame-dvi-file ()
(interactive)
(split-window-right)
(other-window 1)
(tex-view))
I could not find anything that sets a top margin ONLY, but found this:
(set-frame-parameter nil 'internal-border-width 10)
You can share clipboards with this:
(setq x-select-enable-clipboard t)
Anyway, even if it sounds really boring, sometimes it is really useful to take a look at the manual. Sometimes you don't need to read it all and you can find the answer quickly ;-)
Hope it helps!

Emacs linum highlighing

I've recently moved to emacs and have a pretty simple question: when I'm on an specific line, linum changes it's foreground and background color. How can I disable this? What I'm trying to disable is the yellow foreground and grey background. I just get slightly distracted with it.
You seem to have linum-relative installed, it uses the face linum-relative-current-face to highlight current line, you can customize it so that it is less distractive. Here is an example of disabling the highlight completely
(set-face-attribute 'linum-relative-current-face nil :background nil :foreground nil :weight 'normal)

Emacs Linum-mode no fringe in Cocoa

I have the issue visualized here:
The problem is the fringe is not colouring the background of my linum line numbers. I'm using Emacs 24.3.1 with Solarized color theme.
I've looked over various threads, which claims to fix this issue. However, I couldn't get any of them to work.
Any clues?
Try M-x customize-face linum and then Show All Attributes. Change the value of the Background variable and it should change. Easier than having to edit the actual theme file.
(set-face-attribute 'fringe nil :background "white" :foreground "white")
Set the fringe color to whatever you want.

Emacs custom background color by mode

I use emacs to edit a number of file types, and would like an easy visual queue to tell .c files from .vhd or .py files, for instance. How can I add a custom background color to the major mode for that language?
You can do this via hooks. Among other things you can hook is when a new major mode starts. Put something like this into your .emacs file, and emacs will set your background color to purple every time you go into Python mode.
(add-hook 'python-mode-hook
(lambda ()
(set-background-color "purple")))
Resetting the background color to the default in the case that you switch back to a mode that doesn't have an explicit set-background hook for it is left as an exercise for the reader.
You can also manually set the background color with M-x set-background-color
For posterity, as this thread is 4 years old, it is now possible in Emacs 24.4+ to change faces on a buffer local level. Simply define a face and use (face-remap-add-relative) to swap out whatever face you want with it.
Define a defface:
(defface my-special-face '((t :background "aqua")))
Then add a hook to the mode of your choice:
(add-hook 'python-mode-hook
(lambda ()
(face-remap-add-relative 'default 'my-special-face)))
You cannot set the background color on a buffer-by-buffer basis. See the SU question How can I change the background colour of a single emacs buffer?.
The first answer there shows how you can change the background for a single Emacs frame, which might work for you if you have one frame per file (or per mode).