Klimi's new dotfiles with stow.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

469 satır
19 KiB

4 yıl önce
  1. ;; ess-julia.el --- ESS julia mode and inferior interaction -*- lexical-binding: t; -*-
  2. ;;
  3. ;; Copyright (C) 2012-2015 Vitalie Spinu and the ESS Core team.
  4. ;;
  5. ;; Author: Vitalie Spinu
  6. ;; Maintainer: Vitalie Spinu
  7. ;; Created: 02-04-2012 (ESS 12.03)
  8. ;; Keywords: ESS, julia
  9. ;;
  10. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  11. ;;
  12. ;; This file is *NOT* part of GNU Emacs.
  13. ;; This file is part of ESS
  14. ;;
  15. ;; This program is free software; you can redistribute it and/or
  16. ;; modify it under the terms of the GNU General Public License as
  17. ;; published by the Free Software Foundation; either version 3, or
  18. ;; (at your option) any later version.
  19. ;;
  20. ;; This file is distributed in the hope that it will be useful,
  21. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. ;; GNU General Public License for more details.
  24. ;;
  25. ;; A copy of the GNU General Public License is available at
  26. ;; https://www.r-project.org/Licenses/
  27. ;;
  28. ;;
  29. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  30. ;;
  31. ;;; Commentary:
  32. ;;
  33. ;; Customize inferior-julia-program to point to your julia binary
  34. ;; and start the inferior with M-x julia.
  35. ;;
  36. ;; As of Sept 2015, this file depends heavily on julia-mode.el from the Julia
  37. ;; sources. If you install ESS using `make', this will work fine, otherwise
  38. ;; ensure that julia-mode.el is on your path before loading this file.
  39. ;;
  40. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  41. ;;
  42. ;;; Code:
  43. (require 'ess-help)
  44. (require 'ess-inf)
  45. (require 'ess-r-mode)
  46. (require 'ess-utils)
  47. (require 'julia-mode)
  48. (defvar ac-prefix)
  49. (declare-function company-in-string-or-comment "company")
  50. (declare-function company-doc-buffer "company")
  51. (defcustom inferior-julia-args ""
  52. "String of arguments (see `julia --help') used when starting julia."
  53. :group 'ess-julia
  54. :type 'string)
  55. (eval-when-compile
  56. (require 'cl-lib))
  57. (defun ess-julia-send-string-function (process string _visibly)
  58. "Send the Julia STRING to the PROCESS.
  59. VISIBLY is not currently used."
  60. (let ((file (concat temporary-file-directory "julia_eval_region.jl")))
  61. (with-temp-file file
  62. (insert string))
  63. (process-send-string process (format ess-load-command file))))
  64. ;;; HELP
  65. (cl-defmethod ess-help-get-topics (proc &context (ess-dialect "julia"))
  66. (append (with-current-buffer (ess-command "ESS.all_help_topics()\n")
  67. (split-string (buffer-string) "\n"))
  68. (ess-julia--get-objects proc)))
  69. (defun ess-julia--retrive-topics (url)
  70. (with-current-buffer (url-retrieve-synchronously url)
  71. (require 'url)
  72. (goto-char (point-min))
  73. (let (out)
  74. (while (re-search-forward
  75. "toctext[ \"]+href=\"\\([^>]+\\)\">\\([^<]+\\)</a" nil t)
  76. (push (propertize (match-string 2)
  77. :manual (concat url (match-string 1)))
  78. out))
  79. (kill-buffer)
  80. (nreverse out))))
  81. (defvar ess-julia--manual-topics nil)
  82. (cl-defmethod ess--manual-lookup-override (&context (ess-dialect "julia"))
  83. "Look up topics at https://docs.julialang.org/en/latest/manual/."
  84. (let* ((pages (or ess-julia--manual-topics
  85. (setq ess-julia--manual-topics
  86. (ess-julia--retrive-topics
  87. "https://docs.julialang.org/en/latest/"))))
  88. (page (ess-completing-read "Lookup:" pages nil t)))
  89. (browse-url (get-text-property 1 :manual page))))
  90. (defun ess-julia-input-sender (proc string)
  91. "Function to send PROC STRING submitted by user.
  92. See `comint-input-sender'."
  93. (save-current-buffer
  94. (let* ((help-?-regexp "^ *\\(?:\\(?1: *?\\? *\\)\\(?2:.+\\)\\)")
  95. (help-?-match (string-match help-?-regexp string)))
  96. (cond (help-?-match
  97. (ess-display-help-on-object (match-string 2 string))
  98. (process-send-string proc "\n"))
  99. (t ;; normal command
  100. (inferior-ess-input-sender proc string))))))
  101. (cl-defmethod ess-installed-packages (&context (ess-dialect "julia"))
  102. "Return list of installed julia packages."
  103. ;; FIXME: This doesn't work if the user hasn't done "using Pkg" yet
  104. (ess-get-words-from-vector "keys(Pkg.installed())\n"))
  105. (cl-defmethod ess-load-library--override (pack &context (ess-dialect "julia"))
  106. (ess-eval-linewise (format "using %s\n" pack)))
  107. ;;; COMPLETION
  108. (defun ess-julia-latexsub-completion ()
  109. "Complete latex input, and return format required by `completion-at-point-functions'."
  110. (if (julia-latexsub) ; julia-latexsub returns nil if it performed a completion, the point otherwise
  111. nil
  112. (lambda () t) ;; bypass other completion methods
  113. ))
  114. (defun ess-julia-object-completion ()
  115. "Return completions at point in a format required by `completion-at-point-functions'."
  116. (let ((proc (ess-get-next-available-process ess-dialect t))
  117. (beg (ess-symbol-start)))
  118. (if proc
  119. (when beg
  120. (let* ((prefix (buffer-substring-no-properties beg (point)))
  121. (obj (and (string-match "\\(.*\\)\\..*$" prefix)
  122. (match-string 1 prefix)))
  123. (beg (if obj
  124. (+ beg 1 (length obj))
  125. beg)))
  126. (list beg (point)
  127. (nreverse (mapcar 'car (ess-julia--get-objects proc obj)))
  128. :exclusive 'no)))
  129. (when (string-match "complet" (symbol-name last-command))
  130. (message "No ESS process of dialect %s started" ess-dialect)
  131. nil))))
  132. (defun ess-julia-objects (prefix &optional proc)
  133. "Given PREFIX get all cached objects from PROC."
  134. (when prefix
  135. (let ((proc (or proc (ess-get-next-available-process nil t))))
  136. (if (string-match "\\(.*\\)\\..*$" prefix)
  137. (let ((module (match-string 1 prefix)))
  138. (mapcar (lambda (el) (concat module "." (car el)))
  139. (ess-julia--get-objects proc module)))
  140. (ess-julia--get-objects proc)))))
  141. (defun ess-julia--get-objects (&optional proc obj)
  142. "Return all available objects.
  143. Local caching might be used. If MODULE is givven, return only
  144. objects from that MODULE."
  145. (setq proc (or proc (ess-get-process)))
  146. (when (stringp proc)
  147. (setq proc (get-process proc)))
  148. (when (process-live-p proc)
  149. (let ((objects (process-get proc 'objects)))
  150. (if (process-get proc 'busy)
  151. (if obj
  152. (assoc obj objects)
  153. (process-get proc 'objects))
  154. (if obj
  155. (or (cdr (assoc obj objects))
  156. ;; don't cache composite objects and datatypes
  157. (ess-julia--get-components proc obj))
  158. ;; this segment is entered when user completon at top level is
  159. ;; requested, either Tab or AC. Hence Main is always updated.
  160. (let ((modules (ess-get-words-from-vector
  161. "ESS.main_modules()\n" nil nil proc))
  162. (loc (process-get proc 'last-objects-cache))
  163. (lev (process-get proc 'last-eval)))
  164. (prog1
  165. (apply #'nconc
  166. (mapcar
  167. (lambda (mod)
  168. ;; we are caching all modules, and reinit Main every
  169. ;; time user enters commands
  170. (copy-sequence
  171. (or (and (or (not (equal mod "Main"))
  172. (ignore-errors (time-less-p lev loc)))
  173. (cdr (assoc mod objects)))
  174. (ess-julia--get-components proc mod t))))
  175. modules))
  176. (process-put proc 'last-objects-cache (current-time)))))))))
  177. (defun ess-julia--get-components (proc obj &optional cache?)
  178. (with-current-buffer (ess-command (format "ESS.components(%s)\n" obj)
  179. nil nil nil nil proc)
  180. (goto-char (point-min))
  181. (let (list)
  182. (while (re-search-forward
  183. "^\\([^ \t\n]+\\) +\\([^ \t\n]+\\)$" nil t)
  184. (push (cons (match-string 1) (match-string 2)) list))
  185. (when cache?
  186. (let ((objects (process-get proc 'objects)))
  187. (push (cons obj list) objects)
  188. (process-put proc 'objects objects)))
  189. list)))
  190. (defun ess-julia-get-object-help-string (sym)
  191. "Help string for ac."
  192. (let ((proc (ess-get-next-available-process nil t)))
  193. (if (null proc)
  194. "No free ESS process found"
  195. (let ((buf (get-buffer-create " *ess-command-output*")))
  196. (with-current-buffer (process-buffer proc)
  197. (ess-with-current-buffer buf
  198. (ess--flush-help-into-current-buffer sym nil)))
  199. (with-current-buffer buf
  200. (ess-help-underline)
  201. (goto-char (point-min))
  202. (buffer-string))))))
  203. (defvar ac-source-ess-julia-objects
  204. '((prefix . ess-symbol-start)
  205. (requires . 2)
  206. (candidates . ess-ac-julia-objects)
  207. (document . ess-julia-get-object-help-string))
  208. "Auto-completion source for julia objects.")
  209. (defun ess-ac-julia-objects ()
  210. (require 'auto-complete)
  211. (ess-julia-objects ac-prefix))
  212. (declare-function company-begin-backend "company.el")
  213. (defun company-ess-julia-objects (command &optional arg &rest ignored)
  214. (interactive (list 'interactive))
  215. (cl-case command
  216. (interactive (company-begin-backend 'company-ess-julia-objects))
  217. (prefix (unless (company-in-string-or-comment)
  218. (let ((start (ess-symbol-start)))
  219. (when start (buffer-substring-no-properties start (point))))))
  220. (candidates (let ((proc (ess-get-next-available-process)))
  221. (when proc
  222. (all-completions arg (mapcar (lambda (x) (or (car-safe x) x))
  223. (ess-julia-objects arg proc))))))
  224. (doc-buffer (company-doc-buffer (ess-julia-get-object-help-string arg)))))
  225. ;;; ERRORS
  226. (defvar ess-julia-error-regexp-alist '(ess-julia-in ess-julia-at ess-julia-while-load)
  227. "List of symbols which are looked up in `compilation-error-regexp-alist-alist'.")
  228. (add-to-list 'compilation-error-regexp-alist-alist
  229. '(ess-julia-in "^\\s-*in [^ \t\n]* \\(at \\(.*\\):\\([0-9]+\\)\\)" 2 3 nil 2 1))
  230. (add-to-list 'compilation-error-regexp-alist-alist
  231. '(ess-julia-at "^\\S-+\\s-+\\(at \\(.*\\):\\([0-9]+\\)\\)" 2 3 nil 2 1))
  232. (add-to-list 'compilation-error-regexp-alist-alist
  233. '(ess-julia-while-load "^\\s-*\\(while loading\\s-\\(.*\\), in .* on line +\\([0-9]+\\)\\)" 2 3 nil 2 1))
  234. ;;; ELDOC
  235. (defun ess-julia-eldoc-function ()
  236. "Return the doc string, or nil.
  237. If an ESS process is not associated with the buffer, do not try
  238. to look up any doc strings."
  239. (when (and ess-can-eval-in-background
  240. (ess-process-live-p)
  241. (not (ess-process-get 'busy)))
  242. (let ((funname (or (and ess-eldoc-show-on-symbol ;; aggressive completion
  243. (symbol-name (ess-symbol-at-point)))
  244. (car (ess--fn-name-start)))))
  245. (when funname
  246. (let* ((args (copy-sequence (nth 2 (ess-function-arguments funname))))
  247. (W (- (window-width (minibuffer-window)) (+ 4 (length funname))))
  248. (doc (concat (propertize funname 'face font-lock-function-name-face) ": ")))
  249. (when args
  250. (setq args (sort args (lambda (s1 s2)
  251. (< (length s1) (length s2)))))
  252. (setq doc (concat doc (pop args)))
  253. (while (and args (< (+ (length doc) (length (car args))) W))
  254. (setq doc (concat doc " "
  255. (pop args))))
  256. (when (and args (< (length doc) W))
  257. (setq doc (concat doc " {--}")))
  258. doc))))))
  259. ;;; CORE
  260. (defvar ess-julia-customize-alist
  261. '((inferior-ess-primary-prompt . "\\w*> ")
  262. (inferior-ess-secondary-prompt . nil)
  263. (inferior-ess-prompt . "\\w*> ")
  264. (ess-local-customize-alist . 'ess-julia-customize-alist)
  265. (inferior-ess-program . inferior-julia-program)
  266. (ess-load-command . "include(expanduser(\"%s\"))\n")
  267. (ess-funargs-command . "ESS.fun_args(\"%s\")\n")
  268. (ess-dump-error-re . "in \\w* at \\(.*\\):[0-9]+")
  269. (ess-error-regexp . "\\(^\\s-*at\\s-*\\(?3:.*\\):\\(?2:[0-9]+\\)\\)")
  270. (ess-error-regexp-alist . ess-julia-error-regexp-alist)
  271. (ess-mode-completion-syntax-table . ess-julia-completion-syntax-table)
  272. ;; (inferior-ess-objects-command . inferior-ess-r-objects-command)
  273. ;; (inferior-ess-search-list-command . "search()\n")
  274. (inferior-ess-help-command . "ESS.help(\"%s\")\n")
  275. ;; (inferior-ess-help-command . "help(\"%s\")\n")
  276. (ess-language . "julia")
  277. (ess-dialect . "julia")
  278. (ess-suffix . "jl")
  279. (ess-dump-filename-template . (replace-regexp-in-string
  280. "S$" ess-suffix ; in the one from custom:
  281. ess-dump-filename-template-proto))
  282. (ess-mode-editing-alist . nil)
  283. (ess-change-sp-regexp . nil );ess-r-change-sp-regexp)
  284. (ess-help-sec-regex . ess-help-r-sec-regex)
  285. (ess-help-sec-keys-alist . ess-help-r-sec-keys-alist)
  286. (ess-function-pattern . ess-r-function-pattern)
  287. (ess-object-name-db-file . "ess-jl-namedb.el" )
  288. (ess-smart-operators . ess-r-smart-operators)
  289. (inferior-ess-exit-command . "exit()\n")
  290. ;;harmful for shell-mode's C-a: -- but "necessary" for ESS-help?
  291. (inferior-ess-language-start . nil)
  292. (ess-STERM . "iESS")
  293. (ess-editor . ess-r-editor)
  294. (ess-pager . ess-r-pager)
  295. (ess-getwd-command . "pwd()\n")
  296. (ess-setwd-command . "cd(expanduser(\"%s\"))\n"))
  297. "Variables to customize for Julia.")
  298. (cl-defmethod ess--help-web-search-override (cmd &context (ess-dialect "julia"))
  299. "Offer to search the web for a Julia command."
  300. (browse-url (format "https://docs.julialang.org/en/latest/search/?q=%s" cmd)))
  301. (defvar ess-julia-completion-syntax-table
  302. (let ((table (copy-syntax-table ess-r-mode-syntax-table)))
  303. (modify-syntax-entry ?. "_" table)
  304. ;; (modify-syntax-entry ?: "_" table)
  305. ;; (modify-syntax-entry ?$ "_" table)
  306. (modify-syntax-entry ?@ "_" table)
  307. table)
  308. "Syntax table used for completion and help symbol lookup.
  309. It makes underscores and dots word constituent chars.")
  310. (cl-defmethod ess-help-commands (&context (ess-dialect "julia"))
  311. '((packages . "_ess_list_categories()\n")
  312. (package-index . "_ess_print_index(\"%s\")\n")
  313. (index-keyword-reg . "^\\(.*+\\):$*")
  314. (index-start-reg . ":")))
  315. (defvar ess-julia-mode-syntax-table (copy-syntax-table julia-mode-syntax-table))
  316. (defvar ess-julia-mode-map
  317. (let ((map (make-sparse-keymap)))
  318. (set-keymap-parent map ess-mode-map)
  319. map)
  320. "Keymap for `ess-julia-mode'.")
  321. ;;;###autoload
  322. (define-derived-mode ess-julia-mode julia-mode "ESS[julia]"
  323. "Major mode for julia files."
  324. :group 'ess-Julia
  325. (setq-local ess-local-customize-alist ess-julia-customize-alist)
  326. (setq ess-dialect "julia")
  327. (ess-setq-vars-local ess-julia-customize-alist)
  328. ;; eldoc
  329. (add-function :before-until (local 'eldoc-documentation-function)
  330. #'ess-julia-eldoc-function)
  331. (when ess-use-eldoc (eldoc-mode))
  332. ;; auto-complete
  333. (ess--setup-auto-complete '(ac-source-ess-julia-objects))
  334. ;; company
  335. (ess--setup-company '(company-ess-julia-objects))
  336. ;; for emacs >= 24
  337. (remove-hook 'completion-at-point-functions 'ess-filename-completion 'local) ;; should be first
  338. (add-hook 'completion-at-point-functions 'ess-julia-object-completion nil 'local)
  339. (add-hook 'completion-at-point-functions 'ess-filename-completion nil 'local)
  340. (add-hook 'completion-at-point-functions 'ess-julia-latexsub-completion nil 'local)
  341. (if (fboundp 'ess-add-toolbar) (ess-add-toolbar)))
  342. ;; Inferior mode
  343. (defvar inferior-ess-julia-mode-syntax-table
  344. (copy-syntax-table ess-julia-mode-syntax-table)
  345. "Syntax table for `inferior-ess-julia-mode'.")
  346. (define-derived-mode inferior-ess-julia-mode inferior-ess-mode "iESS[julia]"
  347. "Major mode for inferior julia processes."
  348. :group 'ess-Julia
  349. (ess-setq-vars-local ess-julia-customize-alist)
  350. (setq-local comint-use-prompt-regexp t)
  351. (setq comint-prompt-regexp (concat "^" inferior-ess-prompt))
  352. (setq ess-dialect "julia")
  353. ;; eldoc
  354. (add-function :before-until (local 'eldoc-documentation-function)
  355. #'ess-julia-eldoc-function)
  356. (when ess-use-eldoc (eldoc-mode))
  357. ;; auto-complete
  358. (ess--setup-auto-complete '(ac-source-ess-julia-objects) t)
  359. ;; company
  360. (ess--setup-company '(company-ess-julia-objects) t)
  361. (remove-hook 'completion-at-point-functions 'ess-filename-completion 'local) ;; should be first
  362. (add-hook 'completion-at-point-functions 'ess-julia-object-completion nil 'local)
  363. (add-hook 'completion-at-point-functions 'ess-filename-completion nil 'local)
  364. (add-hook 'completion-at-point-functions 'ess-julia-latexsub-completion nil 'local)
  365. (setq comint-input-sender #'ess-julia-input-sender))
  366. (defvar ess-julia-mode-hook nil)
  367. (defvar ess-julia-post-run-hook nil
  368. "Functions run in process buffer after starting julia process.")
  369. ;;;###autoload
  370. (defun run-ess-julia (&optional start-args)
  371. "Start an inferior julia process.
  372. Optional prefix START-ARGS (\\[universal-argument]) allows to set
  373. command line arguments, such as --load=<file>. This should be OS
  374. agnostic. If you have certain command line arguments that should
  375. always be passed to julia, put them in the variable
  376. `inferior-julia-args'."
  377. (interactive "P")
  378. ;; get settings, notably inferior-julia-program :
  379. (if (null inferior-julia-program)
  380. (error "'inferior-julia-program' does not point to 'julia' or 'julia-basic' executable")
  381. (ess-write-to-dribble-buffer ;; for debugging only
  382. (format
  383. "\n(julia): ess-dialect=%s, buf=%s, start-arg=%s\n current-prefix-arg=%s\n"
  384. ess-dialect (current-buffer) start-args current-prefix-arg))
  385. (let* ((jl-start-args
  386. (concat inferior-julia-args " " ; add space just in case
  387. (if start-args
  388. (read-string
  389. (concat "Starting Args"
  390. (if inferior-julia-args
  391. (concat " [other than '" inferior-julia-args "']"))
  392. " ? "))
  393. nil))))
  394. (let ((inf-buf (inferior-ess jl-start-args ess-julia-customize-alist)))
  395. (with-current-buffer inf-buf
  396. (ess--tb-start)
  397. ;; Remove ` from julia's logo
  398. (goto-char (point-min))
  399. (while (re-search-forward "`" nil t)
  400. (replace-match "'"))
  401. ;; Remove an offending unmatched parenthesis
  402. (goto-char (point-min))
  403. (forward-line 4)
  404. (when (re-search-forward "(" nil t)
  405. (replace-match "|"))
  406. (goto-char (point-max))
  407. ;; --> julia helpers from ../etc/ess-julia.jl :
  408. (ess--inject-code-from-file (format "%sess-julia.jl" ess-etc-directory))
  409. (run-mode-hooks 'ess-julia-post-run-hook))
  410. inf-buf))))
  411. ;;;###autoload
  412. (defalias 'julia #'run-ess-julia)
  413. (cl-defmethod ess--help-major-mode (&context (ess-dialect "julia"))
  414. (ess-julia-help-mode))
  415. (define-derived-mode ess-julia-help-mode ess-help-mode "ESS[Julia] Help"
  416. "Major mode for Julia documentation."
  417. :group 'ess-help
  418. (let ((inhibit-read-only t))
  419. ;; Julia help buffers can contain color if julia starts with
  420. ;; --color=yes
  421. (ansi-color-apply-on-region (point-min) (point-max))))
  422. (add-to-list 'auto-mode-alist '("\\.jl\\'" . ess-julia-mode))
  423. (provide 'ess-julia)
  424. ;;; ess-julia.el ends here