1
0

action.go 6.2 KB

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