Subject: 28.0.50; `gnus-search-run-search' for imap does not take parentheses into account
From: Michael Welsh Duggan <[email-masked]>
To: [email-masked]
Date: Sun, 10 Jan 2021 12:49:04 -0500
List-Id: <debbugs-submit.debbugs.gnu.org>

When using the `gnus-search-imap' backend for `gnus-search', there is a
bug in the `gnus-search-run-search' method.  When the search query
begins with parentheses (such as "(OR FROM A FROM B)"), the naive check
for a known IMAP keyword at the beginning fails due to the parenthesis,
and gnus-search adds "TEXT " to the beginning of the query.

Moreover, these are several search keys that are not in
`gnus-search-imap-search-keys'.  These are "new", "not", "old", and
"or".

I include a minimal patch for these problems.

There's another misfeature which I have not attempted to fix that made
debugging the problem much more difficult.  If the result of a query is
not OK, this fact just gets ignored by `gnus-search-run-search' and the
search group is considered empty.  Even after fixing the above problems,
this is a problem when doing a raw search, as one doesn't know whether
the query is bad or if it just does not result in any messages.

Example:

C-u G G "((OR" RET

This results in something like:

577841 BAD Error in IMAP command UID SEARCH: Missing ')' (0.001 + 0.000 secs).

in the process buffer.

Here's a small patch for the first two issues:

diff --git a/lisp/gnus/gnus-search.el b/lisp/gnus/gnus-search.el
index 44f43b073c..f20f9586d2 100644
--- a/lisp/gnus/gnus-search.el
+++ b/lisp/gnus/gnus-search.el
@@ -1036,7 +1036,7 @@ gnus-search-imap-search-keys
   '(body cc bcc from header keyword larger smaller subject text to uid x-gm-raw
 	 answered before deleted draft flagged on since recent seen sentbefore
 	 senton sentsince unanswered undeleted undraft unflagged unkeyword
-	 unseen all)
+         unseen all new not old or)
   "Known IMAP search keys.")
 
 ;; imap interface
@@ -1072,9 +1072,9 @@ gnus-search-imap-search-keys
       ;; A bit of backward-compatibility slash convenience: if the
       ;; query string doesn't start with any known IMAP search
       ;; keyword, assume it is a "TEXT" search.
-      (unless (and (string-match "\\`[^[:blank:]]+" q-string)
+      (unless (and (string-match "\\`(*\\([^[:blank:]]+\\)" q-string)
 		   (memql (intern-soft (downcase
-					(match-string 0 q-string)))
+                                        (match-string 1 q-string)))
 			  gnus-search-imap-search-keys))
 	(setq q-string (concat "TEXT " q-string)))

In GNU Emacs 28.0.50 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo version 1.16.0)
 of 2020-12-20 built on maru2
Repository revision: 27347db0069f9d9fd0152274bfc7c649c387f185
Repository branch: md5i
Windowing system distributor 'The X.Org Foundation', version 11.0.12009000
System Description: Debian GNU/Linux bullseye/sid

Configured using:
 'configure --with-modules --without-toolkit-scroll-bars
 --with-x-toolkit=lucid --with-wide-int --with-gameuser=:staff
 'CFLAGS=-Og -ggdb3''

Configured features:
XPM JPEG TIFF GIF PNG RSVG CAIRO SOUND GPM DBUS GSETTINGS GLIB NOTIFY
INOTIFY ACL LIBSELINUX GNUTLS LIBXML2 FREETYPE HARFBUZZ M17N_FLT LIBOTF
ZLIB LUCID X11 XDBE XIM MODULES THREADS LIBSYSTEMD JSON PDUMPER LCMS2

Important settings:
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: @im=ibus
  locale-coding-system: utf-8-unix

Major mode: Group

Minor modes in effect:
  gnus-topic-mode: t
  cursor-sensor-mode: t
  gnus-undo-mode: t
  display-time-mode: t
  magit-wip-initial-backup-mode: t
  magit-wip-before-change-mode: t
  magit-wip-after-apply-mode: t
  magit-wip-after-save-mode: t
  magit-wip-mode: t
  magit-auto-revert-mode: t
  global-git-commit-mode: t
  async-bytecomp-package-mode: t
  shell-dirtrack-mode: t
  desktop-save-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  buffer-read-only: t
  line-number-mode: t

Load-path shadows:
/home/md5i/.emacs.d/elpa/cmake-mode-20201217.1410/cmake-mode hides /usr/share/emacs/site-lisp/cmake-mode

