Function: org-capture-finalize
org-capture-finalize is an interactive and byte-compiled function
defined in org-capture.el.gz.
Signature
(org-capture-finalize &optional STAY-WITH-CAPTURE)
Documentation
Finalize the capture process.
With prefix argument STAY-WITH-CAPTURE, jump to the location of the captured item after finalizing.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/org/org-capture.el.gz
(defun org-capture-finalize (&optional stay-with-capture)
"Finalize the capture process.
With prefix argument STAY-WITH-CAPTURE, jump to the location of the
captured item after finalizing."
(interactive "P")
(when (org-capture-get :jump-to-captured)
(setq stay-with-capture t))
(unless (and org-capture-mode
(buffer-base-buffer (current-buffer)))
(error "This does not seem to be a capture buffer for Org mode"))
(org-capture--run-template-functions :prepare-finalize 'local)
(run-hooks 'org-capture-prepare-finalize-hook)
;; Update `org-capture-plist' with the buffer-local value. Since
;; captures can be run concurrently, this is to ensure that
;; `org-capture-after-finalize-hook' accesses the proper plist.
(setq org-capture-plist org-capture-current-plist)
;; Did we start the clock in this capture buffer?
(when (and org-capture-clock-was-started
(equal org-clock-marker org-capture-clock-was-started))
;; Looks like the clock we started is still running.
(if org-capture-clock-keep
;; User may have completed clocked heading from the template.
;; Refresh clock mode line.
(org-clock-update-mode-line t)
;; Clock out. Possibly resume interrupted clock.
(let (org-log-note-clock-out) (org-clock-out))
(when (and (org-capture-get :clock-resume 'local)
(markerp (org-capture-get :interrupted-clock 'local))
(buffer-live-p (marker-buffer
(org-capture-get :interrupted-clock 'local))))
(let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
(org-with-point-at clock-in-task (org-clock-in)))
(message "Interrupted clock has been resumed"))))
(let ((abort-note nil))
;; Store the size of the capture buffer
(org-capture-put :captured-entry-size (- (point-max) (point-min)))
(widen)
;; Store the insertion point in the target buffer
(org-capture-put :insertion-point (point))
(if org-note-abort
(let ((beg (org-capture-get :begin-marker 'local))
(end (org-capture-get :end-marker 'local)))
(if (not (and beg end)) (setq abort-note 'dirty)
(setq abort-note t)
(org-with-wide-buffer (kill-region beg end))))
;; Postprocessing: Update Statistics cookies, do the sorting
(when (derived-mode-p 'org-mode)
(save-excursion
(when (ignore-errors (org-back-to-heading))
(org-update-parent-todo-statistics)
(org-update-checkbox-count)))
;; FIXME Here we should do the sorting
;; If we have added a table line, maybe recompute?
(when (and (eq (org-capture-get :type 'local) 'table-line)
(org-at-table-p))
(if (not (org-table-get-stored-formulas)) (org-table-align)
;; Adjust formulas, if necessary. We assume a non-nil
;; `:immediate-finish' means that no confirmation is
;; required. Else, obey `org-table-fix-formulas-confirm'.
;;
;; The delta required to fix formulas depends on the
;; number of rows inserted by the template.
(when (or (org-capture-get :immediate-finish)
(not org-table-fix-formulas-confirm)
(funcall org-table-fix-formulas-confirm "Fix formulas? "))
(org-table-fix-formulas
"@" nil (1- (org-table-current-dline))
(count-lines (org-capture-get :begin-marker 'local)
(org-capture-get :end-marker 'local))))
(org-table-recalculate 'all)))) ;FIXME: should we iterate?
;; Store this place as the last one where we stored something
;; Do the marking in the base buffer, so that it makes sense after
;; the indirect buffer has been killed.
(org-capture-store-last-position)
(org-capture--run-template-functions :before-finalize 'local)
;; Run the hook
(run-hooks 'org-capture-before-finalize-hook))
(when (org-capture-get :decrypted)
(save-excursion
(goto-char (org-capture-get :decrypted))
(org-encrypt-entry)))
(unless (org-capture-get :no-save) (save-buffer))
(let ((return-wconf (org-capture-get :return-to-wconf 'local))
(new-buffer (org-capture-get :new-buffer 'local))
(kill-buffer (org-capture-get :kill-buffer 'local))
(base-buffer (buffer-base-buffer (current-buffer))))
;; Kill the indirect buffer
(kill-buffer (current-buffer))
;; Narrow back the target buffer to its previous state
(with-current-buffer (org-capture-get :buffer)
(let ((reg (org-capture-get :initial-target-region))
(pos (org-capture-get :initial-target-position))
(ipt (org-capture-get :insertion-point))
(size (org-capture-get :captured-entry-size)))
(if (not reg)
(widen)
(cond ((< ipt (car reg))
;; insertion point is before the narrowed region
(narrow-to-region (+ size (car reg)) (+ size (cdr reg))))
((> ipt (cdr reg))
;; insertion point is after the narrowed region
(narrow-to-region (car reg) (cdr reg)))
(t
;; insertion point is within the narrowed region
(narrow-to-region (car reg) (+ size (cdr reg)))))
;; now place back the point at its original position
(if (< ipt (car reg))
(goto-char (+ size pos))
(goto-char (if (< ipt pos) (+ size pos) pos))))))
(if (and base-buffer org-note-abort new-buffer)
;; Unconditionally kill the new buffer when capture is
;; aborted.
(with-current-buffer base-buffer
(set-buffer-modified-p nil)
(kill-buffer))
;; Kill the target buffer if that is desired
(when (and base-buffer new-buffer kill-buffer)
(with-current-buffer base-buffer (save-buffer))
(kill-buffer base-buffer)))
;; Restore the window configuration before capture
(set-window-configuration return-wconf))
;; Do not use the local arg to `org-capture--run-template-functions' here.
;; The buffer-local value has been stored on `org-capture-plist'.
(org-capture--run-template-functions :after-finalize)
(run-hooks 'org-capture-after-finalize-hook)
;; Special cases
(cond
(abort-note
(cl-case abort-note
(clean
(message "Capture process aborted and target buffer cleaned up"))
(dirty
(error "Capture process aborted, but target buffer could not be \
cleaned up correctly"))))
(stay-with-capture
(org-capture-goto-last-stored)))
;; Return if we did store something
(not abort-note)))