-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuse-org-contrib.el
More file actions
380 lines (339 loc) · 17.1 KB
/
use-org-contrib.el
File metadata and controls
380 lines (339 loc) · 17.1 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
;;; use-org-contrib.el --- initialize org-contrib -*- lexical-binding: t; -*-
;;; Commentary:
;;; Code:
(require 'cl-lib) ; Necessary for cl-case, cl-incf, and cl-decf in Emacs 31
(add-to-list 'load-path "~/.emacs.d/org-contrib/lisp")
;; Ensure Org export libraries are loaded
(require 'ox-latex)
(require 'ox-beamer)
(require 'ox-odt)
(require 'ox-html)
(require 'ox-publish)
(require 'org-tempo)
(require 'org-id)
(require 'org-table)
(require 'ox-md)
(require 'ox-org)
(require 'ox-extra)
(ox-extras-activate '(ignore-headlines))
;; --- Tree-sitter Integration for Org-mode source blocks ---
;; Maps org src block language names to -ts-modes so C-c ' opens them
;; in the correct tree-sitter mode for folding and font-lock.
(with-eval-after-load 'org
(setq org-src-lang-modes
(append '(("r" . ess-r) ; ess-r-mode: treesit parser added below
("python" . python-ts)
("bash" . bash-ts)
("sh" . bash-ts)
("yaml" . yaml-ts))
org-src-lang-modes)))
;; --- Treesit parser + folding in org src edit buffers (C-c ') ---
;; When C-c ' opens a src block, org creates a temporary indirect buffer
;; in the relevant major mode. We hook into org-src-mode (which is always
;; active in these buffers) to create treesit parsers and enable folding
;; for languages that need manual parser setup (primarily R/ess-r-mode).
(defun vikas/org-src-setup-treesit-fold ()
"Create treesit parser and enable folding in org src edit buffers.
Covers ess-r-mode and any other mode that doesn't auto-create a parser."
(let ((lang (cond
((eq major-mode 'ess-r-mode) 'r)
((eq major-mode 'python-ts-mode) 'python)
((eq major-mode 'bash-ts-mode) 'bash)
((eq major-mode 'yaml-ts-mode) 'yaml)
((eq major-mode 'js-ts-mode) 'javascript)
((eq major-mode 'typescript-ts-mode) 'typescript)
((eq major-mode 'sql-mode) 'sql)
(t nil))))
(when (and lang
(treesit-language-available-p lang)
(null (treesit-parser-list)))
(treesit-parser-create lang))
;; Enable treesit-fold if a parser now exists
(when (and (treesit-parser-list)
(bound-and-true-p treesit-fold-mode))
(treesit-fold-mode -1)
(treesit-fold-mode 1))
(when (and (treesit-parser-list)
(not (bound-and-true-p treesit-fold-mode)))
(treesit-fold-mode 1))))
(with-eval-after-load 'org-src
(add-hook 'org-src-mode-hook #'vikas/org-src-setup-treesit-fold))
;; Core Org packages
(use-package org-change :ensure t :after org)
(use-package org-sticky-header :ensure t :after org)
;; LaTeX Export Settings
(setq org-latex-to-mathml-convert-command
"latexmlmath \"%i\" --presentationmathml=%o")
(setq org-latex-caption-above '(image table special-block))
(setq org-latex-create-formula-image-program 'dvisvgm)
;; TeX Live Path Configuration
;; Update the year here when you install a new TeX Live release.
(defvar vikas/texlive-year "2026"
"Installed TeX Live release year. Update when upgrading TeX Live.")
(defvar vikas/texlive-bin
(format "/usr/local/texlive/%s/bin/x86_64-linux" vikas/texlive-year)
"Path to the TeX Live bin directory.")
(setenv "PATH" (concat (getenv "PATH") ":" vikas/texlive-bin "/"))
(add-to-list 'exec-path vikas/texlive-bin)
(setq org-preview-latex-default-process 'dvisvgm)
(setq org-preview-latex-process-alist
`((dvipng :programs ("latex" "dvipng") :description "dvi > png"
:message "you need to install the programs: latex and dvipng."
:image-input-type "dvi" :image-output-type "png"
:image-size-adjust (1.0 . 1.0) :latex-compiler
(,(concat vikas/texlive-bin "/latex -interaction nonstopmode -output-directory %o %f"))
:image-converter ("dvipng -D %D -T tight -o %O %f")
:transparent-image-converter
("dvipng -D %D -T tight -bg Transparent -o %O %f"))
(dvisvgm :programs ("latex" "dvisvgm") :description "dvi > svg"
:message "you need to install the programs: latex and dvisvgm."
:image-input-type "dvi" :image-output-type "svg"
:image-size-adjust (1.7 . 1.5) :latex-compiler
(,(concat vikas/texlive-bin "/latex -interaction nonstopmode -output-directory %o %f"))
:image-converter
("dvisvgm %f --no-fonts --exact-bbox --scale=%S --output=%O"))
(imagemagick :programs ("latex" "convert") :description "pdf > png"
:message "you need to install the programs: latex and imagemagick."
:image-input-type "pdf" :image-output-type "png"
:image-size-adjust (1.0 . 1.0) :latex-compiler
(,(concat vikas/texlive-bin "/pdflatex -interaction nonstopmode -output-directory %o %f"))
:image-converter
("convert -density %D -trim -antialias %f -quality 100 %O"))))
;; LaTeX export via latexmk.
;; latexmk handles all reruns automatically: it detects whether biber/bibtex
;; is needed, reruns xelatex as many times as necessary, and stops when the
;; output is stable. No need to manually manage the compile sequence.
;;
;; -pdfxe use xelatex
;; -bibtex run biber/bibtex when needed, skip when not
;; -f force compilation even when errors occur
;; -xelatex=... pass -interaction=nonstopmode to xelatex so it doesn't
;; halt at errors — this is what produces a full PDF rather
;; than a partial one when there are non-fatal errors
;; -outdir=%o output to the directory org expects
(setq org-latex-pdf-process
(list (concat vikas/texlive-bin
"/latexmk -pdfxe -bibtex -f"
" -xelatex=\"xelatex -interaction=nonstopmode\""
" -outdir=%o %f")))
(setq org-format-latex-options (plist-put org-format-latex-options :scale 3.0))
;; --- Org LaTeX Classes ---
(with-eval-after-load 'ox-latex
(add-to-list 'org-latex-classes
'("vmemoir"
"\\documentclass[11pt]{memoir}"
("\\chapter{%s}" . "\\chapter*{%s}")
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")))
(add-to-list 'org-latex-classes
'("vreport"
"\\documentclass[11pt]{report}"
("\\chapter{%s}" . "\\chapter*{%s}")
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
(add-to-list 'org-latex-classes
'("varticle"
"\\documentclass[11pt,article,twoside,openany,strict,extrafontsizes]{memoir}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
(add-to-list 'org-latex-classes
'("vposter"
"\\documentclass[28pt,portrait, margin=0mm, innermargin=15mm, blockverticalspace=15mm, colspace=15mm, subcolspace=8mm]{tikzposter}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
(add-to-list 'org-latex-classes
'("vmemoirbook"
"\\documentclass[11pt]{memoir}"
("\\part{%s}" . "\\part*{%s}")
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
(add-to-list 'org-latex-classes
'("book" "\\documentclass[11pt,oneside]{book}"
("\\part{%s}" . "\\part*{%s}")
("\\chapter{%s}" . "\\chapter*{%s}")
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
(add-to-list 'org-latex-classes
'("beamer"
"\\documentclass[presentation]{beamer}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}"))))
;; Beamer Environments
(add-to-list 'org-beamer-environments-extra
'("onlyenv" "O" "\\begin{onlyenv}%a" "\\end{onlyenv}"))
(add-to-list 'org-beamer-environments-extra
'("textpos" "X" "\\begin{textblock}{10}(3,3) \\visible %a {" "} \\end{textblock}"))
(add-to-list 'org-beamer-environments-extra
'("textpos1" "w" "\\begin{textblock}{%h}(3,3) \\visible %a {" "} \\end{textblock}"))
;; --- Table Export Filters ---
(defun org-export-multicolumn-filter (row backend info)
(cond
((org-export-derived-backend-p backend 'latex)
(org-export-multicolumn-filter-latex row backend info))
((org-export-derived-backend-p backend 'html)
(org-export-multicolumn-filter-html row backend info))))
(defun org-export-multicolumn-filter-latex (row backend info)
(while (string-match
"\\(<\\([0-9]+\\)col\\([lrc]\\)?>[[:blank:]]*\\([^&]+\\)\\)" row)
(let ((columns (string-to-number (match-string 2 row)))
(start (match-end 0))
(contents (replace-regexp-in-string
"\\\\" "\\\\\\\\"
(replace-regexp-in-string "[[:blank:]]*$" ""
(match-string 4 row))))
(algn (or (match-string 3 row) "l")))
(setq row (replace-match
(format "\\\\multicolumn{%d}{%s}{%s}" columns algn contents)
nil nil row 1))
(while (and (> columns 1) (string-match "&" row start))
(setq row (replace-match "" nil nil row))
(cl-decf columns))))
row)
(defun org-export-tabularray-cmidrule-filter-latex (row backend info)
(while (string-match
"\\(<\\([0-9]+\\)cd\\([0-9]+\\)?>[[:blank:]]*\\([^&]+\\)\\)" row)
(let ((start (string-to-number (match-string 2 row)))
(end (or (match-string 3 row) "l")))
(setq row (replace-match
(format "\\\\cmidrule{%s-%s}" start end)
nil nil row 1))
(while (string-match "& \\| \\\\\\\\" row 0)
(setq row (replace-match "" nil nil row))
(cl-decf start))))
row)
(defun org-export-cmidrule-filter-latex (row backend info)
(while (string-match
"\\(<\\([0-9]+\\)cid\\([0-9]+\\)?>[[:blank:]]*\\([^&]+\\)\\)" row)
(let ((start (string-to-number (match-string 2 row)))
(end (or (match-string 3 row) "l")))
(setq row (replace-match
(format "\\\\cmidrule(lr){%s-%s}" start end)
nil nil row 1))
(while (string-match "& \\| \\\\\\\\" row 0)
(setq row (replace-match "" nil nil row))
(cl-decf start))))
row)
(defun org-export-toprule-filter-latex (row _backend _info)
(replace-regexp-in-string "\\(<tid>\\([[:blank:]]+\\&\\)+\\)[[:blank:]]\\\\\\\\.*" "\\\\toprule" row))
(defun org-export-midrule-filter-latex (row _backend _info)
(replace-regexp-in-string "\\(<mid>\\([[:blank:]]+\\&\\)+\\)[[:blank:]]\\\\\\\\.*" "\\\\midrule" row))
(defun org-export-bottomrule-filter-latex (row _backend _info)
(replace-regexp-in-string "\\(<bid>\\([[:blank:]]+\\&\\)+\\)[[:blank:]]\\\\\\\\.*" "\\\\bottomrule" row))
(defun org-export-multicolumn-filter-html (row _backend _info)
(while (string-match "class=\".*\" *><\\([0-9]+\\)col\\([lrc]\\)?>" row)
(let ((columns (string-to-number (match-string 1 row)))
(start (match-end 0))
(algn (cl-case (intern (or (match-string 2 row) "l"))
(c "center")
(r "right")
(l "left"))))
(setq row (replace-match
(format " class=\"%s\" colspan=\"%s\">" algn columns)
nil nil row))
(while (and (> columns 1)
(string-match "<th .*> </th>" row start))
(setq row (replace-match "" nil nil row))
(cl-decf columns))))
row)
(defun org-export-blanks-filter-latex (row backend _info)
(when (org-export-derived-backend-p backend 'latex)
(replace-regexp-in-string "nil" "\\\\mcone{---}" row)))
(defun org-export-blanks-filter-html (row backend _info)
(when (org-export-derived-backend-p backend 'html)
(replace-regexp-in-string "nil" "---" row)))
;; Register table filters
(add-to-list 'org-export-filter-table-row-functions 'org-export-multicolumn-filter)
(add-to-list 'org-export-filter-table-row-functions 'org-export-cmidrule-filter-latex)
(add-to-list 'org-export-filter-table-row-functions 'org-export-tabularray-cmidrule-filter-latex)
(add-to-list 'org-export-filter-table-row-functions 'org-export-toprule-filter-latex)
(add-to-list 'org-export-filter-table-row-functions 'org-export-midrule-filter-latex)
(add-to-list 'org-export-filter-table-row-functions 'org-export-bottomrule-filter-latex)
(add-to-list 'org-export-filter-table-row-functions 'org-export-blanks-filter-latex)
(add-to-list 'org-export-filter-table-row-functions 'org-export-blanks-filter-html)
;; Custom LaTeX Table Handler
(defun my/org-latex--org-table (table contents info)
(let* ((attr (org-export-read-attribute :attr_latex table))
(alignment (org-latex--align-string table info))
(opt (org-export-read-attribute :attr_latex table :options))
(table-env (or (plist-get attr :environment)
(plist-get info :latex-default-table-environment)))
(width
(let ((w (plist-get attr :width)))
(cond ((not w) "")
((member table-env '("tabular" "longtable")) "")
((member table-env '("tabu" "longtabu"))
(format (if (plist-get attr :spread) " spread %s " " to %s ") w))
(t (format "{%s}" w)))))
(caption (org-latex--caption/label-string table info))
(above? (org-latex--caption-above-p table info)))
(cond
((member table-env '("longtable" "longtabu"))
(let ((fontsize (let ((font (plist-get attr :font))) (and font (concat font "\n")))))
(concat (and fontsize (concat "{" fontsize))
(format "\\begin{%s}%s{%s}\n" table-env width alignment)
(and above? (org-string-nw-p caption) (concat caption "\\\\\n"))
contents
(and (not above?) (org-string-nw-p caption) (concat caption "\\\\\n"))
(format "\\end{%s}" table-env)
(and fontsize "}"))))
(t
(let ((output (format "\\begin{%s}%s%s{%s}\n%s\\end{%s}"
table-env (if opt opt "") width alignment contents table-env)))
(org-latex--decorate-table output attr caption above? info))))))
(advice-add 'org-latex--org-table :override #'my/org-latex--org-table)
;; Word count functionality
(defun org-word-count (beg end &optional count-latex-macro-args? count-footnotes?)
(interactive "r")
(unless mark-active (setf beg (point-min) end (point-max)))
(let ((wc 0)
(latex-macro-regexp "\\\\[A-Za-z]+\\(\\[[^]]*\\]\\|\\){\\([^}]*\\)}"))
(save-excursion
(goto-char beg)
(while (< (point) end)
(cond
((or (org-in-commented-line) (org-at-table-p)) nil)
((looking-at org-bracket-link-analytic-regexp)
(when (match-string-no-properties 5)
(let ((desc (match-string-no-properties 5)))
(save-match-data
(cl-incf wc (length (remove "" (org-split-string desc "\\W")))))))
(goto-char (match-end 0)))
((looking-at org-any-link-re) (goto-char (match-end 0)))
((org-in-regexps-block-p "^#\\+BEGIN_SRC\\W" "^#\\+END_SRC\\W") nil)
((save-excursion (backward-char) (looking-at org-babel-inline-src-block-regexp))
(goto-char (match-end 0)) (setf wc (+ 2 wc)))
((save-excursion (backward-char) (looking-at latex-macro-regexp))
(goto-char (if count-latex-macro-args? (match-beginning 2) (match-end 0)))
(setf wc (+ 2 wc)))
((and (not count-footnotes?) (or (org-footnote-at-definition-p) (org-footnote-at-reference-p))) nil)
(t
(let ((contexts (org-context)))
(cond
((or (assoc :todo-keyword contexts) (assoc :priority contexts)
(assoc :keyword contexts) (assoc :checkbox contexts)) nil)
((assoc :tags contexts)
(if (cl-intersection (org-get-tags-at) org-export-exclude-tags :test 'equal)
(org-forward-same-level 1)
nil))
(t (cl-incf wc))))))
(re-search-forward "\\w+\\W*")))
(message (format "%d words in %s." wc (if mark-active "region" "buffer")))))
(use-package stripe-buffer
:ensure t
:after org
:config
(add-hook 'org-mode-hook 'turn-on-stripe-table-mode))
(setq org-plantuml-jar-path "/usr/share/java/plantuml/plantuml.jar")
(org-babel-do-load-languages
'org-babel-load-languages
'((plantuml . t)))
(provide 'use-org-contrib)
;;; use-org-contrib.el ends here