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

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

[nongnu] elpa/markdown-mode 3ec4cf78d6 1/5: Implement yank-media handler


From: ELPA Syncer
Subject: [nongnu] elpa/markdown-mode 3ec4cf78d6 1/5: Implement yank-media handler
Date: Sat, 28 Oct 2023 07:01:38 -0400 (EDT)

branch: elpa/markdown-mode
commit 3ec4cf78d6f0f6037e9b557c4a170886e4e59593
Author: Shohei YOSHIDA <syohex@gmail.com>
Commit: Shohei YOSHIDA <syohex@gmail.com>

    Implement yank-media handler
---
 markdown-mode.el | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/markdown-mode.el b/markdown-mode.el
index 82901b1443..cb328c8280 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))



reply via email to

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