Skip to content

Commit

Permalink
Merge pull request #807 from jrblevin/issue-804
Browse files Browse the repository at this point in the history
Implement yank-media handler
  • Loading branch information
syohex committed Oct 28, 2023
2 parents 1cf1c1b + d419a1c commit b1a862f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][]
Expand All @@ -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
Expand Down
59 changes: 59 additions & 0 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -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 =================================================================
Expand Down Expand Up @@ -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 ==========================================================

Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit b1a862f

Please sign in to comment.