action.go 6.2 KB

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