Klimi's new dotfiles with stow.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

296 líneas
16 KiB

hace 5 años
  1. ;;; ess-bugs-d.el --- ESS[BUGS] 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-l)
  21. (require 'ess-utils)
  22. (require 'ess-inf)
  23. (require 'ess-mode)
  24. (defvar ess-bugs-command "OpenBUGS" "Default BUGS program in PATH.")
  25. (make-local-variable 'ess-bugs-command)
  26. (defvar ess-bugs-monitor '("") "Default list of variables to monitor.")
  27. (make-local-variable 'ess-bugs-monitor)
  28. (defvar ess-bugs-thin 1 "Default thinning parameter.")
  29. (make-local-variable 'ess-bugs-thin)
  30. (defvar ess-bugs-chains 1 "Default number of chains.")
  31. (make-local-variable 'ess-bugs-chains)
  32. (defvar ess-bugs-burnin 10000 "Default burn-in.")
  33. (make-local-variable 'ess-bugs-burnin)
  34. (defvar ess-bugs-update 10000 "Default number of updates after burnin.")
  35. (make-local-variable 'ess-bugs-update)
  36. (defvar ess-bugs-system nil "Default whether BUGS recognizes the system command.")
  37. (defvar ess-bugs-font-lock-keywords
  38. (list
  39. ;; .bug 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\\|C\\|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. ;; .bmd files
  56. (cons (concat (regexp-opt '(
  57. "dicClear" "dicSet" "dicStats"
  58. "infoMemory" "infoModules" "infoNodeMethods"
  59. "infoNodeTypes" "infoNodeValues"
  60. "infoUpdatersbyDepth" "infoUpdatersbyName"
  61. "modelCheck" "modelCompile" "modelData"
  62. "modelDisable" "modelEnable" "modelGenInits"
  63. "modelInits" "modelPrecision" "modelQuit"
  64. "modelSaveState" "modelSetAP" "modelSetIts"
  65. "modelSetOR" "modelSetRN" "modelUpdate"
  66. "ranksClear" "ranksSet" "ranksStats"
  67. "samplesAutoC" "samplesBgr" "samplesCoda"
  68. "samplesDensity" "samplesHistory" "samplesSet"
  69. "sampleStats" "samplesThin"
  70. "summaryClear" "summarySet" "summaryStats"
  71. ) 'words) "(")
  72. font-lock-function-name-face)
  73. (cons (concat (regexp-opt '("Local Variables" "End") 'words) ":")
  74. font-lock-keyword-face)
  75. )
  76. "ESS[BUGS]: Font lock keywords."
  77. )
  78. (defun ess-bugs-switch-to-suffix (suffix &optional bugs-chains bugs-monitor bugs-thin
  79. bugs-burnin bugs-update)
  80. "ESS[BUGS]: Switch to file with suffix."
  81. (find-file (concat ess-bugs-file-dir ess-bugs-file-root suffix))
  82. (if (equal 0 (buffer-size)) (progn
  83. (if (equal ".bug" suffix) (progn
  84. ;(insert "var ;\n")
  85. (insert "model {\n")
  86. (insert " for (i in 1:N) {\n \n")
  87. (insert " }\n")
  88. (insert "}\n")
  89. (insert "#Local Variables" ":\n")
  90. ; (insert "#enable-local-variables: :all\n")
  91. (insert "#ess-bugs-chains:1\n")
  92. (insert "#ess-bugs-monitor:(\"\")\n")
  93. (insert "#ess-bugs-thin:1\n")
  94. (insert "#ess-bugs-burnin:10000\n")
  95. (insert "#ess-bugs-update:10000\n")
  96. (insert "#End:\n")
  97. ))
  98. (if (equal ".bmd" suffix) (let
  99. ((ess-bugs-temp-chains "") (ess-bugs-temp-monitor ""))
  100. (if bugs-chains (setq ess-bugs-chains bugs-chains))
  101. (if bugs-monitor (setq ess-bugs-monitor bugs-monitor))
  102. (if bugs-thin (setq ess-bugs-thin bugs-thin))
  103. (setq ess-bugs-temp-chains
  104. (concat "modelCompile(" (format "%d" ess-bugs-chains) ")\n"))
  105. (setq bugs-chains ess-bugs-chains)
  106. (while (< 0 bugs-chains)
  107. (setq ess-bugs-temp-chains
  108. (concat ess-bugs-temp-chains
  109. "modelInits('" ess-bugs-file-root
  110. ".##" (format "%d" bugs-chains) "', "
  111. (format "%d" bugs-chains) ")\n"))
  112. (setq bugs-chains (- bugs-chains 1)))
  113. (setq ess-bugs-temp-monitor "")
  114. (while (and (listp ess-bugs-monitor) (consp ess-bugs-monitor))
  115. (if (not (string-equal "" (car ess-bugs-monitor)))
  116. (setq ess-bugs-temp-monitor
  117. (concat ess-bugs-temp-monitor "samplesSet('"
  118. (car ess-bugs-monitor)
  119. ;", thin(" (format "%d" ess-bugs-thin)
  120. "')\n")))
  121. (setq ess-bugs-monitor (cdr ess-bugs-monitor)))
  122. (insert "modelCheck('" ess-bugs-file-root ".bug')\n")
  123. (insert "modelData('" ess-bugs-file-root ".bdt')\n")
  124. (insert (ess-replace-in-string ess-bugs-temp-chains "##" "in"))
  125. (insert "modelGenInits()\n")
  126. (insert "modelUpdate(" (format "%d" bugs-burnin) ")\n")
  127. ;(insert "modelUpdate(" (format "%d" (* bugs-thin bugs-burnin)) ")\n")
  128. (insert ess-bugs-temp-monitor)
  129. (insert "modelUpdate(" (format "%d" (* bugs-thin bugs-update)) ")\n")
  130. ; (insert (ess-replace-in-string
  131. ; (ess-replace-in-string ess-bugs-temp-chains
  132. ; "modelCompile([0-9]+)" "#") "##" "to"))
  133. (if (< 1 bugs-thin) (insert "samplesThin(" (format "%d" bugs-thin) ")\n"))
  134. (insert "samplesCoda('*', '" ess-bugs-file-root "')\n")
  135. ; (if ess-bugs-system (progn
  136. ; (insert "system rm -f " ess-bugs-file-root ".ind\n")
  137. ; (insert "system ln -s " ess-bugs-file-root "index.txt " ess-bugs-file-root ".ind\n")
  138. ; (setq bugs-chains ess-bugs-chains)
  139. ; (while (< 0 bugs-chains)
  140. ; (setq ess-bugs-temp-chain (format "%d" bugs-chains))
  141. ; ;.txt not recognized by BOA and impractical to over-ride
  142. ; (insert "system rm -f " ess-bugs-file-root ess-bugs-temp-chain ".out\n")
  143. ; (insert "system ln -s " ess-bugs-file-root "chain" ess-bugs-temp-chain ".txt "
  144. ; ess-bugs-file-root ess-bugs-temp-chain ".out\n")
  145. ; (setq bugs-chains (- bugs-chains 1)))))
  146. (insert "modelQuit()\n")
  147. (insert "Local Variables" ":\n")
  148. ; (insert "enable-local-variables: :all\n")
  149. (insert "ess-bugs-chains:" (format "%d" ess-bugs-chains) "\n")
  150. (insert "ess-bugs-command:\"" ess-bugs-command "\"\n")
  151. (insert "End:\n")
  152. ))
  153. ))
  154. )
  155. (defun ess-bugs-na-bmd (bugs-command)
  156. "ESS[BUGS]: Perform the Next-Action for .bmd."
  157. ;(ess-save-and-set-local-variables)
  158. (if (equal 0 (buffer-size)) (ess-bugs-switch-to-suffix ".bmd")
  159. ;else
  160. (shell)
  161. (ess-sleep)
  162. (when (and (when (fboundp 'w32-shell-dos-semantics)
  163. (w32-shell-dos-semantics))
  164. (string-equal ":" (substring ess-bugs-file 1 2)))
  165. (insert (substring ess-bugs-file 0 2)))
  166. (comint-send-input)
  167. (insert "cd \"" ess-bugs-file-dir "\"")
  168. (comint-send-input)
  169. ; (let ((ess-bugs-temp-chains ""))
  170. ;
  171. ; (while (< 0 bugs-chains)
  172. ; (setq ess-bugs-temp-chains
  173. ; (concat (format "%d " bugs-chains) ess-bugs-temp-chains))
  174. ; (setq bugs-chains (- bugs-chains 1)))
  175. ;; (insert "echo '"
  176. ;; ess-bugs-batch-pre-command " " bugs-command " < "
  177. ;; ess-bugs-file-root ".bmd > " ess-bugs-file-root ".bog 2>&1 "
  178. ;; ess-bugs-batch-post-command "' > " ess-bugs-file-root ".bsh")
  179. ;; (comint-send-input)
  180. ;; (insert "at -f " ess-bugs-file-root ".bsh now")
  181. ;; (comint-send-input)
  182. (insert "echo '"
  183. ess-bugs-batch-pre-command " " bugs-command " < "
  184. ess-bugs-file-root ".bmd > " ess-bugs-file-root ".bog 2>&1 "
  185. ess-bugs-batch-post-command "' | at now")
  186. (comint-send-input)
  187. ))
  188. (defun ess-bugs-na-bug ()
  189. "ESS[BUGS]: Perform Next-Action for .bug"
  190. (if (equal 0 (buffer-size)) (ess-bugs-switch-to-suffix ".bug")
  191. ;else
  192. (ess-save-and-set-local-variables)
  193. (ess-bugs-switch-to-suffix ".bmd"
  194. ess-bugs-chains ess-bugs-monitor ess-bugs-thin ess-bugs-burnin ess-bugs-update))
  195. )
  196. ;;;###autoload
  197. (define-derived-mode ess-bugs-mode ess-mode "ESS[BUGS]"
  198. "Major mode for BUGS."
  199. (setq-local comment-start "#")
  200. (setq font-lock-defaults '(ess-bugs-font-lock-keywords nil t))
  201. (setq ess-language "S") ; mimic S for ess-smart-underscore
  202. (unless (when (fboundp 'w32-shell-dos-semantics)
  203. (w32-shell-dos-semantics))
  204. (add-hook 'comint-output-filter-functions 'ess-bugs-exit-notify-sh)))
  205. ;;;###autoload
  206. (add-to-list 'auto-mode-alist '("\\.[Bb][Uu][Gg]\\'" . ess-bugs-mode))
  207. ;;;###autoload
  208. (add-to-list 'auto-mode-alist '("\\.[Bb][Oo][Gg]\\'" . ess-bugs-mode))
  209. ;;;###autoload
  210. (add-to-list 'auto-mode-alist '("\\.[Bb][Mm][Dd]\\'" . ess-bugs-mode))
  211. (defun ess-sci-to-dec ()
  212. "For BUGS/S family: Express +/-0.000E+/-0 or +/-0.0e+/-00 as a decimal."
  213. (interactive)
  214. (setq buffer-read-only nil)
  215. (save-excursion (goto-char 0)
  216. (save-match-data (let ((ess-temp-replacement-string nil)
  217. (ess-temp-replacement-9 0)
  218. (ess-temp-replacement-diff 0))
  219. (while (search-forward-regexp "-?[0-9][.][0-9][0-9]?[0-9]?[Ee][+-][0-9][0-9]?" nil t)
  220. (setq ess-temp-replacement-string
  221. (int-to-string (string-to-number (match-string 0))))
  222. (setq ess-temp-replacement-diff (- (match-end 0) (match-beginning 0)))
  223. (save-match-data
  224. (setq ess-temp-replacement-9
  225. (string-match "99999999999$" ess-temp-replacement-string))
  226. (if (not ess-temp-replacement-9)
  227. (setq ess-temp-replacement-9
  228. (string-match "000000000001$" ess-temp-replacement-string))))
  229. (if ess-temp-replacement-9
  230. (setq ess-temp-replacement-string
  231. (substring ess-temp-replacement-string 0 ess-temp-replacement-9)))
  232. (setq ess-temp-replacement-diff
  233. (- ess-temp-replacement-diff (string-width ess-temp-replacement-string)))
  234. (while (> ess-temp-replacement-diff 0)
  235. (setq ess-temp-replacement-string (concat ess-temp-replacement-string " "))
  236. (setq ess-temp-replacement-diff (- ess-temp-replacement-diff 1)))
  237. (replace-match ess-temp-replacement-string))))))
  238. (provide 'ess-bugs-d)
  239. ;;; ess-bugs-d.el ends here