如何从请求中提供交互功能的完成?



我想为一个emacs交互函数提供补全功能,该函数的内容将基于HTTP请求的内容。这里,TODO解释了这一切。

(defun sensei-record-flow (flow-type)
"Interactive function to record change in flow."

(interactive (list (completing-read
"Flow: "
;; TODO need to get the completion from a variable filled with user's
;; flow types
'(("Foo" Foo) ("Bar" Bar) ("Baz" Baz))
nil t)))
(let ((directory (projectile-project-root)))
(setq sensei-cur-directory directory)
(sensei-send-event-flow directory flow-type))
)

我该怎么做呢?request.el提供了一个:completion关键块,直到完成一个请求,但我不清楚如何做到这一点?我认为我需要的是使interactive函数继续调用senseiilist-flows,但我不知道如何在elisp中做到这一点。

编辑:这是sensei-list-flows基于request.el的代码

(defun sensei-list-flows (on-success)
"List available flow types for the current user.
ON-SUCCESS is a function that's called upon successful completion of the call
and is passed a list of symbols listing user-defined flow names."
(let* ((config (sensei-read-config))
(auth-token (cdr (assoc 'authToken config)))
(username (cdr (assoc 'configUser config)))
(server-uri (cdr (assoc 'serverUri config))))
(request (concat server-uri "api/users/" username)
:headers `(("Content-Type" . "application/json")
("X-API-Version" . "0.38.0")
("Authorization" . ,(concat "Bearer " auth-token)))
:parser 'json-read
:error  (cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
(message "Got error: %S" error-thrown)))
:success  (cl-function (lambda (&key data &allow-other-keys)
(funcall on-success
(map 'list 'car (cdr (assoc 'userFlowTypes data)))))))))

我对request.el一无所知,你没有在引用的代码中包含任何对list-flows的调用,因此无法评论;但是OOTB你会使用C-hfurl-retrieve-synchronously来通过http获取一些东西。

在结果缓冲区中,url-http-end-of-headers变量特别值得注意(+1表示到达内容的开始);一般来说,您可能希望检查该缓冲区中的url-http-*变量。

我能够使用url-insert-file-contents实现我想要的,这与url-retrieve-synchronously相似:

(defun sensei-list-flows ()
"List available flow types for the current user."
(let* ((config (sensei-read-config))
(auth-token (cdr (assoc 'authToken config)))
(username (cdr (assoc 'configUser config)))
(server-uri (cdr (assoc 'serverUri config)))
(url-request-extra-headers `(("Content-Type" . "application/json")
("X-API-Version" . "0.38.0")
("Authorization" . ,(concat "Bearer " auth-token)))))
(with-temp-buffer
(url-insert-file-contents (concat server-uri "api/users/" username))
(let ((flows (cdr (assoc 'userFlowTypes (json-parse-buffer :object-type 'alist)))))
(map 'list 'car flows)))))

那么交互式完成是微不足道的:

(defun sensei-record-flow (flow-type)
"Interactive function to record change in flow."
(interactive (list (completing-read
"Flow: "
(sensei-list-flows)
nil t)))
(let ((directory (projectile-project-root)))
(setq sensei-cur-directory directory)
(sensei-send-event-flow directory flow-type))
)

最新更新