Function: markdown--insert-link-or-image

markdown--insert-link-or-image is a byte-compiled function defined in markdown-mode.el.

Signature

(markdown--insert-link-or-image IMAGE)

Documentation

Interactively insert new or update an existing link or image.

When IMAGE is non-nil, insert an image. Otherwise, insert a link. This is an internal function called by markdown-insert-link and markdown-insert-image.

Source Code

;; Defined in ~/.emacs.d/elpa/markdown-mode-20260321.143/markdown-mode.el
(defun markdown--insert-link-or-image (image)
  "Interactively insert new or update an existing link or image.
When IMAGE is non-nil, insert an image.  Otherwise, insert a link.
This is an internal function called by
`markdown-insert-link' and `markdown-insert-image'."
  (cl-multiple-value-bind (begin end text uri ref title)
      (if (use-region-p)
          ;; Use region as either link text or URL as appropriate.
          (let ((region (buffer-substring-no-properties
                         (region-beginning) (region-end))))
            (if (string-match markdown-regex-uri region)
                ;; Region contains a URL; use it as such.
                (list (region-beginning) (region-end)
                      nil (match-string 0 region) nil nil)
              ;; Region doesn't contain a URL, so use it as text.
              (list (region-beginning) (region-end)
                    region nil nil nil)))
        ;; Extract and use properties of existing link, if any.
        (markdown-link-at-pos (point)))
    (let* ((ref (when ref (concat "[" ref "]")))
           (defined-refs (mapcar #'car (markdown-get-defined-references)))
           (defined-ref-cands (mapcar (lambda (ref) (concat "[" ref "]")) defined-refs))
           (used-uris (markdown-get-used-uris))
           (uri-or-ref (completing-read
                        "URL or [reference]: "
                        (append defined-ref-cands used-uris)
                        nil nil (or uri ref)))
           (ref (cond ((string-match "\\`\\[\\(.*\\)\\]\\'" uri-or-ref)
                       (match-string 1 uri-or-ref))
                      ((string-equal "" uri-or-ref)
                       "")))
           (uri (unless ref uri-or-ref))
           (text-prompt (if image
                            "Alt text: "
                          (if ref
                              "Link text: "
                            "Link text (blank for plain URL): ")))
           (text (or text (and markdown-link-make-text-function uri
                               (funcall markdown-link-make-text-function uri))))
           (text (completing-read text-prompt defined-refs nil nil text))
           (text (if (= (length text) 0) nil text))
           (plainp (and uri (not text)))
           (implicitp (string-equal ref ""))
           (ref (if implicitp text ref))
           (definedp (and ref (markdown-reference-definition ref)))
           (ref-url (unless (or uri definedp)
                      (completing-read "Reference URL: " used-uris)))
           (title (unless (or plainp definedp markdown-disable-tooltip-prompt)
                    (read-string "Title (tooltip text, optional): " title)))
           (title (if (= (length title) 0) nil title)))
      (when (and image implicitp)
        (user-error "Reference required: implicit image references are invalid"))
      (when (and begin end)
        (delete-region begin end))
      (cond
       ((and (not image) uri text)
        (markdown-insert-inline-link text uri title))
       ((and image uri text)
        (markdown-insert-inline-image text uri title))
       ((and ref text)
        (if image
            (markdown-insert-reference-image text (unless implicitp ref) nil title)
          (markdown-insert-reference-link text (unless implicitp ref) nil title))
        (unless definedp
          (markdown-insert-reference-definition ref ref-url title)))
       ((and (not image) uri)
        (markdown-insert-uri uri))))))