Saturday, September 20, 2008

Emacs Modes - some notes

I've been playing with creating a couple of useful functions in Emacs, and started thinking about packaging a couple of them into some sort of useful grouping.

So I'd need to define either Major, derived or minor modes.

Deciding which was pretty much half of the hassle. Eventually, I ended up going with a half-baked combination of the three, believe it or not. My little bit of usefulness is only handy for people connecting to Microsoft SQLServer databases, wanting to trawl through stored procs, looking for tables, trying to figure out how everything interconnects. Maybe the stuff will be ready for release someday, but I found some stuff pretty interesting.

In the meantime, I couldn't find templates for creating modes, so I wrote some.

Go visit the link below for the Elisp code:
modes-atomic.el at:
http://ryan-white-lisp.googlecode.com/files/modes-atomic.el

Some interesting links to look up:
http://sachachua.com - Sacha is an Emacs nut/guru
http://www.gnu.org/software/emacs/manual/elisp.html The really dense, not too easy to understand, but still good Elisp manual

Tuesday, September 9, 2008

Lisp conditions - atomic example

Lisp allows you to use restarts - magical stuff that allows your app to recover from errors, instead of vomiting everything at you.

Lisp gives you options. Hopefully. One day, I might even be able to use them somewhat.

Here's my grossly simplified, probably wrong internal model of conditions:
  • Something goes wrong
  • A continuation is created, or maybe several are. Conditions are mystical "pause" buttons in the app, or something.
  • The debugger appears, offering you the options
  • When you pick one, lisp magicaly re-injects you where the condition was created.
  • Things hopefully work a bit better.
Pretty sure this will be totally embarrasing in future.

Any way. From what I can gather, the CL condition system is implemented in terms of objects, and a 'condition' is a parent of an 'error'. 'Conditions' allow for signalling of diferent states within your app, but that's quite a fancy-pants subject, for when I know what the heck I'm doing. Typically, you'd define your error condition like this:

(define-condition slave-not-happy (error) ... other code ...)

So you're basically creating your little error as a sub-class of error, which is a subclass of condition.

In your code, you can then enable all sorts of mystical getting-out-of-jail stuff like this:

... other code ... (restart-case (error 'slave-not-happy :useable-key-name useable-key-value
(this-option ... some code)
(that-option ... some other code))

...So when you have a problem, and lisp kicks into the debugger, you'll have something like
..blah blah blah - THERE'S A PROBLEM:
0 - this-option
1 - that-option
2 - abort
3 - maybe some more options

Your stuff appears RIGHT THERE with the system's restarts, and you feel all powerful and stuff. So, basically, you're giving yourself more options for getting out of trouble.

Deciding what to do with the signalled conditions is up to you, but you can start getting really fancy-cool stuff done:

...code
(handler-bind ((slave-not-happy #'(lambda (c) (invoke-restart 'this-option)))
... other code

Of course, you can conditionally decide which restart to invoke, based on some other internal logic. Really spiffy.

here's a complete, working example you can put in a file and play around with:
Also available at

http://ryan-white-lisp.googlecode.com/files/conditions-atomic.lisp

Update:
Nikodemus Siivola (major contributor to SBCL) posted a style guide on conditions and errors:
"Learn the difference between HANDLER-BIND and HANDLER-CASE. Short version: HANDLER-CASE always unwinds, with HANDLER-BIND you can eg. log the condition without handling it, or decide whether to unwind or not after inspecting the condition in more detail."


Fantastic resources:
http://www.nhplace.com/kent/Papers/Condition-Handling-2001.html
(Kent Pitman)
http://www.gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html (Peter Siebel)
enjoy.



#|
Copyright (c) 2008 Ryan White

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.


CONDITIONS - quick and dirty intro/reminder
Originally set up on

Fire up lisp and enter
(load "path-to-this-file")
You should then be able to call
(slave 10)
which will return 10
(slave "foo")
which will give you a set of restarts.
Calling
(master '(1 2 3 "foo"))
will return just (1 2 3)
(master '(1 2 3 "foo") :on-error "change" :chage-value 111)
will return (1 2 3 111)
(emperor '(1 2 3 "foo"))
will return (1 2 3)
(careless-master '(1 2 3 "foo"))
will give a set of restarts
(micromanaging-emperor '(1 2 3 "foo"))
will return (1 2 3 1000000)

Extend.Document.Publish.
|#



#|
Define a condition - an indicator that something isn't
totally and fantastically 100% right
|#


(define-condition slave-not-happy (error)
((given-value :initarg :given-value :reader given-value-read))
(:report (lambda (condition stream)
(format stream "~A is not a number, dammit!" (given-value-read condition)))))

#|
This is the function at the bottom of the chain of events.
When its unhappy, it uses the condition to let everyone know
|#

(defun slave (item)
(if (numberp item) ;the test
item ;if everything's good, just return the number
(restart-case (error 'slave-not-happy :given-value item) ;or start this whole error handling trip
(use-nil () :report "Use nil value." nil)
(replace-item (new-value) :report "Replace the value" new-value)
(replace-item-interactively (new-value)
:report "Provide a value yourself."
:interactive (lambda ()
(format t "~&Value to use: ")
(list (eval (read))))
(slave new-value))))) ;finally - a potentially recursive call...

#|
This is a function up the chain of events. It handles the error spit out
|#

(defun master (mylist &key (on-error "ignore") (change-value 0))
(handler-bind ((slave-not-happy ;figuring out what to do with the slave-not-happy error
(if (equal on-error "change")
#'(lambda (c) (invoke-restart 'replace-item change-value)) ;we either do a substitution
#'(lambda (c) (invoke-restart 'use-nil)) ; or we use nil
)))
(let ((output '()) (tst ""))
(loop for item in mylist do
(setf tst (slave item))
(if (eq nil tst)
()
(push tst output)))
(reverse output))))
#|
This function just orders stuff done, and doesn't want to know
about the details
|#

(defun emperor (list)
(master list))


#|
This version of master doesn't worry about errors at all.
Basically, it just doesn't cater for the error
|#

(defun careless-master (mylist &key)
(let ((output '()) (tst nil))
(loop for item in mylist do
(setf tst (slave item))
(if (eq nil tst)
()
(push tst output)))
(reverse output)))
#|
If the emperor wishes, he can do the managing of
the unhappy slave. In a typically self-serving manner
|#

(defun micromanaging-emperor (list)
(handler-bind
((slave-not-happy #'(lambda (c) (invoke-restart 'replace-item 1000000))))
(careless-master list)))

Why, what, how

(("Place to store snippets")(emacs common-lisp miscellanea) (short-descriptions-and-examples))

------ Random testing here ------
click here to view a shared document online
click here to request the shared document via email

click here to visit a linkedin profile