Browse Source

org-table.el (org-table-copy-increment): Change increment behavior

* org-table.el (org-table-copy-increment): Use a number as a
way to force a fixed increment value.
(org-table-copy-down): Increment by the difference between the
current field and the field in the row above.  This is the
default behavior.  To go back to the previous behavior (to
always increment by 1 unit), set `org-table-copy-increment'
to 1.

Thanks to Michael Brand for suggesting this.
Bastien Guerry 11 years ago
parent
commit
7ac468ff55
1 changed files with 42 additions and 16 deletions
  1. 42 16
      lisp/org-table.el

+ 42 - 16
lisp/org-table.el

@@ -238,7 +238,12 @@ t       accept as input and present for editing"
 (defcustom org-table-copy-increment t
 (defcustom org-table-copy-increment t
   "Non-nil means increment when copying current field with \\[org-table-copy-down]."
   "Non-nil means increment when copying current field with \\[org-table-copy-down]."
   :group 'org-table-calculation
   :group 'org-table-calculation
-  :type 'boolean)
+  :version "24.5"
+  :package-version '(Org . "8.3")
+  :type '(choice
+	  (const :tag "Use the difference between the current and the above fields" t)
+	  (integer :tag "Use a number" 1)
+	  (const :tag "Don't increment the value when copying a field" t)))
 
 
 (defcustom org-calc-default-modes
 (defcustom org-calc-default-modes
   '(calc-internal-prec 12
   '(calc-internal-prec 12
@@ -1099,30 +1104,37 @@ Before doing so, re-align the table if necessary."
 
 
 ;;;###autoload
 ;;;###autoload
 (defun org-table-copy-down (n)
 (defun org-table-copy-down (n)
-  "Copy a field down in the current column.
-If the field at the cursor is empty, copy into it the content of
-the nearest non-empty field above.  With argument N, use the Nth
-non-empty field.  If the current field is not empty, it is copied
-down to the next row, and the cursor is moved with it.
-Therefore, repeating this command causes the column to be filled
-row-by-row.
+  "Copy the value of the current field one row below.
+
+If the field at the cursor is empty, copy the content of the
+nearest non-empty field above.  With argument N, use the Nth
+non-empty field.
+
+If the current field is not empty, it is copied down to the next
+row, and the cursor is moved with it.  Therefore, repeating this
+command causes the column to be filled row-by-row.
+
 If the variable `org-table-copy-increment' is non-nil and the
 If the variable `org-table-copy-increment' is non-nil and the
 field is an integer or a timestamp, it will be incremented while
 field is an integer or a timestamp, it will be incremented while
-copying.  In the case of a timestamp, increment by one day."
+copying.  By default, increment by the difference between the
+value in the current field and the one in the field above.  To
+increment using a fixed integer, set `org-table-copy-increment'
+to a number.  In the case of a timestamp, increment by days."
   (interactive "p")
   (interactive "p")
   (let* ((colpos (org-table-current-column))
   (let* ((colpos (org-table-current-column))
 	 (col (current-column))
 	 (col (current-column))
 	 (field (save-excursion (org-table-get-field)))
 	 (field (save-excursion (org-table-get-field)))
+	 (field-up (or (save-excursion
+			 (org-table-get (1- (org-table-current-line))
+					(org-table-current-column))) ""))
 	 (non-empty (string-match "[^ \t]" field))
 	 (non-empty (string-match "[^ \t]" field))
+	 (non-empty-up (string-match "[^ \t]" field-up))
 	 (beg (org-table-begin))
 	 (beg (org-table-begin))
 	 (orig-n n)
 	 (orig-n n)
-	 txt)
+	 txt txt-up inc)
     (org-table-check-inside-data-field)
     (org-table-check-inside-data-field)
     (if non-empty
     (if non-empty
-	(progn
-	  (setq txt (org-trim field))
-	  (org-table-next-row)
-	  (org-table-blank-field))
+	(setq txt (org-trim field))
       (save-excursion
       (save-excursion
 	(setq txt
 	(setq txt
 	      (catch 'exit
 	      (catch 'exit
@@ -1134,17 +1146,31 @@ copying.  In the case of a timestamp, increment by one day."
 			    "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
 			    "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
 			   (<= (setq n (1- n)) 0))
 			   (<= (setq n (1- n)) 0))
 		      (throw 'exit (match-string 1))))))))
 		      (throw 'exit (match-string 1))))))))
+    (if non-empty-up (setq txt-up (org-trim field-up)))
+    (when txt
+      (org-table-next-row)
+      (org-table-blank-field))
+    (setq inc (cond
+	       ((numberp org-table-copy-increment) org-table-copy-increment)
+	       (txt-up
+		(cond ((and (string-match org-ts-regexp3 txt-up)
+			    (string-match org-ts-regexp3 txt))
+		       (- (org-time-string-to-absolute txt)
+			  (org-time-string-to-absolute txt-up)))
+		      (t (- (string-to-number txt)
+			    (string-to-number txt-up)))))
+	       (t 1)))
     (if txt
     (if txt
 	(progn
 	(progn
 	  (if (and org-table-copy-increment
 	  (if (and org-table-copy-increment
 		   (not (equal orig-n 0))
 		   (not (equal orig-n 0))
 		   (string-match "^[0-9]+$" txt)
 		   (string-match "^[0-9]+$" txt)
 		   (< (string-to-number txt) 100000000))
 		   (< (string-to-number txt) 100000000))
-	      (setq txt (format "%d" (+ (string-to-number txt) 1))))
+	      (setq txt (format "%d" (+ (string-to-number txt) inc))))
 	  (insert txt)
 	  (insert txt)
 	  (org-move-to-column col)
 	  (org-move-to-column col)
 	  (if (and org-table-copy-increment (org-at-timestamp-p t))
 	  (if (and org-table-copy-increment (org-at-timestamp-p t))
-	      (org-timestamp-up-day)
+	      (org-timestamp-up-day inc)
 	    (org-table-maybe-recalculate-line))
 	    (org-table-maybe-recalculate-line))
 	  (org-table-align)
 	  (org-table-align)
 	  (org-move-to-column col))
 	  (org-move-to-column col))