builtins.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package edit
  2. import (
  3. "errors"
  4. "src.elv.sh/pkg/cli"
  5. "src.elv.sh/pkg/cli/modes"
  6. "src.elv.sh/pkg/cli/term"
  7. "src.elv.sh/pkg/cli/tk"
  8. "src.elv.sh/pkg/eval"
  9. "src.elv.sh/pkg/eval/errs"
  10. "src.elv.sh/pkg/eval/vals"
  11. "src.elv.sh/pkg/parse"
  12. "src.elv.sh/pkg/parse/parseutil"
  13. "src.elv.sh/pkg/ui"
  14. )
  15. func closeMode(app cli.App) {
  16. app.PopAddon()
  17. }
  18. func endOfHistory(app cli.App) {
  19. app.Notify(ui.T("End of history"))
  20. }
  21. type redrawOpts struct{ Full bool }
  22. func (redrawOpts) SetDefaultOptions() {}
  23. func redraw(app cli.App, opts redrawOpts) {
  24. if opts.Full {
  25. app.RedrawFull()
  26. } else {
  27. app.Redraw()
  28. }
  29. }
  30. func clear(app cli.App, tty cli.TTY) {
  31. tty.HideCursor()
  32. tty.ClearScreen()
  33. app.RedrawFull()
  34. tty.ShowCursor()
  35. }
  36. func insertRaw(app cli.App, tty cli.TTY) {
  37. codeArea, ok := focusedCodeArea(app)
  38. if !ok {
  39. return
  40. }
  41. tty.SetRawInput(1)
  42. w := modes.NewStub(modes.StubSpec{
  43. Bindings: tk.FuncBindings(func(w tk.Widget, event term.Event) bool {
  44. switch event := event.(type) {
  45. case term.KeyEvent:
  46. codeArea.MutateState(func(s *tk.CodeAreaState) {
  47. s.Buffer.InsertAtDot(string(event.Rune))
  48. })
  49. app.PopAddon()
  50. return true
  51. default:
  52. return false
  53. }
  54. }),
  55. Name: " RAW ",
  56. })
  57. app.PushAddon(w)
  58. }
  59. var errMustBeKeyOrString = errors.New("must be key or string")
  60. func toKey(v any) (ui.Key, error) {
  61. switch v := v.(type) {
  62. case ui.Key:
  63. return v, nil
  64. case string:
  65. return ui.ParseKey(v)
  66. default:
  67. return ui.Key{}, errMustBeKeyOrString
  68. }
  69. }
  70. func notify(app cli.App, x any) error {
  71. // TODO: De-duplicate with the implementation of the styled builtin.
  72. var t ui.Text
  73. switch x := x.(type) {
  74. case string:
  75. t = ui.T(x)
  76. case ui.Text:
  77. t = x.Clone()
  78. default:
  79. return errs.BadValue{What: "argument to edit:notify",
  80. Valid: "string, styled segment or styled text", Actual: vals.Kind(x)}
  81. }
  82. app.Notify(t)
  83. return nil
  84. }
  85. func smartEnter(app cli.App) {
  86. codeArea, ok := focusedCodeArea(app)
  87. if !ok {
  88. return
  89. }
  90. commit := false
  91. codeArea.MutateState(func(s *tk.CodeAreaState) {
  92. buf := &s.Buffer
  93. if isSyntaxComplete(buf.Content) {
  94. commit = true
  95. } else {
  96. buf.InsertAtDot("\n")
  97. }
  98. })
  99. if commit {
  100. app.CommitCode()
  101. }
  102. }
  103. func isSyntaxComplete(code string) bool {
  104. _, err := parse.Parse(parse.Source{Name: "[syntax check]", Code: code}, parse.Config{})
  105. if err != nil {
  106. for _, e := range err.(*parse.Error).Entries {
  107. if e.Context.From == len(code) {
  108. return false
  109. }
  110. }
  111. }
  112. return true
  113. }
  114. func wordify(fm *eval.Frame, code string) error {
  115. out := fm.ValueOutput()
  116. for _, s := range parseutil.Wordify(code) {
  117. err := out.Put(s)
  118. if err != nil {
  119. return err
  120. }
  121. }
  122. return nil
  123. }
  124. func initTTYBuiltins(app cli.App, tty cli.TTY, nb eval.NsBuilder) {
  125. nb.AddGoFns(map[string]any{
  126. "insert-raw": func() { insertRaw(app, tty) },
  127. "clear": func() { clear(app, tty) },
  128. })
  129. }
  130. func initMiscBuiltins(app cli.App, nb eval.NsBuilder) {
  131. nb.AddGoFns(map[string]any{
  132. "binding-table": makeBindingMap,
  133. "close-mode": func() { closeMode(app) },
  134. "end-of-history": func() { endOfHistory(app) },
  135. "key": toKey,
  136. "notify": func(x any) error { return notify(app, x) },
  137. "redraw": func(opts redrawOpts) { redraw(app, opts) },
  138. "return-line": app.CommitCode,
  139. "return-eof": app.CommitEOF,
  140. "smart-enter": func() { smartEnter(app) },
  141. "wordify": wordify,
  142. })
  143. }
  144. // Like mode.FocusedCodeArea, but handles the error by writing a notification.
  145. func focusedCodeArea(app cli.App) (tk.CodeArea, bool) {
  146. codeArea, err := modes.FocusedCodeArea(app)
  147. if err != nil {
  148. app.Notify(modes.ErrorText(err))
  149. return nil, false
  150. }
  151. return codeArea, true
  152. }