[
  {
    "path": ".gitignore",
    "content": "tags\n*/__pycache__\n.*.sw?\n"
  },
  {
    "path": "README.markdown",
    "content": "autotag.vim\n============\n\nIf you use ctags to make tags files of your source, it's nice to be able to re-run ctags on a source file when you save it.\n\nHowever, using `ctags -a` will only change existing entries in a tags file or add new ones. It doesn't delete entries that no longer exist. Should you delete an entity from your source file that's represented by an entry in a tags file, that entry will remain after calling `ctags -a`.\n\nThis python function will do two things:\n\n1) It will search for a tags file starting in the directory where your source file resides and moving up a directory at a time until it either finds one or runs out of directories to try.\n\n2) Should it find a tags file, it will then delete all entries in said tags file referencing the source file you've just saved and then execute `ctags -a` on that source file using the relative path to the source file from the tags file.\n\nThis way, every time you save a file, your tags file will be seamlessly updated.\n\nInstallation\n------------\n\nCurrently I suggest you use Vundle and install as a normal Bundle\n\nFrom the Vim command-line\n\n    :BundleInstall 'craigemery/vim-autotag'\n\nAnd add to your ~/.vimrc\n\n    Bundle 'craigemery/vim-autotag'\n\nOr you can manually install\n    cd\n    git clone git://github.com/craigemery/vim-autotag.git\n    cd ~/.vim/\n    mkdir -p plugin\n    cp ~/vim-autotag.git/plugin/* plugin/\n\n### Install as a Pathogen bundle\n```\ngit clone git://github.com/craigemery/vim-autotag.git ~/.vim/bundle/vim-autotag\n```\n\nGetting round other ctags limitations\n-------------------------------------\nctags is very file name suffix driven. When the file has no suffix, ctags can fail to detect the file type.  \nThe easiest way to replicate this is when using a #! shebang. I've seen \"#!/usr/bin/env python3\" in a \nshebang not get detected by ctags.  \nBut Vim is better at this. So Vim's filetype buffer setting can help.  \nSo when the buffer being written has no suffix to the file name then the Vim filetype value will be used instead.  \nSo far I've only implemented \"python\" as one that is given to ctags --language-force=<here> as is.  \nOther filetypes could be mapped. There's a dict in the AutTag class.  \nTo not map a filetype to a forced language kind, add the vim file type to the comma \",\" separated\nlist in autotagExcludeFiletypes.\n\nConfiguration\n-------------\nAutotag can be configured using the following global variables:\n\n| Name | Purpose |\n| ---- | ------- |\n| `g:autotagExcludeSuffixes` | suffixes to not ctags on |\n| `g:autotagExcludeFiletypes` | filetypes to not try & force a language choice on ctags |\n| `g:autotagVerbosityLevel` | logging verbosity (as in Python logging module) |\n| `g:autotagCtagsCmd` | name of ctags command |\n| `g:autotagTagsFile` | name of tags file to look for |\n| `g:autotagDisabled` | Disable autotag (enable by setting to any non-blank value) |\n| `g:autotagStopAt` | stop looking for a tags file (and make one) at this directory (defaults to $HOME) |\n| `g:autotagStartMethod` | Now AutoTag uses Python multiprocessing, the start method is an internal aspect that Python uses.\n\nThese can be overridden with buffer specific ones. b: instead of g:\nExample:\n```\nlet g:autotagTagsFile=\".tags\"\n```\n\nmacOS, Python 3.8 and 'spawn'\n-----------------------------\nWith the release of Python 3.8, the default start method for multiprocessing on macOS has become 'spawn'\nAt the time of writing there are issues with 'spawn' and I advise making AutoTag ask Python to use 'fork'\ni.e. before loading the plugin:\n```\nlet g:autotagStartMethod='fork'\n```\n\nSelf-Promotion\n--------------\n\nLike autotag.vim? Follow the repository on\n[GitHub](https://github.com/craigemery/vim-autotag) and vote for it on\n[vim.org](http://www.vim.org/scripts/script.php?script_id=1343).  And if\nyou're feeling especially charitable, follow [craigemery] on\n[GitHub](https://github.com/craigemery).\n\nLicense\n-------\n\nCopyright (c) Craig Emery.  Distributed under the same terms as Vim itself.\nSee `:help license`.\n"
  },
  {
    "path": "autoload/autotag.py",
    "content": "\"\"\"\n(c) Craig Emery 2017-2022\nAutoTag.py\n\"\"\"\n\nfrom __future__ import print_function\nimport sys\nimport os\nimport fileinput\nimport logging\nfrom collections import defaultdict\nimport subprocess\nfrom traceback import format_exc\nimport multiprocessing as mp\nfrom glob import glob\nimport vim  # pylint: disable=import-error\n\n__all__ = [\"autotag\"]\n\n# global vim config variables used (all are g:autotag<name>):\n# name purpose\n# ExcludeSuffixes suffixes to not ctags on\n# VerbosityLevel logging verbosity (as in Python logging module)\n# CtagsCmd name of ctags command\n# TagsFile name of tags file to look for\n# Disabled Disable autotag (enable by setting to any non-blank value)\n# StopAt stop looking for a tags file (and make one) at this directory (defaults to $HOME)\nGLOBALS_DEFAULTS = dict(ExcludeSuffixes=\"tml.xml.text.txt\",\n                        VerbosityLevel=logging.WARNING,\n                        CtagsCmd=\"ctags\",\n                        TagsFile=\"tags\",\n                        TagsDir=\"\",\n                        Disabled=0,\n                        StopAt=0,\n                        StartMethod=\"\")\n\n\ndef do_cmd(cmd, cwd):\n    \"\"\" Abstract subprocess \"\"\"\n    with subprocess.Popen(cmd, cwd=cwd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,\n                          stderr=subprocess.PIPE, universal_newlines=True) as proc:\n        stdout = proc.communicate()[0]\n        return stdout.split(\"\\n\")\n\n\ndef vim_global(name, kind=str):\n    \"\"\" Get global variable from vim, cast it appropriately \"\"\"\n    ret = GLOBALS_DEFAULTS.get(name, None)\n    try:\n        vname = \"autotag\" + name\n        v_buffer = \"b:\" + vname\n        exists_buffer = (vim.eval(f\"exists('{v_buffer}')\") == \"1\")\n        v_global = \"g:\" + vname\n        exists_global = (vim.eval(f\"exists('{v_global}')\") == \"1\")\n        if exists_buffer:\n            ret = vim.eval(v_buffer)\n        elif exists_global:\n            ret = vim.eval(v_global)\n        else:\n            if isinstance(ret, int):\n                vim.command(f\"let {v_global}={ret}\")\n            else:\n                vim.command(f\"let {v_global}=\\\"{ret}\\\"\")\n    finally:\n        if kind == bool:\n            ret = (ret in [1, \"1\", \"true\", \"yes\"])\n        elif kind == int:\n            try:\n                val = int(ret)\n            except TypeError:\n                val = ret\n            except ValueError:\n                val = ret\n            ret = val\n        elif kind == str:\n            ret = str(ret)\n    return ret\n\n\ndef init_multiprocessing():\n    \"\"\" Init multiprocessing, set_executable() & get the context we'll use \"\"\"\n    wanted_start_method = vim_global(\"StartMethod\") or None\n    used_start_method = mp.get_start_method()\n    if wanted_start_method in mp.get_all_start_methods():\n        used_start_method = wanted_start_method\n    else:\n        wanted_start_method = None\n    # here wanted_start_method is either a valid method or None\n    # used_start_method is what the module has as the default or our overriden value\n    ret = mp.get_context(wanted_start_method)  # wanted_start_method might be None\n    try:\n        mp.set_executable\n    except AttributeError:\n        return ret\n    if used_start_method == 'spawn':\n        suff = os.path.splitext(sys.executable)[1]\n        pat1 = f\"python*{suff}\"\n        pat2 = os.path.join(\"bin\", pat1)\n        exes = glob(os.path.join(sys.exec_prefix, pat1)) + glob(os.path.join(sys.exec_prefix, pat2))\n        if exes:\n            win = [exe for exe in exes if exe.endswith(f\"w{suff}\")]\n            if win:\n                # In Windows pythonw.exe is best\n                ret.set_executable(win[0])\n            else:\n                # This isn't great, for now pick the first one\n                ret.set_executable(exes[0])\n    return ret\n\n\nCTX = init_multiprocessing()\n\n\nclass VimAppendHandler(logging.Handler):\n    \"\"\" Logger handler that finds a buffer and appends the log message as a new line \"\"\"\n    def __init__(self, name):\n        logging.Handler.__init__(self)\n        self.__name = name\n        self.__formatter = logging.Formatter()\n\n    def __find_buffer(self):\n        \"\"\" Look for the named buffer \"\"\"\n        for buff in vim.buffers:\n            if buff and buff.name and buff.name.endswith(self.__name):\n                yield buff\n\n    def emit(self, record):\n        \"\"\" Emit the logging message \"\"\"\n        for buff in self.__find_buffer():\n            buff.append(self.__formatter.format(record))\n\n\ndef set_logger_verbosity():\n    \"\"\" Set the verbosity of the logger \"\"\"\n    level = vim_global(\"VerbosityLevel\", kind=int)\n    LOGGER.setLevel(level)\n\n\ndef make_and_add_handler(logger, name):\n    \"\"\" Make the handler and add it to the standard logger \"\"\"\n    ret = VimAppendHandler(name)\n    logger.addHandler(ret)\n    return ret\n\n\ntry:\n    LOGGER\nexcept NameError:\n    DEBUG_NAME = \"autotag_debug\"\n    LOGGER = logging.getLogger(DEBUG_NAME)\n    HANDLER = make_and_add_handler(LOGGER, DEBUG_NAME)\n    set_logger_verbosity()\n\n\nclass AutoTag():  # pylint: disable=too-many-instance-attributes\n    \"\"\" Class that does auto ctags updating \"\"\"\n    LOG = LOGGER\n    AUTOFILETYPES = [\"python\"]\n    FILETYPES = {}\n\n    def __init__(self):\n        self.locks = {}\n        self.tags = defaultdict(list)\n        self.excludesuffix = [\".\" + s for s in vim_global(\"ExcludeSuffixes\").split(\".\")]\n        self.excludefiletype = vim_global(\"ExcludeFiletypes\").split(\",\")\n        set_logger_verbosity()\n        self.sep_used_by_ctags = '/'\n        self.ctags_cmd = vim_global(\"CtagsCmd\")\n        self.tags_file = str(vim_global(\"TagsFile\"))\n        self.tags_dir = str(vim_global(\"TagsDir\"))\n        self.parents = os.pardir * (len(os.path.split(self.tags_dir)) - 1)\n        self.count = 0\n        self.stop_at = vim_global(\"StopAt\")\n\n    def find_tag_file(self, source):\n        \"\"\" Find the tag file that belongs to the source file \"\"\"\n        AutoTag.LOG.info('source = \"%s\"', source)\n        (drive, fname) = os.path.splitdrive(source)\n        ret = None\n        while ret is None:\n            fname = os.path.dirname(fname)\n            AutoTag.LOG.info('drive = \"%s\", file = \"%s\"', drive, fname)\n            tags_dir = os.path.join(drive, fname)\n            tags_file = os.path.join(tags_dir, self.tags_dir, self.tags_file)\n            AutoTag.LOG.info('testing tags_file \"%s\"', tags_file)\n            if os.path.isfile(tags_file):\n                stinf = os.stat(tags_file)\n                if stinf:\n                    size = getattr(stinf, 'st_size', None)\n                    if size is None:\n                        AutoTag.LOG.warning(\"Could not stat tags file %s\", tags_file)\n                        ret = \"\"\n                ret = (fname, tags_file)\n            elif tags_dir and tags_dir == self.stop_at:\n                AutoTag.LOG.info(\"Reached %s. Making one %s\", self.stop_at, tags_file)\n                open(tags_file, 'wb').close()\n                ret = (fname, tags_file)\n                ret = \"\"\n            elif not fname or fname == os.sep or fname == \"//\" or fname == \"\\\\\\\\\":\n                AutoTag.LOG.info('bail (file = \"%s\")', fname)\n                ret = \"\"\n        return ret or None\n\n    def add_source(self, source, filetype):\n        \"\"\" Make a note of the source file, ignoring some etc \"\"\"\n        if not source:\n            AutoTag.LOG.warning('No source')\n            return\n        if os.path.basename(source) == self.tags_file:\n            AutoTag.LOG.info(\"Ignoring tags file %s\", self.tags_file)\n            return\n        suff = os.path.splitext(source)[1]\n        if suff:\n            AutoTag.LOG.info(\"Source %s has suffix %s, so filetype doesn't count!\", source, suff)\n            filetype = None\n        else:\n            AutoTag.LOG.info(\"Source %s has no suffix, so filetype counts!\", source)\n\n        if suff in self.excludesuffix:\n            AutoTag.LOG.info(\"Ignoring excluded suffix %s for file %s\", suff, source)\n            return\n        if filetype in self.excludefiletype:\n            AutoTag.LOG.info(\"Ignoring excluded filetype %s for file %s\", filetype, source)\n            return\n        found = self.find_tag_file(source)\n        if found:\n            (tags_dir, tags_file) = found\n            relative_source = os.path.splitdrive(source)[1][len(tags_dir):]\n            if relative_source[0] == os.sep:\n                relative_source = relative_source[1:]\n            if os.sep != self.sep_used_by_ctags:\n                relative_source = relative_source.replace(os.sep, self.sep_used_by_ctags)\n            key = (tags_dir, tags_file, filetype)\n            self.tags[key].append(relative_source)\n            if key not in self.locks:\n                self.locks[key] = CTX.Lock()\n\n    @staticmethod\n    def good_tag(line, excluded):\n        \"\"\" Filter method for stripping tags \"\"\"\n        if line[0] == '!':\n            return True\n        fields = line.split('\\t')\n        AutoTag.LOG.log(1, \"read tags line:%s\", str(fields))\n        if len(fields) > 3 and fields[1] not in excluded:\n            return True\n        return False\n\n    def strip_tags(self, tags_file, sources):\n        \"\"\" Strip all tags for a given source file \"\"\"\n        AutoTag.LOG.info(\"Stripping tags for %s from tags file %s\", \",\".join(sources), tags_file)\n        backup = \".SAFE\"\n        try:\n            with fileinput.FileInput(files=tags_file, inplace=True, backup=backup) as source:\n                for line in source:\n                    line = line.strip()\n                    if self.good_tag(line, sources):\n                        print(line)\n        finally:\n            try:\n                os.unlink(tags_file + backup)\n            except IOError:\n                pass\n\n    def _vim_ft_to_ctags_ft(self, name):\n        \"\"\" convert vim filetype strings to ctags strings \"\"\"\n        if name in AutoTag.AUTOFILETYPES:\n            return name\n        return self.FILETYPES.get(name, None)\n\n    def update_tags_file(self, key, sources):\n        \"\"\" Strip all tags for the source file, then re-run ctags in append mode \"\"\"\n        (tags_dir, tags_file, filetype) = key\n        lock = self.locks[key]\n        if self.tags_dir:\n            sources = [os.path.join(self.parents + s) for s in sources]\n        cmd = [self.ctags_cmd]\n        if self.tags_file:\n            cmd += [\"-f\", self.tags_file]\n        if filetype:\n            ctags_filetype = self._vim_ft_to_ctags_ft(filetype)\n            if ctags_filetype:\n                cmd += [f\"--language-force={ctags_filetype}\"]\n        cmd += [\"-a\"]\n\n        def is_file(src):\n            \"\"\" inner \"\"\"\n            return os.path.isfile(os.path.join(tags_dir, self.tags_dir, src))\n\n        srcs = list(filter(is_file, sources))\n        if not srcs:\n            return\n\n        cmd += [f'\"{s}\"' for s in srcs]\n        cmd = \" \".join(cmd)\n        with lock:\n            self.strip_tags(tags_file, sources)\n            AutoTag.LOG.log(1, \"%s: %s\", tags_dir, cmd)\n            for line in do_cmd(cmd, self.tags_dir or tags_dir):\n                AutoTag.LOG.log(10, line)\n\n    def rebuild_tag_files(self):\n        \"\"\" rebuild the tags file thread worker \"\"\"\n        for (key, sources) in self.tags.items():\n            AutoTag.LOG.info('Process(%s, %s)', key, \",\".join(sources))\n            proc = CTX.Process(target=self.update_tags_file, args=(key, sources))\n            proc.daemon = True\n            proc.start()\n\n\ndef autotag():\n    \"\"\" Do the work \"\"\"\n    try:\n        if not vim_global(\"Disabled\", bool):\n            runner = AutoTag()\n            runner.add_source(vim.eval(\"expand(\\\"%:p\\\")\"), vim.eval(\"&ft\"))\n            runner.rebuild_tag_files()\n    except Exception:  # pylint: disable=broad-except\n        logging.warning(format_exc())\n"
  },
  {
    "path": "autoload/autotag.vim",
    "content": "if ! has(\"python3\")\n    finish\nendif\npython3 import sys, os, vim\npython3 sys.path.insert(0, os.path.dirname(vim.eval('expand(\"<sfile>\")')))\npython3 import autotag\n\nfunction! autotag#Run()\n   if exists(\"b:netrw_method\")\n      return\n   endif\n   python3 autotag.autotag()\n   if exists(\":TlistUpdate\")\n      TlistUpdate\n   endif\nendfunction\n"
  },
  {
    "path": "plugin/autotag.vim",
    "content": "\"\n\" (c) Craig Emery 2017-2022\n\"\n\" Increment the number below for a dynamic #include guard\nlet s:autotag_vim_version=1\n\nif exists(\"g:autotag_vim_version_sourced\")\n   if s:autotag_vim_version == g:autotag_vim_version_sourced\n      finish\n   endif\nendif\n\nlet g:autotag_vim_version_sourced=s:autotag_vim_version\n\n\" This file supplies automatic tag regeneration when saving files\n\" There's a problem with ctags when run with -a (append)\n\" ctags doesn't remove entries for the supplied source file that no longer exist\n\" so this script (implemented in Python) finds a tags file for the file vim has\n\" just saved, removes all entries for that source file and *then* runs ctags -a\n\nif !has(\"python3\")\n   finish\nendif \" !has(\"python3\")\n\nfunction! AutoTagDebug()\n   new\n   file autotag_debug\n   setlocal buftype=nowrite\n   setlocal bufhidden=delete\n   setlocal noswapfile\n   normal \u0017\u0017\nendfunction\n\naugroup autotag\n   au!\n   autocmd BufWritePost,FileWritePost * call autotag#Run ()\naugroup END\n\n\" vim:shiftwidth=3:ts=3\n"
  }
]