Re: [PATCH v2] Show org file title in org-clock clocktable

2022-08-17 Thread Ihor Radchenko
Duy Nguyen  writes:

> Thank you once again for your comments Ihor. Please find attached a new
> patch with your comments addressed.

Thanks!

> +(let ((macros (org-macro--collect-macros)))
> +  (let ((title (assoc-default "title" macros)))

It is not a good idea to call internal function (with "--" in name) from
a different file. Such functions are a subject of change without notice.

`org-macro-templates' is always initialized when Org mode is active, and
you can rely on its value.

-- 
Ihor Radchenko,
Org mode contributor,
Learn more about Org mode at https://orgmode.org/.
Support Org development at https://liberapay.com/org-mode,
or support my work at https://liberapay.com/yantar92



[PATCH v2] Show org file title in org-clock clocktable

2022-08-15 Thread Duy Nguyen
>
> We usually use ~code~ for Elisp symbols and code. So, =t= -> ~t~ and
> ~File~ -> "File". See doc/Documentation_Standards.org


Please, escape #+BEGIN with comma: ,#+BEGIN.
> (this is done automatically if you use C-c ' to edit the src block)


By convention, we consider a document title to be concatenation of all
> the values of #+TITLE keyword inside buffer.


Thank you once again for your comments Ihor. Please find attached a new
patch with your comments addressed.

Duy
From 090811a1bce0d6c6f3469564c4e7cf89b07e8fb6 Mon Sep 17 00:00:00 2001
From: Duy Nguyen 
Date: Fri, 12 Aug 2022 18:40:10 +0200
Subject: [PATCH v2] lisp/org-clock.el: Show file title in org-clock clocktable

* lisp/org-clock.el (org-clocktable-defaults): Add default value for
new clock table option `:filetitle'.
(org-clock-get-file-title): Add new function to extract title of org file.
(org-clocktable-write-default): Print org file name in clock table if
`:filetitle' is set to `t'.

* doc/org-manual.org (The clock table): Include new `:filetitle'
option in manual for clock table.

* etc/ORG-NEWS (New =:filetitle= option for clock table): Include new
`:filetitle' option for clock table.

Allow user to show org file title instead of file name in the
clock table.  If the file does not have a title defined, the file name
will be shown in the clock table.

TINYCHANGE
---
 doc/org-manual.org |  4 
 etc/ORG-NEWS   | 13 +
 lisp/org-clock.el  | 17 -
 3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index 466718e6e..046da3790 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -6797,6 +6797,10 @@ using the =:formatter= parameter.
 
   Indent each headline field according to its level.
 
+- =:filetitle= ::
+
+  Show title in the file column if the file has a =#+title=.
+ 
 - =:hidefiles= ::
 
   Hide the file column when multiple files are used to produce the
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 00fe101dc..eefe4adc2 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -270,6 +270,19 @@ example,
 
 prints a sub-bibliography containing the book entries with =ai= among
 their keywords.
+*** New =:filetitle= option for clock table
+
+The =:filetitle= option for clock tables can be set to ~t~ to show org
+file title (set by =#+title:=) in the File column instead of the
+file name. For example:
+
+#+begin_src org
+,#+BEGIN: clocktable :scope agenda :maxlevel 2 :block thisweek :filetitle t
+#+end_src
+
+If a file does not have a title, the table will show the file name
+instead.
+
 ** New options
 *** A new custom setting =org-hide-drawer-startup= to control initial folding state of drawers
 
diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index 362abe358..f1d1f208b 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -324,6 +324,7 @@ string as argument."
:link nil
:narrow '40!
:indent t
+   :filetitle nil
:hidefiles nil
:formula nil
:timestamp nil
@@ -2469,6 +2470,17 @@ the currently selected interval size."
 	  (org-update-dblock)
 	  t)
 
+;;;###autoload
+(defun org-clock-get-file-title (file-name)
+  "Get the file title from FILE-NAME as a string. Returns short
+FILE-NAME if title is not found."
+  (with-current-buffer (find-file-noselect file-name)
+(let ((macros (org-macro--collect-macros)))
+  (let ((title (assoc-default "title" macros)))
+(if (null title)
+(file-name-nondirectory file-name)
+  title)
+
 ;;;###autoload
 (defun org-dblock-write:clocktable (params)
   "Write the standard clocktable."
@@ -2584,6 +2596,7 @@ from the dynamic block definition."
 	 (emph (plist-get params :emphasize))
 	 (compact? (plist-get params :compact))
 	 (narrow (or (plist-get params :narrow) (and compact? '40!)))
+	 (filetitle (plist-get params :filetitle))
 	 (level? (and (not compact?) (plist-get params :level)))
 	 (timestamp (plist-get params :timestamp))
 	 (tags (plist-get params :tags))
@@ -2723,7 +2736,9 @@ from the dynamic block definition."
 			 (if (eq formula '%) " %s |" "")
 			 "\n")
 
-		 (file-name-nondirectory file-name)
+ (if filetitle
+ (org-clock-get-file-title file-name)
+   (file-name-nondirectory file-name))
 		 (if level?"| " "") ;level column, maybe
 		 (if timestamp "| " "") ;timestamp column, maybe
 		 (if tags  "| " "") ;tags column, maybe
-- 
2.32.1 (Apple Git-133)