- (insert
- (concat
- (if helm-current-prefix-arg
- (helm :sources '((name . "Ref link types")
- (candidates . ("ref" "eqref" "pageref" "nameref"))
- (action . (lambda (x) x))))
- "ref")
- ":" label)))))))))
+
+ (cond
+ ;; no prefix or on a link
+ ((equal helm-current-prefix-arg nil)
+ (let* ((object (org-element-context))
+ (last-char (save-excursion
+ (goto-char (org-element-property :end object))
+ (backward-char)
+ (if (looking-at " ")
+ " "
+ ""))))
+ (if (-contains? '("ref" "eqref" "pageref" "nameref")
+ (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)
+ label
+ (org-element-property :raw-link object))
+ last-char))
+ ;; insert a new link
+ (insert
+ (concat
+ "ref:" label))
+ )))
+ ;; one prefix, alternate ref link
+ ((equal helm-current-prefix-arg '(4))
+ (insert
+ (concat
+ (helm :sources '((name . "Ref link types")
+ (candidates . ("ref" "eqref" "pageref" "nameref"))
+ (action . (lambda (x) x))))
+ ":" label)))
+ ))))))))