From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.bugs Subject: bug#52973: Adding a few context-menu-mode commands Date: Tue, 04 Jan 2022 19:39:48 +0000 Message-ID: <87czl75lyz.fsf@posteo.net> References: <87o84tp69h.fsf@posteo.net> <868rvwsezi.fsf@mail.linkov.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="40296"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 52973@debbugs.gnu.org To: Juri Linkov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Jan 04 20:43:00 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n4phz-000AH3-VR for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 04 Jan 2022 20:43:00 +0100 Original-Received: from localhost ([::1]:34398 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n4phy-0000ov-5p for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 04 Jan 2022 14:42:58 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:35128) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4pg6-0007wE-BZ for bug-gnu-emacs@gnu.org; Tue, 04 Jan 2022 14:41:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:55456) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n4pg6-0004kR-1b for bug-gnu-emacs@gnu.org; Tue, 04 Jan 2022 14:41:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1n4pg6-0005FM-0e for bug-gnu-emacs@gnu.org; Tue, 04 Jan 2022 14:41:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Philip Kaludercic Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 04 Jan 2022 19:41:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 52973 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 52973-submit@debbugs.gnu.org id=B52973.164132520920101 (code B ref 52973); Tue, 04 Jan 2022 19:41:01 +0000 Original-Received: (at 52973) by debbugs.gnu.org; 4 Jan 2022 19:40:09 +0000 Original-Received: from localhost ([127.0.0.1]:38769 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n4pfC-0005E4-6i for submit@debbugs.gnu.org; Tue, 04 Jan 2022 14:40:09 -0500 Original-Received: from mout01.posteo.de ([185.67.36.65]:55119) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n4pf9-0005D0-ES for 52973@debbugs.gnu.org; Tue, 04 Jan 2022 14:40:05 -0500 Original-Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id 362C2240027 for <52973@debbugs.gnu.org>; Tue, 4 Jan 2022 20:39:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1641325197; bh=EYTZR9B60a/qukhVphOLaeC4DZwhY5hneOatJQxNfbg=; h=From:To:Cc:Subject:Autocrypt:Date:From; b=qCPpeh3WodUGjunxeUE3/ReZ1xF5OH9cUEFgR/HRH6ee9ff9V1z+EzCdrr8N5AR9c cJzsWqHJDpFVYJK//xA7gBTjcj0jJdF2n3ZtbsUsWaW5wWp3JYy99ousP4PtHqnk+r jxuHq9Wzol/KZP6mLfMkYg4Ui+7N3F5msX6YJhJ9UocsESHpDM94hVt4CXvP2YgWyP RK8meczdEYfbbWq8m3CQ2R5mfcrMxrJ0sMwPRC8MRe5LNnVVr3THsBS4seW7kGQjQ4 F2uOhkS9h37r8tn1dTKVaZlVw9eRoZkrUCvoKlCYzSg0C61znYxgqWhgykHigEuSRt G7hhBAo4qIm8g== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4JT2y431jTz9rwn; Tue, 4 Jan 2022 20:39:56 +0100 (CET) Autocrypt: addr=philipk@posteo.net; prefer-encrypt=nopreference; keydata= mDMEYHHqUhYJKwYBBAHaRw8BAQdAp3GdmYJ6tm5McweY6dEvIYIiry+Oz9rU4MH6NHWK0Ee0QlBo aWxpcCBLYWx1ZGVyY2ljIChnZW5lcmF0ZWQgYnkgYXV0b2NyeXB0LmVsKSA8cGhpbGlwa0Bwb3N0 ZW8ubmV0PoiQBBMWCAA4FiEEDM2H44ZoPt9Ms0eHtVrAHPRh1FwFAmBx6lICGwMFCwkIBwIGFQoJ CAsCBBYCAwECHgECF4AACgkQtVrAHPRh1FyTkgEAjlbGPxFchvMbxzAES3r8QLuZgCxeAXunM9gh io0ePtUBALVhh9G6wIoZhl0gUCbQpoN/UJHI08Gm1qDob5zDxnIHuDgEYHHqUhIKKwYBBAGXVQEF AQEHQNcRB+MUimTMqoxxMMUERpOR+Q4b1KgncDZkhrO2ql1tAwEIB4h4BBgWCAAgFiEEDM2H44Zo Pt9Ms0eHtVrAHPRh1FwFAmBx6lICGwwACgkQtVrAHPRh1Fw1JwD/Qo7kvtib8jy7puyWrSv0MeTS g8qIxgoRWJE/KKdkCLEA/jb9b9/g8nnX+UcwHf/4VfKsjExlnND3FrBviXUW6NcB In-Reply-To: <868rvwsezi.fsf@mail.linkov.net> (Juri Linkov's message of "Mon, 03 Jan 2022 23:12:53 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:223657 Archived-At: --=-=-= Content-Type: text/plain Juri Linkov writes: >> I am attaching a few patches adding a few features for >> context-menu-mode, that I mentioned and explained here: >> https://ruzkuku.com/texts/emacs-mouse.html. >> >> Would it be OK to add these to Emacs itself? > > Thanks, it would be beneficial to enrich the packages with the > context-menus. Man-context-menu is a good addition to the package > man.el, and hi-lock-context-menu to hi-lock.el. However, > mouse-online-search-at-point looks out of place in mouse.el. > Since it relies on eww, wouldn't the right place for it be > in eww.el? It doesn't really depend on eww in any substantial way, rather eww is the only place where a search engine is defined. What I was thinking about was adding a command like online-search (that could then have both a keyboard and mouse-oriented command), a online-search-engine user option. While one is at it, eww could be updated to use this too, by deprecating eww-search-prefix: --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Add-search-engine-functionality.patch >From 1ebb6881aeeb08bf574bdb688cb1e94af8b5609d Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Tue, 4 Jan 2022 20:36:32 +0100 Subject: [PATCH] Add search engine functionality * lisp/misc.el (online-search): Add command to query an online search engine. (online-search-engine-alist): Add user option to configure `online-search'. (online-search-engine): Add user option to configure `online-search'. (online-search-query): Add internal command to generate a search engine query, to be used elsewhere. (online-search-at-point): Add command. (online-search-at-mouse): Add command. (online-search-context-menu): Add context menu support. * lisp/net/eww.el (eww-search-prefix): Mark as obsolete (eww--dwim-expand-url): Use `online-search-query'. --- lisp/misc.el | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ lisp/net/eww.el | 7 ++-- 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/lisp/misc.el b/lisp/misc.el index 39ec9497d7..473fcac33f 100644 --- a/lisp/misc.el +++ b/lisp/misc.el @@ -27,6 +27,7 @@ (eval-when-compile (require 'tabulated-list)) +(require 'cl-lib) ;;;###autoload (defun copy-from-above-command (&optional arg) @@ -209,6 +210,98 @@ list-dynamic-libraries (display-buffer buffer) nil) +(defgroup online-search () + "Search engine options." + :group 'browse-url) + +(defcustom online-search-engine-alist + '(("DuckDuckGo" . "https://duckduckgo.com/html/?q=") + ("Google" . "https://www.google.com/search?q=") + ("Wikipedia" . "https://en.wikipedia.org/w/index.php?search=")) + "Alist of search engines, mapping strings to queries. +A query is either a string, interpreted as a URL prefix ending +with an incomplete query string, onto which the query will be +concatenated, or a function that accepts one argument and returns +a URL." + :type '(alist :key-type string :value-type string) + :group 'online-search) + +(defcustom online-search-engine "DuckDuckGo" + "Search engine to use for `online-search' and related commands. +The string should denote a key in `online-search-engine-alist'." + :type 'string + :group 'online-search) + +;;;###autoload +(defun online-search-query (string &optional engine) + "Generate an URL query to search for STRING with ENGINE. +See `online-search' for more details on ENGINE." + (let ((query (alist-get (or engine online-search-engine) + online-search-engine-alist + nil nil #'string=))) + (cond + ((stringp query) + (concat query (mapconcat #'url-hexify-string + (split-string string nil t) + "+"))) + ((functionp query) + (funcall query string)) + ((error "Unknown query %S" query))))) + +;;;###autoload +(defun online-search (string &optional engine) + "Search for STRING using a search engine. +If the optional argument ENGINE may be used to override +`online-search-engine', and will be used to look up a query in +`online-search-engine-alist'." + (interactive (list (if (use-region-p) + (buffer-substring (region-beginning) (region-end)) + (read-string "Search: ")) + (and current-prefix-arg + (completing-read + "Use search engine: " + online-search-engine-alist)))) + (browse-url (online-search-query string engine))) + +;;;###autoload +(defun online-search-at-point (&optional engine) + "Search for symbol at point using ENGINE. +See `online-search' for more details." + (interactive (list (and current-prefix-arg + (completing-read + "Use search engine: " + online-search-engine-alist)))) + (let ((query (thing-at-point 'symbol))) + (unless query + (user-error "Nothing to search for")) + (online-search query engine))) + +;;;###autoload +(defun online-search-at-mouse (click) + "Query an online search engine at CLICK. +If a region is active, the entire region will be sent, otherwise +the symbol at point will be used. See `online-search' for more +details." + (interactive "e") + (let ((query (if (use-region-p) + (buffer-substring (region-beginning) + (region-end)) + (thing-at-mouse click 'symbol)))) + (unless query + (user-error "Nothing to search for")) + (online-search query))) + +;;;###autoload +(defun online-search-context-menu (menu click) + "Populate MENU with command to search online at CLICK." + (save-excursion + (mouse-set-point click) + (define-key-after menu [online-search-separator] menu-bar-separator) + (define-key-after menu [online-search-at-mouse] + '(menu-item "Online search" online-search-at-mouse + :help "Search for region or word online"))) + menu) + (provide 'misc) ;;; misc.el ends here diff --git a/lisp/net/eww.el b/lisp/net/eww.el index 8930eb427d..37e84d160b 100644 --- a/lisp/net/eww.el +++ b/lisp/net/eww.el @@ -55,6 +55,7 @@ eww-search-prefix :version "24.4" :group 'eww :type 'string) +(make-obsolete-variable 'eww-search-prefix "online-search-engine" "29.1") (defcustom eww-use-browse-url "\\`mailto:" "EWW will use `browse-url' when following links that match this regexp. @@ -355,7 +356,7 @@ eww-browse (defun eww (url &optional new-buffer buffer) "Fetch URL and render the page. If the input doesn't look like an URL or a domain name, the -word(s) will be searched for via `eww-search-prefix'. +word(s) will be searched for via `online-search'. If NEW-BUFFER is non-nil (interactively, the prefix arg), use a new buffer instead of reusing the default EWW buffer. @@ -464,9 +465,7 @@ eww--dwim-expand-url ;; Some sites do not redirect final / (when (string= (url-filename (url-generic-parse-url url)) "") (setq url (concat url "/")))) - (setq url (concat eww-search-prefix - (mapconcat - #'url-hexify-string (split-string url) "+")))))) + (setq url (online-search-query url))))) url) (defun eww--preprocess-html (start end) -- 2.34.0 --=-=-= Content-Type: text/plain I added this to misc.el, but it could be added to a separate file as well. >> +(defun Man-at-mouse (e) >> ... >> +(defun Man-context-menu (menu click) >> + "Populate MENU with commands that open a man page at point." >> + (save-excursion >> + (mouse-set-point click) >> + (when (save-excursion > > It seems the second 'save-excursion' is superfluous? > >> + (skip-syntax-backward "^ ") >> + (and (looking-at >> + "[[:space:]]*\\([[:alnum:]_-]+([[:alnum:]]+)\\)") >> + (match-string 1))) >> + (define-key-after menu [man-separator] menu-bar-separator) >> + (define-key-after menu [man-at-mouse] >> + '(menu-item "Open man page" man-at-mouse > > Typo: the command name is capitalized 'Man-at-mouse'. > >> + (mouse-set-point click) >> + (when (symbol-at-point) > > This can be simplified to: > > (thing-at-mouse click 'symbol) Thanks for the hints, I have fixed them locally. -- Philip Kaludercic --=-=-=--