Skip to content

Instantly share code, notes, and snippets.

@jmercouris
Created November 14, 2018 12:46
Show Gist options
  • Select an option

  • Save jmercouris/8ad669f06ffc6fd0580d3f2a5aa60b71 to your computer and use it in GitHub Desktop.

Select an option

Save jmercouris/8ad669f06ffc6fd0580d3f2a5aa60b71 to your computer and use it in GitHub Desktop.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; READ-FROM-MINIBUFFER - this is a non-blocking function that
;; invokes callback-function upon completion with the minibuffer input
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmethod read-from-minibuffer (callback-function (minibuffer
minibuffer) &key ...) ...)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; WITH RESULT MACRO - this allows for continuation passing style
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro with-result ((symbol async-form) &body body)
`(,(first async-form)
(lambda (,symbol) ,@body)
,@(rest async-form)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This allows for continuation passing style, non blocking input
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-command bookmark-url ()
"Allow the user to bookmark a URL via minibuffer input."
(with-result (url (read-from-minibuffer *minibuffer*
:input-prompt "Bookmark URL:"))
(%bookmark-url url)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Macro Expansion Example From Above
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(macroexpand-1 '(with-result (url (read-from-minibuffer *minibuffer*)) (%bookmark-url url)))
(READ-FROM-MINIBUFFER (LAMBDA (URL) (%BOOKMARK-URL URL)) *MINIBUFFER*)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; However, this is a problem, I have another macro called
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro define-parenstatic (script-name &rest script-body)
`(progn
(defparameter ,script-name
(ps:ps ,@script-body))
(defun ,script-name (&optional (buffer (active-buffer *interface*)))
(buffer-execute-javascript *interface* buffer ,script-name))))
I would like to adapt this macro somehow to work still with WITH-RESULT
As you can see, this macro simply generates a defun so that I can write
things like this;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Example Defparen
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-parenstatic scroll-to-top
(ps:chain window (scroll-by 0 (- (ps:chain document body scroll-height)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Expansion of Defparen
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(PROGN
(DEFPARAMETER SCROLL-TO-TOP
(PARENSCRIPT:PS
(PARENSCRIPT:CHAIN WINDOW
(SCROLL-BY 0 (- (PARENSCRIPT:CHAIN DOCUMENT BODY SCROLL-HEIGHT))))))
(DEFUN SCROLL-TO-TOP
(&KEY (CALLBACK NIL) (BUFFER (ACTIVE-BUFFER *INTERFACE*)))
(BUFFER-EXECUTE-JAVASCRIPT *INTERFACE* BUFFER SCROLL-TO-TOP CALLBACK)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;So, what is the big problem? BUFFER-EXECUTE-JAVASCRIPT has an optional
;;last argument for the callback, rather than a required first argument as
;;it is for READ-FROM-MINIBUFFER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment