>From d45f4f8aafb3caaf411d8bb5e434f42bc2d5f56a Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Sat, 10 Mar 2018 18:12:55 -0500 Subject: [PATCH v1 2/2] Give suggestions on errors about var names with confusable chars Add some hints to the message for byte compiler 'free variable' warnings and 'void-variable' errors where the variable has confusable characters in it. * lisp/help.el (uni-confusables), uni-confusables-regexp): New constants. (help-command-error-confusable-suggestions): New function, added to `command-error-function'. (help-uni-confusable-suggestions): New function. * lisp/emacs-lisp/bytecomp.el (byte-compile-variable-ref): Use it. * lisp/emacs-lisp/lisp-mode.el (lisp--match-confusable-symbol-character): New function. (lisp-fdefs): Use it to fontify confusable characters with font-lock-warning-face when they occur in symbol names. --- lisp/emacs-lisp/bytecomp.el | 10 +++++++-- lisp/emacs-lisp/lisp-mode.el | 16 ++++++++++++++- lisp/help.el | 48 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el index 8b67e1007d..31d589ab02 100644 --- a/lisp/emacs-lisp/bytecomp.el +++ b/lisp/emacs-lisp/bytecomp.el @@ -3407,7 +3407,10 @@ byte-compile-variable-ref (boundp var) (memq var byte-compile-bound-variables) (memq var byte-compile-free-references)) - (byte-compile-warn "reference to free variable `%S'" var) + (let* ((varname (prin1-to-string var)) + (suggestions (help-uni-confusable-suggestions varname))) + (byte-compile-warn "reference to free variable `%s'%s" varname + (if suggestions (concat "\n " suggestions) ""))) (push var byte-compile-free-references)) (byte-compile-dynamic-variable-op 'byte-varref var)))) @@ -3423,7 +3426,10 @@ byte-compile-variable-set (boundp var) (memq var byte-compile-bound-variables) (memq var byte-compile-free-assignments)) - (byte-compile-warn "assignment to free variable `%s'" var) + (let* ((varname (prin1-to-string var)) + (suggestions (help-uni-confusable-suggestions varname))) + (byte-compile-warn "assignment to free variable `%s'%s" varname + (if suggestions (concat "\n " suggestions) ""))) (push var byte-compile-free-assignments)) (byte-compile-dynamic-variable-op 'byte-varset var)))) diff --git a/lisp/emacs-lisp/lisp-mode.el b/lisp/emacs-lisp/lisp-mode.el index f31b4d4dd2..87bdafa976 100644 --- a/lisp/emacs-lisp/lisp-mode.el +++ b/lisp/emacs-lisp/lisp-mode.el @@ -280,6 +280,17 @@ elisp--font-lock-backslash `(face ,font-lock-warning-face help-echo "This \\ has no effect")))) +(defun lisp--match-confusable-symbol-character (limit) + ;; Match a confusable character within a Lisp symbol. + (catch 'matched + (while t + (if (not (re-search-forward uni-confusables-regexp limit t)) + (throw 'matched nil) + ;; Skip confusables inside strings or comments. + (save-match-data + (if (not (nth 8 (syntax-ppss))) + (throw 'matched t))))))) + (let-when-compile ((lisp-fdefs '("defmacro" "defun")) (lisp-vdefs '("defvar")) @@ -463,7 +474,10 @@ elisp--font-lock-backslash (3 'font-lock-regexp-grouping-construct prepend)) (lisp--match-hidden-arg (0 '(face font-lock-warning-face - help-echo "Hidden behind deeper element; move to another line?"))) + help-echo "Hidden behind deeper element; move to another line?"))) + (lisp--match-confusable-symbol-character + 0 '(face font-lock-warning-face + help-echo "Confusable character")) )) "Gaudy level highlighting for Emacs Lisp mode.") diff --git a/lisp/help.el b/lisp/help.el index 6e06fc9e1c..66d191f863 100644 --- a/lisp/help.el +++ b/lisp/help.el @@ -1489,6 +1489,54 @@ help--make-usage-docstring (help--docstring-quote (format "%S" (help--make-usage fn arglist))))) + +;; Just some quote-like characters for now. TODO: generate this stuff +;; from official Unicode data. +(defconst uni-confusables + '((#x2018 . "'") ;; LEFT SINGLE QUOTATION MARK + (#x2019 . "'") ;; RIGHT SINGLE QUOTATION MARK + (#x201B . "'") ;; SINGLE HIGH-REVERSED-9 QUOTATION MARK + (#x201C . "\"") ;; LEFT DOUBLE QUOTATION MARK + (#x201D . "\"") ;; RIGHT DOUBLE QUOTATION MARK + (#x201F . "\"") ;; DOUBLE HIGH-REVERSED-9 QUOTATION MARK + (#x301E . "\"") ;; DOUBLE PRIME QUOTATION MARK + (#xFF02 . "'") ;; FULLWIDTH QUOTATION MARK + (#xFF07 . "'") ;; FULLWIDTH APOSTROPHE + )) + +(defconst uni-confusables-regexp + (concat "[" (mapcar #'car uni-confusables) "]")) + +(defun help-uni-confusable-suggestions (string) + "Return a message describing confusables in STRING." + (let ((i 0) + (confusables nil)) + (while (setq i (string-match uni-confusables-regexp string i)) + (let ((replacement (alist-get (aref string i) uni-confusables))) + (push (aref string i) confusables) + (setq string (replace-match replacement t t string)) + (setq i (+ i (length replacement))))) + (when confusables + (format-message + (if (> (length confusables) 1) + "Found confusable characters: %s; perhaps you meant: `%s'?" + "Found confusable character: %s, perhaps you meant: `%s'?") + (mapconcat (lambda (c) (format-message "`%c'" c)) + confusables ", ") + string)))) + +(defun help-command-error-confusable-suggestions (data _context _signal) + (pcase data + (`(void-variable ,var) + (princ (concat "\n " (help-uni-confusable-suggestions + (symbol-name var))) + t)) + (_ nil))) + +(add-function :after command-error-function + #'help-command-error-confusable-suggestions) + + (provide 'help) ;;; help.el ends here -- 2.11.0