Browse Source

org-colview: Fix `org-columns-new'

* lisp/org-colview.el (org-columns-new): Fix location of inserted
  column.  Properly handle non-interactive cases.
* testing/lisp/test-org-colview.el (test-org-colview/columns-new): New
  test.
Nicolas Goaziou 5 years ago
parent
commit
b386089ee6
2 changed files with 58 additions and 22 deletions
  1. 22 22
      lisp/org-colview.el
  2. 36 0
      testing/lisp/test-org-colview.el

+ 22 - 22
lisp/org-colview.el

@@ -807,42 +807,42 @@ When COLUMNS-FMT-STRING is non-nil, use it as the column format."
 	      (goto-char (car entry))
 	      (org-columns--display-here (cdr entry)))))))))
 
-(defun org-columns-new (&optional prop title width operator _f _p summarize)
+(defun org-columns-new (&optional prop title width operator &rest _)
   "Insert a new column, to the left of the current column."
   (interactive)
-  (let* ((prop (or prop (completing-read
+  (let* ((automatic (org-string-nw-p prop))
+	 (prop (or prop (completing-read
 			 "Property: "
 			 (mapcar #'list (org-buffer-property-keys t nil t)))))
-	 (title (or title
-		    (read-string (format "Column title [%s]: " prop) prop)))
+	 (title (if automatic title
+		  (read-string (format "Column title [%s]: " prop) prop)))
 	 (width
 	  ;; WIDTH may be nil, but if PROP is provided, assume this is
 	  ;; the expected width.
-	  (if prop width
+	  (if automatic width
 	    ;; Use `read-string' instead of `read-number' to allow
 	    ;; empty width.
 	    (let ((w (read-string "Column width: ")))
 	      (and (org-string-nw-p w) (string-to-number w)))))
 	 (operator
-	  (or operator
-	      (org-string-nw-p
-	       (completing-read
-		"Summary: "
-		(delete-dups
-		 (mapcar (lambda (x) (list (car x)))
-			 (append org-columns-summary-types
-				 org-columns-summary-types-default)))
-		nil t))))
-	 (summarize (or summarize (org-columns--summarize operator)))
+	  (if automatic operator
+	    (org-string-nw-p
+	     (completing-read
+	      "Summary: "
+	      (delete-dups
+	       (mapcar (lambda (x) (list (car x)))
+		       (append org-columns-summary-types
+			       org-columns-summary-types-default)))
+	      nil t))))
+	 (summarize (and prop operator (org-columns--summarize operator)))
 	 (edit
 	  (and prop (assoc-string prop org-columns-current-fmt-compiled t))))
-    (if edit
-	(progn
-	  (setcar edit prop)
-	  (setcdr edit (list title width nil operator nil summarize)))
-      (let ((cell (nthcdr (1- (current-column))
-			  org-columns-current-fmt-compiled)))
-	(push (list prop title width nil operator nil summarize) (cdr cell))))
+    (cond (edit (setcdr edit (list title width operator nil summarize)))
+	  ((= (current-column) 0)
+	   (push (list prop title width operator nil summarize)
+		 org-columns-current-fmt-compiled))
+	  (t (push (list prop title width operator nil summarize)
+		   (nthcdr (current-column) org-columns-current-fmt-compiled))))
     (org-columns-store-format)
     (org-columns-redo)))
 

+ 36 - 0
testing/lisp/test-org-colview.el

@@ -523,6 +523,42 @@
 	    (org-columns-default-format "%A{custom}")) (org-columns))
       (get-char-property (point) 'org-columns-value-modified)))))
 
+(ert-deftest test-org-colview/columns-new ()
+  "Test `org-columns-new' specifications."
+  ;; Insert new column at the left of the current one.
+  (should
+   (equal '("FOO" "ITEM")
+	  (org-test-with-temp-text "* H"
+	    (let ((org-columns-default-format "%ITEM")) (org-columns))
+	    (org-columns-new "FOO")
+	    (list (get-char-property (point) 'org-columns-key)
+		  (get-char-property (1+ (point)) 'org-columns-key)))))
+  (should
+   (equal '("ITEM" "FOO" "ITEM")
+	  (org-test-with-temp-text "* H"
+	    (let ((org-columns-default-format "%ITEM %BAR")) (org-columns))
+	    (forward-char)
+	    (org-columns-new "FOO")
+	    (list (get-char-property (1- (point)) 'org-columns-key)
+		  (get-char-property (point) 'org-columns-key)
+		  (get-char-property (1+ (point)) 'org-columns-key)))))
+  ;; Update #+COLUMNS: keyword if needed.
+  (should
+   (equal "#+COLUMNS: %FOO %ITEM"
+	  (org-test-with-temp-text "#+COLUMNS: %ITEM\n<point>* H"
+	    (let ((org-columns-default-format "%ITEM")) (org-columns))
+	    (org-columns-new "FOO")
+	    (goto-char (point-min))
+	    (buffer-substring-no-properties (point) (line-end-position)))))
+  (should
+   (equal "#+COLUMNS: %ITEM %FOO %BAR"
+	  (org-test-with-temp-text "#+COLUMNS: %ITEM %BAR\n<point>* H"
+	    (let ((org-columns-default-format "%ITEM %BAR")) (org-columns))
+	    (forward-char)
+	    (org-columns-new "FOO")
+	    (goto-char (point-min))
+	    (buffer-substring-no-properties (point) (line-end-position))))))
+
 (ert-deftest test-org-colview/columns-update ()
   "Test `org-columns-update' specifications."
   ;; Update display.