summaryrefslogtreecommitdiff
path: root/.emacs.d/lisp/markdown-mode.el
diff options
context:
space:
mode:
Diffstat (limited to '.emacs.d/lisp/markdown-mode.el')
-rw-r--r--.emacs.d/lisp/markdown-mode.el78
1 files changed, 72 insertions, 6 deletions
diff --git a/.emacs.d/lisp/markdown-mode.el b/.emacs.d/lisp/markdown-mode.el
index 3abecce..0771060 100644
--- a/.emacs.d/lisp/markdown-mode.el
+++ b/.emacs.d/lisp/markdown-mode.el
@@ -51,6 +51,13 @@
51 51
52(declare-function project-roots "project") 52(declare-function project-roots "project")
53(declare-function sh-set-shell "sh-script") 53(declare-function sh-set-shell "sh-script")
54(declare-function mailcap-file-name-to-mime-type "mailcap")
55(declare-function dnd-get-local-file-name "dnd")
56
57;; for older emacs<29
58(declare-function mailcap-mime-type-to-extension "mailcap")
59(declare-function file-name-with-extension "files")
60(declare-function yank-media-handler "yank-media")
54 61
55 62
56;;; Constants ================================================================= 63;;; Constants =================================================================
@@ -73,6 +80,13 @@
73(defvar markdown-gfm-language-history nil 80(defvar markdown-gfm-language-history nil
74 "History list of languages used in the current buffer in GFM code blocks.") 81 "History list of languages used in the current buffer in GFM code blocks.")
75 82
83(defvar markdown-follow-link-functions nil
84 "Functions used to follow a link.
85Each function is called with one argument, the link's URL. It
86should return non-nil if it followed the link, or nil if not.
87Functions are called in order until one of them returns non-nil;
88otherwise the default link-following function is used.")
89
76 90
77;;; Customizable Variables ==================================================== 91;;; Customizable Variables ====================================================
78 92
@@ -7923,11 +7937,11 @@ If the link is a complete URL, open in browser with `browse-url'.
7923Otherwise, open with `find-file' after stripping anchor and/or query string. 7937Otherwise, open with `find-file' after stripping anchor and/or query string.
7924Translate filenames using `markdown-filename-translate-function'." 7938Translate filenames using `markdown-filename-translate-function'."
7925 (interactive (list last-command-event)) 7939 (interactive (list last-command-event))
7926 (save-excursion 7940 (if event (posn-set-point (event-start event)))
7927 (if event (posn-set-point (event-start event))) 7941 (if (markdown-link-p)
7928 (if (markdown-link-p) 7942 (or (run-hook-with-args-until-success 'markdown-follow-link-functions (markdown-link-url))
7929 (markdown--browse-url (markdown-link-url)) 7943 (markdown--browse-url (markdown-link-url)))
7930 (user-error "Point is not at a Markdown link or URL")))) 7944 (user-error "Point is not at a Markdown link or URL")))
7931 7945
7932(defun markdown-fontify-inline-links (last) 7946(defun markdown-fontify-inline-links (last)
7933 "Add text properties to next inline link from point to LAST." 7947 "Add text properties to next inline link from point to LAST."
@@ -8337,7 +8351,7 @@ See `markdown-follow-link-at-point' and
8337`markdown-follow-wiki-link-at-point'." 8351`markdown-follow-wiki-link-at-point'."
8338 (interactive "P") 8352 (interactive "P")
8339 (cond ((markdown-link-p) 8353 (cond ((markdown-link-p)
8340 (markdown--browse-url (markdown-link-url))) 8354 (markdown-follow-link-at-point))
8341 ((markdown-wiki-link-p) 8355 ((markdown-wiki-link-p)
8342 (markdown-follow-wiki-link-at-point arg)) 8356 (markdown-follow-wiki-link-at-point arg))
8343 (t 8357 (t
@@ -9829,6 +9843,48 @@ rows and columns and the column alignment."
9829 (markdown--substitute-command-keys 9843 (markdown--substitute-command-keys
9830 "\\[markdown-toggle-markup-hiding]")))))) 9844 "\\[markdown-toggle-markup-hiding]"))))))
9831 9845
9846(defun markdown--image-media-handler (mimetype data)
9847 (let* ((ext (symbol-name (mailcap-mime-type-to-extension mimetype)))
9848 (filename (read-string "Insert filename for image: "))
9849 (link-text (read-string "Link text: "))
9850 (filepath (file-name-with-extension filename ext))
9851 (dir (file-name-directory filepath)))
9852 (when (and dir (not (file-directory-p dir)))
9853 (make-directory dir t))
9854 (with-temp-file filepath
9855 (insert data))
9856 (when (string-match-p "\\s-" filepath)
9857 (setq filepath (concat "<" filepath ">")))
9858 (markdown-insert-inline-image link-text filepath)))
9859
9860(defun markdown--file-media-handler (_mimetype data)
9861 (let* ((data (split-string data "[\0\r\n]" t "^file://"))
9862 (files (cdr data)))
9863 (while (not (null files))
9864 (let* ((file (url-unhex-string (car files)))
9865 (file (file-relative-name file))
9866 (prompt (format "Link text(%s): " (file-name-nondirectory file)))
9867 (link-text (read-string prompt)))
9868 (when (string-match-p "\\s-" file)
9869 (setq file (concat "<" file ">")))
9870 (markdown-insert-inline-image link-text file)
9871 (when (not (null (cdr files)))
9872 (insert " "))
9873 (setq files (cdr files))))))
9874
9875(defun markdown--dnd-local-file-handler (url _action)
9876 (require 'mailcap)
9877 (require 'dnd)
9878 (let* ((filename (dnd-get-local-file-name url))
9879 (mimetype (mailcap-file-name-to-mime-type filename))
9880 (file (file-relative-name filename))
9881 (link-text "link text"))
9882 (when (string-match-p "\\s-" file)
9883 (setq file (concat "<" file ">")))
9884 (if (string-prefix-p "image/" mimetype)
9885 (markdown-insert-inline-image link-text file)
9886 (markdown-insert-inline-link link-text file))))
9887
9832 9888
9833;;; Mode Definition ========================================================== 9889;;; Mode Definition ==========================================================
9834 9890
@@ -9957,6 +10013,16 @@ rows and columns and the column alignment."
9957 (add-hook 'electric-quote-inhibit-functions 10013 (add-hook 'electric-quote-inhibit-functions
9958 #'markdown--inhibit-electric-quote nil :local) 10014 #'markdown--inhibit-electric-quote nil :local)
9959 10015
10016 ;; drag and drop handler
10017 (setq-local dnd-protocol-alist (cons '("^file:///" . markdown--dnd-local-file-handler)
10018 dnd-protocol-alist))
10019
10020 ;; media handler
10021 (when (version< "29" emacs-version)
10022 (yank-media-handler "image/.*" #'markdown--image-media-handler)
10023 ;; TODO support other than GNOME, like KDE etc
10024 (yank-media-handler "x-special/gnome-copied-files" #'markdown--file-media-handler))
10025
9960 ;; Make checkboxes buttons 10026 ;; Make checkboxes buttons
9961 (when markdown-make-gfm-checkboxes-buttons 10027 (when markdown-make-gfm-checkboxes-buttons
9962 (markdown-make-gfm-checkboxes-buttons (point-min) (point-max)) 10028 (markdown-make-gfm-checkboxes-buttons (point-min) (point-max))