|
43 | 43 | (tm-define (inside-spell-buffer?) |
44 | 44 | (== (current-buffer) (spell-buffer))) |
45 | 45 |
|
| 46 | +(define inline-spell-underlines-serial 0) |
| 47 | +(define inline-spell-underlines-buffer #f) |
| 48 | + |
| 49 | +(define (inline-spell-current-word) |
| 50 | + (let* ((t (buffer-tree)) |
| 51 | + (p (tree->path t)) |
| 52 | + (lan (get-init "language")) |
| 53 | + (cp (cursor-path)) |
| 54 | + (pos (and cp (cDr cp)))) |
| 55 | + (if (and pos (list-starts? pos p)) |
| 56 | + (tree-spell-at lan t p (list-tail pos (length p)) 1000) |
| 57 | + (list)))) |
| 58 | + |
| 59 | +(define (inline-spell-underlines-active?) |
| 60 | + (and (current-view) |
| 61 | + (get-boolean-preference "spell underlines") |
| 62 | + (current-buffer) |
| 63 | + (not (inside-spell-buffer?)) |
| 64 | + (not (buffer-aux? (current-buffer))))) |
| 65 | + |
| 66 | +(define (clear-inline-spell-underlines) |
| 67 | + (set! inline-spell-underlines-serial (+ inline-spell-underlines-serial 1)) |
| 68 | + (set! inline-spell-underlines-buffer #f) |
| 69 | + (when (current-view) |
| 70 | + (clear-spell-errors))) |
| 71 | + |
| 72 | +(tm-define (inline-spell-underlines-refresh) |
| 73 | + (if (not (inline-spell-underlines-active?)) |
| 74 | + (clear-inline-spell-underlines) |
| 75 | + (let* ((t (buffer-tree)) |
| 76 | + (buf (current-buffer)) |
| 77 | + (p (tree->path t))) |
| 78 | + (when (not (== inline-spell-underlines-buffer buf)) |
| 79 | + (set! inline-spell-underlines-buffer buf)) |
| 80 | + (let ((sels (inline-spell-current-word))) |
| 81 | + (if (null? sels) |
| 82 | + (clear-spell-errors) |
| 83 | + (set-spell-errors sels)))))) |
| 84 | + |
| 85 | +(define (inline-spell-underlines-key? key) |
| 86 | + (or (in? key (list "space" "return" "tab" "backspace" "delete" |
| 87 | + "left" "right" "up" "down" |
| 88 | + "home" "end" "pageup" "pagedown")) |
| 89 | + (and (== (string-length key) 1) |
| 90 | + (not (or (char-alphabetic? (string-ref key 0)) |
| 91 | + (char-numeric? (string-ref key 0))))))) |
| 92 | + |
| 93 | +(define (inline-spell-underlines-typing-key? key) |
| 94 | + (and (== (string-length key) 1) |
| 95 | + (or (char-alphabetic? (string-ref key 0)) |
| 96 | + (char-numeric? (string-ref key 0))))) |
| 97 | + |
| 98 | +(define (schedule-inline-spell-underlines-after delay) |
| 99 | + (when (current-view) |
| 100 | + (set! inline-spell-underlines-serial (+ inline-spell-underlines-serial 1)) |
| 101 | + (let ((ticket inline-spell-underlines-serial) |
| 102 | + (buf (current-buffer))) |
| 103 | + (delayed |
| 104 | + (:idle delay) |
| 105 | + (when (and (== ticket inline-spell-underlines-serial) |
| 106 | + (== buf (current-buffer))) |
| 107 | + (inline-spell-underlines-refresh)))))) |
| 108 | + |
| 109 | +(define (schedule-inline-spell-underlines) |
| 110 | + (schedule-inline-spell-underlines-after 350)) |
| 111 | + |
| 112 | +(define (schedule-inline-spell-underlines-slow) |
| 113 | + (schedule-inline-spell-underlines-after 900)) |
| 114 | + |
| 115 | +(tm-define (inline-spell-underlines-preference-changed which val) |
| 116 | + (if (== val "on") |
| 117 | + (schedule-inline-spell-underlines) |
| 118 | + (clear-inline-spell-underlines))) |
| 119 | + |
46 | 120 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
47 | 121 | ;; Highlighting the spell results |
48 | 122 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
|
130 | 204 | (if (not p) lan |
131 | 205 | (tm->stree (tree-descendant-env bt (cDr p) "language" lan)))))) |
132 | 206 |
|
| 207 | +(define (inline-spell-selection-at-cursor) |
| 208 | + (and-with cp (cursor-path) |
| 209 | + (let loop ((sels (get-spell-errors))) |
| 210 | + (cond ((or (null? sels) (null? (cdr sels))) #f) |
| 211 | + ((and (path-less-eq? (car sels) cp) |
| 212 | + (path-less? cp (cadr sels))) |
| 213 | + (list (car sels) (cadr sels))) |
| 214 | + (else (loop (cddr sels))))))) |
| 215 | + |
| 216 | +(define (inline-spell-get-language sel) |
| 217 | + (let* ((bt (buffer-tree)) |
| 218 | + (rp (tree->path bt)) |
| 219 | + (sp (car sel)) |
| 220 | + (p (and (list-starts? sp rp) (sublist sp (length rp) (length sp)))) |
| 221 | + (lan (get-init "language"))) |
| 222 | + (if (not p) lan |
| 223 | + (tm->stree (tree-descendant-env bt (cDr p) "language" lan))))) |
| 224 | + |
| 225 | +(define (inline-spell-suggestions sel) |
| 226 | + (and-with ss (selection->string sel) |
| 227 | + (let* ((lan (inline-spell-get-language sel)) |
| 228 | + (st (tm->stree (spell-check lan ss))) |
| 229 | + (l0 (if (tm-func? st 'tuple) (cdr st) (list))) |
| 230 | + (l1 (if (null? l0) l0 (cdr l0)))) |
| 231 | + (if (<= (length l1) 5) l1 (sublist l1 0 5))))) |
| 232 | + |
| 233 | +(define (inline-spell-suggestions-at-cursor) |
| 234 | + (and-with sel (inline-spell-selection-at-cursor) |
| 235 | + (inline-spell-suggestions sel))) |
| 236 | + |
| 237 | +(define (inline-spell-toolbar-open sel) |
| 238 | + (let* ((u (current-buffer)) |
| 239 | + (aux (spell-buffer))) |
| 240 | + (when (not toolbar-spell-active?) |
| 241 | + (multi-spell-start) |
| 242 | + (set! toolbar-spell-active? #t) |
| 243 | + (set! spell-focus-hack? #t) |
| 244 | + (set! spell-correct-string "") |
| 245 | + (set! spell-suggestions (list)) |
| 246 | + (set! spell-corrected 0) |
| 247 | + (set! spell-accepted 0) |
| 248 | + (set! spell-inserted 0) |
| 249 | + (update-bottom-tools)) |
| 250 | + (buffer-set-body aux `(document "")) |
| 251 | + (buffer-set-master aux u) |
| 252 | + (set! spell-window (current-window)) |
| 253 | + (set-alt-selection "alternate" (get-spell-errors)) |
| 254 | + (set-spell-reference (car sel)) |
| 255 | + (spell-focus-on sel))) |
| 256 | + |
| 257 | +(tm-define (inline-spell-show-toolbar-at-cursor) |
| 258 | + (and-with sel (inline-spell-selection-at-cursor) |
| 259 | + (inline-spell-toolbar-open sel))) |
| 260 | + |
133 | 261 | (define (spell-focus-on sel) |
134 | 262 | ;;(display* "spell-focus-on " sel "\n") |
135 | 263 | (selection-set-range-set sel) |
|
170 | 298 | (when (nin? key (list "pageup" "pagedown" "home" "end")) |
171 | 299 | (former key time))) |
172 | 300 |
|
| 301 | +(tm-define (keyboard-press key time) |
| 302 | + (:require (get-boolean-preference "spell underlines")) |
| 303 | + (former key time) |
| 304 | + (cond ((inline-spell-underlines-key? key) |
| 305 | + (schedule-inline-spell-underlines)) |
| 306 | + ((inline-spell-underlines-typing-key? key) |
| 307 | + (schedule-inline-spell-underlines-slow)))) |
| 308 | + |
| 309 | +(tm-define (keyboard-focus has-focus? time) |
| 310 | + (:require (get-boolean-preference "spell underlines")) |
| 311 | + (former has-focus? time) |
| 312 | + (when has-focus? |
| 313 | + (schedule-inline-spell-underlines))) |
| 314 | + |
| 315 | +(tm-define (mouse-event key x y mods time data) |
| 316 | + (:require (get-boolean-preference "spell underlines")) |
| 317 | + (former key x y mods time data) |
| 318 | + (when (== key "release-left") |
| 319 | + (inline-spell-underlines-refresh) |
| 320 | + (inline-spell-show-toolbar-at-cursor))) |
| 321 | + |
173 | 322 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
174 | 323 | ;; Highlighting a particular next or previous spell result |
175 | 324 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
|
0 commit comments