Last active
November 10, 2021 12:40
-
-
Save Metaxal/c328dca7849018388f792094f8e0895c to your computer and use it in GitHub Desktop.
Quickscript to turn latex-like strings into unicode symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
;;; Author: Laurent Orseau https://github.com/Metaxal | |
;;; License: [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) or | |
;;; [MIT license](http://opensource.org/licenses/MIT) at your option. | |
(require racket/dict | |
racket/class | |
(submod text-block/symbols codes) | |
quickscript | |
racket/gui/base | |
search-list-box) | |
;;; Turns latin strings into unicode characters. | |
;;; Requires the text-block package (which contains the table of symbols) | |
;;; and the search-list-box package (for the gui choice): | |
;;; | |
;;; raco pkg install text-block search-list-box | |
(script-help-string "Converts the selected string (or the backward word) to a unicode character") | |
(define (string->unicode str) | |
(dict-ref codes str str)) | |
(define-script ->unicode | |
#:label "->unicode" | |
#:menu-path ("Sele&ction") | |
#:shortcut #\space | |
#:shortcut-prefix (ctl) | |
(λ (str #:editor ed) | |
(cond | |
[(string=? str "") | |
(define pos (send ed get-start-position)) | |
(send ed move-position 'left #t 'word) ; select backward word | |
(define selected (send ed get-text (send ed get-start-position) (send ed get-end-position))) | |
(define new-str (dict-ref codes selected #f)) | |
(unless new-str (send ed set-position pos)) | |
new-str] | |
[else (dict-ref codes str #f)]))) | |
(define-script insert-symbol | |
#:label "Insert unicode (gui)" | |
#:menu-path ("Sele&ction") | |
(λ (str #:editor ed #:frame drfr) | |
(define slbf | |
(new search-list-box-frame% | |
[parent drfr] | |
[label "Insert unicode symbol"] | |
[contents (hash->list codes)] | |
[key (λ (p) (format "~a\t~a" (cdr p) (car p)))] | |
[show? #false] | |
[callback (λ (idx label content) | |
(send ed insert (cdr content)) | |
(when (send cb get-value) | |
(send slbf show #false)))])) | |
(define cb (new check-box% [parent slbf] [label "Close on select"] [value #true])) | |
(send slbf center) | |
(send slbf show #true))) | |
(module url2script-info racket/base | |
(provide filename url) | |
(define filename "string2unicode.rkt") | |
(define url "https://gist.github.com/Metaxal/c328dca7849018388f792094f8e0895c")) |
New: the keyboard shortcut is ctrl-space
, which you may want to change if you normally use it for Emacs-like selection.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revision 5 improvements:
text-block
table of symbols (which is bigger than DrRacket's default one)