edc.sl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. % A simple desk calculator to run under EMODE. In this mode all the
  2. % numbers in the buffer are summed up, any other characters are inserted
  3. % and ignored, the total is given as the last line of the OUT_WINDOW buffer..
  4. (load useful) % Need useful so that FOR loops work!
  5. % Insert a character, and then sum up all the lines in the buffer.
  6. (DE InsertAndTotal ()
  7. (progn
  8. (InsertSelfCharacter)
  9. (FindBufferTotal)))
  10. (DE DeleteBackwardAndTotal ()
  11. (progn
  12. (!$DeleteBackwardCharacter)
  13. (FindBufferTotal)))
  14. (DE DeleteForwardAndTotal ()
  15. (progn
  16. (!$DeleteForwardCharacter)
  17. (FindBufferTotal)))
  18. (DE kill_line_and_total ()
  19. (progn
  20. (kill_line)
  21. (FindBufferTotal)))
  22. (DE insert_kill_buffer_and_total ()
  23. (progn
  24. (insert_kill_buffer)
  25. (FindBufferTotal)))
  26. (DE FindBufferTotal ()
  27. (prog (total save-point save-line-index itm)
  28. % Remember our spot in the buffer.
  29. (setf save-point point)
  30. (setf save-line-index CurrentLineIndex)
  31. (setf total 0)
  32. % Move to the start of the buffer.
  33. (!$BeginningOfBuffer)
  34. % Read from, and write to, EMODE buffers.
  35. (SelectEmodeChannels)
  36. % Find the total.
  37. (while (not (EndOfBufferP (NextIndex CurrentLineIndex)))
  38. (progn
  39. % NOTE that READ would loose badly here--since it calls
  40. % MakeInputAvailable here, and thus call EMODE recursively.
  41. (setf itm (ChannelRead IN*))
  42. (cond
  43. ((NumberP itm)
  44. (setf total (plus total itm))))))
  45. % Now, show the total in the OUT_WINDOW buffer.
  46. (prog (old-point old-line-index old-buffer)
  47. (setf old-buffer CurrentBufferName)
  48. (SelectBuffer 'OUT_WINDOW)
  49. (!$EndOfBuffer) % Move to end of the buffer.
  50. (setf old-point point)
  51. (setf old-line-index CurrentLineIndex)
  52. % Move to beginning of previous line.
  53. (!$BackwardLine)
  54. (!$BeginningOfLine)
  55. % Delete the old text
  56. (delete_or_copy T CurrentLineIndex point old-line-index old-point)
  57. % Print the total (to the output buffer)
  58. (PRINT total)
  59. (SelectBuffer old-buffer))
  60. % Finally, restore the original point and mark.
  61. (SelectLine save-line-index)
  62. (setf point save-point)))
  63. % Establish keyboard bindings for Desk Calculator mode.
  64. (DE SetDCmode ()
  65. (progn
  66. % Make most characters insert and then find total.
  67. (for (from i 32 126 1)
  68. (do
  69. (SetKey i 'InsertAndTotal)))
  70. (SetKey (char TAB) 'InsertAndTotal)
  71. % Inherit the rest of the bindings from "text mode"
  72. (for (in itm TextDispatchList)
  73. (do
  74. (SetKey (car itm) (cdr itm))))
  75. % Then, rebind (some of?) the folks who actually modify stuff.
  76. (SetKey (char (cntrl D)) 'DeleteForwardAndTotal)
  77. (SetKey (char (cntrl K)) 'kill_line_and_total)
  78. (SetKey (char DELETE) 'DeleteBackwardAndTotal)
  79. (SetKey (char (cntrl Y)) 'insert_kill_buffer_and_total)))
  80. (setf DCMode '(RlispInterfaceDispatch SetDCmode BasicDispatchSetup))
  81. % This code must be run AFTER starting up EMODE.
  82. (prog (old-buffer)
  83. (setf old-buffer CurrentBufferName)
  84. (CreateBuffer 'DC DCMode)
  85. (SelectBuffer 'DC)
  86. (!$CRLF)
  87. (insert_string "0")
  88. (!$CRLF)
  89. (!$BeginningOfBuffer)
  90. (SelectBuffer old-buffer))