Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
2701 views
1
;;; ess-mouse.el --- Support for mouse- or cursor-sensitive actions
2
3
;; Copyright (C) 2001 Richard M. Heiberger <[email protected]>
4
;; Copyright (C) 2002--2004 A.J. Rossini, Richard M. Heiberger, Martin
5
;; Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
6
7
;; Author: Richard M. Heiberger <[email protected]>
8
;; Created: 25 Mar 2001
9
;; Maintainer: ESS-core <[email protected]>
10
11
;; This file is part of ESS
12
13
;; This file is free software; you can redistribute it and/or modify
14
;; it under the terms of the GNU General Public License as published by
15
;; the Free Software Foundation; either version 2, or (at your option)
16
;; any later version.
17
18
;; This file is distributed in the hope that it will be useful,
19
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
;; GNU General Public License for more details.
22
23
;; A copy of the GNU General Public License is available at
24
;; http://www.r-project.org/Licenses/
25
26
;;; Commentary:
27
28
;; Support for mouse- or cursor-sensitive actions. This is based on
29
;; and uses mouseme.el. mouseme.el only does mouse sensititivity.
30
;; The new functions ess-mouse-me and ess-mouse-me-helper do similar
31
;; things based on the cursor, not the mouse, and can be bound to a
32
;; keystroke.
33
34
;;; Code:
35
36
; Requires and autoloads
37
38
;;*;; Requires
39
(require 'mouseme)
40
;;(if (or (equal window-system 'w32)
41
;; (equal window-system 'win32)
42
;; (equal window-system 'mswindows))
43
;; (require 'essiw32b))
44
45
(defun ess-mouse-me ()
46
"Popup a menu of functions to run on selected string or region."
47
(interactive)
48
(ess-mouse-me-helper
49
(lambda ()
50
(or (x-popup-menu (list '(0 0)
51
(get-buffer-window (get-buffer (buffer-name))))
52
(funcall mouse-me-build-menu-function name))
53
(error "No command to run")))))
54
55
56
57
(defun ess-mouse-me-helper (func)
58
"Determine the string to use to process EVENT and call FUNC to get cmd."
59
(let (name sp sm mouse beg end cmd mmtype)
60
;; temporarily goto where the event occurred, get the name clicked
61
;; on and enough info to figure out what to do with it
62
(save-match-data
63
(save-excursion
64
(setq sp (point)) ; saved point
65
(setq sm (mark t)) ; saved mark
66
;;; (set-buffer (window-buffer (posn-window (event-start event))))
67
;;; (setq mouse (goto-char (posn-point (event-start event))))
68
(setq mouse (point)) ;; ess-mouse-me-helper
69
;; if there is a region and point is inside it
70
;; check for sm first incase (null (mark t))
71
;; set name to either the thing they clicked on or region
72
(if (and sm
73
(or (and transient-mark-mode mark-active)
74
(eq last-command 'mouse-drag-region))
75
(>= mouse (setq beg (min sp sm)))
76
(<= mouse (setq end (max sp sm))))
77
(setq name (buffer-substring beg end))
78
(setq name (funcall mouse-me-get-string-function))
79
(if (listp name)
80
(setq beg (nth 1 name)
81
end (nth 2 name)
82
name (car name))
83
(goto-char mouse)
84
(while (not (looking-at (regexp-quote name)))
85
(backward-char 1))
86
(setq beg (point))
87
(setq end (search-forward name))))))
88
;; check if name is null, meaning they clicked on no word
89
(if (or (null name)
90
(and (stringp name) (string= name "" )))
91
(error "No string to pass to function"))
92
;; popup a menu to get a command to run
93
(setq cmd (funcall func))
94
;; run the command, eval'ing if it was a list
95
(if (listp cmd)
96
(setq cmd (eval cmd)))
97
(setq mmtype (get cmd 'mouse-me-type))
98
(cond ((eq mmtype 'region)
99
(funcall cmd beg end))
100
((eq mmtype 'string)
101
(funcall cmd name))
102
(t
103
(funcall cmd name)))))
104
105
(defcustom ess-S-mouse-me-menu-commands-alist
106
'("S-Plus 4 and 6 GUI under Windows"
107
("Edit.data" . ess-mouse-me-Edit.data)
108
"----"
109
("print" . ess-mouse-me-print)
110
("summary" . ess-mouse-me-summary)
111
("plot" . ess-mouse-me-plot)
112
("show" . ess-mouse-me-show)
113
("help" . ess-display-help-on-object)
114
("args" . ess-mouse-me-args)
115
"----"
116
("Browser on" . ess-mouse-me-browser-on)
117
("Browser off" . ess-mouse-me-browser-off))
118
"*Command menu used by `mouse-me-build-menu'.
119
A alist of elements where each element is either a cons cell or a string.
120
If a cons cell the car is a string to be displayed in the menu and the
121
cdr is either a function to call passing a string to, or a list which evals
122
to a function to call passing a string to. If the element is a string
123
it makes a non-selectable element in the menu. To make a separator line
124
use a string consisting solely of hyphens.
125
126
The function returned from this menu will be called with one string
127
argument. Or if the function has the symbol property `mouse-me-type'
128
and if its value is the symbol `region' it will be called with the
129
beginning and ending points of the selected string. If the value is
130
the symbol `string' it will be called with one string argument."
131
:type '(repeat sexp)
132
:group 'mouseme)
133
134
135
(defun ess-mouse-me-Edit.data (string)
136
(ess-mouse-me-eval-expanded string "Edit.data(" ")" nil nil nil))
137
138
(defun ess-mouse-me-print (string)
139
(ess-mouse-me-eval-expanded string "" "" nil (ess-ddeclient-p) t))
140
(defun ess-mouse-me-summary (string)
141
(ess-mouse-me-eval-expanded string "summary(" ")" nil (ess-ddeclient-p) t))
142
(defun ess-mouse-me-plot (string)
143
(ess-mouse-me-eval-expanded string "plot(" ")") nil nil nil)
144
(defun ess-mouse-me-show (string)
145
(ess-mouse-me-eval-expanded string "show(" ")") nil nil nil)
146
(defun ess-mouse-me-args (string)
147
(ess-mouse-me-eval-expanded string "args(" ")" nil (ess-ddeclient-p) t))
148
149
(defun ess-mouse-me-browser-on (string)
150
(if (equal (substring ess-dialect 0 1) "R")
151
(ess-eval-linewise (concat "debug(" string ")"))
152
(ess-mouse-me-eval-expanded string "trace(" ", exit=browser)") nil nil nil))
153
154
(defun ess-mouse-me-browser-off (string)
155
(if (equal (substring ess-dialect 0 1) "R")
156
(ess-eval-linewise (concat "undebug(" string ")"))
157
(ess-mouse-me-eval-expanded string "untrace(" ")") nil nil nil))
158
159
160
161
(defun ess-mouse-me-eval-expanded (string &optional head tail commands-buffer
162
page value-returned)
163
"Send the expanded STRING to the inferior-ess process using `ess-command'
164
after first concating the HEAD and TAIL. Put answer in COMMANDS-BUFFER if
165
specified and not using ddeclient, otherwise in \"tmp-buffer\". In either
166
case the buffer containing the answer is renamed to the value of the
167
constructed command. If PAGE is non-nil and using ddeclient, expand
168
the string one more time by embedding it in a \"page()\" command."
169
(interactive)
170
(let* (scommand
171
page-scommand
172
(lproc-name ess-local-process-name)
173
(ess-mouse-customize-alist ess-local-customize-alist))
174
(if (not head) (setq head "summary("))
175
(if (not tail) (setq tail ")"))
176
(if (not commands-buffer) (setq commands-buffer
177
(get-buffer-create "tmp-buffer")))
178
(setq scommand (concat head string tail))
179
180
(if (ess-ddeclient-p)
181
(progn
182
(setq page-scommand (if page
183
(concat "page(" scommand ")")
184
scommand))
185
(set-buffer-file-coding-system 'undecided-dos)
186
(ess-command page-scommand commands-buffer)
187
(if (not value-returned)
188
nil
189
(sleep-for 2)
190
(switch-to-buffer (car (buffer-list)))))
191
(ess-make-buffer-current)
192
(switch-to-buffer commands-buffer)
193
(ess-setq-vars-local (eval ess-mouse-customize-alist) (current-buffer))
194
(setq ess-local-process-name lproc-name)
195
(ess-command (concat scommand "\n") commands-buffer)
196
(if (not value-returned) (switch-to-buffer (nth 1 (buffer-list)))))
197
(if (not value-returned)
198
nil
199
(if ess-microsoft-p ;; there ought to be a filter
200
(while (search-forward "\r" nil t) ;; function to keep the ^M
201
(replace-match "" nil t))) ;; from showing up at all
202
(ess-transcript-mode (eval ess-mouse-customize-alist))
203
(setq ess-local-process-name lproc-name)
204
(rename-buffer scommand))))
205
206
207
; Provide package
208
209
(provide 'ess-mouse)
210
211
212
213
;;;;;;;; STARTUP STUFF ;;;;;;;;;;;;
214
215
(make-variable-buffer-local 'mouse-me-menu-commands)
216
217
(defun ess-S-mouse-me-menu-commands ()
218
(if (equal ess-language "S")
219
(setq mouse-me-menu-commands ess-S-mouse-me-menu-commands-alist)))
220
221
;; (if (not (featurep 'xemacs))
222
;; (progn
223
;; ;;gnu emacs
224
;; (define-key ess-mode-map [S-mouse-3] 'ess-mouse-me)
225
;; (define-key inferior-ess-mode-map [S-mouse-3] 'ess-mouse-me)
226
;; (defun ess-S-mouse-me-ess-transcript-mode ()
227
;; (define-key ess-transcript-mode-map [S-mouse-3] 'ess-mouse-me)))
228
;; ;; xemacs
229
;; (define-key ess-mode-map [(shift button3)] 'ess-mouse-me)
230
;; (define-key inferior-ess-mode-map [(shift button3)] 'ess-mouse-me)
231
;; (defun ess-S-mouse-me-ess-transcript-mode ()
232
;; (define-key ess-transcript-mode-map [(shift button3)] 'ess-mouse-me)))
233
;;
234
(add-hook 'ess-mode-hook 'ess-S-mouse-me-menu-commands)
235
(add-hook 'inferior-ess-mode-hook 'ess-S-mouse-me-menu-commands)
236
(add-hook 'ess-transcript-mode-hook 'ess-S-mouse-me-menu-commands)
237
;; (add-hook 'ess-transcript-mode-hook 'ess-S-mouse-me-ess-transcript-mode)
238
239
240
; Local variables section
241
242
;;; This file is automatically placed in Outline minor mode.
243
;;; The file is structured as follows:
244
;;; Chapters: ^L ;
245
;;; Sections: ;;*;;
246
;;; Subsections: ;;;*;;;
247
;;; Components: defuns, defvars, defconsts
248
;;; Random code beginning with a ;;;;* comment
249
250
;;; Local variables:
251
;;; mode: emacs-lisp
252
;;; outline-minor-mode: nil
253
;;; mode: outline-minor
254
;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
255
;;; End:
256
257
;;; ess-mouse.el ends here
258
259