Browse Source

org-table: Allow multiple receiver locations for radio tables

* lisp/org-table.el (orgtbl-send-replace-tbl): Allow multiple receiver
  locations.
* testing/lisp/test-org-table.el (test-org-table/send-region): Add test.
Nicolas Goaziou 9 years ago
parent
commit
3781a3dbbd
3 changed files with 32 additions and 13 deletions
  1. 1 0
      etc/ORG-NEWS
  2. 16 12
      lisp/org-table.el
  3. 15 1
      testing/lisp/test-org-table.el

+ 1 - 0
etc/ORG-NEWS

@@ -380,6 +380,7 @@ plain list.
 *** ~fixltx2e~ is removed from ~org-latex-default-packages-alist~
 fixltx2e is obsolete, see LaTeX News 22.
 ** Miscellaneous
+*** Allow multiple receiver locations in radio tables
 *** Allow angular links within link descriptions
 It is now allowed to write, e.g.,
 ~[[http:orgmode.org][<file:unicorn.png>]]~ as an equivalent to

+ 16 - 12
lisp/org-table.el

@@ -4609,20 +4609,24 @@ a radio table."
 	  (beginning-of-line 0)))
       rtn)))
 
-(defun orgtbl-send-replace-tbl (name txt)
-  "Find and replace table NAME with TXT."
+(defun orgtbl-send-replace-tbl (name text)
+  "Find and replace table NAME with TEXT."
   (save-excursion
     (goto-char (point-min))
-    (unless (re-search-forward
-	     (concat "BEGIN +RECEIVE +ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
-      (user-error "Don't know where to insert translated table"))
-    (let ((beg (line-beginning-position 2)))
-      (unless (re-search-forward
-	       (concat "END +RECEIVE +ORGTBL +" name) nil t)
-	(user-error "Cannot find end of insertion region"))
-      (beginning-of-line)
-      (delete-region beg (point)))
-    (insert txt "\n")))
+    (let* ((location-flag nil)
+	   (name (regexp-quote name))
+	   (begin-re (format "BEGIN +RECEIVE +ORGTBL +%s\\([ \t]\\|$\\)" name))
+	   (end-re (format "END +RECEIVE +ORGTBL +%s\\([ \t]\\|$\\)" name)))
+      (while (re-search-forward begin-re nil t)
+	(unless location-flag (setq location-flag t))
+	(let ((beg (line-beginning-position 2)))
+	  (unless (re-search-forward end-re nil t)
+	    (user-error "Cannot find end of receiver location at %d" beg))
+	  (beginning-of-line)
+	  (delete-region beg (point))
+	  (insert text "\n")))
+      (unless location-flag
+	(user-error "No valid receiver location found in the buffer")))))
 
 ;;;###autoload
 (defun org-table-to-lisp (&optional txt)

+ 15 - 1
testing/lisp/test-org-table.el

@@ -1576,7 +1576,21 @@ See also `test-org-table/copy-field'."
 	    (buffer-substring-no-properties
 	     (search-forward "# BEGIN RECEIVE ORGTBL table\n")
 	     (progn (search-forward "# END RECEIVE ORGTBL table")
-		    (match-beginning 0)))))))
+		    (match-beginning 0))))))
+  ;; Allow multiple receiver locations.
+  (should
+   (org-test-with-temp-text "
+# BEGIN RECEIVE ORGTBL table
+# END RECEIVE ORGTBL table
+
+#+ORGTBL: SEND table orgtbl-to-orgtbl :hlines nil
+<point>| a |
+
+# BEGIN RECEIVE ORGTBL table
+# END RECEIVE ORGTBL table"
+     (orgtbl-send-table)
+     (goto-char (point-min))
+     (search-forward "| a |" nil t 3))))
 
 
 ;;; Sorting