branch: elpa/fedi
commit 11322be69e5fd3c2161076e050898e32cba01ff4
Merge: 460ec9874b8 06cb312ef4a
Author: marty hiatt <[email protected]>
Commit: marty hiatt <[email protected]>

    Merge branch 'dev'
---
 fedi-http.el | 38 ++++++++++++++++++++++---------------
 fedi-post.el | 46 +++++++++++++++++++++++++++-----------------
 fedi.el      | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 108 insertions(+), 38 deletions(-)

diff --git a/fedi-http.el b/fedi-http.el
index 5e0d6ef1c0d..ca3e5526e9c 100644
--- a/fedi-http.el
+++ b/fedi-http.el
@@ -81,13 +81,12 @@ A random one is picked at package initialization.")
 
 (defun fedi-http--render-html-err (string)
   "Render STRING as HTML in a temp buffer.
-STRING should be HTML for a 404 errror."
+STRING should be HTML for a 404 or 429 etc. errror."
   (with-temp-buffer
     (insert string)
     (shr-render-buffer (current-buffer))
-    (view-mode))) ; for 'q' to kill buffer and window
-;; FIXME: this is awful, it pops up also:
-;; (error ""))) ; stop subsequent processing
+    (view-mode) ;; for 'q' to kill buffer and window
+    (user-error "HTML response")))
 
 (defun fedi-http--read-file-as-string (filename)
   "Read a file FILENAME as a string. Used to generate image preview."
@@ -149,7 +148,7 @@ SILENT means don't message.
 VECTOR means return json arrays as vectors."
   (car (fedi-http--get-response url params :no-headers silent vector)))
 
