[comp.emacs] dabbrev3 for Freemacs

nelson@IMAGE.SOE.CLARKSON.EDU (Russ Nelson) (02/09/90)

I've improved dabbrev2 a little bit.  It now keeps track of anything it
offered, and skip past anything you've seen before.
-russ

---Michael Hirsch recently sent us all a dynamic abbreviation mode.  I
---had not previously been aware of this ability of GNU Emacs, and
---thank him for the information.  However, I decided to expand upon
---what Michael wrote, and these are the results.  I have reposted
---Michael's introduction, and interspersed my comments prefixed
---with ---.  William Su

foobar
foobaz
foobar
foo		<< test the expansion here.
foobar
foobae
foobar



Here's a macro you might find useful.  It's a partial act-alike to the GNU
dabbrev command.  It does "dynamic abbreviation."  I.e., it completes the
word you are typing in by searching for the last word you typed in that
starts the same.  If no previous word starts the same it searches forward.

There is a variable, U-dabbrev-backwards-only, which, when null makes
dynamic abbreviation occur only with previous words--words occuring
earlier in the current buffer.
---This is a typo.  When U-dabbrev-backwards-only is NOT null, dabbrev
---occurs only with previous words.

The only feature of GNU emacs this is missing is the ability to give a
numeric argument to dabbrev which will then pick the nth previous word
that matches.  This didn't seem worth hacking.
---Modified by William Su, 8/16/89 to handle repeated calls via
---repetition of U:dabbrev.
---Modified by William Su, 8/16/89 to handle numeric arguments.---
---(I disagreed (no offense meant) with Michael about the value
---of these abilties.  Besides, I enjoy programming! |-) )


This is really handy if you are typing in long words frequently.  I find
it much handier than abbrev mode in GNU emacs.  I hope you do to.

This works pretty well, but it would be cleaner to do the search with a
regular expression.  Does anyone out there know how to search for a string
only at the beginning of a word?  It seems that the regular expression
facility of freemacs is not up to it.
---Whether the regular expression facility is up to searching for a string
---only at the beginning of a word or not, I think Michael's solution is
---probably faster because a regular expression search would probably be
---much more work.  (Comments, Russ?)  Those of us who still have
---4.77 MHz machines |-( like anything that helps efficiency.

Hope you like it,
--Michael
---I modifed the code style because Michael uses a different one
---from me, and I needed to be able to trace this easily when I was
---making my modifications.  The modifications were really not that
---difficult; they are really more an expansion of Michael's code
---than a rewrite.
---
---I added another variable:  U-dabbrev-fold.  When not null, a case-
---folded search is performed when matching the user's prefix to the
---other words in the buffer.  Case folding is, I would guess, much
---less expensive than regular expression searches, so those of us
---with slow machines may not mind this as much.  (Comments, Russ?)
---Nevertheless, for programming, case folding is probably
---undesirable, so I have left this defaulting to null.  If you are
---doing some form of English text editing, you may wish to turn this
---on.  The default in GNU Emacs appears to be case folding, even in
---c-mode; I don't know why.  You might want to make the default
---part of the local mode changes for each editing mode.  I haven't
---bothered; I figure I can set folding on if I discover I need it.
---
---I have also included bindings for dabbrev to M-/, matching GNU
---Emacs.  However, I believe the operation of dabbrev is independent
---of key binding.  If you want to repeat, just hit whatever you hit
---the first time.
---
---Any errors are my fault, not Michael's.  (Hopefully, there aren't
---any, but one never knows.)
---Hope you like it,
-----Bill

----- MINT code starts here -----

##(result)

Name:U:dabbrev
Dynamically finish the current word.  Check backwards through the
file to find the last word to start with this pattern of letters, then
complete the current word the same way.  If there is no previous such
word, and U-dabbrev-backwards-only is null, check forwards.
[*]	
#(an)
#(g?,arg1,1,(#(ds,dabbrev-number,arg1)))
#(es,#(ls,(,),dabbrev-seen.))
#(pm,5)
#(sm,3,<)
#(ds,dabbrev-search-string,##(rm,{))
#(lp,##(dabbrev-search-string),,,#(U-dabbrev-fold))
#(sp,{<)
#(==,#(Udabbrev,,([)),(expanded),,(
	#(==,#(U-dabbrev-backwards-only),,(
		#(sp,2>)
		#(==,#(Udabbrev,(#(sp,1)),(])),(expanded),,(
			#(an,Can't find dynamic abbrev(,) forwards or backwards(,) for ##(dabbrev-search-string).)
		))
	),(
		#(an,Can't find dynamic abbrev(,) backwards only(,) for ##(dabbrev-search-string))
	))
))
#(sp,2)
#(pm)
#(ds,dabbrev-number,1)
[*]


Name:Udabbrev
arg1 = null or #(sp,1) to go backwards or forwards.  Arg2 = [ or ] resp.
[*]
#(l?,.,arg2,0,1,(
	#(sp,0)
	#(mb,{,(
		arg1
		#(SELF,(arg1),(arg2))
	),(
		#(==,arg2,[,(#(sm,4,0)),(#(sm,4,1)))
		#(g?,#(dabbrev-number),1,(
			#(ds,dabbrev-number,#(--,#(dabbrev-number),1))
			#(sp,4)
			#(SELF,(arg1),(arg2))
		),(
			#(sp,1})
			#(n?,dabbrev-seen.##(rm,1),(
				#(sp,4)
				#(SELF,(arg1),(arg2))
			),(
				#(ds,dabbrev-seen.##(rm,1))
				#(ds,dabbrev-insert-string,##(rm,1))
				#(sp,2)
				#(sm,1,<)
				#(is,##(dabbrev-insert-string))
				#(Fset-new-mark)
				#(sp,1>)
				#(F:swap-point-and-mark)
				#(rd)
				#(ds,temp,#(g))
				#(==,##(K.##(temp)),U:dabbrev,(
					#(sp,3>)
					#(dm,2)
					#(sp,4)
					#(SELF,(arg1),(arg2))
				),(
					#(Fkbd-in,##(temp))
					expanded
				))
			))
		))
	))
))
[*]

Name:U-dabbrev-fold
If not null, dabbrev will fold case when searching for a
matching prefix.  The prefix the user typed is not changed.
The part copied is copied exactly as written in the original.
No attempt is made to make the case match that of the prefix.
[*][*]

Name:K.M-Slash
[*]U:dabbrev[*]

Name:K.M-/
[*]U:dabbrev[*]
-- 
--russ (nelson@clutx [.bitnet | .clarkson.edu])  Russ.Nelson@$315.268.6667
Violence never solves problems, it just changes them into more subtle problems