Function: org-mobile-apply

org-mobile-apply is an interactive and byte-compiled function defined in org-mobile.el.gz.

Signature

(org-mobile-apply &optional BEG END)

Documentation

Apply all change requests in the current buffer.

If BEG and END are given, only do this in that region.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-mobile.el.gz
(defun org-mobile-apply (&optional beg end)
  "Apply all change requests in the current buffer.
If BEG and END are given, only do this in that region."
  (interactive)
  (require 'org-archive)
  (setq org-mobile-last-flagged-files nil)
  (setq beg (or beg (point-min)) end (or end (point-max)))

  ;; Remove all Note IDs
  (goto-char beg)
  (while (re-search-forward "^\\*\\* Note ID: [-0-9A-F]+[ \t]*\n" end t)
    (replace-match ""))

  ;; Find all the referenced entries, without making any changes yet
  (let ((marker (make-marker))
	(bos-marker (make-marker))
	(end (move-marker (make-marker) end))
	(cnt-new 0)
	(cnt-edit 0)
	(cnt-flag 0)
	(cnt-error 0)
	buf-list
	org-mobile-error)

    ;; Count the new captures
    (goto-char beg)
    (while (re-search-forward "^\\* \\(.*\\)" end t)
      (and (>= (- (match-end 1) (match-beginning 1)) 2)
	   (not (equal (downcase (substring (match-string 1) 0 2)) "f("))
	   (cl-incf cnt-new)))

    ;; Find and apply the edits
    (goto-char beg)
    (while (re-search-forward
	    "^\\*+[ \t]+F(\\([^():\n]*\\)\\(:\\([^()\n]*\\)\\)?)[ \t]+\\[\\[\\(\\(id\\|olp\\):\\([^]\n]+\\)\\)" end t)
      (catch 'next
	(let* ((action (match-string 1))
	       (data (and (match-end 3) (match-string 3)))
	       (id-pos (condition-case msg
			   (org-mobile-locate-entry (match-string 4))
			 (error (nth 1 msg))))
	       (bos (line-beginning-position))
	       (eos (save-excursion (org-end-of-subtree t t)))
	       (cmd (if (equal action "")
			(let ((note (buffer-substring-no-properties
				     (line-beginning-position 2) eos)))
			  (lambda (_data _old _new)
			    (cl-incf cnt-flag)
			    (org-toggle-tag "FLAGGED" 'on)
			    (org-entry-put
			     nil "THEFLAGGINGNOTE"
			     (replace-regexp-in-string "\n" "\\\\n" note))))
		      (cl-incf cnt-edit)
		      (cdr (assoc action org-mobile-action-alist))))
	       ;; Do not take notes interactively.
	       (org-inhibit-logging 'note)
	       old new)

	  (goto-char bos)
	  (when (and (markerp id-pos)
		     (not (member (marker-buffer id-pos) buf-list)))
	    (org-mobile-timestamp-buffer (marker-buffer id-pos))
	    (push (marker-buffer id-pos) buf-list))
	  (unless (markerp id-pos)
            (goto-char (+ 2 (line-beginning-position)))
	    (if (stringp id-pos)
		(insert id-pos " ")
	      (insert "BAD REFERENCE "))
	    (cl-incf cnt-error)
	    (throw 'next t))
	  (unless cmd
	    (insert "BAD FLAG ")
	    (cl-incf cnt-error)
	    (throw 'next t))
	  (move-marker bos-marker (point))
	  (if (re-search-forward "^\\** Old value[ \t]*$" eos t)
	      (setq old (buffer-substring
			 (1+ (match-end 0))
			 (progn (outline-next-heading) (point)))))
	  (if (re-search-forward "^\\** New value[ \t]*$" eos t)
	      (setq new (buffer-substring
			 (1+ (match-end 0))
			 (progn (outline-next-heading)
				(if (eobp) (org-back-over-empty-lines))
				(point)))))
	  (setq old (org-string-nw-p old))
	  (setq new (org-string-nw-p new))
	  (unless (equal data "body")
	    (setq new (and new (org-trim new)))
	    (setq old (and old (org-trim old))))
	  (goto-char (+ 2 bos-marker))
	  ;; Remember this place so that we can return
	  (move-marker marker (point))
	  (setq org-mobile-error nil)
	  (condition-case msg
	      (org-with-point-at id-pos
		(funcall cmd data old new)
		(unless (member data '("delete" "archive" "archive-sibling"
				       "addheading"))
		  (when (member "FLAGGED" (org-get-tags nil t))
		    (add-to-list 'org-mobile-last-flagged-files
				 (buffer-file-name)))))
	    (error (setq org-mobile-error msg)))
	  (when org-mobile-error
	    (pop-to-buffer-same-window (marker-buffer marker))
	    (goto-char marker)
	    (cl-incf cnt-error)
	    (insert (if (stringp (nth 1 org-mobile-error))
			(nth 1 org-mobile-error)
		      "EXECUTION FAILED")
		    " ")
	    (throw 'next t))
	  ;; If we get here, the action has been applied successfully
	  ;; So remove the entry
	  (goto-char bos-marker)
	  (delete-region (point) (org-end-of-subtree t t)))))
    (save-buffer)
    (move-marker marker nil)
    (move-marker end nil)
    (message "%d new, %d edits, %d flags, %d errors"
	     cnt-new cnt-edit cnt-flag cnt-error)
    (sit-for 1)))