Can someone explain ctags to me?

458 views
Skip to first unread message

Oliver Taylor

unread,
Sep 6, 2011, 12:17:13 AM9/6/11
to bbe...@googlegroups.com
Can someone explain ctags to me? I gather that it is a list of words which BBEdit looks for matches against when doing text coloring, populating the function menu, and completion - but beyond that I'm clueless. I tried to look at the ctags docs and knew instantly my kung-fu was too weak.

Assume I have no neck beard.
Assume I'm interested in using ctags for text coloring, populating the function menu, and completion.
Assume I've read the manual and still don't understand what ctags are. At all.

I'm also willing to accept that ctags might be a very difficult way to get what I want (codeless modules and clippings might be easier). But I want to know about BBEdit's full capabilities.

Alex Satrapa

unread,
Sep 6, 2011, 7:58:57 AM9/6/11
to bbe...@googlegroups.com
On 6 Sep 2011, at 14:17, Oliver Taylor wrote:

> Can someone explain ctags to me? I gather that it is a list of words which BBEdit looks for matches against when doing text coloring, populating the function menu, and completion - but beyond that I'm clueless. I tried to look at the ctags docs and knew instantly my kung-fu was too weak.

The short, short version:

1) Open Terminal
2) cd to the directory where your code is
3) type "bbedit --maketags ."

The important bit, of course, is that you'll need to repeat that process any time you modify your code.

Now when you look at your code in BBEdit you'll see new colours for words corresponding to terms defined elsewhere. Right-click on the word to bring up the context menu, and you'll see an entry for "definitions", which then presents a sub-menu of locations where the term has been defined.

The "--maketags" option is mentioned in passing on p296 of the BBEdit User Guide. Search for "ctags" and you'll find plenty of other information about this service.

Alex

Alex Popescu

unread,
Sep 7, 2011, 3:26:01 AM9/7/11
to BBEdit Talk
On Sep 6, 2:58 pm, Alex Satrapa <gr...@goldweb.com.au> wrote:
> On 6 Sep 2011, at 14:17, Oliver Taylor wrote:
>

[✄..clipped .. ✄]

> 3) type "bbedit --maketags ."
>
> The important bit, of course, is that you'll need to repeat that process any time you modify your code.
>
> Now when you look at your code in BBEdit you'll see new colours for words corresponding to terms defined elsewhere. Right-click on the word to bring up the context menu, and you'll see an entry for "definitions", which then presents a sub-menu of locations where the term has been defined.
>
> The "--maketags" option is mentioned in passing on p296 of the BBEdit User Guide. Search for "ctags" and you'll find plenty of other information about this service.
>

Can someone think of any way this could be automated? Using the Unix
Worksheet or some script that is aware of the current project
location, anything...

A://


> Alex
>
>  smime.p7s
> 2KViewDownload

François Schiettecatte

unread,
Sep 7, 2011, 12:15:04 PM9/7/11
to bbe...@googlegroups.com
You could use cron/crontab:

# > man 5 crontab


François

> --
> You received this message because you are subscribed to the
> "BBEdit Talk" discussion group on Google Groups.
> To post to this group, send email to bbe...@googlegroups.com
> To unsubscribe from this group, send email to
> bbedit+un...@googlegroups.com
> For more options, visit this group at
> <http://groups.google.com/group/bbedit?hl=en>
> If you have a feature request or would like to report a problem,
> please email "sup...@barebones.com" rather than posting to the group.
> Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

ascarter

unread,
Sep 7, 2011, 1:23:49 PM9/7/11
to bbe...@googlegroups.com
See the BBEdit manual - attach an AppleScript to documentDidSave event.

François Schiettecatte

unread,
Sep 7, 2011, 1:28:59 PM9/7/11
to bbe...@googlegroups.com
I have been playing around with this, I created the tags file in the root directory of my codebase but bbedit does not 'see it' (selecting 'Find Definition' from the menu does nothing and it does not appear in the contextual menu). Do I need to restart bbedit for the file to be picked up?

Thanks

François

ascarter

unread,
Sep 7, 2011, 2:08:05 PM9/7/11
to bbe...@googlegroups.com
No you shouldn't need to restart BBEdit. Do you see a "tags" file in the root of your project? Are you running in project mode in BBEdit? You can also open the tags file (it's just text) and see that it has your symbols in it.

