1 ;;; idlw-complete-structtag.el --- Completion of structure tags.
2 ;; Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006
3 ;; Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <dominik@science.uva.nl>
6 ;; Maintainer: J.D. Smith <jdsmith@as.arizona.edu>
10 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
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.
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.
51 ;; New versions of IDLWAVE, documentation, and more information available
57 ;; Put this file on the emacs load path and load it with the following
58 ;; line in your .emacs file:
60 ;; (add-hook 'idlwave-load-hook
61 ;; (lambda () (require 'idlw-complete-structtag)))
65 ;; Suppose your IDL program contains something like
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
73 ;; state = {tag1:val1, tag2:val2, ...}
75 ;; and offers the tags for completion.
77 ;; In the idlwave shell, idlwave sends a "print,tag_names()" for the
78 ;; variable to idl and determines the current tag list dynamically.
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
91 (defvar idlwave-completion-help-info
)
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
)
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
)
102 ;; Create the sintern type for structure talks
103 (idlwave-new-sintern-type 'structtag
)
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
)
109 ;;; The main code follows below
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.
117 In the idlwave shell, the current content of the variable is used to get
118 an up-to-date completion list."
122 (case-fold-search t
))
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.
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
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
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
))
161 (defvar idlwave-structtag-struct-location nil
162 "The location of the structure definition, for help display.")
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
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
)
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
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.
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)))
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
200 (list (idlwave-sintern-structtag x
'set
)))
203 (error "Cannot complete structure tags of variable %s" var
))))
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
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
)))))
224 ;; Fake help in the source buffer for structure tags.
225 ;; kwd and name are global-variables here.
228 (defvar idlwave-help-do-struct-tag
)
229 (defun idlwave-complete-structure-tag-help (mode word
)
232 ;; fontify only in source buffers, not in the shell.
233 (not (equal idlwave-current-tags-buffer
234 (get-buffer (idlwave-shell-buffer)))))
237 idlwave-help-do-struct-tag idlwave-structtag-struct-location
))
238 (t (error "This should not happen"))))
240 (provide 'idlw-complete-structtag
)
242 ;;; idlw-complete-structtag.el ends here
245 ;; arch-tag: d1f9e55c-e504-4187-9c31-3c3651fa4bfa