[Martin Simmons] Re: Selecting a portion of text in editor pane

"wojciech.pasieka (as wojciech dot pasieka at ai dot pressiton dot com)" <[email protected]> (Adrian W. Pasieka)
Newsgroups gmane.lisp.lispworks.general
Message-ID <[email protected]>
If Michal has time, perhaps he could review the LLM’s answers to his question.
I looked into it, since the question was short and Martin’s answer was very clear.

This might help us figure out, how to train one of the LLMs on Lisp’s vast resources.
My experience with Lisp and LLMs has been mixed, from 'strokes of genius' to complete chaos.


Lisp’s flexibility has created many variations of the same functions, making it a jungle now.
A subtle change can vastly improve performance, yet optimized libraries often do not exist.

The training process would therefore require establishing a kind of "training standard."
I’m just thinking out loud about how we might approach it in the most efficient way.


Kind Regards,
Adrian W. Pasieka


GEMINI-2.5-PRO
-------------------------------
You need to use lower-level editor functions to programmatically manipulate the buffer's point (the cursor). The editor:*command* functions are high-level commands designed for interactive use, like key bindings, and don't behave as expected when called this way.

The correct way to move the cursor is to get the buffer's point object and then use editor:move-point to change its position.

The Correct Approach

To move the cursor (the point) in an editor pane's buffer, you should follow these steps:
    Get the buffer object from the editor pane using capi:editor-pane-buffer.
    Get the point object from that buffer using editor:buffer-point.
    Move the point using editor:move-point, providing the point object and an integer offset.

Working Example
Here is the corrected version of your code. It creates the editor pane and then successfully moves the cursor one character forward.
Lisp

;; Create and display the editor pane
(setf editor (capi:contain (make-instance 'capi:editor-pane :text "Text")))

;; Programmatically move the cursor in the pane's process
(capi:apply-in-pane-process
 editor
 (lambda ()
   ;; Get the buffer from the CAPI pane
   (let* ((buffer (capi:editor-pane-buffer editor))
          ;; Get the point (cursor) from the buffer
          (point (editor:buffer-point buffer)))
     ;; Move the point 1 character forward
     (editor:move-point point 1))))

After running this code, you will see the cursor positioned between the 'T' and 'e' in "Text", as intended.
-----------------------------------------------------------------------------------------------------


GPT-5
-----------------------------------------------------------------------------------------
The code doesn't work as expected because directly calling an editor command function like
editor:forward-character-command requires the full dynamic context (including the current window,
not just the buffer) to be properly established for it to affect the editor pane. The
editor:use-buffer macro only binds the current buffer—it doesn't handle the window context or other
necessary setup for programmatic command execution in a CAPI environment.

To fix this, use capi:call-editor (which handles the context automatically) to invoke the command by
its string name. Since you want to move forward by 1 character (the default for this command when no
prefix is explicitly passed), the corrected code is:

(setf editor (capi:contain (make-instance 'capi:editor-pane :text "Text")))

(capi:apply-in-pane-process
 editor
 (lambda ()
   (capi:call-editor editor "Forward Character")))

(capi:apply-in-pane-process
 editor
 (lambda ()
   (editor:process-character '(editor:forward-character-command 5)
                             (editor:editor-window editor))))
---------------------------------------------------------------


GROK
-----------------------------------------------------------------------
You ran into a subtle but important distinction in LispWorks Editor API:

The editor:forward-character-command is an interactive command, meant to be bound to keys inside the editor. It does not directly move the point when called from Lisp code. Instead, you should use the editor buffer/point API, not the command layer.

In your example, you want to move the cursor (editor point). Here is how to do it correctly:

(setf editor (capi:contain
              (make-instance 'capi:editor-pane :text "Text")))

(capi:apply-in-pane-process
 editor
 (lambda ()
   (editor:use-buffer (capi:editor-pane-buffer editor)
     (let ((point (editor:buffer-point (capi:editor-pane-buffer editor))))
       ;; Move forward one character
       (editor:move-point point 1)))))

Key points:
editor:buffer-point → returns the "point object" associated with the buffer (i.e. the cursor position).
editor:move-point → moves the point by the given number of characters.

editor:forward-character-command is intended to be called by the editor command loop (like when pressing an arrow key), not directly in programmatic manipulation.
If you want to move by other units (words, lines, etc.), LispWorks provides functions like:

editor:forward-word
editor:line-start
editor:line-end

But in programmatic control, always use the point functions (move-point, set-point, etc.) rather than the interactive commands.
------------------------------------------------------------------------------------------------------------------
(unnamed) (message/rfc822, 9.9 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.