Skip to content

Commit

Permalink
Merge pull request #3413 from atlas-engineer/fix-prompt-buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
aadcg committed Jun 14, 2024
2 parents 6e6551e + 032f080 commit a0e09b7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 58 deletions.
12 changes: 6 additions & 6 deletions source/mode/prompt-buffer.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -495,14 +495,14 @@ Only available if `prompter:enable-marks-p' is non-nil."

(define-command-prompt move-start-of-input (prompt-buffer)
"Move to the beginning of PROMPT-BUFFER input."
(ps-eval :buffer prompt-buffer
(ps-eval :async t :buffer prompt-buffer
(let ((input (nyxt/ps:qs document "#input")))
(setf (ps:@ input selection-start) 0
(ps:@ input selection-end) 0))))

(define-command-prompt move-end-of-input (prompt-buffer)
"Move to the end of PROMPT-BUFFER input."
(ps-eval :buffer prompt-buffer
(ps-eval :async t :buffer prompt-buffer
(let ((input (nyxt/ps:qs document "#input")))
(setf (ps:@ input selection-start) (ps:@ input value length)
(ps:@ input selection-end) (ps:@ input value length)))))
Expand All @@ -518,25 +518,25 @@ Only available if `prompter:enable-marks-p' is non-nil."
&key (scroll-distance
(scroll-distance (current-buffer))))
"Scroll up the buffer behind the prompt."
(ps-eval :buffer (current-buffer)
(ps-eval :async t :buffer (current-buffer)
(ps:chain window (scroll-by 0 (ps:lisp (- scroll-distance))))))

(define-command-prompt scroll-other-buffer-down (prompt-buffer
&key (scroll-distance
(scroll-distance (current-buffer))))
"Scroll down the buffer behind the prompt."
(ps-eval :buffer (current-buffer)
(ps-eval :async t :buffer (current-buffer)
(ps:chain window (scroll-by 0 (ps:lisp scroll-distance)))))

(define-command-prompt scroll-page-up-other-buffer (prompt-buffer)
"Scroll up the buffer behind the prompt by one page."
(ps-eval :buffer (current-buffer)
(ps-eval :async t :buffer (current-buffer)
(ps:chain window (scroll-by 0 (- (* (ps:lisp (page-scroll-ratio (current-buffer)))
(ps:@ window inner-height)))))))

(define-command-prompt scroll-page-down-other-buffer (prompt-buffer)
"Scroll down the buffer behind the prompt by one page."
(ps-eval :buffer (current-buffer)
(ps-eval :async t :buffer (current-buffer)
(ps:chain window (scroll-by 0 (* (ps:lisp (page-scroll-ratio (current-buffer)))
(ps:@ window inner-height))))))

Expand Down
81 changes: 34 additions & 47 deletions source/prompt-buffer.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ The options are:
- `:default', which sets it to a third of the window's height;
- `:fit-to-prompt', which shrinks the height to fit the input area;
- an integer, which corresponds to the height in pixels.")
(prompter:history (prompt-buffer-generic-history *browser*)
;; No need to export or define the accessor since this is
;; an override of the prompter slot.
:accessor nil
:export nil
:documentation "By default the prompter library creates a
new history for each new prompt buffer. Here we set the history to be shared globally.")
;; TODO: Need a changed-callback? Probably not, see `search-buffer'. But
;; can we run the postprocessor without running the filter?
(prompter:history
(prompt-buffer-generic-history *browser*)
;; Both set to nil since it overrides the default value.
:accessor nil
:export nil
:documentation "Override `prompter:history' to share input history globally.")
(invisible-input-p
nil
:documentation "Whether to replace input by a placeholder character. This
Expand All @@ -43,10 +40,6 @@ brackets.")
:export nil
:documentation "Maximum number of total suggestions that were listed at
some point.")
;; TODO: Need max-lines?
;; (max-lines 10
;; :documentation "Max number of suggestion lines to show.
;; You will want edit this to match the changes done to `style'.")
(hide-single-source-header-p
nil
:documentation "Hide source header when there is only one.")
Expand Down Expand Up @@ -279,7 +272,7 @@ See `prompt' for how to invoke prompts.")
(round (/ (ffi-height (window prompt-buffer))
3)))
(:fit-to-prompt
(ps-eval :buffer prompt-buffer
(ps-eval :async t :buffer prompt-buffer
(+ (ps:chain (nyxt/ps:qs document "#prompt-area") offset-height)
;; Buffer whitespace between the prompt buffer's input area and
;; the status buffer. Not clear how to the derive the value
Expand Down Expand Up @@ -529,7 +522,7 @@ This does not redraw the whole prompt buffer, use `prompt-render' for that."
`(funcall (sym:resolve-symbol :toggle-attributes-display :command)
:source ,source))))
(:raw (render-attributes source prompt-buffer))))))
(ps-eval :buffer prompt-buffer
(ps-eval :async t :buffer prompt-buffer
(setf (ps:@ (nyxt/ps:qs document "#suggestions") |innerHTML|)
(ps:lisp
(sera:string-join (loop for i from current-source-index to last-source-index
Expand All @@ -539,38 +532,32 @@ This does not redraw the whole prompt buffer, use `prompt-render' for that."
+newline+)))))
(prompt-render-prompt prompt-buffer)))

(defun erase-document (prompt-buffer)
(ps-eval :async t :buffer prompt-buffer
(ps:chain document (open))
(ps:chain document (close))))

(defun prompt-render-skeleton (prompt-buffer)
(erase-document prompt-buffer)
(html-set (spinneret:with-html-string
(:head
(:nstyle (style prompt-buffer)))
(:body
(:div :id "prompt-area"
(:div :id "prompt" (prompter:prompt prompt-buffer))
(:div :id "prompt-extra" :class "arrow-right" "[?/?]")
(:div :id "prompt-input"
(:input :type (if (invisible-input-p prompt-buffer)
"password"
"text")
:id "input"
:value (prompter:input prompt-buffer)))
(:div :id "prompt-modes" :class "arrow-left" "")
(:div :id "close-button" :class "arrow-left"
(:nbutton
:text "×"
:title "Close prompt"
:buffer prompt-buffer
'(funcall (sym:resolve-symbol :quit-prompt-buffer :command)))))
(:div :id "suggestions"
:style (if (invisible-input-p prompt-buffer)
"visibility:hidden;"
"visibility:visible;"))))
prompt-buffer))
(html-write (spinneret:with-html-string
(:head (:nstyle (style prompt-buffer)))
(:body
(:div
:id "prompt-area"
(:div :id "prompt" (prompter:prompt prompt-buffer))
(:div :id "prompt-extra" :class "arrow-right" "[?/?]")
(:div :id "prompt-input"
(:input :type (if (invisible-input-p prompt-buffer)
"password"
"text")
:id "input"
:value (prompter:input prompt-buffer)))
(:div :id "prompt-modes" :class "arrow-left" "")
(:div :id "close-button" :class "arrow-left"
(:nbutton
:text "×"
:title "Close prompt"
:buffer prompt-buffer
'(funcall (sym:resolve-symbol :quit-prompt-buffer :command)))))
(:div :id "suggestions"
:style (if (invisible-input-p prompt-buffer)
"visibility:hidden;"
"visibility:visible;"))))
prompt-buffer))

(defun prompt-render-focus (prompt-buffer)
(ps-eval :async t :buffer prompt-buffer
Expand Down Expand Up @@ -617,7 +604,7 @@ If you want to set the input, see `set-prompt-buffer-input'."
(defun set-prompt-buffer-input (input &optional (prompt-buffer (current-prompt-buffer)))
"Set HTML INPUT in PROMPT-BUFFER.
See `update-prompt-input' to update the changes visually."
(ps-eval :buffer prompt-buffer
(ps-eval :async t :buffer prompt-buffer
(setf (ps:@ (nyxt/ps:qs document "#input") value)
(ps:lisp input)))
(update-prompt-input prompt-buffer input))
Expand Down
2 changes: 1 addition & 1 deletion source/renderer-script.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ If `setf'-d to a list of two values -- set Y to `first' and X to `second' elemen
Overwrites the whole HTML document (head and body elements included)."
;; Don't use document.write().
;; See https://developer.mozilla.org/en-US/docs/Web/API/Document/write.
(ps-eval :buffer buffer
(ps-eval :async t :buffer buffer
(setf (ps:chain document (get-elements-by-tag-name "html") 0 |innerHTML|)
(ps:lisp html-document))))

Expand Down
7 changes: 3 additions & 4 deletions source/renderer/electron.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,12 @@
&optional world-name)
;; TODO world-name is used in user-script mode.
(declare (ignore world-name))
(electron:execute-javascript (electron:web-contents buffer) javascript))
(electron:execute-javascript-synchronous (electron:web-contents buffer) javascript))

(defmethod ffi-buffer-evaluate-javascript-async ((buffer electron-buffer) javascript
&optional world-name)
(declare (ignore world-name))
;; Temporary fallback.
(ffi-buffer-evaluate-javascript buffer javascript))
(electron:execute-javascript (electron:web-contents buffer) javascript))

(defmethod ffi-inspector-show ((buffer electron-buffer))
(electron:open-dev-tools buffer))
Expand Down Expand Up @@ -208,7 +207,7 @@
(case height
(0
(ffi-buffer-delete prompt-buffer)
(electron:focus (nyxt::active-buffer window)))
(when (current-window) (electron:focus (nyxt::active-buffer window))))
(t
(electron:on window "resize"
(format nil "~a.setBounds({x: 0,
Expand Down

0 comments on commit a0e09b7

Please sign in to comment.