Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Ahk support in ctags. Teleport around in your code (done)


  • Please log in to reply
25 replies to this topic
urlwolf
  • Members
  • 150 posts
  • Last active: Feb 26 2012 07:56 PM
  • Joined: 16 Mar 2006
I finally got done testing the regular expressions to have autohotkey support in ctags. (see the post that started it here).

There are some things that you get with a ctags-based function list that you don't get in other editors with function lists, described in 'features'.

Posted Image
Features

[*:1jj1i0ti] you can have multiple function lists from multiple files opened simultaneously.

[*:1jj1i0ti]Hoover over a function to see a popup with its definition.

[*:1jj1i0ti]The editor can list Variables , not only labels and functions.

[*:1jj1i0ti]When you add a function in your code, it automatically gets added to the function list. Same with variables

[*:1jj1i0ti]Vim highlights the function where the cursor is; good for navigating large files without the need of folding (btw, vim supports folding too).

[*:1jj1i0ti]Teleporting to a function definition that is in a different file (#includes) is possible.
Known bugs and limitations
Ctags uses posix regex. To my knowledge, there's no way of doing multiline matchings (but I'm not a regex wizard, I may be wrong). As a consequence, the regular expressions I have here will work with One true brace (OTB) style code ONLY.
This means that in the following code, the first function will be matched, but not the second. It's pretty easy to fix your code so all your functions are OTB-style though.

foo(){
}
faa()
{
}

Install and run
The list is Editor-independent. Let's create a tags file to use. We will use the command line, but this is just for you to see the process. Normally your editor will do all this for you.


[*:1jj1i0ti]First, you need to Download exhuberant ctags and uncompress the binaries to a folder. You will have ctags.exe and maybe tags.cnf.

[*:1jj1i0ti]Add the directory where ctags lives to the path. Go to my computer > properties > advanced > environment variables > all users > double click "PATH" and add ;/path/to/where/ctags/is/. Click Ok.

