Debian Codegraph @

inst/pander.el

raw
1 ;;; pander.el --- ESS integration of pander (R) package
2
3 ;; Copyright (C) 2012 Gergely Daróczi
4 ;;
5 ;; Author: Gergely Daróczi <[email protected]>
6 ;; Version: 0.1
7 ;; Package-Requires: ((ess))
8 ;; Keywords: ESS, R, report
9 ;; X-URL: https://rapporter.github.io/pander/
10
11 ;; This file is not part of GNU Emacs.
12
13 ;; This "program" is free software; you can redistribute it and/or
14 ;; modify it under the terms of the Affero General Public License
15 ;; as published by the Free Software Foundation; version 3.
16 ;;
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; AGPL for more details: http://www.gnu.org/licenses/agpl-3.0.html
21
22 ;;; Installation and load
23
24 ;; This file can be found on your filesystem if pander R package
25 ;; is installed. If you do not know where it is, run the following
26 ;; command in *R*:
27 ;;
28 ;; system.file('pander.el', package='pander')
29 ;;
30 ;; To use this minor, simply add the above firectory to your path
31 ;; and load pander.el, e.g.:
32 ;;
33 ;; (add-to-list 'load-path "/usr/lib/R/library/pander/")
34 ;; (require 'pander)
35 ;;
36
37 ;;; Feedback
38
39 ;; This minor-mode is under heavy development. Any suggestion/feedback
40 ;; really welcomed at GH issue tracker:
41 ;;
42 ;; https://github.com/rapporter/pander/issues
43
44 ;;; TODO:
45
46 ;; * escaping issue (now changing all double quotes to single quotes in selection)
47
48 ;;; Code:
49
50 ;; minor mode
51
52 (defgroup pander nil
53 "ESS integration of pander R package"
54 :group 'ess)
55
56 (defvar pander-mode-map
57 (let ((keymap (make-sparse-keymap)))
58 (define-key keymap (kbd "C-c p b") 'pander-brew)
59 (define-key keymap (kbd "C-c p B") 'pander-brew-export)
60 (define-key keymap (kbd "C-c p e") 'pander-eval)
61 keymap)
62 "Keymap for pander-mode.")
63
64 (define-minor-mode pander-mode
65 "Toggle pander mode.
66 With no argument, this command toggles the mode.
67 Non-null prefix argument turns on the mode.
68 Null prefix argument turns off the mode.
69
70 When pander mode is enabled, some keybindigs
71 are activated for pander functions."
72 :lighter " pander"
73 :group 'pander
74 )
75
76 ;; Define key-bindings for calling above functions
77
78 ;; (global-set-key (kbd "C-c p e") 'pander-evals-region)
79 ;; (global-set-key (kbd "C-c p r") 'pander-region)
80 ;; (global-set-key (kbd "C-c p c") 'pander-chunk)
81 ;; (global-set-key (kbd "C-c p p") 'pander-region-or-chunk)
82 ;; (global-set-key (kbd "C-c p R") 'pander-region-export)
83 ;; (global-set-key (kbd "C-c p C") 'pander-chunk-export)
84 ;; (global-set-key (kbd "C-c p P") 'pander-region-or-chunk-export)
85
86
87 (defcustom pander-clipboard nil
88 "If non-nil then the result of pander-* functions would be copied to clipboard."
89 :type 'boolean
90 :group 'pander)
91
92 (defcustom pander-show-source nil
93 "If non-nil then the source of R commands would also show up in generated documents while running 'pander-eval'. This would not affect 'brew' function ATM."
94 :type 'boolean
95 :group 'pander)
96
97
98 ;; functions
99
100 (defun pander-postprocess-output ()
101
102 "Prettify results in *ess-output* and optionally copy to clipboard."
103
104 ;; remove possible "+" chars at the beginning of the result
105 (set-buffer "*ess-output*")
106 (beginning-of-line)
107 (while (re-search-forward "\\+ " (min (point-at-eol)) 'go)
108 (replace-match ""))
109
110 ;; copy to clipboard
111 (if pander-clipboard
112 (clipboard-kill-ring-save (point-min) (point-max))
113 )
114
115 )
116
117
118 (defun pander-brew ()
119 "Run Pandoc.brew on current buffer or region (if mark is active), show results in *ess-output* and (optionally) copy results to clipboard while setting working directory to tempdir() temporary."
120 (interactive)
121
122 (save-excursion
123 (if mark-active
124 (let (
125 (selection (buffer-substring-no-properties (region-beginning) (region-end))))
126 (ess-execute (format "require(pander, quietly=T);wd<-getwd();setwd(tempdir());Pandoc.brew(text=\"%s\");setwd(wd)\n" (replace-regexp-in-string "\"" "'" selection)))
127 )
128 (ess-execute (format "require(pander, quietly=T);wd<-getwd();setwd(tempdir());Pandoc.brew(\"%s\");setwd(wd)\n" buffer-file-name))
129 )
130 (pander-postprocess-output)
131 )
132
133 )
134
135
136 (defun pander-brew-export ()
137 "Run Pandoc.brew on current buffer or region (if mark is active) and export results to specified (auto-complete in minibuffer) format. Also tries to open exported document."
138 (interactive)
139
140 (save-excursion
141 (let ((output-format (completing-read "Output format: "
142 '(("html" 1) ("pdf" 2) ("odt" 3) ("docx" 4)) nil nil "html")))
143 (if mark-active
144 (let (
145 (selection (buffer-substring-no-properties (region-beginning) (region-end))))
146 (ess-command (format "require(pander, quietly=T);wd<-getwd();setwd(tempdir());Pandoc.brew(text=\"%s\",output=tempfile(),convert=\"%s\" );setwd(wd)\n" (replace-regexp-in-string "\"" "'" selection) output-format))
147 )
148 (ess-command (format "require(pander, quietly=T);wd<-getwd();setwd(tempdir());Pandoc.brew(\"%s\",output=tempfile(),convert=\"%s\");setwd(wd)\n" buffer-file-name output-format))
149 )
150 )
151 )
152 )
153
154
155 (defun pander-eval ()
156 "Run pander on (automatically evaluated) region *or* current chunk (if marker is not set), show results (of last returned R object) in *ess-output* and (optionally) copy those to clipboard while setting working directory to tempdir() temporary. Chunk is recognized by opening '<%' or '<%=', and closing '%>' tags."
157 (interactive)
158
159 (save-excursion
160 (let ((show-src
161 (if pander-show-source
162 (concat "TRUE")
163 (concat "FALSE"))
164 ))
165
166 (if mark-active
167 (let (
168 (selection (buffer-substring-no-properties (region-beginning) (region-end))))
169 (if (= (length selection) 0)
170 (message "Nothing selected in region.")
171 (ess-execute (format "pander:::ess.pander.evals(\"%s\", show.src=%s)\n" (replace-regexp-in-string "\"" "'" selection) show-src))))
172
173 (let (p1 p2)
174 (skip-chars-backward "^<%[=]+") (setq p1 (point))
175 (skip-chars-forward "^%>") (setq p2 (point))
176 (let (
177 (selection (buffer-substring-no-properties p1 p2)))
178 (if (= (length selection) 0)
179 (message "Pointer is not inside a chunk!")
180 (ess-execute (format "pander:::ess.pander.evals(\"%s\", show.src=%s)\n" (replace-regexp-in-string "\"" "'" selection) show-src))))))
181
182 )(pander-postprocess-output))
183
184 )
185
186
187 (provide 'pander)