diff --git a/README.org b/README.org index 831ef8e..3c38584 100644 --- a/README.org +++ b/README.org @@ -1,5 +1,6 @@ #+TITLE: ElDoc box -This package displays ElDoc documentations in a childframe. + +This package displays ElDoc documentations in a childframe. The childfrme is selectable and scrollable with mouse, even thought the cursor is hidden. [[https://melpa.org/#/eldoc-box][file:https://melpa.org/packages/eldoc-box-badge.svg]] [[https://stable.melpa.org/#/eldoc-box][file:https://stable.melpa.org/packages/eldoc-box-badge.svg]] @@ -46,6 +47,18 @@ It is also available on [[https://melpa.org/#/eldoc-box][MELPA]]. To keep eldoc from displaying documentation at point without enabling any minor mode above: =(add-to-list 'eglot-ignored-server-capabilites :hoverProvider)=. +** Default prettifier + +By default, eldoc-box tries to prettify the displayed markdown documentation as shown below. If you wish to disable them, remove the prettifier functions from =eldoc-box-buffer-hook=. Report an issue if there are other things can be prettfied away. + +Before: + +[[./before.png]] + +After: + +[[./after.png]] + * Credit - Thanks to for [[https://github.com/joaotavora][João Távora]] for valuable contribution and explaining eldoc and eglot internals to me. diff --git a/after.png b/after.png new file mode 100644 index 0000000..1d37699 Binary files /dev/null and b/after.png differ diff --git a/before.png b/before.png new file mode 100644 index 0000000..70f5609 Binary files /dev/null and b/before.png differ diff --git a/eldoc-box.el b/eldoc-box.el index 90805fb..baa0ddb 100644 --- a/eldoc-box.el +++ b/eldoc-box.el @@ -161,9 +161,14 @@ It will be passes with two arguments: WIDTH and HEIGHT of the childframe.") "T means fringe's background color is set to as same as that of default." :type 'boolean) -(defvar eldoc-box-buffer-hook nil +(defvar eldoc-box-buffer-hook '(eldoc-box--prettify-markdown-separator + eldoc-box--replace-en-space + eldoc-box--remove-linked-images + eldoc-box--fontify-html-header + eldoc-box--condense-large-newline-gaps) "Hook run after buffer for doc is setup. -Run inside the new buffer.") +Run inside the new buffer. By default, it contains some Markdown +prettifiers, which see.") (defvar eldoc-box-frame-hook nil "Hook run after doc frame is setup but just before it is made visible. @@ -608,6 +613,80 @@ You can use \[keyboard-quit] to hide the doc." (or (line-number-display-width t) 0)) nil)) +;;;; Markdown compatibility + +(defun eldoc-box--prettify-markdown-separator () + "Prettify the markdown separator in doc returned by Eglot. +Refontify the separator so they span exactly the width of the +childframe." + (save-excursion + (goto-char (point-min)) + (let (prop) + (while (setq prop (text-property-search-forward 'markdown-hr)) + (add-text-properties (prop-match-beginning prop) + (prop-match-end prop) + '( display (space :width text) + face ( :strike-through t + :height 0.4))))))) + +(defun eldoc-box--replace-en-space () + "Replace the en spaces in documentation with regular spaces." + (save-excursion + (goto-char (point-min)) + (while (search-forward " " nil t) + (replace-match " ")))) + +(defun eldoc-box--condense-large-newline-gaps () + "Condense exceedingly large gaps made of consecutive newlines. + +These gaps are usually made of hidden \"```\" and/or consecutive +newlines. Replace those gaps with a single empty line at 0.5 line +height." + (save-excursion + (goto-char (point-min)) + (while (re-search-forward + (rx (>= 2 (or "\n" (seq "```" (+ (syntax word))) "
" + (seq bol (+ (or " " "\t" " ")) eol)))) + nil t) + (if (or (eq (match-beginning 0) (point-min)) + (eq (match-end 0) (point-max))) + (replace-match "") + (replace-match "\n\n") + (add-text-properties (1- (point)) (point) + '( font-lock-face (:height 0.4) + face (:height 0.4))))))) + +(defun eldoc-box--remove-linked-images () + "Some documentation embed image links in the doc...remove them." + (save-excursion + (goto-char (point-min)) + ;; Find every Markdown image link, and remove them. + (while (re-search-forward + (rx "[" (seq "![" (+? anychar) "](" (+? anychar) ")") "]" + "(" (+? anychar) ")") + nil t) + (replace-match "")))) + +(defun eldoc-box--fontify-html-header () + "Fontify

tags." + (save-excursion + (goto-char (point-min)) + (while (re-search-forward + (rx bol + (group "") + (group (*? anychar)) + (group "") + eol) + nil t) + (add-text-properties (match-beginning 2) + (match-end 2) + '( face (:weight bold) + font-lock-face (:weight bold))) + (put-text-property (match-beginning 1) (match-end 1) + 'invisible t) + (put-text-property (match-beginning 3) (match-end 3) + 'invisible t)))) + (provide 'eldoc-box) ;;; eldoc-box.el ends here