1
0

action.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. package editor
  2. import(
  3. "strings"
  4. "strconv"
  5. "slices"
  6. "os"
  7. "github.com/zyedidia/rope"
  8. "golang.design/x/clipboard"
  9. "moose/internal/util"
  10. )
  11. type ActionManager = struct {
  12. Common []Action
  13. Normal []Action
  14. Insert []Action
  15. Palette []Action
  16. CM ChordManager
  17. }
  18. type Action = struct {
  19. Bindings []Binding
  20. Commands []string
  21. AskArgs bool
  22. Callback func(*Model, []string)
  23. }
  24. type Binding = struct {
  25. Type BindingType
  26. Single string
  27. Chord []string
  28. }
  29. type BindingType int
  30. const (
  31. BindingSingle BindingType = iota
  32. BindingChord
  33. )
  34. type ChordManager = struct {
  35. Recording bool
  36. Recorded []string
  37. }
  38. func NewChordManager() ChordManager {
  39. return ChordManager{
  40. Recording: false,
  41. Recorded: []string{},
  42. }
  43. }
  44. func DefaultActionManager() ActionManager {
  45. return ActionManager{
  46. CM: NewChordManager(),
  47. Common: []Action{
  48. Action{
  49. Bindings: []Binding{
  50. Binding{
  51. Type: BindingSingle,
  52. Single: "f12",
  53. },
  54. },
  55. Callback: func(m *Model, args []string) {
  56. m.Mode = ModeNormal
  57. },
  58. },
  59. Action{
  60. Bindings: []Binding{
  61. Binding{
  62. Type: BindingSingle,
  63. Single: "ctrl+q",
  64. },
  65. },
  66. Commands: []string{"q", "quit"},
  67. AskArgs: true,
  68. Callback: func(m *Model, args []string) {
  69. m.Quit()
  70. },
  71. },
  72. Action{
  73. Bindings: []Binding{
  74. Binding{
  75. Type: BindingSingle,
  76. Single: "ctrl+s",
  77. },
  78. },
  79. Commands: []string{"s", "save"},
  80. AskArgs: false,
  81. Callback: func(m *Model, args []string) {
  82. path := ""
  83. if len(args) < 1 || args[0] == "" {
  84. if m.BM.Current().Path == "" {
  85. m.Mode = ModeNormal
  86. m.BM.PaletteBuffer.Clear()
  87. m.BM.PaletteBuffer.Insert("moose.error:Missing filename argument for save")
  88. return
  89. }
  90. }
  91. if len(args) > 0 && args[0] != "" {
  92. m.BM.Current().Path = args[0]
  93. }
  94. path = m.BM.Current().Path
  95. data := []byte(m.BM.Current().String())
  96. err := os.WriteFile(path, data, 0644)
  97. if err != nil {
  98. m.Mode = ModeNormal
  99. m.BM.PaletteBuffer.Clear()
  100. m.BM.PaletteBuffer.Insert("moose.error:Could not save to file \"" + path + "\": " + err.Error())
  101. return
  102. }
  103. m.BM.PaletteBuffer.Clear()
  104. m.BM.PaletteBuffer.Insert("moose.info:Wrote " + strconv.Itoa(len(data)) + " bytes to \"" + path + "\"")
  105. return
  106. },
  107. },
  108. Action{
  109. Bindings: []Binding{
  110. Binding{
  111. Type: BindingSingle,
  112. Single: "ctrl+shift+s",
  113. },
  114. },
  115. Commands: []string{"s", "save"},
  116. AskArgs: true,
  117. },
  118. Action{
  119. Bindings: []Binding{
  120. Binding{
  121. Type: BindingSingle,
  122. Single: "ctrl+e",
  123. },
  124. },
  125. Commands: []string{"e", "edit"},
  126. AskArgs: true,
  127. Callback: func(m *Model, args []string) {
  128. if len(args) < 1 {
  129. m.Mode = ModeNormal
  130. m.BM.PaletteBuffer.Clear()
  131. m.BM.PaletteBuffer.Insert("moose.error:Did not specify path for edit")
  132. return
  133. }
  134. content, err := os.ReadFile(args[0])
  135. if err != nil {
  136. m.Mode = ModeNormal
  137. m.BM.PaletteBuffer.Clear()
  138. m.BM.PaletteBuffer.Insert("moose.error:Could not edit file \"" + args[0] + "\": " + err.Error())
  139. return
  140. }
  141. m.BM.Current().Clear()
  142. m.BM.Current().Rope = rope.New(content)
  143. m.BM.Current().LI.Rebuild(m.BM.Current().Rope)
  144. m.BM.Current().Path = args[0]
  145. },
  146. },
  147. Action{
  148. Bindings: []Binding{
  149. Binding{
  150. Type: BindingSingle,
  151. Single: "left",
  152. },
  153. },
  154. Callback: func(m *Model, args []string) {
  155. if m.Mode == ModePalette {
  156. m.BM.PaletteBuffer.MoveHoriz(-1)
  157. } else {
  158. m.BM.Current().MoveHoriz(-1)
  159. }
  160. },
  161. },
  162. Action{
  163. Bindings: []Binding{
  164. Binding{
  165. Type: BindingSingle,
  166. Single: "right",
  167. },
  168. },
  169. Callback: func(m *Model, args []string) {
  170. if m.Mode == ModePalette {
  171. m.BM.PaletteBuffer.MoveHoriz(1)
  172. } else {
  173. m.BM.Current().MoveHoriz(1)
  174. }
  175. },
  176. },
  177. Action{
  178. Bindings: []Binding{
  179. Binding{
  180. Type: BindingSingle,
  181. Single: "up",
  182. },
  183. },
  184. Callback: func(m *Model, args []string) {
  185. if m.Mode != ModePalette {
  186. m.BM.Current().MoveVert(-1)
  187. }
  188. },
  189. },
  190. Action{
  191. Bindings: []Binding{
  192. Binding{
  193. Type: BindingSingle,
  194. Single: "down",
  195. },
  196. },
  197. Callback: func(m *Model, args []string) {
  198. if m.Mode != ModePalette {
  199. m.BM.Current().MoveVert(1)
  200. }
  201. },
  202. },
  203. Action{
  204. Bindings: []Binding{
  205. Binding{
  206. Type: BindingSingle,
  207. Single: "shift+left",
  208. },
  209. },
  210. AskArgs: false,
  211. Callback: func(m *Model, args []string) {
  212. if m.Mode == ModePalette {
  213. m.BM.PaletteBuffer.MoveWordHoriz(-1)
  214. } else {
  215. m.BM.Current().MoveWordHoriz(-1)
  216. }
  217. },
  218. },
  219. Action{
  220. Bindings: []Binding{
  221. Binding{
  222. Type: BindingSingle,
  223. Single: "shift+right",
  224. },
  225. },
  226. AskArgs: false,
  227. Callback: func(m *Model, args []string) {
  228. if m.Mode == ModePalette {
  229. m.BM.PaletteBuffer.MoveWordHoriz(1)
  230. } else {
  231. m.BM.Current().MoveWordHoriz(1)
  232. }
  233. },
  234. },
  235. },
  236. Normal: []Action{
  237. Action{
  238. Bindings: []Binding{
  239. Binding{
  240. Type: BindingChord,
  241. Chord: []string{"d", "d"},
  242. },
  243. },
  244. Callback: func(m *Model, args []string) {
  245. m.BM.Current().DeleteLine()
  246. },
  247. },
  248. Action{
  249. Bindings: []Binding{
  250. Binding{
  251. Type: BindingSingle,
  252. Single: "w",
  253. },
  254. },
  255. Callback: func(m *Model, args []string) {
  256. m.Mode = ModeWrite
  257. },
  258. },
  259. Action{
  260. Bindings: []Binding{
  261. Binding{
  262. Type: BindingSingle,
  263. Single: "q",
  264. },
  265. },
  266. Callback: func(m *Model, args []string) {
  267. m.Mode = ModePalette
  268. m.BM.PaletteBuffer.Clear()
  269. m.BM.PaletteBuffer.Insert("/")
  270. },
  271. },
  272. Action{
  273. Bindings: []Binding{
  274. Binding{
  275. Type: BindingSingle,
  276. Single: "f",
  277. },
  278. },
  279. AskArgs: true,
  280. Callback: func(m *Model, args []string) {
  281. m.Mode = ModePalette
  282. m.BM.PaletteBuffer.Clear()
  283. m.BM.PaletteBuffer.Insert("?")
  284. },
  285. },
  286. Action{
  287. Bindings: []Binding{
  288. Binding{
  289. Type: BindingSingle,
  290. Single: "v",
  291. },
  292. },
  293. Commands: []string{"p", "paste"},
  294. AskArgs: false,
  295. Callback: func(m *Model, args []string) {
  296. text := clipboard.Read(clipboard.FmtText)
  297. if string(text) != "" {
  298. if m.Mode == ModePalette {
  299. m.BM.PaletteBuffer.Insert(string(text))
  300. } else {
  301. m.BM.Current().Insert(string(text))
  302. }
  303. }
  304. },
  305. },
  306. Action{
  307. Bindings: []Binding{
  308. Binding{
  309. Type: BindingSingle,
  310. Single: "j",
  311. },
  312. },
  313. Callback: func(m *Model, args []string) {
  314. if m.Mode == ModePalette {
  315. m.BM.PaletteBuffer.MoveHoriz(-1)
  316. } else {
  317. m.BM.Current().MoveHoriz(-1)
  318. }
  319. },
  320. },
  321. },
  322. Insert: []Action{
  323. Action{
  324. Bindings: []Binding{
  325. Binding{
  326. Type: BindingSingle,
  327. Single: "shift+up",
  328. },
  329. },
  330. Commands: []string{"TODO:"},
  331. AskArgs: false,
  332. Callback: func(m *Model, args []string) {
  333. m.BM.Current().AddCursorVert(-1)
  334. },
  335. },
  336. Action{
  337. Bindings: []Binding{
  338. Binding{
  339. Type: BindingSingle,
  340. Single: "shift+down",
  341. },
  342. },
  343. Commands: []string{"TODO:"},
  344. AskArgs: false,
  345. Callback: func(m *Model, args []string) {
  346. m.BM.Current().AddCursorVert(1)
  347. },
  348. },
  349. Action{
  350. Bindings: []Binding{
  351. Binding{
  352. Type: BindingSingle,
  353. Single: "esc",
  354. },
  355. },
  356. Commands: []string{"clrc", "clearcursors"},
  357. AskArgs: false,
  358. Callback: func(m *Model, args []string) {
  359. m.BM.Current().ClearCursors()
  360. },
  361. },
  362. Action{
  363. Bindings: []Binding{
  364. Binding{
  365. Type: BindingSingle,
  366. Single: "tab",
  367. },
  368. },
  369. Commands: []string{"TODO:"},
  370. AskArgs: false,
  371. Callback: func(m *Model, args []string) {
  372. for _ = range 4 {
  373. m.BM.Current().Insert(" ")
  374. }
  375. },
  376. },
  377. Action{
  378. Bindings: []Binding{
  379. Binding{
  380. Type: BindingSingle,
  381. Single: "shift+tab",
  382. },
  383. },
  384. Commands: []string{"TODO:"},
  385. AskArgs: false,
  386. Callback: func(m *Model, args []string) {
  387. // TODO: implement m.BM.Current().remove...
  388. },
  389. },
  390. Action{
  391. Bindings: []Binding{
  392. Binding{
  393. Type: BindingSingle,
  394. Single: "backspace",
  395. },
  396. },
  397. Callback: func(m *Model, args []string) {
  398. m.BM.Current().Delete()
  399. },
  400. },
  401. Action{
  402. Bindings: []Binding{
  403. Binding{
  404. Type: BindingSingle,
  405. Single: "enter",
  406. },
  407. },
  408. Callback: func(m *Model, args []string) {
  409. m.BM.Current().Insert("\n")
  410. },
  411. },
  412. },
  413. Palette: []Action{
  414. Action{
  415. Bindings: []Binding{
  416. Binding{
  417. Type: BindingSingle,
  418. Single: "backspace",
  419. },
  420. },
  421. Callback: func(m *Model, args []string) {
  422. m.BM.PaletteBuffer.Delete()
  423. },
  424. },
  425. Action{
  426. Bindings: []Binding{
  427. Binding{
  428. Type: BindingSingle,
  429. Single: "enter",
  430. },
  431. },
  432. Callback: func(m *Model, _ []string) {
  433. input := m.BM.PaletteBuffer.String()
  434. args := strings.Split(input, " ")
  435. if args[0] == "" {
  436. m.Mode = ModeNormal
  437. return
  438. }
  439. cmd := string([]rune(args[0])[1:])
  440. if strings.HasPrefix(args[0], "/") {
  441. for _, action := range append(m.AM.Common, append(m.AM.Normal, m.AM.Insert...)...) {
  442. if action.Callback == nil { continue }
  443. if len(action.Commands) > 0 && slices.Contains(util.StandardizeBindingsArray(action.Commands), util.StandardizeBinding(cmd)) {
  444. m.Mode = ModeNormal
  445. action.Callback(m, args[1:])
  446. return
  447. }
  448. }
  449. m.Mode = ModeNormal
  450. m.BM.PaletteBuffer.Clear()
  451. m.BM.PaletteBuffer.Insert("moose.error:Unknown command \"" + cmd + "\"")
  452. } else if strings.HasPrefix(args[0], "?") {
  453. m.Mode = ModeFind
  454. m.BM.PaletteBuffer.Clear()
  455. m.BM.PaletteBuffer.Insert("moose.error:Find mode unimplemented \"" + cmd + "\"")
  456. } else {
  457. m.Mode = ModeNormal
  458. m.BM.PaletteBuffer.Clear()
  459. m.BM.PaletteBuffer.Insert("moose.error:Unknown palette input \"" + cmd + "\"")
  460. }
  461. },
  462. },
  463. },
  464. }
  465. }