]> code.delx.au - gnu-emacs/blob - lisp/progmodes/idlw-complete-structtag.el
(gud-def): Add %c case.
[gnu-emacs] / lisp / progmodes / idlw-complete-structtag.el
1 ;;; idlw-complete-structtag.el --- Completion of structure tags.
2 ;; Copyright (c) 2001,2002,2003,2004,2005,2006 Free Software Foundation
3
4 ;; Author: Carsten Dominik <dominik@astro.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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, 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 (require 'idlwave)
91
92 ;; Some variables to identify the previously used structure
93 (defvar idlwave-current-tags-var nil)
94 (defvar idlwave-current-tags-buffer nil)
95 (defvar idlwave-current-tags-completion-pos nil)
96
97 ;; The tag list used for completion will be stored in the following vars
98 (defvar idlwave-current-struct-tags nil)
99 (defvar idlwave-sint-structtags nil)
100
101 ;; Create the sintern type for structure talks
102 (idlwave-new-sintern-type 'structtag)
103
104 ;; Hook the plugin into idlwave
105 (add-to-list 'idlwave-complete-special 'idlwave-complete-structure-tag)
106 (add-hook 'idlwave-update-rinfo-hook 'idlwave-structtag-reset)
107
108 ;;; The main code follows below
109 (defvar idlwave-completion-help-info)
110 (defun idlwave-complete-structure-tag ()
111 "Complete a structure tag.
112 This works by looking in the current file for a structure assignment to a
113 variable with the same name and takes the tags from there. Quite useful
114 for big structures like the state variables of a widget application.
115
116 In the idlwave shell, the current content of the variable is used to get
117 an up-to-date completion list."
118 (interactive)
119 (let ((pos (point))
120 start
121 (case-fold-search t))
122 (if (save-excursion
123 ;; Check if the context is right.
124 ;; In the shell, this could be extended to expressions like
125 ;; x[i+4].name.g*. But it is complicated because we would have
126 ;; to really parse this expression. For now, we allow only
127 ;; substructures, like "aaa.bbb.ccc.ddd"
128 (skip-chars-backward "[a-zA-Z0-9._$]")
129 (setq start (point)) ;; remember the start of the completion pos.
130 (and (< (point) pos)
131 (not (equal (char-before) ?!)) ; no sysvars
132 (looking-at "\\([a-zA-Z][.a-zA-Z0-9_]*\\)\\.")
133 (>= pos (match-end 0))
134 (not (string= (downcase (match-string 1)) "self"))))
135 (let* ((var (downcase (match-string 1))))
136 ;; Check if we need to update the "current" structure. Basically we
137 ;; do it always, except for subsequent completions at the same
138 ;; spot, to save a bit of time. Implementation: We require
139 ;; an update if
140 ;; - the variable is different or
141 ;; - the buffer is different or
142 ;; - we are completing at a different position
143 (if (or (not (string= var (or idlwave-current-tags-var "@")))
144 (not (eq (current-buffer) idlwave-current-tags-buffer))
145 (not (equal start idlwave-current-tags-completion-pos)))
146 (idlwave-prepare-structure-tag-completion var))
147 (setq idlwave-current-tags-completion-pos start)
148 (setq idlwave-completion-help-info
149 (list 'idlwave-complete-structure-tag-help))
150 (idlwave-complete-in-buffer 'structtag 'structtag
151 idlwave-current-struct-tags nil
152 "Select a structure tag" "structure tag")
153 t) ; we did the completion: return t to skip other completions
154 nil))) ; return nil to allow looking for other ways to complete
155
156 (defun idlwave-structtag-reset ()
157 "Force an update of the current structure tag list upon next use."
158 (setq idlwave-current-tags-buffer nil))
159
160 (defvar idlwave-structtag-struct-location nil
161 "The location of the structure definition, for help display.")
162
163 (defun idlwave-prepare-structure-tag-completion (var)
164 "Find and parse the tag list for structure tag completion."
165 ;; This works differently in source buffers and in the shell
166 (if (eq major-mode 'idlwave-shell-mode)
167 ;; OK, we are in the shell, do it dynamically
168 (progn
169 (message "preparing shell tags")
170 ;; The following call puts the tags into `idlwave-current-struct-tags'
171 (idlwave-complete-structure-tag-query-shell var)
172 ;; initialize
173 (setq idlwave-sint-structtags nil
174 idlwave-current-tags-buffer (current-buffer)
175 idlwave-current-tags-var var
176 idlwave-structtag-struct-location (point)
177 idlwave-current-struct-tags
178 (mapcar (lambda (x)
179 (list (idlwave-sintern-structtag x 'set)))
180 idlwave-current-struct-tags))
181 (if (not idlwave-current-struct-tags)
182 (error "Cannot complete structure tags of variable %s" var)))
183 ;; Not the shell, so probably a source buffer.
184 (unless
185 (catch 'exit
186 (save-excursion
187 (goto-char (point-max))
188 ;; Find possible definitions of the structure.
189 (while (idlwave-find-structure-definition var nil 'all)
190 (let ((tags (idlwave-struct-tags)))
191 (when tags
192 ;; initialize
193 (setq idlwave-sint-structtags nil
194 idlwave-current-tags-buffer (current-buffer)
195 idlwave-current-tags-var var
196 idlwave-structtag-struct-location (point)
197 idlwave-current-struct-tags
198 (mapcar (lambda (x)
199 (list (idlwave-sintern-structtag x 'set)))
200 tags))
201 (throw 'exit t))))))
202 (error "Cannot complete structure tags of variable %s" var))))
203
204 (defun idlwave-complete-structure-tag-query-shell (var)
205 "Ask the shell for the tags of the structure in variable or expression VAR."
206 (idlwave-shell-send-command
207 (format "if size(%s,/TYPE) eq 8 then print,tag_names(%s)" var var)
208 'idlwave-complete-structure-tag-get-tags-from-help
209 'hide 'wait))
210
211 (defvar idlwave-shell-prompt-pattern)
212 (defvar idlwave-shell-command-output)
213 (defun idlwave-complete-structure-tag-get-tags-from-help ()
214 "Filter structure tag name output, result to `idlwave-current-struct-tags'."
215 (setq idlwave-current-struct-tags
216 (if (string-match (concat "tag_names(.*) *\n"
217 "\\(\\(.*[\r\n]?\\)*\\)"
218 "\\(" idlwave-shell-prompt-pattern "\\)")
219 idlwave-shell-command-output)
220 (split-string (match-string 1 idlwave-shell-command-output)))))
221
222
223 ;; Fake help in the source buffer for structure tags.
224 ;; kwd and name are global-variables here.
225 (defvar name)
226 (defvar kwd)
227 (defvar idlwave-help-do-struct-tag)
228 (defun idlwave-complete-structure-tag-help (mode word)
229 (cond
230 ((eq mode 'test)
231 ;; fontify only in source buffers, not in the shell.
232 (not (equal idlwave-current-tags-buffer
233 (get-buffer (idlwave-shell-buffer)))))
234 ((eq mode 'set)
235 (setq kwd word
236 idlwave-help-do-struct-tag idlwave-structtag-struct-location))
237 (t (error "This should not happen"))))
238
239 (provide 'idlw-complete-structtag)
240
241 ;;; idlw-complete-structtag.el ends here
242
243
244 ;; arch-tag: d1f9e55c-e504-4187-9c31-3c3651fa4bfa