[Prev][Next][Index][Thread]

Question on implementing a Deuce command



Doing some playing around with Deuce, the Dylan Editor, I thought I'd
try implementing the equivalent of some Emacs Lisp packages to see
what programming with Deuce is like compared to Emacs.

Starting simple, I used morse.el, and attempted a morse.dylan. There
being little in the way of Deuce documentation I thought I'd post my
attempt here to get some feedback as to whether I'm doing it the right
way - or if there are other ways of doing it.

Here's my 'morse-region' code:

------------------8<-------------------------
define command morse-region(frame)
    "Convert the selected text to morse code."
  let bp1 = copy-bp(point());
  let bp2 = mark();
  when(bp2)
    do-morse-region(frame, bp1, bp2);
  end when;    
end command morse-region;

define method do-morse-region(frame, 
                              bp1 :: <basic-bp>, 
                              bp2 :: <basic-bp>)
 => ()
  let window = frame.frame-window;
  let buffer = frame.frame-buffer;
  let reverse? = bp-less?(bp1, bp2);
  let interval = make-interval(bp1, bp2, in-order?: reverse?);
  check-read-only(interval);
  clear-mark!();
  queue-region-redisplay(window, bp1, bp2, centering: #f);  

  local method morse-line(line :: <line>, si :: <integer>, ei :: <integer>, last?)
    let line = note-line-changed(line);
    let contents = line-contents(line);
    let morse = convert-to-morse(as-lowercase(contents), start: si, end: ei);

    let start-bp = make-bp(line, si);
    let end-bp = make-bp(line, ei);

    insert!(kill!(make-interval(start-bp, end-bp)), morse);
  end method morse-line;

  with-change-recording(buffer, <replace-change-record>, interval: interval)
    do-lines(morse-line, interval);
  end with-change-recording;
end method do-morse-region;
------------------8<-------------------------

Note that 'convert-to-morse' takes a string and converts it to morse
code in much the same way as morse.el does it. It returns the
converted string. I won't post my attempt at convert-to-morse and
convert-from-morse just yet. An exercise for the reader!

I couldn't work out how to do the conversion character by character
within the Deuce buffer itself, so I went for the function that
returns a converted string and did it line by line. How would I do it
character by character 'in-place'?

Is the 'note-line-changed' needed? What exactly does that do? Inform
clients that the line has changed so it can be redisplayed?

The 'with-change-recording' macro...Does that do the do/undo handling
for me? I notice I can undo the morse-region.

I added this code to 'standalone-deuce' and it worked ok. Just curious
if it's the right approach. Deuce is fairly large and there is a lot
in there.

Anyone else using Deuce in anger?

Chris.
-- 
http://www.double.co.nz/dylan




Follow-Ups: