From 121a5abeaee85e7955786d838f07103ce074a63b Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Sun, 3 Oct 2021 20:35:49 +0300 Subject: [PATCH] Move context-menu selection items Defun/List/Symbol to prog-mode (bug#9054) * lisp/mouse.el (context-menu-functions): Add context-menu-middle-separator to choices. (context-menu-region): Move Defun/List/Symbol selection items to prog-context-menu. * lisp/progmodes/prog-mode.el (prog-context-menu): Move Defun/List/Symbol selection items from context-menu-region. Include text-mode select menu only in strings and comments. * lisp/textmodes/text-mode.el (text-mode-menu): New function. (text-mode): Add text-mode-menu to context-menu-functions. --- lisp/mouse.el | 13 +------------ lisp/progmodes/prog-mode.el | 22 ++++++++++++++++++++++ lisp/textmodes/text-mode.el | 25 ++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/lisp/mouse.el b/lisp/mouse.el index 5d4e05fa25e..bb47d04a3a8 100644 --- a/lisp/mouse.el +++ b/lisp/mouse.el @@ -290,6 +290,7 @@ and should return the same menu with changes such as added new menu items." :type '(repeat (choice (function-item context-menu-undo) (function-item context-menu-region) + (function-item context-menu-middle-separator) (function-item context-menu-toolbar) (function-item context-menu-global) (function-item context-menu-local) @@ -478,14 +479,6 @@ Some context functions add menu items below the separator." `(menu-item "All" ,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'buffer)) :help "Mark the whole buffer for a subsequent cut/copy")) - (define-key-after submenu [mark-defun] - `(menu-item "Defun" - ,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'defun)) - :help "Mark the defun at click for a subsequent cut/copy")) - (define-key-after submenu [mark-list] - `(menu-item "List" - ,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'list)) - :help "Mark the list at click for a subsequent cut/copy")) (when (let* ((pos (posn-point (event-end click))) (char (when pos (char-after pos)))) (or (and char (eq (char-syntax char) ?\")) @@ -498,10 +491,6 @@ Some context functions add menu items below the separator." `(menu-item "Line" ,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'line)) :help "Mark the line at click for a subsequent cut/copy")) - (define-key-after submenu [mark-symbol] - `(menu-item "Symbol" - ,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'symbol)) - :help "Mark the symbol at click for a subsequent cut/copy")) (when (region-active-p) (define-key-after submenu [mark-none] `(menu-item "None" diff --git a/lisp/progmodes/prog-mode.el b/lisp/progmodes/prog-mode.el index 6c09dcf881d..4f15686dc87 100644 --- a/lisp/progmodes/prog-mode.el +++ b/lisp/progmodes/prog-mode.el @@ -68,6 +68,28 @@ `(menu-item "Find Definition" xref-find-definitions-at-mouse :help ,(format "Find definition of `%s'" identifier)) 'prog-separator))) + + (when (thing-at-mouse click 'symbol) + (define-key-after menu [select-region mark-symbol] + `(menu-item "Symbol" + ,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'symbol)) + :help "Mark the symbol at click for a subsequent cut/copy") + 'mark-whole-buffer)) + (define-key-after menu [select-region mark-list] + `(menu-item "List" + ,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'list)) + :help "Mark the list at click for a subsequent cut/copy") + 'mark-whole-buffer) + (define-key-after menu [select-region mark-defun] + `(menu-item "Defun" + ,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'defun)) + :help "Mark the defun at click for a subsequent cut/copy") + 'mark-whole-buffer) + + ;; Include text-mode select menu only in strings and comments. + (when (nth 8 (save-excursion (syntax-ppss (posn-point (event-end click))))) + (text-mode-menu menu click)) + menu) (defvar prog-mode-map diff --git a/lisp/textmodes/text-mode.el b/lisp/textmodes/text-mode.el index 74c6d412a65..3243bd31c4c 100644 --- a/lisp/textmodes/text-mode.el +++ b/lisp/textmodes/text-mode.el @@ -95,6 +95,28 @@ inherit all the commands defined in this map.") :style toggle :selected (memq 'turn-on-auto-fill text-mode-hook)])) +(defun text-mode-menu (menu click) + "Populate MENU with text selection commands at CLICK." + + (when (thing-at-mouse click 'word) + (define-key-after menu [select-region mark-word] + `(menu-item "Word" + ,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'word)) + :help "Mark the word at click for a subsequent cut/copy") + 'mark-whole-buffer)) + (define-key-after menu [select-region mark-sentence] + `(menu-item "Sentence" + ,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'sentence)) + :help "Mark the sentence at click for a subsequent cut/copy") + 'mark-whole-buffer) + (define-key-after menu [select-region mark-paragraph] + `(menu-item "Paragraph" + ,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'paragraph)) + :help "Mark the paragraph at click for a subsequent cut/copy") + 'mark-whole-buffer) + + menu) + (define-derived-mode text-mode nil "Text" "Major mode for editing text written for humans to read. @@ -104,7 +126,8 @@ You can thus get the full benefit of adaptive filling \\{text-mode-map} Turning on Text mode runs the normal hook `text-mode-hook'." (setq-local text-mode-variant t) - (setq-local require-final-newline mode-require-final-newline)) + (setq-local require-final-newline mode-require-final-newline) + (add-hook 'context-menu-functions 'text-mode-menu 10 t)) (define-derived-mode paragraph-indent-text-mode text-mode "Parindent" "Major mode for editing text, with leading spaces starting a paragraph. -- 2.39.5