From a429db57b6fc60d6bd026bd530cdb80554101622 Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Fri, 14 Mar 2025 05:43:36 +0100 Subject: Upgrade markdown-mode --- .emacs.d/lisp/markdown-mode.el | 78 ++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 22 deletions(-) (limited to '.emacs.d') diff --git a/.emacs.d/lisp/markdown-mode.el b/.emacs.d/lisp/markdown-mode.el index a914eb3..311050f 100644 --- a/.emacs.d/lisp/markdown-mode.el +++ b/.emacs.d/lisp/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") @@ -108,7 +109,7 @@ Any changes to the output buffer made by this hook will be saved.") :group 'text :link '(url-link "https://jblevins.org/projects/markdown-mode/")) -(defcustom markdown-command (let ((command (cl-loop for cmd in '("markdown" "pandoc" "markdown_py") +(defcustom markdown-command (let ((command (cl-loop for cmd in '("markdown" "pandoc" "markdown_py" "cmark" "cmark-gfm") when (executable-find cmd) return (file-name-nondirectory it)))) (or command "markdown")) @@ -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 ============================================== @@ -7694,8 +7709,8 @@ Return the name of the output buffer used." (erase-buffer)) (if (stringp command) (if (not (null command-args)) - (apply #'call-process-region begin-region end-region command nil buf nil command-args) - (call-process-region begin-region end-region command nil buf)) + (apply #'call-process-region begin-region end-region command nil (list buf nil) nil command-args) + (call-process-region begin-region end-region command nil (list buf nil))) (if markdown-command-needs-filename (if (not buffer-file-name) (user-error "Must be visiting a file") @@ -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))) @@ -10197,10 +10231,10 @@ rows and columns and the column alignment." ;; Add a buffer-local hook to reload after file-local variables are read (add-hook 'hack-local-variables-hook #'markdown-handle-local-variables nil t) ;; For imenu support - (setq imenu-create-index-function - (if markdown-nested-imenu-heading-index - #'markdown-imenu-create-nested-index - #'markdown-imenu-create-flat-index)) + (setq-local imenu-create-index-function (if markdown-nested-imenu-heading-index + #'markdown-imenu-create-nested-index + #'markdown-imenu-create-flat-index) + imenu-submenus-on-top nil) ;; Defun movement (setq-local beginning-of-defun-function #'markdown-beginning-of-defun) -- cgit v1.3