Mercurial > emacs
comparison lisp/progmodes/idlw-complete-structtag.el @ 64001:82d080bf4f42
Updated to IDLWAVE v5.7 (see idlwave.org), and variable cleanup
author | J.D. Smith <jdsmith@as.arizona.edu> |
---|---|
date | Mon, 04 Jul 2005 01:51:24 +0000 |
parents | |
children | 442764288956 |
comparison
equal
deleted
inserted
replaced
64000:e1ccb8b46dea | 64001:82d080bf4f42 |
---|---|
1 ;;; idlw-complete-structtag.el --- Completion of structure tags. | |
2 ;; Copyright (c) 2001,2002 Free Software Foundation | |
3 | |
4 ;; Author: Carsten Dominik <dominik@science.uva.nl> | |
5 ;; Maintainer: J.D. Smith <jdsmith@as.arizona.edu> | |
6 ;; Version: 1.2 | |
7 ;; Keywords: languages | |
8 | |
9 ;; This file is part of GNU Emacs. | |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
15 | |
16 ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
25 | |
26 ;;; Commentary: | |
27 | |
28 ;; Completion of structure tags can be done automatically in the | |
29 ;; shell, since the list of tags can be determined dynamically through | |
30 ;; interaction with IDL. | |
31 | |
32 ;; Completion of structure tags in a source buffer is highly ambiguous | |
33 ;; since you never know what kind of structure a variable will hold at | |
34 ;; runtime. To make this feature useful in source buffers, we need a | |
35 ;; special assumption/convention. We will assume that the structure is | |
36 ;; defined in the same buffer and directly assigned to the correct | |
37 ;; variable. This is mainly useful for applications in which there is one | |
38 ;; main structure which contains a large amount of information (and many | |
39 ;; tags). For example, many widget applications define a "state" structure | |
40 ;; that contains all important data about the application. The different | |
41 ;; routines called by the event handler then use this structure. If you | |
42 ;; use the same variable name for this structure throughout your | |
43 ;; application (a good idea for many reasons), IDLWAVE can support | |
44 ;; completion for its tags. | |
45 ;; | |
46 ;; This file is a completion plugin which implements this kind of | |
47 ;; completion. It is also an example which shows how completion plugins | |
48 ;; should be programmed. | |
49 ;; | |
50 ;; New versions of IDLWAVE, documentation, and more information available | |
51 ;; from: | |
52 ;; http://idlwave.org | |
53 ;; | |
54 ;; INSTALLATION | |
55 ;; ============ | |
56 ;; Put this file on the emacs load path and load it with the following | |
57 ;; line in your .emacs file: | |
58 ;; | |
59 ;; (add-hook 'idlwave-load-hook | |
60 ;; (lambda () (require 'idlw-complete-structtag))) | |
61 ;; | |
62 ;; DESCRIPTION | |
63 ;; =========== | |
64 ;; Suppose your IDL program contains something like | |
65 ;; | |
66 ;; myvar = state.a* | |
67 ;; | |
68 ;; where the star marks the cursor position. If you now press the | |
69 ;; completion key M-TAB, IDLWAVE searches the current file for a | |
70 ;; structure definition | |
71 ;; | |
72 ;; state = {tag1:val1, tag2:val2, ...} | |
73 ;; | |
74 ;; and offers the tags for completion. | |
75 ;; | |
76 ;; In the idlwave shell, idlwave sends a "print,tag_names()" for the | |
77 ;; variable to idl and determines the current tag list dynamically. | |
78 ;; | |
79 ;; Notes | |
80 ;; ----- | |
81 ;; - The structure definition assignment "state = {...}" must use the | |
82 ;; same variable name as the the completion location "state.*". | |
83 ;; - The structure definition must be in the same file. | |
84 ;; - The structure definition is searched backwards and then forward | |
85 ;; from the current position, until a definition with tags is found. | |
86 ;; - The file is parsed again for each new completion variable and location. | |
87 ;; - You can force an update of the tag list with the usual command | |
88 ;; to update routine info in IDLWAVE: C-c C-i | |
89 | |
90 | |
91 ;; Some variables to identify the previously used structure | |
92 (defvar idlwave-current-tags-var nil) | |
93 (defvar idlwave-current-tags-buffer nil) | |
94 (defvar idlwave-current-tags-completion-pos nil) | |
95 | |
96 ;; The tag list used for completion will be stored in the following vars | |
97 (defvar idlwave-current-struct-tags nil) | |
98 (defvar idlwave-sint-structtags nil) | |
99 | |
100 ;; Create the sintern type for structure talks | |
101 (idlwave-new-sintern-type 'structtag) | |
102 | |
103 ;; Hook the plugin into idlwave | |
104 (add-to-list 'idlwave-complete-special 'idlwave-complete-structure-tag) | |
105 (add-hook 'idlwave-update-rinfo-hook 'idlwave-structtag-reset) | |
106 | |
107 ;;; The main code follows below | |
108 | |
109 (defun idlwave-complete-structure-tag () | |
110 "Complete a structure tag. | |
111 This works by looking in the current file for a structure assignment to a | |
112 variable with the same name and takes the tags from there. Quite useful | |
113 for big structures like the state variables of a widget application. | |
114 | |
115 In the idlwave shell, the current content of the variable is used to get | |
116 an up-to-date completion list." | |
117 (interactive) | |
118 (let ((pos (point)) | |
119 start | |
120 (case-fold-search t)) | |
121 (if (save-excursion | |
122 ;; Check if the context is right. | |
123 ;; In the shell, this could be extended to expressions like | |
124 ;; x[i+4].name.g*. But it is complicated because we would have | |
125 ;; to really parse this expression. For now, we allow only | |
126 ;; substructures, like "aaa.bbb.ccc.ddd" | |
127 (skip-chars-backward "[a-zA-Z0-9._$]") | |
128 (setq start (point)) ;; remember the start of the completion pos. | |
129 (and (< (point) pos) | |
130 (not (equal (char-before) ?!)) ; no sysvars | |
131 (looking-at "\\([a-zA-Z][.a-zA-Z0-9_]*\\)\\.") | |
132 (>= pos (match-end 0)) | |
133 (not (string= (downcase (match-string 1)) "self")))) | |
134 (let* ((var (downcase (match-string 1)))) | |
135 ;; Check if we need to update the "current" structure. Basically we | |
136 ;; do it always, except for subsequent completions at the same | |
137 ;; spot, to save a bit of time. Implementation: We require | |
138 ;; an update if | |
139 ;; - the variable is different or | |
140 ;; - the buffer is different or | |
141 ;; - we are completing at a different position | |
142 (if (or (not (string= var (or idlwave-current-tags-var "@"))) | |
143 (not (eq (current-buffer) idlwave-current-tags-buffer)) | |
144 (not (equal start idlwave-current-tags-completion-pos))) | |
145 (idlwave-prepare-structure-tag-completion var)) | |
146 (setq idlwave-current-tags-completion-pos start) | |
147 (setq idlwave-completion-help-info | |
148 (list 'idlwave-complete-structure-tag-help)) | |
149 (idlwave-complete-in-buffer 'structtag 'structtag | |
150 idlwave-current-struct-tags nil | |
151 "Select a structure tag" "structure tag") | |
152 t) ; we did the completion: return t to skip other completions | |
153 nil))) ; return nil to allow looking for other ways to complete | |
154 | |
155 (defun idlwave-structtag-reset () | |
156 "Force an update of the current structure tag list upon next use." | |
157 (setq idlwave-current-tags-buffer nil)) | |
158 | |
159 (defvar idlwave-structtag-struct-location nil | |
160 "The location of the structure definition, for help display.") | |
161 | |
162 (defun idlwave-prepare-structure-tag-completion (var) | |
163 "Find and parse the tag list for structure tag completion." | |
164 ;; This works differently in source buffers and in the shell | |
165 (if (eq major-mode 'idlwave-shell-mode) | |
166 ;; OK, we are in the shell, do it dynamically | |
167 (progn | |
168 (message "preparing shell tags") | |
169 ;; The following call puts the tags into `idlwave-current-struct-tags' | |
170 (idlwave-complete-structure-tag-query-shell var) | |
171 ;; initialize | |
172 (setq idlwave-sint-structtags nil | |
173 idlwave-current-tags-buffer (current-buffer) | |
174 idlwave-current-tags-var var | |
175 idlwave-structtag-struct-location (point) | |
176 idlwave-current-struct-tags | |
177 (mapcar (lambda (x) | |
178 (list (idlwave-sintern-structtag x 'set))) | |
179 idlwave-current-struct-tags)) | |
180 (if (not idlwave-current-struct-tags) | |
181 (error "Cannot complete structure tags of variable %s" var))) | |
182 ;; Not the shell, so probably a source buffer. | |
183 (unless | |
184 (catch 'exit | |
185 (save-excursion | |
186 (goto-char (point-max)) | |
187 ;; Find possible definitions of the structure. | |
188 (while (idlwave-find-structure-definition var nil 'all) | |
189 (let ((tags (idlwave-struct-tags))) | |
190 (when tags | |
191 ;; initialize | |
192 (setq idlwave-sint-structtags nil | |
193 idlwave-current-tags-buffer (current-buffer) | |
194 idlwave-current-tags-var var | |
195 idlwave-structtag-struct-location (point) | |
196 idlwave-current-struct-tags | |
197 (mapcar (lambda (x) | |
198 (list (idlwave-sintern-structtag x 'set))) | |
199 tags)) | |
200 (throw 'exit t)))))) | |
201 (error "Cannot complete structure tags of variable %s" var)))) | |
202 | |
203 (defun idlwave-complete-structure-tag-query-shell (var) | |
204 "Ask the shell for the tags of the structure in variable or expression VAR." | |
205 (idlwave-shell-send-command | |
206 (format "if size(%s,/TYPE) eq 8 then print,tag_names(%s)" var var) | |
207 'idlwave-complete-structure-tag-get-tags-from-help | |
208 'hide 'wait)) | |
209 | |
210 (defvar idlwave-shell-prompt-pattern) | |
211 (defvar idlwave-shell-command-output) | |
212 (defun idlwave-complete-structure-tag-get-tags-from-help () | |
213 "Filter structure tag name output, result to `idlwave-current-struct-tags'." | |
214 (setq idlwave-current-struct-tags | |
215 (if (string-match (concat "tag_names(.*) *\n" | |
216 "\\(\\(.*[\r\n]?\\)*\\)" | |
217 "\\(" idlwave-shell-prompt-pattern "\\)") | |
218 idlwave-shell-command-output) | |
219 (split-string (match-string 1 idlwave-shell-command-output))))) | |
220 | |
221 | |
222 ;; Fake help in the source buffer for structure tags. | |
223 ;; kwd and name are global-variables here. | |
224 (defvar name) | |
225 (defvar kwd) | |
226 (defvar idlwave-help-do-struct-tag) | |
227 (defun idlwave-complete-structure-tag-help (mode word) | |
228 (cond | |
229 ((eq mode 'test) | |
230 ;; fontify only in source buffers, not in the shell. | |
231 (not (equal idlwave-current-tags-buffer | |
232 (get-buffer (idlwave-shell-buffer))))) | |
233 ((eq mode 'set) | |
234 (setq kwd word | |
235 idlwave-help-do-struct-tag idlwave-structtag-struct-location)) | |
236 (t (error "This should not happen")))) | |
237 | |
238 (provide 'idlw-complete-structtag) | |
239 | |
240 ;;; idlw-complete-structtag.el ends here | |
241 | |
242 |