From 1890d288f6aed64bc0f9f3de7b13b1b7a496fb05 Mon Sep 17 00:00:00 2001 From: xeals Date: Thu, 22 Jun 2023 11:24:24 +1000 Subject: [PATCH] wip --- bookstack.el | 58 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/bookstack.el b/bookstack.el index a02a208..88eb5c2 100644 --- a/bookstack.el +++ b/bookstack.el @@ -61,9 +61,9 @@ "Retrieves the ENDPOINT from the current `bookstack-server' asynchronously, executing CALLBACK with the decoded result." (unless bookstack-token - (user-error "%s" "`bookstack-token' must be defined")) + (user-error "`bookstack-token' must be defined")) (unless bookstack-server - (user-error "%s" "`bookstack-server' must be defined")) + (user-error "`bookstack-server' must be defined")) (let ((url-request-extra-headers `(("Authorization" . ,(format "Token %s" bookstack-token)) ("Content-Type" . "application/json"))) @@ -102,7 +102,8 @@ PAGES should be a sequence of pages (as returned by `bookstack-pages')." (bookstack-page id (lambda (page) - (or page (error "Missing full page '%d'" id)) + (unless page + (error "Missing page '%d'" id)) (let-alist page (let ((buffer (get-buffer-create (format "%s.md" .name))) ;; fix line endings @@ -134,12 +135,63 @@ PAGES should be a sequence of pages (as returned by `bookstack-pages')." (bookstack--write-page bookstack--buffer-page-id (buffer-string)) (message "Wrote %s/.../page/%s" bookstack-server slug))) +(defun bookstack--delete-page (id callback &optional force-p) + (bookstack-page + id + (lambda (page) + (let ((page-name (alist-get 'name page))) + (unless (or force-p (y-or-n-p (format "Really delete %S?" page-name))) + (user-error "Aborted")) + (let ((url-request-method "DELETE")) + (bookstack--retrieve + (format "pages/%d" page) + (lambda (_) (apply callback page)))))))) + +(defun bookstack-delete-page (&optional id force-p) + "Delete PAGE and kill its buffers. + +If ID is not specified, defaults to the current buffer's page, if available. + +If FORCE-P, delete without confirmation." + (interactive + (list bookstack--buffer-page-id + current-prefix-arg)) + (unless (or id bookstack--buffer-page-id) + (user-error "Buffer is not visiting a BookStack page")) + (bookstack-pages + (lambda (pages) + (bookstack--delete-page (bookstack--read-page pages) force-p) + (lambda (page) + (let ((buf (current-buffer))) + (kill-buffer buf) + (message "Deleted %S" page)))))) + +(defun bookstack-delete-this-page (&optional id force-p) + "Delete PAGE and kill its buffers. + +If ID is not specified, defaults to the current buffer's page, if available. + +If FORCE-P, delete without confirmation." + (interactive + (list bookstack--buffer-page-id + current-prefix-arg)) + (unless (or id bookstack--buffer-page-id) + (user-error "Buffer is not visiting a BookStack page")) + (bookstack-pages + (lambda (pages) + (bookstack--delete-page (bookstack--read-page pages) force-p) + (lambda (page) + (let ((buf (current-buffer))) + (kill-buffer buf) + (message "Deleted %S" page)))))) + (define-minor-mode bookstack-mode "Minor mode for editing remote BookStack buffers." :init-value nil :lighter " BookStack" :keymap (let ((map (make-sparse-keymap))) (define-key map [remap save-buffer] #'bookstack-save-buffer) + (define-key map [remap delete-file] #'bookstack-delete-page) map)) ;;;###autoload