[*:1jj1i0ti]create a file ctags.cnf and paste the following in it:
--langdef=autohotkey
--langmap=autohotkey:.ahk
--regex-autohotkey=/^[ \t]*([a-zA-Z0-9_]+)\:\s*$/\1/l,label/
--regex-autohotkey=/^[ \t]*([a-zA-Z0-9_]+)[ |\t]*:?=/\1/v,variable/
--regex-autohotkey=/^[ \t]*(NOTE|note|Note)[ \t]*\:[ \t]*(.*)/\2/n,note/
--regex-autohotkey=/^[ \t]*(TODO|todo|Todo)[ \t]*\:[ \t]*(.*)/\2/t,todo/
--regex-autohotkey=/^([a-zA-Z0-9_@]+)[ \t]*\(.*\)[ \t]*{/\1/f,function/


The locations checked for a ctags.cnf file are as follows:

/ctags.cnf (i.e. on current drive)
$HOME/ctags.cnf
$HOMEDRIVE$HOMEPATH/ctags.cnf
./ctags.cnf


Made sure that you drop your config file in the $HOME folder (if you don't have a HOME variable when looking at the environment variables, add it). This way ctags supports ahk anywhere in the filesystem.


:shock: edit: it seems that bbcode gets confused/doesn't allow brakets inside the code sections? formatting looks good when using preview with bbCodeEditor, but not when I submit it to the forum. Sorry about the large fonts.

[*:1jj1i0ti] To make sure ctags works even before opening an editor, open a command line and type ctags yourAhkScript.ahk. This will prodice a 'tags' file in the same directory the script is. Look at the 'tags' file. You should have a list of your functions, together with a regular expression that tells the program how to find it in the code.

If you don't want to touch a command line, just fire your editor and make it show the function list. In vim, you just open the file ahd type :TlistToggle.
That's all you need. Your editor will take it from there.

I'll assume that you got your ctags working. I use vim for this example. If you get your editor to work, please post what you did here.
Here are the steps for working with Vim and Ctags to get results similar to the above screenshot.

[*:1jj1i0ti] install vim. You can also use cream if you don't want to learn the modal way (esc goes to normal mode, where what you type does not insert stuff; some people are scared by it.). Cream is a wrapper for vim that makes it work like a 'normal' editor. It takes plugins etc exactly like vim. I've never used it though, as once you learn to use vim you feel like you have superpowers moving around in text and you never look back.
[*:1jj1i0ti] Do all the steps in the previous list. Make sure there's a tags file in the folder where your script is.
[*:1jj1i0ti] Install taglist.vim. Follow instructions in that webste.
[*:1jj1i0ti] Edit /vimfiles/plugins/taglist.vim, and add autohotkey support by pasting this:
" autohotkey language
let s:tlist_def_autohotkey_settings = 'autohotkey;l:label;f:function;v:variable;n:note;t:todo;h:hotkey'

Save and close.
[*:1jj1i0ti] Open your ahk file with vim. Pres esc to enter normal mode. Type colon. You will be in the command line. Type:
TlistToggle
You should see the function list.
I'll keep working on the regular expressions. If anyone knows how to make it work with non-OTB functions, let me know.

Also, I want to know why ctags doesn't follow the links in the #includes.

Happy testing.

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005

As a consequence, the regular expressions I have here will work with One true brace (OTB) style code.


Perhaps you should add the word "Only"

Otherwise pretty cool looking. I don't want to use vim or emacs, though.

urlwolf
  • Members
  • 150 posts
  • Last active: Feb 26 2012 07:56 PM
  • Joined: 16 Mar 2006
Thanks engunneer. Fixed that.

You don't have to use vim or emacs. You can use:
Jedit, slickEdit, multiEdit2006, Notepad++,Crimson Editor, EditPlus or UltraEdit:
<!-- m -->http://openctags.sourceforge.net/<!-- m -->

... and who knows which other editors.
Details here:
<!-- m -->http://www.autohotke... ... ight=ctags<!-- m -->.

The actionscript post linked there explains how to configure Jedit. the openctags link does the same for all the other editors.

Bummer about PSPad, as it seems to be a favorite here.

Mustang
  • Members
  • 421 posts
  • Last active: Dec 26 2010 10:08 PM
  • Joined: 17 May 2007
Im still using trusty old Notepad atm

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
I used to use UltraEdit, until I decided that I didn't want to "borrow" software anymore (And I couldn't afford it back then). I will have to see if ctags works with PSPad, or I could just buy UE, but I would need 4 copies minimum :shock:

urlwolf
  • Members
  • 150 posts
  • Last active: Feb 26 2012 07:56 PM
  • Joined: 16 Mar 2006
UPDATE:
function regex now matches functions with default vaules in arguments. Example

ISENSE_Init( lastGUI=0, subMenu="", bStandalone=true ){

variable regex changed to match only := (it gets lots of variables this way, and skips the false alarms in ifs.

Also, testing #includes, I realize that you just have to call ctags with the -R flag. It works.

Now I only have to figure out how to tweak taglist.vim (1000s of lines of code!) to make all calls recursive.

Slow but steady progress.

A future compiled version will remove the pain of having to use a .cnf file.

Teleporting gives a whole new way of looking at code.

I'm stopping now for a rest and then do some actual work :)

I'm looking forward to ctags working with some other editor (just to see how easy it is; I love vim so I won't move). Also, if you are agnostic -i.e. don't hate vim, just the modal interface-, try to use cream too.

ahklerner
  • Members
  • 1386 posts
  • Last active: Oct 08 2014 10:29 AM
  • Joined: 26 Jun 2006

I used to use UltraEdit, until I decided that I didn't want to "borrow" software anymore (And I couldn't afford it back then). I will have to see if ctags works with PSPad, or I could just buy UE, but I would need 4 copies minimum :shock:

You should check out SciTE. If this supports Notepad++ (which I think is Scintilla based) it should work with SciTE, too.

urlwolf
  • Members
  • 150 posts
  • Last active: Feb 26 2012 07:56 PM
  • Joined: 16 Mar 2006
I think notepad++ offers support through a plugin.
Not sure if SciTE has the same plugin.

What makes you take SciTE over notepad++?

urlwolf
  • Members
  • 150 posts
  • Last active: Feb 26 2012 07:56 PM
  • Joined: 16 Mar 2006
Something cool I just found.
In vim, if you press gd (go to definition) in any occurrence of a function in your code, it will highlight where it is in the function list.

It works for variables too. do gd, and it takes you to where the variable was first initialized. How cool is that? Once you know what you wanted to know, you can get back to where you were by pressing ctrl + o (old position).

ahklerner
  • Members
  • 1386 posts
  • Last active: Oct 08 2014 10:29 AM
  • Joined: 26 Jun 2006

I think notepad++ offers support through a plugin.
Not sure if SciTE has the same plugin.

What makes you take SciTE over notepad++?

I have both, but prefer SciTE.
I do not normally need all of the fluff that notpad++ has.

urlwolf
  • Members
  • 150 posts
  • Last active: Feb 26 2012 07:56 PM
  • Joined: 16 Mar 2006
Update.
First, I have found a solution for the config file having to be in the same dir as the script.
This is no longer needed.

I got an answer from Darren, the author of ctags:

The locations checked for a ctags.cnf file are as follows:

/ctags.cnf (i.e. on current drive)
$HOME/ctags.cnf
$HOMEDRIVE$HOMEPATH/ctags.cnf
./ctags.cnf


So I just made sure that I drop my config file in the $HOME folder (if you don't have a HOME variable when looking at the environment variables as described in the original post, add it). This way ctags supports ahk anywhere in the filesystem.

I have updated the original post.

The regular expression for functions is improved too. Before it Skipped some functions. Now, there's the danger of detecting IFs as functions ( false alarms). I have been struggling for a while to exclude strings that start with IF, but negation is not easy with one single regular expression.

A last resort is to change the code for taglist and make it exclude the IFs.

So, right now, beware that you'll find lots of IFs in the function list.

I only wish Autohotkey were easier to parse. Having something to mark function definitions would save all this trouble.

People who have done syntax highlighting for ahk might have faced the same problems.

urlwolf
  • Members
  • 150 posts
  • Last active: Feb 26 2012 07:56 PM
  • Joined: 16 Mar 2006
UPDATE:
problem with false alarms and IFs solved.

added support for TODOs (it will list them on the side bar).

The only problem that remains is OTB support only.

  • Guests
  • Last active:
  • Joined: --

The only problem that remains is OTB support only.

...OTB-only support is not a "problem"...everyone should use OTB (also no one should use vim, but that's a-whole-nother issue...I can't even explain the pains I went thru trying to edit text via SSH on a Linux box {with vi or vim}, until I found out about pico...pico = normal, vim = headache {you can't even Exit if you accidentally run vi/vim}...don't get me wrong, I'm open to someone explaining to me exactly how to use vim {& why?} {no don't tell me to read the man page, it hasn't helped yet} but I don't understand "learning how to use vim" when using a normal editor/pico (Linux)/Notepad2 (Windows) is already intuitive)...but your OTB example should be...

test() {
}
...not...

test(){
}
...it needs a space there...also (I didn't check) but if putting a space there breaks your regex, then that does need fixed...

Also...do I even need to mention not posting big-ass screenshots (horiz scrolling!)?...can't people make a small version & link to the big?...

urlwolf
  • Members
  • 150 posts
  • Last active: Feb 26 2012 07:56 PM
  • Joined: 16 Mar 2006
the regex works with any amount of space anywhere.

I'm not sure OTB is the norm for ahk code. I have seen all kind of indentation. For example Auto-Syntax-Tidy 'corrects' indentation for ahk. It offers 3 types of indentation, none of them is OTB.

The limitation of ctags is that the regex is applied line by line. Then when the curly brace is in a different line, it doesn't match. One could compile ctags with a different regex engine, but that's too much trouble. I write OTB code anyway, and it's easier to fix the one I download from here.

As I said before you can use ctags with a plethora of editors, the ones you call 'normal'. I invested a week of my life learning vim and it's probably one of the most profitable time investments I've done. It's true that you need to learn 'vim-fu', and it's easier if there's someone around that already use it.

Saying things like "everyone should use OTB" also "no one should use vim" is not going to convince anyone either way.

canta
  • Members
  • 49 posts
  • Last active: May 30 2013 03:08 PM
  • Joined: 30 May 2006
urlwolf: I get the list up with TlistToggle on any other file than ahk. I get this message on ahk files:
"E:\MyAhkFile.ahk" 0L, 0C
Taglist: Failed to generate tags for E:\MyAhkFile.ahk
Press ENTER or type command to continue
^@E:\>E:\programfiler\ctags57\ctags.exe -f - --format=2 --excmd=pattern --fields=nks --sort=no --language-force=a
utohotkey --autohotkey-types=lfvnth "E:\MyAhkFile.ahk" ^@ctags.exe: Unknown language "autohotkey" in "language-fo
rce" option^@
Do you know if there is something i have missed when installing?