[PATCH 2/2] completion: generate notmuch-completion.zsh using doc/conf.py

David Bremner david at tethera.net
Sat Jan 3 04:58:20 PST 2015


I noticed that the (out of date) list of subcommands was simply
repeating the summary lines from the manpage. Since those summary
lines are already kept in machine readable form in doc/conf.py,
it's easy to compute that part of the completion file.
---
 completion/Makefile.local         |  12 ++++-
 completion/gen-zsh-completion.py  | 108 ++++++++++++++++++++++++++++++++++++++
 completion/notmuch-completion.zsh |  96 ---------------------------------
 3 files changed, 119 insertions(+), 97 deletions(-)
 create mode 100644 completion/gen-zsh-completion.py
 delete mode 100644 completion/notmuch-completion.zsh

diff --git a/completion/Makefile.local b/completion/Makefile.local
index dfc1271..616afa4 100644
--- a/completion/Makefile.local
+++ b/completion/Makefile.local
@@ -6,7 +6,15 @@ dir := completion
 # directly in any shell commands. Instead we save its value in other,
 # private variables that we can use in the commands.
 bash_script := $(srcdir)/$(dir)/notmuch-completion.bash
-zsh_script := $(srcdir)/$(dir)/notmuch-completion.zsh
+gen_zsh_script := $(srcdir)/$(dir)/gen-zsh-completion.py
+conf_file := $(srcdir)/$(dir)/../doc/conf.py
+
+$(dir)/notmuch-completion.zsh: $(dir)/gen-zsh-completion.py
+	$(PYTHON) $(gen_zsh_script)  $(conf_file) > $@
+
+ifeq ($(WITH_ZSH),1)
+all: $(dir)/notmuch-completion.zsh
+endif
 
 install: install-$(dir)
 
@@ -20,3 +28,5 @@ ifeq ($(WITH_ZSH),1)
 	mkdir -p "$(DESTDIR)$(zsh_completion_dir)"
 	install -m0644 $(zsh_script) "$(DESTDIR)$(zsh_completion_dir)/_notmuch"
 endif
+
+CLEAN += $(dir)/notmuch-completion.zsh
diff --git a/completion/gen-zsh-completion.py b/completion/gen-zsh-completion.py
new file mode 100644
index 0000000..26cd13e
--- /dev/null
+++ b/completion/gen-zsh-completion.py
@@ -0,0 +1,108 @@
+# Copyright 2014 David Bremner <david at tethera.net>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see http://www.gnu.org/licenses/ .
+#
+# This script is based on notmuch-completion.zsh, by
+#               Ingmar Vanhassel <ingmar at exherbo.org>
+
+from sys import argv
+
+conffile = argv[1]
+
+with open(conffile) as cf:
+    exec(cf.read())
+
+print(r'''#compdef notmuch
+
+# This file was generated from gen-zsh-completion.py in the notmuch source.
+# You should probably not edit it directly
+_notmuch_commands()
+{
+  local -a notmuch_commands
+  notmuch_commands=(
+    'help:display documentation for a subcommand'
+    'setup:interactively configure notmuch'
+''')
+
+for command in man_pages:
+    parts=command[1].split('-')
+    if len(parts) == 1 or command[4] != 1:
+        continue
+    print ('    \'{0:s}:{1:s}\''.format(parts[1],command[2]))
+
+print (
+r'''  )
+
+  _describe -t command 'command' notmuch_commands
+}
+
+_notmuch_dump()
+{
+  _files
+}
+
+_notmuch_help_topics()
+{
+  local -a notmuch_help_topics
+  notmuch_help_topics=(
+    'search-terms:show common search-terms syntax'
+  )
+  _describe -t notmuch-help-topics 'topic' notmuch_help_topics
+}
+
+_notmuch_help()
+{
+  _alternative \
+    _notmuch_commands \
+    _notmuch_help_topics
+}
+
+_notmuch_restore()
+{
+  _files
+}
+
+_notmuch_search()
+{
+  _arguments -s : \
+    '--max-threads=[display only the first x threads from the search results]:number of threads to show: ' \
+    '--first=[omit the first x threads from the search results]:number of threads to omit: ' \
+    '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))' \
+    '--output=[select what to output]:output:((summary threads messages files tags))'
+}
+
+_notmuch_address()
+{
+  _arguments -s : \
+    '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))' \
+    '--output=[select what to output]:output:((sender recipients count))'
+}
+
+_notmuch()
+{
+  if (( CURRENT > 2 )) ; then
+    local cmd=${words[2]}
+    curcontext="${curcontext%:*:*}:notmuch-$cmd"
+    (( CURRENT-- ))
+    shift words
+    _call_function ret _notmuch_$cmd
+    return ret
+  else
+    _notmuch_commands
+  fi
+}
+
+_notmuch "$@"
+
+''')
diff --git a/completion/notmuch-completion.zsh b/completion/notmuch-completion.zsh
deleted file mode 100644
index 46b33d1..0000000
--- a/completion/notmuch-completion.zsh
+++ /dev/null
@@ -1,96 +0,0 @@
-#compdef notmuch
-
-# ZSH completion for `notmuch`
-# Copyright © 2009 Ingmar Vanhassel <ingmar at exherbo.org>
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see http://www.gnu.org/licenses/ .
-
-_notmuch_commands()
-{
-  local -a notmuch_commands
-  notmuch_commands=(
-    'setup:interactively set up notmuch for first use'
-    'new:find and import any new message to the database'
-    'search:search for messages matching the search terms, display matching threads as results'
-    'address:get addresses from messages matching the given search terms'
-    'reply:constructs a reply template for a set of messages'
-    'show:show all messages matching the search terms'
-    'tag:add or remove tags for all messages matching the search terms'
-    'dump:creates a plain-text dump of the tags of each message'
-    'restore:restores the tags from the given file'
-    'help:show details on a command'
-  )
-
-  _describe -t command 'command' notmuch_commands
-}
-
-_notmuch_dump()
-{
-  _files
-}
-
-_notmuch_help_topics()
-{
-  local -a notmuch_help_topics
-  notmuch_help_topics=(
-    'search-terms:show common search-terms syntax'
-  )
-  _describe -t notmuch-help-topics 'topic' notmuch_help_topics
-}
-
-_notmuch_help()
-{
-  _alternative \
-    _notmuch_commands \
-    _notmuch_help_topics
-}
-
-_notmuch_restore()
-{
-  _files
-}
-
-_notmuch_search()
-{
-  _arguments -s : \
-    '--max-threads=[display only the first x threads from the search results]:number of threads to show: ' \
-    '--first=[omit the first x threads from the search results]:number of threads to omit: ' \
-    '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))' \
-    '--output=[select what to output]:output:((summary threads messages files tags))'
-}
-
-_notmuch_address()
-{
-  _arguments -s : \
-    '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))' \
-    '--output=[select what to output]:output:((sender recipients count))'
-}
-
-_notmuch()
-{
-  if (( CURRENT > 2 )) ; then
-    local cmd=${words[2]}
-    curcontext="${curcontext%:*:*}:notmuch-$cmd"
-    (( CURRENT-- ))
-    shift words
-    _call_function ret _notmuch_$cmd
-    return ret
-  else
-    _notmuch_commands
-  fi
-}
-
-_notmuch "$@"
-
-# vim: set sw=2 sts=2 ts=2 et ft=zsh :
-- 
2.1.3



More information about the notmuch mailing list