Preserve indentation when modifying planning info

* lisp/org.el (org-add-planning-info): Preserve indentation when
  modifying planning info.  Refactor code.

Thanks to Sébastien Vauban for suggesting the idea.
This commit is contained in:
Nicolas Goaziou 2014-11-27 00:07:20 +01:00
parent ae9ac316a5
commit 80d13fffe0
1 changed files with 42 additions and 57 deletions

View File

@ -13515,63 +13515,48 @@ be removed."
default-time default-input))))) default-time default-input)))))
(org-with-wide-buffer (org-with-wide-buffer
(let (col list elt ts buffer-invisibility-spec) (org-back-to-heading t)
(org-back-to-heading t) (forward-line)
(looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*")) (unless (bolp) (insert "\n"))
(goto-char (match-end 1)) (cond ((org-looking-at-p org-planning-line-re)
(setq col (current-column)) ;; Move to current indentation.
(goto-char (match-end 0)) (skip-chars-forward " \t")
(if (eobp) (insert "\n") (forward-char 1)) ;; Check if we have to remove something.
(unless (or what (org-looking-at-p org-planning-line-re)) (dolist (type (if what (cons what remove) remove))
;; Nothing to add, nothing to remove...... :-) (when (save-excursion
(throw 'exit nil)) (re-search-forward
(if (and (not (looking-at org-outline-regexp)) (case type
(looking-at (concat "[^\r\n]*?" org-keyword-time-regexp (closed org-closed-time-regexp)
"[^\r\n]*")) (deadline org-deadline-time-regexp)
(not (equal (match-string 1) org-clock-string))) (scheduled org-scheduled-time-regexp)
(narrow-to-region (match-beginning 0) (match-end 0)) (otherwise (error "Invalid planning type: %s" type)))
(insert-before-markers "\n") (line-end-position) t))
(backward-char 1) (replace-match "")
(narrow-to-region (point) (point)) (when (looking-at "--+<[^>]+>") (replace-match ""))))
(and org-adapt-indentation (org-indent-to-column col))) ;; Remove leading white spaces.
;; Check if we have to remove something. (when (looking-at "[ \t]+") (replace-match "")))
(setq list (cons what remove)) ((not what) (throw 'exit nil)) ; Nothing to do.
(while list (t (insert-before-markers "\n")
(setq elt (pop list)) (backward-char 1)
(when (or (and (eq elt 'scheduled) (when org-adapt-indentation
(re-search-forward org-scheduled-time-regexp nil t)) (org-indent-to-column (1+ (org-outline-level))))))
(and (eq elt 'deadline) (when what
(re-search-forward org-deadline-time-regexp nil t)) ;; Insert planning keyword.
(and (eq elt 'closed) (insert (case what
(re-search-forward org-closed-time-regexp nil t))) (closed org-closed-string)
(replace-match "") (deadline org-deadline-string)
(if (looking-at "--+<[^>]+>") (replace-match "")))) (scheduled org-scheduled-string)
(and (looking-at "[ \t]+") (replace-match "")) (otherwise (error "Invalid planning type: %s" what)))
(and org-adapt-indentation (bolp) (org-indent-to-column col)) " ")
(when what ;; Insert associated timestamp.
(insert (let ((ts (org-insert-time-stamp
(if (or (bolp) (eq (char-before) ?\s)) "" " ") time
(cond ((eq what 'scheduled) org-scheduled-string) (or org-time-was-given
((eq what 'deadline) org-deadline-string) (and (eq what 'closed) org-log-done-with-time))
((eq what 'closed) org-closed-string)) (eq what 'closed)
" ") nil nil (list org-end-time-was-given))))
(setq ts (org-insert-time-stamp (unless (eolp) (insert " "))
time ts))))))
(or org-time-was-given
(and (eq what 'closed) org-log-done-with-time))
(eq what 'closed)
nil nil (list org-end-time-was-given)))
(unless (or (bolp)
(eq (char-before) ?\s)
(memq (char-after) '(?\n ?\s))
(eobp))
(insert " "))
(end-of-line 1))
(goto-char (point-min))
(widen)
(when (and (looking-at "[ \t]*\n") (eq (char-before) ?\n))
(delete-region (1- (point)) (line-end-position)))
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)