Skip to content

Commit eba5441

Browse files
authored
Merge pull request #429 from Chris00/prettify
Enable the use of prettify-symbols-mode
2 parents 65f9627 + f14fa86 commit eba5441

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,21 @@ on save:
129129
(setq rust-format-on-save t)
130130
```
131131

132+
### Prettifying
133+
134+
You can toggle prettification of your code by running `M-x
135+
prettify-symbols-mode`. If you'd like to automatically enable this
136+
for all rust files, add the following to your init.el.
137+
138+
```elisp
139+
(add-hook 'rust-mode-hook
140+
(lambda () (prettify-symbols-mode)))
141+
```
142+
143+
You can add your own prettifications to `rust-prettify-symbols-alist`.
144+
For example, to display `x.add(y)` as `x∔(y)`, simply add to your init
145+
file `(push '(".add" . ?∔) rust-prettify-symbols-alist)`.
146+
132147
### Running / testing / compiling code
133148

134149
The `rust-run`, `rust-test`, `rust-compile` and `rust-check` functions

rust-mode.el

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ This variable might soon be remove again.")
4141
:type 'function
4242
:group 'rust-mode)
4343

44+
(defvar rust-prettify-symbols-alist
45+
'(("&&" . ?∧) ("||" . ?∨)
46+
("<=" . ?≤) (">=" . ?≥) ("!=" . ?≠)
47+
("INFINITY" . ?∞) ("->" . ?→) ("=>" . ?⇒))
48+
"Alist of symbol prettifications used for `prettify-symbols-alist'.")
49+
4450
;;; Customization
4551

4652
(defgroup rust-mode nil
@@ -209,6 +215,20 @@ Use idomenu (imenu with `ido-mode') for best mileage.")
209215
table)
210216
"Syntax definitions and helpers.")
211217

218+
;;; Prettify
219+
220+
(defun rust--prettify-symbols-compose-p (start end match)
221+
"Return true iff the symbol MATCH should be composed.
222+
See `prettify-symbols-compose-predicate'."
223+
(and (fboundp 'prettify-symbols-default-compose-p)
224+
(prettify-symbols-default-compose-p start end match)
225+
;; Make sure there is a space before || as it is also used for
226+
;; functions with 0 arguments.
227+
(not (and (string= match "||")
228+
(save-excursion
229+
(goto-char start)
230+
(looking-back "\\(?:\\<move\\|=\\) *"))))))
231+
212232
;;; Mode
213233

214234
(defvar rust-mode-map
@@ -278,6 +298,9 @@ Use idomenu (imenu with `ido-mode') for best mileage.")
278298
(setq-local electric-pair-inhibit-predicate
279299
'rust-electric-pair-inhibit-predicate-wrap)
280300
(setq-local electric-pair-skip-self 'rust-electric-pair-skip-self-wrap)
301+
;; Configure prettify
302+
(setq prettify-symbols-alist rust-prettify-symbols-alist)
303+
(setq prettify-symbols-compose-predicate #'rust--prettify-symbols-compose-p)
281304

282305
(add-hook 'before-save-hook rust-before-save-hook nil t)
283306
(add-hook 'after-save-hook rust-after-save-hook nil t))

0 commit comments

Comments
 (0)