vim 之ptags.py

來源:互聯網
上載者:User

在看ctags的協助文檔時看到了ptags.py,專為python產生tags.具體目錄位於python原始碼的Tools/scripts/ptags.py。自己可以到Python官網下載

原始碼,解壓提取ptags.py放在path變數搜尋的目錄裡。Python2.x和Python3.x都有對應的ptags.py ,其實這裡面的原理是一樣的,根本不涉及到2.x和3.x之間有差異的文法,而且產生的tags檔案也主要是針對類和函數(class and def)所以,沒有什麼區別,完全可以在想聲稱python3.x的tags時,使用python2.x的pytags。

 

我的機器上放置的位置為:Debian Squeeze/Wheezy/Sid  --> /usr/local/bin

                                                Mandriva /openSUSE  ------------->$HOME/bin

 

 

 

[wolf@localhost tmp]$ diff ptags.py ptags3.py<br />1c1<br />< #! /usr/bin/env python<br />---<br />> #! /usr/bin/env python3<br />##就只有那麼點差異而已,解譯器使用的不一樣而已。

 

ptags.py(for python2.x):

#! /usr/bin/env python<br /># ptags<br />#<br /># Create a tags file for Python programs, usable with vi.<br /># Tagged are:<br /># - functions (even inside other defs or classes)<br /># - classes<br /># - filenames<br /># Warns about files it cannot open.<br /># No warnings about duplicate tags.<br />import sys, re, os<br />tags = [] # Modified global variable!<br />def main():<br /> args = sys.argv[1:]<br /> for filename in args:<br /> treat_file(filename)<br /> if tags:<br /> fp = open('tags', 'w')<br /> tags.sort()<br /> for s in tags: fp.write(s)<br />expr = '^[ /t]*(def|class)[ /t]+([a-zA-Z0-9_]+)[ /t]*[:/(]'<br />matcher = re.compile(expr)<br />def treat_file(filename):<br /> try:<br /> fp = open(filename, 'r')<br /> except:<br /> sys.stderr.write('Cannot open %s/n' % filename)<br /> return<br /> base = os.path.basename(filename)<br /> if base[-3:] == '.py':<br /> base = base[:-3]<br /> s = base + '/t' + filename + '/t' + '1/n'<br /> tags.append(s)<br /> while 1:<br /> line = fp.readline()<br /> if not line:<br /> break<br /> m = matcher.match(line)<br /> if m:<br /> content = m.group(0)<br /> name = m.group(2)<br /> s = name + '/t' + filename + '/t/^' + content + '//n'<br /> tags.append(s)<br />if __name__ == '__main__':<br /> main()<br />

 

ptags.py(for python3.x)

 

#! /usr/bin/env python3<br /># ptags<br />#<br /># Create a tags file for Python programs, usable with vi.<br /># Tagged are:<br /># - functions (even inside other defs or classes)<br /># - classes<br /># - filenames<br /># Warns about files it cannot open.<br /># No warnings about duplicate tags.<br />import sys, re, os<br />tags = [] # Modified global variable!<br />def main():<br /> args = sys.argv[1:]<br /> for filename in args:<br /> treat_file(filename)<br /> if tags:<br /> fp = open('tags', 'w')<br /> tags.sort()<br /> for s in tags: fp.write(s)<br />expr = '^[ /t]*(def|class)[ /t]+([a-zA-Z0-9_]+)[ /t]*[:/(]'<br />matcher = re.compile(expr)<br />def treat_file(filename):<br /> try:<br /> fp = open(filename, 'r')<br /> except:<br /> sys.stderr.write('Cannot open %s/n' % filename)<br /> return<br /> base = os.path.basename(filename)<br /> if base[-3:] == '.py':<br /> base = base[:-3]<br /> s = base + '/t' + filename + '/t' + '1/n'<br /> tags.append(s)<br /> while 1:<br /> line = fp.readline()<br /> if not line:<br /> break<br /> m = matcher.match(line)<br /> if m:<br /> content = m.group(0)<br /> name = m.group(2)<br /> s = name + '/t' + filename + '/t/^' + content + '//n'<br /> tags.append(s)<br />if __name__ == '__main__':<br /> main()<br />

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.