From 2cc2d8f1f6265d12c92b734931f2e3e417276b05 Mon Sep 17 00:00:00 2001 From: Duy Nguyen Date: Fri, 12 Aug 2022 18:40:10 +0200 Subject: [PATCH] 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 | 16 +++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/doc/org-manual.org b/doc/org-manual.org index 2db45a84fb..40bfc7b7b8 100644 --- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -6800,6 +6800,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 94779e3064..d708e77f5b 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 362abe358b..89c5d9e0d7 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,16 @@ the currently selected interval size." (org-update-dblock) t))))) +(defun org-clock-get-file-title (file-name) + "Get the file title from FILE-NAME as a string. +Return short FILE-NAME if #+title keyword is not found." + (with-current-buffer (find-file-noselect file-name) + (org-macro-initialize-templates) + (let ((title (assoc-default "title" org-macro-templates))) + (if (null title) + (file-name-nondirectory file-name) + title)))) + ;;;###autoload (defun org-dblock-write:clocktable (params) "Write the standard clocktable." @@ -2584,6 +2595,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 +2735,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