make double prefix replace ref link
authorJohn Kitchin <jkitchin@andrew.cmu.edu>
Mon, 26 Jan 2015 20:49:49 +0000 (15:49 -0500)
committerJohn Kitchin <jkitchin@andrew.cmu.edu>
Mon, 26 Jan 2015 20:49:49 +0000 (15:49 -0500)
org-ref.org

index 7d66a79..22cf65e 100644 (file)
@@ -1145,7 +1145,8 @@ Another alternative ref insertion is to use helm.
 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
 (defun org-ref-helm-insert-ref-link ()
   "Helm menu to insert ref links to labels in the document.
-Use C-u to insert a different kind of ref link."
+Use C-u to insert a different kind of ref link.
+C-u C-u to replace the current ref with selection"
   (interactive)
   (let* ((labels (org-ref-get-labels))
         (contexts (mapcar 'org-ref-get-label-context labels))
@@ -1165,14 +1166,44 @@ Use C-u to insert a different kind of ref link."
                                                           ) "\n\n") label)))
                      (action . (lambda (label)
                                  (switch-to-buffer ,cb)
-                                 (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
+                                  ((equal helm-current-prefix-arg nil)
+                                    (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)))
+                                  ;; two prefixes, replace current label
+                                  ((equal helm-current-prefix-arg '(16))
+                                   ;; get link
+                                   (let* ((object (org-element-context))
+                                          (last-char (save-excursion
+                                                       (goto-char (org-element-property :end object))
+                                                       (backward-char)
+                                                       (if (looking-at " ")
+                                                           " "
+                                                         ""))))
+                                     (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))))))))))))
+
+
+
 #+END_SRC
 
 #+RESULTS:
@@ -3153,8 +3184,6 @@ C-u C-u will change the key at point to the selected keys.
             (-contains?
              org-ref-cite-types
              (org-element-property :type (org-element-context)))))
-      ;;(message-box "at end of a link")
-      ;; (goto-char (org-element-property :end object))
       (while (looking-back " ") (backward-char))
       (insert (concat "," (mapconcat 'identity keys ","))))