Skip to content

Commit

Permalink
Add abbrev-mode support, plus an insert-symbol command
Browse files Browse the repository at this point in the history
Intended to replace racket-unicode-input-method (which would remain
but become deprecated), which I'd originally cargo-culted from
haskell-mode. On a fresh look, ,I don't see why this kind of feature
should be implemented as an input method. Instead these can be seen
simply as abbreviations: When I type X, substitute it with Y.

Although I haven't used abbrev-mode previously, and it seems like a
fairly ancient Emacs feature, the basic functionality seems fine.

A major mode can specify a set of "system" abbrevs, which is what we
do here.

Allow customization of:

- the list of substitutions

- an optional prefix (such as "\\"), which can avoid unintended
  substitutions

Both of which should mean users don't need to open an issue about
changing some particular substitution, as has happened a couple times
recently. IOW this is an effort to solve this generally, not ad hoc,
by making it super easy for users to self-help.
  • Loading branch information
greghendershott committed Oct 13, 2024
1 parent ec8b514 commit 2f48990
Show file tree
Hide file tree
Showing 9 changed files with 452 additions and 90 deletions.
4 changes: 2 additions & 2 deletions doc/generate.el
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
(require 'racket-xp)
(require 'racket-util)
(require 'racket-show)
(require 'racket-unicode-input-method)
(require 'racket-smart-open)
(require 'racket-repl-buffer-name)
(require 'racket-hash-lang)
Expand Down Expand Up @@ -49,7 +48,6 @@
racket-insert-closing
racket-cycle-paren-shapes
racket-backward-up-list
racket-unicode-input-method-enable
racket-align
racket-unalign
racket-complete-at-point
Expand Down Expand Up @@ -198,6 +196,8 @@
racket-xp-eldoc-level
racket-documentation-search-location
racket-expand-hiding
racket-abbrevs
racket-abbrev-prefix
"Hash lang variables"
racket-hash-lang-token-face-alist
racket-hash-lang-pairs
Expand Down
21 changes: 15 additions & 6 deletions doc/racket-mode.org
Original file line number Diff line number Diff line change
Expand Up @@ -382,20 +382,29 @@ Note: Racket Mode does not support the "old" eldoc API that uses ~eldoc-document

You can use {{{ref(racket-mode-start-faster)}}} to make the Racket REPL start faster.

** Unicode input method
** Math and programming symbols

An optional Emacs input method, ~racket-unicode~, lets you easily type various Unicode symbols that might be useful when writing Racket code.
Various modes supply an Eamcs abbrev table which you can use by enabling the minor mode ~abbrev-mode~.

To automatically enable the ~racket-unicode~ input method in ~racket-mode~ and ~racket-repl-mode~ buffers, put the following code in your Emacs init file:
#+BEGIN_SRC lisp
(add-hook 'racket-mode-hook #'abbrev-mode)
(add-hook 'racket-repl-mode-hook #'abbrev-mode)
#+END_SRC

This abbrev table is populated from the customization variables {{{ref(racket-abbrevs)}}} and {{{ref(racket-abbrev-prefix)}}}.

By default this table defines names for various math and programming symbols, and uses a {{{kbd(\)}}} prefix key. So for example you can type ~\gg~ followed by a space, and it will change to ~≫~.

*** Deprecated racket-unicode input method

Older versions supplied an Emacs input method, ~racket-unicode~. This is now deprecated. If you have configuration like this:

#+BEGIN_SRC lisp
(add-hook 'racket-mode-hook #'racket-unicode-input-method-enable)
(add-hook 'racket-repl-mode-hook #'racket-unicode-input-method-enable)
#+END_SRC

{{{see(racket-unicode-input-method-enable)}}}.

{{{see(racket-insert-lambda)}}}.
You can delete that and instead use the ~abbrev-mode~ configuration described above.

** Ligatures

Expand Down
145 changes: 68 additions & 77 deletions doc/racket-mode.texi
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ Configure
* Edit buffers and REPL buffers::
* eldoc::
* Start faster::
* Unicode input method::
* Math and programming symbols::
* Ligatures::
Math and programming symbols
* Deprecated racket-unicode input method::
Commands
* Edit::
Expand Down Expand Up @@ -108,7 +112,6 @@ Edit
* racket-insert-closing::
* racket-cycle-paren-shapes::
* racket-backward-up-list::
* racket-unicode-input-method-enable::
* racket-align::
* racket-unalign::
* racket-complete-at-point::
Expand Down Expand Up @@ -218,6 +221,8 @@ General variables
* racket-xp-eldoc-level::
* racket-documentation-search-location::
* racket-expand-hiding::
* racket-abbrevs::
* racket-abbrev-prefix::
Hash lang variables
Expand Down Expand Up @@ -557,7 +562,7 @@ You can @code{setq} this directly in your Emacs init file (@samp{~/.emacs} or @s
* Edit buffers and REPL buffers::
* eldoc::
* Start faster::
* Unicode input method::
* Math and programming symbols::
* Ligatures::
@end menu

Expand Down Expand Up @@ -844,21 +849,35 @@ Note: Racket Mode does not support the ``old'' eldoc API that uses @code{eldoc-d

You can use @ref{racket-mode-start-faster} to make the Racket REPL start faster.

@node Unicode input method
@section Unicode input method
@node Math and programming symbols
@section Math and programming symbols

Various modes supply an Eamcs abbrev table which you can use by enabling the minor mode @code{abbrev-mode}.

@lisp
(add-hook 'racket-mode-hook #'abbrev-mode)
(add-hook 'racket-repl-mode-hook #'abbrev-mode)
@end lisp

This abbrev table is populated from the customization variables @ref{racket-abbrevs} and @ref{racket-abbrev-prefix}.

By default this table defines names for various math and programming symbols, and uses a @kbd{\} prefix key. So for example you can type @code{\gg} followed by a space, and it will change to @code{}.

@menu
* Deprecated racket-unicode input method::
@end menu

An optional Emacs input method, @code{racket-unicode}, lets you easily type various Unicode symbols that might be useful when writing Racket code.
@node Deprecated racket-unicode input method
@subsection Deprecated racket-unicode input method

To automatically enable the @code{racket-unicode} input method in @code{racket-mode} and @code{racket-repl-mode} buffers, put the following code in your Emacs init file:
Older versions supplied an Emacs input method, @code{racket-unicode}. This is now deprecated. If you have configuration like this:

@lisp
(add-hook 'racket-mode-hook #'racket-unicode-input-method-enable)
(add-hook 'racket-repl-mode-hook #'racket-unicode-input-method-enable)
@end lisp

@xref{racket-unicode-input-method-enable}.

@xref{racket-insert-lambda}.
You can delete that and instead use the @code{abbrev-mode} configuration described above.

@node Ligatures
@section Ligatures
Expand Down Expand Up @@ -951,7 +970,6 @@ You can also view these by using the normal Emacs help mechanism:
* racket-insert-closing::
* racket-cycle-paren-shapes::
* racket-backward-up-list::
* racket-unicode-input-method-enable::
* racket-align::
* racket-unalign::
* racket-complete-at-point::
Expand Down Expand Up @@ -1034,7 +1052,12 @@ penultimate step during initialization.

Insert λ.

To insert Unicode symbols generally, see @ref{racket-unicode-input-method-enable}.
To insert Unicode symbols generally, see the command
@code{racket-insert-symbol}.

Also you can enable @code{abbrev-mode} to type abbreviations in the
buffer that are automatically substituted. For more information
see the customization variable @ref{racket-abbrevs}.

@node racket-fold-all-tests
@subsection racket-fold-all-tests
Expand Down Expand Up @@ -1390,71 +1413,6 @@ especially not repeatedly. Instead, initially use
string or comment, if any, then use normal @code{backward-up-list}
repeatedly.

@node racket-unicode-input-method-enable
@subsection racket-unicode-input-method-enable

@kbd{M-x} @code{racket-unicode-input-method-enable}

Set input method to racket-unicode.

The racket-unicode input method lets you easily type various
Unicode symbols that might be useful when writing Racket code.

To automatically enable the racket-unicode input method in
racket-mode and racket-repl-mode buffers, put the following code
in your Emacs init file:

@lisp
(add-hook 'racket-mode-hook #'racket-unicode-input-method-enable)
(add-hook 'racket-repl-mode-hook #'racket-unicode-input-method-enable)
@end lisp

To temporarily enable this input method for a single buffer you
can use ``M-x racket-unicode-input-method-enable''.

Use the standard Emacs key C-\ to toggle the input method.

When the racket-unicode input method is active, you can for
example type ``All'' and it is immediately replaced with ``∀''. A
few other examples:

@multitable {aaaaaaaaa} {aaaaaaaaaaaaaaaaaaaaaaaa}
@item omega
@tab ω
@item x@math{_1}
@tab x₁
@item x@math{^1}
@tab
@item A
@tab 𝔸
@item test-->>E
@tab test-->>∃ (racket/redex)
@item vdash
@tab
@end multitable

To see a table of all key sequences use ``M-x
describe-input-method <RET> racket-unicode''.

If you want to add your own mappings to the ``racket-unicode''
input method, you may add code like the following example in your
Emacs init file:

@lisp
;; Either (require 'racket-mode) here, or, if you use
;; use-package, put the code below in the :config section.
(with-temp-buffer
(racket-unicode-input-method-enable)
(set-input-method "racket-unicode")
(let ((quail-current-package (assoc "racket-unicode"
quail-package-alist)))
(quail-define-rules ((append . t))
("^o" [""]))))
@end lisp

If you don’t like the highlighting of partially matching tokens you
can turn it off by setting @code{input-method-highlight-flag} to nil.

@node racket-align
@subsection racket-align

Expand Down Expand Up @@ -3111,6 +3069,8 @@ Delete the ``compiled'' directories made by @ref{racket-mode-start-faster}.
* racket-xp-eldoc-level::
* racket-documentation-search-location::
* racket-expand-hiding::
* racket-abbrevs::
* racket-abbrev-prefix::
@end menu

@node racket-program
Expand Down Expand Up @@ -3300,6 +3260,37 @@ string should be a properly encoded URL@.

The macro hiding policy for commands like @ref{racket-expand-file}.

@node racket-abbrevs
@subsection racket-abbrevs

List of abbreviations and expansions.

Used by @code{abbrev-mode} when that is enabled, as well as by the
command @code{racket-insert-symbol}.

By default, the abbreviations are names for expansions that use
various Unicode symbols.

The abbreviations in this list do @emph{not} include
@ref{racket-abbrev-prefix}, which you can configure separately.

If you change this using @code{setq} or @code{add-to-list} -- instead of
@code{setopt} or the customization UI -- be sure to call
@code{racket-abbrev-setup} afterwards.

@node racket-abbrev-prefix
@subsection racket-abbrev-prefix

A non-blank string prefix with which to start all abbrevs, or nil.

When not nil, this is automatically added to all the items in
@ref{racket-abbrevs}, to form the abbreviations used by
@code{abbrev-mode}.

If you change this using @code{setq} -- instead of @code{setopt} or the
customization UI -- be sure to call @code{racket-abbrev-setup}
afterwards.

@node Hash lang variables
@section Hash lang variables

Expand Down
Loading

0 comments on commit 2f48990

Please sign in to comment.