Category:Guish: Difference between revisions

no edit summary
No edit summary
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 161:
There are single and double quoting: anything embedded inside single quotes '', is treated literally (no escaping takes place) and as a single token.
 
Variable interpolationand function interpolations (respectively via "'''@{...}'''" and "'''@(...)'''") isare used inside double quotes "", external command substitution quotes `` and external window id substitution '''<( ...)'''.
 
Escaping ("\n\t\r\f\v\b") takes place only inside double quotes "".
 
<pre> a = 'my string'; puts &quot;this is: @{a}&quot;</pre>
puts &quot;sum of 1 + 5 is: @(add(1, 5))&quot;</pre>
Anything embedded inside '''{}''' is treated as a code block and no variable substitution is done at definition time.
 
Line 190 ⟶ 191:
<pre> fn = {return 1 2 3}
puts join(fn())</pre>
== Joining ==
 
 
 
By using the special '''++''' operator, it's possible to join tokens and blocks; it works simirarly to the '''join''' function, except that the second operand it's treated always as a token (even if it is a '{').
 
<pre> puts a ++ b</pre>
== Conditionals ==
 
Line 215 ⟶ 209:
<pre> a = 1
after 4 {a = 0}
while {eq(@a, 1)} {
wait 1 puts 'true'
}
Line 224 ⟶ 218:
And another one of a '''for''' loop:
 
<pre> for x in 1 2 3 4: { puts @x }</pre>
== Tail recursion optimization (TCO) ==
 
Line 288 ⟶ 282:
; '''self'''
: variable holding the window id when in signal code.
; '''args'''
: refers to the block in which there are positional arguments and function arguments (alternative syntax).
; '''FILE'''
: variable holding the path of current source file.
Line 316 ⟶ 312:
; '''c'''
: A checkbox.
; '''t'''
: A label with a totally transparent background (requires a compositor).
 
= SPECIAL COMMANDS =
Line 325 ⟶ 323:
; '''=&gt; &lt;signal&gt; &lt;subcmd&gt;'''
: register a sub-command &lt;subcmd&gt; to run when &lt;signal&gt; triggers. For normal signals (eg. signals tied to elements), there must exist an implied subject.
; '''global &lt;var!&gt; = [&lt;valsignal&gt;]'''
: unregister a sub-command &lt;subcmd&gt; previously registered on signal &lt;signal&gt;.
: defines a variable using the same rules of variable definition, but defining that variable in the global scope; this usually can be used inside functions to define a global variable which doesn't exist already.
; '''q'''
: quit guish (exit status 0).
Line 332 ⟶ 330:
: quit guish (exit status &lt;status&gt;).
; '''cd &lt;path&gt;'''
: change curretcurrent working directory using &lt;path&gt;.
; '''run &lt;cmd&gt;'''
: execute shell command &lt;cmd&gt;.
Line 356 ⟶ 354:
: schedule &lt;block&gt; to run once after &lt;seconds&gt; seconds are elapsed.
; '''wait &lt;seconds&gt;'''
: stop command execution and wait &lt;seconds&gt; seconds before resuming; (accepts decimal values too). XEvent handling, schedules actions and signal execution are unaffected by this option.
; '''if &lt;condition&gt; &lt;block&gt;'''
: executes &lt;block&gt; if condition evaluates to true (see Conditionals).
Line 369 ⟶ 367:
; '''until &lt;condition&gt; &lt;block&gt;'''
: executes &lt;block&gt; until &lt;condition&gt; evaluates to true (see Conditionals).
; '''for [&lt;variablevar1&gt;, &lt;listvar2&gt;, of...] itemsin [&lt;val1&gt;, &lt;val2&gt;, ...] : &lt;codeblock&gt;'''
: executes the block &lt;codeblock&gt; for each item in listgroup of items using a variable. Note that &lt;code&gt; must be a code blockvariables.
; '''break'''
: exit from current loop.
Line 448 ⟶ 446:
; '''m &lt;x&gt; &lt;y&gt;'''
: move element to coords &lt;x&gt; &lt;y&gt;.
; '''/ [&lt;l|L|a|A|p|x|n&gt; [&lt;...&gt;]]'''
: draws/fills lines, points and arcs depending on operation (See Drawing operations subsection). Origin coordinates correspond to the bottom-left corner as default Cartesian axes (instead of upper-left one used for windows/elements). If no operation is given, then all drawings are discarded from the implied element.
; '''A &lt;element&gt; &lt;alignment&gt;'''
: moves implied element to &lt;element&gt; using &lt;alignment&gt; (see alignment in STYLE AND ATTRIBUTES section, as &lt;alignment&gt; opts are similar).
Line 462:
; '''Y'''
: enable/disable (toggle) moving an element inside its parent by click-and-drag on y axis Enabling this will automatically exclude the flag to click-and-drag and move parent.
 
== Drawing operations ==
 
 
 
; '''l [&lt;color&gt; &lt;x1&gt; &lt;y1&gt; &lt;x2&gt; &lt;y2&gt; [&lt;...&gt;]]'''
: draws lines between given points using color &lt;color&gt; (beware this command consumes all the phrase). If no arguments are given, then all element lines are discarded.
; '''L [&lt;color&gt; &lt;x1&gt; &lt;y1&gt; &lt;x2&gt; &lt;y2&gt; [&lt;...&gt;]]'''
: fills the polygon described by given points using color &lt;color&gt; (beware this command consumes all the phrase). If no arguments are given, then all filled polygons are discarded.
; '''a [&lt;color&gt; &lt;x&gt; &lt;y&gt; &lt;w&gt; &lt;h&gt; &lt;alpha&gt; &lt;beta&gt; [&lt;...&gt;]]'''
: draws an arc using color &lt;color&gt; and whose &quot;center&quot; is at &lt;x&gt; &lt;y&gt;, major and minor axes are respectively &lt;w&gt; and &lt;h&gt;, start and stop angles are &lt;alpha&gt; and &lt;beta&gt; (consumes all the phrase). If no arguments are given, then all element arcs are discarded.
; '''A [&lt;color&gt; &lt;x&gt; &lt;y&gt; &lt;w&gt; &lt;h&gt; &lt;alpha&gt; &lt;beta&gt; [&lt;...&gt;]]'''
: fills an arc using color &lt;color&gt; and whose &quot;center&quot; is at &lt;x&gt; &lt;y&gt;, major and minor axes are respectively &lt;w&gt; and &lt;h&gt;, start and stop angles are &lt;alpha&gt; and &lt;beta&gt; (consumes all the phrase). If no arguments are given, then all element arcs are discarded.
; '''p [&lt;color&gt; [&lt;...&gt;]]'''
: draws given points using color &lt;color&gt; (beware this command consumes all the phrase). If no arguments are given, then all points are discarded.
; '''x [&lt;color&gt; [&lt;...&gt;]]'''
: draws given pixels using color &lt;color&gt; (beware this command consumes all the phrase). If no arguments are given, then all pixels are discarded.
; '''n [&lt;color&gt; &lt;name&gt; &lt;x&gt; &lt;y&gt;]'''
: draws given point using color &lt;color&gt; and putting the text &lt;name&gt; at that point. If no arguments are given, then all points are discarded.
 
= NORMAL COMMANDS =
Line 615 ⟶ 634:
Every phrase is reduced to an empty phrase while evaluating:
 
<pre> a = 234 ;{i1=|i|;&lt;'input1'+}(); {i2=|i|;&lt;'input2'+}() |b|&lt;btn+</pre>
This example is composed by 2 phrases, and the code block in each phrase is executed before each assignment.
 
Line 639 ⟶ 658:
 
 
With the '''=''' operator (actually, it's a special statement command), it's possible to assign a valuevalues to a variable, reusing it later by simply referencing it using '''@''' operator when not inside quotes or by wrapping it inside '''@{}''' when in double quotes, shell command substitution quotes '''``''', or external window id substitution '''&lt;( )'''.
 
<pre> b = 123; puts @a</pre>
There are two methods to define/create empty variables: by explicitely assing an empty string to a variable (ex. a = &quot;&quot;) or by simply omit the value (ex. a =).
 
In addition, if there is more than one value to assign, a block is automatically created (embedding those values) and assigned to that variable:
 
<pre> a = 1 # this simply assigns '1' to the variable 'a'
b = 1, 2, 3, 4 # this instead assigns the block '{1, 2, 3, 4}' to the variable 'a'
c = {1, 2, 3, 4} # same but explicit</pre>
Each block has it's own scope, and variable resolution works by searching from the last scope to the first. Ex:
 
Line 664 ⟶ 689:
name = 'random name'
puts &quot;@{gname} is maybe @{name}&quot;</pre>
== Element substitutionexpressions ==
 
 
 
Anything inside '''||''' is an element expression; a widget of a given element is created and its X11 window id substituted. The synopsis is: |&lt;element&gt;[{&lt;width&gt;, &lt;height&gt;}]| Ex.
 
<pre> |b|+</pre>
Line 694 ⟶ 719:
<pre> a = &lt;(xterm)</pre>
xterm program is spawn, and can be driven (almost) like a normal element.
 
= OPERATORS =
 
 
 
== Binary ==
 
 
 
; '''&lt;s&gt; .. &lt;e&gt;'''
: returns integers starting at &lt;s&gt; and ending at &lt;e&gt; (inclusive) as multiple tokens.
; '''&lt;var&gt; = [&lt;val&gt;, [&lt;val1&gt;, ...]]'''
: defines a variable (consumes all the phrase). If no value is given, an empty token is assigned to the variable. If a single value is given, that value is assigned to the variable. If multiple values are given, then all these values are wrapped inside a block, and this block is assigned to the variable.
; '''&lt;attr&gt; .= [&lt;val&gt;, [&lt;val1&gt;, ...]]'''
: defines an element using the implied subject attribute (consumes all the phrase). If no value is given, an empty token is assigned to the variable. If a single value is given, that value is assigned to the variable. If multiple values are given, then all these values are wrapped inside a block, and this block is assigned to the variable.
 
== Unary ==
 
 
 
; '''@&lt;varname|num&gt;'''
: dereferences a variable name (or positional argument).
; '''@*'''
: returns all function parameters as tokens (usable with command line parameters too).
; '''[&lt;eid&gt;].&lt;attr&gt;'''
: dereferences an element attribute; if &lt;eid&gt; is given, uses that element, otherwise uses implied subject.
 
= ELEMENT ATTRIBUTES =
Line 699 ⟶ 750:
 
 
Every element can have some default readonly attributes and a variable number of custom attributes (which can be set by using assignment only, not by using '''let''' function). To get/set an attribute, use the special dot&quot;'''.='''&quot; operator; to get it instead use the &quot;'''.'''&quot; operator.
 
<pre> b = |b|; @b.myattr .= 'here'; puts(@b.myattr)</pre>
In the last example, a custom attribute, &quot;myattr&quot; is created and used.
 
Line 748 ⟶ 799:
: returns 1 if a widget with id &lt;eid&gt; exists, 0 otherwise.
; '''read([&lt;file&gt;])'''
: reads and returns a line (excluding newline) from standard input; if an existing [file] is given, reads and returns all its content. Beware that this function blocks the GUI events, and returns nothing when reading from stdin and source is non-blocking.
; '''write(&lt;text&gt;, &lt;file&gt;)'''
: writes text into file and returns the number of characters written. Creates the file if it doesn't exist yet.
Line 755 ⟶ 806:
; '''eval(...)'''
: evaluates code by first stringifying all given arguments and then returns the result of evaluation if any. Beware that this function runs in the &quot;current&quot; scope, and can modify it.
; '''call(&lt;name&gt;, ...)'''
: gets a variable name and a variable number of arguments, then calls the function whose name is &quot;name&quot; with those arguments and returns the result (if any).
; '''builtin(&lt;func&gt;, ...)'''
: gets the name of a builtin function and a variable number of arguments, then calls the builtin function with those arguments and returns the result (if any). It's useful when overriding builtin functions.
Line 771 ⟶ 820:
; '''if(&lt;cond&gt;, [&lt;v1&gt;, [&lt;v2&gt;]])'''
: if &lt;cond&gt; is true and &lt;v1&gt; is given, returns &lt;v1&gt;, else if &lt;cond&gt; is false and &lt;v2&gt; is given, returns &lt;v2&gt;.
; '''unless(&lt;cond&gt;, [&lt;v1&gt;, [&lt;v2&gt;]])'''
: if &lt;cond&gt; is false and &lt;v1&gt; is given, returns &lt;v1&gt;, else if &lt;cond&gt; is true and &lt;v2&gt; is given, returns &lt;v2&gt;.
; '''and(...)'''
: returns the first true argument; if there are no true arguments, returns the last one which is false. The function evaluates any block given.
; '''or(...)'''
: returns the last true argument if all arguments are true, otherwise returns the first false argument. The function evaluates any block given.
; '''flat(...)'''
: returns all given arguments; if a block is found, then it is flatted.
Line 787 ⟶ 842:
; '''popb()'''
: pops the first argument from function arguments (works with command line parameters too). This function is somewhat special, as if there are no arguments to pop.
; '''slice(&lt;si&gt;, &lt;ei&gt;, ...)'''
: returns arguments starting at &quot;si&quot; and ending at &quot;ei&quot; (inclusive). This function is somewhat special, as when there is nothing to get, it'll return nothing (statement behaviour).
; '''times(&lt;n&gt;, &lt;arg&gt;)'''
: returns a sequence of tokens made by &lt;n&gt; times &lt;arg&gt;. This function is somewhat special, as when there are 0 tokens to replicate, it'll return nothing (statement behaviour).
; '''get(&lt;name&gt;)'''
: returns the value of the variable with name &lt;name&gt;, or an empty token if the variable doesn'tdoes not exist.
; '''at(&lt;n&gt;, ...)'''
: returns the &quot;nth&quot; argument given (by index). This function is somewhat special, as when there is nothing to get, it'll return nothing (statement behaviour).
; '''true(&lt;arg&gt;)'''
: returns 1 if &lt;arg&gt; is true, 0 otherwise.
Line 803 ⟶ 854:
; '''join(...)'''
: joins blocks and/or tokens by applying the following rules to all arguments given, and accumulates the result as the first operand. If the operands are blocks, then a single new block is created by joining them; if the operands are tokens, then a single new token is created by joining them, and its type will be that of the &quot;second&quot; token; if the operands are mixed (eg. a block and a token), then the token will be embedded inside the block.
; '''range(&lt;start&gt;, &lt;end&gt;)'''
: returns integers starting at &lt;start&gt; and ending at &lt;end&gt; (inclusive) as multiple tokens.
; '''isdef(&lt;token&gt;)'''
: returns 1 if &lt;token&gt; is a variable, 0 otherwise.
Line 823 ⟶ 872:
; '''seq(&lt;t1&gt;, &lt;t2&gt;, ...)'''
: returns 1 if all arguments are equal (string comparison), 0 otherwise.
; '''count(...)'''
: returns the number of given arguments.
; '''any(...)'''
: returns the first true argument; if there are no true arguments, returns the last one which is false. This function is special, as the evaluation will be interrupted as soon as there is at least 1 true argument.
; '''all(...)'''
: returns the last true argument if all arguments are true, otherwise returns the first false argument. This function is special, as the evaluation will be interrupted as soon as there is at least 1 false argument.
; '''add(...)'''
: perform addition.
Line 839 ⟶ 882:
; '''mod(...)'''
: perform modulus.
; '''rand()'''
: returns a random positive integer.
; '''sqrt(&lt;n&gt;)'''
: returns the square root of &lt;n&gt;.
; '''cbrt(&lt;n&gt;)'''
: returns the cube root of &lt;n&gt;.
; '''pow(&lt;n&gt;, &lt;e&gt;)'''
: returns the power of &lt;n&gt; raised to &lt;e&gt;.
; '''log(&lt;n&gt;)'''
: returns the base 10 logarithm of &lt;n&gt;.
; '''ln(&lt;n&gt;)'''
: returns the natural logarithm of &lt;n&gt;.
; '''sin(&lt;n&gt;)'''
: returns the sine of &lt;n&gt; (degrees).
; '''cos(&lt;n&gt;)'''
: returns the cosine of &lt;n&gt; (degrees).
; '''tan(&lt;n&gt;)'''
: returns the tangent of &lt;n&gt; (degrees).
; '''hex(&lt;n&gt;)'''
: returns &lt;n&gt; in its hexadecimal representation.
; '''int(&lt;n&gt;)'''
: returns integral part of given number &lt;n&gt;; rounds to nearest integer.
; '''xor(&lt;n1&gt;, &lt;n2&gt;, ...)'''
: perform bitwise XOR.
; '''andband(&lt;n1&gt;, &lt;n2&gt;, ...)'''
: perform bitwise AND.
; '''orbor(&lt;n1&gt;, &lt;n2&gt;, ...)'''
: perform bitwise OR.
; '''lsh(&lt;n1&gt;, &lt;n2&gt;, ...)'''
Line 879 ⟶ 944:
Each field must be separated by newlines, ''';''' or '''|'''. Colors can be specified by using a common shortname, such as &quot;yellow&quot;, or by using RGB value, such as &quot;#ff32ae&quot;.
 
; '''background | bg: &lt;color|/&lt;path to image&gt;&gt;'''
: set background color or a background image by specifying image path; if the string &quot;null&quot; is given as &lt;path to image&gt;, current image is removed. (Background image loading requires building guish with Imlib2 support.)
: set background color
; '''color | foreground | fg: &lt;color&gt;'''
: set foreground color
Line 893 ⟶ 958:
; '''border-color | bc: &lt;color&gt;'''
: set border color
; '''background-image | i: &lt;path&gt;'''
: set background image by specifying image path; if the string &quot;null&quot; is given, current image is removed. (This attribute requires building guish with Imlib2 support.)
; '''width | w: &lt;value in pixels&gt;'''
: set width
Line 901 ⟶ 964:
; '''border | b: &lt;value in pixels&gt;'''
: set border width
; '''line | l: &lt;value in pixels&gt;'''
: set line width (use with &quot;/&quot; command)
; '''margin | g: &lt;value in pixels&gt;'''
: set margin width
39

edits