ascarter

unread,
Sep 7, 2011, 2:09:22 PM9/7/11
to bbe...@googlegroups.com
And here's a script that you can run in BBEdit to build the tags file for the currently active project. I tried adding it as an attachment script for documentDidSave but it didn't seem to fire.

David Kelly

unread,
Sep 7, 2011, 4:25:15 PM9/7/11
to bbe...@googlegroups.com

Its easy to miss the dot on the end but the command is bbedit --maketags .
Or specify another directory.

If you don't list the dot then bbedit silently does nothing.

--
David Kelly N4HHE, dke...@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

François Schiettecatte

unread,
Sep 7, 2011, 5:10:47 PM9/7/11
to bbe...@googlegroups.com
Not at my end, it created the tags file without a dot at the end of the command, it was silent though, maybe a verbose mode...

François

Alex Popescu

unread,
Sep 7, 2011, 5:45:53 PM9/7/11
to BBEdit Talk
> https://github.com/ascarter/BBEdit-ApplicationSupport/blob/master/Scr...

Minutes after posting a question related to this script I've figured
it out:

1. name the script Document.documentDidSave.scpt

2. add the following code:

on documentDidSave()
makeTags()
end documentDidSave

3. when saving the script make sure that it is saved as Script + Run
only (nb: according to the documentation the script must be compiled
and I assume Run only option means it is compiled)

4. done

Thanks a lot,

A://

David Kelly

unread,
Sep 7, 2011, 5:56:39 PM9/7/11
to bbe...@googlegroups.com
On Wed, Sep 07, 2011 at 05:10:47PM -0400, Fran?ois Schiettecatte wrote:
> Not at my end, it created the tags file without a dot at the end of
> the command, it was silent though, maybe a verbose mode...

So it does. Didn't see the tags file the first time but duped the
directory, nuked the tags file, and ran it again which produced a new
tags file.

John Delacour

unread,
Sep 7, 2011, 6:20:37 PM9/7/11
to bbe...@googlegroups.com
At 14:45 -0700 07/09/2011, Alex Popescu wrote:

>3. when saving the script make sure that it is saved as Script + Run
>only (nb: according to the documentation the script must be compiled
>and I assume Run only option means it is compiled)

I have not experimented with the application under discussion but in
general any AppleScript script saved as such (and not simply as text)
will be compiled. The 'run only' option is simply to stop people
hacking the source code.

JD

François Schiettecatte

unread,
Sep 8, 2011, 7:44:06 AM9/8/11
to bbe...@googlegroups.com
The tags file is there and has stuff in it which all looks right. It is the root directory of my project directory hierarchy and I have created a bbedit project and added the files I needed, and the tags is located further up in the hierarchy.

François

On Sep 7, 2011, at 2:08 PM, ascarter wrote:

> No you shouldn't need to restart BBEdit. Do you see a "tags" file in the root of your project? Are you running in project mode in BBEdit? You can also open the tags file (it's just text) and see that it has your symbols in it.
>

Alex Popescu

unread,
Sep 21, 2011, 9:07:34 AM9/21/11
to BBEdit Talk
I'm reviving this thread to post some more updates and also ask for
some help.

1. As mentioned previously I've used ascarter's script as a
Document.documentDidSave. After doing it BBEdit started to freeze even
if I wasn't just navigating through a project.

The reason for BBEdit freezing was exactly the tags generation script.
BBEdit is performing autosaves and so it was triggering it very often.
In fact it was so often that made BBEdit almost unusable with decently
sized projects.

Lesson learned: don't use the tags generation script as a
documentDidSave event, except you can filter an actual source file
save.

2. The conclusion above made me think that a better event to attach
the tags generation would be the Save action. So I've transformed it
into a File • Save script.

This approach has solved the above issue, but the script in the
current form was not able to make the difference between a real
project (with a folder structure) and a bunch a separate files opened
in the current window.

3. This is the part where I'm asking for help. The script in the
current form can be found at the end of the post. What I'd need help
with is:

3.1 Is there a way to determine if the current window belongs to a
real project vs a set of random files?

3.2. Could someone validate the following code snippet which tries to
get the parent folder if the main script result is a file:


if kind of (info for theFile) is "folder" then
-- Run the maketags script
set thePath to POSIX path of theFile
else
set thePath to POSIX path of (container of theFile)
end if
set cmd to "cd " & thePath & "; /usr/local/bin/bbedit --maketags"

3.3 Is there a way to install this script and trigger it through a
menu item/shortcut against the current project instead of having it
automatically triggered by the Save action?

on menuselect(menuName, itemName)
if menuName = "File" and itemName = "Save" then
-- must return false to let the application do the work
return false
end if
end menuselect
on postmenuselect(menuName, itemName)
-- this is called after the application has processed
-- the command
if menuName = "File" and itemName = "Save" then
my makeTags()
end if
end postmenuselect

on makeTags()
set theFile to missing value

tell application "BBEdit"
set activeWindow to front window

if (class of activeWindow is project window) then
set projectDocument to project document of activeWindow

if ((count of items of projectDocument) > 0) then
set firstFileItem to item 1 of projectDocument as alias
else
set firstFileItem to file of document of activeWindow as alias
end if

if (on disk of projectDocument) then
set theProjectFile to file of projectDocument as alias

tell application "Finder"
set theProjectDir to container of theProjectFile
set firstFileDir to container of firstFileItem
end tell

if (firstFileDir is equal to theProjectDir) then
-- Use project file
set theFile to theProjectDir as alias
else
-- External project file -> use first item to set context
set theFile to firstFileItem
end if
else
-- BBEdit doesn't provide direct access to the Instaproject root
-- Use the first node from the project list
set theFile to firstFileItem
end if
end if
end tell

if theFile is equal to missing value then
-- No base file found for reference
-- Signal error by beep and end
-- beep
else
if kind of (info for theFile) is "folder" then
-- Run the maketags script
set thePath to POSIX path of theFile
else
set thePath to POSIX path of (container of theFile)
end if
set cmd to "cd " & thePath & "; /usr/local/bin/bbedit --maketags"
do shell script cmd
end if
end makeTags

Many thanks in advance for the help,

A://

ascarter

unread,
Sep 25, 2011, 1:44:27 PM9/25/11
to bbe...@googlegroups.com


On Wednesday, September 21, 2011 6:07:34 AM UTC-7, Alex Popescu wrote:
Lesson learned: don't use the tags generation script as a
documentDidSave event, except you can filter an actual source file
save. 

2.  The conclusion above made me think that a better event to attach
the tags generation would be the Save action. So I've transformed it
into a File • Save script.

This approach has solved the above issue, but the script in the
current form was not able to make the difference between a real
project (with a folder structure) and a bunch a separate files opened
in the current window.  

3. This is the part where I'm asking for help. The script in the
current form can be found at the end of the post. What I'd need help
with is:

3.1 Is there a way to determine if the current window belongs to a
real project vs a set of random files?

My script was trying to guess just that. But it relies on some circumstantial evidence. If there is a real project file, the line:

set projectDocument to project document of activeWindow

This should set projectDocument to a "project" that is on-disk or an insta-project.

It gets dicey after that. I am a Rails and Python programmer mostly. So I use insta-projects on the root of my Rails source tree typically. So in general, finding the first item's parent directory works. But that is not a very reliable means. There doesn't appear to be any other way.

A more complicated method would be to walk all the items of the project and find the actual tree or tree, running tags from the lowest part of the tree that is in common.

I think the safest bet is to create a real BBEdit project document where you want to run tags. That would serve as a placeholder. Alternately, maybe searching for your ".git" folder or some other identifying file could help the script automagically find the root to run tags.
 

3.2. Could someone validate the following code snippet which tries to
get the parent folder if the main script result is a file:


if kind of (info for theFile) is "folder" then
-- Run the maketags script
set thePath to POSIX path of theFile
else
set thePath to POSIX path of (container of theFile)
end if
set cmd to "cd " & thePath & "; /usr/local/bin/bbedit --maketags"


Are you sure you ever get into a state where the folder is the active document?

 
3.3 Is there a way to install this script and trigger it through a
menu item/shortcut against the current project instead of having it
automatically triggered by the Save action?


I just put this script in my BBEdit/Scripts folder so it shows up in the scripts menu. From there, I just assigned a shortcut key.
Reply all
Reply to author
Forward
0 new messages