Skip to content

Commit 5bf2987

Browse files
authored
Merge pull request #886 from jrblevin/issue-885
Implement D&D handler like org-mode
2 parents 6fc5904 + b194469 commit 5bf2987

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- GNU Emacs 28.1 or later is required.
77

88
* New Features:
9+
- Introduce `markdown-yank-dnd-method` what action to perform on the dropped files
10+
like `org-mode`.
911

1012
* Bug fixes:
1113

markdown-mode.el

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
(declare-function sh-set-shell "sh-script")
5454
(declare-function mailcap-file-name-to-mime-type "mailcap")
5555
(declare-function dnd-get-local-file-name "dnd")
56+
(declare-function dnd-open-local-file "dnd")
5657

5758
;; for older emacs<29
5859
(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
705706
(const :tag "on: before closing tags first" t)
706707
(const :tag "reversed: after closing tags first" reversed))))
707708
:package-version '(markdown-mode . "2.7"))
709+
710+
(defcustom markdown-yank-dnd-method 'file-link
711+
"Action to perform on the dropped files.
712+
When the value is the symbol,
713+
- `copy-and-insert' -- copy file in current directory and insert its link
714+
- `open' -- open dropped file in Emacs
715+
- `insert-link' -- insert link of dropped/pasted file
716+
- `ask' -- ask what to do out of the above."
717+
:group 'markdown
718+
:package-version '(markdown-mode "2.8")
719+
:type '(choice (const :tag "Copy and insert" copy-and-insert)
720+
(const :tag "Open file" open)
721+
(const :tag "Insert file link" file-link)
722+
(const :tag "Ask what to do" ask)))
708723

709724
;;; Markdown-Specific `rx' Macro ==============================================
710725

@@ -9898,12 +9913,8 @@ indicate that sorting should be done in reverse order."
98989913
(t 1))))
98999914
(sorting-type
99009915
(or sorting-type
9901-
(progn
9902-
;; workaround #641
9903-
;; Emacs < 28 hides prompt message by another message. This erases it.
9904-
(message "")
9905-
(read-char-exclusive
9906-
"Sort type: [a]lpha [n]umeric (A/N means reversed): ")))))
9916+
(read-char-exclusive
9917+
"Sort type: [a]lpha [n]umeric (A/N means reversed): "))))
99079918
(save-restriction
99089919
;; Narrow buffer to appropriate sorting area
99099920
(if (region-active-p)
@@ -10119,18 +10130,41 @@ rows and columns and the column alignment."
1011910130
(insert " "))
1012010131
(setq files (cdr files))))))
1012110132

10122-
(defun markdown--dnd-local-file-handler (url _action)
10133+
(defun markdown--dnd-read-method ()
10134+
(let ((choice (read-multiple-choice "What to do with file?"
10135+
'((?c "copy and insert")
10136+
(?o "open")
10137+
(?i "insert link")))))
10138+
(cl-case (car choice)
10139+
(?c 'copy-and-insert)
10140+
(?o 'open)
10141+
(?i 'insert)
10142+
(otherwise (markdown--dnd-read-method)))))
10143+
10144+
(defun markdown--dnd-insert-path (filename)
10145+
(let ((mimetype (mailcap-file-name-to-mime-type filename))
10146+
(link-text "link text"))
10147+
(when (string-match-p "\\s-" filename)
10148+
(setq filename (concat "<" filename ">")))
10149+
(if (string-prefix-p "image/" mimetype)
10150+
(markdown-insert-inline-image link-text filename)
10151+
(markdown-insert-inline-link link-text filename))))
10152+
10153+
(defun markdown--dnd-local-file-handler (url action)
1012310154
(require 'mailcap)
1012410155
(require 'dnd)
1012510156
(let* ((filename (dnd-get-local-file-name url))
10126-
(mimetype (mailcap-file-name-to-mime-type filename))
10127-
(file (file-relative-name filename))
10128-
(link-text "link text"))
10129-
(when (string-match-p "\\s-" file)
10130-
(setq file (concat "<" file ">")))
10131-
(if (string-prefix-p "image/" mimetype)
10132-
(markdown-insert-inline-image link-text file)
10133-
(markdown-insert-inline-link link-text file))))
10157+
(method (if (eq markdown-yank-dnd-method 'ask)
10158+
(markdown--dnd-read-method)
10159+
markdown-yank-dnd-method)))
10160+
(cl-case method
10161+
(copy-and-insert
10162+
(let ((copied (expand-file-name (file-name-nondirectory filename))))
10163+
(copy-file filename copied)
10164+
(markdown--dnd-insert-path (file-relative-name copied))))
10165+
(open
10166+
(dnd-open-local-file url action))
10167+
(insert (markdown--dnd-insert-path (file-relative-name filename))))))
1013410168

1013510169
(defun markdown--dnd-multi-local-file-handler (urls action)
1013610170
(let ((multile-urls-p (> (length urls) 1)))

0 commit comments

Comments
 (0)