Klimi's new dotfiles with stow.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

463 rader
16 KiB

5 år sedan
  1. ;; ess-rd.el --- Support for editing R documentation (Rd) source -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 1997--2005 A.J. Rossini, Richard M. Heiberger, Martin
  3. ;; Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
  4. ;; Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  5. ;; Created: 25 July 1997
  6. ;; Maintainer: ESS-core <ESS-core@r-project.org>
  7. ;; This file is part of ESS (Emacs Speaks Statistics).
  8. ;; This file is free software; you may redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by the
  10. ;; Free Software Foundation; either version 2, or (at your option) any
  11. ;; later version.
  12. ;;
  13. ;; This is distributed in the hope that it will be useful, but WITHOUT
  14. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. ;; for more details.
  17. ;;
  18. ;; A copy of the GNU General Public License is available on the World
  19. ;; Wide Web at https://www.gnu.org/copyleft/gpl.html. You can also
  20. ;; obtain it by writing to the Free Software Foundation, Inc., 675 Mass
  21. ;; Ave, Cambridge, MA 02139, USA.
  22. ;;; Commentary:
  23. ;;
  24. ;;; Code:
  25. (eval-when-compile
  26. (require 'subr-x))
  27. (require 'ess-help)
  28. (require 'ess-inf)
  29. ;; Silence the byte compiler, see TODO below; can we remove these?
  30. (defvar ess-help-r-sec-regex)
  31. (defvar ess-help-r-sec-keys-alist)
  32. (defvar ess-r-customize-alist)
  33. (defcustom Rd-mode-hook nil
  34. "Hook to be run when Rd mode is entered."
  35. :type 'hook
  36. :group 'ess-R
  37. :group 'ess-hooks)
  38. (define-abbrev-table 'Rd-mode-skeleton-abbrev-table
  39. '(("`ag" "\\arguments" nil :system t)
  40. ("`al" "\\alias" nil :system t)
  41. ("`au" "\\author" nil :system t)
  42. ("`bf" "\\bold" nil :system t)
  43. ("`co" "\\code" nil :system t)
  44. ("`de" "\\describe" nil :system t)
  45. ("`dn" "\\description" nil :system t)
  46. ("`dt" "\\details" nil :system t)
  47. ("`em" "\\emph" nil :system t)
  48. ("`en" "\\enumerate" nil :system t)
  49. ("`ex" "\\examples" nil :system t)
  50. ("`fi" "\\file" nil :system t)
  51. ("`fo" "\\format" nil :system t)
  52. ("`it" "\\item" nil :system t)
  53. ("`iz" "\\itemize" nil :system t)
  54. ("`kw" "\\keyword" nil :system t)
  55. ("`li" "\\link" nil :system t)
  56. ("`me" "\\method" nil :system t)
  57. ("`na" "\\name" nil :system t)
  58. ("`no" "\\note" nil :system t)
  59. ("`re" "\\references" nil :system t)
  60. ("`sa" "\\seealso" nil :system t)
  61. ("`se" "\\section" nil :system t)
  62. ("`so" "\\source" nil :system t)
  63. ("`ss" "\\subsection" nil :system t)
  64. ("`sy" "\\synopsis" nil :system t)
  65. ("`ta" "\\tabular" nil :system t)
  66. ("`ti" "\\title" nil :system t)
  67. ("`us" "\\usage" nil :system t)
  68. ("`va" "\\value" nil :system t))
  69. "Abbrev table for R documentation keywords.
  70. All Rd mode abbrevs start with a grave accent (`)."
  71. :case-fixed t)
  72. (define-abbrev-table 'Rd-mode-abbrev-table ()
  73. "Abbrev table for Rd mode."
  74. :parents (list Rd-mode-skeleton-abbrev-table))
  75. (defvar Rd-mode-syntax-table
  76. (let ((tab (copy-syntax-table text-mode-syntax-table)))
  77. (modify-syntax-entry ?\\ "\\" tab)
  78. (modify-syntax-entry ?\{ "(}" tab)
  79. (modify-syntax-entry ?\} "){" tab)
  80. ;; Nice for editing, not for parsing ...
  81. (modify-syntax-entry ?\( "()" tab)
  82. (modify-syntax-entry ?\) ")(" tab)
  83. (modify-syntax-entry ?\[ "(]" tab)
  84. (modify-syntax-entry ?\] ")[" tab)
  85. ;; To get strings right
  86. ;; (modify-syntax-entry ?\' "\"" Rd-mode-syntax-table)
  87. (modify-syntax-entry ?\" "\"" tab)
  88. ;; To make abbrevs starting with a grave accent work ...
  89. (modify-syntax-entry ?\` "w" tab)
  90. ;; Comments
  91. (modify-syntax-entry ?\% "<" tab)
  92. (modify-syntax-entry ?\n ">" tab)
  93. tab)
  94. "Syntax table for `Rd-mode'.")
  95. (defvar Rd-mode-parse-syntax-table
  96. (let ((tab (copy-syntax-table Rd-mode-syntax-table)))
  97. ;; To make parse-partial-sexps do the thing we want for computing
  98. ;; indentations
  99. (modify-syntax-entry ?\( "_" tab)
  100. (modify-syntax-entry ?\) "_" tab)
  101. (modify-syntax-entry ?\[ "_" tab)
  102. (modify-syntax-entry ?\] "_" tab)
  103. tab)
  104. "Syntax table for parsing Rd mode.")
  105. (defvar Rd-section-names
  106. '("Rdversion" "arguments" "alias" "author" "concept" "describe" "description"
  107. "details" "docType" "encoding" "enumerate" "examples" "format"
  108. "itemize" "keyword" "name" "note" "preformatted" "references"
  109. "seealso" "section" "source" "subsection" "synopsis"
  110. "tabular" "title" "usage"
  111. "value"))
  112. (defvar Rd-keywords
  113. '(
  114. ;; the next two lines: only valid in R <= 2.8.1
  115. ;; commented out on 2011-01-14 for ESS version 5.13:
  116. ;; "Alpha" "Gamma" "alpha" "beta" "epsilon" "lambda" "mu" "pi" "sigma"
  117. ;; "ge" "le" "left" "right"
  118. ;;
  119. "R" "RdOpts" "S3method" "S4method" "Sexpr" "acronym"
  120. "bold" "cite" "code" "command" "cr" "dQuote" "deqn" "dfn" "dontrun"
  121. "dontshow" "donttest" "dots" "email" "emph" "enc" "env" "eqn" "figure" "file"
  122. "href" "if" "ifelse"
  123. "item" "kbd" "ldots" "linkS4class" "link" "method"
  124. "newcommand" "option" "out"
  125. "pkg" "sQuote" "renewcommand"
  126. "samp" "strong" "tab" "url" "var" "verb"
  127. ;; System macros (from <R>/share/Rd/macros/system.Rd ):
  128. "CRANpkg" "PR" "sspace" "doi"
  129. "packageTitle" "packageDescription" "packageAuthor"
  130. "packageMaintainer" "packageDESCRIPTION" "packageIndices"
  131. ))
  132. (defvar Rd-font-lock-keywords
  133. (list
  134. (cons
  135. (concat "\\\\\\("
  136. (mapconcat 'identity Rd-section-names "\\|")
  137. "\\>\\)")
  138. 'font-lock-reference-face) ; Rd-bold-face
  139. (cons
  140. (concat "\\\\\\("
  141. (mapconcat 'identity Rd-keywords "\\|")
  142. "\\>\\)")
  143. 'font-lock-keyword-face)
  144. '("^#\\(ifn?def\\)\\s-+\\(\\sw+\\)"
  145. (1 font-lock-builtin-face)
  146. (2 font-lock-variable-name-face nil t))
  147. '("^#\\(endif\\)" 1 font-lock-builtin-face))
  148. "Additional Rd expressions to highlight.")
  149. (defvar Rd-indent-level 2
  150. "Indentation of Rd code with respect to containing blocks.")
  151. (defvar Rd-mode-map
  152. (let ((map (make-sparse-keymap)))
  153. (define-key map "\t" #'indent-according-to-mode)
  154. (define-key map "\C-j" #'reindent-then-newline-and-indent)
  155. (define-key map "\C-m" #'reindent-then-newline-and-indent)
  156. (define-key map "\C-c\C-p" #'Rd-preview-help)
  157. (define-key map "\C-c\C-j" #'Rd-mode-insert-item)
  158. (define-key map "\C-c\C-e" #'Rd-mode-insert-skeleton)
  159. (define-key map "\C-c\C-f" #'Rd-font)
  160. (define-key map "\C-c\C-s" #'Rd-mode-insert-section)
  161. (define-key map "\C-c\C-n" #'ess-eval-line-visibly-and-step)
  162. (define-key map "\C-c\C-r" #'ess-eval-region)
  163. (define-key map "\C-c\C-c" #'ess-eval-region-or-function-or-paragraph-and-step)
  164. (define-key map "\C-\M-x" #'ess-eval-region-or-function-or-paragraph)
  165. (define-key map "\C-c\C-v" #'ess-display-help-on-object)
  166. (define-key map "\C-c\C-w" #'ess-switch-process)
  167. (define-key map "\C-c\C-y" #'ess-switch-to-ESS)
  168. (define-key map "\C-c\C-z" #'ess-switch-to-end-of-ESS)
  169. map)
  170. "Keymap used in Rd mode.")
  171. (defvar Rd-mode-menu
  172. (list "Rd"
  173. ["Markup [word]" Rd-font t]
  174. ["Insert Item" Rd-mode-insert-item t]
  175. ["Insert Section" Rd-mode-insert-section t]
  176. ["Insert Skeleton" Rd-mode-insert-skeleton t]
  177. "-"
  178. ["Preview" Rd-preview-help t]
  179. "-"
  180. ["Eval Line" ess-eval-line-visibly-and-step t]
  181. ["Eval Region" ess-eval-region t]
  182. ["Switch to ESS Process" ess-switch-to-ESS t]
  183. ["Switch the ESS Process" ess-switch-process t]
  184. ["Switch to end{ESS Pr}" ess-switch-to-end-of-ESS t]
  185. "-"
  186. ["Toggle Abbrev Mode" abbrev-mode t]
  187. ["Toggle Auto-Fill Mode" auto-fill-mode t]
  188. "-"
  189. ["Submit Bug Report" ess-submit-bug-report t]
  190. "-"
  191. ["Describe Rd Mode" describe-mode t])
  192. "Menu used in Rd mode.")
  193. (defvar Rd-to-help-command "R CMD Rd2txt"
  194. "Shell command for converting R documentation source to help text.")
  195. (defvar Rd-font-list
  196. '((?\C-b "\\bold{" "}")
  197. (?\C-c "\\code{" "}")
  198. (?\C-e "\\emph{" "}")
  199. (?\C-l "\\link{" "}")
  200. (?l "\\code{\\link{" "}}")
  201. (?\C-m "\\email{" "}")
  202. (?\C-q "\\eqn{" "}")
  203. (?\C-u "\\url{" "}")
  204. )
  205. "List of \"fonts\" used by `Rd-font'.
  206. Each entry is a list. The first element is the key to activate
  207. the font. The second element is the string to insert before
  208. point, and the third element is the string to insert after
  209. point.")
  210. ;;;###autoload
  211. (define-derived-mode Rd-mode text-mode "Rd"
  212. "Major mode for editing R documentation source files.
  213. Type \\[list-abbrevs] to display the built-in abbrevs for Rd
  214. keywords.To automatically turn on the abbrev(iate) features, add
  215. the following to your Emacs configuration file:
  216. (add-hook 'Rd-mode-hook #'abbrev-mode)"
  217. (setq ess-language "S" ess-dialect "R")
  218. (require 'ess-r-mode)
  219. (ess-setq-vars-local ess-r-customize-alist)
  220. (setq-local indent-line-function 'Rd-mode-indent-line)
  221. (setq fill-column 72)
  222. (setq-local comment-start-skip "\\s<+\\s-*")
  223. (setq-local comment-start "% ")
  224. (setq-local comment-end "")
  225. (setq font-lock-defaults
  226. '(Rd-font-lock-keywords nil nil))
  227. ;; Here is a workaround for an Emacs bug related to indirect buffers and
  228. ;; spurious lockfiles that rears its ugly head with .Rd files
  229. ;; https://lists.gnu.org/archive/html/bug-gnu-emacs/2013-02/msg01368.html
  230. ;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=14328
  231. (setq-local create-lockfiles nil)
  232. (easy-menu-define Rd-mode-menu-map Rd-mode-map
  233. "Menu keymap for Rd mode." Rd-mode-menu)
  234. (turn-on-auto-fill))
  235. ;;;###autoload
  236. (add-to-list 'auto-mode-alist '("\\.Rd\\'" . Rd-mode))
  237. (defun Rd-describe-major-mode ()
  238. "Describe the current major mode."
  239. (declare (obsolete describe-mode "ESS 19.04"))
  240. (describe-function major-mode))
  241. (defun Rd-mode-in-verbatim-p ()
  242. "Return non-nil if in a usage, examples, or synopsis."
  243. (let ((pos (point)))
  244. (save-excursion
  245. (if (and (re-search-backward
  246. "\\\\\\(usage\\|examples\\|synopsis\\)" nil t)
  247. (re-search-forward "\\s(" nil t))
  248. (condition-case ()
  249. (progn
  250. (up-list 1)
  251. (< pos (point)))
  252. (error t))
  253. nil))))
  254. (defun Rd-mode-in-preprocessor-line-p ()
  255. "Return non-nil if in a preprocessor line."
  256. (save-excursion
  257. (beginning-of-line)
  258. (looking-at "[ \t]*#\\(ifdef\\|endif\\)")))
  259. (defun Rd-mode-calculate-indent ()
  260. "Return appropriate indentation for current line in Rd mode."
  261. (save-excursion
  262. (beginning-of-line)
  263. (cond
  264. ((Rd-mode-in-verbatim-p)
  265. ;; Don't do anything in verbatims
  266. nil)
  267. ((Rd-mode-in-preprocessor-line-p)
  268. ;; Indent to 0
  269. 0)
  270. (t
  271. (let ((p (progn
  272. (re-search-forward "[ \t]*\\s)*" (point-at-eol) t)
  273. (point))))
  274. (if (or (< (forward-line -1) 0)
  275. (Rd-mode-in-verbatim-p))
  276. 0
  277. (set-syntax-table Rd-mode-parse-syntax-table)
  278. (while (and (or (looking-at "[ \t]*$")
  279. (Rd-mode-in-preprocessor-line-p))
  280. (not (bobp)))
  281. (forward-line -1))
  282. (re-search-forward "[ \t]*\\s)*" (point-at-eol) t)
  283. (prog1
  284. (+ (current-indentation)
  285. (* (car (parse-partial-sexp (point) p))
  286. Rd-indent-level))
  287. (set-syntax-table Rd-mode-syntax-table))))))))
  288. (defun Rd-mode-indent-line ()
  289. "Indent current line as Rd source."
  290. (when-let ((ic (Rd-mode-calculate-indent))
  291. (rp (- (current-column) (current-indentation))))
  292. (when (< ic 0)
  293. (error "Unmatched parenthesis"))
  294. (indent-line-to ic)
  295. (when (> rp 0)
  296. (move-to-column (+ ic rp)))))
  297. (defun Rd-mode-insert-item ()
  298. "Insert \\item{ on a newline."
  299. (interactive)
  300. (reindent-then-newline-and-indent)
  301. (insert "\\item{")
  302. )
  303. (defun Rd-mode-insert-section ()
  304. "Insert a section from `Rd-section-names'."
  305. (interactive)
  306. (let ((s (ess-completing-read
  307. "Insert section: "
  308. (mapcar (lambda (x) (cons x x)) Rd-section-names)
  309. nil t)))
  310. (if (string= s "")
  311. (progn (insert "\\section{}{") (backward-char 2))
  312. (insert (format "\\%s{" s)))))
  313. (defun Rd-mode-insert-skeleton ()
  314. "Insert several empty Rd fields."
  315. (interactive)
  316. ;; Hmm: in theory this should be kept in sync with prompt()
  317. ;; --- maybe using prompt() [or promptClass()...] would be better anyway?!
  318. (insert "\\name{}\n")
  319. (insert "\\alias{}\n")
  320. (insert "\\title{}\n")
  321. (insert "\\description{\n}\n")
  322. (insert "\\usage{\n}\n")
  323. (insert "\\arguments{\n}\n")
  324. (insert "\\value{\n}\n")
  325. (insert "\\details{\n}\n")
  326. (insert "\\references{\n}\n")
  327. (insert "\\seealso{\n}\n")
  328. (insert "\\examples{\n}\n")
  329. (insert "\\author{}\n")
  330. (insert "\\keyword{}\n"))
  331. ;; This is an `easy' version of (defun TeX-font ..) in AUCtex's tex.el ;
  332. ;; see TeX-font-list and also LaTeX-font-list in latex.el
  333. (defun Rd-font (what)
  334. "Insert template for font command.
  335. WHAT determines the font to use, as specified by `Rd-font-list'."
  336. (interactive "c")
  337. ;;TeX had : (Rd-update-style)
  338. (let* ((entry (assoc what Rd-font-list))
  339. (before (nth 1 entry))
  340. (after (nth 2 entry)))
  341. (cond ((null entry) ;; help on possibilities :
  342. (let ((help
  343. (concat
  344. "Rd Markup (available from C-c C-f):\n\n\t"
  345. "KEY Rd-Markup\n\n"
  346. (mapconcat
  347. (lambda (entry)
  348. ;; A textual description of an ENTRY in TeX-font-list.
  349. (concat (format "%11s "
  350. (key-description
  351. (char-to-string (nth 0 entry))))
  352. (format "%14s %-3s"
  353. (nth 1 entry) (nth 2 entry))))
  354. Rd-font-list "\n"))))
  355. (with-output-to-temp-buffer "*Help*"
  356. (set-buffer "*Help*")
  357. (insert help))))
  358. ((region-active-p)
  359. (save-excursion
  360. (cond ((> (mark) (point))
  361. (insert before)
  362. (goto-char (mark))
  363. (insert after))
  364. (t
  365. (insert after)
  366. (goto-char (mark))
  367. (insert before)))))
  368. (t
  369. (insert before)
  370. (save-excursion
  371. (insert after))))))
  372. (defun Rd-preview-help (&optional via-shell)
  373. "Preview the current Rd buffer contents as help.
  374. If the current buffer is not associated with a file, create a
  375. temporary one in variable `temporary-file-directory'."
  376. (declare (advertised-calling-convention () "ESS 19.04"))
  377. (interactive "P") ; If optional VIA-SHELL is set, using `Rd-to-help-command'.
  378. (let ((file buffer-file-name)
  379. (pbuf (get-buffer-create "R Help Preview"))
  380. del-p)
  381. (unless file
  382. (setq file (make-temp-file "RD_" nil ".Rd"))
  383. (write-region (point-min) (point-max) file)
  384. (setq del-p t))
  385. (if via-shell ;; FIXME eventually get rid of this option
  386. ;; only method in ESS <= 14.09 -- calls "R" even if in "R-devel"; slower
  387. (let ((shcmd (format "%s '%s'" Rd-to-help-command file)))
  388. (set-buffer pbuf)
  389. (erase-buffer)
  390. (ess-write-to-dribble-buffer
  391. (format "Rd-preview-help: (shell-command |%s| t)" shcmd))
  392. (shell-command shcmd t))
  393. ;; else directly:
  394. (ess-force-buffer-current "R process to use: ")
  395. (ess-command (format ".ess_Rd2txt(\"%s\")\n" file) pbuf)
  396. (set-buffer pbuf))
  397. ;; FIXME(2): once got rid of via-shell, consider
  398. ;; (ess--flush-help-into-current-buffer file "tools::Rd2txt(\"%s\")\n")
  399. ;; instead of all this :
  400. (ess-setq-vars-local ess-r-customize-alist)
  401. ;; mostly cut'n'paste from ess--flush-help* (see FIXME(2)):
  402. (ess-help-underline)
  403. (ess--help-major-mode)
  404. ;; FIXME: Is this really needed?
  405. (setq ess-help-sec-regex ess-help-r-sec-regex
  406. ess-help-sec-keys-alist ess-help-r-sec-keys-alist)
  407. (goto-char (point-min))
  408. (set-buffer-modified-p 'nil)
  409. (setq buffer-read-only t)
  410. (setq truncate-lines nil)
  411. (when del-p (delete-file file))
  412. (unless (get-buffer-window pbuf 'visible)
  413. (display-buffer pbuf t))))
  414. (define-obsolete-function-alias 'Rd-submit-bug-report 'ess-submit-bug-report "2018-08-16")
  415. (provide 'ess-rd)
  416. ;;; ess-rd.el ends here