Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create an org-link from embark-file-map or insert relative filename. #733

Open
tre-flip opened this issue Oct 2, 2024 · 0 comments
Open

Comments

@tre-flip
Copy link

tre-flip commented Oct 2, 2024

I wrote this command for faster linking of files in org-mode. In other modes it inserts relative filenames. Please, let me know what you think about it.

;; Fast and convenient file links with Embark

(defcustom embark-file-link-separator " "
  "A string separator to insert after the link."
  :type 'string)

(defcustom embark-file-link-filename-replace-chars (list ?_ ?-)
  "Characters in file names to replace with spaces."
  :type 'list)

(defun embark-file-insert-link-dwim-get-title (filename)
  "Converts a FILENAME to a link tiltle."
  (let ((short-fname (file-name-sans-extension (file-name-nondirectory filename))))
	(dolist (ch embark-file-link-filename-replace-chars)
	  (subst-char-in-string ch ?\  short-fname t))
	short-fname))

(defun embark-file-insert-link-dwim (file)
  (let ((link-text "")
		(separator embark-file-link-separator))
	(cond
	 ;; in org-mode insert org-link
	 ((equal major-mode 'org-mode)
	  (if (not (region-active-p))
		  (setq link-text (concat "[" (embark-file-insert-link-dwim-get-title file) "]"))
		(setq link-text (concat "[" (buffer-substring (region-beginning) (region-end)) "]"))
		(kill-region (region-beginning) (region-end))
		(setq separator ""))
          (insert (if (cl-search default-directory  (expand-file-name file))
				  (format "[[./%s]%s]" (file-relative-name file) link-text)
				(format "[[%s]%s]" (f-abbrev file) link-text))))
	 ;; in other modes insert relative path
	 (t
	  (embark-insert-relative-path file)))
	(insert separator)))

(keymap-set embark-file-map "TAB" #'embark-file-insert-link-dwim)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant