emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/markdown-mode b1a862f016 5/5: Merge pull request #807 from


From: ELPA Syncer
Subject: [nongnu] elpa/markdown-mode b1a862f016 5/5: Merge pull request #807 from jrblevin/issue-804
Date: Sat, 28 Oct 2023 07:01:38 -0400 (EDT)

branch: elpa/markdown-mode
commit b1a862f0165b7bafe0f874738a55be1b1720dd7d
Merge: 1cf1c1b270 d419a1cb16
Author: Shohei YOSHIDA <syohex@gmail.com>
Commit: GitHub <noreply@github.com>

    Merge pull request #807 from jrblevin/issue-804
    
    Implement yank-media handler
---
 CHANGES.md       |  2 ++
 markdown-mode.el | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)

diff --git a/CHANGES.md b/CHANGES.md
index 648bb6298b..6f0df291e6 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
diff --git a/markdown-mode.el b/markdown-mode.el
index 82901b1443..0771060ca7 100644
--- a/markdown-mode.el
+++ b/markdown-mode.el
@@ -51,6 +51,13 @@
 
 (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")
+(declare-function file-name-with-extension "files")
+(declare-function yank-media-handler "yank-media")
 
 
 ;;; Constants =================================================================
@@ -9836,6 +9843,48 @@ 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))))))
+
+(defun markdown--dnd-local-file-handler (url _action)
+  (require 'mailcap)
+  (require 'dnd)
+  (let* ((filename (dnd-get-local-file-name url))
+         (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 ">")))
+    (if (string-prefix-p "image/" mimetype)
+        (markdown-insert-inline-image link-text file)
+      (markdown-insert-inline-link link-text file))))
+
 
 ;;; Mode Definition  ==========================================================
 
@@ -9964,6 +10013,16 @@ 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 support other than GNOME, 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))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]