emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/excorporate 47fb189 73/93: Excorporate: Add some helper


From: Stefan Monnier
Subject: [elpa] externals/excorporate 47fb189 73/93: Excorporate: Add some helper functions to Org backend
Date: Sun, 29 Nov 2020 15:43:12 -0500 (EST)

branch: externals/excorporate
commit 47fb189b963775ded9aeb32f6f2150e63a58c0f1
Author: Thomas Fitzsimmons <fitzsim@fitzsim.org>
Commit: Thomas Fitzsimmons <fitzsim@fitzsim.org>

    Excorporate: Add some helper functions to Org backend
    
    * packages/excorporate/excorporate-org.el
    (exco-org--item-identifier-at-point): New helper function.
    (exco-org--handle-response): New macro.
    (exco-org--remove-element): New helper function.
    (exco-org-cancel-meeting): Call new helper macro and functions.
    (exco-org-delete-appointment): Likewise.
---
 excorporate-org.el | 90 ++++++++++++++++++++++++++----------------------------
 1 file changed, 44 insertions(+), 46 deletions(-)

diff --git a/excorporate-org.el b/excorporate-org.el
index 78d04ff..25b7986 100644
--- a/excorporate-org.el
+++ b/excorporate-org.el
@@ -41,6 +41,12 @@
     (string-match "Calendar (\\(.*\\))$" headline)
     (car (read-from-string (match-string 1 headline)))))
 
+(defun exco-org--item-identifier-at-point ()
+  "Return the item identifier associated with point."
+  (car
+   (read-from-string
+   (org-entry-get (car (org-get-property-block)) "Identifier"))))
+
 (defun exco-org--is-meeting ()
   "Return t if the entry at point is a meeting, not an appointment."
   (save-excursion
@@ -78,6 +84,32 @@
      (t
       (error "Did not recognize error")))))
 
+(defmacro exco-org--handle-response (response
+                                    response-type success failure &rest forms)
+  "Handle a server response RESPONSE.
+RESPONSE-TYPE is one of CreateItemResponseMessage or
+DeleteItemResponseMessage.  SUCCESS and FAILURE are strings added
+to the success and failure messages to the user.  FORMS are what
+to do, starting from point being in the calendar entry being
+operated on."
+  `(let ((response-code (exco-extract-value '(ResponseMessages
+                                             ,response-type
+                                             ResponseCode)
+                                           ,response)))
+       (if (equal response-code "NoError")
+          (progn ,@forms (message "excorporate-org: Successfully %s" ,success))
+        (message "excorporate-org: Failed to %s: %S" ,failure ,response))))
+
+(defun exco-org--remove-element ()
+  "Remove the element at point."
+  (with-current-buffer (get-buffer-create excorporate-org-buffer-name)
+    (save-excursion
+      (org-back-to-heading)
+      (let* ((inhibit-read-only t)
+            (element (org-element-at-point))
+            (begin (org-element-property :begin element))
+            (end (org-element-property :end element)))
+       (delete-region begin end)))))
 (defun exco-org-cancel-meeting ()
   "Cancel the meeting at point, prompting for a cancellation message."
   (interactive)
@@ -85,8 +117,7 @@
     (error (concat "This looks like an appointment,"
                   " try `exco-org-delete-appointment' instead.")))
   (let ((identifier (exco-org--connection-identifier-at-point))
-       (item-identifier
-        (org-entry-get (car (org-get-property-block)) "Identifier")))
+       (item-identifier (exco-org--item-identifier-at-point)))
     ;; Make sure the meeting owner matches the connection owner before
     ;; attempting to cancel the meeting.
     (unless (exco-org--organizer-matches-connection)
@@ -94,29 +125,13 @@
                     " meetings for which you are the organizer")))
     (when item-identifier
       (exco-calendar-item-meeting-cancel
-       identifier
-       (car (read-from-string item-identifier))
+       identifier item-identifier
        (read-from-minibuffer "Cancellation message: ")
        (lambda (identifier response)
-        (let ((response-code
-               (exco-extract-value '(ResponseMessages
-                                     CreateItemResponseMessage
-                                     ResponseCode)
-                                   response)))
-          (if (equal response-code "NoError")
-              (with-current-buffer (get-buffer-create
-                                    excorporate-org-buffer-name)
-                (save-excursion
-                  (org-back-to-heading)
-                  (let* ((inhibit-read-only t)
-                         (element (org-element-at-point))
-                         (begin (org-element-property :begin element))
-                         (end (org-element-property :end element)))
-                    (kill-region begin end)
-                    (message
-                     "excorporate-org: Successfully cancelled meeting"))))
-            (message "excorporate-org: Failed to cancel meeting: %S"
-                     response-code))))))))
+        (exco-org--handle-response
+         response CreateItemResponseMessage
+         "cancelled meeting" "cancel meeting"
+         (exco-org--remove-element)))))))
 
 (defun exco-org-delete-appointment ()
   "Delete the appointment at point."
@@ -124,32 +139,15 @@
   (when (exco-org--is-meeting)
     (error "This looks like a meeting, try `exco-org-cancel-meeting' instead"))
   (let ((identifier (exco-org--connection-identifier-at-point))
-       (item-identifier
-        (org-entry-get (car (org-get-property-block)) "Identifier")))
+       (item-identifier (exco-org--item-identifier-at-point)))
     (when item-identifier
       (exco-calendar-item-appointment-delete
-       identifier
-       (car (read-from-string item-identifier))
+       identifier item-identifier
        (lambda (identifier response)
-        (let ((response-code
-               (exco-extract-value '(ResponseMessages
-                                     DeleteItemResponseMessage
-                                     ResponseCode)
-                                   response)))
-          (if (equal response-code "NoError")
-              (with-current-buffer (get-buffer-create
-                                    excorporate-org-buffer-name)
-                (save-excursion
-                  (org-back-to-heading)
-                  (let* ((inhibit-read-only t)
-                         (element (org-element-at-point))
-                         (begin (org-element-property :begin element))
-                         (end (org-element-property :end element)))
-                    (kill-region begin end)
-                    (message
-                     "excorporate-org: Successfully deleted appointment"))))
-            (message "excorporate-org: Failed to delete appointment: %S"
-                     response-code))))))))
+        (exco-org--handle-response
+         response DeleteItemResponseMessage
+         "deleted appointment" "delete appointment"
+         (exco-org--remove-element)))))))
 
 (defun exco-org-initialize-buffer ()
   "Add initial text to the destination buffer."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]