Browse Source

ob-core: Fix hash with time stamp

* lisp/ob-core.el (org-babel-result-regexp): Fix regexp.
(org-babel--insert-results-keyword): Simplify format-string.
(org-babel--clear-results-maybe): Fix return value.

* testing/lisp/test-ob.el (test-ob/where-is-src-block-result): Add
  tests.

Reported-by: Thomas Alexander Gerds <tag@biostat.ku.dk>
<http://permalink.gmane.org/gmane.emacs.orgmode/109418>
Nicolas Goaziou 3 years ago
parent
commit
9d7d650cd7
2 changed files with 40 additions and 5 deletions
  1. 7 5
      lisp/ob-core.el
  2. 33 0
      testing/lisp/test-ob.el

+ 7 - 5
lisp/ob-core.el

@@ -481,9 +481,11 @@ For the format of SAFE-LIST, see `org-babel-safe-header-args'."
   "Regexp matching a NAME keyword.")
 
 (defconst org-babel-result-regexp
-  (format "^[ \t]*#\\+%s\\(?:\\[\\(?:%S\\)?\\([[:alnum:]]+\\)\\]\\)?:[ \t]*"
+  (format "^[ \t]*#\\+%s\\(?:\\[\\(?:%s \\)?\\([[:alnum:]]+\\)\\]\\)?:[ \t]*"
 	  org-babel-results-keyword
-	  "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>")
+	  ;; <%Y-%m-%d %H:%M:%S>
+	  "<\\(?:[0-9]\\{4\\}-[0-1][0-9]-[0-3][0-9] \
+[0-2][0-9]\\(?::[0-5][0-9]\\)\\{2\\}\\)>")
   "Regular expression used to match result lines.
 If the results are associated with a hash key then the hash will
 be saved in match group 1.")
@@ -1860,7 +1862,7 @@ the results hash, or nil.  Leave point before the keyword."
 		  (cond ((not hash) nil)
 			(org-babel-hash-show-time
 			 (format "[%s %s]"
-				 (format-time-string "<%Y-%m-%d %H:%M:%S>")
+				 (format-time-string "<%F %T>")
 				 hash))
 			(t (format "[%s]" hash)))
 		  ":"
@@ -1895,8 +1897,8 @@ leave point where new results should be inserted."
 	  (delete-region (line-beginning-position)
 			 (line-beginning-position 2)))
 	(goto-char post)
-	(set-marker post nil)))
-    t))
+	(set-marker post nil)
+	t))))
 
 (defun org-babel-where-is-src-block-result (&optional insert _info hash)
   "Find where the current source block results begin.

+ 33 - 0
testing/lisp/test-ob.el

@@ -1659,6 +1659,39 @@ echo \"$data\"
       (let ((org-babel-results-keyword "RESULTS"))
 	(goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
       (org-trim (buffer-substring-no-properties (point) (point-max))))))
+  ;; Handle hashes with times.
+  (should
+   (equal
+    "#+RESULTS[<2014-03-04 00:41:10> bbbb]:"
+    (org-test-with-temp-text
+	"
+<point>#+BEGIN_SRC emacs-lisp
+\(+ 1 1)
+#+END_SRC
+
+#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
+      (let ((org-babel-results-keyword "RESULTS")
+	    (org-babel-hash-show-time t))
+	(cl-letf (((symbol-function 'format-time-string)
+		   (lambda (&rest _) "<2014-03-04 00:41:10>")))
+	  (goto-char (org-babel-where-is-src-block-result nil nil "bbbb"))
+	  (org-trim (buffer-substring-no-properties (point) (point-max))))))))
+  (should
+   (equal
+    "#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
+    (org-test-with-temp-text
+	"
+<point>#+BEGIN_SRC emacs-lisp
+\(+ 1 1)
+#+END_SRC
+
+#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
+      (let ((org-babel-results-keyword "RESULTS")
+	    (org-babel-hash-show-time t))
+	(cl-letf (((symbol-function 'format-time-string)
+		   (lambda (&rest _) "<2014-03-04 00:41:10>")))
+	  (goto-char (org-babel-where-is-src-block-result nil nil "aaaa"))
+	  (org-trim (buffer-substring-no-properties (point) (point-max))))))))
   ;; RESULTS keyword may not be the last affiliated keyword.
   (should
    (equal