ฉันจะลบไฮเปอร์ลิงก์ที่มีอยู่โดยไม่ต้องลุยและลบวงเล็บด้วยตนเองได้อย่างไร เมื่อฉันพยายามและเพียงแค่ลบการเชื่อมโยงที่มีอยู่ให้ฉันได้รับorg-insert-link
Lisp error: (error "Empty link")
ฉันต้องการลบลิงก์และเก็บรักษาข้อความ (เช่นคำอธิบาย)
ฉันจะลบไฮเปอร์ลิงก์ที่มีอยู่โดยไม่ต้องลุยและลบวงเล็บด้วยตนเองได้อย่างไร เมื่อฉันพยายามและเพียงแค่ลบการเชื่อมโยงที่มีอยู่ให้ฉันได้รับorg-insert-link
Lisp error: (error "Empty link")
ฉันต้องการลบลิงก์และเก็บรักษาข้อความ (เช่นคำอธิบาย)
คำตอบ:
ฟังก์ชั่น 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)))))
เรียกคำสั่งนี้เมื่อจุดอยู่ที่ใดก็ได้หลังจาก[[
วงเล็บแรกของ org-link (หรือที่ใดก็ได้บน / หลัง org-link ที่เชื่อมโยงหลายมิติ)
ลิงค์องค์กรจะถูกลบหากเป็นรูปแบบ[[LINK][DESCRIPTION]]
หรือ[[LINK]]
ในorg-mode
บัฟเฟอร์ ไม่มีอะไรจะเกิดขึ้น
เพื่อความปลอดภัยลิงค์ทิ้งจาก org-link จะถูกบันทึกไว้kill-ring
ในกรณีที่จำเป็นต้องใช้ลิงค์นั้นในที่อื่น
(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)))))))
มีวิธีแก้ปัญหาที่หลีกเลี่ยงการใช้การแยกวิเคราะห์แบบกำหนดเองด้วย regexes และใช้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)))))))
เร็วที่สุดอาจวางเคอร์เซอร์ไว้ข้างหน้าลิงก์จากนั้นพิมพ์C-M-space
( mark-sexp
) ซึ่งจะทำเครื่องหมายลิงก์ทั้งหมด แล้วลบได้โดยการพิมพ์ Backspace (ถ้าคุณใช้delete-selection-mode
) C-w
หรือ
มาโครที่รวดเร็วและสกปรกนี้น่าจะเป็นวิธีหนึ่งไม่ใช่วิธีที่ดีที่สุด:
(fset 'my/org-remove-link
[?\M-a delete delete ?\M-x ?z ?a ?p ?- ?t ?o ?- ?c ?h ?a ?r return ?\[ ?\C-e backspace backspace ?\C-x])
[[LINK]]
ลิงก์รูปแบบองค์กรด้วย ฉันเรียนรู้เกี่ยวกับmatch-beginning
และmatch-end
จากคำตอบของคุณ