[Fwd: Re: lisp newbie help (semi long)]



This was helpful as well.  Forwarded for future searchers.

-----Forwarded Message-----

> Bret Hughes writes:
> > What I want to do first is find a window that has a certain string in
> > the title, and do something, say print its name.
> 
> First step: printing the names of each window.
> 
>   (mapcar window-name (managed-windows))
> 
> MANAGED-WINDOWS returns each window in a list, as you know.  MAPCAR
> calls a function on each element in the list and returns another
> list.  Here, that function is WINDOW-NAME.  You should see a list of
> all the window names.
> 
> The next step is to get something that recognizes window names, for
> matching.  This function takes a window and returns true if the window
> name matches the regular expression "x".
> 
>   (defun window-with-x-in-name? (win)
>     (string-match "x" (window-name win)))
> 
> With this, we can use the REMOVE-IF-NOT function to find just the
> windows with "x" in their names:
> 
>   (setq winlist (remove-if-not window-with-x-in-name? (managed-windows)))
> 
> On my machine, the only window with an "x" in its name is "xconsole",
> so WINLIST ends up only having the "xconsole" window.  You can look at
> the names of the resulting windows by running WINDOW-NAME on each of
> them:
> 
>   (mapcar window-name winlist)
> 
> However, setting global variables like WINLIST is generally considered
> bad style; one normally uses a local variable:
> 
>   (let ((matches (remove-if-not window-with-x-in-name? (managed-windows)))))
>     (mapcar window-name matches))
> 
> Here MATCHES is a local variable that will disappear when we exit the
> expression.
> 
> The REMOVE-IF-NOT function never got added to the librep
> documentation, except for a single line in an old NEWS entry.  I just
> knew to look for it because it appears in other Lisps.  MAPCAR should
> appear in the Emacs Lisp documentation.
> 
> > From sawfish-client -  I have been typing or cutting and pasting from vi
> > the whole form I am trying to evaluate because it seems quicker thatn
> > restarting sawfish all the time to reread testlisp.jl.
> 
> I developed this stuff in Emacs.  The Emacs Sawfish mode has
> keybindings to send expressions to Sawfish for evaluation, which is
> more convenient than cutting and pasting.
> 
> Derek





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]