branch: externals/mathsheet
commit da128406fca3eeb66358d479082cd78f2fdeabb4
Author: Ian Martins <[email protected]>
Commit: Ian Martins <[email protected]>

    Open new worksheet after writing it
---
 README.md     | 47 +++++++++++++++++++++++++++++++++++++++++------
 mathsheet.el  | 27 +++++++++++++++++++++------
 mathsheet.org | 46 ++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 102 insertions(+), 18 deletions(-)

diff --git a/README.md b/README.md
index 258ad958ff..cd5d73c08e 100644
--- a/README.md
+++ b/README.md
@@ -109,6 +109,10 @@ Mathsheet allows for the following customizations:
 -   **`mathsheet-output-directory`:** This is where worksheets should be
     written. It defaults to your home directory. You'll probably want to
     move it somewhere else.
+-   **`dired-guess-shell-alist-user`:** This is an existing variable that
+    comes with dired which can be used to configure which programs are
+    associated with various file types. When mathsheet tries to open the
+    new worksheet, this will determine which program is used.
 
 
 ### Problem Templates
@@ -214,7 +218,7 @@ format.
     ;; Author: Ian Martins <[email protected]>
     ;; Keywords: tools, education, math
     ;; Homepage: https://gitlab.com/ianxm/mathsheet
-    ;; Version: 1.2
+    ;; Version: 1.3
     ;; Package-Requires: ((peg "1.0")
     ;;                    (emacs "28.1"))
     
@@ -253,6 +257,8 @@ included in Emacs but we need to make sure they have been 
loaded.
     
     (declare-function math-read-expr "calc-ext")
     (declare-function calc-set-language "calc-lang")
+    (declare-function dired-do-shell-command "dired-aux")
+    (declare-function dired-guess-shell-command "dired-aux")
 
 
 ### Variables
@@ -322,6 +328,10 @@ scope where the macro is called.
     (defvar mathsheet--field-problems nil
       "The form record problems field.")
 
+In addition to these variables, we also use
+`dired-guess-shell-alist-user` to determine which program to use to open
+the generated PDF file.
+
 
 ## UI Form
 
@@ -533,15 +543,17 @@ This function is used to parse each problem row.
               ;; absolute path without extension
               (fname (concat
                       (file-name-as-directory mathsheet-output-directory)
-                      (string-replace " " "-" (alist-get :name config)))))
+                      (string-replace " " "-" (alist-get :name config))
+                      ".pdf")))
           (mathsheet--write-worksheet
            fname
            (alist-get :instr config)
            problems
            (alist-get :cols config))
-          (message "Wrote %s problems to %s.pdf"
+          (message "Wrote %s problems to %s"
                    (alist-get :count config)
-                   fname))))
+                   fname)
+          (mathsheet--open-worksheet fname))))
 
 
 ## Problem generation
@@ -985,7 +997,7 @@ of how each section is filled in is described below.
         (let* ((default-directory mathsheet-output-directory)
                (ret (shell-command-on-region
                      (point-min) (point-max)
-                     (format "groff -mm -e -Tpdf - > %s" (concat fname 
".pdf")))))
+                     (format "groff -mm -e -Tpdf - > %s" fname))))
           (unless (eq ret 0)
             (error "PDF generation failed")))))
 
@@ -1043,7 +1055,7 @@ fill-problems:
          (unless (= index 0)
            (insert ".NCOL\n"))
          (dolist (row group)
-           (message "convert to eqn %s -> %s" (car row) 
(mathsheet--convert-to-eqn (car row)))
+           ;; (message "convert to eqn %s -> %s" (car row) 
(mathsheet--convert-to-eqn (car row)))
            (insert (format (if (nth 3 row)
                                ".LI\n.EQ\n%s\n.EN\n.SP \\n[vs]p\n"
                              ".LI\n.EQ\n%s =\n.EN\n\\l'5\\_'\n.SP \\n[vs]p\n")
@@ -1070,6 +1082,29 @@ fill-answers:
                  (if (< index (length problems)) "\n" "")))))
 
 
