branch: elpa/markdown-mode
commit 5bf29873b63e8ad517868607b9bce8516fb92f02
Merge: 6fc5904257 b194469280
Author: Shohei YOSHIDA <syo...@gmail.com>
Commit: GitHub <nore...@github.com>

    Merge pull request #886 from jrblevin/issue-885
    
    Implement D&D handler like org-mode
---
 CHANGES.md       |  2 ++
 markdown-mode.el | 64 +++++++++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 51 insertions(+), 15 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index d3f62d0890..9d1f0379f6 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -6,6 +6,8 @@
     - GNU Emacs 28.1 or later is required.
 
 *   New Features:
+    - Introduce `markdown-yank-dnd-method` what action to perform on the 
dropped files
+      like `org-mode`.
 
 *   Bug fixes:
 
diff --git a/markdown-mode.el b/markdown-mode.el
index 5386c1e7c4..1bdd0b1ec3 100644
--- a/markdown-mode.el
+++ b/markdown-mode.el
@@ -53,6 +53,7 @@
 (declare-function sh-set-shell "sh-script")
 (declare-function mailcap-file-name-to-mime-type "mailcap")
 (declare-function dnd-get-local-file-name "dnd")
+(declare-function dnd-open-local-file "dnd")
 
 ;; for older emacs<29
 (declare-function mailcap-mime-type-to-extension "mailcap")
@@ -705,6 +706,20 @@ This may also be a cons cell where the behavior for `C-a' 
and
                         (const :tag "on: before closing tags first" t)
                         (const :tag "reversed: after closing tags first" 
reversed))))
   :package-version '(markdown-mode . "2.7"))
+
+(defcustom markdown-yank-dnd-method 'file-link
+  "Action to perform on the dropped files.
+When the value is the symbol,
+  - `copy-and-insert' -- copy file in current directory and insert its link
+  - `open' -- open dropped file in Emacs
+  - `insert-link' -- insert link of dropped/pasted file
+  - `ask' -- ask what to do out of the above."
+  :group 'markdown
+  :package-version '(markdown-mode "2.8")
+  :type '(choice (const :tag "Copy and insert" copy-and-insert)
+                 (const :tag "Open file" open)
+                 (const :tag "Insert file link" file-link)
+                 (const :tag "Ask what to do" ask)))
 
 ;;; Markdown-Specific `rx' Macro ==============================================
 
@@ -9898,12 +9913,8 @@ indicate that sorting should be done in reverse order."
                         (t 1))))
         (sorting-type
          (or sorting-type
-             (progn
-               ;; workaround #641
-               ;; Emacs < 28 hides prompt message by another message. This 
erases it.
-               (message "")
-               (read-char-exclusive
-                "Sort type: [a]lpha [n]umeric (A/N means reversed): ")))))
+             (read-char-exclusive
+              "Sort type: [a]lpha [n]umeric (A/N means reversed): "))))
     (save-restriction
       ;; Narrow buffer to appropriate sorting area
       (if (region-active-p)
@@ -10119,18 +10130,41 @@ rows and columns and the column alignment."
           (insert " "))
         (setq files (cdr files))))))
 
-(defun markdown--dnd-local-file-handler (url _action)
+(defun markdown--dnd-read-method ()
+  (let ((choice (read-multiple-choice "What to do with file?"
+                                      '((?c "copy and insert")
+                                        (?o "open")
+                                        (?i "insert link")))))
+    (cl-case (car choice)
+      (?c 'copy-and-insert)
+      (?o 'open)
+      (?i 'insert)
+      (otherwise (markdown--dnd-read-method)))))
+
+(defun markdown--dnd-insert-path (filename)
+  (let ((mimetype (mailcap-file-name-to-mime-type filename))
+        (link-text "link text"))
+    (when (string-match-p "\\s-" filename)
+      (setq filename (concat "<" filename ">")))
+    (if (string-prefix-p "image/" mimetype)
+        (markdown-insert-inline-image link-text filename)
+      (markdown-insert-inline-link link-text filename))))
+
+(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))))
+         (method (if (eq markdown-yank-dnd-method 'ask)
+                     (markdown--dnd-read-method)
+                   markdown-yank-dnd-method)))
+    (cl-case method
+      (copy-and-insert
+       (let ((copied (expand-file-name (file-name-nondirectory filename))))
+         (copy-file filename copied)
+         (markdown--dnd-insert-path (file-relative-name copied))))
+      (open
+       (dnd-open-local-file url action))
+      (insert (markdown--dnd-insert-path (file-relative-name filename))))))
 
 (defun markdown--dnd-multi-local-file-handler (urls action)
   (let ((multile-urls-p (> (length urls) 1)))

Reply via email to