Untitled

mail@pastecode.io avatar
unknown
lisp
7 months ago
986 B
1
Indexable
Never
(defun insert-local-file-link-from-clipboard ()
  "Inserts an org-mode link using the clipboard content as the file path."
  (interactive)
  (let* ((clip-content (substring-no-properties (current-kill 0)))
         (file-name (file-name-nondirectory clip-content))
         (file-name-base (file-name-sans-extension file-name))
         (hyphen-and-underscore-replaced (replace-regexp-in-string "[-_]" " " file-name-base))
         (no-leading-numbers (replace-regexp-in-string "^[0-9]*" "" hyphen-and-underscore-replaced))
         (start-pos (point)))
    (insert (format "[[%s][%s]]" clip-content (string-trim no-leading-numbers)))
    (goto-char start-pos)
    (search-forward "[[" nil t)
    (search-forward "[" nil t)
    (let ((title-start (point)))
      (search-forward "]" nil t)
      (backward-char)
      (capitalize-region title-start (point)))
    (goto-char (+ 2 start-pos))
    (search-forward "[" nil t)
    (search-forward "]" nil t))
  nil)  ; Explicitly return nil