libgeda: Make sure all object fields are initialized
[geda-gaf.git] / gschem / scheme / auto-uref.scm
blobcad1c883ddf7511c4d81d82a1e659736e538ea38
1 ;; gEDA - GPL Electronic Design Automation
2 ;; gschem - gEDA Schematic Capture
3 ;; Copyright (C) 1998-2010 Ales Hvezda
4 ;; Copyright (C) 1998-2020 gEDA Contributors (see ChangeLog for details)
5 ;;
6 ;; This program is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 2 of the License, or
9 ;; (at your option) any later version.
11 ;; This program is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with this program; if not, write to the Free Software
18 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 (use-modules (ice-9 regex)
21              (srfi srfi-1)
22              (geda page)
23              (geda attrib)
24              (gschem window))
26 (define auto-uref-page-offset 0)
28 ;; Redefine value of page-offset.
29 ;; Refdeses will be numbered from integer multiples of page-offset,
30 ;; depending on the lowest refdes value found on the page.
31 ;; If lowest value is 323 and page offset is 100, then next refdes
32 ;; will be 301.
33 ;; Setting to 0 disables the feature.
34 (define (auto-uref-set-page-offset new-offset)
35   (set! auto-uref-page-offset new-offset))
37 ;; Modify attributes of an object to assign next unused refdes value
38 (define (auto-uref attribs)
40   ; Return (prefix . number) on match or #f on failure
41   (define (split-value value)
42     (let ((match (string-match "^([A-Za-z]*)([0-9]+)$" value)))
43       (if match
44           (cons (match:substring match 1)
45                 (string->number (match:substring match 2)))
46           #f)))
48   ; Extract prefix from a refdes attribute value
49   (define (get-prefix value)
50     (let ((prefix (string-match "^[A-Za-z]*" value)))
51       (if prefix (match:substring prefix) #f)))
53   ; Filter non-inherited refdes values
54   (define (refdes-attrs attribs)
55     (filter (lambda (a)
56               (and
57                 (not (attrib-inherited? a))
58                 (string=? "refdes" (attrib-name a))))
59             attribs))
61   ; Extract numbers from refdeses that have given prefix
62   (define (extract-numbers object prefix)
63     (let* ((refdeses (refdes-attrs (object-attribs object)))
64            (vals (map attrib-value refdeses))
65            (prefix-numbers (filter-map split-value vals))
66            (numbers (filter-map (lambda (n.v)
67                                   (if (string=? prefix (car n.v))
68                                       (cdr n.v)
69                                       #f))
70                                 prefix-numbers)))
71       numbers))
73   ; Collect all numbers associated with prefix on current page
74   (define (collect-all-numbers prefix)
75     (let ((objects (page-contents (active-page))))
76       (concatenate (map (lambda (o)
77                           (extract-numbers o prefix))
78                         objects))))
80   ; Return first number not present in used greater or equal to minimum
81   (define (find-first-unused used minimum)
82     (define (go n xs)
83       (cond ((null? xs) n)
84             ((< n (car xs)) n)
85             ((= n (car xs)) (go (1+ n) (cdr xs)))
86             (else (go n (cdr xs)))))
87     (go minimum used))
89   ; Do the work - first check if attributes contain refdes with prefix
90   (let* ((refdeses (refdes-attrs attribs))
91          (refdes (if (null? refdeses)
92                      #f
93                      (car refdeses)))
94          (prefix (if refdes
95                      (get-prefix (attrib-value refdes))
96                      #f)))
97     (if prefix
98         (let* ((used-nums (sort-list (collect-all-numbers prefix) <))
99                ; minimum value considering the page-offset
100                (min-num (if (or
101                               (null? used-nums)
102                               (<= auto-uref-page-offset 0))
103                             0
104                             (* (floor (/ (car used-nums)
105                                          auto-uref-page-offset))
106                                auto-uref-page-offset)))
107                ; next refdes number to be assigned
108                (next-num (find-first-unused used-nums (1+ min-num))))
109           ;(simple-format #t "~A: ~A -> ~A~%"
110           ;                  prefix
111           ;                  used-nums
112           ;                  next-num)
113           (set-attrib-value!
114             refdes
115             (string-append prefix (number->string next-num)))))))