[comp.editors] find-tags for uEMACS

ram@altger.UUCP (Reimer A. Mellin) (11/01/88)

Hello,

After beginning of using the uEMACS, I missed one feature of VI very
soon: the Find-tags command. 

At the end of this Article there is now my solution, consisting basicly
of a short macro, which can be added to the uEMACS startup-file. 

Now you can simply move to the beginning of an identifier and activate
the macro (I bound it on my system to M-TAB, please change this as you
like).

After finding the 'TAGS'-file (created by the program 'ctags') and the
specified identifier, the macro splits your current window and displays
the start of the definition (Always in the lower window).  In the upper
window you will still see your old context. 

BUGS: the macro doesn't prompt for a name
      the TAGS file is loaded after the first search (running out of
      memory?)
      if you current window is 2 lines high, it can't be split
      the default search-pattern will be overwritten ...
      maybe unlimited .. :-)

I hope it will be useful for some of you ....

Greetings
	Reimer Mellin

PS: This was my first uEMACS-Macro, I am sure it can be made better :-)

----------------------CUT HERE-------------------------------------------
; Find-Tags Macro for uEMACS 3.8i and higher ....
; simply add to your '.emacsrc'-file
; Author: Reimer Mellin
; Date: 13.apr.88
; You can copy, modify, use, abuse this stuff as you wish
;
; If you use this Macro please send a small contribution to GreenPeace
; or to some organisation against famine in the third world !!!!!!!!

21      store-macro
	!if &not &band $cmode 2
		!return
	!endif			; only allowed in CMODE 
	set $discmd FALSE
	set %tmp01 $cwline	; remember line
	add-mode "magic"
	set-mark
!force  search-forward "[^0-9A-Za-z_]"	; search for end of identifier
        delete-mode "magic"
	backward-character
	copy-region		; get identifier
        !if &seq $kill ""
           set %var1 "no identifier specified"
          !goto error2
        !endif
        set %var1 &sub $wline $cwline
        %var1 next-line		; go to last line of window
        split-current-window	; so the new window will be the lower one
        previous-window
        %var1 previous-line
        exchange-point-and-mark
	set %tmp02 %tmp01
	!if &gre %tmp02 $wline
		set %tmp02 $wline
	!endif
	!if &les $cwline %tmp02
		&sub %tmp02 $cwline move-window-up
	!endif			; complicate calculation for restoring the
				; old context
	next-window
	!if &exist TAGS		; the TAGS-file ...
		find-file TAGS
	!else
                set %var1 "tags-file not found"
                previous-window
		!goto error1
	!endif
	add-mode "exact"	; for speed ....
	beginning-of-file	; rewind tags-file
!force  search-forward $kill
	!if &not $status
            set %var1 &cat $kill " not in tags-file"
*error1
!force 		delete-window
*error2
		write-message &cat "Fatal error: " %var1
		set $discmd TRUE
		!return
	!endif
        forward-character	; we were at end of identifier ...
	set-mark		; extract filename ..
	search-forward &chr 9
	backward-character
	copy-region
	set %var1 $kill
	3 forward-character	; extract pattern without '^' and '$' !!
	set-mark
	end-of-line
	2 backward-character
	copy-region
	set %pattern $kill
!force  find-file %var1		; look for filename
	!if &not $status
                set %var1 &cat %var1 " not found"
		!goto error1
	!endif
        beginning-of-file
        add-mode "exact"
!force  search-forward %pattern
	!if &not $status
                set %var1 &cat %var1 " is not as I remember (run ctags again)"
		!goto error1
	!endif
        delete-mode "exact"
        beginning-of-line	; found it ....
	set-mark
	set $discmd TRUE
!endm					;DONE

bind-to-key execute-macro-21 M-^I	; whatever you like ...
----------------------CUT HERE-------------------------------------------

/* E O F */

edf@rocky2.rockefeller.edu (David MacKenzie) (11/08/88)

Here is a modification of Reimar's MicroEMACS tag-finding macro.  I have
split it up into two front ends; one of them takes the tag from the
buffer starting at point (the way his does) and the other prompts for it
on the message line.

I also renamed some variables to make the code clearer, and made some
other improvements like respecting the '^' and '$' characters in the
tags file's regular expressions for more accuracy.

