Last active
December 29, 2023 19:09
-
-
Save kdmsnr/ed44f4465f4f4037558456870af6f747 to your computer and use it in GitHub Desktop.
Markdown で [[WikiLink]] を開きたい
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Markdown で [[WikiLink]] を開きたい | |
(defun open-wiki-link (link-name) | |
(let ((directories '("." "books")) | |
(file-found nil)) | |
(dolist (dir directories) | |
(let ((path (concat dir "/" link-name ".md"))) | |
(when (and (not file-found) (file-exists-p path)) | |
(setq file-found t) | |
(find-file path)))) | |
(unless file-found | |
(message "File not found for link: %s" link-name)))) | |
(defun open-wiki-link-at-point () | |
(interactive) | |
(let ((start nil) | |
(end nil) | |
(link-name nil) | |
(line-start (line-beginning-position)) | |
(line-end (line-end-position))) | |
(save-excursion | |
(goto-char (point)) | |
(when (search-backward "[[" line-start t) | |
(setq start (point))) | |
(goto-char (point)) | |
(when (search-forward "]]" line-end t) | |
(setq end (point)))) | |
(when (and start end (> end start)) | |
(setq link-name (buffer-substring-no-properties (+ start 2) (- end 2))) | |
(open-wiki-link link-name)))) | |
(defun markdown-wiki-link-setup () | |
(setq markdown-wiki-link-regexp "\\[\\[\\(.*?\\)\\]\\]") | |
(add-to-list 'markdown-mode-font-lock-keywords | |
(cons markdown-wiki-link-regexp '(1 'link))) | |
(defun open-wiki-link-at-point-conditional () | |
"Open wiki link at point if on a link, otherwise execute default return key action." | |
(interactive) | |
(if (eq (get-text-property (point) 'face) 'link) | |
(open-wiki-link-at-point) | |
(markdown-enter-key))) | |
(local-set-key (kbd "<return>") 'open-wiki-link-at-point-conditional)) | |
(add-hook 'markdown-mode-hook 'markdown-wiki-link-setup) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment