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