Browse Source

Bug fixes related to clocking in with narrowed buffers.

Carsten Dominik 17 years ago
parent
commit
f476a046f5
4 changed files with 73 additions and 48 deletions
  1. 8 0
      ChangeLog
  2. 59 47
      lisp/org-clock.el
  3. 0 1
      lisp/org-compat.el
  4. 6 0
      lisp/org.el

+ 8 - 0
ChangeLog

@@ -1,5 +1,13 @@
 2008-04-29  Carsten Dominik  <dominik@science.uva.nl>
 
+	* lisp/org-clock.el (org-clock-goto): Widen buffer if necessary.
+	(org-clock-in): Make sure that also tasks outside the narrowed
+	region will be clocked in correctly.
+	(org-clock-insert-selection-line): Widen the buffer so that we can
+	find the correct task heading.
+
+	* lisp/org.el (org-base-buffer): New function.
+
 	* lisp/org-exp.el (org-icalendar-cleanup-string): Make sure ',"
 	and ";" are escaped.
 	(org-print-icalendar-entries): Also apply

+ 59 - 47
lisp/org-clock.el

@@ -172,14 +172,16 @@ of a different task.")
 (defun org-clock-insert-selection-line (i marker)
   (when (marker-buffer marker)
     (let (file cat task)
-      (with-current-buffer (marker-buffer marker)
+      (with-current-buffer (org-base-buffer (marker-buffer marker))
 	(save-excursion
-	  (goto-char marker)
-	  (setq file (buffer-file-name (marker-buffer marker))
-		cat (or (org-get-category)
-			(progn (org-refresh-category-properties)
-			       (org-get-category)))
-		task (org-get-heading 'notags))))
+	  (save-restriction
+	    (widen)
+	    (goto-char marker)
+	    (setq file (buffer-file-name (marker-buffer marker))
+		  cat (or (org-get-category)
+			  (progn (org-refresh-category-properties)
+				 (org-get-category)))
+		  task (org-get-heading 'notags)))))
       (when (and cat task)
 	(insert (format "[%c] %-15s %s\n" i cat task))
 	(cons i marker)))))
@@ -206,60 +208,69 @@ is as the default task, a special task that will always be offered in
 the clocking selection, associated with the letter `d'."
   (interactive "P")
   (let ((interrupting (marker-buffer org-clock-marker))
-	ts selected-task)
+	ts selected-task target-pos)
     (when (equal select '(4))
       (setq selected-task (org-clock-select-task "Clock-in on task: "))
       (if selected-task
 	  (setq selected-task (copy-marker selected-task))
 	(error "Abort")))
-    ;; Are we interrupting the clocking of a differnt task?
-    (if interrupting
-	(progn
-	  (move-marker org-clock-interrupted-task
-		       (marker-position org-clock-marker)
-		       (marker-buffer org-clock-marker))
-	  (org-clock-out t)))
+    (when interrupting
+      ;; We are interrupting the clocking of a differnt task.
+      ;; Save a marker to this task, so that we can go back.
+      (move-marker org-clock-interrupted-task
+		   (marker-position org-clock-marker)
+		   (marker-buffer org-clock-marker))
+      (org-clock-out t))
     
     (when (equal select '(16))
+      ;; Mark as default clocking task
       (save-excursion
 	(org-back-to-heading t)
 	(move-marker org-clock-default-task (point))))
     
+    (setq target-pos (point))  ;; we want to clock in at this location
     (save-excursion
-      (org-back-to-heading t)
       (when (and selected-task (marker-buffer selected-task))
-	(set-buffer (marker-buffer selected-task))
-	(goto-char selected-task)
+	;; There is a selected task, move to the correct buffer
+	;; and set the new target position.
+	(set-buffer (org-base-buffer (marker-buffer selected-task)))
+	(setq target-pos (marker-position selected-task))
 	(move-marker selected-task nil))
-      (or interrupting (move-marker org-clock-interrupted-task nil))
-      (org-clock-history-push)
-      (when (and org-clock-in-switch-to-state
-		 (not (looking-at (concat outline-regexp "[ \t]*"
-					  org-clock-in-switch-to-state
-					  "\\>"))))
-	(org-todo org-clock-in-switch-to-state))
-      (if (and org-clock-heading-function
-	       (functionp org-clock-heading-function))
-	  (setq org-clock-heading (funcall org-clock-heading-function))
-	(if (looking-at org-complex-heading-regexp)
-	    (setq org-clock-heading (match-string 4))
-	  (setq org-clock-heading "???")))
-      (setq org-clock-heading (propertize org-clock-heading 'face nil))
-      (org-clock-find-position)
-      
-      (insert "\n") (backward-char 1)
-      (indent-relative)
-      (insert org-clock-string " ")
-      (setq org-clock-start-time (current-time))
-      (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
-      (move-marker org-clock-marker (point) (buffer-base-buffer))
-      (or global-mode-string (setq global-mode-string '("")))
-      (or (memq 'org-mode-line-string global-mode-string)
-	  (setq global-mode-string
-		(append global-mode-string '(org-mode-line-string))))
-      (org-update-mode-line)
-      (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
-      (message "Clock started at %s" ts))))
+      (save-excursion
+	(save-restriction
+	  (widen)
+	  (goto-char target-pos)
+	  (org-back-to-heading t)
+	  (or interrupting (move-marker org-clock-interrupted-task nil))
+	  (org-clock-history-push)
+	  (when (and org-clock-in-switch-to-state
+		     (not (looking-at (concat outline-regexp "[ \t]*"
+					      org-clock-in-switch-to-state
+					      "\\>"))))
+	    (org-todo org-clock-in-switch-to-state))
+	  (if (and org-clock-heading-function
+		   (functionp org-clock-heading-function))
+	      (setq org-clock-heading (funcall org-clock-heading-function))
+	    (if (looking-at org-complex-heading-regexp)
+		(setq org-clock-heading (match-string 4))
+	      (setq org-clock-heading "???")))
+	  (setq org-clock-heading (propertize org-clock-heading 'face nil))
+	  (org-clock-find-position)
+	  
+	  (insert "\n") (backward-char 1)
+	  (indent-relative)
+	  (insert org-clock-string " ")
+	  (setq org-clock-start-time (current-time))
+	  (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
+	  (move-marker org-clock-marker (point) (buffer-base-buffer))
+	  (or global-mode-string (setq global-mode-string '("")))
+	  (or (memq 'org-mode-line-string global-mode-string)
+	      (setq global-mode-string
+		    (append global-mode-string '(org-mode-line-string))))
+	  (org-update-mode-line)
+	  (setq org-mode-line-timer
+		(run-with-timer 60 60 'org-update-mode-line))
+	  (message "Clock started at %s" ts))))))
 
 (defun org-clock-find-position ()
   "Find the location where the next clock line should be inserted."
@@ -389,6 +400,7 @@ With prefix arg SELECT, offer recently clocked tasks."
 	    (error "No task selected")
 	  (error "No active clock")))
     (switch-to-buffer (marker-buffer m))
+    (if (or (< m (point-min)) (> m (point-max))) (widen))
     (goto-char m)
     (org-show-entry)
     (org-back-to-heading)

+ 0 - 1
lisp/org-compat.el

@@ -248,7 +248,6 @@ that can be added."
 			      (cadr ext-inv-spec))))
    (move-to-column column force)))
  
-
 (provide 'org-compat)
 
 ;; arch-tag: a0a0579f-e68c-4bdf-9e55-93768b846bbe

+ 6 - 0
lisp/org.el

@@ -12712,6 +12712,12 @@ upon the next fontification round."
       (setq l (- l (get-text-property b 'org-dwidth-n s))))
     l))
 
+(defun org-base-buffer (buffer)
+  "Return the base buffer of BUFFER, if it has one.  Else return the buffer."
+  (if (not buffer)
+      buffer
+    (or (buffer-base-buffer buffer)
+	buffer)))
 
 (defun org-trim (s)
   "Remove whitespace at beginning and end of string."