summaryrefslogtreecommitdiff
path: root/ksh-forms.el
blob: 33776b1d8514b9deb971a006f5930a131cc57b40 (plain)
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
;; -*- lexical-binding: t -*-

(require 'org)
(require 'subr-x)
(require 'cl-lib)

;; General settings
(setq doc-view-continuous t)
(setq lpr-switches (list "-o sides=two-sided-long-edge"
			 "-o fit-to-page"))
(setq export-properties
      (list :section-numbers nil
	    :with-toc nil))

;; Printing
(defun print-report ()
  (interactive)
  (shell-command
   (format "lpr -o sides=two-sided-long-edge -o fit-to-page %s" (buffer-file-name))))
(defalias 'print-buffer 'print-report)

;; Utilities
(defun set-values (record values)
  "Set record RECORD to values in alist VALUES"
  (seq-do (pcase-lambda (`(,field . ,value))
            (aset record field value))
          values))

;; Entry creation functions for form display
(defun form-entry (label-field)
  (pcase label-field
    (`(,label . ,field)
     (list (format "%s\n" label)
           field"\n\n"))))

(defun new-record-filter (record)
  (set-values record default-field-values)
  record)
(setq forms-new-record-filter 'new-record-filter)

;; Entry creation functions for org export
(defun ksh-forms-org-entry-function (format-string label-field)
  (pcase label-field
    (`(,label . ,field-no)
     (let ((value (get-field field-no)))
       (if value
           (princ (format format-string label
                          (replace-regexp-in-string "\n" "\n\n" value)))
         (user-error "Field \"%s\" should not be blank" label))))))

(defun get-field (field)
  "Parse form and return field FIELD from form"
  (cond ((integerp field) (nth (1- field) (forms--parse-form)))
	((stringp field) field)))

(defun single-line-org-entry (label-field)
  (ksh-forms-org-entry-function "*%s:* %s\n\n" label-field))

(defun multi-line-org-entry (label-field)
  (ksh-forms-org-entry-function "\n* %s\n%s\n\n" label-field))

(defun text-if-non-blank (text)
  (if (and text (not (string-blank-p text)))
      (princ (format "%s\n\n" text))))

(defun ksh-forms-org-keyword (keyword-value)
  (pcase keyword-value
    (`(,keyword . ,value)
     (princ (format "#+%s: %s\n" keyword value)))))

(defun ksh-forms-org-latex-header (header)
  (ksh-forms-org-keyword (cons "LATEX_HEADER" header)))

(defun forms-print ()
  (interactive)
  (org-open-file (org-to-pdf (funcall form-to-org))))

(setq org-latex-compiler "xelatex")

(defun org-to-pdf (org-source)
  (let ((tex-file-path (format "%s.tex" (make-temp-file "report"))))
    (with-temp-buffer
      (insert org-source)
      (org-export-to-file 'latex tex-file-path
	nil nil nil nil export-properties 'org-latex-compile))))

(make-variable-buffer-local 'form-to-org)

(provide 'ksh-forms)