emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [FR] org-colview.el, add annotation for functions summary-types
@ 2024-08-08 17:53 Sławomir Grochowski
  2024-08-10 13:03 ` Ihor Radchenko
  0 siblings, 1 reply; 8+ messages in thread
From: Sławomir Grochowski @ 2024-08-08 17:53 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 860 bytes --]

Dear all,

When the user wants to add a new column (or edit an existing one), he
executes the command `org-columns-new'. Then he can enter or select a few
'column attributes' (see in manual https://orgmode.org/org.html#Column-attributes-1).
One of the attributes is "SUMMARY-TYPE". This attribute can be selected
from the list of very enigmatic symbols such as "+", "$", "X"
which do not convey any information to the user. So, I have prepared a
solution that will address this issue.

For each symbol, the 'docstring' of the function assigned to that symbol
will be displayed. For example: 
"+ -- Compute the sum of VALUES."  
"$ -- Compute the sum of VALUES, with two decimals."

What do you think about this solution? 
I also included a question in the commit message regarding the function
`org-columns--first-line-docstring' that I created.

Patch below:

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch --]
[-- Type: text/x-diff, Size: 3504 bytes --]

From 1725dddfc6e574737f1b79f2ba93d5fa6b09cffb Mon Sep 17 00:00:00 2001
From: Slawomir Grochowski <slawomir.grochowski@gmail.com>
Date: Sat, 3 Aug 2024 19:53:25 +0200
Subject: [PATCH] lisp/org-colview.el: add annotation for summary-types

* org-colview.el (org-columns--first-line-docstring): add function that
retrieves the first line of function's docstring.
I have not found a function that would do such a simple thing, it seems
to me that such functionality is often used but there is no special
function for it? I couldn't find one, so I wrote my own. But it is a
general-purpose function and should not be located in org-colview.

(org-columns--summary-types-annotate): add function that return
annotation for one of the summary-type function in
`org-columns-summary-types-all'.
(org-columns-new): refactor: extract variable (append
org-columns-summary-types org-columns-summary-types-default) to
`org-columns-summary-types-all' because it is used in two places.
---
 lisp/org-colview.el | 39 ++++++++++++++++++++++++++++++---------
 1 file changed, 30 insertions(+), 9 deletions(-)

diff --git a/lisp/org-colview.el b/lisp/org-colview.el
index ed4d1ee16..50fb72121 100644
--- a/lisp/org-colview.el
+++ b/lisp/org-colview.el
@@ -953,6 +953,24 @@ 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--first-line-docstring (fun)
+  "Return the first line of the documentation string of FUN."
+  (let* ((docstring (documentation fun))
+         (first-line (car (split-string docstring "\n"))))
+    first-line))
+
+(defun org-columns--summary-types-annotate (fun)
+  "Return annotation for one of the FUN in `org-columns-summary-types-all'.
+
+If FUN is not empty, retrieves the first line of the docstring for FUN
+from the `org-columns-summary-types-all', formats it, and decorates it
+with the `completions-annotations` face."
+  (when (not (string-empty-p fun))
+    (format " -- %s"
+            (propertize (org-columns--first-line-docstring
+                         (cdr (assoc fun org-columns-summary-types-all)))
+                        'face 'completions-annotations))))
+
 (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
@@ -980,15 +998,18 @@ details."
 					(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)))
+                          (let* ((completion-extra-properties
+                                 '(:annotation-function org-columns--summary-types-annotate))
+                                (org-columns-summary-types-all (append
+					                        org-columns-summary-types
+					                        org-columns-summary-types-default)))
+			    (completing-read
+			     "Summary: "
+			     (delete-dups
+			      (cons '("") ;Allow empty operator.
+				    (mapcar (lambda (x) (list (car x)))
+					    org-columns-summary-types-all)))
+			     nil t (nth 3 spec))))
 			 (org-string-nw-p
 			  (read-string "Format: " (nth 4 spec))))))))
     (if spec
-- 
2.30.2


[-- Attachment #3: Type: text/plain, Size: 35 bytes --]



Regards,
-- 
Slawomir Grochowski

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-08-18  9:56 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-08 17:53 [FR] org-colview.el, add annotation for functions summary-types Sławomir Grochowski
2024-08-10 13:03 ` Ihor Radchenko
2024-08-15 14:35   ` Sławomir Grochowski
2024-08-15 16:48     ` Ihor Radchenko
2024-08-15 17:29       ` Sławomir Grochowski
2024-08-15 17:41         ` Ihor Radchenko
2024-08-16 11:13           ` Sławomir Grochowski
2024-08-18  9:56             ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).