Skip to content

Instantly share code, notes, and snippets.

@Metaxal
Last active November 10, 2021 12:40
Show Gist options
  • Save Metaxal/c328dca7849018388f792094f8e0895c to your computer and use it in GitHub Desktop.
Save Metaxal/c328dca7849018388f792094f8e0895c to your computer and use it in GitHub Desktop.
Quickscript to turn latex-like strings into unicode symbols
#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"))
@Metaxal
Copy link
Author

Metaxal commented Aug 8, 2021

Revision 5 improvements:

  • uses the text-block table of symbols (which is bigger than DrRacket's default one)
  • uses backward-word instead of backward-sexp so as to work in comments too

@Metaxal
Copy link
Author

Metaxal commented Nov 4, 2021

Now with a search-list-box! (raco pkg install search-list-box)
Screenshot from 2021-11-04 08-28-01

@Metaxal
Copy link
Author

Metaxal commented Nov 10, 2021

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