10

I'd like to hide markdown / org-mode markup characters when viewing a file, so I set org-hide-emphasis-markers and markdown-hide-markup to t. However, when editing a section of text, I'd like to see the characters. A function like markdown-toggle-markup-hiding will change the setting for the entire buffer.

Is there an easy way to apply the hiding to the entire buffer except the current line (or paragraph)? That way most of the buffer will be in "view" mode but when I move the cursor to edit something, that line will let me see the formatting characters.

2
  • That sounds expensive to me: emacs would have to recalculate every time you move and although that's possible, it needs care lest it slow down everything to a crawl - maybe it can be done with an idle timer.
    – NickD
    Commented Feb 6, 2019 at 16:49
  • org-appear package seems to do this in org-mode. But it doesn't work in markdown mode.
    – Gangula
    Commented Aug 17 at 17:45

1 Answer 1

6

I have a quick & dirty proof of concept for markdown-mode. Idea: keep track of the current line (similar to hl-line-mode). Use a font-lock rule to remove the markup hiding for the current line (markdown-mode seems to use display and invisible text properties, so remove those). Re-fontify the local area when moving from one line to another.

(defvar my/current-line '(0 . 0)
  "(start . end) of current line in current buffer")
(make-variable-buffer-local 'my/current-line)

(defun my/unhide-current-line (limit)
  "Font-lock function"
  (let ((start (max (point) (car my/current-line)))
        (end (min limit (cdr my/current-line))))
    (when (< start end)
      (remove-text-properties start end 
                      '(invisible t display "" composition ""))
      (goto-char limit)
      t)))

(defun my/refontify-on-linemove ()
  "Post-command-hook"
  (let* ((start (line-beginning-position))
         (end (line-beginning-position 2))
         (needs-update (not (equal start (car my/current-line)))))
    (setq my/current-line (cons start end))
    (when needs-update
      (font-lock-fontify-block 3))))

(defun my/markdown-unhighlight ()
  "Install"
  (markdown-toggle-markup-hiding 1)
  (font-lock-add-keywords nil '((my/unhide-current-line)) t)
  (add-hook 'post-command-hook #'my/refontify-on-linemove nil t))

(add-hook 'markdown-mode-hook #'my/markdown-unhighlight)

I don't know if this is the best way to do it. I'm seeing some occasional errors in the message area but it seems to be working ok so far.

Edit: also works in org mode with (add-hook 'org-mode-hook #'my/markdown-unhighlight)

1
  • This can also be used with org mode. Remove the composition text property to undo org-pretty-entities on the current line.
    – amitp
    Commented Aug 16, 2020 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.