Browse Source

Capture: Put final touched to file+regexp & file+function targets

* lisp/org-capture.el (org-capture-set-target-location): Store
exact positions for file+regexp and file+function targets.
(org-capture-place-entry, org-capture-place-item)
(org-capture-place-table-line, org-capture-place-plain-text): Respect
exact positions.
(org-capture-finalize): Make sure we are at the beginning of a line
when fixing the empty lines after the entry.
Carsten Dominik 14 years ago
parent
commit
8b7c742750
1 changed files with 17 additions and 4 deletions
  1. 17 4
      lisp/org-capture.el

+ 17 - 4
lisp/org-capture.el

@@ -454,6 +454,7 @@ bypassed."
 			 '(entry item checkitem plain)))
 	(save-excursion
 	  (goto-char end)
+	  (or (bolp) (newline))
 	  (org-capture-empty-lines-after
 	   (or (org-capture-get :empty-lines 'local) 0))))
       ;; Postprocessing:  Update Statistics cookies, do the sorting
@@ -567,9 +568,10 @@ already gone."
 	(goto-char (point-min))
 	(if (re-search-forward (nth 2 target) nil t)
 	    (progn
-	      (goto-char (match-beginning 0))
+	      (goto-char (if (org-capture-get :prepend)
+			     (match-beginning 0) (match-end 0)))
+	      (org-capture-put :exact-position (point))
 	      (setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
-	  (kill-buffer (current-buffer))
 	  (error "No match for target regexp in file %s" (nth 1 target))))
 
        ((eq (car target) 'file+datetree)
@@ -588,6 +590,7 @@ already gone."
        ((eq (car target) 'file+function)
 	(set-buffer (org-capture-target-buffer (nth 1 target)))
 	(funcall (nth 2 target))
+	(org-capture-put :exact-position (point))
 	(setq target-entry-p (and (org-mode-p) (org-at-heading-p))))
 
        ((eq (car target) 'clock)
@@ -640,6 +643,8 @@ already gone."
 	 (target-entry-p (org-capture-get :target-entry-p))
 	 level beg end)
     (cond
+     ((org-capture-get :exact-position)
+      (goto-char (org-capture-get :exact-position)))
      ((not target-entry-p)
       ;; Insert as top-level entry, either at beginning or at end of file
       (setq level 1)
@@ -674,8 +679,11 @@ already gone."
   "Place the template as a new plain list item."
   (let* ((txt (org-capture-get :template))
 	 (target-entry-p (org-capture-get :target-entry-p))
-	 ind beg end)
+	 (ind 0)
+	 beg end)
     (cond
+     ((org-capture-get :exact-position)
+      (goto-char (org-capture-get :exact-position)))
      ((not target-entry-p)
       ;; Insert as top-level entry, either at beginning or at end of file
       (setq beg (point-min) end (point-max)))
@@ -731,6 +739,8 @@ already gone."
 	 (table-line-pos (org-capture-get :table-line-pos))
 	 ind beg end)
     (cond
+     ((org-capture-get :exact-position)
+      (goto-char (org-capture-get :exact-position)))
      ((not target-entry-p)
       ;; Table is not necessarily under a heading
       (setq beg (point-min) end (point-max)))
@@ -804,7 +814,10 @@ already gone."
   "Place the template plainly."
   (let* ((txt (org-capture-get :template))
 	 beg end)
-    (goto-char (if (org-capture-get :prepend) (point-min) (point-max)))
+    (goto-char (cond
+		((org-capture-get :exact-position))
+		((org-capture-get :prepend) (point-min))
+		(t (point-max))))
     (or (bolp) (newline))
     (org-capture-empty-lines-before)
     (setq beg (point))