From c986de298d9ae631c5c70bc66aa4c327f38e6c88 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Thu, 31 Dec 2020 13:20:36 +0530 Subject: Provide ksh-forms feature. * ksh-forms.el: Provide ksh-forms. * ksh-discharge.el, ksh-scan2.el, ksh-scan3.el: Require ksh-forms. --- ksh-discharge.el | 2 +- ksh-forms-improved.el | 87 ------------------------------------------------- ksh-forms.el | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ ksh-scan2.el | 3 +- ksh-scan3.el | 3 +- 5 files changed, 94 insertions(+), 90 deletions(-) delete mode 100644 ksh-forms-improved.el create mode 100644 ksh-forms.el diff --git a/ksh-discharge.el b/ksh-discharge.el index 4aa382d..80c382b 100644 --- a/ksh-discharge.el +++ b/ksh-discharge.el @@ -1,6 +1,6 @@ ;; -*- lexical-binding: t -*- -(load "ksh-forms-improved.el") +(require 'ksh-forms) ;; Datafile path and fields (setq forms-file (expand-file-name "discharge.dat" ksh-data-path)) diff --git a/ksh-forms-improved.el b/ksh-forms-improved.el deleted file mode 100644 index 83fa0e0..0000000 --- a/ksh-forms-improved.el +++ /dev/null @@ -1,87 +0,0 @@ -;; -*- 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) - (find-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) diff --git a/ksh-forms.el b/ksh-forms.el new file mode 100644 index 0000000..da6d047 --- /dev/null +++ b/ksh-forms.el @@ -0,0 +1,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) + (find-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) diff --git a/ksh-scan2.el b/ksh-scan2.el index fae934d..0ea63dd 100644 --- a/ksh-scan2.el +++ b/ksh-scan2.el @@ -1,6 +1,7 @@ ;; -*- lexical-binding: t -*- -(load "ksh-forms-improved.el") +(require 'ksh-forms) + (load "ksh-report-header.el") (load "ksh-biometry.el") diff --git a/ksh-scan3.el b/ksh-scan3.el index ac50f36..cb8947e 100644 --- a/ksh-scan3.el +++ b/ksh-scan3.el @@ -1,6 +1,7 @@ ;; -*- lexical-binding: t -*- -(load "ksh-forms-improved.el") +(require 'ksh-forms) + (load "ksh-report-header.el") (load "ksh-biometry.el") -- cgit v1.2.3