Function: org-log-beginning

org-log-beginning is a byte-compiled function defined in org.el.gz.

Signature

(org-log-beginning &optional CREATE)

Documentation

Return expected start of log notes in current entry.

When optional argument CREATE is non-nil, the function creates a drawer to store notes, if necessary. Returned position ignores narrowing.

Source Code

;; Defined in /usr/src/emacs/lisp/org/org.el.gz
(defun org-log-beginning (&optional create)
  "Return expected start of log notes in current entry.
When optional argument CREATE is non-nil, the function creates
a drawer to store notes, if necessary.  Returned position ignores
narrowing."
  (org-with-wide-buffer
   (let ((drawer (org-log-into-drawer)))
     (cond
      (drawer
       ;; This either moves past planning and property drawer, to
       ;; first line below heading, or to `eob' (if heading is the
       ;; last heading in buffer without contents).
       (org-end-of-meta-data)
       (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
	     (end (if (org-at-heading-p) (point)
		    (save-excursion (outline-next-heading) (point))))
	     (case-fold-search t))
	 (catch 'exit
	   ;; Try to find existing drawer.
	   (while (re-search-forward regexp end t)
	     (let ((element (org-element-at-point)))
	       (when (org-element-type-p element 'drawer)
		 (let ((cend  (org-element-contents-end element)))
		   (when (and (not org-log-states-order-reversed) cend)
		     (goto-char cend)))
		 (throw 'exit nil))))
	   ;; No drawer found.  Create one, if permitted.
	   (when create
             ;; `org-end-of-meta-data' ended up at next heading
             ;; * Heading to insert darawer<maybe folded>
             ;; * Another heading
             ;;
             ;; Unless current heading is the last heading in buffer
             ;; and does not have a newline, `org-end-of-meta-data'
             ;; can move us to the next heading.
             ;; Avoid situation when we insert drawer right before
             ;; first "*".  Otherwise, if the heading is folded, we
             ;; are inserting after visible newline at the end of the
             ;; fold, thus breaking the fold continuity.
             (unless (eobp)
               (when (org-at-heading-p) (backward-char)))
             (org-fold-core-ignore-modifications
               (let (;; Heading
                     ;; <point>
                     ;; Text
                     (at-blank-line? (looking-at-p "^[ \t]*$"))
                     ;; Heading
                     ;; <point>Text
                     (at-beginning-of-non-blank-line?
                      (and (bolp) (not (eolp)))))
                 (unless (bolp)
                   ;; Heading<point> (see `backward-char' branch above)
                   (insert-and-inherit "\n"))
                 (let ((beg (point)) cbeg)
                   (insert-and-inherit ":" drawer ":")
                   (setq cbeg (point))
                   (insert-and-inherit "\n:END:")
                   (cond
                    (at-blank-line?
                     ;; Heading
                     ;; :LOGBOOK:
                     ;; :END:
                     ;;
                     ;; Text
                     (insert "\n")
                     (backward-char))
                    (at-beginning-of-non-blank-line?
                     ;; Heading
                     ;; :LOGBOOK:
                     ;; :END:
                     ;; Text
                     (insert "\n")
                     (backward-char)))
                   (org-indent-region beg (point))
                   (org-fold-region cbeg (point) t 'drawer)))))
	   (end-of-line 0))))
      (t
       (org-end-of-meta-data org-log-state-notes-insert-after-drawers)
       (let ((endpos (point)))
         (skip-chars-forward " \t\n")
         (forward-line 0)
         (unless org-log-states-order-reversed
	   (org-skip-over-state-notes)
	   (skip-chars-backward " \t\n")
	   (forward-line 1))
         ;; When current headline is at the end of buffer and does not
         ;; end with trailing newline the above can move to the
         ;; beginning of the headline.
         (when (< (point) endpos) (goto-char endpos))))))
   (if (bolp) (point) (line-beginning-position 2))))