+### Open new worksheet
+
+This opens the worksheet PDF file after it is written. This makes it
+easy to review and print.
+
+This uses `dired-do-shell-command` to open the file, and
+`dired-guess-shell-command` to choose the program to use to open the
+file. The file should be a PDF so the program should be a PDF
+viewer. This can be configured for the local system using the variable
+`dired-guess-shell-alist-user`.
+
+    (defun mathsheet--open-worksheet (fname)
+      "Open the worksheet FNAME.
+    
+    FNAME is the file to open, probably a worksheet."
+      (dired-do-shell-command
+       (dired-guess-shell-command
+        (format "Open %s with " fname)
+        (list fname))
+       nil
+       (list fname)))
+
+
 ## Convenience functions
 
 
diff --git a/mathsheet.el b/mathsheet.el
index 3584995424..782b0ee265 100644
--- a/mathsheet.el
+++ b/mathsheet.el
@@ -5,7 +5,7 @@
 ;; Author: Ian Martins <[email protected]>
 ;; Keywords: tools, education, math
 ;; Homepage: https://gitlab.com/ianxm/mathsheet
-;; Version: 1.2
+;; Version: 1.3
 ;; Package-Requires: ((peg "1.0")
 ;;                    (emacs "28.1"))
 
@@ -38,6 +38,8 @@
 
 (declare-function math-read-expr "calc-ext")
 (declare-function calc-set-language "calc-lang")
+(declare-function dired-do-shell-command "dired-aux")
+(declare-function dired-guess-shell-command "dired-aux")
 
 (defgroup mathsheet nil
   "Options for customizing Mathsheet."
@@ -268,15 +270,17 @@ which validation checks to perform."
           ;; absolute path without extension
           (fname (concat
                   (file-name-as-directory mathsheet-output-directory)
-                  (string-replace " " "-" (alist-get :name config)))))
+                  (string-replace " " "-" (alist-get :name config))
+                  ".pdf")))
       (mathsheet--write-worksheet
        fname
        (alist-get :instr config)
        problems
        (alist-get :cols config))
-      (message "Wrote %s problems to %s.pdf"
+      (message "Wrote %s problems to %s"
                (alist-get :count config)
-               fname))))
+               fname)
+      (mathsheet--open-worksheet fname))))
 
 (defun mathsheet--scan-problem ()
   "Scan a problem.
@@ -566,7 +570,7 @@ ordered."
            (unless (= index 0)
              (insert ".NCOL\n"))
            (dolist (row group)
-             (message "convert to eqn %s -> %s" (car row) 
(mathsheet--convert-to-eqn (car row)))
+             ;; (message "convert to eqn %s -> %s" (car row) 
(mathsheet--convert-to-eqn (car row)))
              (insert (format (if (nth 3 row)
                                  ".LI\n.EQ\n%s\n.EN\n.SP \\n[vs]p\n"
                                ".LI\n.EQ\n%s =\n.EN\n\\l'5\\_'\n.SP 
\\n[vs]p\n")
@@ -594,10 +598,21 @@ ordered."
     (let* ((default-directory mathsheet-output-directory)
            (ret (shell-command-on-region
                  (point-min) (point-max)
-                 (format "groff -mm -e -Tpdf - > %s" (concat fname ".pdf")))))
+                 (format "groff -mm -e -Tpdf - > %s" fname))))
       (unless (eq ret 0)
         (error "PDF generation failed")))))
 
