0
|
1 ;;!emacs
|
|
2 ;;
|
|
3 ;; FILE: br-python-ft.el
|
|
4 ;; SUMMARY: Python OO-Browser class and member functions.
|
|
5 ;; USAGE: GNU Emacs Lisp Library
|
|
6 ;; KEYWORDS: python, oop, tools
|
|
7 ;;
|
|
8 ;; AUTHOR: Harri Pasanen, based on the C++ feature browser
|
|
9 ;; by Bob Weiner
|
|
10 ;; ORG: Tekla Oy
|
|
11 ;;
|
|
12 ;; ORIG-DATE: 5-Apr-96
|
24
|
13 ;; LAST-MOD: 23-Dec-96 at 22:09:00 by Bob Weiner
|
0
|
14 ;;
|
24
|
15 ;; Copyright (C) 1990-1996 Free Software Foundation, Inc.
|
0
|
16 ;; See the file BR-COPY for license information.
|
|
17 ;;
|
|
18 ;; This file is part of the OO-Browser.
|
|
19 ;;
|
|
20 ;; DESCRIPTION:
|
|
21 ;; There may still be traces of C++ origin in this file.
|
|
22 ;; DESCRIP-END.
|
|
23
|
|
24 ;;; ************************************************************************
|
|
25 ;;; Other required Elisp libraries
|
|
26 ;;; ************************************************************************
|
|
27
|
|
28 (require 'br-python)
|
|
29
|
|
30 ;;; ************************************************************************
|
|
31 ;;; Public variables
|
|
32 ;;; ************************************************************************
|
|
33
|
|
34 (defvar python-import-dirs '("/usr/local/lib/python/")
|
|
35 "Ordered list of module directories by default searched by python
|
|
36 interpreter. Each directory must end with a directory separator.")
|
|
37
|
|
38 (defconst python-type-tag-separator "@"
|
|
39 "String that separates a tag's type from its normalized definition form.
|
|
40 This should be a single character which is unchanged when quoted for use as a
|
|
41 literal in a regular expression.")
|
|
42
|
|
43 (defconst python-tag-fields-regexp
|
|
44 ;; The \\\\? below is necessary because we sometimes use this expression to
|
|
45 ;; test against a string that has ben regexp-quoted and some of the
|
|
46 ;; characters in br-feature-type-regexp will then be preceded by \\.
|
|
47 (format "\\`\\([^%s \n]+\\)%s\\\\?\\(%s \\)\\([^%s\n]+\\)%s"
|
|
48 python-type-tag-separator python-type-tag-separator br-feature-type-regexp
|
|
49 python-type-tag-separator python-type-tag-separator)
|
|
50 "Regexp matching the fields of a Python feature tag line.
|
|
51 Group 1 is the class of the feature. Group 2 is the prefix preceding the
|
|
52 feature when displayed within a listing buffer. Group 3 is the feature name.
|
|
53 The feature definition signature begins at the end of the regexp match,
|
|
54 i.e. (match-end 0), and goes to the end of the string or line.")
|
|
55
|
|
56 ;;; ************************************************************************
|
|
57 ;;; Public functions
|
|
58 ;;; ************************************************************************
|
|
59
|
|
60 (defun python-add-default-classes ()
|
|
61 ;; Add to 'system' class table.
|
|
62 ;; Add this default class for global functions
|
24
|
63 (br-add-class "[function]" br-null-path nil))
|
0
|
64
|
|
65 (defun python-feature-implementors (name)
|
|
66 "Return unsorted list of Python feature tags which implement feature NAME.
|
|
67 This includes classes which define the interface for NAME as a pure virtual
|
|
68 function."
|
|
69 (python-feature-matches (concat "^" (regexp-quote name) "$")))
|
|
70
|
|
71
|
|
72 (defun python-feature-signature-to-name (signature &optional with-class for-display)
|
|
73 "Extracts the feature name from SIGNATURE.
|
|
74 The feature's class name is dropped from signature unless optional WITH-CLASS
|
|
75 is non-nil. If optional FOR-DISPLAY is non-nil, a feature type character is
|
|
76 prepended to the name for display in a browser listing."
|
|
77 (let ((name))
|
|
78 (cond
|
|
79 ;; member
|
|
80 ((string-match python-tag-fields-regexp signature)
|
|
81 (setq name (substring signature (match-beginning (if for-display 2 3))
|
|
82 (match-end 3)))
|
|
83 (if with-class
|
|
84 (setq name (concat
|
|
85 (substring signature (match-beginning 1) (match-end 1))
|
|
86 "." name)))
|
|
87 ;; Remove any trailing whitespace.
|
|
88 (br-delete-space name))
|
|
89 ;;
|
|
90 ;; unknown
|
|
91 (t ;; Remove any trailing whitespace and add display prefix.
|
|
92 (setq name (br-delete-space signature))
|
|
93 (if for-display (python-feature-add-prefix name "" signature) name)))))
|
|
94
|
|
95 (defun python-feature-tree-command-p (class-or-signature)
|
|
96 "Display definition of CLASS-OR-SIGNATURE if a signature and return t, else return nil."
|
|
97 (if (python-routine-p class-or-signature)
|
|
98 (progn
|
|
99 (if (br-in-browser) (br-to-view-window))
|
|
100 (br-feature-found-p (br-feature-file class-or-signature)
|
|
101 class-or-signature))))
|
|
102
|
|
103 (defun python-list-features (class &optional indent)
|
|
104 "Return sorted list of Python feature tags lexically defined in CLASS."
|
|
105 (let ((obuf (current-buffer))
|
|
106 (features)
|
|
107 (class-tag (concat "\n" class python-type-tag-separator))
|
|
108 feature)
|
|
109 (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
|
|
110 (goto-char 1)
|
|
111 (if (or (null indent) (<= indent 2))
|
|
112 ;; Include all features.
|
|
113 (while (search-forward class-tag nil t)
|
|
114 (setq features (cons (br-feature-current) features)))
|
|
115 ;; Omit friend features which are not inherited since indent > 2.
|
|
116 (let ((friend-regexp (format "%s%% " python-type-tag-separator)))
|
|
117 (while (search-forward class-tag nil t)
|
|
118 (setq feature (br-feature-current))
|
|
119 (or (string-match friend-regexp feature)
|
|
120 (setq features (cons feature features))))))
|
|
121 (set-buffer obuf)
|
|
122 (python-sort-features (nreverse features))))
|
|
123
|
|
124 (defun python-routine-p (str)
|
|
125 (string-match "([^\)]*)" str))
|
|
126
|
|
127 (defun python-scan-features ()
|
|
128 "Return reverse ordered list of Python function definitions in current
|
24
|
129 buffer. Assume point is at the beginning of a widened buffer."
|
0
|
130 (save-excursion
|
|
131 (let ((routines) class name rout)
|
|
132 (while (re-search-forward python-routine-def nil t)
|
24
|
133 (setq class "[function]"
|
0
|
134 name (buffer-substring (match-beginning python-feature-name-grpn)
|
|
135 (match-end python-feature-name-grpn))
|
|
136 rout (python-feature-normalize
|
|
137 (concat "def " name (python-scan-arguments)) class name)
|
|
138 routines (cons rout routines)))
|
|
139 routines)))
|
|
140
|
|
141 (defun python-scan-arguments()
|
|
142 "Return the functions arguments, point is assumed to be at the start of them"
|
|
143 (let ((opoint (point)))
|
|
144 (progn
|
|
145 (search-forward ":" nil t)
|
|
146 (buffer-substring opoint (point)))))
|
|
147
|
|
148 (defun python-sort-features (routine-list)
|
|
149 (sort routine-list 'python-feature-lessp))
|
|
150
|
|
151 (defun python-to-definition (&optional other-win)
|
|
152 "If point is on an import statement, look for the module file.
|
|
153 With OTHER-WIN non-nil, show it in another window."
|
|
154 (interactive)
|
|
155 (let ((opoint (point)))
|
|
156 (cond
|
|
157 ((python-import-file other-win))
|
|
158 (t (beep)
|
|
159 (message
|
|
160 "(OO-Browser): Select a module from import statement display its source.")
|
|
161 nil))))
|
|
162
|
|
163 (defun python-store-class-info (class)
|
|
164 "Lookup Python doc-string for class or method/function"
|
|
165 (setq python-docstring (python-lookup-docstring class)))
|
|
166
|
|
167
|
|
168 (defun python-insert-class-info ()
|
|
169 "Use the info facility to display Python doc-strings"
|
|
170 (interactive)
|
|
171 (insert python-docstring))
|
|
172
|
|
173 ;;; ************************************************************************
|
|
174 ;;; Private functions
|
|
175 ;;; ************************************************************************
|
|
176
|
|
177 (defun python-lookup-docstring (class)
|
|
178 "Looks up a docstring for any browser listing entry."
|
|
179 (let ((entry class)
|
|
180 (filename nil)
|
|
181 (feature-sig nil)
|
|
182 (docstring nil))
|
|
183 (cond ((br-find-feature-entry)
|
|
184 (progn
|
|
185 (setq feature-sig (br-feature-get-signature))
|
|
186 (setq filename (br-feature-file feature-sig))))
|
|
187 ((and (setq entry (br-find-class-name))
|
|
188 (br-class-in-table-p entry))
|
|
189 (setq filename (br-class-path entry)))
|
|
190 (t (error "(OO-Browser): Entry may be referenced but not defined in the Environment.")))
|
|
191 (if filename
|
|
192 (setq docstring
|
|
193 (python-get-docstring-from-source entry feature-sig filename)))
|
|
194 (if docstring
|
|
195 docstring
|
|
196 (concat class " does not have a documentation string."))))
|
|
197
|
|
198 (defun python-get-file-buffer (filename)
|
|
199 "Insert FILENAME contents into a temporary buffer and select buffer.
|
|
200 Does not run any find-file hooks. Marks buffer read-only to prevent
|
|
201 any accidental editing."
|
|
202 (let ((buf (get-buffer-create *br-tmp-buffer*)))
|
|
203 (set-buffer buf)
|
|
204 (buffer-disable-undo buf)
|
|
205 (setq buffer-read-only nil)
|
|
206 (erase-buffer)
|
|
207 (insert-file-contents filename t)))
|
|
208
|
|
209 (defun python-get-docstring-from-source (entry feature-sig filename)
|
|
210 "Scan source for docstring for entry. If feature-sig non nil, locate
|
|
211 feature, otherwise entry is the class"
|
|
212 (let ((no-kill (get-file-buffer filename))
|
|
213 (docstring nil))
|
|
214 (if no-kill
|
|
215 (set-buffer no-kill)
|
|
216 (python-get-file-buffer filename))
|
|
217 (save-restriction
|
|
218 (save-excursion
|
|
219 (widen)
|
|
220 (goto-char (point-min))
|
|
221 (if feature-sig
|
|
222 (if (python-feature-locate-p feature-sig)
|
|
223 (setq docstring (python-extract-docstring))
|
|
224 nil)
|
|
225 (if (re-search-forward (python-class-definition-regexp entry) nil t)
|
|
226 (setq docstring (python-extract-docstring))
|
|
227 nil))))
|
|
228 (if (not no-kill)
|
|
229 (kill-buffer *br-tmp-buffer*))
|
|
230 docstring))
|
|
231
|
|
232 (defun python-extract-docstring ()
|
|
233 "Return the documentation string for the class or method at point, or
|
|
234 nil if it does not exist"
|
|
235 (search-forward ":" nil t)
|
|
236 (if (looking-at
|
|
237 (concat python-empty-line "+"
|
|
238 whitespace python-string-start))
|
|
239 (progn
|
|
240 (let ((start (match-end 0))
|
|
241 (end-quote (buffer-substring (match-beginning 4) (match-end 4))))
|
|
242 (goto-char start)
|
|
243 (search-forward end-quote nil t)
|
|
244 (buffer-substring start (match-beginning 0))))
|
|
245 nil))
|
|
246
|
|
247 (defconst python-string-start
|
|
248 (concat
|
|
249 "\\("
|
|
250 "'''" ; triple single-quoted
|
|
251 "\\|" ; or
|
|
252 "\"\"\"" ; triple double-quoted
|
|
253 "\\|" ; or
|
|
254 "'" ; single-quoted, not empty
|
|
255 "\\|" ; or
|
|
256 "\"" ; double-quoted, not empty
|
|
257 "\\)")
|
|
258 "regexp matching python string literal starting quotes")
|
|
259
|
|
260 (defconst python-empty-line
|
|
261 (concat
|
|
262 "\\("
|
|
263 "\\(" whitespace "\n\\)"
|
|
264 "\\|"
|
|
265 "\\(" whitespace "#.*$\\)"
|
|
266 "\\)")
|
|
267 "regexp matching an empty python line, which can be a comment line")
|
|
268
|
|
269 (defun python-feature-decl ()
|
|
270 (if (looking-at python-class-decl)
|
|
271 nil
|
|
272 (looking-at python-feature-decl)))
|
|
273
|
|
274 (defun py-count-triple-quotes-forward ()
|
|
275 "Count the number of trible quotes from the point to eof"
|
|
276 (let ((count 0))
|
|
277 (while (re-search-forward "'''\\|\"\"\"" nil t)
|
|
278 (setq count (1+ count)))
|
|
279 count))
|
|
280
|
|
281 (defun python-within-string-p ()
|
|
282 "Return non-nil if point is within a multi-line python string."
|
|
283 (save-excursion
|
|
284 (if (= (% (py-count-triple-quotes-forward) 2) 1)
|
|
285 t
|
|
286 nil)))
|
|
287
|
|
288 (defun python-feature-lessp (routine1 routine2)
|
|
289 (string-lessp (python-feature-signature-to-name routine1)
|
|
290 (python-feature-signature-to-name routine2)))
|
|
291
|
|
292 (defun python-feature-matches (regexp)
|
|
293 "Return an unsorted list of feature tags whose names match in part or whole to REGEXP."
|
|
294 ;; Ensure match to feature names only; also handle "^" and "$" meta-chars
|
|
295 (setq regexp
|
|
296 (concat (format "^[^%s \n]+%s%s "
|
|
297 python-type-tag-separator python-type-tag-separator
|
|
298 br-feature-type-regexp)
|
|
299 (if (equal (substring regexp 0 1) "^")
|
|
300 (progn (setq regexp (substring regexp 1)) nil)
|
|
301 python-identifier-chars)
|
|
302 (if (equal (substring regexp -1) "$")
|
|
303 (substring regexp 0 -1)
|
|
304 (concat regexp python-identifier-chars))
|
|
305 python-type-tag-separator))
|
|
306 (save-excursion
|
|
307 (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
|
|
308 (goto-char 1)
|
|
309 (let ((features))
|
|
310 (while (re-search-forward regexp nil t)
|
|
311 (setq features (cons (br-feature-current) features)))
|
|
312 features)))
|
|
313
|
|
314 (defun python-feature-normalize (routine class name)
|
|
315 (setq class (br-delete-space class)
|
|
316 name (concat "- " name)
|
|
317 routine (concat class python-type-tag-separator
|
|
318 name python-type-tag-separator
|
|
319 (br-delete-space routine)))
|
|
320 routine)
|
|
321
|
|
322 (defun python-feature-tag-class (signature)
|
|
323 "Extract the class name from SIGNATURE."
|
|
324 (cond ((string-match python-type-tag-separator signature)
|
|
325 (substring signature 0 (match-beginning 0)))
|
|
326 (t "")))
|
|
327
|
|
328 (defun python-feature-tags-lookup (class-list ftr-pat &optional other-win)
|
|
329 "Display routine definition derived from CLASS-LIST, matching FTR-PAT.
|
|
330 Use routine tags table to locate a match. Caller must use 'set-buffer'
|
|
331 to restore prior buffer when a match is not found."
|
|
332 (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
|
|
333 (let ((classes class-list)
|
|
334 (found-ftr)
|
|
335 (ftr-regexp)
|
|
336 (class)
|
|
337 (ftr-path))
|
|
338 (if (or (null class-list) (equal class-list '(nil)))
|
|
339 nil
|
|
340 (while (and (not found-ftr) classes)
|
|
341 (setq class (car classes)
|
|
342 ftr-regexp (funcall ftr-pat class)
|
|
343 ftr-path (br-feature-def-file ftr-regexp)
|
|
344 found-ftr (if ftr-path
|
|
345 (br-edit-feature (br-feature-current)
|
|
346 ftr-path other-win))
|
|
347 classes (if found-ftr nil (cdr classes))))
|
|
348 (if found-ftr
|
|
349 (or class t)
|
|
350 (python-feature-tags-lookup
|
|
351 (apply 'append (mapcar (function (lambda (cl) (br-get-parents cl)))
|
|
352 class-list))
|
|
353 ftr-pat)))))
|
|
354
|
|
355 (defun python-files-with-source (class)
|
|
356 "Use CLASS to compute set of files that match to a Python source file regexp.
|
|
357 Return as a list."
|
|
358 (let ((file (if class (br-class-path class) buffer-file-name)))
|
|
359 (and file
|
|
360 (let* ((src-file-regexp (concat "^" (br-filename-head file)
|
|
361 python-code-file-regexp))
|
|
362 (dir (file-name-directory file))
|
|
363 (files (directory-files dir nil src-file-regexp)))
|
|
364 (mapcar (function (lambda (f) (concat dir f)))
|
|
365 files)))))
|
|
366
|
|
367 (defun python-find-ancestors-feature (class-list ftr-pat &optional other-win)
|
|
368 "Scan ancestors of CLASS-LIST and show routine definition matching FTR-PAT."
|
|
369 ;; If no class, search for non-member function.
|
|
370 (or class-list (setq class-list '(nil)))
|
|
371 (let ((obuf (current-buffer)))
|
|
372 (prog1
|
|
373 (if (and br-feature-tags-file
|
|
374 (file-exists-p br-feature-tags-file)
|
|
375 (file-readable-p br-feature-tags-file))
|
|
376 (python-feature-tags-lookup class-list ftr-pat other-win)
|
|
377 ;; Only works if features are in same directory as class def.
|
|
378 (python-scan-ancestors-feature class-list ftr-pat other-win))
|
|
379 (set-buffer obuf))))
|
|
380
|
|
381 (defun python-find-class-name ()
|
|
382 "Return current word as a potential class name."
|
|
383 (save-excursion
|
|
384 (let* ((start)
|
|
385 (ignore "\]\[ \t\n;,.\(\){}*&-")
|
|
386 (pat (concat "^" ignore)))
|
|
387 (forward-char 1)
|
|
388 (skip-chars-backward ignore)
|
|
389 (skip-chars-backward pat)
|
|
390 (setq start (point))
|
|
391 (skip-chars-forward (concat pat ":"))
|
|
392 (buffer-substring start (point)))))
|
|
393
|
|
394
|
|
395 (defun python-get-class-name-from-source ()
|
|
396 "Return class name from closest class definition preceding point or nil."
|
|
397 (let ((opoint (point))
|
|
398 (class))
|
|
399 (save-excursion
|
|
400 (if (re-search-backward python-class-def-regexp nil t)
|
|
401 (progn (goto-char (match-beginning python-class-def-derived-grpn))
|
|
402 (setq class (python-normalize-class-match nil))
|
|
403 ;; Ensure that declaration occurs within class definition.
|
|
404 (forward-list)
|
|
405 (and (> (point) opoint)
|
|
406 class))))))
|
|
407
|
|
408 (defun python-get-feature-tags (routine-file &optional routine-list)
|
|
409 "Scan Python ROUTINE-FILE and hold routine tags in 'br-feature-tags-file'.
|
|
410 Assume ROUTINE-FILE has already been read into a buffer and that
|
|
411 'br-feature-tags-init' has been called. Optional ROUTINE-LIST can be
|
|
412 provided so that a non-standard scan function can be used before calling
|
|
413 this function."
|
|
414 (interactive)
|
|
415 (let ((obuf (current-buffer)))
|
|
416 (or routine-list
|
|
417 (setq routine-list (python-sort-features (nreverse
|
|
418 (python-scan-features)))))
|
|
419 (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
|
|
420 (goto-char 1)
|
|
421 ;; Delete any prior routine tags associated with routine-file
|
|
422 (if (search-forward routine-file nil 'end)
|
|
423 (progn (forward-line -1)
|
|
424 (let ((start (point)))
|
|
425 (search-forward "\^L" nil 'end 2)
|
|
426 (backward-char 1)
|
|
427 (delete-region start (point))
|
|
428 )))
|
|
429 (if routine-list
|
|
430 (progn (insert "\^L\n" routine-file "\n")
|
|
431 (mapcar (function (lambda (tag) (insert tag "\n")))
|
|
432 routine-list)
|
|
433 ))
|
|
434 (set-buffer obuf)))
|
|
435
|
|
436 (defun python-find-module-name ()
|
|
437 "Return current word as a potential module name."
|
|
438 (save-excursion
|
|
439 (let ((start))
|
|
440 (forward-char 1)
|
|
441 (skip-chars-backward python-identifier-chars)
|
|
442 (setq start (point))
|
|
443 (skip-chars-forward python-identifier-chars)
|
|
444 (buffer-substring start (point)))))
|
|
445
|
|
446 (defun python-import-file (&optional other-win)
|
|
447 "If point is on an import module line, try to display module.
|
|
448 Return non-nil iff an import file line, even if file is not found.
|
|
449 Look for include file in directory list 'python-import-dirs'"
|
|
450 (let ((opoint (point)))
|
|
451 (beginning-of-line)
|
|
452 (if (and (looking-at python-import-regexp)
|
|
453 (goto-char opoint))
|
|
454 (let ((file (concat (python-find-module-name) ".py"))
|
|
455 (path)
|
|
456 (dir-list (append python-lib-search-dirs python-sys-search-dirs
|
|
457 python-import-dirs))
|
|
458 (found))
|
|
459 (setq dir-list (cons (file-name-directory buffer-file-name)
|
|
460 dir-list))
|
|
461 (while dir-list
|
|
462 (setq path (concat (car dir-list) file)
|
|
463 dir-list (if (setq found (file-exists-p path))
|
|
464 nil
|
|
465 (cdr dir-list))))
|
|
466 ;;
|
|
467 ;; If not found in normal include dirs, check all Env paths also.
|
|
468 ;;
|
|
469 (if (not found)
|
|
470 (let ((paths (delq nil (hash-map 'cdr br-paths-htable))))
|
|
471 (while paths
|
|
472 (setq path (car paths))
|
|
473 (if (string-equal (file-name-nondirectory path) file)
|
|
474 (setq found t paths nil)
|
|
475 (setq paths (cdr paths))))))
|
|
476 ;;
|
|
477 ;; If found, display file
|
|
478 ;;
|
|
479 (if found
|
|
480 (if (file-readable-p path)
|
|
481 (progn
|
|
482 (funcall br-edit-file-function path other-win)
|
|
483 (if (not (fboundp 'br-lang-mode))
|
|
484 (python-mode-setup))
|
|
485 (br-major-mode))
|
|
486 (beep)
|
|
487 (message "(OO-Browser): Module file '%s' unreadable." path))
|
|
488 (beep)
|
|
489 (message "(OO-Browser): Module file '%s' not found." file))
|
|
490 path)
|
|
491 (goto-char opoint)
|
|
492 nil)))
|
|
493
|
|
494 (defun python-locate-feature (ftr class ftr-pat &optional other-win)
|
|
495 ;; 'class' may = nil, implying non-member function
|
24
|
496 (or class (setq class "[function]"))
|
0
|
497 (let ((def-class))
|
|
498 (if (and ftr-pat
|
|
499 (setq def-class
|
|
500 (python-find-ancestors-feature (list class)
|
|
501 ftr-pat other-win)))
|
|
502 (progn (if (and class (not (equal class def-class)))
|
|
503 (message
|
|
504 "Member `%s` of class '%s' inherited from class '%s'."
|
|
505 ftr class def-class))
|
|
506 t))))
|
|
507
|
|
508 (defun python-scan-ancestors-feature (class-list ftr-pat &optional other-win)
|
|
509 "Display routine definition derived from CLASS-LIST, matching FTR-PAT.
|
|
510 Scan files with same base name as class file."
|
|
511 (let ((classes class-list)
|
|
512 (found-ftr)
|
|
513 (code-def-files)
|
|
514 (file)
|
|
515 (ftr-regexp)
|
|
516 (class))
|
|
517 (if (null class-list)
|
|
518 nil
|
|
519 (while (and (not found-ftr) classes)
|
|
520 (setq class (car classes)
|
|
521 code-def-files (python-files-with-source class)
|
|
522 ftr-regexp (funcall ftr-pat class))
|
|
523 (while (and (setq file (car code-def-files))
|
|
524 (not (setq found-ftr
|
|
525 (br-feature-found-p file ftr-regexp
|
|
526 nil other-win t))))
|
|
527 (setq code-def-files (cdr code-def-files)))
|
|
528 (setq classes (if found-ftr nil (cdr classes))))
|
|
529 (if found-ftr
|
|
530 (or class t)
|
|
531 (python-scan-ancestors-feature
|
|
532 (apply 'append (mapcar (function (lambda (cl) (br-get-parents cl)))
|
|
533 class-list))
|
|
534 ftr-pat)))))
|
|
535
|
|
536 (defun python-scan-features-in-class (class start end)
|
|
537 "Return reverse ordered list of Python routine definitions within CLASS def.
|
|
538 START and END give buffer region to search."
|
|
539 (setq class (br-delete-space class))
|
|
540 (save-excursion
|
|
541 (save-restriction
|
|
542 (narrow-to-region start end)
|
|
543 (goto-char start)
|
|
544 (let ((routines) rout name type)
|
|
545 ;;
|
|
546 ;; Get member definitions
|
|
547 ;;
|
|
548 (while (re-search-forward python-routine-def-in-class nil t)
|
|
549 (setq start (match-beginning 0)
|
|
550 name (buffer-substring
|
|
551 (match-beginning python-feature-name-grpn)
|
|
552 (match-end python-feature-name-grpn))
|
|
553 rout (python-feature-normalize
|
|
554 (concat "def " name (python-scan-arguments)) class name)
|
|
555 routines (cons rout routines)))
|
|
556 routines))))
|
|
557
|
|
558 (defun python-skip-past-comments ()
|
|
559 "Skip over comments immediately following point."
|
|
560 (skip-chars-forward " \t\n")
|
|
561 (while
|
|
562 (cond ((looking-at "#")
|
|
563 (equal (forward-line 1) 0))
|
|
564 (t nil))))
|
|
565
|
|
566 (defun python-skip-to-statement ()
|
|
567 (if (re-search-backward "^[ \t]*" nil t)
|
|
568 (progn (goto-char (match-end 0))
|
|
569 (skip-chars-forward " \t")
|
|
570 t)))
|
|
571
|
|
572 (defun python-feature-locate-p (feature-tag &optional regexp-flag)
|
|
573 "Leaves point at the start of FEATURE-TAG's definition in the current buffer.
|
|
574 Assumes caller has moved point to the beginning of the buffer or to the point
|
|
575 of desired search start.
|
|
576 Optional REGEXP-FLAG means FEATURE-TAG is a regular expression."
|
|
577 ;;
|
|
578 ;; first move to the proper class implementation if feature-tag does not
|
|
579 ;; include a <class>:: part and this is not a [default-class], so that if
|
|
580 ;; two classes in the same file have the same feature signature, we still
|
|
581 ;; end up at the right one.
|
|
582 (if (string-match python-tag-fields-regexp feature-tag)
|
|
583 (let ((class (substring feature-tag (match-beginning 1) (match-end 1))))
|
|
584 (setq feature-tag (substring feature-tag (match-end 0)))
|
|
585 (if regexp-flag
|
|
586 (if (not (string-match "\\`\\\\\\[\\|::" feature-tag))
|
|
587 (re-search-forward (python-class-definition-regexp class t)
|
|
588 nil t))
|
|
589 (if (not (string-match "\\`\\[\\|::" feature-tag))
|
|
590 (re-search-forward (python-class-definition-regexp class)
|
|
591 nil t)))))
|
|
592 (let ((found) (start))
|
|
593 ;; Now look for feature expression.
|
|
594 (or regexp-flag (setq feature-tag
|
|
595 (python-feature-signature-to-regexp feature-tag)))
|
|
596 (while (and (re-search-forward feature-tag nil t)
|
|
597 (setq start (match-beginning 0))
|
|
598 (not (setq found (not
|
|
599 (if (python-within-string-p)
|
|
600 (progn (search-forward "*/" nil t)
|
|
601 t)))))))
|
|
602 (if found
|
|
603 (progn (goto-char start)
|
|
604 (skip-chars-forward " \t\n")
|
|
605 (python-to-comments-begin)
|
|
606 (recenter 0)
|
|
607 (goto-char start)
|
|
608 t))))
|
|
609
|
|
610 (defun python-feature-name-to-regexp (name)
|
|
611 "Converts routine NAME into a regular expression matching the routine's name tag."
|
|
612 (setq name (python-feature-signature-to-regexp name))
|
|
613 (aset name (1- (length name)) ?\() ;; Match only to functions
|
|
614 name)
|
|
615
|
|
616
|
|
617 (defun python-feature-signature-to-regexp (signature)
|
|
618 "Given a Python SIGNATURE, return regexp used to match to its definition."
|
|
619 (setq signature (regexp-quote signature))
|
|
620 (let ((prefix-info
|
|
621 (if (string-match python-tag-fields-regexp signature)
|
|
622 (prog1 (substring signature (match-beginning 0) (match-end 0))
|
|
623 (setq signature (substring signature (match-end 0)))))))
|
|
624 (let ((pat) (i 0) (c) (len (length signature)))
|
|
625 (while (< i len)
|
|
626 (setq c (aref signature i)
|
|
627 pat (cond ((= c ? )
|
|
628 ;; Allow for possible single line comment
|
|
629 ;; following any whitespace, e.g. following
|
|
630 ;; each routine argument.
|
|
631 (concat pat "[ \t\n\^M]*\\(//.*\\)?"))
|
|
632 (t
|
|
633 (concat pat (char-to-string c))))
|
|
634 i (1+ i)))
|
|
635 (setq pat (concat prefix-info pat)))))
|
|
636
|
|
637
|
|
638
|
|
639
|
|
640 ;;; ************************************************************************
|
|
641 ;;; Private variables
|
|
642 ;;; ************************************************************************
|
|
643
|
|
644 (defvar python-docstring ""
|
|
645 "Documentation string for python class, method or function.")
|
|
646
|
|
647 (defconst python-code-file-regexp "\\.py\\"
|
|
648 "Regular expression matching a unique part of Python source (non-header) file name and no others.")
|
|
649
|
|
650 (defconst python-import-regexp
|
|
651 (concat "\\([ \t]*import[ \t]+\\)\\|"
|
|
652 "\\([ \t]*from[ \t]+"
|
|
653 python-identifier
|
|
654 "[ \t]+import[ \t]+\\)")
|
|
655 "Regexp to match to Python import statement
|
|
656 of include, user-specified via double quote, or system-related starting with
|
|
657 '<' is given by grouping 1.")
|
|
658
|
|
659 (defconst python-feature-name-grpn 1)
|
|
660
|
|
661 (defconst python-routine-def
|
|
662 (concat "^def[ \t]+" python-identifier whitespace)
|
|
663 "Matches global python function definition. group 1 gives the function name.
|
|
664 On return the point is at the starting '(' for parameters")
|
|
665
|
|
666 (defconst python-routine-def-in-class
|
|
667 (concat "^[ \t]+def[ \t]+" python-identifier)
|
|
668 "Matches python class method. group 1 gives the function name.
|
|
669 On return the point is at the starting '(' for parameters")
|
|
670
|
|
671 (defconst python-decl-template-grpn 3)
|
|
672 (defconst python-class-name-grpn 5)
|
|
673
|
|
674 (defconst python-stringlit
|
|
675 (concat
|
|
676 "'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
|
|
677 "\\|" ; or
|
|
678 "\"\\([^\"\n\\]\\|\\\\.\\)*\"") ; double-quoted
|
|
679 "regexp matching a Python string literal")
|
|
680
|
|
681
|
|
682 (provide 'br-python-ft)
|