Commits

Ivan Andrus committed 072aeb6

Allow strict matching
Added whizzy-narrow-to-tikz

  • Participants
  • Parent commits cc3eba3

Comments (0)

Files changed (3)

 
   \node[ADVI,/tikz/at={(4.6286,2.1118)}] {x};
   \def\y{0.5}
-  \node[circle,fill=red!60,ADVI] at (2.6817,-\y) {y};
+  \node[circle,fill=red!60,ADVI={type=macro}] at (2.681,-\y) {y};
+  \node[circle,fill=red!60,ADVI={type=macro}] at (2.98259,18.60109) {yy};
 
 
   \draw[ADVI] (1,1) -- (2,2) node{z} -- (3,1);

File whizzy-tikz.el

                      (whizzy-reslice))))))))))
 
 
+(defvar whizzy-tikz-environments
+  '("tikzpicture"
+    "scope"))
+
+(defun whizzy-narrow-to-tikz ()
+  "Currently only supports environment form of tikz.
+Return nil if not in a tikz environment as defined by
+`whizzy-tikz-environments'."
+  (interactive)
+  (let ((n 1)
+        (env (LaTeX-current-environment)))
+    ;; Limit to the current environment
+    (while (and (not (member env whizzy-tikz-environments))
+                (not (string= "document" env))
+                (< n 10))
+      (setq n (1+ n))
+      (setq env (LaTeX-current-environment n)))
+    (if (string= "document" env)
+        nil ;; Couldn't narrow
+      (narrow-to-region
+       (save-excursion
+         (let ((LaTeX-syntactic-comments nil))
+           (dotimes (dummy n)
+             (LaTeX-find-matching-begin)))
+         ;; (sit-for 1)
+         (point))
+       (save-excursion
+         (let ((LaTeX-syntactic-comments nil))
+           (dotimes (dummy n)
+             (LaTeX-find-matching-end)))
+         ;; (sit-for 1)
+         (point)))
+
+      ;; Now try to limit to the current statement.
+
+      ;; This is not guaranteed to work since might have ; in node
+      ;; name, comment etc.
+      (let* ((b1 (save-excursion
+                   (beginning-of-line)
+                   (when (search-backward ";" nil t)
+                       (forward-char 1))
+                   (point)))
+             (e1 (save-excursion
+                   (beginning-of-line)
+                   (search-forward ";" nil t) ;; Maybe not guaranteed to work since
+                   ;; might have ; in node name etc.
+                   (point)))
+             (lep (line-end-position))
+             ;; No because this will miss the ; at the end of the line!!!!
+             (just-this-line (or (> e1 lep)
+                                 (save-excursion
+                                   (goto-char e1)
+                                   (looking-at "\\s *\\(%\\|$\\)"))))
+             (e2 (if just-this-line
+                     e1
+                   (save-excursion
+                     (end-of-line)
+                     (search-forward ";" nil t) ;; Maybe not guaranteed to work since
+                     ;; might have ; in node name etc.
+                     (show (point))))))
+        (narrow-to-region b1 e2)))))
+
+(defun whizzy-tikz-strip-trailing-zeros (string)
+  "Return STRING stripped of all whitespace."
+  (save-match-data
+    (while (string-match "0+$" string)
+      (setq string (replace-match "" t t string))))
+  string)
+
+;; These could certainly use some work
+(defvar whizzy-tikz-node-regexp
+  '(
+    ;; strict seems pretty useless, but it could be used to validate
+    ;; that one of the coordinates are close (assuming that one is a
+    ;; number and the other is a macro).  Then it even prompt to
+    ;; change the macro/forloop if desired.  This shouldn't be an
+    ;; alist of regexps, but an alist of matching functions.  Then I
+    ;; would have a similar alist of replacing functions, one of which
+    ;; would be duplicate, one might change a macro, one might round
+    ;; etc.  (whizzy-tikz-strip-trailing-zeros(format "%.10f" 1.2345))
+    (strict . "\\(?:at *\\)?(\\(\\(?:-?[0-9.]+\\|\\),-?[0-9.]+\\))")
+    ;; Lax can be used
+    (lax    . "\\(?:at *\\)?(\\([()]+\\))")))
 
 (defun whizzy-tikz-match (command name first line file type dx dy)
   (save-window-excursion
     (save-excursion
-      (show file)
-      (show name)
-      (and (prog1 (whizzy-goto-file file)
-             (widen))
-           (goto-char (point-min))
-           (prog1 (forward-line (1- (string-to-number line)))
-             (end-of-line))
+      (save-restriction
+        (show file)
+        (show name)
+        (and (prog1 (whizzy-goto-file file)
+               (widen))
+             (goto-char (point-min))
+             (prog1 (forward-line (1- (string-to-number line)))
+               (beginning-of-line))
 
-           ;; (narrow-to-defun)
-           (show 'here)
-           (sit-for 2)
-           (let ((regexp "\\(?:at *\\)?(\\(-?[0-9.]+,-?[0-9.]+\\))")
-                 (replacement (format "%s,%s" dx dy))
-                 (modified  (buffer-modified-p)))
+             ;; (narrow-to-defun)
+             ;; (show line)
+             ;; (sit-for 2)
+             (show (whizzy-narrow-to-tikz))
 
-             (when
-                 (or (re-search-backward regexp (point-min) t)
-                     (re-search-forward regexp (point-max) t))
+             ;; Here we need strict methods of finding and keep loosening them...
 
-               ;; (show 'there)
-               ;; (sit-for 5)
-               (let (;(begin  (match-beginning 1))
-                     (edited
-                      ;;  (save-match-data
-                      ;;    (and (string= (match-string-no-properties 1) dx)
-                      ;;         (string= (match-string-no-properties 2) dy))))
-                      t)
-                     )
-                 (show (match-string-no-properties 0))
-                 ;; (show (match-beginning 1))
-                 ;; (show (match-end 1))
-                 (show (replace-match replacement t t nil 1))
-                 ;; Have to figure out how to account for center vs lower left
-                 ;; (replace-match dx t t nil 1)
-                 ;; (replace-match dy t t nil 2) ;or the other order??? Can you do both?
-                 ;; (goto-char begin)
-                 ;; (message "%S=%S %S=%S" x dx y dy)
-                 ;; (setq edited (whizzy-edit-field x dx))
-                 ;; (setq edited (or (whizzy-edit-field y dy) edited))
-                 (unless (not edited)
-                   (if (or modified
-                           (equal (whizzy-get whizzy-active-buffer)
-                                  (current-buffer)))
-                       (whizzy-observe-changes)
-                     (save-buffer)
-                     (whizzy-reslice))))))))))
+             (let* (;; type orig x y xmid ymid
+                    ;;  0    1   2 3  4    5
+                    (pieces (split-string name "@"))
+                    (newx (+ (string-to-number (nth 4 pieces))
+                             (- (string-to-number dx)
+                                (string-to-number (nth 2 pieces)))))
+                    (newy (+ (string-to-number (nth 5 pieces))
+                             (- (string-to-number dy)
+                                (string-to-number (nth 3 pieces)))))
+                    (replacement (format "%s,%s" newx newy))
+                    ;; (regexp (aget whizzy-tikz-node-regexp 'lax))
+                    (modified  (buffer-modified-p)))
+               (show pieces)
+               (show replacement)
+               (dolist (regexp (list
+                                ;; Check for an exact match
+                                (concat "\\b\\(" (nth 1 pieces) "\\)\\b")
+                                ;; Check for a fuzzy match
+                                (aget whizzy-tikz-node-regexp 'lax)))
+                 ;; We should probably check how many there are and act accordingly
+                 (when
+                     ;; We start at the end of the line
+                     (or (re-search-backward regexp (point-min) t)
+                         (re-search-forward regexp (point-max) t))
+
+                   ;; (show 'there)
+                   ;; (sit-for 5)
+                   (let (;(begin  (match-beginning 1))
+                         (edited
+                          ;;  (save-match-data
+                          ;;    (and (string= (match-string-no-properties 1) dx)
+                          ;;         (string= (match-string-no-properties 2) dy))))
+                          t)
+                         )
+                     ;; (show (match-string-no-properties 0))
+                     ;; (show (match-beginning 1))
+                     ;; (show (match-end 1))
+                     ;; (show (replace-match replacement t t nil 1))
+                     (replace-match replacement t t nil 1)
+                     ;; Have to figure out how to account for center vs lower left
+                     ;; (replace-match dx t t nil 1)
+                     ;; (replace-match dy t t nil 2) ;or the other order??? Can you do both?
+                     ;; (goto-char begin)
+                     ;; (message "%S=%S %S=%S" x dx y dy)
+                     ;; (setq edited (whizzy-edit-field x dx))
+                     ;; (setq edited (or (whizzy-edit-field y dy) edited))
+                     (unless (not edited)
+                       (if (or modified
+                               (equal (whizzy-get whizzy-active-buffer)
+                                      (current-buffer)))
+                           (whizzy-observe-changes)
+                         (save-buffer)
+                         (whizzy-reslice))))))))))))

File whizzy-tikz.sty

 \def\tikz@scan@no@calculator#1(#2){%
   % \gdef\my@node@def{#}
   \edef\tikz@temp{(#2)}%
-  \global\def\my@node@def{(#2)}%
-  \message{\my@node@def}
+  \global\def\my@node@def{#2}%
+  % \message{\my@node@def}
   \expandafter\tikz@@scan@@no@calculator\expandafter#1\tikz@temp%
 }