emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Duy Nguyen <ddnguyen2101@gmail.com>
To: Emacs-orgmode@gnu.org
Subject: [Patch] Show org file title in org-clock clocktable
Date: Fri, 12 Aug 2022 23:54:40 +0200	[thread overview]
Message-ID: <CAM=3T04s=kH_9O2Tvssjz9wM1Lyc2F7xpVcnT10fuH1ESsWHLQ@mail.gmail.com> (raw)


[-- 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)


             reply	other threads:[~2022-08-12 21:56 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-12 21:54 Duy Nguyen [this message]
2022-08-13  8:00 ` [Patch] Show org file title in org-clock clocktable 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAM=3T04s=kH_9O2Tvssjz9wM1Lyc2F7xpVcnT10fuH1ESsWHLQ@mail.gmail.com' \
    --to=ddnguyen2101@gmail.com \
    --cc=Emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).