ในโหมด org วิธีการลบลิงค์?


11

ฉันจะลบไฮเปอร์ลิงก์ที่มีอยู่โดยไม่ต้องลุยและลบวงเล็บด้วยตนเองได้อย่างไร เมื่อฉันพยายามและเพียงแค่ลบการเชื่อมโยงที่มีอยู่ให้ฉันได้รับorg-insert-linkLisp error: (error "Empty link")

ฉันต้องการลบลิงก์และเก็บรักษาข้อความ (เช่นคำอธิบาย)

คำตอบ:


7

ฟังก์ชั่น 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))))

นั่นเป็นทางออกที่กะทัดรัดมาก ๆ ! ฉันอัปเดตคำตอบเพื่อสนับสนุน[[LINK]]ลิงก์รูปแบบองค์กรด้วย ฉันเรียนรู้เกี่ยวกับmatch-beginningและmatch-endจากคำตอบของคุณ
Kaushal Modi

5

ฉันพยายามจะเพิ่มคำตอบของ @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)))))

4

เรียกคำสั่งนี้เมื่อจุดอยู่ที่ใดก็ได้หลังจาก[[วงเล็บแรกของ 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)))))))

2

มีวิธีแก้ปัญหาที่หลีกเลี่ยงการใช้การแยกวิเคราะห์แบบกำหนดเองด้วย regexes และใช้org-elementAPI ในตัวโดยตรง:

(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)))))))

คุณสามารถเพิ่มวิธีผูกคำสั่งของคุณกับคีย์เพื่อให้เพียงพอสำหรับผู้เริ่มต้นได้หรือไม่
DoMiNeLa10

ดูเหมือนว่าจะลบช่องว่างสีขาวหลังจากลิงค์ ความคิดวิธีการเก็บพวกเขา?
Timm

1

เร็วที่สุดอาจวางเคอร์เซอร์ไว้ข้างหน้าลิงก์จากนั้นพิมพ์C-M-space( mark-sexp) ซึ่งจะทำเครื่องหมายลิงก์ทั้งหมด แล้วลบได้โดยการพิมพ์ Backspace (ถ้าคุณใช้delete-selection-mode) C-wหรือ


1
ดูเหมือนว่าจะลบลิงก์ทั้งหมด แต่ฉันต้องการลบลิงก์และเก็บรักษาข้อความ (เช่นคำอธิบาย)
Incandescentman

1
ขออภัยฉันเดาว่าคุณเข้าใจผิดคำถามของคุณ
Harald Hanche-Olsen

1
สำหรับผู้ลงคะแนนเสียงที่นั่น: คำถามนั้นคลุมเครือเมื่อฉันตอบคำถาม มันได้รับการแก้ไขตั้งแต่นั้นมาลบความกำกวม ฉันจะทิ้งคำตอบไว้ แต่เนื่องจากมันยังคงเป็นวิธีที่ง่ายที่สุดในการลบลิงก์คำอธิบายและทั้งหมด
Harald Hanche-Olsen

0

มาโครที่รวดเร็วและสกปรกนี้น่าจะเป็นวิธีหนึ่งไม่ใช่วิธีที่ดีที่สุด:

(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])
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.