all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [Patch] Show org file title in org-clock clocktable
@ 2022-08-12 21:54 Duy Nguyen
  2022-08-13  8:00 ` Ihor Radchenko
  0 siblings, 1 reply; 12+ messages in thread
From: Duy Nguyen @ 2022-08-12 21:54 UTC (permalink / raw)
  To: Emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 1489 bytes --]

Hello,

Please find attached a patch to allow users to show the org file titles
(i.e. the value of #+title) instead of the file name in the org-clock
clocktable.

I created this patch as I am using a combination of org-roam and org-agenda
to manage my tasks, where each project has its own org-roam file (and
therefore, a #+title). For my work I also need to generate weekly time
reports, with my todos spread over different org(-roam) files. I think the
clocktable looks nicer with the org file title than the name generated by
org-roam, which also has some other irrelevant information (for the
clocktable) in it such as date and time created. I believe this feature
could be useful for other users as well who have their tasks spread over
multiple org-roam files like me. The feature can easily be used by adding
":filetitle t" as an option in the clocktable.

Please note that the org-clock-get-file-prop function is heavily inspired
by vulpea-buffer-prop-get from the vulpea package - I just copied it over
and made some adjustments to make it work for the envisioned use case.

As I am relatively new to emacs / elisp and this is my first patch
submission ever, I am open to feedback on my patch or on how I did this
submission in general.

Special thanks to Ihor, who provided me with hints on how to solve this
issue and inspired me to submit a patch (original post
<https://www.reddit.com/r/orgmode/comments/wlz2yc/showing_org_document_title_in_orgclockreports/>
).

Thanks,

Duy

[-- Attachment #1.2: Type: text/html, Size: 1700 bytes --]

[-- Attachment #2: 0001-lisp-org-clock.el-Show-file-title-in-org-clock-clock.patch --]
[-- Type: application/octet-stream, Size: 2875 bytes --]

From ede10f5f6103cb5802a734df9525b7b47c26c8aa Mon Sep 17 00:00:00 2001
From: Duy Nguyen <duynguyen@Duys-MBP.home>
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 clocktable option `:filetitle'.
(org-clock-get-file-prop): Add new function to extract title of org file.
(org-clocktable-write-default): Print org file name in clocktable if
`:filetitle' is set to `t'.

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

TINYCHANGE
---
 lisp/org-clock.el | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index 362abe358..7424e3a83 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,23 @@ the currently selected interval size."
 	  (org-update-dblock)
 	  t)))))
 
+;;;###autoload
+(defun org-clock-get-file-prop (file-name name)
+  "Get a property from FILE-NAME called NAME as a string. Returns
+short FILE-NAME if property is not found."
+  (with-current-buffer (find-file-noselect file-name)
+    (org-with-point-at 1
+      (if (re-search-forward (concat "^#\\+" name ": \\(.*\\)")
+                             (point-max) t)
+          (let ((value (string-trim
+                        (buffer-substring-no-properties
+                         (match-beginning 1)
+                         (match-end 1)))))
+            (unless (string-empty-p value)
+              value))
+        (let ((value (file-name-nondirectory file-name)))
+          value)))))
+
 ;;;###autoload
 (defun org-dblock-write:clocktable (params)
   "Write the standard clocktable."
@@ -2584,6 +2602,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 +2742,9 @@ from the dynamic block definition."
 			     (if (eq formula '%) " %s |" "")
 			     "\n")
 
-		     (file-name-nondirectory file-name)
+                     (if filetitle
+                         (org-clock-get-file-prop file-name "title")
+                       (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)


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

end of thread, other threads:[~2022-08-28 11:32 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-08-12 21:54 [Patch] Show org file title in org-clock clocktable Duy Nguyen
2022-08-13  8:00 ` Ihor Radchenko
2022-08-13 10:55   ` Duy Nguyen
2022-08-13 11:35     ` Ihor Radchenko
2022-08-15  6:40       ` [PATCH v2] " Duy Nguyen
2022-08-17  9:34         ` Ihor Radchenko
2022-08-17 18:53           ` [PATCH v3] " Duy Nguyen
2022-08-20  6:14             ` Ihor Radchenko
2022-08-21 12:06               ` Duy Nguyen
2022-08-28  8:11                 ` Duy Nguyen
2022-08-28  8:33                   ` Duy Nguyen
2022-08-28 11:30                     ` Duy Nguyen

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.