@@ -1,12 +1,16 @@ |
| 1 | 1 | module fgof_lineedit |
| 2 | | - use fgof_lineedit_types, only : lineedit_state, prompt_spec |
| 2 | + use fgof_lineedit_types, only : history_entry, lineedit_state, prompt_spec |
| 3 | 3 | implicit none |
| 4 | 4 | private |
| 5 | 5 | |
| 6 | + public :: add_history_entry |
| 6 | 7 | public :: buffer_length |
| 7 | 8 | public :: delete_left |
| 8 | 9 | public :: delete_right |
| 9 | 10 | public :: default_prompt |
| 11 | + public :: history_count |
| 12 | + public :: history_next |
| 13 | + public :: history_previous |
| 10 | 14 | public :: init_lineedit |
| 11 | 15 | public :: insert_text |
| 12 | 16 | public :: lineedit_state |
@@ -40,6 +44,10 @@ contains |
| 40 | 44 | editor%buffer = "" |
| 41 | 45 | editor%cursor = 1 |
| 42 | 46 | editor%active = .true. |
| 47 | + editor%history_index = 0 |
| 48 | + editor%browsing_history = .false. |
| 49 | + editor%stashed_buffer = "" |
| 50 | + editor%stashed_cursor = 1 |
| 43 | 51 | call normalize_lineedit(editor) |
| 44 | 52 | end subroutine init_lineedit |
| 45 | 53 | |
@@ -50,6 +58,7 @@ contains |
| 50 | 58 | editor%buffer = "" |
| 51 | 59 | editor%cursor = 1 |
| 52 | 60 | editor%active = .false. |
| 61 | + call clear_history_navigation(editor) |
| 53 | 62 | end subroutine reset_lineedit |
| 54 | 63 | |
| 55 | 64 | integer function buffer_length(editor) result(length) |
@@ -68,15 +77,7 @@ contains |
| 68 | 77 | integer, intent(in), optional :: cursor |
| 69 | 78 | |
| 70 | 79 | call normalize_lineedit(editor) |
| 71 | | - |
| 72 | | - editor%buffer = text |
| 73 | | - if (present(cursor)) then |
| 74 | | - editor%cursor = cursor |
| 75 | | - else |
| 76 | | - editor%cursor = len(text) + 1 |
| 77 | | - end if |
| 78 | | - |
| 79 | | - call clamp_cursor(editor) |
| 80 | + call assign_buffer(editor, text, cursor, clear_history=.true.) |
| 80 | 81 | end subroutine set_buffer |
| 81 | 82 | |
| 82 | 83 | subroutine insert_text(editor, text) |
@@ -87,6 +88,7 @@ contains |
| 87 | 88 | |
| 88 | 89 | call normalize_lineedit(editor) |
| 89 | 90 | if (len(text) == 0) return |
| 91 | + call clear_history_navigation(editor) |
| 90 | 92 | |
| 91 | 93 | insert_at = editor%cursor |
| 92 | 94 | old_length = len(editor%buffer) |
@@ -117,6 +119,8 @@ contains |
| 117 | 119 | return |
| 118 | 120 | end if |
| 119 | 121 | |
| 122 | + call clear_history_navigation(editor) |
| 123 | + |
| 120 | 124 | delete_at = editor%cursor - 1 |
| 121 | 125 | if (old_length == 1) then |
| 122 | 126 | editor%buffer = "" |
@@ -145,6 +149,8 @@ contains |
| 145 | 149 | return |
| 146 | 150 | end if |
| 147 | 151 | |
| 152 | + call clear_history_navigation(editor) |
| 153 | + |
| 148 | 154 | delete_at = editor%cursor |
| 149 | 155 | if (old_length == 1) then |
| 150 | 156 | editor%buffer = "" |
@@ -200,14 +206,130 @@ contains |
| 200 | 206 | editor%cursor = len(editor%buffer) + 1 |
| 201 | 207 | end subroutine move_cursor_end |
| 202 | 208 | |
| 209 | + integer function history_count(editor) result(count) |
| 210 | + type(lineedit_state), intent(in) :: editor |
| 211 | + |
| 212 | + if (allocated(editor%history)) then |
| 213 | + count = size(editor%history) |
| 214 | + else |
| 215 | + count = 0 |
| 216 | + end if |
| 217 | + end function history_count |
| 218 | + |
| 219 | + subroutine add_history_entry(editor, text) |
| 220 | + type(lineedit_state), intent(inout) :: editor |
| 221 | + character(len=*), intent(in) :: text |
| 222 | + type(history_entry), allocatable :: new_history(:) |
| 223 | + integer :: count |
| 224 | + integer :: i |
| 225 | + |
| 226 | + call normalize_lineedit(editor) |
| 227 | + if (len_trim(text) == 0) return |
| 228 | + |
| 229 | + count = history_count(editor) |
| 230 | + allocate(new_history(count + 1)) |
| 231 | + do i = 1, count |
| 232 | + new_history(i)%text = editor%history(i)%text |
| 233 | + end do |
| 234 | + new_history(count + 1)%text = text |
| 235 | + |
| 236 | + call move_alloc(new_history, editor%history) |
| 237 | + call clear_history_navigation(editor) |
| 238 | + end subroutine add_history_entry |
| 239 | + |
| 240 | + logical function history_previous(editor) result(changed) |
| 241 | + type(lineedit_state), intent(inout) :: editor |
| 242 | + integer :: count |
| 243 | + |
| 244 | + call normalize_lineedit(editor) |
| 245 | + count = history_count(editor) |
| 246 | + if (count == 0) then |
| 247 | + changed = .false. |
| 248 | + return |
| 249 | + end if |
| 250 | + |
| 251 | + if (.not. editor%browsing_history) then |
| 252 | + editor%stashed_buffer = editor%buffer |
| 253 | + editor%stashed_cursor = editor%cursor |
| 254 | + editor%browsing_history = .true. |
| 255 | + editor%history_index = count |
| 256 | + else if (editor%history_index > 1) then |
| 257 | + editor%history_index = editor%history_index - 1 |
| 258 | + else |
| 259 | + changed = .false. |
| 260 | + return |
| 261 | + end if |
| 262 | + |
| 263 | + call assign_buffer(editor, editor%history(editor%history_index)%text, len(editor%history(editor%history_index)%text) + 1, & |
| 264 | + clear_history=.false.) |
| 265 | + changed = .true. |
| 266 | + end function history_previous |
| 267 | + |
| 268 | + logical function history_next(editor) result(changed) |
| 269 | + type(lineedit_state), intent(inout) :: editor |
| 270 | + integer :: count |
| 271 | + |
| 272 | + call normalize_lineedit(editor) |
| 273 | + if (.not. editor%browsing_history) then |
| 274 | + changed = .false. |
| 275 | + return |
| 276 | + end if |
| 277 | + |
| 278 | + count = history_count(editor) |
| 279 | + if (count == 0) then |
| 280 | + call clear_history_navigation(editor) |
| 281 | + changed = .false. |
| 282 | + return |
| 283 | + end if |
| 284 | + |
| 285 | + if (editor%history_index < count) then |
| 286 | + editor%history_index = editor%history_index + 1 |
| 287 | + call assign_buffer(editor, editor%history(editor%history_index)%text, len(editor%history(editor%history_index)%text) + 1, & |
| 288 | + clear_history=.false.) |
| 289 | + changed = .true. |
| 290 | + return |
| 291 | + end if |
| 292 | + |
| 293 | + call assign_buffer(editor, editor%stashed_buffer, editor%stashed_cursor, clear_history=.false.) |
| 294 | + call clear_history_navigation(editor) |
| 295 | + changed = .true. |
| 296 | + end function history_next |
| 297 | + |
| 203 | 298 | subroutine normalize_lineedit(editor) |
| 204 | 299 | type(lineedit_state), intent(inout) :: editor |
| 205 | 300 | |
| 206 | 301 | if (.not. allocated(editor%prompt%text)) editor%prompt%text = "" |
| 207 | 302 | if (.not. allocated(editor%buffer)) editor%buffer = "" |
| 303 | + if (.not. allocated(editor%stashed_buffer)) editor%stashed_buffer = "" |
| 208 | 304 | call clamp_cursor(editor) |
| 209 | 305 | end subroutine normalize_lineedit |
| 210 | 306 | |
| 307 | + subroutine assign_buffer(editor, text, cursor, clear_history) |
| 308 | + type(lineedit_state), intent(inout) :: editor |
| 309 | + character(len=*), intent(in) :: text |
| 310 | + integer, intent(in), optional :: cursor |
| 311 | + logical, intent(in) :: clear_history |
| 312 | + |
| 313 | + editor%buffer = text |
| 314 | + if (present(cursor)) then |
| 315 | + editor%cursor = cursor |
| 316 | + else |
| 317 | + editor%cursor = len(text) + 1 |
| 318 | + end if |
| 319 | + |
| 320 | + call clamp_cursor(editor) |
| 321 | + if (clear_history) call clear_history_navigation(editor) |
| 322 | + end subroutine assign_buffer |
| 323 | + |
| 324 | + subroutine clear_history_navigation(editor) |
| 325 | + type(lineedit_state), intent(inout) :: editor |
| 326 | + |
| 327 | + editor%browsing_history = .false. |
| 328 | + editor%history_index = 0 |
| 329 | + editor%stashed_buffer = "" |
| 330 | + editor%stashed_cursor = 1 |
| 331 | + end subroutine clear_history_navigation |
| 332 | + |
| 211 | 333 | subroutine clamp_cursor(editor) |
| 212 | 334 | type(lineedit_state), intent(inout) :: editor |
| 213 | 335 | integer :: max_cursor |