From cc1ccc0cddf6d37df473121c25d7d1692c205cae Mon Sep 17 00:00:00 2001 From: Gregory Heytings Date: Wed, 3 Nov 2021 14:25:46 +0000 Subject: [PATCH] Make bidi reordering characters visible * lisp/progmodes/prog-mode.el (bidi-reordering-characters-visible, bidi-reordering-characters-fontify, bidi-reordering-characters-toggle-visibility): New functions. --- lisp/progmodes/prog-mode.el | 41 ++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/lisp/progmodes/prog-mode.el b/lisp/progmodes/prog-mode.el index db350a5f70..0ac5b53f72 100644 --- a/lisp/progmodes/prog-mode.el +++ b/lisp/progmodes/prog-mode.el @@ -289,6 +289,44 @@ turn-on-prettify-symbols-mode (local-variable-p 'prettify-symbols-alist)) (prettify-symbols-mode 1))) +(defvar-local bidi-reordering-characters-visible nil) + +(defun bidi-reordering-characters-fontify () + (font-lock-add-keywords nil '(("⁩\\|‬\\|⁨\\|⁧\\|⁦\\|‫\\|‪\\|‮\\|‭" . 'font-lock-warning-face)))) + +(defun bidi-reordering-characters-visible () + (setq buffer-display-table (or buffer-display-table + standard-display-table + (make-display-table))) + (bidi-reordering-character-toggle-visibility) + (add-hook 'font-lock-mode-hook #'bidi-reordering-characters-fontify)) + +;;;###autoload +(defun bidi-reordering-character-toggle-visibility () + (interactive) + (setq bidi-reordering-characters-visible + (not bidi-reordering-characters-visible)) + (when bidi-reordering-characters-visible + (aset buffer-display-table ?‪ [?→]) + (aset buffer-display-table ?‫ [?←]) + (aset buffer-display-table ?‭ [?→]) + (aset buffer-display-table ?‮ [?←]) + (aset buffer-display-table ?⁦ [?→]) + (aset buffer-display-table ?⁧ [?←]) + (aset buffer-display-table ?⁨ [?↓]) + (aset buffer-display-table ?‬ [?↑]) + (aset buffer-display-table ?⁩ [?↑])) + (unless bidi-reordering-characters-visible + (aset buffer-display-table ?‪ nil) + (aset buffer-display-table ?‫ nil) + (aset buffer-display-table ?‭ nil) + (aset buffer-display-table ?‮ nil) + (aset buffer-display-table ?⁦ nil) + (aset buffer-display-table ?⁧ nil) + (aset buffer-display-table ?⁨ nil) + (aset buffer-display-table ?‬ nil) + (aset buffer-display-table ?⁩ nil))) + ;;;###autoload (define-globalized-minor-mode global-prettify-symbols-mode prettify-symbols-mode turn-on-prettify-symbols-mode) @@ -300,7 +338,8 @@ prog-mode (setq-local parse-sexp-ignore-comments t) (add-hook 'context-menu-functions 'prog-context-menu 10 t) ;; Any programming language is always written left to right. - (setq bidi-paragraph-direction 'left-to-right)) + (setq bidi-paragraph-direction 'left-to-right) + (bidi-reordering-characters-visible)) (provide 'prog-mode) -- 2.33.0