Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 188 lines (144 sloc) 6.351 kb
6b171d4 @daroczig fix: ESS integration issues and adding auto-revognisation of chunks
daroczig authored
1 ;;; pander.el --- ESS integration of pander (R) package
2
3 ;; Copyright (C) 2012 Gergely Daróczi
4 ;;
5 ;; Author: Gergely Daróczi <gergely@snowl.net>
6 ;; Version: 0.1
e1596d9 @daroczig towards a minor mode
daroczig authored
7 ;; Package-Requires: ((ess))
6b171d4 @daroczig fix: ESS integration issues and adding auto-revognisation of chunks
daroczig authored
8 ;; Keywords: ESS, R, report
60a0e49 @daroczig finishing touches before submitting to CRAN
daroczig authored
9 ;; X-URL: http://rapporter.github.com/pander/
6b171d4 @daroczig fix: ESS integration issues and adding auto-revognisation of chunks
daroczig authored
10
e1596d9 @daroczig towards a minor mode
daroczig authored
11 ;; This file is not part of GNU Emacs.
12
13 ;; This "program" is free software; you can redistribute it and/or
6b171d4 @daroczig fix: ESS integration issues and adding auto-revognisation of chunks
daroczig authored
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
e1596d9 @daroczig towards a minor mode
daroczig authored
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 ;;
6b171d4 @daroczig fix: ESS integration issues and adding auto-revognisation of chunks
daroczig authored
36
e1596d9 @daroczig towards a minor mode
daroczig authored
37 ;;; Feedback
6b171d4 @daroczig fix: ESS integration issues and adding auto-revognisation of chunks
daroczig authored
38
e1596d9 @daroczig towards a minor mode
daroczig authored
39 ;; This minor-mode is under heavy development. Any suggestion/feedback
40 ;; really welcomed at GH issue tracker:
6b171d4 @daroczig fix: ESS integration issues and adding auto-revognisation of chunks
daroczig authored
41 ;;
60a0e49 @daroczig finishing touches before submitting to CRAN
daroczig authored
42 ;; http://rapporter.github.com/pander/issues
e1596d9 @daroczig towards a minor mode
daroczig authored
43
44 ;;; TODO:
45
6b171d4 @daroczig fix: ESS integration issues and adding auto-revognisation of chunks
daroczig authored
46 ;; * escaping issue (now changing all double quotes to single quotes in selection)
e1596d9 @daroczig towards a minor mode
daroczig authored
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)
6b171d4 @daroczig fix: ESS integration issues and adding auto-revognisation of chunks
daroczig authored
85
86
e1596d9 @daroczig towards a minor mode
daroczig authored
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
38bf3fc @daroczig add: option to show R sources while running pander-eval
daroczig authored
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
e1596d9 @daroczig towards a minor mode
daroczig authored
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 )
6b171d4 @daroczig fix: ESS integration issues and adding auto-revognisation of chunks
daroczig authored
116
117
6b55ee5 @daroczig add: ESS (kind of) integration
daroczig authored
118 (defun pander-brew ()
e1596d9 @daroczig towards a minor mode
daroczig authored
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."
6b55ee5 @daroczig add: ESS (kind of) integration
daroczig authored
120 (interactive)
e1596d9 @daroczig towards a minor mode
daroczig authored
121
86a81e7 @daroczig towards prettifying results in ESS: remove possible "+" chars at the beg...
daroczig authored
122 (save-excursion
e1596d9 @daroczig towards a minor mode
daroczig authored
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)
86a81e7 @daroczig towards prettifying results in ESS: remove possible "+" chars at the beg...
daroczig authored
131 )
e1596d9 @daroczig towards a minor mode
daroczig authored
132
133 )
6b55ee5 @daroczig add: ESS (kind of) integration
daroczig authored
134
6b171d4 @daroczig fix: ESS integration issues and adding auto-revognisation of chunks
daroczig authored
135
e1596d9 @daroczig towards a minor mode
daroczig authored
136 (defun pander-brew-export ()
0954bc5 @daroczig update docs based on recent changes
daroczig authored
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."
6b55ee5 @daroczig add: ESS (kind of) integration
daroczig authored
138 (interactive)
e1596d9 @daroczig towards a minor mode
daroczig authored
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
38bf3fc @daroczig add: option to show R sources while running pander-eval
daroczig authored
160 (let ((show-src
161 (if pander-show-source
162 (concat "TRUE")
163 (concat "FALSE"))
164 ))
e1596d9 @daroczig towards a minor mode
daroczig authored
165
38bf3fc @daroczig add: option to show R sources while running pander-eval
daroczig authored
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))))))
e1596d9 @daroczig towards a minor mode
daroczig authored
181
38bf3fc @daroczig add: option to show R sources while running pander-eval
daroczig authored
182 )(pander-postprocess-output))
183
e1596d9 @daroczig towards a minor mode
daroczig authored
184 )
185
186
60a0e49 @daroczig finishing touches before submitting to CRAN
daroczig authored
187 (provide 'pander)
Something went wrong with that request. Please try again.