diff options
Diffstat (limited to '.emacs.d/lisp/markdown-mode.el')
| -rw-r--r-- | .emacs.d/lisp/markdown-mode.el | 79 |
1 files changed, 60 insertions, 19 deletions
diff --git a/.emacs.d/lisp/markdown-mode.el b/.emacs.d/lisp/markdown-mode.el index 77f9bf5..ddeaf7b 100644 --- a/.emacs.d/lisp/markdown-mode.el +++ b/.emacs.d/lisp/markdown-mode.el | |||
| @@ -38,6 +38,7 @@ | |||
| 38 | (require 'thingatpt) | 38 | (require 'thingatpt) |
| 39 | (require 'cl-lib) | 39 | (require 'cl-lib) |
| 40 | (require 'url-parse) | 40 | (require 'url-parse) |
| 41 | (require 'url-util) | ||
| 41 | (require 'button) | 42 | (require 'button) |
| 42 | (require 'color) | 43 | (require 'color) |
| 43 | (require 'rx) | 44 | (require 'rx) |
| @@ -2253,6 +2254,9 @@ Depending on your font, some reasonable choices are: | |||
| 2253 | (2 'markdown-markup-face) | 2254 | (2 'markdown-markup-face) |
| 2254 | (3 'markdown-metadata-value-face))) | 2255 | (3 'markdown-metadata-value-face))) |
| 2255 | (markdown-fontify-hrs) | 2256 | (markdown-fontify-hrs) |
| 2257 | (,markdown-regex-strike-through . ((3 markdown-markup-properties) | ||
| 2258 | (4 'markdown-strike-through-face) | ||
| 2259 | (5 markdown-markup-properties))) | ||
| 2256 | (markdown-match-code . ((1 markdown-markup-properties prepend) | 2260 | (markdown-match-code . ((1 markdown-markup-properties prepend) |
| 2257 | (2 'markdown-inline-code-face prepend) | 2261 | (2 'markdown-inline-code-face prepend) |
| 2258 | (3 markdown-markup-properties prepend))) | 2262 | (3 markdown-markup-properties prepend))) |
| @@ -2318,9 +2322,6 @@ Depending on your font, some reasonable choices are: | |||
| 2318 | (markdown-match-italic . ((1 markdown-markup-properties prepend) | 2322 | (markdown-match-italic . ((1 markdown-markup-properties prepend) |
| 2319 | (2 'markdown-italic-face append) | 2323 | (2 'markdown-italic-face append) |
| 2320 | (3 markdown-markup-properties prepend))) | 2324 | (3 markdown-markup-properties prepend))) |
| 2321 | (,markdown-regex-strike-through . ((3 markdown-markup-properties) | ||
| 2322 | (4 'markdown-strike-through-face) | ||
| 2323 | (5 markdown-markup-properties))) | ||
| 2324 | (markdown--match-highlighting . ((3 markdown-markup-properties) | 2325 | (markdown--match-highlighting . ((3 markdown-markup-properties) |
| 2325 | (4 'markdown-highlighting-face) | 2326 | (4 'markdown-highlighting-face) |
| 2326 | (5 markdown-markup-properties))) | 2327 | (5 markdown-markup-properties))) |
| @@ -3282,11 +3283,18 @@ processed elements." | |||
| 3282 | (markdown-end-of-text-block) | 3283 | (markdown-end-of-text-block) |
| 3283 | (point)))) | 3284 | (point)))) |
| 3284 | ;; Move over balanced expressions to closing right bracket. | 3285 | ;; Move over balanced expressions to closing right bracket. |
| 3285 | ;; Catch unbalanced expression errors and return nil. | 3286 | ;; Catch unbalanced expression errors, then try to search right bracket manually. |
| 3286 | (first-end (condition-case nil | 3287 | (first-end (condition-case nil |
| 3287 | (and (goto-char first-begin) | 3288 | (and (goto-char first-begin) |
| 3288 | (scan-sexps (point) 1)) | 3289 | (scan-sexps (point) 1)) |
| 3289 | (error nil))) | 3290 | (error |
| 3291 | (save-match-data | ||
| 3292 | (let ((last (match-end 4)) | ||
| 3293 | ok end-pos) | ||
| 3294 | (while (and (not ok) (search-forward "]" last t)) | ||
| 3295 | (unless (= (char-before (1- (point))) ?\\) | ||
| 3296 | (setq ok t end-pos (point)))) | ||
| 3297 | end-pos))))) | ||
| 3290 | ;; Continue with point at CONT-POINT upon failure. | 3298 | ;; Continue with point at CONT-POINT upon failure. |
| 3291 | (cont-point (min (1+ first-begin) last)) | 3299 | (cont-point (min (1+ first-begin) last)) |
| 3292 | second-begin second-end url-begin url-end | 3300 | second-begin second-end url-begin url-end |
| @@ -8160,6 +8168,27 @@ See `markdown-wiki-link-p' for more information." | |||
| 8160 | (thing-at-point-looking-at markdown-regex-uri) | 8168 | (thing-at-point-looking-at markdown-regex-uri) |
| 8161 | (thing-at-point-looking-at markdown-regex-angle-uri)))))) | 8169 | (thing-at-point-looking-at markdown-regex-angle-uri)))))) |
| 8162 | 8170 | ||
| 8171 | (defun markdown--unhex-url-string (url) | ||
| 8172 | "Unhex control characters and spaces in URL. | ||
| 8173 | This is similar to `url-unhex-string' but this doesn't unhex all percent-encoded | ||
| 8174 | characters." | ||
| 8175 | (let ((str (or url "")) | ||
| 8176 | (tmp "") | ||
| 8177 | (case-fold-search t)) | ||
| 8178 | (while (string-match "%\\([01][0-9a-f]\\|20\\)" str) | ||
| 8179 | (let* ((start (match-beginning 0)) | ||
| 8180 | (ch1 (url-unhex (elt str (+ start 1)))) | ||
| 8181 | (code (+ (* 16 ch1) | ||
| 8182 | (url-unhex (elt str (+ start 2)))))) | ||
| 8183 | (setq tmp (concat | ||
| 8184 | tmp (substring str 0 start) | ||
| 8185 | (cond | ||
| 8186 | ((or (= code ?\n) (= code ?\r)) | ||
| 8187 | " ") | ||
| 8188 | (t (byte-to-string code)))) | ||
| 8189 | str (substring str (match-end 0))))) | ||
| 8190 | (concat tmp str))) | ||
| 8191 | |||
| 8163 | (defun markdown-link-at-pos (pos) | 8192 | (defun markdown-link-at-pos (pos) |
| 8164 | "Return properties of link or image at position POS. | 8193 | "Return properties of link or image at position POS. |
| 8165 | Value is a list of elements describing the link: | 8194 | Value is a list of elements describing the link: |
| @@ -8183,7 +8212,9 @@ Value is a list of elements describing the link: | |||
| 8183 | url (match-string-no-properties 6)) | 8212 | url (match-string-no-properties 6)) |
| 8184 | ;; consider nested parentheses | 8213 | ;; consider nested parentheses |
| 8185 | ;; if link target contains parentheses, (match-end 0) isn't correct end position of the link | 8214 | ;; if link target contains parentheses, (match-end 0) isn't correct end position of the link |
| 8186 | (let* ((close-pos (scan-sexps (match-beginning 5) 1)) | 8215 | (let* ((close-pos (condition-case nil |
| 8216 | (scan-sexps (match-beginning 5) 1) | ||
| 8217 | (error (match-end 0)))) | ||
| 8187 | (destination-part (string-trim (buffer-substring-no-properties (1+ (match-beginning 5)) (1- close-pos))))) | 8218 | (destination-part (string-trim (buffer-substring-no-properties (1+ (match-beginning 5)) (1- close-pos))))) |
| 8188 | (setq end close-pos) | 8219 | (setq end close-pos) |
| 8189 | ;; A link can contain spaces if it is wrapped with angle brackets | 8220 | ;; A link can contain spaces if it is wrapped with angle brackets |
| @@ -8193,7 +8224,7 @@ Value is a list of elements describing the link: | |||
| 8193 | (setq url (match-string-no-properties 1 destination-part) | 8224 | (setq url (match-string-no-properties 1 destination-part) |
| 8194 | title (substring (match-string-no-properties 2 destination-part) 1 -1))) | 8225 | title (substring (match-string-no-properties 2 destination-part) 1 -1))) |
| 8195 | (t (setq url destination-part))) | 8226 | (t (setq url destination-part))) |
| 8196 | (setq url (url-unhex-string url)))) | 8227 | (setq url (markdown--unhex-url-string url)))) |
| 8197 | ;; Reference link at point. | 8228 | ;; Reference link at point. |
| 8198 | ((thing-at-point-looking-at markdown-regex-link-reference) | 8229 | ((thing-at-point-looking-at markdown-regex-link-reference) |
| 8199 | (setq bang (match-string-no-properties 1) | 8230 | (setq bang (match-string-no-properties 1) |
| @@ -9728,6 +9759,18 @@ This function assumes point is on a table." | |||
| 9728 | (cl-loop for c across line | 9759 | (cl-loop for c across line |
| 9729 | always (member c '(?| ?- ?: ?\t ? ))))) | 9760 | always (member c '(?| ?- ?: ?\t ? ))))) |
| 9730 | 9761 | ||
| 9762 | (defun markdown-table-align-raw (cells fmtspec widths) | ||
| 9763 | (let (fmt width) | ||
| 9764 | (mapconcat | ||
| 9765 | (lambda (cell) | ||
| 9766 | (setq fmt (car fmtspec) fmtspec (cdr fmtspec)) | ||
| 9767 | (setq width (car widths) widths (cdr widths)) | ||
| 9768 | (if (equal fmt 'c) | ||
| 9769 | (setq cell (concat (make-string (/ (- width (length cell)) 2) ?\s) cell))) | ||
| 9770 | (unless (equal fmt 'r) (setq width (- width))) | ||
| 9771 | (format (format " %%%ds " width) cell)) | ||
| 9772 | cells "|"))) | ||
| 9773 | |||
| 9731 | (defun markdown-table-align () | 9774 | (defun markdown-table-align () |
| 9732 | "Align table at point. | 9775 | "Align table at point. |
| 9733 | This function assumes point is on a table." | 9776 | This function assumes point is on a table." |
| @@ -9752,8 +9795,6 @@ This function assumes point is on a table." | |||
| 9752 | (maxcells (if cells | 9795 | (maxcells (if cells |
| 9753 | (apply #'max (mapcar #'length cells)) | 9796 | (apply #'max (mapcar #'length cells)) |
| 9754 | (user-error "Empty table"))) | 9797 | (user-error "Empty table"))) |
| 9755 | ;; Empty cells to fill short lines | ||
| 9756 | (emptycells (make-list maxcells "")) | ||
| 9757 | maxwidths) | 9798 | maxwidths) |
| 9758 | ;; Calculate maximum width for each column | 9799 | ;; Calculate maximum width for each column |
| 9759 | (dotimes (i maxcells) | 9800 | (dotimes (i maxcells) |
| @@ -9765,20 +9806,20 @@ This function assumes point is on a table." | |||
| 9765 | (setq fmtspec (markdown-table-colfmt fmtspec)) | 9806 | (setq fmtspec (markdown-table-colfmt fmtspec)) |
| 9766 | ;; Compute formats needed for output of table lines | 9807 | ;; Compute formats needed for output of table lines |
| 9767 | (let ((hfmt (concat indent "|")) | 9808 | (let ((hfmt (concat indent "|")) |
| 9768 | (rfmt (concat indent "|")) | 9809 | hfmt1 fmt (fmts fmtspec)) |
| 9769 | hfmt1 rfmt1 fmt) | 9810 | (dolist (width maxwidths) |
| 9770 | (dolist (width maxwidths (setq hfmt (concat (substring hfmt 0 -1) "|"))) | 9811 | (setq fmt (car fmts) fmts (cdr fmts)) |
| 9771 | (setq fmt (pop fmtspec)) | 9812 | (cond ((equal fmt 'l) (setq hfmt1 ":%s-|")) |
| 9772 | (cond ((equal fmt 'l) (setq hfmt1 ":%s-|" rfmt1 " %%-%ds |")) | 9813 | ((equal fmt 'r) (setq hfmt1 "-%s:|")) |
| 9773 | ((equal fmt 'r) (setq hfmt1 "-%s:|" rfmt1 " %%%ds |")) | 9814 | ((equal fmt 'c) (setq hfmt1 ":%s:|")) |
| 9774 | ((equal fmt 'c) (setq hfmt1 ":%s:|" rfmt1 " %%-%ds |")) | 9815 | (t (setq hfmt1 "-%s-|"))) |
| 9775 | (t (setq hfmt1 "-%s-|" rfmt1 " %%-%ds |"))) | ||
| 9776 | (setq rfmt (concat rfmt (format rfmt1 width))) | ||
| 9777 | (setq hfmt (concat hfmt (format hfmt1 (make-string width ?-))))) | 9816 | (setq hfmt (concat hfmt (format hfmt1 (make-string width ?-))))) |
| 9778 | ;; Replace modified lines only | 9817 | ;; Replace modified lines only |
| 9779 | (dolist (line lines) | 9818 | (dolist (line lines) |
| 9780 | (let ((line (if line | 9819 | (let ((line (if line |
| 9781 | (apply #'format rfmt (append (pop cells) emptycells)) | 9820 | (concat indent "|" |
| 9821 | (markdown-table-align-raw (pop cells) fmtspec maxwidths) | ||
| 9822 | "|") | ||
| 9782 | hfmt)) | 9823 | hfmt)) |
| 9783 | (previous (buffer-substring (point) (line-end-position)))) | 9824 | (previous (buffer-substring (point) (line-end-position)))) |
| 9784 | (if (equal previous line) | 9825 | (if (equal previous line) |
