- (insert
- (concat
- "label:"
- (or label
- helm-pattern))))))))))
-#+END_SRC
-
-Now we create the completion function. This works from the org-machinery, e.g. if you type C-c C-l to insert a link, and use completion by pressing tab.
+ (let* ((object (org-element-context))
+ (last-char (save-excursion
+ (goto-char (org-element-property :end object))
+ (backward-char)
+ (if (looking-at " ")
+ " "
+ ""))))
+ (if (-contains? '("label")
+ (org-element-property :type object))
+ ;; we are on a link, so replace it.
+ (setf
+ (buffer-substring
+ (org-element-property :begin object)
+ (org-element-property :end object))
+ (concat
+ (replace-regexp-in-string
+ (org-element-property :path object)
+ helm-pattern
+ (org-element-property :raw-link object))
+ last-char))
+ ;; new link
+ (insert
+ (concat
+ "label:"
+ (or label
+ helm-pattern))))))))))))
+#+END_SRC
+
+Now we create a completion function. This works from the org-machinery, e.g. if you type C-c C-l to insert a link, and use completion by pressing tab.