Prechádzať zdrojové kódy

org-table.el (org-TBLFM-begin): Add function

* org-table.el (org-TBLFM-begin): New defun.
(org-TBLFM-begin-regexp): New variable.

* testing/lisp/test-org-table.el: Add test.
Ippei FURUHASHI 12 rokov pred
rodič
commit
c8c17460f1
2 zmenil súbory, kde vykonal 137 pridanie a 0 odobranie
  1. 14 0
      lisp/org-table.el
  2. 123 0
      testing/lisp/test-org-table.el

+ 14 - 0
lisp/org-table.el

@@ -52,6 +52,8 @@ This can be used to add additional functionality after the table is sent
 to the receiver position, otherwise, if table is not sent, the functions
 are not run.")
 
+(defvar org-TBLFM-begin-regexp "|\n[ \t]*#\\+TBLFM: ")
+
 (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
   "Non-nil means use the optimized table editor version for `orgtbl-mode'.
 In the optimized version, the table editor takes over all simple keys that
@@ -3169,6 +3171,18 @@ with the prefix ARG."
 	      (setq checksum c1)))
 	  (user-error "No convergence after %d iterations" imax))))))
 
+(defun org-TBLFM-begin ()
+  "Find the beginning of the TBLFM lines and return its position.
+Return nil when the beginning of TBLFM line was not found."
+  (save-excursion
+    (when (progn (forward-line 1)
+	      (re-search-backward
+	       org-TBLFM-begin-regexp
+	       nil t))
+	  (point-at-bol 2))))
+
+
+
 (defun org-table-expand-lhs-ranges (equations)
   "Expand list of formulas.
 If some of the RHS in the formulas are ranges or a row reference, expand

+ 123 - 0
testing/lisp/test-org-table.el

@@ -769,6 +769,129 @@ reference (with row).  Format specifier N."
     (forward-line 4)
     (should (equal (org-at-TBLFM-p) nil))))
 
+(ert-deftest test-org-table/org-TBLFM-begin ()
+  (org-test-with-temp-text-in-file
+      "
+| 1 |
+| 2 |
+#+TBLFM: $2=$1*2
+
+"
+    (goto-char (point-min))
+    (should (equal (org-TBLFM-begin)
+		   nil))
+
+    (goto-char (point-min))
+    (forward-line 1)
+    (should (equal (org-TBLFM-begin)
+		   nil))
+
+    (goto-char (point-min))
+    (forward-line 3)
+    (should (= (org-TBLFM-begin)
+		   14))
+
+    (goto-char (point-min))
+    (forward-line 4)
+    (should (= (org-TBLFM-begin)
+		   14))
+
+    ))
+
+(ert-deftest test-org-table/org-TBLFM-begin-for-multiple-TBLFM-lines ()
+  "For multiple #+TBLFM lines."
+  (org-test-with-temp-text-in-file
+      "
+| 1 |
+| 2 |
+#+TBLFM: $2=$1*1
+#+TBLFM: $2=$1*2
+
+"
+    (goto-char (point-min))
+    (should (equal (org-TBLFM-begin)
+		   nil))
+
+    (goto-char (point-min))
+    (forward-line 1)
+    (should (equal (org-TBLFM-begin)
+		   nil))
+
+    (goto-char (point-min))
+    (forward-line 3)
+    (should (= (org-TBLFM-begin)
+		   14))
+
+    (goto-char (point-min))
+    (forward-line 4)
+    (should (= (org-TBLFM-begin)
+		   14))
+
+    (goto-char (point-min))
+    (forward-line 5)
+    (should (= (org-TBLFM-begin)
+		   14))
+
+    ))
+
+(ert-deftest test-org-table/org-TBLFM-begin-for-pultiple-TBLFM-lines-blocks ()
+  (org-test-with-temp-text-in-file
+      "
+| 1 |
+| 2 |
+#+TBLFM: $2=$1*1
+#+TBLFM: $2=$1*2
+
+| 6 |
+| 7 |
+#+TBLFM: $2=$1*1
+#+TBLFM: $2=$1*2
+
+"
+    (goto-char (point-min))
+    (should (equal (org-TBLFM-begin)
+		   nil))
+
+    (goto-char (point-min))
+    (forward-line 1)
+    (should (equal (org-TBLFM-begin)
+		   nil))
+
+    (goto-char (point-min))
+    (forward-line 3)
+    (should (= (org-TBLFM-begin)
+		   14))
+
+    (goto-char (point-min))
+    (forward-line 4)
+    (should (= (org-TBLFM-begin)
+		   14))
+
+    (goto-char (point-min))
+    (forward-line 5)
+    (should (= (org-TBLFM-begin)
+		   14))
+
+    (goto-char (point-min))
+    (forward-line 6)
+    (should (= (org-TBLFM-begin)
+		   14))
+
+    (goto-char (point-min))
+    (forward-line 8)
+    (should (= (org-TBLFM-begin)
+		   61))
+
+    (goto-char (point-min))
+    (forward-line 9)
+    (should (= (org-TBLFM-begin)
+		   61))
+
+    (goto-char (point-min))
+    (forward-line 10)
+    (should (= (org-TBLFM-begin)
+		   61))))
+
 (provide 'test-org-table)
 
 ;;; test-org-table.el ends here