-
Notifications
You must be signed in to change notification settings - Fork 6
/
lazytab.el
79 lines (69 loc) · 2.44 KB
/
lazytab.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(require 'cdlatex)
(require 'org-table)
(defun lazytab-position-cursor-and-edit ()
;; (if (search-backward "\?" (- (point) 100) t)
;; (delete-char 1))
(cdlatex-position-cursor)
(lazytab-orgtbl-edit))
(defun lazytab-orgtbl-edit ()
(when (memq major-mode '(LaTeX-mode latex-mode))
(advice-add 'orgtbl-ctrl-c-ctrl-c :after #'lazytab-orgtbl-replace)
(orgtbl-mode 1)
(open-line 1)
(insert "\n|")))
(defun lazytab-orgtbl-replace (_)
(interactive "P")
(unless (org-at-table-p) (user-error "Not at a table"))
(let* ((table (org-table-to-lisp))
(params '(:backend latex :raw t))
(replacement-table
(if (texmathp)
(lazytab-orgtbl-to-amsmath table params)
(orgtbl-to-latex table params))))
(kill-region (org-table-begin) (org-table-end))
(open-line 1)
(push-mark)
(insert replacement-table)
(align-regexp (region-beginning) (region-end) "\\([:space:]*\\)& ")
(orgtbl-mode -1)
(advice-remove 'orgtbl-ctrl-c-ctrl-c #'lazytab-orgtbl-replace)))
(defun lazytab-orgtbl-to-amsmath (table params)
(orgtbl-to-generic
table
(org-combine-plists
'(:splice t
:lstart ""
:lend " \\\\"
:sep " & "
:hline nil
:llend "")
params)))
(defun lazytab-cdlatex-or-orgtbl-next-field ()
(when (and (bound-and-true-p orgtbl-mode)
(org-table-p)
(looking-at "[[:space:]]*\\(?:|\\|$\\)")
(let ((s (thing-at-point 'sexp)))
(not (and s (assoc s cdlatex-command-alist-comb)))))
(call-interactively #'org-table-next-field)
t))
;;;###autoload
(defun lazytab-org-table-next-field-maybe ()
(interactive)
(if (bound-and-true-p cdlatex-mode)
(cdlatex-tab)
(org-table-next-field)))
;;;###autoload
(define-minor-mode lazytab-mode
"Type in matrices, arrays and tables in LaTeX buffers with
orgtbl syntax."
:global nil
(if lazytab-mode
(progn (require 'org-table)
(define-key orgtbl-mode-map (kbd "<tab>") 'lazytab-org-table-next-field-maybe)
(define-key orgtbl-mode-map (kbd "TAB") 'lazytab-org-table-next-field-maybe)
(add-hook 'cdlatex-tab-hook 'lazytab-cdlatex-or-orgtbl-next-field))
(define-key orgtbl-mode-map (kbd "<tab>") 'org-table-next-field)
(define-key orgtbl-mode-map (kbd "TAB") 'org-table-next-field)
(remove-hook 'cdlatex-tab-hook 'lazytab-cdlatex-or-orgtbl-next-field)))
(provide 'lazytab)
;;; lazytab.el ends here