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