From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: Re: Allow #+SETUPFILE to point to an URL for the org file Date: Thu, 25 May 2017 12:13:54 +0200 Message-ID: <87shjtb5wd.fsf@nicolasgoaziou.fr> References: <87h96eh4qb.fsf@nicolasgoaziou.fr> <871sxigkhk.fsf@nicolasgoaziou.fr> <87twaef3iq.fsf@nicolasgoaziou.fr> <874lybp5ua.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:50978) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dDpnO-000422-0n for emacs-orgmode@gnu.org; Thu, 25 May 2017 06:15:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dDpnJ-0002YT-S1 for emacs-orgmode@gnu.org; Thu, 25 May 2017 06:15:06 -0400 Received: from relay4-d.mail.gandi.net ([2001:4b98:c:538::196]:45805) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dDpnJ-0002Xp-L9 for emacs-orgmode@gnu.org; Thu, 25 May 2017 06:15:01 -0400 In-Reply-To: (Kaushal Modi's message of "Tue, 23 May 2017 19:07:16 +0000") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Kaushal Modi Cc: emacs-org list Hello, Kaushal Modi writes: > I have attached a patch following your recommendation. Please review > it. Thank you. Comments follow. >> `org--setupfile-clear-cache' or `org-setupfile--clear-cache' depending >> on the location of the function. > > This is an interactive, a user-facing function; do we want to add > double-dashes in that name? The function is now called org-file-clear-cache. Interactive functions do not have double-dashes in their names. However, I have concerns about this interactive status. Given than the function is not properly documented in the manual, there is little chance it will be actually used. And if it isn't, it could return surprising results. Another idea would be to replace NOCACHE with CLEAR-CACHE. When this is non-nil, the cache is reset at the beginning of the function. The point is to reset the cache the first time the function is called, but not on recursive calls, which ensures any file is retrieved only once. Of course the cache doesn't survive to multiple exports, but at least it is transparent to the user. Yet another idea is to add a time-to-live to cached values and remove them from cache past it. However, I prefer the idea above. > I tried (org-file-remote-p "http://foo.bar") but it returned nil. Looks > like both org-file-remote-p and file-remote-p will not work for URLs. OK. > setupfile is always a string so I used %s. If setupfile is not a string > (may be nil?), then the very first string-match-p will through an error. Is > there a specific reason for using %S? I did not use %S because I did not > want the double-quotes to be printed around the string in the echo > area. The specific reason is, as you noticed, %S wraps file name within double quotes. IMO, `...' would be for symbols or key bindings. > Correct. The attached patch has everything integrated in > org-file-contents. `prog1' is difficult to read when the first SEXP is large. Also, we should avoid splitting error messages and constructing them back, for hypothetical i18n considerations. I suggest the following re-factoring, where I limited the number of bindings and remove some trivial comments. (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. 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-file-url-p file)) (cache (and is-url (not nocache) (gethash file org--file-cache)))) (cond (cache) (is-url (with-current-buffer (url-retrieve-synchronously file) (goto-char (point-min)) (if (re-search-forward "HTTP.*\\s-+200\\s-OK" nil t) ;; URL retrieved correctly. Move point to after the ;; url-retrieve header, update the cache `org--file-cache' ;; and return contents. (progn (search-forward "\n\n" nil 'move) (puthash file (buffer-substring-no-properties (point) (point-max)) org--file-cache)) (funcall (if noerror #'message #'user-error) (format "Unable to fetch file from %S" file))))) (t (with-temp-buffer (condition-case err (progn (insert-file-contents file) (buffer-string)) (file-error (funcall (if noerror #'message #'user-error) (error-message-string err))))))))) I'm not sure about the return value when NOERROR is non-nil, but it may not matter, per the suggestion above. > Do we need to update the code using org-file-contents in these places: What do you meant by "update"? Regards, -- Nicolas Goaziou