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)
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)
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
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)))
44 (cons (match:substring match 1)
45 (string->number (match:substring match 2)))
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)
57 (not (attrib-inherited? a))
58 (string=? "refdes" (attrib-name a))))
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))
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))
80 ; Return first number not present in used greater or equal to minimum
81 (define (find-first-unused used minimum)
85 ((= n (car xs)) (go (1+ n) (cdr xs)))
86 (else (go n (cdr xs)))))
89 ; Do the work - first check if attributes contain refdes with prefix
90 (let* ((refdeses (refdes-attrs attribs))
91 (refdes (if (null? refdeses)
95 (get-prefix (attrib-value refdes))
98 (let* ((used-nums (sort-list (collect-all-numbers prefix) <))
99 ; minimum value considering the page-offset
102 (<= auto-uref-page-offset 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~%"
115 (string-append prefix (number->string next-num)))))))