%        File: lwords.sty
%          By: H.A.Trujillo
%     Created: Mon 16 Sep 2014 
% Last Change:      4 May 2019
%____________________________
%
%  LWords:  prints individual words of Lorem Ipsum dummy text.
%  		(contrast this to the "lipsum" package, which prints
%  		 paragraphs of Lorem Ipsum dummy text.)
%
%
%  Usage:
%
%       \lwords[i]{n}	print n lower-case words, beginning i words after
%			the end of the previous set of words printed.
%			    i defaults to 0
%			    i can be negative
%			    n must be less than 192 (the length of the word list)
%
%       \LWords		ditto, upper-case
%
%	\LWORDS		ditto, all-caps
%
%       \Lwords	   	ditto, with first word only capitalized.  
%			    (ie, a sentence without punctuation.)
%
%	\lwPush		saves the current position in the Lorem text
%	\lwPop		returns to the position saved using \lwPush
%			 -> The position may also be changed by altering
%			    the \lwStart counter.
%
%
%  Quirks/Bugs:
%  	- Will reset \lwStart to 1 if words would be used up.  Nicer if this
%  	  would use the word-list up then scroll to 1.
%
%	- If the count resets in sentence mode, the first word is from
%	  before the reset, but the remaining are from afterwards.
%
%	- Dies if more words than the word-list contains are requested in a
%	  single \lwords command.  
%
%
%  Acknowledgment:
%       The list of words was obtained from the first two paragraphs of the
%       Lorem Ipsum text in Patrick Happel's "lipsum" package, v1.2 (c) 2011,
%       with close duplicate words removed.
%


\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{lwords}
    [2015/07/12  v1.0  Individual words of Lorem Ipsum dummy text]


\newcount\lwStart		% word number on which to start
\lwStart=1
\newcount\lwStop		% word number on which to end
\chardef\lw@MaxWords=192	% length of word list


\newcommand{\lwords} 		{% lower-case
    \def\make@uc(##1){##1}%
    \lw@startstop}

\newcommand{\LWords} 		{% capitalized
    \def\make@uc(##1){\MakeUppercase##1}%
    \lw@startstop}

\newcommand{\LWORDS} 		{% all-caps
    \def\make@uc(##1){\MakeUppercase{##1}}%
    \lw@startstop}
    
\newcommand{\Lwords}[2][0]	{% sentence-style  (1st word upper, rest lower)
    \LWords[#1]{1}
    \count0=#2
    \advance\count0 -1
    \lwords{\count0}}


\newcommand{\lw@startstop}[2][0]{%
    \ifnum#2 > \lw@MaxWords 
	\PackageError{lwords}{Too many words requested!  192 words max}{}
	\fi
    \chardef\lw@nwords=#2
    \advance\lwStart #1
    \lwStop=\lw@nwords
    \advance\lwStop \lwStart
    \ifnum\lwStop > \lw@MaxWords
	\lwStart=1 
	\lwStop=\lw@nwords \advance\lwStop 1
	\fi
    \count255=1		% number of words gobbled  (printed or not)
    \let\next\GetWords	% set up recursion
    \GetWords
	lorem ipsum dolor sit amet consectetuer adipiscing elit ut purus
	vestibulum ut placerat ac adipiscing vitae felis curabitur dictum
	gravida mauris nam arcu libero nonummy eget consectetuer id
	vulputate a magna donec vehicula augue eu neque pellentesque
	habitant morbi tristique senectus et netus malesuada fames ac
	turpis egestas mauris ut leo cras viverra metus rhoncus sem nulla
	et lectus urna fringilla ultrices phasellus tellus sit tortor
	gravida placerat integer sapien est iaculis in pretium quis viverra
	ac nunc praesent sem vel leo ultrices bibendum aenean faucibus
	morbi nulla malesuada eu pulvinar at mollis curabitur auctor semper
	donec varius orci eget risus duis nibh mi congue accumsan eleifend
	sagittis quis diam duis eget orci sit amet orci dignissim rutrum
	    nam dui ligula fringilla euismod sodales sollicitudin vel wisi
	morbi auctor lorem non justo nam lacus libero pretium at lobortis
	vitae ultricies tellus donec aliquet tortor sed accumsan
	bibendum erat ligula aliquet magna vitae ornare odio metus mi morbi
	ac orci et nisl hendrerit mollis suspendisse ut massa cras nec ante
	pellentesque nulla cum sociis natoque penatibus magnis dis
	parturient montes nascetur ridiculus mus aliquam tincidunt urna
	ullamcorper vestibulum turpis pellentesque cursus luctus mauris
    ST@P}


\def\GetWords#1 #2ST@P{%	#1 = 1st word    #2 = remaining words
    \ifnum\count255 < \lwStart \else \make@uc(#1) \fi
    \advance \count255 1
    \ifnum \count255 = \lwStop 
      \global\lwStart = \count255 
      \def\next#2ST@P{\relax}\unskip
    \fi
    \next#2ST@P}


\newcommand{\lwPush}	{\chardef\SavedStart=\lwStart}
\newcommand{\lwPop}	{\lwStart=\SavedStart}


%----------------------------

% v0.4	20sep14	 Removed adventitious word-space.
%
% v1.0	12jul15	 Changed def of \GetWords to minimize recursion stack.
% 		 \lwords and \LWords no longer have separate lists.
% 		 Introduced \LWORDS.
% v1.1	 5may19  Removed unnecessary redefinition of \next in \GetWords
%

