Browse Source

org-colview: Fix `org-columns-view' and `org-columns-edit-value'

* lisp/org-colview.el (org-columns-new): Change signature to allow both
  editing and column creation non-interactively.
(org-columns-edit-attributes): Use new signature.
* testing/lisp/test-org-colview.el (test-org-colview/columns-new):
  Update tests.
Nicolas Goaziou 3 years ago
parent
commit
a4ad618f62
2 changed files with 49 additions and 41 deletions
  1. 41 33
      lisp/org-colview.el
  2. 8 8
      testing/lisp/test-org-colview.el

+ 41 - 33
lisp/org-colview.el

@@ -811,38 +811,48 @@ 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 _)
-  "Insert a new column, to the left of the current column."
+(defun org-columns-new (&optional spec &rest attributes)
+  "Insert a new column, to the left of the current column.
+Interactively fill attributes for new column.  When column format
+specification SPEC is provided, edit it instead.
+
+When optional argument attributes can be a list of columns
+specifications attributes to create the new column
+non-interactively.  See `org-columns-compile-format' for
+details."
   (interactive)
-  (let* ((automatic (org-string-nw-p prop))
-	 (prop (or prop (completing-read
+  (let ((new (or attributes
+		 (let ((prop
+			(completing-read
 			 "Property: "
-			 (mapcar #'list (org-buffer-property-keys t nil t)))))
-	 (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 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
-	  (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))))
-	 (edit
-	  (and prop (assoc-string prop org-columns-current-fmt-compiled t))))
-    (if edit (setcdr edit (list title width operator nil))
-      (push (list prop title width operator nil)
-	    (nthcdr (current-column) org-columns-current-fmt-compiled)))
+			 (mapcar #'list (org-buffer-property-keys t nil t))
+			 nil nil (nth 0 spec))))
+		   (list prop
+			 (read-string (format "Column title [%s]: " prop)
+				      (nth 1 spec))
+			 ;; Use `read-string' instead of `read-number'
+			 ;; to allow empty width.
+			 (let ((w (read-string
+				   "Column width: "
+				   (and (nth 2 spec)
+					(number-to-string (nth 2 spec))))))
+			   (and (org-string-nw-p w) (string-to-number w)))
+			 (org-string-nw-p
+			  (completing-read
+			   "Summary: "
+			   (delete-dups
+			    (cons '("")	;Allow empty operator.
+				  (mapcar (lambda (x) (list (car x)))
+					  (append
+					   org-columns-summary-types
+					   org-columns-summary-types-default))))
+			   nil t (nth 3 spec)))
+			 (org-string-nw-p
+			  (read-string "Format: " (nth 4 spec))))))))
+    (if spec
+	(progn (setcar spec (car new))
+	       (setcdr spec (cdr new)))
+      (push new (nthcdr (current-column) org-columns-current-fmt-compiled)))
     (org-columns-store-format)
     (org-columns-redo)))
 
@@ -864,9 +874,7 @@ When COLUMNS-FMT-STRING is non-nil, use it as the column format."
 (defun org-columns-edit-attributes ()
   "Edit the attributes of the current column."
   (interactive)
-  (let* ((n (current-column))
-	 (info (nth n org-columns-current-fmt-compiled)))
-    (apply 'org-columns-new info)))
+  (org-columns-new (nth (current-column) org-columns-current-fmt-compiled)))
 
 (defun org-columns-widen (arg)
   "Make the column wider by ARG characters."

+ 8 - 8
testing/lisp/test-org-colview.el

@@ -630,7 +630,7 @@
    (equal '("FOO" "ITEM")
 	  (org-test-with-temp-text "* H"
 	    (let ((org-columns-default-format "%ITEM")) (org-columns))
-	    (org-columns-new "FOO")
+	    (org-columns-new nil "FOO" "FOO" nil nil nil)
 	    (list (get-char-property (point) 'org-columns-key)
 		  (get-char-property (1+ (point)) 'org-columns-key)))))
   (should
@@ -638,7 +638,7 @@
 	  (org-test-with-temp-text "* H"
 	    (let ((org-columns-default-format "%ITEM %BAR")) (org-columns))
 	    (forward-char)
-	    (org-columns-new "FOO")
+	    (org-columns-new nil "FOO" "FOO" nil nil nil)
 	    (list (get-char-property (1- (point)) 'org-columns-key)
 		  (get-char-property (point) 'org-columns-key)
 		  (get-char-property (1+ (point)) 'org-columns-key)))))
@@ -647,7 +647,7 @@
    (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")
+	    (org-columns-new nil "FOO" "FOO" nil nil nil)
 	    (goto-char (point-min))
 	    (buffer-substring-no-properties (point) (line-end-position)))))
   (should
@@ -655,7 +655,7 @@
 	  (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")
+	    (org-columns-new nil "FOO" "FOO" nil nil nil)
 	    (goto-char (point-min))
 	    (buffer-substring-no-properties (point) (line-end-position)))))
   ;; Mind case when updating #+COLUMNS.
@@ -664,7 +664,7 @@
 	  (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")
+	    (org-columns-new nil "Foo" "Foo" nil nil nil)
 	    (goto-char (point-min))
 	    (buffer-substring-no-properties (point) (line-end-position)))))
   (should
@@ -672,7 +672,7 @@
 	  (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")
+	    (org-columns-new nil "Foo" "Foo" nil nil nil)
 	    (goto-char (point-min))
 	    (buffer-substring-no-properties (point) (line-end-position)))))
   ;; Also update :COLUMNS: properties.
@@ -680,14 +680,14 @@
    (equal "%FOO %ITEM"
 	  (org-test-with-temp-text "* H\n:PROPERTIES:\n:COLUMNS: %ITEM\n:END:"
 	    (let ((org-columns-default-format "%ITEM")) (org-columns))
-	    (org-columns-new "FOO")
+	    (org-columns-new nil "FOO" "FOO" nil nil nil)
 	    (org-entry-get nil "COLUMNS"))))
   ;; If no keyword nor any property is available, insert one.
   (should
    (string-match-p (regexp-quote "#+COLUMNS: %FOO %ITEM")
 		   (org-test-with-temp-text "* H"
 		     (let ((org-columns-default-format "%ITEM")) (org-columns))
-		     (org-columns-new "FOO")
+		     (org-columns-new nil "FOO" "FOO" nil nil nil)
 		     (buffer-string)))))
 
 (ert-deftest test-org-colview/columns-update ()