[comp.editors] a few MicroEmacs 3.10 command procedures

jrl@images1.Waterloo.NCR.COM (john Latala) (08/23/90)

TIMESTAMPING buffer contents
----------------------------

A while ago I said I was looking for a way to put a date/time stamp into
the file I was editting using MicroEmacs (3.10 in this case). After a
bit of poking I came up with something that appears to be working.

The command procedure looks for the following two lines:

	Edit-Date: [Wed Aug 22 13:57:41 EDT 1990]
	Edit-Vers: [8]

The 'Edit-Date:' is just the date and time that the file was being
editted on and the 'Edit-Vers:' is just a number that gets incremented
each time the file is editted.

The command procedure will not insert these into the file if they're not
there and it won't do anything with a VIEW mode buffer.

The commands needed in your emacs.rc file are shown below:

-------------------- Start of emacs.rc fragment --------------------

... near the top of the emacs.rc file ...

;		Edit-Date: [Wed Aug 01 10:05:13 199]
;		Edit-Vers: [0]

... near the bottom of the emacs.rc file ...

;
;>>> EDITDATE.CMD - adjust the edit date and edit number
;
;	The edit date is the date that the file was last editted on. The
;	edit number is just an incrementing number that gets bumped whenever
;	the file is editted.
;
;	Formats of the edit date and edit number field are:
;
;		Edit-Date: [Wed Aug 01 10:05:13 199]
;		Edit-Vers: [0]
;
;	You should also put the two above lines at the top of your
;	emacs.rc file so that whenever you edit it they wlll get
;	updated!
;
22	store-macro
	write-message "[Checking if buffer is in VIEW mode]"
	!if &equal &band $cmode 16 0
		write-message "[Looking for Edit Date]"
		beginning-of-file
		!force search-forward &cat "Edit-Date: " "["
		!if &seq $status TRUE
			0 set-mark
			search-forward "]"
			1 backward-character
			kill-region
			insert-string $time
		!endif
		write-message "[Looking for Edit Version]"
		!force search-forward &cat "Edit-Vers: " "["
		!if &seq $status TRUE
			0 set-mark
			search-forward "]"
			1 backward-character
			kill-region
			insert-string &add $kill 1
		!endif
		beginning-of-file
		clear-message-line
	!endif
!endm
;
set $writehook execute-macro-22
--------------------- End of emacs.rc fragment ---------------------

You can use any macro number that is convenient. There are a few nice
things about attaching this procedure to the $writehook:

	- If you're writing out a file you probably want it timestamped

	- if you're writing out a file it's probably 'dirty' anyway so
		making another change shouldn't mess things up to much.

	- if you do a <ESC>-Z only the 'dirty' buffers will be stamped
		just before they're written out.

One other thing to notice is the format of the two search commands that
are used:

		!force search-forward &cat "Edit-Date: " "["
		!force search-forward &cat "Edit-Vers: " "["

The '&cat "Edit-Vers: " "["' construct is used so that if this macro
is run on itself it will NOT change the two search commands!

Adding and Removing line numbers
--------------------------------

Here are two procedures that will add or remove line numbers from the
current buffer.

I leave these in files and execute them using:

	<ESC>-X execute-file addln.cmd
or
	<ESC>-X execute-file removeln.cmd

The format of the inserted line numbers is:

	xxxx:<TAB>

where the first 'x' is actually in column 1. The line numbers are NOT
fixed width but the text lines up nicely because of the <TAB> character
at the end of the line number.

The removeln.cmd procedure actually searches for each ':<TAB>' and
removes everything from there back to the beginning of the current line.
This may wreak havoc if you do a removeln.cmd on an assembler source
without a matching previous addln.cmd being done (many assemblers use
the ':' to delimit labels!).

-------------------- Start of addln.cmd --------------------
;
;>>> ADDLN.CMD - add line numbers to the current buffer
;
	end-of-file
	set %lines $curline
	set %line 1
	beginning-of-file
;
	!while &less %line %lines
		insert-string %line
		insert-string ":	"
		beginning-of-line
		update-screen
		next-line
		set %line &add %line 1
	!endwhile
	beginning-of-file
	beginning-of-line
	update-screen
--------------------- End of addln.cmd ---------------------

-------------------- Start of removeln.cmd --------------------
;
;>>> REMOVELN.CMD - remove line numbers from the current buffer
;
	end-of-file
	set %lines $curline
	set %line 1
	beginning-of-file
;
	!while &less %line %lines
		search-forward ":	"
		set-mark
		beginning-of-line
		kill-region
		update-screen
		next-line
		set %line &add %line 1
	!endwhile
	beginning-of-file
	beginning-of-line
--------------------- End of removeln.cmd ---------------------

Continuous LINE/COL display
---------------------------

A few people here use this to keep the line/col displayed on the screen.
It slows the editor up something fierce but in some cases it does prove
useful.

-------------------- Start of linecol.cmd --------------------
;
;>>> DEFINE: Display line and column macro
;
30	store-macro
	write-message &cat &cat &cat "Line: " $curline "  Col: " $curcol
!endm
;
set $cmdhook execute-macro-30
--------------------- End of linecol.cmd ---------------------

Somebody said that you could actually save the old line/column and only
display the new ones if they've changed. I'm not sure whether it's worth
the effort.

Notes
-----

* Depending on the complexity of your emacs.rc file you may have to change
  the macro numbers used by some of the above routines when they define
  macros.

* Do what you want with these things.

* If you come across any bugs or weirdnesses I would like to know about
  them.

--
john.Latala@Waterloo.NCR.COM