branch: elpa/flycheck
commit 52b4a924cddc523dc3e9da755010e8c75d150954
Author: Bozhidar Batsov <[email protected]>
Commit: Bozhidar Batsov <[email protected]>

    [Fix #2401] Show the Flycheck menu in the context menu
    
    context-menu-minor builds the minor mode part of the context menu from
    the modes' menu bar bindings, and Flycheck's is a bare keymap, which no
    menu displays.  That's what keeps it off the menu bar, where it would
    duplicate Tools > Syntax Checking, so leave it, and swap the copy in the
    context menu for a menu item in its place.  A context menu without
    context-menu-minor gets nothing, as with other minor modes.
---
 CHANGELOG.md                     |  1 +
 flycheck.el                      | 22 ++++++++++++++++++----
 test/specs/test-flycheck-mode.el | 35 ++++++++++++++++++++++++++++++++++-
 3 files changed, 53 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 826545f341..1e5ac6a531 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -36,6 +36,7 @@
 
 ### Bugs fixed
 
+- [#2405](https://github.com/flycheck/flycheck/pull/2405): The Flycheck menu 
shows up in the context menu of `context-menu-mode` 
([#2401](https://github.com/flycheck/flycheck/issues/2401)).
 - [#2406](https://github.com/flycheck/flycheck/pull/2406): Stop the `*eldoc*` 
window popping open with an eager `eldoc-documentation-strategy`, such as 
`eldoc-documentation-compose-eagerly`, which displays again each time a source 
answers ([#2387](https://github.com/flycheck/flycheck/issues/2387)).
 - [#2302](https://github.com/flycheck/flycheck/pull/2302): `proselint` errors 
no longer extend one character past the text they flag with proselint 0.14.
 - [#2302](https://github.com/flycheck/flycheck/pull/2302): `proselint` detects 
the tool's version from what `proselint --version` prints rather than whether 
it succeeds, so a development build of proselint, which accepts the option 
again, is no longer run with the arguments of 0.14.
diff --git a/flycheck.el b/flycheck.el
index 2afcdb3338..72db2f5ea8 100644
--- a/flycheck.el
+++ b/flycheck.el
@@ -3389,14 +3389,25 @@ Slots:
 (defvar flycheck-mode-map
   (let ((map (make-sparse-keymap)))
     (define-key map flycheck-keymap-prefix flycheck-command-map)
-    ;; We place the menu under a custom menu key.  Since this menu key is not
-    ;; present in the menu of the global map, no top-level menu entry is added
-    ;; to the global menu bar.  However, it still appears on the mode line
-    ;; lighter.
+    ;; A bare keymap rather than a menu item, which keeps the menu off the
+    ;; menu bar (it lives under Tools), but not off the mode line lighter
     (define-key map [menu-bar flycheck] flycheck-mode-menu-map)
     map)
   "Keymap of command `flycheck-mode'.")
 
+(defun flycheck-context-menu (menu _click)
+  "Make the Flycheck menu show in the context MENU.
+
+Flycheck mode adds this to `context-menu-functions', after
+`context-menu-minor'.  That copies the menu from `flycheck-mode-map',
+where it is a bare keymap rather than a menu item, which no menu
+displays.  Replace the copy with one that shows, where it is."
+  (when (lookup-key menu [flycheck])
+    (define-key menu [flycheck]
+      `(menu-item ,(keymap-prompt flycheck-mode-menu-map)
+                  ,flycheck-mode-menu-map)))
+  menu)
+
 (defvar-local flycheck-old-next-error-function nil
   "Remember the old `next-error-function'.")
 
@@ -3502,6 +3513,8 @@ ARG is ‘toggle’; disable the mode otherwise."
 
     (pcase-dolist (`(,hook . ,fn) (reverse flycheck-hooks-alist))
       (add-hook hook fn nil 'local))
+    ;; After `context-menu-minor', whose entry for Flycheck it replaces
+    (add-hook 'context-menu-functions #'flycheck-context-menu 90 'local)
 
     (setq flycheck-old-next-error-function
           (if flycheck-standard-error-navigation
@@ -3524,6 +3537,7 @@ ARG is ‘toggle’; disable the mode otherwise."
 
     (pcase-dolist (`(,hook . ,fn) flycheck-hooks-alist)
       (remove-hook hook fn 'local))
+    (remove-hook 'context-menu-functions #'flycheck-context-menu 'local)
 
     (flycheck-teardown))))
 
diff --git a/test/specs/test-flycheck-mode.el b/test/specs/test-flycheck-mode.el
index 45f4681cca..2ac10dc520 100644
--- a/test/specs/test-flycheck-mode.el
+++ b/test/specs/test-flycheck-mode.el
@@ -64,7 +64,40 @@
         (let ((hack-local-variables-hook))
           (revert-buffer 'ignore-auto 'no-confirm))
         (expect flycheck-current-errors :not :to-be-truthy)
-        (expect (flycheck-deferred-check-p) :not :to-be-truthy)))))
+        (expect (flycheck-deferred-check-p) :not :to-be-truthy))))
+
+  (describe "the context menu"
+    (it "offers the Flycheck menu while the mode is on"
+      (flycheck-buttercup-with-temp-buffer
+        (emacs-lisp-mode)
+        (flycheck-mode 1)
+        ;; The raw entry, as `lookup-key' would look through `menu-item'.
+        ;; A bare keymap, which `context-menu-minor' copies from the mode
+        ;; map, is never shown (#2401).
+        (let* ((menu (cdr (context-menu-map)))
+               (keys (mapcar #'car-safe menu))
+               (entry (cdr (assq 'flycheck menu))))
+          (expect (car-safe entry) :to-be 'menu-item)
+          (expect (nth 1 entry) :to-equal "Syntax Checking")
+          (expect (nth 2 entry) :to-be flycheck-mode-menu-map)
+          ;; In place of the copy, among the other minor modes' menus
+          (expect (seq-count (lambda (key) (eq key 'flycheck)) keys)
+                  :to-equal 1)
+          (expect (cadr (memq 'separator-minor keys)) :to-be 'flycheck))
+        (flycheck-mode -1)
+        (expect (assq 'flycheck (cdr (context-menu-map))) :to-be nil)))
+
+    (it "leaves the menu out where minor mode menus are"
+      (flycheck-buttercup-with-temp-buffer
+        (emacs-lisp-mode)
+        (flycheck-mode 1)
+        ;; The global value: Flycheck's function is in the local one
+        (cl-letf (((default-value 'context-menu-functions)
+                   (remq 'context-menu-minor
+                         (default-value 'context-menu-functions))))
+          (expect (memq 'flycheck-context-menu context-menu-functions)
+                  :to-be-truthy)
+          (expect (assq 'flycheck (cdr (context-menu-map))) :to-be nil))))))
 
 (describe "Syntax Checker Selection"
 

Reply via email to