Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
14 KiB

4 years ago
  1. ;;; ess-jags-d.el --- ESS[JAGS] dialect -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2008-2011 Rodney Sparapani
  3. ;; Author: Rodney Sparapani
  4. ;; Created: 13 March 2008
  5. ;; Maintainer: ESS-help <ess-help@r-project.org>
  6. ;; This file is part of ESS
  7. ;; This file is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 2, or (at your option)
  10. ;; any later version.
  11. ;;
  12. ;; This file is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;;
  17. ;; A copy of the GNU General Public License is available at
  18. ;; https://www.r-project.org/Licenses/
  19. ;;; Code:
  20. (require 'ess-bugs-d)
  21. (require 'ess-utils)
  22. (require 'ess-inf)
  23. (require 'ess-mode)
  24. (defvar ess-jags-command "jags" "Default JAGS program in PATH.")
  25. (make-local-variable 'ess-jags-command)
  26. (defvar ess-jags-monitor '("") "Default list of variables to monitor.")
  27. (make-local-variable 'ess-jags-monitor)
  28. (defvar ess-jags-thin 1 "Default thinning parameter.")
  29. (make-local-variable 'ess-jags-thin)
  30. (defvar ess-jags-chains 1 "Default number of chains.")
  31. (make-local-variable 'ess-jags-chains)
  32. (defvar ess-jags-burnin 10000 "Default burn-in.")
  33. (make-local-variable 'ess-jags-burnin)
  34. (defvar ess-jags-update 10000 "Default number of updates after burnin.")
  35. (make-local-variable 'ess-jags-update)
  36. (defvar ess-jags-system t "Default whether JAGS recognizes the system command.")
  37. (defvar ess-jags-font-lock-keywords
  38. (list
  39. ;; .jag files
  40. (cons "#.*\n" font-lock-comment-face)
  41. (cons "^[ \t]*\\(model\\|var\\)\\>"
  42. font-lock-keyword-face)
  43. (cons (concat "\\<d\\(bern\\|beta\\|bin\\|cat\\|chisq\\|"
  44. "dexp\\|dirch\\|exp\\|\\(gen[.]\\)?gamma\\|hyper\\|"
  45. "interval\\|lnorm\\|logis\\|mnorm\\|mt\\|multi\\|"
  46. "negbin\\|norm\\(mix\\)?\\|par\\|pois\\|sum\\|t\\|"
  47. "unif\\|weib\\|wish\\)[ \t\n]*(")
  48. font-lock-constant-face)
  49. (cons (concat "\\<\\(abs\\|cos\\|dim\\|\\(i\\)?cloglog\\|equals\\|"
  50. "exp\\|for\\|inprod\\|interp[.]line\\|inverse\\|length\\|"
  51. "\\(i\\)?logit\\|logdet\\|logfact\\|loggam\\|max\\|mean\\|"
  52. "mexp\\|min\\|phi\\|pow\\|probit\\|prod\\|rank\\|round\\|"
  53. "sd\\|sin\\|sort\\|sqrt\\|step\\|sum\\|t\\|trunc\\|T\\)[ \t\n]*(")
  54. font-lock-function-name-face)
  55. ;; .jmd files
  56. (cons (concat "\\<\\(adapt\\|cd\\|clear\\|coda\\|data\\|dir\\|"
  57. "exit\\|in\\(itialize\\)?\\|load\\|model\\|monitors\\|parameters\\|"
  58. "pwd\\|run\\|s\\(amplers\\|ystem\\)\\|to\\|update\\)[ \t\n]")
  59. font-lock-keyword-face)
  60. (cons "\\<\\(compile\\|monitor\\)[, \t\n]"
  61. font-lock-keyword-face)
  62. (cons "[, \t\n]\\(by\\|chain\\|nchains\\|stem\\|thin\\|type\\)[ \t\n]*("
  63. font-lock-function-name-face)
  64. )
  65. "ESS[JAGS]: Font lock keywords."
  66. )
  67. (defun ess-jags-switch-to-suffix (suffix &optional jags-chains jags-monitor jags-thin
  68. jags-burnin jags-update)
  69. "ESS[JAGS]: Switch to file with suffix."
  70. (find-file (concat ess-bugs-file-dir ess-bugs-file-root suffix))
  71. (if (equal 0 (buffer-size)) (progn
  72. (if (equal ".jag" suffix) (progn
  73. (insert "var ;\n")
  74. (insert "model {\n")
  75. (insert " for (i in 1:N) {\n \n")
  76. (insert " }\n")
  77. (insert "}\n")
  78. (insert "#Local Variables" ":\n")
  79. (insert "#ess-jags-chains:1\n")
  80. (insert "#ess-jags-monitor:(\"\")\n")
  81. (insert "#ess-jags-thin:1\n")
  82. (insert "#ess-jags-burnin:10000\n")
  83. (insert "#ess-jags-update:10000\n")
  84. (insert "#End:\n")
  85. ))
  86. (if (equal ".jmd" suffix) (let
  87. ((ess-jags-temp-chains "") (ess-jags-temp-monitor "") (ess-jags-temp-chain ""))
  88. (if jags-chains (setq ess-jags-chains jags-chains))
  89. (if jags-monitor (setq ess-jags-monitor jags-monitor))
  90. (if jags-thin (setq ess-jags-thin jags-thin))
  91. (setq ess-jags-temp-chains
  92. (concat "compile, nchains(" (format "%d" ess-jags-chains) ")\n"))
  93. (setq jags-chains ess-jags-chains)
  94. (while (< 0 jags-chains)
  95. (setq ess-jags-temp-chains
  96. (concat ess-jags-temp-chains
  97. "parameters ## \"" ess-bugs-file-root
  98. ".##" (format "%d" jags-chains) "\", chain("
  99. (format "%d" jags-chains) ")\n"))
  100. (setq jags-chains (- jags-chains 1)))
  101. (setq ess-jags-temp-monitor "")
  102. (while (and (listp ess-jags-monitor) (consp ess-jags-monitor))
  103. (if (not (string-equal "" (car ess-jags-monitor)))
  104. (setq ess-jags-temp-monitor
  105. (concat ess-jags-temp-monitor "monitor "
  106. (car ess-jags-monitor) ", thin(" (format "%d" ess-jags-thin) ")\n")))
  107. (setq ess-jags-monitor (cdr ess-jags-monitor)))
  108. (insert "model in \"" ess-bugs-file-root ".jag\"\n")
  109. (insert "data in \"" ess-bugs-file-root ".jdt\"\n")
  110. (insert (ess-replace-in-string ess-jags-temp-chains "##" "in"))
  111. (insert "initialize\n")
  112. ;(insert "update " (format "%d" (* jags-thin jags-burnin)) "\n")
  113. (insert "update " (format "%d" jags-burnin) "\n")
  114. (insert ess-jags-temp-monitor)
  115. (insert "update " (format "%d" (* jags-thin jags-update)) "\n")
  116. (insert (ess-replace-in-string
  117. (ess-replace-in-string ess-jags-temp-chains
  118. "compile, nchains([0-9]+)" "#") "##" "to"))
  119. (insert "coda "
  120. ;(if ess-microsoft-p (if (w32-shell-dos-semantics) "*" "\\*") "\\*")
  121. "*, stem(\"" ess-bugs-file-root "\")\n")
  122. (if ess-jags-system (progn
  123. (insert "system rm -f " ess-bugs-file-root ".ind\n")
  124. (insert "system ln -s " ess-bugs-file-root "index.txt " ess-bugs-file-root ".ind\n")
  125. (setq jags-chains ess-jags-chains)
  126. (while (< 0 jags-chains)
  127. (setq ess-jags-temp-chain (format "%d" jags-chains))
  128. ;.txt not recognized by BOA and impractical to over-ride
  129. (insert "system rm -f " ess-bugs-file-root ess-jags-temp-chain ".out\n")
  130. (insert "system ln -s " ess-bugs-file-root "chain" ess-jags-temp-chain ".txt "
  131. ess-bugs-file-root ess-jags-temp-chain ".out\n")
  132. (setq jags-chains (- jags-chains 1)))))
  133. (insert "exit\n")
  134. (insert "Local Variables" ":\n")
  135. (insert "ess-jags-chains:" (format "%d" ess-jags-chains) "\n")
  136. (insert "ess-jags-command:\"jags\"\n")
  137. (insert "End:\n")
  138. ))
  139. ))
  140. )
  141. (defun ess-jags-na-jmd (jags-command)
  142. "ESS[JAGS]: Perform the Next-Action for .jmd."
  143. ;(ess-save-and-set-local-variables)
  144. (if (equal 0 (buffer-size)) (ess-jags-switch-to-suffix ".jmd")
  145. ;else
  146. (shell)
  147. (ess-sleep)
  148. (if (when (fboundp 'w32-shell-dos-semantics)
  149. (w32-shell-dos-semantics))
  150. (if (string-equal ":" (substring ess-bugs-file 1 2))
  151. (progn
  152. (insert (substring ess-bugs-file 0 2))
  153. (comint-send-input)
  154. )
  155. )
  156. )
  157. (insert "cd \"" ess-bugs-file-dir "\"")
  158. (comint-send-input)
  159. ; (let ((ess-jags-temp-chains ""))
  160. ;
  161. ; (while (< 0 jags-chains)
  162. ; (setq ess-jags-temp-chains
  163. ; (concat (format "%d " jags-chains) ess-jags-temp-chains))
  164. ; (setq jags-chains (- jags-chains 1)))
  165. (insert ess-bugs-batch-pre-command " " jags-command " "
  166. ess-bugs-file-root ".jmd "
  167. (if (or (equal shell-file-name "/bin/csh")
  168. (equal shell-file-name "/bin/tcsh")
  169. (equal shell-file-name "/bin/zsh")
  170. (equal shell-file-name "/bin/bash"))
  171. (concat ">& " ess-bugs-file-root ".jog ")
  172. ;else
  173. "> " ess-bugs-file-root ".jog 2>&1 ")
  174. ; ;.txt not recognized by BOA and impractical to over-ride
  175. ; "&& (rm -f " ess-bugs-file-root ".ind; "
  176. ; "ln -s " ess-bugs-file-root "index.txt " ess-bugs-file-root ".ind; "
  177. ; "for i in " ess-jags-temp-chains "; do; "
  178. ; "rm -f " ess-bugs-file-root "$i.out; "
  179. ; "ln -s " ess-bugs-file-root "chain$i.txt " ess-bugs-file-root "$i.out; done) "
  180. ess-bugs-batch-post-command)
  181. (comint-send-input)
  182. ))
  183. (defun ess-jags-na-bug ()
  184. "ESS[JAGS]: Perform Next-Action for .jag"
  185. (if (equal 0 (buffer-size)) (ess-jags-switch-to-suffix ".jag")
  186. ;else
  187. (ess-save-and-set-local-variables)
  188. (ess-jags-switch-to-suffix ".jmd"
  189. ess-jags-chains ess-jags-monitor ess-jags-thin ess-jags-burnin ess-jags-update))
  190. )
  191. ;;;###autoload
  192. (define-derived-mode ess-jags-mode ess-bugs-mode "ESS[JAGS]"
  193. "Major mode for JAGS."
  194. (setq-local comment-start "#")
  195. (setq font-lock-defaults '(ess-jags-font-lock-keywords nil t))
  196. (setq ess-language "S") ; mimic S for ess-smart-underscore
  197. (unless (and (fboundp 'w32-shell-dos-semantics)
  198. (w32-shell-dos-semantics))
  199. (add-hook 'comint-output-filter-functions 'ess-bugs-exit-notify-sh))
  200. )
  201. ;;;###autoload
  202. (add-to-list 'auto-mode-alist '("\\.[Jj][Aa][Gg]\\'" . ess-jags-mode))
  203. (provide 'ess-jags-d)
  204. ;;; ess-jags-d.el ends here