-(defun fedi-http--post (url &optional params headers json)
+(defun fedi-http--post (url &optional params headers json silent)
   "POST synchronously to URL, optionally with PARAMS and HEADERS.
 JSON means we are posting a JSON payload, so we add headers and
 json-string PARAMS."
@@ -162,22 +161,31 @@ json-string PARAMS."
               (fedi-http--build-params-string params))))
          ;; TODO: perhaps leave these headers to the package now that
          ;; `fedi-request' takes header args?
-         (headers (when json
-                    (append headers
-                            '(("Content-Type" . "application/json")
-                              ("Accept" . "application/json")))))
+         (headers (if json
+                      (append headers
+                              '(("Content-Type" . "application/json")
+                                ("Accept" . "application/json")))
+                    '(("Content-Type" . "application/x-www-form-urlencoded"))))
          (url-request-extra-headers
-          (append url-request-extra-headers ; set in macro
-                  headers)))
+          (if (not url-request-extra-headers)
+              headers ;; no need to add anything
+            (append headers  url-request-extra-headers))))
     (with-temp-buffer
-      (fedi-http--url-retrieve-synchronously url))))
+      (fedi-http--url-retrieve-synchronously url silent))))
 
-(defun fedi-http--delete (url &optional params)
+(defun fedi-http--delete (url &optional params json)
   "Make DELETE request to URL.
-PARAMS is an alist of any extra parameters to send with the request."
+PARAMS is an alist of any extra parameters to send with the request.
+If JSON, encode PARAMS as JSON."
   ;; url-request-data only works with POST requests?
   (let ((url-request-method "DELETE")
-        (url (fedi-http--concat-params-to-url url params)))
+        (url-request-data
+         (when params
+           (if json
+               (encode-coding-string
+                (json-encode params) 'utf-8)
+             (fedi-http--build-params-string params)))))
+    ;; (url (fedi-http--concat-params-to-url url params)))
     (with-temp-buffer
       (fedi-http--url-retrieve-synchronously url))))
 
diff --git a/fedi-post.el b/fedi-post.el
index 1a55724c6d9..31969ab0340 100644
--- a/fedi-post.el
+++ b/fedi-post.el
@@ -427,7 +427,7 @@ to update status fields."
            for name = (alist-get 'name item)
            for prop = (alist-get 'prop item)
            concat (propertize (capitalize name)
-                              prop t)))
+                              (if (consp prop) (car prop) prop) t)))
 
 (defun fedi-post--display-docs-and-status-fields (&optional mode prefix
                                                             fields type edit)
@@ -479,23 +479,31 @@ FIELDS is a list of alists of fields to add, using 
`fedi-post--concat-fields'."
 
 (defun fedi-post--update-status-field (item)
   "ITEM."
+  ;; FIXME: remove these evals
   (let-alist item
     (let ((region (fedi--find-property-range .prop (point-min))))
-      (add-text-properties (car region) (cdr region)
-                           (list 'display
-                                 (if (eval .item-var)
-                                     (format
-                                      (concat
-                                       (if .no-label
-                                           ""
-                                         (concat (capitalize .name)
-                                                 ": "))
-                                       (propertize "%s"
-                                                   'face .face)
-                                       " ⋅ ")
-                                      (eval .item-var))
-                                   "")
-                                 'face 'fedi-post-docs-face)))))
+      (add-text-properties
+       (car region) (cdr region)
+       (list 'display
+             (if (eval .item-var)
+                 (format
+                  (concat
+                   (if .no-label
+                       ""
+                     (concat (capitalize .name)
+                             ": "))
+                   (propertize "%s"
+                               'face .face)
+                   " ⋅ ")
+                  (if (listp (eval .item-var))
+                      (mapconcat (lambda (x)
+                                   ;; for alists, concat the cars:
+                                   (if (consp x) (car x) x))
+                                 (eval .item-var) " ")
+                    (eval .item-var)))
+               ;; .item-var))
+               "")
+             'face 'fedi-post-docs-face)))))
 
 (defun fedi-post--update-status-fields (&rest _args)
   "Update the status fields in the header based on the current state."
@@ -657,7 +665,11 @@ string, the other elements should be symbols."
         (declare-function company-mode-on "company")
         (set (make-local-variable 'company-backends)
              (add-to-list 'company-backends 'company-capf))
-        (company-mode-on)))
+        (company-mode-on))
+      ;; corfu
+      (when (require 'corfu nil :no-error)
+        (setq-local corfu-auto t)
+        (corfu--on)))
     ;; after-change:
     (make-local-variable 'after-change-functions)
     ;; (cl-pushnew #'fedi-post--save-post-text after-change-functions)
diff --git a/fedi.el b/fedi.el
index ee24ce0cf89..e60f4b77302 100644
--- a/fedi.el
+++ b/fedi.el
@@ -157,6 +157,52 @@ If COERCE, make numbers strings."
 ;;                          (symbol-name arg))))
 ;;     `(when ,arg (cons ,str "true"))))
 
+(defmacro fedi-opt-params (&rest params)
+  "From PARAMS, a list of symbols, create an alist of parameters.
+Used to conditionally create fields in the parameters alist.
+
+A param can also be an expression, in which case the car should be the
+symbol name of the param as used locally. The cdr should be a plist
+that may contain the fields :boolean, :alias, :when and :list.
+:boolean should be a string, either \"true\" or \"false\".
+:alias should be the name of the parameter as it is on the server.
+:when should be a condition clause to test against rather than the mere
+value of the parameter symbol
+:list should be a list of values for the parameter.
+
+For example:
+
+\(fedi-opt-params (query :alias \"q\") (topic :boolean \"true\")
+                  uid (mode :when (member mode fj-search-modes))
+                  (include-desc :alias \"includeDesc\"
+                                :boolean \"true\")
+                  (list :list (\"123\" \"abc\")
+                  order page limit)."
+  (declare (debug t))
+  `(append ,@(fedi--opt-params-whens params)))
+
+(defun fedi--opt-params-whens (params)
+  "Return a when clause for each of PARAMS, a list of symbols."
+  (cl-loop for x in params
+           collect (fedi--opt-param-expr x)))
+
+(defun fedi--opt-param-expr (param)
+  "For PARAM, return a when expression.
+It takes the form:
+\(when param '(\"param\" . param)).
+Param itself can also be an expression. See `fedi-opt-params' for
+details."
+  (if (consp param)
+      (let* ((name (car param))
+             (boolean (plist-get (cdr param) :boolean))
+             (alias (plist-get (cdr param) :alias))
+             (clause (plist-get (cdr param) :when))
+             (list (plist-get (cdr param) :list))
+             (value (or list boolean name))
+             (str (or alias (symbol-name name))))
+        `(when ,(or clause name)
+           `((,,str . ,,value))))
+    `(when ,param `((,(symbol-name ',param) . ,,param)))))
 
 ;;; BUFFER MACRO
 
@@ -214,10 +260,13 @@ Optionally start from POS."
 (defun fedi-next-tab-item (&optional previous prop)
   "Move to the next interesting item.
 This could be the next toot, link, or image; whichever comes first.
-Don't move if nothing else to move to is found, i.e. near the end of the 
buffer.
+Don't move if nothing else to move to is found, i.e. near the end of the
+buffer.
 This also skips tab items in invisible text, i.e. hidden spoiler text.
 PREVIOUS means move to previous item.
-PROP is the text property to search for."
+PROP is the text property to search for.
+Returns nil if nothing found, returns the value of point moved to if
+something found."
   (interactive)
   (let (next-range
         (search-pos (point)))
@@ -233,9 +282,10 @@ PROP is the text property to search for."
     (if (null next-range)
         (prog1 nil ;; return nil if nothing (so we can use in or clause)
           (message "Nothing else here."))
-      (goto-char (car next-range))
-      (if-let* ((hecho (fedi--property 'help-echo)))
-          (message "%s" hecho)))))
+      (prog1 ;; return point not nil if we moved:
+          (goto-char (car next-range))
+        (if-let* ((hecho (fedi--property 'help-echo)))
+            (message "%s" hecho))))))
 
 (defun fedi-previous-tab-item ()
   "Move to the previous interesting item.
@@ -310,7 +360,7 @@ NAME is not part of the symbol table, '?' is returned."
 (defun fedi-font-lock-comment (&rest strs)
   "Font lock comment face STRS."
   (propertize (mapconcat #'identity strs "")
-              'face font-lock-comment-face))
+              'face 'font-lock-comment-face))
 
 (defun fedi-thing-json ()
   "Get json of thing at point, comment, post, community or user."

Reply via email to