Function: org-capture-place-table-line
org-capture-place-table-line is a byte-compiled function defined in
org-capture.el.gz.
Signature
(org-capture-place-table-line)
Documentation
Place the template as a table line.
Source Code
;; Defined in /usr/src/emacs/lisp/org/org-capture.el.gz
(defun org-capture-place-table-line ()
"Place the template as a table line."
(require 'org-table)
(let ((text
(pcase (org-trim (org-capture-get :template))
((pred (string-match-p org-table-border-regexp))
"| %?Bad template |")
(text (concat text "\n"))))
(table-line-pos (org-capture-get :table-line-pos))
beg end)
(cond
((org-capture-get :exact-position)
(org-with-point-at (org-capture-get :exact-position)
(setq beg (line-beginning-position))
(setq end (if (org-capture-get :insert-here) beg
(org-entry-end-position)))))
((not (org-capture-get :target-entry-p))
;; Table is not necessarily under a heading. Find first table
;; in the buffer.
(setq beg (point-min) end (point-max)))
(t
;; We are at a heading, limit search to the body.
(setq beg (line-beginning-position 2))
(setq end (save-excursion (outline-next-heading) (point)))))
(goto-char beg)
;; Narrow to the table, possibly creating one if necessary.
(catch :found
(while (re-search-forward org-table-dataline-regexp end t)
(pcase (org-element-lineage (org-element-at-point) 'table t)
(`nil nil)
((pred (lambda (e) (eq 'table.el (org-element-property :type e))))
nil)
(table
(goto-char (org-element-contents-end table))
(narrow-to-region (org-element-post-affiliated table)
(point))
(throw :found t))))
;; No table found. Create it with an empty header.
(goto-char end)
(unless (bolp) (insert "\n"))
(let ((origin (point-marker)))
(insert "| |\n|---|\n")
(narrow-to-region origin (point))))
;; In the current table, find the appropriate location for TEXT.
(cond
((org-capture-get :insert-here) nil)
((and table-line-pos
(string-match "\\(I+\\)\\([-+][0-9]+\\)" table-line-pos))
(goto-char (point-min))
(let ((line
(condition-case _
(progn
(save-match-data (org-table-analyze))
(aref org-table-hlines
(- (match-end 1) (match-beginning 1))))
(error
(error "Invalid table line specification %S" table-line-pos))))
(delta (string-to-number (match-string 2 table-line-pos))))
(forward-line (+ line delta (if (< delta 0) 0 -1)))
(forward-line))) ;insert below
((org-capture-get :prepend)
(goto-char (point-min))
(cond
((not (re-search-forward org-table-hline-regexp nil t)))
((re-search-forward org-table-dataline-regexp nil t) (forward-line 0))
(t (goto-char (org-table-end)))))
(t
(goto-char (org-table-end))))
;; Insert text and position point according to template.
(let ((origin (point-marker)))
(unless (bolp) (insert "\n"))
(let ((beg (point))
(end (save-excursion
(insert text)
(point))))
(org-capture-position-for-last-stored 'table-line)
(org-capture-mark-kill-region origin end)
;; TEXT is guaranteed to end with a newline character. Ignore
;; it when narrowing so as to not alter data on the next line.
(org-capture-narrow beg (1- end))
(org-capture--position-cursor beg (1- end))))))