Function: org-file-contents
org-file-contents is a byte-compiled function defined in org.el.gz.
Signature
(org-file-contents FILE &optional NOERROR NOCACHE)
Documentation
Return the contents of FILE, as a string.
FILE can be a file name or URL.
If FILE is a URL, download the contents. If the URL contents are
already cached in the org--file-cache hash table, the download step
is skipped.
If NOERROR is non-nil, ignore the error when unable to read the FILE from file or URL, and return nil.
If NOCACHE is non-nil, do a fresh fetch of FILE even if cached version is available. This option applies only if FILE is a URL.
Source Code
;; Defined in /usr/src/emacs/lisp/org/org.el.gz
(defun org-file-contents (file &optional noerror nocache)
"Return the contents of FILE, as a string.
FILE can be a file name or URL.
If FILE is a URL, download the contents. If the URL contents are
already cached in the `org--file-cache' hash table, the download step
is skipped.
If NOERROR is non-nil, ignore the error when unable to read the FILE
from file or URL, and return nil.
If NOCACHE is non-nil, do a fresh fetch of FILE even if cached version
is available. This option applies only if FILE is a URL."
(let* ((is-url (org-url-p file))
(is-remote (condition-case nil
(file-remote-p file)
;; In case of error, be safe.
;; See bug#68976.
(t t)))
(cache (and is-url
(not nocache)
(gethash file org--file-cache))))
(cond
(cache)
((or is-url is-remote)
(if (org--should-fetch-remote-resource-p file)
(condition-case error
(with-current-buffer (url-retrieve-synchronously file)
(goto-char (point-min))
;; Move point to after the url-retrieve header.
(search-forward "\n\n" nil :move)
;; Search for the success code only in the url-retrieve header.
(if (save-excursion
(re-search-backward "HTTP.*\\s-+200\\s-OK" nil :noerror))
;; Update the cache `org--file-cache' and return contents.
(puthash file
(buffer-substring-no-properties (point) (point-max))
org--file-cache)
(funcall (if noerror #'message #'user-error)
"Unable to fetch file from %S"
file)
nil))
(error (if noerror
(message "Org couldn't download \"%s\": %s %S" file (car error) (cdr error))
(signal (car error) (cdr error)))))
(funcall (if noerror #'message #'user-error)
"The remote resource %S is considered unsafe, and will not be downloaded."
file)))
(t
(with-temp-buffer
(condition-case nil
(progn
(insert-file-contents file)
(buffer-string))
(file-error
(funcall (if noerror #'message #'user-error)
"Unable to read file %S"
file)
nil)))))))