Plan 9: Code Assist


Jump to definition with ctags 

 

Use ctags for jump-to-defintion. Also, you can use Button3 in acme to jump to include files - both system and local headers.

readtags uses binary search. So it makes jump to symbol faster on a large codebase. 

Note:

  • Use sig command for signature lookup. 
  • Use src command for jumping to Plan 9 command sources. 

If you have mounted contrib sources, use this. Alternatively, download tar from https://ctags.sourceforge.net/ .

Make following changes:

  1. in Makefile, add '-DHAVE_REMOVE' (remove() instead of unlink()) and '-DHAVE_OPENDIR' (for directory recursion option) to DEFS
  2. in config.h, remove or comment out the definition of INT_MAX 
# Use one below to enable APE
bind -b /$objtype/bin/ape /bin
ape/psh 
 
# ls doesn't support -di option in Plan 9
sed 's/ls -di/ls/' configure > configure.new
mv configure.new configure
 
# Fake presence of egrep
cp /bin/grep /$objtype/bin/
 
./configure 
# ctags
make
# readtags needs explicit build 
make readtags 
cp ctags readtags /$objtype/bin
 
# Generate tags file for a single file
ctags main.c
# Generate tags file for all files recursively.
# Use absolute directory path for absolute path generation
ctags -R .
 
# Write output to stdout
ctags -f - main.c
 

readtags also support prefix (-p) and ignorecase (-i) match. -e includes extended fields in the output.

ctags --'list-kinds=C'
c  classes
d  macro definitions
e  enumerators (values inside an enumeration)
f  function definitions
g  enumeration names
l  local variables [off]
m  class, struct, and union members
n  namespaces
p  function prototypes [off]
s  structure names
t  typedefs
u  union names
v  variable definitions
x  external and forward variable declarations [off]
 


Tag file

#!/bin/rc
sym=$1
res=`{readtags $sym}
if(~ $#res 0){echo no tag: $sym >[1=2]; exit 1}
file=$res(2); addr=$res(3)
plumb -d edit $file:$addr
 


Code Outline with ctags

Use ctags extended format (-x) to create code outline.

ctags -x main.c | awk '{printf "%-20s %-10s %s\n", $4":"$3, $2, $1}' | sort -k3
main.c:47            macro      ANCHOR_BUF_SIZE
main.c:48            macro      ANCHOR_SIZE
main.c:404           macro      CLOCKS_PER_SEC
main.c:408           macro      CLOCKS_PER_SEC
main.c:402           macro      CLOCK_AVAILABLE
main.c:407           macro      CLOCK_AVAILABLE
main.c:57            macro      boolean
main.c:63            macro      boolean
main.c:417           macro      clock
main.c:89            macro      plural
main.c:485           macro      timeStamp
main.c:518           macro      timeStamp
main.c:94            member     bytes
main.c:94            member     files
main.c:94            member     lines
main.c:94            variable   Totals
main.c:98            variable   VERsion
main.c:116           function   addTotals
main.c:409           function   clock
main.c:220           function   createTagsForAmigaWildcard
main.c:335           function   createTagsForArgs
main.c:286           function   createTagsForEntry
main.c:311           function   createTagsForWildcardArg
main.c:172           function   createTagsForWildcardEntry
main.c:189           function   createTagsForWildcardUsingFindfirst
main.c:358           function   createTagsFromFileInput
main.c:384           function   createTagsFromListFile
main.c:464           function   etagsInclude
main.c:125           function   isDestinationStdout
main.c:525           function   main
main.c:469           function   makeTags
main.c:420           function   printTotals
main.c:247           function   recurseIntoDirectory
main.c:142           function   recurseUsingOpendir
  

outline -i will print Font Awesome icons instead of kind. If the font is not available, you'll see notdef symbols.

 

Call Hierarchy

Print call hierarchy of function main. This is similar to cflow on UNIX. You can pass multiple files. If the a function definition is missing in the input files, it shows up as [external]. 

Options:

p    Add include paths

w    Indent width

D,I,U    Define, Include and Undefine flags are passed to cpp  

Source: https://9p.io/sources/plan9/sys/src/cmd/calls.c

term% calls -f main /sys/src/cmd/cat.c

1   main
2    cat
3        read [external]
4        write [external]
5        sysfatal [external]
6    open [external]
7    sysfatal [external]
8    close [external]
9    exits [external]
 


Font

Change the font for a window. 

# Via tag command
Font /n/ttf/dejavusans.ttf.28/font
 
# Via ctl write (note the small case)
echo font /n/ttf/dejavusans.ttf.28/font >  /mnt/acme/$winid/ctl
 
# Read the current configuration
# Fields: id tagchars bodychars isdir ismod width fontname tabwidth
cat /mnt/acme/$winid/ctl
 


Acme Scripting

"The environment of such commands contains the variable $% with value set to the filename of the window in which the command is run, and $winid set to the window's id number."

# select lines 2 to 5
echo 2,5 > /mnt/acme/$winid/addr
echo dot=addr > /mnt/acme/$winid/ctl
echo show > /mnt/acme/$winid/ctl
 

 

Lessons Learnt

  1. Use ctags --excmd=number for maximum compatibility across editors i.e. use line number instead of regex patterns generated for tag search.

 

Troubleshooting

This makes pooldel and free check every block on each operation, so it aborts at the corrupting write's neighbourhood, not much later.

#include <pool.h>
mainmem->flags |= POOL_PARANOIA | POOL_ANTAGONISM;
 

 

Code

  1. https://gitlab.com/atamariya/plan9front/-/blob/front/rc/bin/code/gentags
  2. https://gitlab.com/atamariya/plan9front/-/blob/front/rc/bin/code/outline
  3. https://gitlab.com/atamariya/plan9front/-/blob/front/rc/bin/code/jump 
  4. https://gitlab.com/atamariya/plan9front/-/blob/front/sys/src/cmd/calls.c 

 

Comments

Popular posts from this blog

Plan 9 : The Infinity Notebook

Plan 9: Quick Boot with UEFI

Plan 9: Key Visualizer