Browse Source

Small refactoring

* lisp/org.el (org-add-planning-info): Small refactoring.
Nicolas Goaziou 10 years ago
parent
commit
1ff01cc07a
1 changed files with 68 additions and 75 deletions
  1. 68 75
      lisp/org.el

+ 68 - 75
lisp/org.el

@@ -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
 be removed."
   (interactive)
-  (let (org-time-was-given org-end-time-was-given ts
-			   end default-time default-input)
-
+  (let (org-time-was-given org-end-time-was-given default-time default-input)
     (catch 'exit
       (when (and (memq what '(scheduled deadline))
 		 (or (not time)
@@ -13408,15 +13406,14 @@ be removed."
 	;; Try to get a default date/time from existing timestamp
 	(save-excursion
 	  (org-back-to-heading t)
-	  (setq end (save-excursion (outline-next-heading) (point)))
-	  (when (re-search-forward (if (eq what 'scheduled)
-				       org-scheduled-time-regexp
-				     org-deadline-time-regexp)
-				   end t)
-	    (setq ts (match-string 1)
-		  default-time
-		  (apply 'encode-time (org-parse-time-string ts))
-		  default-input (and ts (org-get-compact-tod ts))))))
+	  (let ((end (save-excursion (outline-next-heading) (point))) ts)
+	    (when (re-search-forward (if (eq what 'scheduled)
+					 org-scheduled-time-regexp
+				       org-deadline-time-regexp)
+				     end t)
+	      (setq ts (match-string 1)
+		    default-time (apply 'encode-time (org-parse-time-string ts))
+		    default-input (and ts (org-get-compact-tod ts)))))))
       (when what
 	(setq time
 	      (if (stringp time)
@@ -13429,74 +13426,70 @@ be removed."
 					default-time default-input)))))
 
       (when (and org-insert-labeled-timestamps-at-point
-		 (member what '(scheduled deadline)))
+		 (memq what '(scheduled deadline)))
 	(insert
 	 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
 	(org-insert-time-stamp time org-time-was-given
 			       nil nil nil (list org-end-time-was-given))
 	(setq what nil))
-      (save-excursion
-	(save-restriction
-	  (let (col list elt ts buffer-invisibility-spec)
-	    (org-back-to-heading t)
-	    (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
-	    (goto-char (match-end 1))
-	    (setq col (current-column))
-	    (goto-char (match-end 0))
-	    (if (eobp) (insert "\n") (forward-char 1))
-	    (when (and (not what)
-		       (not (looking-at
-			     (concat "[ \t]*"
-				     org-keyword-time-not-clock-regexp))))
-	      ;; 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.
-	    (setq list (cons what remove))
-	    (while list
-	      (setq elt (pop list))
-	      (when (or (and (eq elt 'scheduled)
-			     (re-search-forward org-scheduled-time-regexp nil t))
-			(and (eq elt 'deadline)
-			     (re-search-forward org-deadline-time-regexp nil t))
-			(and (eq elt 'closed)
-			     (re-search-forward org-closed-time-regexp nil t)))
-		(replace-match "")
-		(if (looking-at "--+<[^>]+>") (replace-match ""))))
-	    (and (looking-at "[ \t]+") (replace-match ""))
-	    (and org-adapt-indentation (bolp) (org-indent-to-column col))
-	    (when what
-	      (insert
-	       (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
-	       (cond ((eq what 'scheduled) org-scheduled-string)
-		     ((eq what 'deadline) org-deadline-string)
-		     ((eq what 'closed) org-closed-string))
-	       " ")
-	      (setq ts (org-insert-time-stamp
-			time
-			(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)))
-	      (insert
-	       (if (not (or (bolp) (eq (char-before) ?\ )
-			    (memq (char-after) '(32 10))
-			    (eobp))) " " ""))
-	      (end-of-line 1))
-	    (goto-char (point-min))
-	    (widen)
-	    (if (and (looking-at "[ \t]*\n")
-		     (equal (char-before) ?\n))
-		(delete-region (1- (point)) (point-at-eol)))
-	    ts))))))
+      (org-with-wide-buffer
+       (let (col list elt ts buffer-invisibility-spec)
+	 (org-back-to-heading t)
+	 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
+	 (goto-char (match-end 1))
+	 (setq col (current-column))
+	 (goto-char (match-end 0))
+	 (if (eobp) (insert "\n") (forward-char 1))
+	 (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.
+	 (setq list (cons what remove))
+	 (while list
+	   (setq elt (pop list))
+	   (when (or (and (eq elt 'scheduled)
+			  (re-search-forward org-scheduled-time-regexp nil t))
+		     (and (eq elt 'deadline)
+			  (re-search-forward org-deadline-time-regexp nil t))
+		     (and (eq elt 'closed)
+			  (re-search-forward org-closed-time-regexp nil t)))
+	     (replace-match "")
+	     (if (looking-at "--+<[^>]+>") (replace-match ""))))
+	 (and (looking-at "[ \t]+") (replace-match ""))
+	 (and org-adapt-indentation (bolp) (org-indent-to-column col))
+	 (when what
+	   (insert
+	    (if (or (bolp) (eq (char-before) ?\s)) "" " ")
+	    (cond ((eq what 'scheduled) org-scheduled-string)
+		  ((eq what 'deadline) org-deadline-string)
+		  ((eq what 'closed) org-closed-string))
+	    " ")
+	   (setq ts (org-insert-time-stamp
+		     time
+		     (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-purpose nil)