From 53c6d08b54e1dc449349e94335408d46c86615a3 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 29 Oct 2011 01:01:59 -0700 Subject: [PATCH] Add a toggle word to namespaces and update docs, tests, and usages. Document with-global too. --- basis/ui/tools/listener/listener.factor | 2 +- basis/xmode/marker/marker.factor | 4 ++-- core/namespaces/namespaces-docs.factor | 14 ++++++++++++++ core/namespaces/namespaces-tests.factor | 9 +++++++++ core/namespaces/namespaces.factor | 1 + 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/basis/ui/tools/listener/listener.factor b/basis/ui/tools/listener/listener.factor index 38035b4204..15f1c5c05b 100644 --- a/basis/ui/tools/listener/listener.factor +++ b/basis/ui/tools/listener/listener.factor @@ -417,7 +417,7 @@ interactor "completion" f { \ com-help H{ { +nullary+ t } } define-command : com-auto-use ( -- ) - auto-use? [ not ] change ; + auto-use? toggle ; \ com-auto-use H{ { +nullary+ t } { +listener+ t } } define-command diff --git a/basis/xmode/marker/marker.factor b/basis/xmode/marker/marker.factor index 4daa059911..f4245e0e65 100644 --- a/basis/xmode/marker/marker.factor +++ b/basis/xmode/marker/marker.factor @@ -132,7 +132,7 @@ GENERIC: handle-rule-end ( match-count rule -- ) find-escape-rule dup [ dup rule-start-matches? dup [ swap handle-rule-start - delegate-end-escaped? [ not ] change + delegate-end-escaped? toggle t ] [ 2drop f @@ -162,7 +162,7 @@ M: escape-rule handle-rule-start drop ?end-rule process-escape? get [ - escaped? [ not ] change + escaped? toggle position [ + ] change ] [ drop ] if ; diff --git a/core/namespaces/namespaces-docs.factor b/core/namespaces/namespaces-docs.factor index 05a72c6025..cebd5ba59c 100644 --- a/core/namespaces/namespaces-docs.factor +++ b/core/namespaces/namespaces-docs.factor @@ -19,6 +19,7 @@ ARTICLE: "namespaces-change" "Changing variable values" dec change change-global + toggle } ; ARTICLE: "namespaces-global" "Global variables" @@ -28,6 +29,7 @@ ARTICLE: "namespaces-global" "Global variables" get-global set-global initialize + with-global } ; ARTICLE: "namespaces.private" "Namespace implementation details" @@ -93,6 +95,18 @@ HELP: change-global { $description "Applies the quotation to the old value of the global variable, and assigns the resulting value to the global variable." } { $side-effects "variable" } ; +HELP: toggle +{ $values + { "variable" "a variable, by convention a symbol" } +} +{ $description "Changes the boolean value of a variable to its opposite." } ; + +HELP: with-global +{ $values + { "quot" quotation } +} +{ $description "Runs the quotation in the global namespace." } ; + HELP: +@ { $values { "n" "a number" } { "variable" "a variable, by convention a symbol" } } { $description "Adds " { $snippet "n" } " to the value of the variable. A variable value of " { $link f } " is interpreted as being zero." } diff --git a/core/namespaces/namespaces-tests.factor b/core/namespaces/namespaces-tests.factor index a7b1e50880..1397028f24 100644 --- a/core/namespaces/namespaces-tests.factor +++ b/core/namespaces/namespaces-tests.factor @@ -26,3 +26,12 @@ f test-initialize set-global test-initialize [ 5 ] initialize [ 5 ] [ test-initialize get-global ] unit-test + +SYMBOL: toggle-test +[ f ] [ toggle-test get ] unit-test +[ t ] [ toggle-test [ toggle ] [ get ] bi ] unit-test +[ f ] [ toggle-test [ toggle ] [ get ] bi ] unit-test + +[ t ] [ toggle-test [ on ] [ get ] bi ] unit-test +[ f ] [ toggle-test [ off ] [ get ] bi ] unit-test + diff --git a/core/namespaces/namespaces.factor b/core/namespaces/namespaces.factor index 38c6870cb8..9604be281a 100644 --- a/core/namespaces/namespaces.factor +++ b/core/namespaces/namespaces.factor @@ -25,6 +25,7 @@ PRIVATE> : set-global ( value variable -- ) global set-at ; inline : change ( variable quot -- ) [ [ get ] keep ] dip dip set ; inline : change-global ( variable quot -- ) [ global ] dip change-at ; inline +: toggle ( variable -- ) [ not ] change ; inline : +@ ( n variable -- ) [ 0 or + ] change ; inline : inc ( variable -- ) 1 swap +@ ; inline : dec ( variable -- ) -1 swap +@ ; inline -- 2.34.1