Skip to content

Instantly share code, notes, and snippets.

@ashok-khanna
Created February 12, 2022 20:46
Show Gist options
  • Save ashok-khanna/2f3b2d21746e75d530d461936f11acd6 to your computer and use it in GitHub Desktop.
Save ashok-khanna/2f3b2d21746e75d530d461936f11acd6 to your computer and use it in GitHub Desktop.
CAPI Editor Example
;;;;****************************************************************************
;;; Editor Example
;;;;****************************************************************************
(in-package "CL-USER")
(defvar *editor-text*
";;----------------------------------------------------------------------------
;;
;;
;; This is a minimal example of using the EDITOR-PANE class.
;; To try it, compile and load this file and then execute:
;;
;; (CL-USER::TEST-EDITOR-PANE)
;;
;;----------------------------------------------------------------------------
")
(capi:define-interface editor-pane-test ()
()
(:panes
(editor-pane
capi:editor-pane
:flag 'minimal-example
:text *editor-text*
:buffer-name :TEMP
:echo-area-pane echo-area
:visible-min-width '(character 80)
:visible-min-height '(character 15))
(buttons
capi:push-button-panel
:items '("Beginning Of Buffer" ; These strings are names of editor commands
"End Of Buffer"
"Kill Line"
"Undo")
:callback-type :data
:selection-callback #'(lambda (command)
(capi:call-editor editor-pane command)))
(echo-area capi:echo-area-pane :max-height t))
(:default-initargs
:title "Editor Pane Test"))
;; http://www.lispworks.com/documentation/lw80/capi-m/capi-choice-ug-10.htm#CAPI)choice-ug-64732
;; http://www.lispworks.com/documentation/lw80/capi-m/capi-common-elements-ug-5.htm
(defmethod capi:pane-popup-menu-items
((self capi:editor-pane) (interface editor-pane-test))
(list*
(make-instance 'capi:menu-item
:title "Item for My Editor Menu."
:selection-callback 'my-callback)
(make-instance 'capi:menu-item
:title "The Motto"
:selection-callback 'test
:callback-type :data-interface)
(call-next-method)))
(defun my-callback (pane)
(let ((buffer (capi:editor-pane-buffer pane)))
(let ((point (editor:buffers-end buffer)))
(editor:insert-string point "foo"))
(use-buffer buffer
(let ((current-point (copy-point (current-point))))
(put-text-property current-point (editor::move-buffer-point-to-offset (current-buffer)
(+ (editor::point-to-offset (current-point)) 4))
'face *sc-keyword-face*))
(capi:display-message "Current Data ~S." (editor:current-point)))))
(defun test-editor-pane (&optional read-only)
(let ((ept (make-instance 'editor-pane-test)))
(when read-only
(with-slots (editor-pane) ept
(setf (capi:simple-pane-enabled editor-pane) :read-only)))
(capi:display ept)))
(use-package :editor)
(defun test (&rest args)
(let ((current-point (copy-point (current-point))))
(put-text-property current-point (editor::move-buffer-point-to-offset (current-buffer)
(+ (editor::point-to-offset (current-point)) 4))
'face *sc-keyword-face*)))
;; Not work
(defun test2 ()
(let ((current-point (copy-point (current-point))))
(editor::merge-text-property-list current-point (editor::move-buffer-point-to-offset (current-buffer)
(+ (editor::point-to-offset (current-point)) 2))
'face *my-bold-face*)))
;; asfsdafasdf
;; asdfasdfdasfadsf
;; (move-buffer-point-to-offset (current-buffer) n) (point-to-offset (current-point))
(defvar *sc-keyword-face*
(make-face 'sc-keyword-face
:foreground :purple
:if-exists :overwrite))
(defvar *my-bold-face*
(make-face 'my-bold-face
:bold-p :t
:if-exists :overwrite))
;; need to disable font lock mode for the above to work
;; need to figure out how to merge properties - see above
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment