From 3ec4cf78d6f0f6037e9b557c4a170886e4e59593 Mon Sep 17 00:00:00 2001 From: Shohei YOSHIDA Date: Wed, 25 Oct 2023 14:23:11 +0900 Subject: [PATCH 1/4] Implement yank-media handler --- markdown-mode.el | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/markdown-mode.el b/markdown-mode.el index 82901b14..cb328c82 100644 --- a/markdown-mode.el +++ b/markdown-mode.el @@ -52,6 +52,11 @@ (declare-function project-roots "project") (declare-function sh-set-shell "sh-script") +;; for older emacs<29 +(declare-function mailcap-mime-type-to-extension "mailcap") +(declare-function file-name-with-extension "files") +(declare-function yank-media-handler "yank-media") + ;;; Constants ================================================================= @@ -9836,6 +9841,35 @@ rows and columns and the column alignment." (markdown--substitute-command-keys "\\[markdown-toggle-markup-hiding]")))))) +(defun markdown--image-media-handler (mimetype data) + (let* ((ext (symbol-name (mailcap-mime-type-to-extension mimetype))) + (filename (read-string "Insert filename for image: ")) + (link-text (read-string "Link text: ")) + (filepath (file-name-with-extension filename ext)) + (dir (file-name-directory filepath))) + (when (and dir (not (file-directory-p dir))) + (make-directory dir t)) + (with-temp-file filepath + (insert data)) + (when (string-match-p "\\s-" filepath) + (setq filepath (concat "<" filepath ">"))) + (markdown-insert-inline-image link-text filepath))) + +(defun markdown--file-media-handler (_mimetype data) + (let* ((data (split-string data "[\0\r\n]" t "^file://")) + (files (cdr data))) + (while (not (null files)) + (let* ((file (url-unhex-string (car files))) + (file (file-relative-name file)) + (prompt (format "Link text(%s): " (file-name-nondirectory file))) + (link-text (read-string prompt))) + (when (string-match-p "\\s-" file) + (setq file (concat "<" file ">"))) + (markdown-insert-inline-image link-text file) + (when (not (null (cdr files))) + (insert " ")) + (setq files (cdr files)))))) + ;;; Mode Definition ========================================================== @@ -9964,6 +9998,12 @@ rows and columns and the column alignment." (add-hook 'electric-quote-inhibit-functions #'markdown--inhibit-electric-quote nil :local) + ;; media handler + (when (version< "29" emacs-version) + (yank-media-handler "image/.*" #'markdown--image-media-handler) + ;; TODO other than GNOME support, like KDE etc + (yank-media-handler "x-special/gnome-copied-files" #'markdown--file-media-handler)) + ;; Make checkboxes buttons (when markdown-make-gfm-checkboxes-buttons (markdown-make-gfm-checkboxes-buttons (point-min) (point-max)) From c6f63dea9e2cb0271bf6749caa8dae7191aca1db Mon Sep 17 00:00:00 2001 From: Shohei YOSHIDA Date: Fri, 27 Oct 2023 14:28:27 +0900 Subject: [PATCH 2/4] Implement drag and drop handler --- markdown-mode.el | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/markdown-mode.el b/markdown-mode.el index cb328c82..fff60783 100644 --- a/markdown-mode.el +++ b/markdown-mode.el @@ -9870,6 +9870,14 @@ rows and columns and the column alignment." (insert " ")) (setq files (cdr files)))))) +(defun markdown--dnd-local-file-handler (url _action) + (require 'mailcap) + (let* ((filename (dnd-get-local-file-name url)) + (file (file-relative-name filename))) + (when (string-match-p "\\s-" file) + (setq file (concat "<" file ">"))) + (markdown-insert-inline-image "link text" file))) + ;;; Mode Definition ========================================================== @@ -9998,10 +10006,14 @@ rows and columns and the column alignment." (add-hook 'electric-quote-inhibit-functions #'markdown--inhibit-electric-quote nil :local) + ;; drag and drop handler + (setq-local dnd-protocol-alist (cons '("^file:///" . markdown--dnd-local-file-handler) + dnd-protocol-alist)) + ;; media handler (when (version< "29" emacs-version) (yank-media-handler "image/.*" #'markdown--image-media-handler) - ;; TODO other than GNOME support, like KDE etc + ;; TODO support other than GNOME, like KDE etc (yank-media-handler "x-special/gnome-copied-files" #'markdown--file-media-handler)) ;; Make checkboxes buttons From e10e5af7940d19b82cb49d11ee7fedfb9d3c9355 Mon Sep 17 00:00:00 2001 From: Shohei YOSHIDA Date: Fri, 27 Oct 2023 14:30:08 +0900 Subject: [PATCH 3/4] Update CHANGES --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 648bb629..6f0df291 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,7 @@ `markdown-follow-link-at-point` similarly to Org's `org-open-at-point-functions`, allowing other libraries to handle links specially. [GH-780][] + - Support media handler for images and drag and drop images [GH-804][] * Bug fixes: - Don't highlight superscript/subscript in math inline/block [GH-802][] @@ -16,6 +17,7 @@ [gh-780]: https://github.com/jrblevin/markdown-mode/issues/780 [gh-802]: https://github.com/jrblevin/markdown-mode/issues/802 + [gh-804]: https://github.com/jrblevin/markdown-mode/issues/804 [gh-805]: https://github.com/jrblevin/markdown-mode/issues/805 # Markdown Mode 2.6 From d419a1cb16734244b1fd194f3e7eecb721a77c11 Mon Sep 17 00:00:00 2001 From: Shohei YOSHIDA Date: Fri, 27 Oct 2023 21:12:25 +0900 Subject: [PATCH 4/4] Fix byte-compile warnings --- markdown-mode.el | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/markdown-mode.el b/markdown-mode.el index fff60783..0771060c 100644 --- a/markdown-mode.el +++ b/markdown-mode.el @@ -51,6 +51,8 @@ (declare-function project-roots "project") (declare-function sh-set-shell "sh-script") +(declare-function mailcap-file-name-to-mime-type "mailcap") +(declare-function dnd-get-local-file-name "dnd") ;; for older emacs<29 (declare-function mailcap-mime-type-to-extension "mailcap") @@ -9872,11 +9874,16 @@ rows and columns and the column alignment." (defun markdown--dnd-local-file-handler (url _action) (require 'mailcap) + (require 'dnd) (let* ((filename (dnd-get-local-file-name url)) - (file (file-relative-name filename))) + (mimetype (mailcap-file-name-to-mime-type filename)) + (file (file-relative-name filename)) + (link-text "link text")) (when (string-match-p "\\s-" file) (setq file (concat "<" file ">"))) - (markdown-insert-inline-image "link text" file))) + (if (string-prefix-p "image/" mimetype) + (markdown-insert-inline-image link-text file) + (markdown-insert-inline-link link-text file)))) ;;; Mode Definition ==========================================================