[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [AUCTeX] Indent custom env like itemize/enumerate environment

From: Arash Esbati
Subject: Re: [AUCTeX] Indent custom env like itemize/enumerate environment
Date: Sun, 11 Jun 2017 12:01:22 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2

Thorsten Grothe <address@hidden> writes:

> thank you very much for doing my job :-)

You're welcome :-)

> I will test your code and report anny issues to you, this can take a
> couple of days because I have a lot of work at the moment...

Thanks, but please test the code below.  There was a `concat' missing in
`LaTeX-moodle-question-env-with-args'.  I also modified
`LaTeX-moodle-item-argument' in order to offer an asterisk in `multi'

--8<---------------cut here---------------start------------->8---
;;; Code:

(defvar LaTeX-moodle-key-val-options
    ("default grade")
  "Key=value options for moodle macros and environments.")

(defun LaTeX-moodle-question-env-with-args (env)
  "Insert ENV provided by moodle.sty incl. arguments and first \\item."
   (let ((opts (TeX-read-key-val
                (cond (;; 3.3.1 Multiple Choice
                       (string= env "multi")
                       (append '(("shuffle"   ("true" "false"))
                                 ("numbering" ("alph" "Alph" "arabic"
                                               "roman" "Roman" "none"))
                                 ("single"    ("true" "false"))
                                 ("multiple"  ("true" "false")))
                      ;; 3.3.3 Short Answer
                      ((string= env "shortanswer")
                       (append '(("case sensitive" ("true" "false"))
                                 ("usecase"        ("true" "false")))
                      ;; 3.3.4 Essay Questions
                      ((string= env "essay")
                       (append '(("response required" ("true" "false"))
                                 ("response format"   ("html" "file"
                                                       "text" "monospaced"))
                                 ("response field lines")
                                 ("attachments allowed"  ("0" "1" "2" "3"
                                 ("attachments required" ("0" "1" "2" "3"))
                                 ("response template"))
                      ;; 3.4 Matching Questions
                      ((string= env "matching")
                       (append '(("shuffle"       ("true" "false"))
                                 ("drag and drop" ("true" "false"))
                                 ("dd"            ("true" "false")))
                      (t LaTeX-moodle-key-val-options))))
         (qname (TeX-read-string (TeX-argument-prompt nil nil "Question 
      (when (and opts (not (string= opts "")))
        (format "[%s]" opts))
      (format "{%s}" qname))))
  (if (TeX-active-mark)
        (end-of-line 1))
    (end-of-line 0))
  (delete-char 1)
  (when (looking-at (concat "^[ \t]+$\\|"
                            "^[ \t]*" TeX-comment-start-regexp "+[ \t]*$"))
    (delete-region (point) (line-end-position)))
  ;; Deactivate the mark here in order to prevent `TeX-parse-macro'
  ;; from swapping point and mark and the \item ending up right after
  ;; \begin{...}.
  ;; Query and insert the question text.
  (let ((qtext (TeX-read-string (TeX-argument-prompt nil nil "Question Text"))))
    (when (and qtext (not (string= qtext "")))
      (insert qtext)
  ;; The inserted \item may have outdented the first line to the
  ;; right.  Fill it, if appropriate.
  (when (and (not (looking-at "$"))
             (not (assoc env LaTeX-indent-environment-list))
             (> (- (line-end-position) (line-beginning-position))
    (LaTeX-fill-paragraph nil)))

(defun LaTeX-moodle-item-argument ()
  "Insert an \\item with optional argument in environments of moodle package."
  (let ((TeX-insert-macro-default-style 'mandatory-args-only))
    (TeX-insert-macro "item"))
  ;; Add * to `LaTeX-moodle-key-val-options' in multi environment:
  (let ((opts
         (TeX-read-key-val t (if (string= "multi" (LaTeX-current-environment))
                                 (append '(("*")) LaTeX-moodle-key-val-options)
    (when (and opts (not (string= opts "")))
      (if (string= opts "*")
          (insert opts)
        (insert LaTeX-optop opts LaTeX-optcl))))
  (when (string= "matching" (LaTeX-current-environment))
      (insert TeX-esc "answer")

 (lambda ()

    ;; 3.2 Quiz and Question Environments
      (lambda (environment)
         (let ((opts (TeX-read-key-val t LaTeX-moodle-key-val-options))
               (bank (TeX-read-string (TeX-argument-prompt nil nil "Question 
            (when (and opts (not (string= opts "")))
              (format "[%s]" opts))
            (format "{%s}" bank)))))))

    '("moodleset" (TeX-arg-key-val LaTeX-moodle-key-val-options)))

   ;; Make environments available to AUCTeX:
   (dolist (env '("multi" "numerical" "shortanswer" "essay" "matching"))
     (LaTeX-add-environments `(,env LaTeX-moodle-question-env-with-args))
     (add-to-list 'LaTeX-item-list `(,env . LaTeX-moodle-item-argument) t))

   ;; Fontification
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("moodleset" "{"))
     (font-latex-add-keywords '(("answer" "")
                                ;; Cater for a fontified starred \item
                                ("item"   "*["))

(defvar LaTeX-moodle-package-options
  "Package options for the moodle package.")

;;; moodle.el ends here
--8<---------------cut here---------------end--------------->8---

Best, Arash

reply via email to

[Prev in Thread] Current Thread [Next in Thread]