CS 1410-20 Homework 4-challenge

Due: Friday, September 24th, 2010 9:40am

This assignment builds on the text editor of HW 2, and it needs helper functions much like the ones that you created for HW 3. You can start with the solution to HW 2 as available from the handin-status server. The data definitions change for this assignment, but you should be able to adapt most of the old code to the new data definitions.

You do not have to include templates in you submitted work, but be prepared to supply a template if you ask the instructor or TA for help on a function. (That is, you can skip writing down the template, but you must have one in mind to write the code, anyway.) Contracts and tests are always required.

  ;; An item is either
  ;;  - string containing one character
  ;;  - image
  
  ;; A list-of-item  is either
  ;;  - empty
  ;;  - (cons item list-of-item)
  
  ;; An editor1 is
  ;;  (make-editor1 list-of-item num)
  ;;      where the num is always between 0 and
  ;;      the length of the list-of-item
  (define-struct editor1 (content caret))
  
  ;; An editor2 is
  ;;  (make-editor2 list-of-item num num)
  ;;      where the first num is always between 0 and
  ;;      the one less length of the list-of-item, and the
  ;;      second num is greater than the first and less or
  ;;      equal to the length of the list-of-item
  (define-struct editor2 (content left right))
  
  ;; An editor is either
  ;;  - an editor1
  ;;  - an editor2

Part 1 – Plain Text

Implement the function plain-text, which takes an editor and returns a string representing the content of the editor. Each image in the editor becomes “.” in the output string.

Example:

  (check-expect
   (plain-text (make-editor1 (list "f" "r" "o" "g" ":" " " ) 0))
   "frog: .")

You probably will not need any old code for this function.

Part 2 – Remove Items

Implement remove-items, which takes two numbers and a list-of-item. The two numbers are start and count. The result is a list-of-item that drops count items starting from the start index.

You could implement remove-items as a composition of three functions: drop-after, which takes a position number and a list and drops all of the items after the given position; drop-before, which takes a position number a list and drops all of the items before the given position, and the built-in function append, which takes two lists and appends them.

Part 3 – Key Press

Implement the function handle-key, which takes an editor and a key string and returns an editor after the key is handled according to the same rules as in HW 2.

You’ll need to adapt most of the old code from HW 2 to the new data definition. Don’t forget to fix the contracts! Along the way, you’ll need to replace string operations with functions like remove-items and insert-item, where the latter is similar to insert from HW 3.

Part 4 – Editor Image

Implement editor-image, which takes an editor and produces an image that reflects its content and the caret or selection. See HW 2 for further hints.

Add

  ;; main : editor -> editor
  (define (main starting-editor)
    (big-bang starting-editor
              (on-key handle-key)
              (to-draw editor-image)))

to your program, and then you can run your editor with (main (make-editor1 (list ) 0) in the interactions window.

Part 5 – Optional: Mouse Handling

This part is optional.

Read about big-bang and the on-mouse handler, and add support for positioning the caret in your editor by clicking with the mouse.


Last update: Wednesday, October 20th, 2010
mflatt@cs.utah.edu