From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id 4C6UESPW/2EAdwEAgWs5BA (envelope-from ) for ; Sun, 06 Feb 2022 15:07:31 +0100 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id IDnuDSPW/2GZPQAAauVa8A (envelope-from ) for ; Sun, 06 Feb 2022 15:07:31 +0100 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id BF8A1222A5 for ; Sun, 6 Feb 2022 15:07:30 +0100 (CET) Received: from localhost ([::1]:60718 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nGiCP-00058J-Tr for larch@yhetil.org; Sun, 06 Feb 2022 09:07:29 -0500 Received: from eggs.gnu.org ([209.51.188.92]:55466) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nGiBT-00057o-Se for emacs-orgmode@gnu.org; Sun, 06 Feb 2022 09:06:31 -0500 Received: from [2a00:1450:4864:20::635] (port=45929 helo=mail-ej1-x635.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nGiBD-0001Oa-SP for emacs-orgmode@gnu.org; Sun, 06 Feb 2022 09:06:18 -0500 Received: by mail-ej1-x635.google.com with SMTP id s21so6600896ejx.12 for ; Sun, 06 Feb 2022 06:06:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=MqvO1RIDb4YS7xYCJ0DZRdBCQmrTRGLtKZ8gl5weugo=; b=o5f2TnYtEvhhLM3v/mv7ERZhWj2iG451BUSUcS1lTow0jdpo1tawwj7K8iEkC7k2X3 m63JF9As2+4rXetnGIsU+nQ+ixrOTMQuJFQsgl/smDoVpkynNYp+x+AEMoQ1wwEIr+5V 2ChiR4wp74Qij26el/qnxxoLnAjWfu+i19yOx++PBKVN+pwc+ariHtkZR5bDfdu1q0E2 2BWMCJ7jTRDjL547zlmZCNzM5bzALHXUuWWOpA+pRoc4obRc/YuxkzfA0OxECojCCTm5 o0omAIQFUuY+igzoerIPsCW8x/G6JZoW88aDBygGTqSuCoeJOYMjMhowzjwMF8LTQUYm ZRxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=MqvO1RIDb4YS7xYCJ0DZRdBCQmrTRGLtKZ8gl5weugo=; b=AaIDyHyV38MZCD/KOlRlpXRDAMiEuERSHUdctGUI8jKpoGpDD7ydimmdSCNjgsD+xg X0uANS6DvU3C7FtM76ZsPM/E8qXgJXjW6pZEMgWuNXSMBOByLhpznJf7NWsynvM+rLyA qWl9Fl6L9o6mpFZtjWNPjYQLKsDPztTOd82inostTEeXqelSWm0LQjNx3fAXxtqjXV8u F+xPPj0lumlsUbV16JnEQMlMDV0sgT2VcV4BkrkPqdnmVixm/NIe+nzO8XRBu6nI5CsX nLDl+iLk68AxCTIVtgmzhEdAVP3X4At93ZFDdzRnqLX1BIF0RIDuWHdjSZSYEnT46thk /rzQ== X-Gm-Message-State: AOAM533/AMJB0BIW1KOT8GhSBsD/VfMGwZh2M0k6E+s7Z4BqIJND9Sfg Fymoa7lqKMX7REdmULgekAR1lr/ljPPkwg== X-Google-Smtp-Source: ABdhPJye1pbTGWVRz6ienXSeH3nenruh/UT5HEnO8sfIRnbedjdV125BBqftkMpx+llQiPbJa+ZN8g== X-Received: by 2002:a17:907:16a6:: with SMTP id hc38mr6670163ejc.291.1644156374000; Sun, 06 Feb 2022 06:06:14 -0800 (PST) Received: from localhost (p2e52f89b.dip0.t-ipconnect.de. [46.82.248.155]) by smtp.gmail.com with ESMTPSA id n3sm2639762ejr.6.2022.02.06.06.06.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Feb 2022 06:06:13 -0800 (PST) From: Valentin Herrmann To: emacs-orgmode@gnu.org Subject: [PATCH] lisp/org-capture.el: Add hook & hook options to org-capture Date: Sun, 6 Feb 2022 15:05:51 +0100 Message-Id: <20220206140551.1263130-1-herr.valentin.mann@gmail.com> X-Mailer: git-send-email 2.33.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::635 (failed) Received-SPF: pass client-ip=2a00:1450:4864:20::635; envelope-from=herr.valentin.mann@gmail.com; helo=mail-ej1-x635.google.com X-Spam_score_int: 17 X-Spam_score: 1.7 X-Spam_bar: + X-Spam_report: (1.7 / 5.0 requ) AC_FROM_MANY_DOTS=2.999, BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Valentin Herrmann Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1644156451; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=MqvO1RIDb4YS7xYCJ0DZRdBCQmrTRGLtKZ8gl5weugo=; b=vBFiBBcydernjQCEYgazVkesRqlN5a0FJLW2MkXTN2tH/yY+Sobzc1xpQdM8QVot4QDvQB akJVcRoMkA/mKhKpaJRpViwoXhvxxNwX0Q0LxrUcb42HOOn9dE/lDJ23Cqpc9zuabPTyCf WkVp445IshENEkOakyXGltm+weVtFvR42IuxHGe4FxpeDc6nuL8MrC+CkmUJZqSmq5Vzwx fgIKiSqa8iE6PyDuHUF7KEDsMFDNWWNggwqd7wJMPs3SGsByylZC+N5UmJl3Zqpc+/HNqV UL3TRMBbXTdIwsU3kNrUDkuWCHh5J16unuKI9JCPKgorJ7CAZRxImv05KgeKPg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1644156451; a=rsa-sha256; cv=none; b=cNK177a5xTe83FsGHyL7fVh7J8Ex9ZGhYOTMI3iF3hTrce6vFBbr94l5zTHyKZ9x3Pmi0+ BQzAfh3vthwGdUJZp5Oydw7cS4mbCS1t9GopzmHf72DwPOf4mhH9s/A3ADi6iP7p9pdGLP j6qsYFkDjpG0hs1JuPFU3bOi2Ic7CdD9pY9zUi4m30dRfvK2/3EodfgYP+JFPIezNRwS2T dosJPoTFBFV9IF4xh++xRM56dTImfYwRYEzsOJqu1Ys/54lRQ0+dAWyAowJ0hWVvMVbRWE /3SNFBlxVAhoAyICv8iSjFI6E5XIwWbq8NHWHxvHhuYt0/O3HPfk6H2B4zHbfg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=o5f2TnYt; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -3.83 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=o5f2TnYt; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: BF8A1222A5 X-Spam-Score: -3.83 X-Migadu-Scanner: scn1.migadu.com X-TUID: hQvnpw2j3rxp * lisp/org-capture.el: (org-capture-templates): Document the new template options. (org-capture-before-view-hook): Add new hook for parity with the template options. (org-capture): Implement the new template options. (org-capture-finalize): Implement the new template options. (org-capture-kill): Remove unneeded line. * doc/org-manual.org (Template elements): Document the new template options This change comes from my personal config, where I use e.g. the option `:before-finalize-hook' to capture an task automatically, whenever I capture using a certain template. --- doc/org-manual.org | 21 ++++++++ etc/ORG-NEWS | 26 ++++++++++ lisp/org-capture.el | 123 +++++++++++++++++++++++++++----------------- 3 files changed, 124 insertions(+), 46 deletions(-) diff --git a/doc/org-manual.org b/doc/org-manual.org index 2c54fde87..5efc2d797 100644 --- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -7817,6 +7817,27 @@ Now lets look at the elements of a template definition. Each entry in - ~:refile-targets :: Temporarily set ~org-refile-targets~ to the value of this property. + - ~:before-view-hook~ :: + + Hook that is run right after the capture buffer is made current. + The buffer is still narrowed. + + - ~:prepare-finalize-hook~ :: + + Hook that is run before the finalization starts. + The capture buffer is current and still narrowed. + + - ~:before-finalize-hook~ :: + + Hook that is run right before a capture process is finalized. + The capture buffer is still current when this hook runs and it is + widened to the entire buffer. + + - ~:after-finalize-hook~ :: + + Hook that is run right after a capture process is finalized. + Suitable for window cleanup. + **** Template expansion :PROPERTIES: :DESCRIPTION: Filling in information about time and context. diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 5a94e737e..25441027f 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -63,6 +63,32 @@ list of various table options (between brackets in LaTeX export), since certain tabular environments, such as longtblr of the tabularray LaTeX package, provides this structure. +*** New hook =org-capture-before-view-hook= for =org-capture= before view + +Hook that is run right after the capture buffer is made current. +The buffer is still narrowed. + +*** New option =:before-view-hook= for =org-capture= + +Hook that is run right after the capture buffer is made current. +The buffer is still narrowed. + +*** New option =:prepare-finalize-hook= for =org-capture= + +Hook that is run before the finalization starts. +The capture buffer is current and still narrowed. + +*** New option =:before-finalize-hook= for =org-capture= + +Hook that is run right before a capture process is finalized. +The capture buffer is still current when this hook runs and it is +widened to the entire buffer. + +*** New option =:after-finalize-hook= for =org-capture= + +Hook that is run right after a capture process is finalized. +Suitable for window cleanup. + ** New functions and changes in function arguments *** New function ~org-element-cache-map~ for quick mapping across Org elements diff --git a/lisp/org-capture.el b/lisp/org-capture.el index 5195b785e..27dd72ef8 100644 --- a/lisp/org-capture.el +++ b/lisp/org-capture.el @@ -235,64 +235,77 @@ template The template for creating the capture item. The rest of the entry is a property list of additional options. Recognized properties are: - :prepend Normally newly captured information will be appended at - the target location (last child, last table line, - last list item...). Setting this property will - change that. + :prepend Normally newly captured information will be appended at + the target location (last child, last table line, + last list item...). Setting this property will + change that. - :immediate-finish When set, do not offer to edit the information, just - file it away immediately. This makes sense if the - template only needs information that can be added - automatically. + :immediate-finish When set, do not offer to edit the information, just + file it away immediately. This makes sense if the + template only needs information that can be added + automatically. - :jump-to-captured When set, jump to the captured entry when finished. + :jump-to-captured When set, jump to the captured entry when finished. - :refile-targets When exiting capture mode via `org-capture-refile', the - variable `org-refile-targets' will be temporarily bound - to the value of this property. + :refile-targets When exiting capture mode via `org-capture-refile', the + variable `org-refile-targets' will be temporarily bound + to the value of this property. - :empty-lines Set this to the number of lines that should be inserted - before and after the new item. Default 0, only common - other value is 1. + :empty-lines Set this to the number of lines that should be inserted + before and after the new item. Default 0, only common + other value is 1. - :empty-lines-before Set this to the number of lines that should be inserted - before the new item. Overrides :empty-lines for the - number lines inserted before. + :empty-lines-before Set this to the number of lines that should be inserted + before the new item. Overrides :empty-lines for the + number lines inserted before. - :empty-lines-after Set this to the number of lines that should be inserted - after the new item. Overrides :empty-lines for the - number of lines inserted after. + :empty-lines-after Set this to the number of lines that should be inserted + after the new item. Overrides :empty-lines for the + number of lines inserted after. - :clock-in Start the clock in this item. + :clock-in Start the clock in this item. - :clock-keep Keep the clock running when filing the captured entry. + :clock-keep Keep the clock running when filing the captured entry. - :clock-resume Start the interrupted clock when finishing the capture. - Note that :clock-keep has precedence over :clock-resume. - When setting both to t, the current clock will run and - the previous one will not be resumed. + :clock-resume Start the interrupted clock when finishing the capture. + Note that :clock-keep has precedence over :clock-resume. + When setting both to t, the current clock will run and + the previous one will not be resumed. - :time-prompt Prompt for a date/time to be used for date/week trees - and when filling the template. + :time-prompt Prompt for a date/time to be used for date/week trees + and when filling the template. - :tree-type When `week', make a week tree instead of the month-day - tree. When `month', make a month tree instead of the - month-day tree. + :tree-type When `week', make a week tree instead of the month-day + tree. When `month', make a month tree instead of the + month-day tree. - :unnarrowed Do not narrow the target buffer, simply show the - full buffer. Default is to narrow it so that you - only see the new stuff. + :unnarrowed Do not narrow the target buffer, simply show the + full buffer. Default is to narrow it so that you + only see the new stuff. - :table-line-pos Specification of the location in the table where the - new line should be inserted. It should be a string like - \"II-3\", meaning that the new line should become the - third line before the second horizontal separator line. + :table-line-pos Specification of the location in the table where the + new line should be inserted. It should be a string like + \"II-3\", meaning that the new line should become the + third line before the second horizontal separator line. - :kill-buffer If the target file was not yet visited by a buffer when - capture was invoked, kill the buffer again after capture - is finalized. + :kill-buffer If the target file was not yet visited by a buffer when + capture was invoked, kill the buffer again after capture + is finalized. - :no-save Do not save the target file after finishing the capture. + :no-save Do not save the target file after finishing the capture. + + :before-view-hook Hook that is run right after the capture buffer is made current. + The buffer is still narrowed. + + :prepare-finalize-hook Hook that is run before the finalization starts. + The capture buffer is current and still narrowed. + + :before-finalize-hook Hook that is run right before a capture process is finalized. + The capture buffer is still current when this hook runs and it is + widened to the entire buffer. + + :after-finalize-hook Hook that is run right after a capture process is finalized. + Suitable for window cleanup. The template defines the text to be inserted. Often this is an Org mode entry (so the first line should start with a star) that @@ -476,6 +489,13 @@ The capture buffer is current and still narrowed." :version "24.1" :type 'hook) +(defcustom org-capture-before-view-hook nil + "Hook that is run right after the capture buffer is made current. +The buffer is still narrowed." + :group 'org-capture + :version "28.1" + :type 'hook) + (defcustom org-capture-bookmark t "When non-nil, add bookmark pointing at the last stored position when capturing." :group 'org-capture @@ -705,6 +725,10 @@ of the day at point (if any) or the current HH:MM time." (error "Capture template `%s': %s" (org-capture-get :key) (error-message-string error)))) + + (run-hooks 'org-capture-before-view-hook) + (if-let ((bvh (org-capture-get :before-view-hook))) + (funcall bvh)) (when (and (derived-mode-p 'org-mode) (org-capture-get :clock-in)) (condition-case nil (progn @@ -745,6 +769,8 @@ captured item after finalizing." (error "This does not seem to be a capture buffer for Org mode")) (run-hooks 'org-capture-prepare-finalize-hook) + (when-let ((pfh (org-capture-get :prepare-finalize-hook))) + (funcall pfh)) ;; Update `org-capture-plist' with the buffer-local value. Since ;; captures can be run concurrently, this is to ensure that @@ -817,7 +843,11 @@ captured item after finalizing." (org-capture-store-last-position) ;; Run the hook - (run-hooks 'org-capture-before-finalize-hook)) + (unless org-note-abort + (run-hooks 'org-capture-before-finalize-hook) + + (when-let ((bfh (org-capture-get :before-finalize-hook))) + (funcall bfh)))) (when (org-capture-get :decrypted) (save-excursion @@ -865,6 +895,8 @@ captured item after finalizing." (set-window-configuration return-wconf)) (run-hooks 'org-capture-after-finalize-hook) + (when-let ((afh (org-capture-get :after-finalize-hook))) + (funcall afh)) ;; Special cases (cond (abort-note @@ -918,8 +950,7 @@ for `entry'-type templates")) (interactive) ;; FIXME: This does not do the right thing, we need to remove the ;; new stuff by hand it is easy: undo, then kill the buffer - (let ((org-note-abort t) - (org-capture-before-finalize-hook nil)) + (let ((org-note-abort t)) (org-capture-finalize))) (defun org-capture-goto-last-stored () -- 2.33.1