Small refactoring

* lisp/org.el (org-add-planning-info): Small refactoring.
This commit is contained in:
Nicolas Goaziou 2014-09-22 22:29:22 +02:00
parent ed825d738b
commit 1ff01cc07a
1 changed files with 68 additions and 75 deletions

View File

@ -13397,9 +13397,7 @@ If non is given, the user is prompted for a date.
REMOVE indicates what kind of entries to remove. An old WHAT entry will also REMOVE indicates what kind of entries to remove. An old WHAT entry will also
be removed." be removed."
(interactive) (interactive)
(let (org-time-was-given org-end-time-was-given ts (let (org-time-was-given org-end-time-was-given default-time default-input)
end default-time default-input)
(catch 'exit (catch 'exit
(when (and (memq what '(scheduled deadline)) (when (and (memq what '(scheduled deadline))
(or (not time) (or (not time)
@ -13408,15 +13406,14 @@ be removed."
;; Try to get a default date/time from existing timestamp ;; Try to get a default date/time from existing timestamp
(save-excursion (save-excursion
(org-back-to-heading t) (org-back-to-heading t)
(setq end (save-excursion (outline-next-heading) (point))) (let ((end (save-excursion (outline-next-heading) (point))) ts)
(when (re-search-forward (if (eq what 'scheduled) (when (re-search-forward (if (eq what 'scheduled)
org-scheduled-time-regexp org-scheduled-time-regexp
org-deadline-time-regexp) org-deadline-time-regexp)
end t) end t)
(setq ts (match-string 1) (setq ts (match-string 1)
default-time default-time (apply 'encode-time (org-parse-time-string ts))
(apply 'encode-time (org-parse-time-string ts)) default-input (and ts (org-get-compact-tod ts)))))))
default-input (and ts (org-get-compact-tod ts))))))
(when what (when what
(setq time (setq time
(if (stringp time) (if (stringp time)
@ -13429,74 +13426,70 @@ be removed."
default-time default-input))))) default-time default-input)))))
(when (and org-insert-labeled-timestamps-at-point (when (and org-insert-labeled-timestamps-at-point
(member what '(scheduled deadline))) (memq what '(scheduled deadline)))
(insert (insert
(if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ") (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
(org-insert-time-stamp time org-time-was-given (org-insert-time-stamp time org-time-was-given
nil nil nil (list org-end-time-was-given)) nil nil nil (list org-end-time-was-given))
(setq what nil)) (setq what nil))
(save-excursion (org-with-wide-buffer
(save-restriction (let (col list elt ts buffer-invisibility-spec)
(let (col list elt ts buffer-invisibility-spec) (org-back-to-heading t)
(org-back-to-heading t) (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
(looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*")) (goto-char (match-end 1))
(goto-char (match-end 1)) (setq col (current-column))
(setq col (current-column)) (goto-char (match-end 0))
(goto-char (match-end 0)) (if (eobp) (insert "\n") (forward-char 1))
(if (eobp) (insert "\n") (forward-char 1)) (unless (or what (org-looking-at-p org-planning-line-re))
(when (and (not what) ;; Nothing to add, nothing to remove...... :-)
(not (looking-at (throw 'exit nil))
(concat "[ \t]*" (if (and (not (looking-at org-outline-regexp))
org-keyword-time-not-clock-regexp)))) (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
;; Nothing to add, nothing to remove...... :-) "[^\r\n]*"))
(throw 'exit nil)) (not (equal (match-string 1) org-clock-string)))
(if (and (not (looking-at org-outline-regexp)) (narrow-to-region (match-beginning 0) (match-end 0))
(looking-at (concat "[^\r\n]*?" org-keyword-time-regexp (insert-before-markers "\n")
"[^\r\n]*")) (backward-char 1)
(not (equal (match-string 1) org-clock-string))) (narrow-to-region (point) (point))
(narrow-to-region (match-beginning 0) (match-end 0)) (and org-adapt-indentation (org-indent-to-column col)))
(insert-before-markers "\n") ;; Check if we have to remove something.
(backward-char 1) (setq list (cons what remove))
(narrow-to-region (point) (point)) (while list
(and org-adapt-indentation (org-indent-to-column col))) (setq elt (pop list))
;; Check if we have to remove something. (when (or (and (eq elt 'scheduled)
(setq list (cons what remove)) (re-search-forward org-scheduled-time-regexp nil t))
(while list (and (eq elt 'deadline)
(setq elt (pop list)) (re-search-forward org-deadline-time-regexp nil t))
(when (or (and (eq elt 'scheduled) (and (eq elt 'closed)
(re-search-forward org-scheduled-time-regexp nil t)) (re-search-forward org-closed-time-regexp nil t)))
(and (eq elt 'deadline) (replace-match "")
(re-search-forward org-deadline-time-regexp nil t)) (if (looking-at "--+<[^>]+>") (replace-match ""))))
(and (eq elt 'closed) (and (looking-at "[ \t]+") (replace-match ""))
(re-search-forward org-closed-time-regexp nil t))) (and org-adapt-indentation (bolp) (org-indent-to-column col))
(replace-match "") (when what
(if (looking-at "--+<[^>]+>") (replace-match "")))) (insert
(and (looking-at "[ \t]+") (replace-match "")) (if (or (bolp) (eq (char-before) ?\s)) "" " ")
(and org-adapt-indentation (bolp) (org-indent-to-column col)) (cond ((eq what 'scheduled) org-scheduled-string)
(when what ((eq what 'deadline) org-deadline-string)
(insert ((eq what 'closed) org-closed-string))
(if (not (or (bolp) (eq (char-before) ?\ ))) " " "") " ")
(cond ((eq what 'scheduled) org-scheduled-string) (setq ts (org-insert-time-stamp
((eq what 'deadline) org-deadline-string) time
((eq what 'closed) org-closed-string)) (or org-time-was-given
" ") (and (eq what 'closed) org-log-done-with-time))
(setq ts (org-insert-time-stamp (eq what 'closed)
time nil nil (list org-end-time-was-given)))
(or org-time-was-given (unless (or (bolp)
(and (eq what 'closed) org-log-done-with-time)) (eq (char-before) ?\s)
(eq what 'closed) (memq (char-after) '(?\n ?\s))
nil nil (list org-end-time-was-given))) (eobp))
(insert (insert " "))
(if (not (or (bolp) (eq (char-before) ?\ ) (end-of-line 1))
(memq (char-after) '(32 10)) (goto-char (point-min))
(eobp))) " " "")) (widen)
(end-of-line 1)) (when (and (looking-at "[ \t]*\n") (eq (char-before) ?\n))
(goto-char (point-min)) (delete-region (1- (point)) (line-end-position)))
(widen) ts)))))
(if (and (looking-at "[ \t]*\n")
(equal (char-before) ?\n))
(delete-region (1- (point)) (point-at-eol)))
ts))))))
(defvar org-log-note-marker (make-marker)) (defvar org-log-note-marker (make-marker))
(defvar org-log-note-purpose nil) (defvar org-log-note-purpose nil)