bar.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. local micro = import("micro")
  2. local buffer = import("micro/buffer")
  3. SideBar = {
  4. buffer_pane = nil,
  5. owner = nil,
  6. -- Must be initialized by user
  7. -- Initialized in mdtree
  8. callbacks = {}
  9. }
  10. function SideBar:Create(buffer_pane)
  11. self.owner = buffer_pane
  12. self:CreatePane(35)
  13. end
  14. function SideBar:CreatePane(size)
  15. -- Create and configure buffer of sidebar
  16. local sidebar_buf = buffer.NewBuffer("", "mdtree")
  17. -- Enable syntax highlighting
  18. sidebar_buf:SetOptionNative("filetype", "mdtree")
  19. -- Make the buffer unsavable and uneditable
  20. sidebar_buf.Type.Scratch = true
  21. sidebar_buf.Type.Readonly = true
  22. -- Configure status line to show nothing
  23. sidebar_buf:SetOptionNative("statusformatl", "")
  24. sidebar_buf:SetOptionNative("statusformatr", "")
  25. -- Disable window decorations
  26. sidebar_buf:SetOptionNative("ruler", false)
  27. sidebar_buf:SetOptionNative("diff", false)
  28. sidebar_buf:SetOptionNative("diffgutter", false)
  29. sidebar_buf:SetOptionNative("scrollbar", false)
  30. sidebar_buf:SetOptionNative("softwrap", true)
  31. self.buffer_pane = micro.CurPane():VSplitIndex(sidebar_buf, false)
  32. -- NOTE: Bugged with current micro version (2.0.13)
  33. -- Resize the leftmost pane instead of current
  34. -- https://github.com/zyedidia/micro/issues/3046
  35. self.buffer_pane:ResizePane(size)
  36. end
  37. function SideBar:Close()
  38. self.buffer_pane:Quit()
  39. self.buffer_pane = nil
  40. self.owner = nil
  41. end
  42. -- To prevent creation new field and modification other methods
  43. function SideBar:IsOpen()
  44. if self.buffer_pane == nil
  45. then
  46. return false
  47. else
  48. return true
  49. end
  50. end
  51. function SideBar:Insert(location, text)
  52. self.buffer_pane.Buf.EventHandler:Insert(location, text)
  53. end
  54. function SideBar:Append(text)
  55. local buf = self.buffer_pane.Buf
  56. self:Insert(buf:End(), text)
  57. end
  58. function SideBar:Clear()
  59. local buf = self.buffer_pane.Buf
  60. buf.EventHandler:Remove(buf:Start(), buf:End())
  61. end
  62. function SideBar:SelectLine(line)
  63. if line ~= nil
  64. then
  65. self.buffer_pane.Cursor.Loc.Y = line
  66. end
  67. self.buffer_pane.Cursor:DeleteSelection()
  68. self.buffer_pane:SelectLine()
  69. end
  70. ------------------------------------------
  71. -- Hooks of micro
  72. -- NOTE: They aren't part of the `SideBar`
  73. -- so must use `SideBar` instead of `self`
  74. ------------------------------------------
  75. function preQuit(buffer_pane)
  76. -- Handle: when user wants to close bar via `command:Quit`
  77. if buffer_pane == SideBar.buffer_pane
  78. then
  79. SideBar:Close()
  80. return false
  81. end
  82. -- Handle when user wants to close buffer for which bar was created
  83. if buffer_pane == SideBar.owner
  84. then
  85. SideBar:Close()
  86. return true
  87. end
  88. end
  89. -- Update information on save
  90. function onSave(buffer_pane)
  91. if not buffer_pane == SideBar.owner
  92. then
  93. SideBar.callbacks:Update()
  94. end
  95. end
  96. -- Handle if user edited document to show correct information
  97. function onSetActive(buffer_pane)
  98. if buffer_pane == SideBar.buffer_pane
  99. then
  100. SideBar.callbacks:Update()
  101. end
  102. end
  103. -- Handle when user wants to select some entry
  104. function preInsertNewline(buffer_pane)
  105. if buffer_pane == SideBar.buffer_pane
  106. then
  107. SideBar.callbacks:Jump(SideBar.buffer_pane.Cursor.Y)
  108. return false
  109. end
  110. end
  111. ------------------------------------------
  112. -- Cursor movement hooks
  113. ------------------------------------------
  114. -- NOTE: Workaround that emulates normal Deselect behavior
  115. function preCursorDown(buffer_pane)
  116. if buffer_pane == SideBar.buffer_pane
  117. then
  118. buffer_pane:Deselect()
  119. buffer_pane.Cursor:End()
  120. buffer_pane.Cursor:Down()
  121. SideBar:SelectLine()
  122. return false
  123. end
  124. return true
  125. end
  126. -- Can be used instead of above workaround after micro merge https://github.com/zyedidia/micro/pull/3091
  127. -- function onCursorDown(buffer_pane)
  128. -- if buffer_pane == SideBar.buffer_pane
  129. -- then
  130. -- SideBar:SelectLine()
  131. -- end
  132. -- return true
  133. -- end
  134. function onCursorUp(buffer_pane)
  135. if buffer_pane == SideBar.buffer_pane
  136. then
  137. SideBar:SelectLine()
  138. end
  139. return true
  140. end
  141. -- TODO: Handle when user tries to interact with bar in any way different from
  142. -- up, down select
  143. function onCursorRight(buffer_pane)
  144. end
  145. function onCursorLeft(buffer_pane)
  146. end