action.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package editor
  2. import(
  3. "strings"
  4. "strconv"
  5. "os"
  6. "github.com/zyedidia/rope"
  7. )
  8. type ActionManager = struct {
  9. Common []Action
  10. Normal []Action
  11. Insert []Action
  12. Palette []Action
  13. }
  14. type Action = struct {
  15. Binding string
  16. Command string
  17. AskArgs bool
  18. Callback func(*Model, []string)
  19. }
  20. func DefaultActionManager() ActionManager {
  21. return ActionManager{
  22. Common: []Action{
  23. Action{
  24. Binding: "f12",
  25. Callback: func(m *Model, args []string) {
  26. m.Mode = ModeNormal
  27. },
  28. },
  29. Action{
  30. Binding: "left",
  31. Callback: func(m *Model, args []string) {
  32. if m.Mode == ModePalette {
  33. m.BM.PaletteBuffer.MoveHoriz(-1)
  34. } else {
  35. m.BM.Current().MoveHoriz(-1)
  36. }
  37. },
  38. },
  39. Action{
  40. Binding: "right",
  41. Callback: func(m *Model, args []string) {
  42. if m.Mode == ModePalette {
  43. m.BM.PaletteBuffer.MoveHoriz(1)
  44. } else {
  45. m.BM.Current().MoveHoriz(1)
  46. }
  47. },
  48. },
  49. Action{
  50. Binding: "up",
  51. Callback: func(m *Model, args []string) {
  52. if m.Mode != ModePalette {
  53. m.BM.Current().MoveVert(-1)
  54. }
  55. },
  56. },
  57. Action{
  58. Binding: "down",
  59. Callback: func(m *Model, args []string) {
  60. if m.Mode != ModePalette {
  61. m.BM.Current().MoveVert(1)
  62. }
  63. },
  64. },
  65. Action{
  66. Binding: "ctrl+q",
  67. Command: "q",
  68. AskArgs: true,
  69. Callback: func(m *Model, args []string) {
  70. m.Quit()
  71. },
  72. },
  73. Action{
  74. Binding: "ctrl+s",
  75. Command: "s",
  76. AskArgs: false,
  77. Callback: func(m *Model, args []string) {
  78. path := ""
  79. if len(args) < 1 || args[0] == "" {
  80. if m.BM.Current().Path == "" {
  81. m.Mode = ModeNormal
  82. m.BM.PaletteBuffer.Clear()
  83. m.BM.PaletteBuffer.Insert("moose.error:Missing filename argument for save")
  84. return
  85. }
  86. }
  87. if len(args) > 0 && args[0] != "" {
  88. m.BM.Current().Path = args[0]
  89. }
  90. path = m.BM.Current().Path
  91. data := []byte(m.BM.Current().String())
  92. err := os.WriteFile(path, data, 0644)
  93. if err != nil {
  94. m.Mode = ModeNormal
  95. m.BM.PaletteBuffer.Clear()
  96. m.BM.PaletteBuffer.Insert("moose.error:Could not save to file \"" + path + "\": " + err.Error())
  97. return
  98. }
  99. m.BM.PaletteBuffer.Clear()
  100. m.BM.PaletteBuffer.Insert("moose.info:Wrote " + strconv.Itoa(len(data)) + " bytes to \"" + path + "\"")
  101. return
  102. },
  103. },
  104. Action{
  105. Binding: "shift+ctrl+Rune[s]",
  106. Command: "s",
  107. AskArgs: true,
  108. },
  109. Action{
  110. Binding: "ctrl+e",
  111. Command: "e",
  112. AskArgs: true,
  113. Callback: func(m *Model, args []string) {
  114. if len(args) < 1 {
  115. m.Mode = ModeNormal
  116. m.BM.PaletteBuffer.Clear()
  117. m.BM.PaletteBuffer.Insert("moose.error:Did not specify path for edit")
  118. return
  119. }
  120. content, err := os.ReadFile(args[0])
  121. if err != nil {
  122. m.Mode = ModeNormal
  123. m.BM.PaletteBuffer.Clear()
  124. m.BM.PaletteBuffer.Insert("moose.error:Could not edit file \"" + args[0] + "\": " + err.Error())
  125. return
  126. }
  127. m.BM.Current().Clear()
  128. m.BM.Current().Rope = rope.New(content)
  129. m.BM.Current().LI.Rebuild(m.BM.Current().Rope)
  130. m.BM.Current().Path = args[0]
  131. },
  132. },
  133. },
  134. Normal: []Action{
  135. Action{
  136. Binding: "Rune[w]",
  137. Callback: func(m *Model, args []string) {
  138. m.Mode = ModeWrite
  139. },
  140. },
  141. Action{
  142. Binding: "Rune[q]",
  143. Callback: func(m *Model, args []string) {
  144. m.Mode = ModePalette
  145. m.BM.PaletteBuffer.Clear()
  146. m.BM.PaletteBuffer.Insert("/")
  147. },
  148. },
  149. Action{
  150. Binding: "Rune[f]",
  151. AskArgs: true,
  152. Callback: func(m *Model, args []string) {
  153. m.Mode = ModePalette
  154. m.BM.PaletteBuffer.Clear()
  155. m.BM.PaletteBuffer.Insert("?")
  156. },
  157. },
  158. },
  159. Insert: []Action{
  160. Action{
  161. Binding: "shift+up",
  162. Command: "TODO:",
  163. AskArgs: false,
  164. Callback: func(m *Model, args []string) {
  165. m.BM.Current().AddCursorVert(-1)
  166. },
  167. },
  168. Action{
  169. Binding: "shift+down",
  170. Command: "TODO:",
  171. AskArgs: false,
  172. Callback: func(m *Model, args []string) {
  173. m.BM.Current().AddCursorVert(1)
  174. },
  175. },
  176. Action{
  177. Binding: "esc",
  178. Command: "TODO:",
  179. AskArgs: false,
  180. Callback: func(m *Model, args []string) {
  181. m.BM.Current().Insert("test")
  182. },
  183. },
  184. Action{
  185. Binding: "tab",
  186. Command: "TODO:",
  187. AskArgs: false,
  188. Callback: func(m *Model, args []string) {
  189. for _ = range 4 {
  190. m.BM.Current().Insert(" ")
  191. }
  192. },
  193. },
  194. Action{
  195. Binding: "shift+tab",
  196. Command: "TODO:",
  197. AskArgs: false,
  198. Callback: func(m *Model, args []string) {
  199. // TODO: implement m.BM.Current().remove...
  200. },
  201. },
  202. Action{
  203. Binding: "backspace",
  204. Callback: func(m *Model, args []string) {
  205. m.BM.Current().Delete()
  206. },
  207. },
  208. Action{
  209. Binding: "enter",
  210. Callback: func(m *Model, args []string) {
  211. m.BM.Current().Insert("\n")
  212. },
  213. },
  214. },
  215. Palette: []Action{
  216. Action{
  217. Binding: "backspace",
  218. Callback: func(m *Model, args []string) {
  219. m.BM.PaletteBuffer.Delete()
  220. },
  221. },
  222. Action{
  223. Binding: "enter",
  224. Callback: func(m *Model, _ []string) {
  225. input := m.BM.PaletteBuffer.String()
  226. args := strings.Split(input, " ")
  227. if args[0] == "" {
  228. m.Mode = ModeNormal
  229. return
  230. }
  231. cmd := string([]rune(args[0])[1:])
  232. if strings.HasPrefix(args[0], "/") {
  233. for _, action := range append(m.AM.Common, append(m.AM.Normal, m.AM.Insert...)...) {
  234. if action.Callback == nil { continue }
  235. if action.Command != "" && cmd == action.Command {
  236. m.Mode = ModeNormal
  237. action.Callback(m, args[1:])
  238. return
  239. }
  240. }
  241. m.Mode = ModeNormal
  242. m.BM.PaletteBuffer.Clear()
  243. m.BM.PaletteBuffer.Insert("moose.error:Unknown command \"" + cmd + "\"")
  244. } else if strings.HasPrefix(args[0], "?") {
  245. m.Mode = ModeFind
  246. m.BM.PaletteBuffer.Clear()
  247. m.BM.PaletteBuffer.Insert("moose.error:Find mode unimplemented \"" + cmd + "\"")
  248. } else {
  249. m.Mode = ModeNormal
  250. m.BM.PaletteBuffer.Clear()
  251. m.BM.PaletteBuffer.Insert("moose.error:Unknown palette input \"" + cmd + "\"")
  252. }
  253. },
  254. },
  255. },
  256. }
  257. }