Browse Source

org-export: Allow to retrieve all previous/next exportable objects

* contrib/lisp/org-export.el (org-export-get-previous-element,
  org-export-get-next-element): Allow to retrieve all previous/next
  exportable objects by specifying a non-nil, non positive integer
  argument.
* testing/lisp/test-org-export.el: Add tests.
Nicolas Goaziou 13 years ago
parent
commit
b1f36921d7
2 changed files with 26 additions and 7 deletions
  1. 7 5
      contrib/lisp/org-export.el
  2. 19 2
      testing/lisp/test-org-export.el

+ 7 - 5
contrib/lisp/org-export.el

@@ -4636,8 +4636,8 @@ object, a string, or nil.
 
 
 When optional argument N is a positive integer, return a list
 When optional argument N is a positive integer, return a list
 containing up to N siblings before BLOB, from closest to
 containing up to N siblings before BLOB, from closest to
-farthest."
-  (when (and n (not (wholenump n))) (setq n nil))
+farthest.  With any other non-nil value, return a list containing
+all of them."
   (let ((siblings
   (let ((siblings
 	 ;; An object can belong to the contents of its parent or
 	 ;; An object can belong to the contents of its parent or
 	 ;; to a secondary string.  We check the latter option
 	 ;; to a secondary string.  We check the latter option
@@ -4657,6 +4657,7 @@ farthest."
       (mapc (lambda (obj)
       (mapc (lambda (obj)
 	      (cond ((memq obj (plist-get info :ignore-list)))
 	      (cond ((memq obj (plist-get info :ignore-list)))
 		    ((null n) (throw 'exit obj))
 		    ((null n) (throw 'exit obj))
+		    ((not (wholenump n)) (push obj prev))
 		    ((zerop n) (throw 'exit (nreverse prev)))
 		    ((zerop n) (throw 'exit (nreverse prev)))
 		    (t (decf n) (push obj prev))))
 		    (t (decf n) (push obj prev))))
 	    (cdr (memq blob (reverse siblings))))
 	    (cdr (memq blob (reverse siblings))))
@@ -4670,9 +4671,9 @@ a communication channel.  Return next exportable element or
 object, a string, or nil.
 object, a string, or nil.
 
 
 When optional argument N is a positive integer, return a list
 When optional argument N is a positive integer, return a list
-containing up to N siblings after BLOB, from closest to
-farthest."
-  (when (and n (not (wholenump n))) (setq n nil))
+containing up to N siblings after BLOB, from closest to farthest.
+With any other non-nil value, return a list containing all of
+them."
   (let ((siblings
   (let ((siblings
 	 ;; An object can belong to the contents of its parent or to
 	 ;; An object can belong to the contents of its parent or to
 	 ;; a secondary string.  We check the latter option first.
 	 ;; a secondary string.  We check the latter option first.
@@ -4691,6 +4692,7 @@ farthest."
       (mapc (lambda (obj)
       (mapc (lambda (obj)
 	      (cond ((memq obj (plist-get info :ignore-list)))
 	      (cond ((memq obj (plist-get info :ignore-list)))
 		    ((null n) (throw 'exit obj))
 		    ((null n) (throw 'exit obj))
+		    ((not (wholenump n)) (push obj next))
 		    ((zerop n) (throw 'exit (nreverse next)))
 		    ((zerop n) (throw 'exit (nreverse next)))
 		    (t (decf n) (push obj next))))
 		    (t (decf n) (push obj next))))
 	    siblings)
 	    siblings)

+ 19 - 2
testing/lisp/test-org-export.el

@@ -2212,12 +2212,21 @@ Another text. (ref:text)
 	 (org-element-type
 	 (org-element-type
 	  (org-export-get-next-element
 	  (org-export-get-next-element
 	   (org-element-map tree 'plain-text 'identity info t nil t) info)))))
 	   (org-element-map tree 'plain-text 'identity info t nil t) info)))))
-  ;; With optional argument N, return a list containing up to
+  ;; With optional argument N, return a list containing all the
+  ;; following elements.
+  (should
+   (equal
+    '(bold code underline)
+    (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
+      (mapcar 'car
+	      (org-export-get-next-element
+	       (org-element-map tree 'italic 'identity info t) info t)))))
+  ;; When N is a positive integer, return a list containing up to
   ;; N following elements.
   ;; N following elements.
   (should
   (should
    (equal
    (equal
     '(bold code)
     '(bold code)
-    (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
+    (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
       (mapcar 'car
       (mapcar 'car
 	      (org-export-get-next-element
 	      (org-export-get-next-element
 	       (org-element-map tree 'italic 'identity info t) info 2))))))
 	       (org-element-map tree 'italic 'identity info t) info 2))))))
@@ -2265,6 +2274,14 @@ Another text. (ref:text)
 	   (org-element-map tree 'plain-text 'identity info t nil t) info)))))
 	   (org-element-map tree 'plain-text 'identity info t nil t) info)))))
   ;; With optional argument N, return a list containing up to
   ;; With optional argument N, return a list containing up to
   ;; N previous elements.
   ;; N previous elements.
+  (should
+   (equal '(bold italic underline)
+	  (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
+	    (mapcar 'car
+		    (org-export-get-previous-element
+		     (org-element-map tree 'code 'identity info t) info t)))))
+  ;; When N is a positive integer, return a list containing up to
+  ;; N previous elements.
   (should
   (should
    (equal '(bold italic)
    (equal '(bold italic)
 	  (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
 	  (org-test-with-parsed-data "_a_ /b/ *c* ~d~"