Thursday, 5 January 2012

Adress book

Here an example of a simple and effective address book:
Here the source code:
REBOL [
    title: "Card File"
    date: 5-Mar-2010
    file: %card-file.r
    author: Nick Antonaccio
    purpose: { This is the quintessential simple text field storage application. It can be used as shown here, to save contact information, but by adjusting just a few lines of code and text labels, it could be easily adapted to store recipes, home inventory information, or any other type of related pages of data. A version of this script with line-by-line documentation is available at http://re-bol.com }
    ]
write/append %data.txt ""
database: load %data.txt
view center-face gui: layout [
    text "Load an existing record:"
    name-list: text-list blue 400x100 data sort (extract database 4) [
        if value = none [return]
        marker: index? find database value
        n/text: pick database marker
        a/text: pick database (marker + 1)
        p/text: pick database (marker + 2)
        o/text: pick database (marker + 3)
        show gui
        ]
    text "Name:" n: field 400
    text "Address:" a: field 400
    text "Phone:" p: field 400
    text "Notes:" o: area 400x100
    across
    btn "Save" [
        if n/text = "" [alert "You must enter a name." return]
        if find (extract database 4) n/text [
            either true = request "Overwrite existing record?" [
                remove/part (find database n/text) 4
                ] [return ]
            ]
        save %data.txt repend database [n/text a/text p/text o/text]
        name-list/data: sort (extract copy database 4)
        show name-list
        ]
    btn "Delete" [
        if true = request rejoin ["Delete " n/text "?"] [
            remove/part (find database n/text) 4
            save %data.txt database
            do-face clear-button 1
            name-list/data: sort (extract copy database 4)
            show name-list
            ]
        ]
    clear-button: btn "New" [
    n/text: copy ""
    a/text: copy ""
    p/text: copy ""
    o/text: copy ""
    show gui
    ]
]

Friday, 30 December 2011

Console output

Reading console history is very easy, just read system/console/hystory, like this:

>> print "Hello, word!"
== "Hello, world!"
>> 2 + 2
== 4
>> probe system/console/history
== [ "2 + 2" {print "Hello, word!"}]


but how to register the console output?
It's easy using one of the most important rule of Rebol, word redefinition.
The only command that send output on the console are prin and print, so we can redefine these commands to store the output in a variable. See this example, where we store all console output in temp:

>> temp: copy []
>> print-bak: get 'print
>> prin-bak: get 'prin
>> print: func [value] [append temp (reform value)  print-bak value]
>> prin: func [value] [append  temp (reform value) prin-bak value]
>> ? print-bak
USAGE:
PRINT-BAK value

DESCRIPTION:
Outputs a value followed by a line break.
PRINT-BAK is a native value.

ARGUMENTS:
value -- The value to print (Type: any)

>> probe temp
== ["USAGE:^/^-" "PRINT-BAK " "value " "" "^/DESCRIPTION:" "^- Outputs a value followed by a line break. " "^- PRINT-BAK is a nati...

Wednesday, 28 December 2011

Captcha

What is a captcha? Is a tool to avoid automatic spam on forum, comment, or whatever field on our sites. The captcha show an image containing a letter sequence that only a human being can understand.
Here an example:
It's possible to use Rebol to create the random captcha images, you can find all script and example in this page:
http://softinnov.org/rebol/captcha.shtml

Friday, 23 December 2011

Spellcheck

Check spelling is an important feature, and Rebol has integrated in the RebGUI script, just hit CTRL+S in any text area and spell check wik start (remember to download the dictionaries...).
However, if you don't like RebGUI, there are a lot of open source programs that can be used. The most famous are Ispell and Aspell. You can use these programs to check spelling texts with Rebol.
I'll show you Aspell, that is more recent, but you can use any software.
First of all download Aspell for your platform from here:
ftp.gnu.org/gnu/aspell/w32/
and install also the dictionaries of your languages.
Then we need a phrase as example:
"Hello, this is Carl; a great warrrior!"
There is an error on warrrior, now let's try with Rebol:

a: "Hello, this is Carl; a great warrrior!"

we should split the original sentences in single words, and is very easy with Rebol:

>> b: parse a none
== ["Hello" "this" "is" "Carl" "a" "great" "warrrior!"]

Now we use Aspell and we create a file were every line is a word checked. If a line starts with *,the word is OK; else the line starts with a &, the word is misspelled and Aspeel gives the suggested word.

call/input " c:\Programmi\Aspell\bin\aspell.exe -a --lang=en > ./spellout.txt " a

The spellout file will be:

@(#) International Ispell Version 3.1.20 (but really Aspell 0.50.3)
*
*
*
*
*
*
& warrrior 10 30: warrior, warriors, warier, worrier, wearier, waterier, warrior's, wartier, wearer, barrio

Now let's show to the user that he misspelled some word:

c: read/lines %spellout.txt
foreach word b [
c: next c ; so we skip the first line with Aspell copyright
if (first c/1) = #"&" [ alert reform ["You misspelled:" word]]
]

This are the basis of spell-checking. If you want to see something better let's see this example:
view layout [
    a: area
    text "English spellcheck"
    button "Spellcheck" [
        call/input " c:\Programmi\Aspell\bin\aspell.exe -a --lang=en > ./spellout.txt " a/text
        b: parse a/text none
        c: read/lines %spellout.txt
        temp: copy a/text
        foreach word b [
            temp: copy (find/case temp word); so we traverse the text
            c: next c ; so we skip the first line with Aspell copyright
            if (first c/1) = #"&" [
                temp2: copy/part (find/case/tail temp word) 50
                view/new/title layout [
                    across
                    text bold red word
                    text 200 temp2
                    return
                    box black 200x2
                    return
                    text 200 reform ["Suggest words" (find c/1 ":")]
                ] "Spellcheck"
]]]]

It's just a simple example in 10 lines of code, but here is the result:

Wednesday, 21 December 2011

Calculator

There are a lot of calculator scripts, let's see them (in alphabetic order).


Calculator
It's a tiny calculator with just 30 lines of code:
Here the source:
REBOL [
    Title: "Calculator"
    Date: 2-Apr-2001
    Version: 1.2.0
    File: %calculator.r
    Author: "Jeff Kreis"
    Purpose: "Simple numeric calculator."
]
auto-clear: true
calculate: does [
    if error? try [text-box/text: form do text-box/text][
        text-box/text: "Error"
        text-box/color: red
    ]
    auto-clear: true
    show text-box
]
calculator: layout [
    style btn button 40x24
    style kc btn brick [text-box/text: copy "0" auto-clear: true show text-box]
    style k= btn [calculate]
    style k   btn [
        if auto-clear [clear text-box/text text-box/color: snow auto-clear: false]
        append text-box/text face/text
        show text-box
    ]
    origin 10
    backcolor rebolor
    space 4
    text-box: field "0" 172x24 bold snow right feel none
    pad 4
    across
    kc "C" keycode [#"C" #"c" page-down]
    k "(" #"("   k ")" #")"   k " / " #"/" return
    k "7" #"7"   k "8" #"8"   k "9" #"9"   k " * " #"*" return
    k "4" #"4"   k "5" #"5"   k "6" #"6"   k " - " #"-" return
    k "1" #"1"   k "2" #"2"   k "3" #"3"   k " + " #"+" return
    k "0" #"0"   k "-"       k "." #"."
    k= "=" keycode [#"=" #"^m"] return
    key keycode [#"^(ESC)" #"^q"] [quit]
]
view center-face calculator

Calculator tutorial
It's a demo made by Nick Antonaccio:
Here the source (always no more than 30 lines of code):
REBOL [
    title: "calculator"
    date: 28-feb-2009
    file: %calculator-tutorial.r
    purpose: {
        A little GUI calculator example, with printout.  
        Taken from the tutorial at http://musiclessonz.com/rebol_tutorial.html
    }
]
prev-val: cur-val: 0 cur-eval: "+" display-flag: false
print "0"
view center-face layout/tight [
    size 300x350 space 0x0 across
    display: field 300x50 font-size 28 "0" return
    style butn button 100x50   [
        if display-flag = true [display/text: "" display-flag: false]
        if display/text = "0" [display/text: ""]
        display/text: rejoin [display/text value]
        show display
        cur-val: display/text
    ]
    style eval button 100x50 brown font-size 13 [
        prev-val: cur-val
        display/text: "" show display
        cur-eval: value
    ]
    butn "1"   butn "2"   butn "3"   return
    butn "4"   butn "5"   butn "6"   return
    butn "7"   butn "8"   butn "9"   return
    butn "0"   butn "."   eval "+" return
    eval "-" eval "*" eval "/" return
    button 300x50 gray font-size 16 "=" [
        if display-flag <> true [
            if ((cur-eval = "/") and (cur-val = "0")) [
                alert "Division by 0 is not allowed." break
            ]
            prin rejoin [prev-val " " cur-eval " " cur-val " = "]
            print display/text: cur-val: do rejoin [prev-val " " cur-eval " " cur-val ]
            show display
            display-flag: true
        ]
    ]
]

Desktop Calculator
It's a small calculator:
Here the source (70 lines):
REBOL [
    Title: "Desk Calculator"
    Date: 4-Oct-2004
    Version: 1.3.0
    File: %desk-calc.r
    Author: "Ryan S. Cole"
    Purpose: "A tool for simple calculations."
    Comment: "Standard function calculator."
    Email: ryan@skurunner.com
]
acc:         ; Accumulator
op:         ; Selected operation
mem:         ; Memory storage
err: none   ; Error state
reg: []     ; Register stack

; For working with the displayed number...
cur-str: does [any [reg/1 acc form 0]]
cur-num: does [to-decimal cur-str]
cur-set: func [val] [
    either not either op [reg/2][reg/1] [insert reg form val] [reg/1: form val]
]
; Updates the screen...
display: does [
    if not find lcd/text: copy cur-str "." [append lcd/text "."]
    err-flag/font/color: either err [yellow][gray]
    mem-flag/font/color: either mem [green][gray]
    show [lcd err-flag mem-flag]
]
; Does the equation...
solve: does [
    if not reg/2 [insert reg reg/1]
    acc: none
    if op [err: error? try [acc: form do reform [reg/2 op 'to-decimal reg/1]]]
    reg: copy []
    op: none
]
; Handles keypresses
press: func [key] [
    err: no
    if find ".0123456789" key [
        if not either op [reg/2][reg/1] [insert reg copy ""]
        if all ["." = key   find reg/1 key] [exit]
        either all [reg/1 = "0"   key <> "."] [reg/1: copy key] [append reg/1 key]
    ]
    if find "+-*/" key [
        if reg/2 [solve]
        if not reg/1 [insert reg cur-str]
        op: key
    ]
    switch key [
        "C" [acc: op: none reg: copy []]
        "E" [remove reg op: acc: none]
        "±" [cur-set negate cur-num]
        "MC" [mem: none]
        "MR" [cur-set any [mem 0]]
        "M+" [mem: add any [mem 0] cur-num]
        "M-" [mem: subtract any [mem 0] cur-num]
        "pi" [cur-set pi]
        "=" [solve]
    ]
]
; Construct the screen...
view layout compose [
    backdrop effect [gradient 0x1 85.155.205 80.130.180]
    origin 10x10 space 0x0 pad 0x10 across
    style text text 15x20 bold gray  
    mem-flag: text "M"
    err-flag: text "E"
    pad 5x0   space 5x5
    lcd: field "0." silver 170x20 right bold feel none
    return
    style k (pick [btn button] link?) 30x20 [press face/text display]
    k #"C" "C"   k "MC"   k #"7" "7"   k #"8" "8"   k #"9" "9"   k #"/" "/" return
    k #"E" "E"   k "MR"   k #"4" "4"   k #"5" "5"   k #"6" "6"   k #"*" "*" return
    k #"p" "pi"   k "M+"   k #"1" "1"   k #"2" "2"   k #"3" "3"   k #"+" "+" return
    k #"i" "±"   k "M-"   k #"0" "0"   k #"." "."   k #"=" "="   k #"-" "-"
    keycode #"^M" [press "=" display]
]                        

Mini calc
It's really a mini calculator:
Here the source (30 lines):
REBOL [
    Title: "Mini-Calculator"
    Date: 6-Mar-2002
    Version: 1.1.3
    File: %mini-calc.r
    Author: "Ryan Cole"
    Purpose: "Tiny calculator example."
    Email: ryancole@usa.com
]
reg: []
op: no

do solve: does [
    acc: none
    if op [error? try [acc: do reform [any [reg/2 acc 0] op 'to-decimal reg/1]]]
    reg: copy []
    op: no
]
calc: func [key] [
    if find ".0123456789" key [
        if none? pick reg not op [insert reg copy ""]
        if not all ["." = key   find reg/1 key] [append reg/1 key]
    ]
    if find "+-*/" key [
        if reg/2 [solve]
        any [reg/1 insert reg any [acc 0]]
        op: key
    ]
    if key = "=" [solve]
]
view layout [
    origin 0x0 space 0x0 across
    lcd: field "0." silver 140x22 right feel none
    return
    style k button 35x25 [
        calc face/text
        if not find lcd/text: form any [reg/1 acc 0] "." [append lcd/text "."]
        show lcd
    ]
    k "7" k "8" k "9" k "/" return
    k "4" k "5" k "6" k "*" return
    k "1" k "2" k "3" k "-" return
    k "0" k "." k "=" k "+"
]                                              

Scientific calculator
It's a scientific calculator, it reduce conde lenght because it uses the calculase dialect/script:
Here the source (40 lines):
REBOL [
    Title: "Scientific Calculator"
    Date: 16-Mar-2002
    Version: 0.9.5
    File: %sci-calc.r
    Author: ["Ryan S. Cole" "Massimiliano Vessi"]
    Purpose: {For scientific calculations.   Currently in beta, so dont use it to figure out critical information just yet.}
    Email: [ryanc@iesco-dms.com   maxint@tiscali.it]
]
if not exists? %calculese.r [
    temp: read http://www.rebol.org/download-a-script.r?script-name=calculese.r
    write temp %calculese.r
    ]
   
do %calculese.r
; depth of stack shown in parens
depth: has [fathoms] [
    fathoms: copy ""
    loop length? calc-engine/stack [append fathoms "'"]
    return fathoms
]
view layout [
    backdrop effect [gradient 0x1 74.74.74 32.32.32]
    origin 6x6 space 3x3
    lcd: field "0." 262x32 right bold silver feel none font-size 22
    across
    style k button gray 50x20 [
        lcd/text: calculese face/text
        lcd/effect: compose/deep [pen 0.0.0 draw [text 2x19 (depth)]]
        show lcd
    ]
    style r k brick
    style g k leaf
    style o k orange
    style s k sienna
    style t k teal
    style a k aqua
    style b k tan
    r "CE" b "and" b "or" b "xor" b "not" return
    r "AC" a "arcsin" a "arccos" a "arctan" a #"p" "pi" return
    g "M÷" a "sin" a "cos" a "tan" a "abs" return
    g "M×" a "exp-e" a "log-10" a "log-2" a "log-e" return
    g "M-" a "mod" a "sqr" a "exp" a "¹/x" return
    g "M+" a #"±" "±" a #"r" "rnd" a "²" a "³" return
    g "MR" k #"7" "7" k #"8" "8" k #"9" "9" t #"/" "÷" return
    g "MC" k #"4" "4" k #"5" "5" k #"6" "6" t #"*" "×" return
    s #"(" "(" k #"1" "1" k #"2" "2" k #"3" "3" t #"-" "-" return
    s #")" ")" k #"0" "0" k #"." "." o #"^M" "="   t #"+" "+"
]                                                

Supercalculator
It's a scientific calculator with history, configuration and session. All it's stored in ".supercalculator.conf" file:
Suorce code is about 200 lines, because it doesn't use any calculus lib, but it uses the RebGUI lib. You can download the script here:
http://www.rebol.org/view-script.r?script=supercalculator.r

Monday, 19 December 2011

Developer Conference 2012

In the weekend of 3 and 4 march, we will organise a DevCon for the REBOL family of programming languages in the "De War" Kleine Koppel 40, 3812 PH, Amersfoort in the Netherlands. Which is ten to fifteen minutes walking from Amersfoort Central Station.
To get an impression of the previous conference see here
http://reborcon.esperconsultancy.nl/
"De War" alongside the river "Eem" in the neighboorhood of the Koppelpoort on OpenStreetMap:
http://www.openstreetmap.org/?mlat=52.1628&mlon=5.3768&zoom=14&layers=M
Amersfoort Central Station is one of the largest railway junctions in the country.
http://en.wikipedia.org/wiki/Amersfoort
http://en.wikipedia.org/wiki/Koppelpoort
http://en.wikipedia.org/wiki/Eem

Amersfoort is also the birthplace of painter Piet Mondriaan. As a result of that you can visit the Mondriaan House:
http://en.wikipedia.org/wiki/The_Mondriaan_House

Nenad will be giving a workshop about Red. Kaj wil present his bindings. Other REBOL-related subjects are welcome?
Contact bas@esperconsultancy.nl