Skip to content

Commit 1340684

Browse files
committedJun 4, 2023
add prebeacon function (for setting pre-input highlights and highlight state) and extract exit function
1 parent 1fbd605 commit 1340684

File tree

2 files changed

+264
-217
lines changed

2 files changed

+264
-217
lines changed
 

‎fnl/leap/main.fnl

+34-20
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,37 @@ is either labeled (C) or not (B).
357357
(when aot?
358358
(resolve-conflicts targets)))))
359359

360-
(fn with-highlight-chores [opts task]
361-
(do (local opts (or opts {}))
362-
(hl:cleanup opts.hl-affected-windows)
363-
(hl:apply-backdrop (or opts.backdrop-ranges []))
360+
; State that is persisted between the API invocations of a single usage.
361+
(local target-state {:backdrop-ranges nil
362+
:hl-affected-windows nil
363+
})
364+
365+
366+
(fn with-highlight-chores [task]
367+
(do (local hl-affected-windows (or target-state.hl-affected-windows []))
368+
(local backdrop-ranges (or target-state.backdrop-ranges []))
369+
(hl:cleanup (or hl-affected-windows []))
370+
(hl:apply-backdrop (or backdrop-ranges []))
364371
(task)
365372
(hl:highlight-cursor)
366373
(vim.cmd :redraw)))
367374

375+
(fn pre-exit []
376+
(set target-state.hl-affected-windows nil)
377+
(set target-state.backdrop-ranges nil))
378+
379+
(macro exit []
380+
`(do (hl:cleanup target-state.hl-affected-windows)
381+
(pre-exit)
382+
(exec-user-autocmds :LeapLeave)
383+
(lua :return)))
384+
385+
(fn prebeacon [opts]
386+
(set target-state.backdrop-ranges opts.backdrop-ranges)
387+
(set target-state.hl-affected-windows opts.hl-affected-windows)
388+
(with-highlight-chores (fn []))
389+
)
390+
368391
(fn light-up-beacons [targets opts]
369392
(local opts (or opts {}))
370393
(for [i (or opts.start 1) (or opts.end (length targets))]
@@ -400,12 +423,6 @@ is either labeled (C) or not (B).
400423
:offset nil}
401424
:saved_editor_opts {}})
402425

403-
; State that is persisted between the API invocations of a single usage
404-
(local target-state {:backdrop-ranges nil
405-
:hl-affected-windows nil
406-
})
407-
408-
409426
(fn leap [kwargs]
410427
"Entry point for Leap motions."
411428
(local {:dot_repeat dot-repeat?
@@ -487,11 +504,6 @@ is either labeled (C) or not (B).
487504

488505
; Macros
489506

490-
(macro exit []
491-
`(do (hl:cleanup hl-affected-windows)
492-
(exec-user-autocmds :LeapLeave)
493-
(lua :return)))
494-
495507
; Be sure not to call the macro twice accidentally,
496508
; `handle-interrupted-change-op!` moves the cursor!
497509
(macro exit-early []
@@ -673,7 +685,7 @@ is either labeled (C) or not (B).
673685
(values start end))))
674686

675687
(fn get-first-pattern-input []
676-
(with-highlight-chores {: backdrop-ranges : hl-affected-windows} (fn [] (echo ""))) ; clean up the command line
688+
(with-highlight-chores (fn [] (echo ""))) ; clean up the command line
677689
(case (get-input-by-keymap prompt)
678690
; Here we can handle any other modifier key as "zeroth" input,
679691
; if the need arises.
@@ -692,7 +704,7 @@ is either labeled (C) or not (B).
692704
; char<enter> partial input (but it implies not needing
693705
; to show beacons).
694706
(not count))
695-
(with-highlight-chores {: backdrop-ranges : hl-affected-windows} (fn [] (light-up-beacons targets))))
707+
(with-highlight-chores (fn [] (light-up-beacons targets))))
696708
(get-input-by-keymap prompt))
697709

698710
(fn get-full-pattern-input []
@@ -715,7 +727,7 @@ is either labeled (C) or not (B).
715727
(set-label-states targets {: group-offset}))
716728
(set-beacons targets {:aot? vars.aot? : no-labels? : user-given-targets?})
717729
(local (start end) (get-highlighted-idx-range targets no-labels?))
718-
(with-highlight-chores {: backdrop-ranges : hl-affected-windows} (fn [] (light-up-beacons targets {: start : end}))))
730+
(with-highlight-chores (fn [] (light-up-beacons targets {: start : end}))))
719731
; ---
720732
(fn loop [group-offset first-invoc?]
721733
(display group-offset)
@@ -780,7 +792,7 @@ is either labeled (C) or not (B).
780792
(fn display []
781793
(set-beacons targets {: no-labels? :aot? vars.aot? : user-given-targets?})
782794
(local (start end) (get-highlighted-idx-range targets no-labels?))
783-
(with-highlight-chores {: backdrop-ranges : hl-affected-windows} (fn [] (light-up-beacons targets {: start : end}))))
795+
(with-highlight-chores (fn [] (light-up-beacons targets {: start : end}))))
784796
; ---
785797
(fn get-new-idx [idx in]
786798
(if (contains? spec-keys.next_target in) (min (inc idx) (length targets))
@@ -817,6 +829,8 @@ is either labeled (C) or not (B).
817829

818830
(exec-user-autocmds :LeapEnter)
819831

832+
(prebeacon {: backdrop-ranges : hl-affected-windows})
833+
820834
(local (in1 ?in2) (if dot-repeat? (if state.dot_repeat.callback
821835
(values true true)
822836
(values state.dot_repeat.in1
@@ -892,7 +906,7 @@ is either labeled (C) or not (B).
892906
targets**
893907
; The action callback should expect a list in this case.
894908
; It might also get user input, so keep the beacons highlighted.
895-
(do (with-highlight-chores {: backdrop-ranges : hl-affected-windows} (fn [] (light-up-beacons targets**)))
909+
(do (with-highlight-chores (fn [] (light-up-beacons targets**)))
896910
(do-action targets**)))
897911
(exit))
898912

0 commit comments

Comments
 (0)