]> git.donarmstrong.com Git - lilypond.git/blobdiff - elisp/lilypond-what-beat.el
lilypond-what-beat.el: avoid global assignment to new-duration
[lilypond.git] / elisp / lilypond-what-beat.el
index dae02d53d23f6f5bb1c6cff35e643abe56fd0f8f..7541a34e4a9ef14c15c9d9567badef55da6563d9 100644 (file)
 
 (defun add-fractions (f1 f2)
   "Adds two fractions, both are (numerator denominator)"
-  (set 'result (list (+ (* (car f1) (cadr f2)) (* (car f2) (cadr f1)))
+  (setresult (list (+ (* (car f1) (cadr f2)) (* (car f2) (cadr f1)))
                     (* (cadr f1) (cadr f2))))
-  (set 'result (reduce-fraction result 2))
-  (set 'result (reduce-fraction result 3))
-  (set 'result (reduce-fraction result 5))
-  (set 'result (reduce-fraction result 7))
+  (setresult (reduce-fraction result 2))
+  (setresult (reduce-fraction result 3))
+  (setresult (reduce-fraction result 5))
+  (setresult (reduce-fraction result 7))
 )
 
 
@@ -72,7 +72,7 @@
              (= 0 (% (cadr result) divisor))
              (< 1 (cadr result))
              (< 0 (car result)))
-    (set 'result (list (/ (car result) divisor) (/ (cadr result) divisor))))
+    (setresult (list (/ (car result) divisor) (/ (cadr result) divisor))))
   result
 )
 
        (denominator (or (extract-match duration 8) "1")))
     (if (and (not (null dots)) (< 0 (string-width dots)))
        (dotimes (dummy (string-width dots))
-         (set 'result (list (1+ (* 2 (car result))) (* 2 (cadr result))))))
+         (setresult (list (1+ (* 2 (car result))) (* 2 (cadr result))))))
     (list (* (string-to-int numerator) (car result))
          (* (string-to-int denominator) (cadr result)))
 ))
 
 (defun walk-note-duration ()
-"Returns duration of next note, moving point past note.
+  "Returns duration of next note, moving point past note.
 If point is not before a note, returns nil
 If next note has no duration, returns t"
-  (if (not (looking-at pitch-regex))
-      nil
-    (progn
+  (let ((have-pitch (looking-at pitch-regex)))
+    (if have-pitch (goto-char (match-end 0)))
+    (if (not (looking-at duration-regex))
+       have-pitch
       (goto-char (match-end 0))
-      (if (not (looking-at duration-regex))
-         t
-       (progn
-         (goto-char (match-end 0))
-         (parse-duration (match-string 0)))))))
+      (parse-duration (match-string 0)))))
 
 ; returns nil if not at a comment
 (defun skip-comment ()
@@ -161,7 +158,7 @@ If next note has no duration, returns t"
             (skip-quotation)
             (skip-sexp)))
   ; Now skip anything that isn't alphanum or \. And skip comments or quotations
-  (while (or (< 0 (skip-chars-forward "^A-Za-z<%}#=\""))
+  (while (or (< 0 (skip-chars-forward "^A-Za-z1-9<%}#=\""))
             (skip-comment)
             (skip-quotation)
             (skip-sexp)))
@@ -202,11 +199,10 @@ If next note has no duration, returns t"
             (result '(0 1)))           ; 0 in fraction form
        (if (= measure-start -1)
            (message "No | before point")
-         (progn
-           (goto-char (1+ measure-start))
-           (goto-note-begin)
-           (while (< (point) end)
-             (set 'new-duration (walk-note-duration))
+         (goto-char (1+ measure-start))
+         (goto-note-begin)
+         (while (< (point) end)
+           (let ((new-duration (walk-note-duration)))
              (if (null new-duration)
                  (if (not (looking-at "\\\\times[ \t]*\\([1-9]*\\)/\\([1-9]*\\)[ \t\n]*{"))
                      (skip-good-keywords)
@@ -218,14 +214,14 @@ If next note has no duration, returns t"
                      (goto-note-begin)
                      (while (and (not (looking-at "}"))
                                  (< (point) end))
-                       (set 'new-duration (walk-note-duration))
+                       (setnew-duration (walk-note-duration))
                        (if (null new-duration)
                            (if (looking-at "\\\\[a-z]*[ \t]*[a-z]*")
                                (goto-char (match-end 0))
                              (error "Unknown text: %S %s" result(buffer-substring (point) end))))
                        (if (not (eq new-duration t))
-                           (set 'duration new-duration))
-                       (set 'result (add-fractions result
+                           (setduration new-duration))
+                       (setresult (add-fractions result
                                                    (list (* numerator (car duration))
                                                          (* denominator (cadr duration)))))
                        (goto-note-begin))
@@ -233,12 +229,11 @@ If next note has no duration, returns t"
                          (forward-char 1)))) ; skip }
 
                (if (not (eq new-duration t))
-                   (set 'duration new-duration))
-               (set 'result (add-fractions result duration)))
-             (goto-note-begin))
+                   (setduration new-duration))
+               (setresult (add-fractions result duration)))
+             (goto-note-begin)))
 
-           result
-))))))
+         result)))))
 
 (defun LilyPond-what-beat ()
   "Returns how much of a measure lies between last measaure '|' and point.