From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id 8dl3LeamAGIIeAEAgWs5BA (envelope-from ) for ; Mon, 07 Feb 2022 05:58:14 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id oHJKJeamAGI+RgEAG6o9tA (envelope-from ) for ; Mon, 07 Feb 2022 05:58:14 +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 DDB8C366A4 for ; Mon, 7 Feb 2022 05:58:13 +0100 (CET) Received: from localhost ([::1]:37510 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nGw6O-0003w2-Os for larch@yhetil.org; Sun, 06 Feb 2022 23:58:12 -0500 Received: from eggs.gnu.org ([209.51.188.92]:42676) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nGdll-0004xs-UE for emacs-orgmode@gnu.org; Sun, 06 Feb 2022 04:23:42 -0500 Received: from [2a00:1450:4864:20::533] (port=44554 helo=mail-ed1-x533.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nGdlj-0001gf-6w for emacs-orgmode@gnu.org; Sun, 06 Feb 2022 04:23:41 -0500 Received: by mail-ed1-x533.google.com with SMTP id bx2so7089523edb.11 for ; Sun, 06 Feb 2022 01:23:38 -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=K1NHGHyf4MLzm7hV/DyvNzAoaAWNu3zDR5j0NpSj3S3jko1+54XeS4UOvblJmIAKBN /kUmBHT0+RSzIAb11KqymblyDJ+xk5eNAwXOUbgjvlbVATCbmCUY2WpegIZGZ/CiSD1l hS6NgCvQJO6GXB0+dfp5elIIJYWll6qX2eRODERMw56Tj2wi5pxppseOqacC22luYFcP 1DgMlUyFsNDY4cH7/1E4tOa9wJ6b57kVckc66d1FDlfZDYYrhGW4oHBG1Nb/uG2LAwhc vA0DIJps70lhHfRgl18kvAYyTx9wOvLk9WfN1CymhCLzsW6df4zanaQhD9PFfYlmWQlr tp4w== 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=4xdSbfxCgH9nTFGUTifMz64Gni+Or36Zca1P22IFEpG0RgC9R5E250aQ9inWdqzlR+ Fq/RX20RPEu/mLWw5+Lx6dS4LoAOGR+op3ZNykQ7uETWYoTvNS/Vu8lWsmy42KB+QLG+ TYBFbLb/vxkaZiKjCHz3nu1sfSFmBdRa8Mb8oC+YbGaDvcp3zhQwIsjGLhMs47UTmQcE 3BY0WYn3kIw9sl7Lt4IxFV0bB/EMq4Z8jJWYkfi7x0GQW0Ao09X6tabwAueLrqGVqn/5 XVo35dvoTKLWsyGT1tOG+b0ydy2DXXz93T948a47sI5esICb4BCS9GK6V4L3v2LmzlMt 9uhA== X-Gm-Message-State: AOAM532N3JDceJ02qODXJQT0k7nEO+4hjYGqkIotyNePuyl2poB2n6F7 9IaacyGtUNOiiIRtYMS7atCp6L4zfkAScQ== X-Google-Smtp-Source: ABdhPJyIYrdHWJRRkn8p3MJ7q7q6YOCi+c7Gg9hNM/8xEl36YlLYbgcpgtEb9WV+odb6J+UOYjbkvA== X-Received: by 2002:a05:6402:4385:: with SMTP id o5mr8294278edc.48.1644139417157; Sun, 06 Feb 2022 01:23:37 -0800 (PST) Received: from localhost (p2e52f89b.dip0.t-ipconnect.de. [46.82.248.155]) by smtp.gmail.com with ESMTPSA id s9sm3414846edj.48.2022.02.06.01.23.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Feb 2022 01:23:36 -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 10:22:51 +0100 Message-Id: <20220206092251.1159025-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::533 (failed) Received-SPF: pass client-ip=2a00:1450:4864:20::533; envelope-from=herr.valentin.mann@gmail.com; helo=mail-ed1-x533.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-Mailman-Approved-At: Sun, 06 Feb 2022 23:56:46 -0500 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=1644209894; 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=XHnqeuSGG2spkxj2i0MJnKAGwT6oJt/YDSpuL6Jk5AVqURDbvgpSVcFyeID6xmLlZJGuhS sZqO7tPWAufK1cx0F/X+H1XoJXwpfd2L+Yb9jb0lqu4SocGiN2QYMeaQEzAbxJh2QPR0sN M3T2tu4Iqs4MTgklhFa4t3pw8UbPMmHsXYAExbrrT1t3nW/SbsaoepBwLA+aJBKzn4Vx// s2JwJGsNnrF2BsJd1aZ+0Z3WrCl5EpqrND1e19yJchPP7bX89HQe0759F2i3Nk6aeA6V2P aJjTXWBoTqILMzaFIiTdDjf2nv73s3DiOtavE3MBNk8HrIDZuznjO+PVtCMyLg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1644209894; a=rsa-sha256; cv=none; b=fADKD7R43gDxyRaI85YHi/smi1oQs1gsOXktlnT0ud+faSrvLf2tPROYwxbXHVrnnHKLgk j7mLTdbE14gIit2jTP9/mN+Al7IuAuTaaM07A03z5AXGqeZPgCUxHorKTjlBE+aJL+YCH3 sgvMnkLxvsvTL0MX3lDA2wLrH+Qzn5Y15DqVqNSY8ZxdgNbGccoglp9j9OzxcQOqd+BDJs eghq1TAUplMayPO60GqboRTGHoJCpDSOYamRyG6XP1FHhJLwouKRaMVPmMaBEoXNAkKjca Y6Nvwe9TxrvUKuht4HIP6W56SbYIkE5IBsJPgueAbvDHemfB8Ho9CZQTRY4PVA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=K1NHGHyf; 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: -5.33 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=K1NHGHyf; 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: DDB8C366A4 X-Spam-Score: -5.33 X-Migadu-Scanner: scn0.migadu.com X-TUID: zjVBnG7nGjrd * 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