]> git.donarmstrong.com Git - org-ref.git/blobdiff - org-ref.el
add new function to find bad file links, bad \attachfile{} and attachfile links
[org-ref.git] / org-ref.el
index 240adda8562d2f43b4a055d4a11727d33e289c3b..4509d2d9b263de5c817af0d1e670659c504946ff 100644 (file)
@@ -504,7 +504,8 @@ Format according to the type in `org-ref-bibliography-entry-format'."
                 (loop for file in org-ref-bibliography-files do
                       (if (org-ref-key-in-file-p key (file-truename file))
                           (throw 'result file)
-                        (message "%s not found in %s" key (file-truename file))))))
+                        (message "%s not found in %s"
+                                 key (file-truename file))))))
 
     (with-temp-buffer
       (insert-file-contents file)
@@ -992,10 +993,8 @@ ARG does nothing."
    "on clicking goto the label. Navigate back with C-c &"
    (org-mark-ring-push)
    ;; next search from beginning of the buffer
-
    ;; it is possible you would not find the label if narrowing is in effect
    (widen)
-
    (unless
        (or
        ;; our label links
@@ -1011,12 +1010,14 @@ ARG does nothing."
        ;; #+label: name  org-definition
        (progn
          (goto-char (point-min))
-         (re-search-forward (format "^#\\+label:\\s-*\\(%s\\)\\b" label) nil t))
+         (re-search-forward
+          (format "^#\\+label:\\s-*\\(%s\\)\\b" label) nil t))
 
        ;; org tblname
        (progn
          (goto-char (point-min))
-         (re-search-forward (format "^#\\+tblname:\\s-*\\(%s\\)\\b" label) nil t))
+         (re-search-forward
+          (format "^#\\+tblname:\\s-*\\(%s\\)\\b" label) nil t)))
 
      ;; we did not find anything, so go back to where we came
      (org-mark-ring-goto)
@@ -2394,7 +2395,7 @@ Makes a new buffer with clickable links."
           (kill-buffer "*Missing citations*"))
       (message "No bad cite links found"))))
 
-;; ** helm interface to bad citations, labels, refs in orgfile
+;; ** helm interface to bad citations, labels, refs and files in orgfile
 (defun org-ref-bad-cite-candidates ()
   "Return a list of conses (key . marker) where key does not exist in the known bibliography files, and marker points to the key."
   (let* ((cp (point))                  ; save to return to later
@@ -2481,6 +2482,29 @@ Makes a new buffer with clickable links."
            (goto-char cp)))))
       multiple-labels))
 
+(defun org-ref-bad-file-link-candidates ()
+  "Return list of conses (link . marker) wehre the file in the link does not exist."
+  (let* ((bad-files '()))
+    (org-element-map (org-element-parse-buffer) 'link
+      (lambda (link)
+       (let ((type (org-element-property :type link)))
+         (when (or  (string= "file" type)
+                    (string= "attachfile" type))
+           (unless (file-exists-p (org-element-property :path link))
+             (add-to-list 'bad-files
+                          (cons (org-element-property :path link)
+                                (save-excursion
+                                  (goto-char
+                                   (org-element-property :begin link))
+                                  (point-marker)))))))))
+    ;; Let us also check \attachfile{fname}
+    (save-excursion
+      (goto-char (point-min))
+      (while (re-search-forward "\\attachfile{\\(.*\\)}" nil t)
+       (unless (file-exists-p (match-string 1))
+         (add-to-list 'bad-files (cons (match-string 1) (point-marker))))))
+    bad-files))
+
 ;;;###autoload
 (defun org-ref ()
   "Opens a helm interface to actions for org-ref.
@@ -2489,7 +2513,8 @@ Shows bad citations, ref links and labels"
   (let ((cb (current-buffer))
        (bad-citations (org-ref-bad-cite-candidates))
        (bad-refs (org-ref-bad-ref-candidates))
-       (bad-labels (org-ref-bad-label-candidates)))
+       (bad-labels (org-ref-bad-label-candidates))
+       (bad-files (org-ref-bad-file-link-candidates)))
 
     (helm :sources `(((name . "Bad citations")
                       (candidates . ,bad-citations)
@@ -2509,6 +2534,12 @@ Shows bad citations, ref links and labels"
                                          (switch-to-buffer (marker-buffer marker))
                                          (goto-char marker))))
                     ;;
+                    ((name . "Bad file links")
+                     (candidates . ,bad-files)
+                     (lambda (marker)
+                                  (switch-to-buffer (marker-buffer marker))
+                                  (goto-char marker)))
+                    ;;
                     ((name . "Utilities")
                      (candidates . (("Check buffer again" . org-ref)
                                     ("Insert citation" . helm-bibtex)
@@ -2533,8 +2564,7 @@ Shows bad citations, ref links and labels"
                                     ))
                      (action . (lambda (x)
                                  (switch-to-buffer ,cb)
-                                 (funcall x))))
-                     ))))
+                                 (funcall x))))))))
 
 ;; ** Find non-ascii charaters
 (defun org-ref-find-non-ascii-characters ()