libgeda: Don't double free path argument
[geda-gaf.git] / docs / wiki / geda-guile_scripting.html
blob48d28ebff72cf5fb2a2c066f40d033464bd75689
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html>
4 <head>
5 <link rel="stylesheet" media="screen" type="text/css" href="./style.css" />
6 <link rel="stylesheet" media="screen" type="text/css" href="./design.css" />
7 <link rel="stylesheet" media="print" type="text/css" href="./print.css" />
9 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
10 </head>
11 <body>
13 <p>
14 <em>Translations of this page are also available in the following languages:</em> <a href="geda-guile_scripting.ru.html" class="wikilink1" title="geda-guile_scripting.ru.html">Русский</a>.
15 </p>
17 <h2 id="guilescripting">Guile scripting</h2>
18 <div class="level2">
20 <p>
21 gEDA/gaf uses Guile Scheme to provide Scheme scripting capabilities, and all of the features of Guile are available to use. The <em>Guile Reference Manual</em> is available as an Info manual (<code>info guile</code> on most systems), or on the Guile website.
22 </p>
24 <p>
25 A collection of modules is provided for accessing and modifying gEDA objects and pages, called the gEDA Scheme API. The <em>gEDA Scheme Reference Manual</em> is also available as an Info manual (<code>info geda-scheme</code>).
26 </p>
28 </div>
30 <h3 id="tutorials">Tutorials</h3>
31 <div class="level3">
32 <ul>
33 <li class="level1"><div class="li"> <a href="geda-gnetlistschemetutorial.html" class="wikilink2" title="geda-gnetlistschemetutorial.html">Scripting a gnetlist backend in scheme</a> (John Doty)</div>
34 </li>
35 </ul>
37 <p>
38 See also the <a href="geda-gschem_ug-extensions.html" class="wikilink1" title="geda-gschem_ug-extensions.html">Extending gschem</a> section of the <em><a href="geda-gschem_ug.html" class="wikilink1" title="geda-gschem_ug.html">gEDA gschem User Guide</a></em>, and <a href="geda-gnetlist_ug.html#scheme_backend_api" class="wikilink1" title="geda-gnetlist_ug.html">Scheme Backend API</a> section of the <em><a href="geda-gnetlist_ug.html" class="wikilink1" title="geda-gnetlist_ug.html">gEDA gnetlist User Guide</a></em>.
39 </p>
41 </div>
43 <h3 id="referencedocuments">Reference documents</h3>
44 <div class="level3">
45 <ul>
46 <li class="level1"><div class="li"> <a href="geda-gnetlistschemeprimitives.html" class="wikilink2" title="geda-gnetlistschemeprimitives.html">gnetlist Scheme primitives</a></div>
47 </li>
48 <li class="level1"><div class="li"> <a href="geda-gschemrepl.html" class="wikilink2" title="geda-gschemrepl.html">Using REPL in gschem</a></div>
49 </li>
50 </ul>
52 </div>
54 <h3 id="scriptingexamples">Scripting examples</h3>
55 <div class="level3">
57 <p>
58 You can download each script example and load it in <strong>gschem</strong>:
59 </p>
60 <ul>
61 <li class="level1"><div class="li"> just hit <kbd>:</kbd> and enter <pre class="code lisp"><span class="br0">&#40;</span>load <span class="st0">&quot;filename.scm&quot;</span><span class="br0">&#41;</span></pre>
62 </div>
63 </li>
64 <li class="level1"><div class="li"> then hit <kbd>Enter</kbd></div>
65 </li>
66 </ul>
68 <p>
69 You can install them as well if you don&#039;t want to load them every time:
70 </p>
71 <ul>
72 <li class="level1"><div class="li"> copy the script you want into your <em><code>~/.gEDA</code></em> directory</div>
73 </li>
74 <li class="level1"><div class="li"> put the line <pre class="code lisp"><span class="br0">&#40;</span>load <span class="st0">&quot;filename.scm&quot;</span><span class="br0">&#41;</span></pre>
76 <p>
77 into your <em><code>~/.gEDA/gschemrc</code></em> (replace <em>filename.scm</em> with the real name of the script)
78 </p>
79 </div>
80 </li>
81 </ul>
83 </div>
85 <h4 id="removingobjectswithspecificproperties">Removing objects with specific properties</h4>
86 <div class="level4">
88 <p>
89 For instance, let&#039;s remove all objects which are circles or arcs
90 with zero radius:
91 </p>
92 <dl class="file">
93 <dt><a href="/./doku.php?do=export_code&amp;id=:geda:guile_scripting.txt&amp;codeblock=2" title="Download Snippet" class="mediafile mf_scm">remove-objects.scm</a></dt>
94 <dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>geda page<span class="br0">&#41;</span><span class="br0">&#41;</span>
95 &nbsp;
96 <span class="co1">; Checks if the OBJECT is a circle or an arc with zero radius</span>
97 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>zero-radius-object? object<span class="br0">&#41;</span>
98 <span class="br0">&#40;</span><span class="kw1">or</span>
99 <span class="br0">&#40;</span><span class="kw1">and</span> <span class="br0">&#40;</span>circle? object<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="sy0">=</span> <span class="br0">&#40;</span>circle-radius object<span class="br0">&#41;</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
100 <span class="br0">&#40;</span><span class="kw1">and</span> <span class="br0">&#40;</span>arc? object<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="sy0">=</span> <span class="br0">&#40;</span>arc-radius object<span class="br0">&#41;</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
101 &nbsp;
102 <span class="br0">&#40;</span><span class="kw1">apply</span> page-remove<span class="sy0">!</span> <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span>
103 <span class="br0">&#40;</span>filter
104 zero-radius-object?
105 <span class="br0">&#40;</span>page-contents <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
106 </dd></dl>
109 Let&#039;s suppose we have a component with a known attribute to
110 remove, then we have to detach and remove all its attributes, too.
111 The function below does exactly this.
112 </p>
113 <dl class="file">
114 <dt><a href="/./doku.php?do=export_code&amp;id=:geda:guile_scripting.txt&amp;codeblock=3" title="Download Snippet" class="mediafile mf_scm">remove-components-with-attribs.scm</a></dt>
115 <dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>geda page<span class="br0">&#41;</span><span class="br0">&#41;</span>
116 <span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>geda object<span class="br0">&#41;</span><span class="br0">&#41;</span>
117 <span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>geda attrib<span class="br0">&#41;</span><span class="br0">&#41;</span>
118 &nbsp;
119 <span class="co1">; Removes all components having the attrib NAME=VALUE from PAGE</span>
120 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>delete-components-by-attrib<span class="sy0">!</span> page <span class="kw1">name</span> <span class="kw1">value</span><span class="br0">&#41;</span>
121 <span class="br0">&#40;</span>for-each
122 <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>obj<span class="br0">&#41;</span>
123 <span class="br0">&#40;</span><span class="kw1">if</span> <span class="br0">&#40;</span>component? obj<span class="br0">&#41;</span>
124 <span class="br0">&#40;</span>for-each
125 <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>attr<span class="br0">&#41;</span>
126 <span class="br0">&#40;</span><span class="kw1">and</span>
127 <span class="br0">&#40;</span>string<span class="sy0">=</span>? <span class="br0">&#40;</span>attrib-name attr<span class="br0">&#41;</span> <span class="kw1">name</span><span class="br0">&#41;</span>
128 <span class="br0">&#40;</span>string<span class="sy0">=</span>? <span class="br0">&#40;</span>attrib-value attr<span class="br0">&#41;</span> <span class="kw1">value</span><span class="br0">&#41;</span>
129 <span class="br0">&#40;</span><span class="kw1">let</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>attached-attribs <span class="br0">&#40;</span>object-attribs obj<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
130 <span class="br0">&#40;</span><span class="kw1">apply</span> detach-attribs<span class="sy0">!</span> obj attached-attribs<span class="br0">&#41;</span>
131 <span class="br0">&#40;</span><span class="kw1">apply</span> page-remove<span class="sy0">!</span> page obj attached-attribs<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
132 <span class="br0">&#40;</span>object-attribs obj<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
133 <span class="br0">&#40;</span>page-contents page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
134 </dd></dl>
137 After loading the file, hit <kbd>:</kbd> and enter, for example,
138 </p>
139 <pre class="code lisp"><span class="br0">&#40;</span>delete-components-by-attrib<span class="sy0">!</span> <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span> <span class="st0">&quot;refdes&quot;</span> <span class="st0">&quot;R1&quot;</span><span class="br0">&#41;</span></pre>
141 </div>
143 <h4 id="proceduresforinput-output">Procedures for input-output</h4>
144 <div class="level4">
147 The following script defines two procedures that can be used in
148 <strong>gaf shell</strong> batch scripts:
149 </p>
150 <ul>
151 <li class="level1"><div class="li"> <code>schematic-file-&gt;page</code></div>
152 </li>
153 <li class="level1"><div class="li"> <code>page-&gt;schematic-file</code></div>
154 </li>
155 </ul>
156 <dl class="file">
157 <dt><a href="/./doku.php?do=export_code&amp;id=:geda:guile_scripting.txt&amp;codeblock=5" title="Download Snippet" class="mediafile mf_scm">geda-io.scm</a></dt>
158 <dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>ice-<span class="nu0">9</span> lineio<span class="br0">&#41;</span><span class="br0">&#41;</span>
159 <span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>geda page<span class="br0">&#41;</span><span class="br0">&#41;</span>
160 &nbsp;
161 <span class="co1">; Input/output procedures</span>
162 <span class="co1">; reads FILE and outputs string</span>
163 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>file-<span class="sy0">&gt;</span>string file<span class="br0">&#41;</span>
164 <span class="br0">&#40;</span><span class="kw1">let</span>* <span class="br0">&#40;</span><span class="br0">&#40;</span>port <span class="br0">&#40;</span>make-line-buffering-input-port <span class="br0">&#40;</span>open-file file <span class="st0">&quot;r&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
165 <span class="br0">&#40;</span><span class="kw1">do</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>line <span class="st0">&quot;&quot;</span> <span class="br0">&#40;</span>read-string port<span class="br0">&#41;</span><span class="br0">&#41;</span>
166 <span class="br0">&#40;</span>s <span class="st0">&quot;&quot;</span> <span class="br0">&#40;</span>string-append s line<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
167 <span class="br0">&#40;</span><span class="br0">&#40;</span>eof-object? line<span class="br0">&#41;</span> <span class="co1">; test</span>
168 <span class="br0">&#40;</span>close-port port<span class="br0">&#41;</span> <span class="co1">; expression(s) to evaluate in the end</span>
169 s<span class="br0">&#41;</span> <span class="co1">; return value</span>
170 <span class="co1">; empty body</span>
171 <span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
172 &nbsp;
173 <span class="co1">; reads schematic FILE and outputs PAGE object</span>
174 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>schematic-file-<span class="sy0">&gt;</span>page file<span class="br0">&#41;</span>
175 <span class="br0">&#40;</span>string-<span class="sy0">&gt;</span>page file <span class="br0">&#40;</span>file-<span class="sy0">&gt;</span>string file<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
176 &nbsp;
177 <span class="co1">; saves schematic PAGE to FILE</span>
178 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>page-<span class="sy0">&gt;</span>schematic-file page file<span class="br0">&#41;</span>
179 <span class="br0">&#40;</span>with-output-to-file file
180 <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#40;</span>display <span class="br0">&#40;</span>page-<span class="sy0">&gt;</span>string page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
181 </dd></dl>
183 </div>
185 <h4 id="copymoveandrotateobjects">Copy, move, and rotate objects</h4>
186 <div class="level4">
187 <dl class="file">
188 <dt><a href="/./doku.php?do=export_code&amp;id=:geda:guile_scripting.txt&amp;codeblock=6" title="Download Snippet" class="mediafile mf_scm">move-and-rotate.scm</a></dt>
189 <dd><pre class="code file lisp"><span class="co1">; Scripting example by vzh per request of Kai-Martin Knaak :-)</span>
190 <span class="co1">; Use at your own risk.</span>
191 &nbsp;
192 <span class="co1">; The main procedure here is</span>
193 <span class="co1">; multiple-copy-move-and-rotate-selection which can be abbreviated</span>
194 <span class="co1">; as mcmars.</span>
195 <span class="co1">; Usage:</span>
196 <span class="co1">; launch gschem so it can use this script, e.g.</span>
197 <span class="co1">; gschem -s move-and-rotate.scm</span>
198 <span class="co1">; select objects in gschem, then hit ':' (semicolon) and type</span>
199 <span class="co1">; (mcmars '(1000 . 500) 90 10)</span>
200 <span class="co1">; hit &lt;Enter&gt;</span>
201 <span class="co1">; Enjoy!</span>
202 &nbsp;
203 &nbsp;
204 <span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
205 &nbsp;
206 <span class="co1">; align coords by ALIGN</span>
207 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>ceiling-coords vector align<span class="br0">&#41;</span>
208 <span class="br0">&#40;</span><span class="kw1">cons</span>
209 <span class="br0">&#40;</span>* <span class="br0">&#40;</span>ceiling-quotient <span class="br0">&#40;</span><span class="kw1">car</span> vector<span class="br0">&#41;</span> align<span class="br0">&#41;</span> align<span class="br0">&#41;</span>
210 <span class="br0">&#40;</span>* <span class="br0">&#40;</span>ceiling-quotient <span class="br0">&#40;</span><span class="kw1">cdr</span> vector<span class="br0">&#41;</span> align<span class="br0">&#41;</span> align<span class="br0">&#41;</span>
211 <span class="br0">&#41;</span><span class="br0">&#41;</span>
212 &nbsp;
213 <span class="co1">; Get minimum X and minimum Y of two pairs of coords</span>
214 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>min-coords coord1 coord2<span class="br0">&#41;</span>
215 <span class="br0">&#40;</span><span class="kw1">let</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>x <span class="br0">&#40;</span><span class="kw1">min</span> <span class="br0">&#40;</span><span class="kw1">car</span> coord1<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">car</span> coord2<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
216 <span class="br0">&#40;</span>y <span class="br0">&#40;</span><span class="kw1">min</span> <span class="br0">&#40;</span><span class="kw1">cdr</span> coord1<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">cdr</span> coord2<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
217 <span class="co1">; return value</span>
218 <span class="br0">&#40;</span><span class="kw1">cons</span> x y<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
219 &nbsp;
220 <span class="co1">; Copy, move and rotate current selection. The selected objects</span>
221 <span class="co1">; are first copied, then translated by VECTOR and finally rotated</span>
222 <span class="co1">; by ANGLE about center which is calculated as rounded by 100</span>
223 <span class="co1">; lower left coordinate of all objects in selection.</span>
224 <span class="co1">; If no objects are selected, opens gschem message dialog with</span>
225 <span class="co1">; warning.</span>
226 <span class="co1">; Returns the copied objects.</span>
227 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>copy-move-and-rotate-selection vector angle<span class="br0">&#41;</span>
228 <span class="br0">&#40;</span><span class="kw1">let</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>objects <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
229 <span class="br0">&#40;</span><span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">null</span>? objects<span class="br0">&#41;</span>
230 <span class="br0">&#40;</span>gschem-msg <span class="st0">&quot;Select something first!&quot;</span><span class="br0">&#41;</span>
231 <span class="co1">; else</span>
232 <span class="br0">&#40;</span><span class="kw1">let</span>* <span class="br0">&#40;</span><span class="br0">&#40;</span>copied-objects <span class="br0">&#40;</span>map copy-object objects<span class="br0">&#41;</span><span class="br0">&#41;</span>
233 <span class="br0">&#40;</span>translated-objects <span class="br0">&#40;</span><span class="kw1">apply</span> translate-objects<span class="sy0">!</span> vector copied-objects<span class="br0">&#41;</span><span class="br0">&#41;</span>
234 <span class="br0">&#40;</span>bounds <span class="br0">&#40;</span><span class="kw1">apply</span> object-bounds translated-objects<span class="br0">&#41;</span><span class="br0">&#41;</span>
235 <span class="br0">&#40;</span>rotation-center <span class="br0">&#40;</span>ceiling-coords <span class="br0">&#40;</span>min-coords <span class="br0">&#40;</span><span class="kw1">car</span> bounds<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">cdr</span> bounds<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="nu0">100</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
236 <span class="br0">&#40;</span>rotated-objects <span class="br0">&#40;</span><span class="kw1">apply</span> rotate-objects<span class="sy0">!</span> rotation-center angle translated-objects<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
237 <span class="br0">&#40;</span><span class="kw1">apply</span> page-append<span class="sy0">!</span> <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span> rotated-objects<span class="br0">&#41;</span>
238 rotated-objects<span class="br0">&#41;</span>
239 <span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
240 &nbsp;
241 <span class="co1">; Multiply VECTOR which must be a pair by NUMBER</span>
242 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>multiply-vector-by vector number<span class="br0">&#41;</span>
243 <span class="br0">&#40;</span><span class="kw1">cons</span> <span class="br0">&#40;</span>* number <span class="br0">&#40;</span><span class="kw1">car</span> vector<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#40;</span>* number <span class="br0">&#40;</span><span class="kw1">cdr</span> vector<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
244 &nbsp;
245 <span class="co1">; Copy, move and rotate current selection NUMBER times. Applies</span>
246 <span class="co1">; the copy-move-and-rotate-selection procedure multiple times</span>
247 <span class="co1">; increasing every time vector and angle by given values of VECTOR</span>
248 <span class="co1">; and ANGLE.</span>
249 <span class="co1">; If no objects are selected, opens gschem message dialog with</span>
250 <span class="co1">; warning.</span>
251 <span class="co1">; Return value is unspecified.</span>
252 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>multiple-copy-move-and-rotate-selection vector angle num<span class="br0">&#41;</span>
253 <span class="br0">&#40;</span><span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">null</span>? <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
254 <span class="br0">&#40;</span>gschem-msg <span class="st0">&quot;Select something first!&quot;</span><span class="br0">&#41;</span>
255 <span class="co1">; else</span>
256 <span class="br0">&#40;</span><span class="kw1">do</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>i num <span class="br0">&#40;</span><span class="nu0">1</span>- i<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
257 <span class="br0">&#40;</span><span class="br0">&#40;</span><span class="sy0">=</span> i <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
258 <span class="br0">&#40;</span>copy-move-and-rotate-selection
259 <span class="br0">&#40;</span>multiply-vector-by vector i<span class="br0">&#41;</span> <span class="br0">&#40;</span>* angle i<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
260 <span class="br0">&#41;</span><span class="br0">&#41;</span>
261 &nbsp;
262 <span class="co1">; Abbreviated name for the multiple-copy-move-and-rotate-selection</span>
263 <span class="co1">; procedure</span>
264 <span class="br0">&#40;</span>define mcmars multiple-copy-move-and-rotate-selection<span class="br0">&#41;</span></pre>
265 </dd></dl>
267 </div>
269 <h4 id="groupattributeediting">Group attribute editing</h4>
270 <div class="level4">
273 Let&#039;s suppose you have selected several resistors&#039; refdeses and
274 want to rename them at once, e.g., if they were copy from another
275 place.
276 </p>
277 <dl class="file">
278 <dt><a href="/./doku.php?do=export_code&amp;id=:geda:guile_scripting.txt&amp;codeblock=7" title="Download Snippet" class="mediafile mf_">set-selected-attribs-value</a></dt>
279 <dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
280 &nbsp;
281 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>set-selected-attribs-value<span class="sy0">!</span> <span class="kw1">value</span><span class="br0">&#41;</span>
282 <span class="br0">&#40;</span>for-each
283 <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>attrib<span class="br0">&#41;</span>
284 <span class="br0">&#40;</span>set-attrib-value<span class="sy0">!</span> attrib <span class="kw1">value</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
285 <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
286 </dd></dl>
289 Usage of the procedure in <strong>gschem</strong>:
290 </p>
291 <pre class="code lisp"><span class="br0">&#40;</span>set-selected-attribs-value<span class="sy0">!</span> <span class="st0">&quot;R100.?&quot;</span><span class="br0">&#41;</span></pre>
294 Now, after renumbering them using <kbd>T</kbd> <kbd>U</kbd>, you
295 copy them all and want to rename those copied resistors appending a suffix:
296 </p>
297 <dl class="file">
298 <dt><a href="/./doku.php?do=export_code&amp;id=:geda:guile_scripting.txt&amp;codeblock=9" title="Download Snippet" class="mediafile mf_scm">append-selected-attribs-suffix.scm</a></dt>
299 <dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
300 &nbsp;
301 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>append-selected-attribs-suffix<span class="sy0">!</span> suffix<span class="br0">&#41;</span>
302 <span class="br0">&#40;</span>for-each
303 <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>attrib<span class="br0">&#41;</span>
304 <span class="br0">&#40;</span>set-attrib-value<span class="sy0">!</span>
305 attrib
306 <span class="br0">&#40;</span>string-append <span class="br0">&#40;</span>attrib-value attrib<span class="br0">&#41;</span> suffix<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
307 <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
308 </dd></dl>
311 Usage of the procedure in <strong>gschem</strong>:
312 </p>
313 <pre class="code lisp"><span class="br0">&#40;</span>append-selected-attribs-suffix<span class="sy0">!</span> <span class="st0">&quot;-top&quot;</span><span class="br0">&#41;</span></pre>
316 Now, let&#039;s rename some other attributes by adding a prefix:
317 </p>
318 <dl class="file">
319 <dt><a href="/./doku.php?do=export_code&amp;id=:geda:guile_scripting.txt&amp;codeblock=11" title="Download Snippet" class="mediafile mf_scm">append-selected-attribs-prefix.scm</a></dt>
320 <dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
321 &nbsp;
322 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>append-selected-attribs-prefix<span class="sy0">!</span> prefix<span class="br0">&#41;</span>
323 <span class="br0">&#40;</span>for-each
324 <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>object<span class="br0">&#41;</span>
325 <span class="br0">&#40;</span><span class="kw1">and</span> <span class="br0">&#40;</span>attribute? object<span class="br0">&#41;</span>
326 <span class="br0">&#40;</span>set-attrib-value<span class="sy0">!</span>
327 object
328 <span class="br0">&#40;</span>string-append prefix <span class="br0">&#40;</span>attrib-value object<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
329 <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
330 </dd></dl>
333 Usage of the procedure in <strong>gschem</strong>:
334 </p>
335 <pre class="code lisp"><span class="br0">&#40;</span>append-selected-attribs-prefix<span class="sy0">!</span> <span class="st0">&quot;A1.&quot;</span><span class="br0">&#41;</span></pre>
338 Let&#039;s replace first letters of selected attribs with prefix:
339 </p>
340 <dl class="file">
341 <dt><a href="/./doku.php?do=export_code&amp;id=:geda:guile_scripting.txt&amp;codeblock=13" title="Download Snippet" class="mediafile mf_scm">append-selected-attribs-prefix.scm</a></dt>
342 <dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
343 &nbsp;
344 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>replace-selected-attribs-prefix<span class="sy0">!</span> prefix<span class="br0">&#41;</span>
345 <span class="br0">&#40;</span>for-each
346 <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>object<span class="br0">&#41;</span>
347 <span class="br0">&#40;</span><span class="kw1">and</span> <span class="br0">&#40;</span>attribute? object<span class="br0">&#41;</span>
348 <span class="br0">&#40;</span>set-attrib-value<span class="sy0">!</span>
349 object
350 <span class="br0">&#40;</span>string-append
351 prefix
352 <span class="br0">&#40;</span>string-copy <span class="br0">&#40;</span>attrib-value object<span class="br0">&#41;</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
353 <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
354 </dd></dl>
357 Usage of the procedure in <strong>gschem</strong>:
358 </p>
359 <pre class="code lisp"><span class="br0">&#40;</span>replace-selected-attribs-prefix<span class="sy0">!</span> <span class="st0">&quot;C&quot;</span><span class="br0">&#41;</span></pre>
362 Let&#039;s rename selected <code>netname=</code> attributes increasing them by a
363 fixed number:
364 </p>
365 <dl class="file">
366 <dt><a href="/./doku.php?do=export_code&amp;id=:geda:guile_scripting.txt&amp;codeblock=15" title="Download Snippet" class="mediafile mf_scm">add-selected-attribs-number.scm</a></dt>
367 <dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
368 &nbsp;
369 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>add-selected-attribs-number<span class="sy0">!</span> number<span class="br0">&#41;</span>
370 <span class="br0">&#40;</span>for-each
371 <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>object<span class="br0">&#41;</span>
372 <span class="br0">&#40;</span><span class="kw1">and</span> <span class="br0">&#40;</span>attribute? object<span class="br0">&#41;</span>
373 <span class="br0">&#40;</span>set-attrib-value<span class="sy0">!</span>
374 object
375 <span class="br0">&#40;</span>number-<span class="sy0">&gt;</span>string
376 <span class="br0">&#40;</span>+ <span class="br0">&#40;</span>string-<span class="sy0">&gt;</span>number <span class="br0">&#40;</span>attrib-value object<span class="br0">&#41;</span><span class="br0">&#41;</span> number<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
377 <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
378 </dd></dl>
381 Usage of the procedure in <strong>gschem</strong>:
382 </p>
383 <pre class="code lisp"><span class="br0">&#40;</span>add-selected-attribs-number<span class="sy0">!</span> <span class="nu0">100</span><span class="br0">&#41;</span></pre>
386 We could set any function instead of &quot;+&quot; on the net number in this procedure.
387 For instance:
388 </p>
389 <dl class="file">
390 <dt><a href="/./doku.php?do=export_code&amp;id=:geda:guile_scripting.txt&amp;codeblock=17" title="Download Snippet" class="mediafile mf_scm">use-another-func.scm</a></dt>
391 <dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
392 &nbsp;
393 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>use-another-func<span class="sy0">!</span> func<span class="br0">&#41;</span>
394 <span class="br0">&#40;</span>for-each
395 <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>object<span class="br0">&#41;</span>
396 <span class="br0">&#40;</span><span class="kw1">and</span> <span class="br0">&#40;</span>attribute? object<span class="br0">&#41;</span>
397 <span class="br0">&#40;</span>set-attrib-value<span class="sy0">!</span>
398 object
399 <span class="br0">&#40;</span>number-<span class="sy0">&gt;</span>string
400 <span class="br0">&#40;</span>func <span class="br0">&#40;</span>string-<span class="sy0">&gt;</span>number <span class="br0">&#40;</span>attrib-value object<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
401 <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
402 </dd></dl>
405 Usage of the procedure in <strong>gschem</strong>:
406 </p>
407 <pre class="code lisp"><span class="br0">&#40;</span>use-another-func<span class="sy0">!</span> -<span class="br0">&#41;</span>
408 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>multiply-by-<span class="nu0">2</span> x<span class="br0">&#41;</span>
409 <span class="br0">&#40;</span>* <span class="nu0">2</span> x<span class="br0">&#41;</span><span class="br0">&#41;</span>
410 <span class="br0">&#40;</span>use-another-func<span class="sy0">!</span> multiply-by-<span class="nu0">2</span><span class="br0">&#41;</span></pre>
412 </div>
414 <h4 id="movingobjectsusingarrows">Moving objects using arrows</h4>
415 <div class="level4">
418 Let&#039;s define actions to move selected objects using
419 <kbd>Shift</kbd> + arrow keys.
420 </p>
421 <dl class="file">
422 <dt><a href="/./doku.php?do=export_code&amp;id=:geda:guile_scripting.txt&amp;codeblock=19" title="Download Snippet" class="mediafile mf_scm">arrow-move.scm</a></dt>
423 <dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
424 &nbsp;
425 <span class="co1">; Default offset to move</span>
426 <span class="br0">&#40;</span>define offset <span class="nu0">100</span><span class="br0">&#41;</span>
427 &nbsp;
428 <span class="co1">; Get moving vector</span>
429 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>move-selection direction<span class="br0">&#41;</span>
430 <span class="br0">&#40;</span><span class="kw1">apply</span> translate-objects<span class="sy0">!</span>
431 <span class="br0">&#40;</span><span class="kw1">case</span> direction
432 <span class="br0">&#40;</span><span class="br0">&#40;</span>left <span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">cons</span> <span class="br0">&#40;</span>- offset<span class="br0">&#41;</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
433 <span class="br0">&#40;</span><span class="br0">&#40;</span>right<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">cons</span> <span class="br0">&#40;</span>+ offset<span class="br0">&#41;</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
434 <span class="br0">&#40;</span><span class="br0">&#40;</span>down <span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">cons</span> <span class="nu0">0</span> <span class="br0">&#40;</span>- offset<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
435 <span class="br0">&#40;</span><span class="br0">&#40;</span>up <span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">cons</span> <span class="nu0">0</span> <span class="br0">&#40;</span>+ offset<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
436 <span class="br0">&#40;</span>else #f<span class="br0">&#41;</span><span class="br0">&#41;</span>
437 <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
438 &nbsp;
439 <span class="co1">; Define actions</span>
440 <span class="br0">&#40;</span>define <span class="br0">&#40;</span><span class="sy0">&amp;</span>move-selection-left <span class="br0">&#41;</span> <span class="br0">&#40;</span>move-selection 'left <span class="br0">&#41;</span><span class="br0">&#41;</span>
441 <span class="br0">&#40;</span>define <span class="br0">&#40;</span><span class="sy0">&amp;</span>move-selection-right<span class="br0">&#41;</span> <span class="br0">&#40;</span>move-selection 'right<span class="br0">&#41;</span><span class="br0">&#41;</span>
442 <span class="br0">&#40;</span>define <span class="br0">&#40;</span><span class="sy0">&amp;</span>move-selection-down <span class="br0">&#41;</span> <span class="br0">&#40;</span>move-selection 'down <span class="br0">&#41;</span><span class="br0">&#41;</span>
443 <span class="br0">&#40;</span>define <span class="br0">&#40;</span><span class="sy0">&amp;</span>move-selection-up <span class="br0">&#41;</span> <span class="br0">&#40;</span>move-selection 'up <span class="br0">&#41;</span><span class="br0">&#41;</span>
444 &nbsp;
445 <span class="co1">; Define shortcuts</span>
446 <span class="br0">&#40;</span>global-set-key <span class="st0">&quot;&lt;Shift&gt;Left&quot;</span> '<span class="sy0">&amp;</span>move-selection-left<span class="br0">&#41;</span>
447 <span class="br0">&#40;</span>global-set-key <span class="st0">&quot;&lt;Shift&gt;Right&quot;</span> '<span class="sy0">&amp;</span>move-selection-right<span class="br0">&#41;</span>
448 <span class="br0">&#40;</span>global-set-key <span class="st0">&quot;&lt;Shift&gt;Up&quot;</span> '<span class="sy0">&amp;</span>move-selection-up<span class="br0">&#41;</span>
449 <span class="br0">&#40;</span>global-set-key <span class="st0">&quot;&lt;Shift&gt;Down&quot;</span> '<span class="sy0">&amp;</span>move-selection-down<span class="br0">&#41;</span></pre>
450 </dd></dl>
453 The following script redefines current shortcuts so that if
454 nothing is selected the canvas is moved with arrow keys (without
455 <kbd>Shift</kbd> in this case), otherwise selected objects are
456 moved.
457 </p>
458 <dl class="file">
459 <dt><a href="/./doku.php?do=export_code&amp;id=:geda:guile_scripting.txt&amp;codeblock=20" title="Download Snippet" class="mediafile mf_scm">arrow-move2.scm</a></dt>
460 <dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
461 &nbsp;
462 <span class="co1">; Default offset to move</span>
463 <span class="br0">&#40;</span>define offset <span class="nu0">100</span><span class="br0">&#41;</span>
464 &nbsp;
465 <span class="co1">; Get moving vector</span>
466 <span class="br0">&#40;</span>define <span class="br0">&#40;</span>move-selection direction<span class="br0">&#41;</span>
467 <span class="br0">&#40;</span><span class="kw1">let</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>selection <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
468 <span class="br0">&#40;</span><span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">null</span>? selection<span class="br0">&#41;</span>
469 <span class="co1">; default behaviour</span>
470 <span class="br0">&#40;</span><span class="kw1">case</span> direction
471 <span class="br0">&#40;</span><span class="br0">&#40;</span>left <span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="sy0">&amp;</span>view-pan-left<span class="br0">&#41;</span><span class="br0">&#41;</span>
472 <span class="br0">&#40;</span><span class="br0">&#40;</span>right<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="sy0">&amp;</span>view-pan-right<span class="br0">&#41;</span><span class="br0">&#41;</span>
473 <span class="br0">&#40;</span><span class="br0">&#40;</span>down <span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="sy0">&amp;</span>view-pan-down<span class="br0">&#41;</span><span class="br0">&#41;</span>
474 <span class="br0">&#40;</span><span class="br0">&#40;</span>up <span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="sy0">&amp;</span>view-pan-up<span class="br0">&#41;</span><span class="br0">&#41;</span>
475 <span class="br0">&#40;</span>else #f<span class="br0">&#41;</span><span class="br0">&#41;</span>
476 <span class="co1">; modified behaviour</span>
477 <span class="br0">&#40;</span><span class="kw1">apply</span> translate-objects<span class="sy0">!</span>
478 <span class="br0">&#40;</span><span class="kw1">case</span> direction
479 <span class="br0">&#40;</span><span class="br0">&#40;</span>left <span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">cons</span> <span class="br0">&#40;</span>- offset<span class="br0">&#41;</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
480 <span class="br0">&#40;</span><span class="br0">&#40;</span>right<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">cons</span> <span class="br0">&#40;</span>+ offset<span class="br0">&#41;</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
481 <span class="br0">&#40;</span><span class="br0">&#40;</span>down <span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">cons</span> <span class="nu0">0</span> <span class="br0">&#40;</span>- offset<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
482 <span class="br0">&#40;</span><span class="br0">&#40;</span>up <span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">cons</span> <span class="nu0">0</span> <span class="br0">&#40;</span>+ offset<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
483 <span class="br0">&#40;</span>else #f<span class="br0">&#41;</span><span class="br0">&#41;</span>
484 <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
485 <span class="br0">&#41;</span><span class="br0">&#41;</span>
486 &nbsp;
487 <span class="co1">; Define actions</span>
488 <span class="br0">&#40;</span>define <span class="br0">&#40;</span><span class="sy0">&amp;</span>move-selection-left <span class="br0">&#41;</span> <span class="br0">&#40;</span>move-selection 'left <span class="br0">&#41;</span><span class="br0">&#41;</span>
489 <span class="br0">&#40;</span>define <span class="br0">&#40;</span><span class="sy0">&amp;</span>move-selection-right<span class="br0">&#41;</span> <span class="br0">&#40;</span>move-selection 'right<span class="br0">&#41;</span><span class="br0">&#41;</span>
490 <span class="br0">&#40;</span>define <span class="br0">&#40;</span><span class="sy0">&amp;</span>move-selection-down <span class="br0">&#41;</span> <span class="br0">&#40;</span>move-selection 'down <span class="br0">&#41;</span><span class="br0">&#41;</span>
491 <span class="br0">&#40;</span>define <span class="br0">&#40;</span><span class="sy0">&amp;</span>move-selection-up <span class="br0">&#41;</span> <span class="br0">&#40;</span>move-selection 'up <span class="br0">&#41;</span><span class="br0">&#41;</span>
492 &nbsp;
493 <span class="co1">; Define shortcuts</span>
494 <span class="br0">&#40;</span>global-set-key <span class="st0">&quot;Left&quot;</span> '<span class="sy0">&amp;</span>move-selection-left<span class="br0">&#41;</span>
495 <span class="br0">&#40;</span>global-set-key <span class="st0">&quot;Right&quot;</span> '<span class="sy0">&amp;</span>move-selection-right<span class="br0">&#41;</span>
496 <span class="br0">&#40;</span>global-set-key <span class="st0">&quot;Up&quot;</span> '<span class="sy0">&amp;</span>move-selection-up<span class="br0">&#41;</span>
497 <span class="br0">&#40;</span>global-set-key <span class="st0">&quot;Down&quot;</span> '<span class="sy0">&amp;</span>move-selection-down<span class="br0">&#41;</span></pre>
498 </dd></dl>
500 </div>
501 </body>
502 </html>