Features:
(nroff-mode prolog align ps-bdf ps-mule markdown-mode gnus-eform
sh-script smie executable cmake-font-lock cmake-mode autoload tar-mode
arc-mode archive-mode lisp-mnt url-http url-gw url-auth finder-inf xref
project profiler timer-list goto-addr vc-mtn vc-src vc-sccs vc-svn
vc-cvs vc-rcs vc vc-dispatcher bug-reference tex-mode latexenc rst
pcmpl-unix man generic thai-util thai-word org-element avl-tree
generator ol-eww eww xdg ol-rmail ol-mhe ol-irc ol-info ol-gnus
ol-docview doc-view ol-bibtex bibtex ol-bbdb ol-w3m org ob ob-tangle
ob-ref ob-lob ob-table ob-exp org-macro org-footnote org-src ob-comint
org-pcomplete org-list org-faces org-entities org-version ob-emacs-lisp
ob-core ob-eval org-table ol org-keys org-compat org-macs org-loaddefs
cal-menu calendar cal-loaddefs image-mode exif zenscript-mode
zenscript-indentation zenscript-completion zenscript-language
zenscript-parser zenscript-highlighting zenscript-common calc-undo js
vc-hg vc-bzr tramp-cmds rfc2104 noutline outline tramp-cache novice
magit-extras face-remap conf-mode dired-aux shr-color color flow-fill
calc-misc calc-alg calc-ext calc-menu calc calc-loaddefs calc-macs
mm-archive gnus-bcklg qp copyright time-stamp whitespace tabify grep
compile shadow sort mail-extr emacsbug vc-git dabbrev edebug deuglify
gnus-async gnus-cite gnus-cus gnus-diary nndiary gnus-draft gnus-dup
gnus-fun gnus-html url-queue url-cache mm-url gnus-kill gnus-logic
gnus-mh mh-comp mh-scan mh-gnus mh-e mh-compat mh-buffers mh-loaddefs
gnus-registry registry eieio-base gnus-salt gnus-uu yenc gnus-vm
sendmail mule-util cl-print debug backtrace misearch multi-isearch
jka-compr thingatpt shortdoc help-fns radix-tree gnus-ml disp-table
nnselect gnus-search eieio-opt speedbar ezimage dframe find-func
gnus-topic cursor-sensor utf-7 nndraft nnmh epa-file network-stream nsm
nnml gnus-agent gnus-srvr gnus-score score-mode nnvirtual gnus-msg
gnus-art mm-uu mml2015 mm-view mml-smime smime dig gnus-cache gnus-sum
shr kinsoku svg gnus-demon nntp gnus-group gnus-undo gnutls gnus-start
gnus-dbus gnus-cloud nnimap nnmail mail-source utf7 netrc nnoo gnus-spec
gnus-int gnus-range gnus-win gnus nnheader flyspell ispell view uptimes
descr-text tramp-sh cus-edit pp cus-start cus-load tramp tramp-loaddefs
trampver tramp-integration files-x tramp-compat parse-time iso8601
ls-lisp time sieve-manage sasl sasl-anonymous sasl-login sasl-plain
rng-loc rng-uri rng-parse rng-match rng-dt rng-util rng-pttrn nxml-parse
nxml-ns nxml-enc xmltok nxml-util sgml-mode dom psvn wid-edit cl
ps-print ps-print-loaddefs ps-def lpr picture magit-submodule
magit-obsolete magit-blame magit-stash magit-reflog magit-bisect
magit-push magit-pull magit-fetch magit-clone magit-remote magit-commit
magit-sequence magit-notes magit-worktree magit-tag magit-merge
magit-branch magit-reset magit-files magit-refs magit-status magit
magit-repos magit-apply magit-wip magit-log which-func imenu magit-diff
smerge-mode diff diff-mode magit-core magit-autorevert autorevert
filenotify magit-margin magit-transient magit-process magit-mode
git-commit transient format-spec magit-git magit-section magit-utils crm
log-edit easy-mmode message rmc puny rfc822 mml mml-sec epa gnus-util
rmail rmail-loaddefs text-property-search time-date mm-decode mm-bodies
mm-encode mail-parse rfc2231 rfc2047 rfc2045 mm-util ietf-drums
mail-prsvr mailabbrev mail-utils gmm-utils mailheader pcvs-util add-log
with-editor cl-extra help-mode async-bytecomp advice async shell
pcomplete server dash lua-mode rx comint ansi-color ring generated
generic-x epg epg-config ediff ediff-merg ediff-mult ediff-wind
ediff-diff ediff-help ediff-init ediff-util pcase dbus xml desktop
frameset dired-x dired dired-loaddefs cc-mode cc-fonts cc-guess cc-menus
cc-cmds cc-styles cc-align cc-engine cc-vars cc-defs edmacro kmacro
derived debian-el info package easymenu browse-url url url-proxy
url-privacy url-expand url-methods url-history url-cookie url-domsuf
url-util mailcap url-handlers url-parse auth-source cl-seq eieio
eieio-core cl-macs eieio-loaddefs password-cache json subr-x map
url-vars seq byte-opt gv bytecomp byte-compile cconv cl-loaddefs cl-lib
iso-transl tooltip eldoc electric uniquify ediff-hook vc-hooks
lisp-float-type mwheel term/x-win x-win term/common-win x-dnd tool-bar
dnd fontset image regexp-opt fringe tabulated-list replace newcomment
text-mode elisp-mode lisp-mode prog-mode register page tab-bar menu-bar
rfn-eshadow isearch timer select scroll-bar mouse jit-lock font-lock
syntax facemenu font-core term/tty-colors frame minibuffer cl-generic
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite charscript charprop
case-table epa-hook jka-cmpr-hook help simple abbrev obarray
cl-preloaded nadvice button loaddefs faces cus-face macroexp files
window text-properties overlay sha1 md5 base64 format env code-pages
mule custom widget hashtable-print-readable backquote threads dbusbind
inotify lcms2 dynamic-setting system-font-setting font-render-setting
cairo x-toolkit x multi-tty make-network-process emacs)

Memory information:
((conses 16 3100306 317740)
 (symbols 48 59489 65)
 (strings 32 300970 101472)
 (string-bytes 1 21728841)
 (vectors 16 156616)
 (vector-slots 8 3056454 276397)
 (floats 8 943 1294)
 (intervals 56 327249 13446)
 (buffers 984 241))

-- 
Michael Welsh Duggan
([email-masked])