about summary refs log blame commit diff stats
path: root/html/real-files.mu.html
blob: fab7777b8d9a5949fdd2fd0119f660a01201660c (plain) (tree)
1
2
3
4
5
6
7
8
9



                                                                                          
                                 
                                         

                                                
                                                                                                               
                                                 

                       

                                                                                                 
                    
                                     
                             



                             





                               






















                       
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - real-files.mu</title>
<meta name="Generator" content="Vim/8.0">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal-light">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #c6c6c6; }
body { font-size:12pt; font-family: monospace; color: #000000; background-color: #c6c6c6; }
a { color:inherit; }
* { font-size:12pt; font-size: 1em; }
.muRecipe { color: #ff8700; }
.LineNr { }
.Constant { color: #008787; }
.Special { color: #ff6060; }
.Comment { color: #005faf; }
-->
</style>

<script type='text/javascript'>
<!--

/* function to open any folds containing a jumped-to line before jumping to it */
function JumpToLine()
{
  var lineNum;
  lineNum = window.location.hash;
  lineNum = lineNum.substr(1); /* strip off '#' */

  if (lineNum.indexOf('L') == -1) {
    lineNum = 'L'+lineNum;
  }
  lineElem = document.getElementById(lineNum);
  /* Always jump to new location even if the line was hidden inside a fold, or
   * we corrected the raw number to a line ID.
   */
  if (lineElem) {
    lineElem.scrollIntoView(true);
  }
  return true;
}
if ('onhashchange' in window) {
  window.onhashchange = JumpToLine;
}

-->
</script>
</head>
<body onload='JumpToLine();'>
<a href='https://github.com/akkartik/mu/blob/master/real-files.mu'>https://github.com/akkartik/mu/blob/master/real-files.mu</a>
<pre id='vimCodeElement'>
<span id="L1" class="LineNr"> 1 </span><span class="Comment"># example program: read a character from one file and write it to another</span>
<span id="L2" class="LineNr"> 2 </span><span class="Comment"># BEWARE: this will modify your file system</span>
<span id="L3" class="LineNr"> 3 </span><span class="Comment"># before running it, put a character into /tmp/mu-x</span>
<span id="L4" class="LineNr"> 4 </span><span class="Comment"># after running it, check /tmp/mu-y</span>
<span id="L5" class="LineNr"> 5 </span>
<span id="L6" class="LineNr"> 6 </span><span class="muRecipe">def</span> <a href='real-files.mu.html#L6'>main</a> [
<span id="L7" class="LineNr"> 7 </span>  <span class="Constant">local-scope</span>
<span id="L8" class="LineNr"> 8 </span>  f:num/file <span class="Special">&lt;-</span> $open-file-for-reading <span class="Constant">[/tmp/mu-x]</span>
<span id="L9" class="LineNr"> 9 </span>  $print <span class="Constant">[file to read from: ]</span>, f, <span class="Constant">10/newline</span>
<span id="L10" class="LineNr">10 </span>  c:char, eof?:bool <span class="Special">&lt;-</span> $read-from-file f
<span id="L11" class="LineNr">11 </span>  $print <span class="Constant">[copying ]</span>, c, <span class="Constant">10/newline</span>
<span id="L12" class="LineNr">12 </span>  f <span class="Special">&lt;-</span> $close-file f
<span id="L13" class="LineNr">13 </span>  $print <span class="Constant">[file after closing: ]</span>, f, <span class="Constant">10/newline</span>
<span id="L14" class="LineNr">14 </span>  f <span class="Special">&lt;-</span> $open-file-for-writing <span class="Constant">[/tmp/mu-y]</span>
<span id="L15" class="LineNr">15 </span>  $print <span class="Constant">[file to <a href='075channel.mu.html#L60'>write</a> to: ]</span>, f, <span class="Constant">10/newline</span>
<span id="L16" class="LineNr">16 </span>  $write-to-file f, c
<span id="L17" class="LineNr">17 </span>  f <span class="Special">&lt;-</span> $close-file f
<span id="L18" class="LineNr">18 </span>]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
lass="mi">1, 1, 1) love.graphics.rectangle('fill', 0, 0, App.screen.width-1, App.screen.height-1) --? love.graphics.setColor(0, 1, 0) --? love.graphics.line(Line_width,0, Line_width,App.screen.height) love.graphics.setColor(0, 0, 0) -- some hysteresis while resizing if Last_resize_time then if love.timer.getTime() - Last_resize_time < 0.1 then return else Last_resize_time = nil end end assert(Text.le1(Screen_top1, Cursor1)) Cursor_y = -1 local y = Margin_top --? print('== draw') for line_index,line in ipairs(Lines) do --? print('draw:', y, line_index, line) if y + Line_height > App.screen.height then break end --? print('a') if line_index >= Screen_top1.line then Screen_bottom1.line = line_index if line.mode == 'text' and line.data == '' then line.y = y button('draw', {x=4,y=y+4, w=12,h=12, color={1,1,0}, icon = icon.insert_drawing, onpress1 = function() Drawing.before = snapshot(line_index-1, line_index) table.insert(Lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}}) if Cursor1.line >= line_index then Cursor1.line = Cursor1.line+1 end save_to_disk(Lines, Filename) record_undo_event({before=Drawing.before, after=snapshot(line_index-1, line_index+1)}) end}) if Search_term == nil then if line_index == Cursor1.line then Text.draw_cursor(Margin_left, y) end end Screen_bottom1.pos = Screen_top1.pos y = y + Line_height elseif line.mode == 'drawing' then y = y+Drawing_padding_top line.y = y Drawing.draw(line) y = y + Drawing.pixels(line.h) + Drawing_padding_bottom else --? print('text') line.y = y y, Screen_bottom1.pos = Text.draw(line, Line_width, line_index) y = y + Line_height --? print('=> y', y) end end end if Cursor_y == -1 then Cursor_y = App.screen.height end --? print('screen bottom: '..tostring(Screen_bottom1.pos)..' in '..tostring(Lines[Screen_bottom1.line].data)) if Search_term then Text.draw_search_bar() end end function App.update(dt) Cursor_time = Cursor_time + dt -- some hysteresis while resizing if Last_resize_time then if love.timer.getTime() - Last_resize_time < 0.1 then return else Last_resize_time = nil end end Drawing.update(dt) end function App.mousepressed(x,y, mouse_button) if Search_term then return end propagate_to_button_handlers(x,y, mouse_button) for line_index,line in ipairs(Lines) do if line.mode == 'text' then if Text.in_line(line_index,line, x,y) then -- delicate dance between cursor, selection and old cursor -- manual tests: -- regular press+release: sets cursor, clears selection -- shift press+release: -- sets selection to old cursor if not set otherwise leaves it untouched -- sets cursor -- press and hold to start a selection: sets selection on press, cursor on release -- press and hold, then press shift: ignore shift -- i.e. mousereleased should never look at shift state Old_cursor1 = Cursor1 Old_selection1 = Selection1 Mousepress_shift = App.shift_down() Selection1 = {line=line_index, pos=Text.to_pos_on_line(line, x, y)} end elseif line.mode == 'drawing' then if Drawing.in_drawing(line, x, y) then Lines.current_drawing_index = line_index Lines.current_drawing = line Drawing.before = snapshot(line_index) Drawing.mouse_pressed(line, x,y, button) end end end end function App.mousereleased(x,y, button) if Search_term then return end if Lines.current_drawing then Drawing.mouse_released(x,y, button) save_to_disk(Lines, Filename) if Drawing.before then record_undo_event({before=Drawing.before, after=snapshot(Lines.current_drawing_index)}) Drawing.before = nil end else for line_index,line in ipairs(Lines) do if line.mode == 'text' then if Text.in_line(line_index,line, x,y) then Cursor1 = {line=line_index, pos=Text.to_pos_on_line(line, x, y)} --? print(Cursor1.line, Cursor1.pos) if Mousepress_shift then if Old_selection1.line == nil then Selection1 = Old_cursor1 else Selection1 = Old_selection1 end end Old_cursor1, Old_selection1, Mousepress_shift = nil end end end --? print('select:', Selection1.line, Selection1.pos) end end function App.textinput(t) for _,line in ipairs(Lines) do line.y = nil end -- just in case we scroll if Search_term then Search_term = Search_term..t Search_text = nil Text.search_next() elseif Current_drawing_mode == 'name' then local before = snapshot(Lines.current_drawing_index) local drawing = Lines.current_drawing local p = drawing.points[drawing.pending.target_point] p.name = p.name..t record_undo_event({before=before, after=snapshot(Lines.current_drawing_index)}) else Text.textinput(t) end save_to_disk(Lines, Filename) end function App.keychord_pressed(chord) if Search_term then if chord == 'escape' then Search_term = nil Search_text = nil Cursor1 = Search_backup.cursor Screen_top1 = Search_backup.screen_top Search_backup = nil Text.redraw_all() -- if we're scrolling, reclaim all fragments to avoid memory leaks elseif chord == 'return' then Search_term = nil Search_text = nil Search_backup = nil elseif chord == 'backspace' then local len = utf8.len(Search_term) local byte_offset = utf8.offset(Search_term, len) Search_term = string.sub(Search_term, 1, byte_offset-1) Search_text = nil elseif chord == 'down' then Cursor1.pos = Cursor1.pos+1 Text.search_next() elseif chord == 'up' then Text.search_previous() end return elseif chord == 'C-f' then Search_term = '' Search_backup = {cursor={line=Cursor1.line, pos=Cursor1.pos}, screen_top={line=Screen_top1.line, pos=Screen_top1.pos}} assert(Search_text == nil) elseif chord == 'C-=' then initialize_font_settings(Font_height+2) Text.redraw_all() elseif chord == 'C--' then initialize_font_settings(Font_height-2) Text.redraw_all() elseif chord == 'C-0' then initialize_font_settings(20) Text.redraw_all() elseif chord == 'C-z' then for _,line in ipairs(Lines) do line.y = nil end -- just in case we scroll local event = undo_event() if event then local src = event.before Screen_top1 = deepcopy(src.screen_top) Cursor1 = deepcopy(src.cursor) Selection1 = deepcopy(src.selection) patch(Lines, event.after, event.before) Text.redraw_all() -- if we're scrolling, reclaim all fragments to avoid memory leaks save_to_disk(Lines, Filename) end elseif chord == 'C-y' then for _,line in ipairs(Lines) do line.y = nil end -- just in case we scroll local event = redo_event() if event then local src = event.after Screen_top1 = deepcopy(src.screen_top) Cursor1 = deepcopy(src.cursor) Selection1 = deepcopy(src.selection) patch(Lines, event.before, event.after) Text.redraw_all() -- if we're scrolling, reclaim all fragments to avoid memory leaks save_to_disk(Lines, Filename) end -- clipboard elseif chord == 'C-c' then for _,line in ipairs(Lines) do line.y = nil end -- just in case we scroll local s = Text.selection() if s then App.setClipboardText(s) end elseif chord == 'C-x' then for _,line in ipairs(Lines) do line.y = nil end -- just in case we scroll local s = Text.cut_selection() if s then App.setClipboardText(s) end save_to_disk(Lines, Filename) elseif chord == 'C-v' then for _,line in ipairs(Lines) do line.y = nil end -- just in case we scroll -- We don't have a good sense of when to scroll, so we'll be conservative -- and sometimes scroll when we didn't quite need to. local before_line = Cursor1.line local before = snapshot(before_line) local clipboard_data = App.getClipboardText() for _,code in utf8.codes(clipboard_data) do local c = utf8.char(code) if c == '\n' then Text.insert_return() else Text.insert_at_cursor(c) end end App.draw() if Cursor_y >= App.screen.height - Line_height then Text.snap_cursor_to_bottom_of_screen() end save_to_disk(Lines, Filename) record_undo_event({before=before, after=snapshot(before_line, Cursor1.line)}) -- dispatch to drawing or text elseif App.mouse_down(1) or chord:sub(1,2) == 'C-' then -- DON'T reset line.y here local drawing_index, drawing = Drawing.current_drawing() if drawing_index then local before = snapshot(drawing_index) Drawing.keychord_pressed(chord) record_undo_event({before=before, after=snapshot(drawing_index)}) save_to_disk(Lines, Filename) end elseif chord == 'escape' and App.mouse_down(1) then local _,drawing = Drawing.current_drawing() if drawing then drawing.pending = {} end elseif chord == 'escape' and not App.mouse_down(1) then for _,line in ipairs(Lines) do if line.mode == 'drawing' then line.show_help = false end end elseif Current_drawing_mode == 'name' then if chord == 'return' then Current_drawing_mode = Previous_drawing_mode Previous_drawing_mode = nil else local before = snapshot(Lines.current_drawing_index) local drawing = Lines.current_drawing local p = drawing.points[drawing.pending.target_point] if chord == 'escape' then p.name = nil elseif chord == 'backspace' then local len = utf8.len(p.name) local byte_offset = utf8.offset(p.name, len-1) p.name = string.sub(p.name, 1, byte_offset) end record_undo_event({before=before, after=snapshot(Lines.current_drawing_index)}) end save_to_disk(Lines, Filename) else for _,line in ipairs(Lines) do line.y = nil end -- just in case we scroll Text.keychord_pressed(chord) end end function App.keyreleased(key, scancode) end