Odpowiedzi:
Następująca funkcja elisp pobierze łącze wokół bieżącego punktu rozpoznanego przez org-bracket-link-regexp, więc albo [[Link][Description]]albo [[Link]], i zastąpi go Descriptionw pierwszym przypadku lub Linkw drugim przypadku.
(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))))
Próbowałem dodać to do odpowiedzi od @Andrew, ale było za długo na komentarz ...
Naprawdę podobało mi się jego rozwiązanie, tyle że przesunęło kursor. (Technicznie wydaje mi się, że przesunęło to punkt. W każdym razie ...) Na szczęście łatwo było dodać, save-excursionaby tego uniknąć:
(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)))))
Wywołaj to polecenie, gdy punkt znajduje się w dowolnym miejscu po pierwszych [[nawiasach org-link (lub w dowolnym miejscu na / po hiperłączonym org-link).
Powiązanie org zostanie usunięty, jeśli jest z formatem [[LINK][DESCRIPTION]]lub [[LINK]]w org-modebuforze; inaczej nic się nie wydarzy.
Ze względów bezpieczeństwa odrzucone LINK z org-link jest zapisywane kill-ringw przypadku, gdy zajdzie potrzeba użycia tego linku w innym miejscu.
(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)))))))
Istnieje rozwiązanie, które pozwala uniknąć niestandardowego parsowania z wyrażeniami regularnymi i bezpośrednio korzysta z wbudowanego org-elementinterfejsu 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)))))))
Najszybszym może być umieszczenie kursora przed linkiem, a następnie wpisz C-M-space( mark-sexp), co spowoduje zaznaczenie całego linku. Następnie usuń go, wpisując backspace (jeśli używasz delete-selection-mode) lub C-w.
[[LINK]]również linki do formatu org. Dowiedziałem sięmatch-beginningimatch-endod twojej odpowiedzi.