Answers:
以下elisp函数将采用识别的当前点周围的链接org-bracket-link-regexp
,因此,[[Link][Description]]
或者[[Link]]
,Description
在第一种情况或Link
第二种情况下将其替换为。
(defun afs/org-replace-link-by-link-description ()
"Replace an org link by its description or if empty its address"
(interactive)
(if (org-in-regexp org-bracket-link-regexp 1)
(let ((remove (list (match-beginning 0) (match-end 0)))
(description (if (match-end 3)
(org-match-string-no-properties 3)
(org-match-string-no-properties 1))))
(apply 'delete-region remove)
(insert description))))
我试图将其添加到@Andrew的答案中,但是评论太久了……
我非常喜欢他的解决方案,除了它可以移动光标。(从技术上讲,我想这是正确的。无论如何...)幸运的是,很容易添加它save-excursion
来避免这种情况:
(defun afs/org-replace-link-by-link-description ()
"Replace an org link by its description or if empty its address"
(interactive)
(if (org-in-regexp org-bracket-link-regexp 1)
(save-excursion
(let ((remove (list (match-beginning 0) (match-end 0)))
(description (if (match-end 3)
(org-match-string-no-properties 3)
(org-match-string-no-properties 1))))
(apply 'delete-region remove)
(insert description)))))
当该点在[[
组织链接的第一个方括号之后的任何地方(或在超链接组织的链接上/之后的任何地方)时,请调用此命令。
如果它是格式的有机链接将被删除[[LINK][DESCRIPTION]]
或[[LINK]]
在一个org-mode
缓冲器中; 否则什么都不会发生。
为了安全起见,kill-ring
如果需要在其他地方使用该链接,则将从org-link中丢弃的LINK保存到。
(defun my/org-delete-link ()
"Replace an org link of the format [[LINK][DESCRIPTION]] with DESCRIPTION.
If the link is of the format [[LINK]], delete the whole org link.
In both the cases, save the LINK to the kill-ring.
Execute this command while the point is on or after the hyper-linked org link."
(interactive)
(when (derived-mode-p 'org-mode)
(let ((search-invisible t) start end)
(save-excursion
(when (re-search-backward "\\[\\[" nil :noerror)
(when (re-search-forward "\\[\\[\\(.*?\\)\\(\\]\\[.*?\\)*\\]\\]" nil :noerror)
(setq start (match-beginning 0))
(setq end (match-end 0))
(kill-new (match-string-no-properties 1)) ; Save the link to kill-ring
(replace-regexp "\\[\\[.*?\\(\\]\\[\\(.*?\\)\\)*\\]\\]" "\\2" nil start end)))))))
最快的方法可能是将光标放在链接之前,然后键入C-M-space
(mark-sexp
),这将标记整个链接。然后通过键入退格键(如果使用delete-selection-mode
)或将其删除C-w
。
有一种解决方案可以避免对正则表达式使用自定义解析,而直接使用内置org-element
API:
(defun org-link-delete-link ()
"Remove the link part of an org-mode link at point and keep
only the description"
(interactive)
(let ((elem (org-element-context)))
(if (eq (car elem) 'link)
(let* ((content-begin (org-element-property :contents-begin elem))
(content-end (org-element-property :contents-end elem))
(link-begin (org-element-property :begin elem))
(link-end (org-element-property :end elem)))
(if (and content-begin content-end)
(let ((content (buffer-substring-no-properties content-begin content-end)))
(delete-region link-begin link-end)
(insert content)))))))
[[LINK]]
格式组织链接。我了解match-beginning
并match-end
从你的答案。