+(defun mathsheet--open-worksheet (fname)
+  "Open the worksheet FNAME.
+
+FNAME is the file to open, probably a worksheet."
+  (dired-do-shell-command
+   (dired-guess-shell-command
+    (format "Open %s with " fname)
+    (list fname))
+   nil
+   (list fname)))
+
 (when (null forms-mode-map)
   (add-to-list
    'forms-mode-hook
diff --git a/mathsheet.org b/mathsheet.org
index b971a777df..c834a4cdf5 100644
--- a/mathsheet.org
+++ b/mathsheet.org
@@ -89,6 +89,10 @@ Mathsheet allows for the following customizations:
 - ~mathsheet-output-directory~ :: This is where worksheets should be
   written. It defaults to your home directory. You'll probably want to
   move it somewhere else.
+- ~dired-guess-shell-alist-user~ :: This is an existing variable that
+  comes with dired which can be used to configure which programs are
+  associated with various file types. When mathsheet tries to open the
+  new worksheet, this will determine which program is used.
 *** Problem Templates
 The worksheet is made of a set of math problems. Each problem is
 defined by a template that lays out an equation or expression and
@@ -204,7 +208,7 @@ format.
   ;; Author: Ian Martins <[email protected]>
   ;; Keywords: tools, education, math
   ;; Homepage: https://gitlab.com/ianxm/mathsheet
-  ;; Version: 1.2
+  ;; Version: 1.3
   ;; Package-Requires: ((peg "1.0")
   ;;                    (emacs "28.1"))
 
@@ -230,6 +234,8 @@ included in Emacs but we need to make sure they have been 
loaded.
 
   (declare-function math-read-expr "calc-ext")
   (declare-function calc-set-language "calc-lang")
+  (declare-function dired-do-shell-command "dired-aux")
+  (declare-function dired-guess-shell-command "dired-aux")
 #+end_src
 
 *** Variables
@@ -304,6 +310,10 @@ scope where the macro is called.
 
 #+end_src
 
+In addition to these variables, we also use
+~dired-guess-shell-alist-user~ to determine which program to use to open
+the generated PDF file.
+
 ** UI Form
 *** Form configuration
 See details 
[[https://www.gnu.org/software/emacs/manual/html_mono/forms.html][here]].
@@ -506,15 +516,17 @@ This function is used to parse each problem row.
             ;; absolute path without extension
             (fname (concat
                     (file-name-as-directory mathsheet-output-directory)
-                    (string-replace " " "-" (alist-get :name config)))))
+                    (string-replace " " "-" (alist-get :name config))
+                    ".pdf")))
         (mathsheet--write-worksheet
          fname
          (alist-get :instr config)
          problems
          (alist-get :cols config))
-        (message "Wrote %s problems to %s.pdf"
+        (message "Wrote %s problems to %s"
                  (alist-get :count config)
-                 fname))))
+                 fname)
+        (mathsheet--open-worksheet fname))))
 #+end_src
 ** Problem generation
 *** Scan problem
@@ -1036,7 +1048,7 @@ of how each section is filled in is described below.
       (let* ((default-directory mathsheet-output-directory)
              (ret (shell-command-on-region
                    (point-min) (point-max)
-                   (format "groff -mm -e -Tpdf - > %s" (concat fname 
".pdf")))))
+                   (format "groff -mm -e -Tpdf - > %s" fname))))
         (unless (eq ret 0)
           (error "PDF generation failed")))))
 #+end_src
@@ -1100,7 +1112,7 @@ fill-problems:
        (unless (= index 0)
          (insert ".NCOL\n"))
        (dolist (row group)
-         (message "convert to eqn %s -> %s" (car row) 
(mathsheet--convert-to-eqn (car row)))
+         ;; (message "convert to eqn %s -> %s" (car row) 
(mathsheet--convert-to-eqn (car row)))
          (insert (format (if (nth 3 row)
                              ".LI\n.EQ\n%s\n.EN\n.SP \\n[vs]p\n"
                            ".LI\n.EQ\n%s =\n.EN\n\\l'5\\_'\n.SP \\n[vs]p\n")
@@ -1129,6 +1141,28 @@ fill-answers:
                (if (< index (length problems)) "\n" "")))))
 #+end_src
 
+*** Open new worksheet
+This opens the worksheet PDF file after it is written. This makes it
+easy to review and print.
+
+This uses ~dired-do-shell-command~ to open the file, and
+~dired-guess-shell-command~ to choose the program to use to open the
+file. The file should be a PDF so the program should be a PDF
+viewer. This can be configured for the local system using the variable
+~dired-guess-shell-alist-user~.
+
+#+begin_src elisp :results silent :noweb tangle :tangle mathsheet.el
+  (defun mathsheet--open-worksheet (fname)
+    "Open the worksheet FNAME.
+
+  FNAME is the file to open, probably a worksheet."
+    (dired-do-shell-command
+     (dired-guess-shell-command
+      (format "Open %s with " fname)
+      (list fname))
+     nil
+     (list fname)))
+#+end_src
 ** Convenience functions
 *** Add key binding to form
 This adds the keybinding to run the mathsheet generator from the

Reply via email to