Function: org-id-update-id-locations

org-id-update-id-locations is an autoloaded, interactive and byte-compiled function defined in org-id.el.gz.

Signature

(org-id-update-id-locations &optional FILES SILENT)

Documentation

Scan relevant files for IDs.

Store the relation between files and corresponding IDs. This will scan all agenda files, all associated archives, all open Org files, and all files currently mentioned in org-id-locations. When FILES is given, scan also these files. If SILENT is non-nil, messages are suppressed.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-id.el.gz
;; Storing ID locations (files)

;;;###autoload
(defun org-id-update-id-locations (&optional files silent)
  "Scan relevant files for IDs.
Store the relation between files and corresponding IDs.
This will scan all agenda files, all associated archives, all open Org
files, and all files currently mentioned in `org-id-locations'.
When FILES is given, scan also these files.
If SILENT is non-nil, messages are suppressed."
  (interactive)
  (unless org-id-track-globally
    (error "Please turn on `org-id-track-globally' if you want to track IDs"))
  (let* ((files
          (delete-dups
           (mapcar #'file-truename
                   (cl-remove-if-not
		    ;; Default `org-id-extra-files' value contains
		    ;; `agenda-archives' symbol.
		    #'stringp
		    (append
		     ;; Agenda files and all associated archives.
		     (org-agenda-files t org-id-search-archives)
		     ;; Explicit extra files.
		     (if (symbolp org-id-extra-files)
			 (symbol-value org-id-extra-files)
		       org-id-extra-files)
		     ;; All files known to have IDs.
		     org-id-files
                     ;; All Org files open in Emacs.
                     (mapcar #'buffer-file-name (org-buffer-list 'files t))
		     ;; Additional files from function call.
		     files)))))
         (nfiles (length files))
         (id-regexp
	  (rx (seq bol (0+ (any "\t ")) ":ID:" (1+ " ") (not (any " ")))))
         (seen-ids (make-hash-table :test #'equal))
         (ndup 0)
         (i 0)
         (checksum
          (mapcar
           (lambda (f)
             (when (file-exists-p f)
               (list f (file-attribute-modification-time (file-attributes f)))))
           (sort (copy-sequence files) #'string<))))
    (unless (equal checksum org-id--locations-checksum) ; Files have changed since the last update.
      (setq org-id-locations nil)
      (with-temp-buffer
        (delay-mode-hooks
	  (org-mode)
	  (dolist (file files)
	    (when (file-exists-p file)
              (unless silent
                (cl-incf i)
                (message "Finding ID locations (%d/%d files): %s" i nfiles file))
	      (insert-file-contents file nil nil nil 'replace)
              (let ((ids nil)
                    node
		    (case-fold-search t))
                (org-with-point-at 1
		  (while (re-search-forward id-regexp nil t)
                    (setq node (org-element-at-point))
		    (when (org-element-type-p node 'node-property)
                      (push (org-element-property :value node) ids)))
		  (when ids
		    (push (cons (abbreviate-file-name file) ids)
			  org-id-locations)
		    (dolist (id ids)
                      (cond
                       ((not (gethash id seen-ids)) (puthash id t seen-ids))
                       (silent nil)
                       (t
                        (message "Duplicate ID %S" id)
                        (cl-incf ndup)))))))))))
      (setq org-id-files (mapcar #'car org-id-locations))
      (org-id-locations-save)
      ;; Now convert to a hash table.
      (setq org-id-locations (org-id-alist-to-hash org-id-locations))
      (setq org-id--locations-checksum checksum)
      (when (and (not silent) (> ndup 0))
        (warn "WARNING: %d duplicate IDs found, check *Messages* buffer" ndup))
      (message "%d files scanned, %d files contains IDs, and %d IDs found."
               nfiles (length org-id-files) (hash-table-count org-id-locations)))
    org-id-locations))