[pgf-tikz] shift matrices in emacs

Tom Hirschowitz tom.hirschowitz at univ-smb.fr
Wed Jan 15 07:54:49 CET 2020


Hi all,

When typing tikz code in emacs, I have a few macros to insert square or triangle templates 
using node names in m-1-1, m-2-2, etc. So it is often quicker to shift entire parts of 
matrices than to retype the whole. I did this manually for a while, but finally wrote the 
code below which does it automatically and defines M-[direction] key bindings.

That's my first not entirely trivial elisp code, and I wasn't even able to factor out the 
obvious, but it seems to work. Can anyone more used to elisp improve the code?

Also, apart from sending it here, is there anything I should do to share it?

Tom



(defun tikz-shift-downwards (beginning end)
  "Shifts tikz matrix coordinates down by 1."
  (interactive "*r")
  (setq end (copy-marker end))
  (save-match-data
    (save-excursion
      (goto-char beginning)
      (while (re-search-forward "-\\([0-9]+\\)-" end t)
        (replace-match (number-to-string (+ 1 (string-to-number (match-string 1)))) nil nil nil 1))
      (set-marker end nil))))
(global-set-key (kbd "M-<down>") 'tikz-shift-downwards)

(defun tikz-shift-upwards (beginning end)
  "Shifts tikz matrix coordinates down by 1."
  (interactive "*r")
  (setq end (copy-marker end))
  (save-match-data
    (save-excursion
      (goto-char beginning)
      (while (re-search-forward "-\\([0-9]+\\)-" end t)
        (replace-match (number-to-string (- (string-to-number (match-string 1)) 1)) nil nil nil 1)) 
      (set-marker end nil))))
(global-set-key (kbd "M-<up>") 'tikz-shift-upwards)

(defun tikz-shift-right (beginning end)
  "Shifts tikz matrix coordinates down by 1."
  (interactive "*r")
  (setq end (copy-marker end))
  (save-match-data
    (save-excursion
      (goto-char beginning)
      (while (re-search-forward "-\\([0-9]+\\))" end t)
        (replace-match (number-to-string (+ 1 (string-to-number (match-string 1)))) nil nil nil 1)) 
      (set-marker end nil))))
(global-set-key (kbd "M-<right>") 'tikz-shift-right)

(defun tikz-shift-left (beginning end)
  "Shifts tikz matrix coordinates down by 1."
  (interactive "*r")
  (setq end (copy-marker end))
  (save-match-data
    (save-excursion
      (goto-char beginning)
      (while (re-search-forward "-\\([0-9]+\\))" end t)
        (replace-match (number-to-string (- (string-to-number (match-string 1)) 1)) nil nil nil 1))
      (set-marker end nil))))
(global-set-key (kbd "M-<left>") 'tikz-shift-left)


More information about the pgf-tikz mailing list