Function: ghostel--line-mode-restore

ghostel--line-mode-restore is a natively compiled function defined in ghostel-line-mode.el.

Signature

(ghostel--line-mode-restore SNAPSHOT)

Documentation

Re-insert SNAPSHOT'd input after the new prompt and restore point/mark.

Returns non-nil on success. Returns nil when the prompt cannot be located (shell integration dropped out, or the prompt scrolled off because of async output during composition); callers fall back to forwarding the pending input raw.

Trims pure-whitespace tails past the input but preserves any non-blank content the renderer wrote past the prompt row (e.g. a status bar). Point is restored from the snapshot's offset when it was inside the input region, and left untouched otherwise.

Source Code

;; Defined in /nix/store/1252krff0gb2kisjcpp45m50cpdskfrl-emacs-packages-deps/share/emacs/site-lisp/ghostel-line-mode.el
(defun ghostel--line-mode-restore (snapshot)
  "Re-insert SNAPSHOT'd input after the new prompt and restore point/mark.
Returns non-nil on success.  Returns nil when the prompt cannot be
located (shell integration dropped out, or the prompt scrolled off
because of async output during composition); callers fall back to
forwarding the pending input raw.

Trims pure-whitespace tails past the input but preserves any
non-blank content the renderer wrote past the prompt row (e.g. a
status bar).  Point is restored from the snapshot's offset when it
was inside the input region, and left untouched otherwise."
  (when snapshot
    (let ((prompt-end (ghostel-input-start-point)))
      (when prompt-end
        ;; Keep the re-insert out of the undo list.
        (let ((inhibit-read-only t)
              (buffer-undo-list t)
              (input (plist-get snapshot :input)))
          (ghostel--line-mode-trim-trailing-blank prompt-end)
          (when (markerp ghostel--line-input-start)
            (set-marker ghostel--line-input-start nil))
          (when (markerp ghostel--line-input-end)
            (set-marker ghostel--line-input-end nil))
          (setq ghostel--line-input-start (copy-marker prompt-end nil))
          (set-marker-insertion-type ghostel--line-input-start nil)
          (setq ghostel--line-input-end (copy-marker prompt-end t))
          (set-marker-insertion-type ghostel--line-input-end t)
          (when (and input (> (length input) 0))
            (save-excursion
              (goto-char (marker-position ghostel--line-input-start))
              ;; Insertion advances `ghostel--line-input-end' (insertion
              ;; type t) so end-marker tracks the tail of the input.
              (insert input)))
          (let ((start-pos (marker-position ghostel--line-input-start))
                (end-pos (marker-position ghostel--line-input-end)))
            (ghostel--line-mode-apply-readonly start-pos)
            ;; Mark the line-mode input region with `ghostel-input' so
            ;; `ghostel--skip-match-p' skips it on the cursor's
            ;; line — without this, a path the user typed locally
            ;; (e.g. `cd src/main.rs') would get linkified and RET
            ;; would open the file instead of running
            ;; `ghostel-line-mode-send'.
            (when (< start-pos end-pos)
              (put-text-property start-pos end-pos 'ghostel-input t))
            (let ((po (plist-get snapshot :point-offset))
                  (mo (plist-get snapshot :mark-offset)))
              (when po
                (goto-char (+ start-pos po)))
              (when mo
                (set-mark (+ start-pos mo))))))
        ;; Re-arm an empty history at the input's new location; the old
        ;; positions are stale now the redraw has moved it.
        (setq buffer-undo-list nil)
        t))))