Skip to content

Commit

Permalink
Migrate editor demo to use updated CL-CHARMS.
Browse files Browse the repository at this point in the history
  • Loading branch information
stylewarning committed Sep 21, 2014
1 parent 8c32dbd commit ced77f1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 120 deletions.
146 changes: 70 additions & 76 deletions editor/editor.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@

(defun refresh-editor ()
(check-editor-initialized)
(charms:wrefresh charms:*stdscr*))
(charms:refresh-window charms:*standard-window*))

(defun move-cursor (x y)
(check-editor-initialized)
(charms:wmove charms:*stdscr* y x)
(charms:wrefresh charms:*stdscr*))
(charms:move-cursor charms:*standard-window* x y)
(refresh-editor))

(defun printablep (char)
(and (graphic-char-p char)
Expand All @@ -60,85 +60,79 @@
(dotimes (y height)
(dotimes (x width)
(let ((char (canvas-ref canvas x y)))
(charms:mvwaddch scr
(+ y y0)
(+ x x0)
(char-code (if (printablep char)
char
#\?))))))))
(charms/ll:mvwaddch scr
(+ y y0)
(+ x x0)
(char-code (if (printablep char)
char
#\?))))))))

(defun highlight-region (window region)
(loop :for y :from (formulador::region-min-y region)
:below (formulador::region-max-y region)
:do (loop :for x :from (formulador::region-min-x region)
:below (formulador::region-max-x region)
:for c := (charms:mvwinch window y x)
:do (charms:mvwaddch window y x (logior c charms:A_STANDOUT)))))
:for c := (charms/ll:mvwinch window y x)
:do (charms/ll:mvwaddch window y x (logior c charms/ll:A_STANDOUT)))))

(defun start-editor ()
;; Clear any output that hasn't been written.
(force-output *terminal-io*)

;; Initialize ncurses.
(charms:initscr)
(unwind-protect
(let ((*editor-initialized* t))
;; Do not echo characters immediately. We will do this
;; ourselves.
(charms:noecho)

;; Do not buffer.
(charms:cbreak)

;; Disable blocking.
(charms:nodelay charms:*stdscr* charms:TRUE)

;; Enable function keys, arrow keys, etc.
(charms:keypad charms:*stdscr* charms:TRUE)

;; Wait for a character
(loop :named driver-loop
:with regions := (formulador::find-associations *canvas* 0 0)
:with level := 0
:for c := (charms:wgetch charms:*stdscr*)
:do (labels ((cursor-moved ()
(multiple-value-bind (cursor-x cursor-y)
(cursor-position charms:*stdscr*)
(setf regions
(formulador::find-associations *canvas*
cursor-x
cursor-y))
(setf level 0)))
(descend ()
(setf level (mod (1+ level) (length regions))))
(highlight-level ()
(when regions
(highlight-region charms:*stdscr*
(car (elt regions level))))))
(with-restored-cursor charms:*stdscr*
(render-formula charms:*stdscr* 0 0)
(highlight-level))

(cond
((= c (char-code #\/))
(descend))
((= c charms:KEY_UP)
(cursor-up charms:*stdscr*)
(cursor-moved))
((= c charms:KEY_DOWN)
(cursor-down charms:*stdscr*)
(cursor-moved))
((= c charms:KEY_LEFT)
(cursor-left charms:*stdscr*)
(cursor-moved))
((= c charms:KEY_RIGHT)
(cursor-right charms:*stdscr*)
(cursor-moved))
((= c charms:ERR) nil)
(t (return-from driver-loop)))

;; Refresh the screen.
(charms:wrefresh charms:*stdscr*))))

;; Finalize ncurses.
(charms:endwin)))
(charms:with-curses ()
(let ((*editor-initialized* t))
;; Do not echo characters immediately. We will do this
;; ourselves.
(charms:disable-echoing)

;; Do not buffer.
(charms:enable-raw-input :interpret-control-characters t)

;; Disable blocking.
(charms/ll:nodelay charms/ll:*stdscr* charms/ll:TRUE)

;; Enable function keys, arrow keys, etc.
(charms:enable-extra-keys charms:*standard-window*)

;; Wait for a character
(loop :named driver-loop
:with regions := (formulador::find-associations *canvas* 0 0)
:with level := 0
:for c := (charms/ll:wgetch charms/ll:*stdscr*)
:do (labels ((cursor-moved ()
(multiple-value-bind (cursor-x cursor-y)
(charms:cursor-position charms:*standard-window*)
(setf regions
(formulador::find-associations *canvas*
cursor-x
cursor-y))
(setf level 0)))
(descend ()
(setf level (mod (1+ level) (length regions))))
(highlight-level ()
(when regions
(highlight-region charms/ll:*stdscr*
(car (elt regions level))))))
(charms:with-restored-cursor charms:*standard-window*
(render-formula (charms::window-pointer
charms:*standard-window*) 0 0)
(highlight-level))

(cond
((= c (char-code #\/))
(descend))
((= c charms/ll:KEY_UP)
(charms:move-cursor-up charms:*standard-window*)
(cursor-moved))
((= c charms/ll:KEY_DOWN)
(charms:move-cursor-down charms:*standard-window*)
(cursor-moved))
((= c charms/ll:KEY_LEFT)
(charms:move-cursor-left charms:*standard-window*)
(cursor-moved))
((= c charms/ll:KEY_RIGHT)
(charms:move-cursor-right charms:*standard-window*)
(cursor-moved))
((= c charms/ll:ERR) nil)
(t (return-from driver-loop)))

;; Refresh the screen.
(refresh-editor))))))
43 changes: 0 additions & 43 deletions editor/ncurses.lisp

This file was deleted.

1 change: 0 additions & 1 deletion formulador-editor.asd
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@
:components ((:module "editor"
:serial t
:components ((:file "package")
(:file "ncurses")
(:file "editor")))))

0 comments on commit ced77f1

Please sign in to comment.