David MacKenzie
edf@rocky2.rockefeller.edu

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  tags.cmd
# Wrapped by dave@edfdc  on Tue Nov  8 01:27:26 1988
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'tags.cmd' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'tags.cmd'\"
else
echo shar: Extracting \"'tags.cmd'\" \(2884 characters\)
sed "s/^X//" >'tags.cmd' <<'END_OF_FILE'
X; Find-Tags Macro for MicroEMACS 3.8i and higher
X; Author: Reimer Mellin
X; Modified by: David MacKenzie
X; Date: 4 November 1988
X;
X; Goes to the tag in a second, bottom window.
X; Only allowed in CMODE.
X
X; Searches for the tag of the identifier starting at point.
X15	store-macro
X	run setup-tags
X	!if &not %ok
X		!return
X	!endif
X	set %savecm $cmode
X	add-mode "magic"
X!force  search-forward "[^0-9A-Za-z_]"	; search for end of identifier
X	set $cmode %savecm
X	backward-character
X	copy-region		; get identifier
X	!if &sequal $kill ""
X		write-message "No tag specified"
X		set $discmd TRUE
X		!return
X	!endif
X	set %ident $kill
X	run find-tags
X!endm
Xbind-to-key execute-macro-15 M-T
X
X; Searches for tag of the identifier typed in the message line.
X16	store-macro
X	run setup-tags
X	!if &not %ok
X		!return
X	!endif
X	set %ident @"Search for tag: "
X	run find-tags
X!endm
Xbind-to-key execute-macro-16 ^XT
X
Xstore-procedure setup-tags
X	set $discmd FALSE
X	!if &not  &band $cmode 2
X		write-message "Must be in CMODE"
X		set $discmd TRUE
X		set %ok FALSE
X		!return
X	!endif
X
X	set %startcwl $cwline	; remember line
X	set-mark
X	set %ok TRUE
X!endm
X
Xstore-procedure find-tags
X	write-message  &cat  "Searching for tag "  &cat %ident " . . ."
X
X	delete-other-windows
X	set %lfrombot  &sub $wline $cwline
X	%lfrombot next-line	; go to last line of window
X	split-current-window	; so the new window will be the lower one
X	previous-window
X	; complicated calculation for restoring the old context
X	%lfrombot previous-line
X	exchange-point-and-mark	; return to original point
X	!if &less $wline %startcwl
X		set %startcwl $wline
X	!endif
X	!if &less $cwline %startcwl
X		&sub %startcwl $cwline  move-window-up
X	!endif
X
X	next-window
X	!if &exist "tags"
X		find-file "tags"
X	!else
X		set %errmesg "No tags file"
X		previous-window
X		!goto error1
X	!endif
X
X	; find identifier in tags file
X	add-mode "exact"
X	add-mode "magic"
X	beginning-of-file	; rewind tags file
X!force  search-forward  &cat "^"  &cat %ident &chr 9
X	!if &not $status
X		set %errmesg &cat %ident ": No such tag in tags file"
X*error1
X!force 		delete-window
X*error2
X		write-message %errmesg
X		set $discmd TRUE
X		!return
X	!endif
X
X	; extract filename
X	set-mark
X	search-forward &chr 9	; find tab after filename
X	backward-character
X	copy-region
X	set %tagfile $kill
X
X	; extract search string
X	2 forward-character	; skip past tab /
X	set-mark
X	end-of-line
X	backward-character	; back up past /
X	copy-region
X	set %pattern $kill
X
X!force  find-file %tagfile
X	!if &not $status
X		set %errmesg  &cat %tagfile " not found"
X		!goto error1
X	!endif
X
X	; search for pattern in tagfile
X	beginning-of-file
X	set %savecm $cmode
X	add-mode "exact"
X	add-mode "magic"
X!force  search-forward %pattern
X	!if &not $status
X		set %errmesg "Pattern for tag not found; rerun ctags"
X		!goto error1
X	!endif
X	set $cmode %savecm
X
X	beginning-of-line	; found the tag
X	set-mark
X
X	clear-message-line
X	set $discmd TRUE
X!endm
END_OF_FILE
if test 2884 -ne `wc -c <'tags.cmd'`; then
    echo shar: \"'tags.cmd'\" unpacked with wrong size!
fi
# end of 'tags.cmd'
fi
echo shar: End of shell archive.
exit 0
-----
David MacKenzie
Environmental Defense Fund
edf@rocky2.rockefeller.edu (...rutgers!cmcl2!rocky2!edf)