1 ;;; semantic/texi.el --- Semantic details for Texinfo files
3 ;;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
4 ;;; Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Parse Texinfo buffers using regular expressions. The core parser
26 ;; engine is the function `semantic-texi-parse-headings'. The
27 ;; parser plug-in is the function `semantic-texi-parse-region' that
28 ;; overrides `semantic-parse-region'.
31 (require 'semantic
/format
)
35 (require 'semantic
/db
)
36 (require 'semantic
/db-find
)
37 (require 'semantic
/ctxt
)
38 (require 'semantic
/find
)
39 ;; (require 'semantic/imenu)
40 (require 'semantic
/doc
)
44 (declare-function lookup-words
"ispell")
46 (defvar semantic-texi-super-regex
47 "^@\\(top\\|chapter\\|\\(sub\\)*section\\|unnumbered\\(\\(sub\\)*sec\\)?\\|\
48 \\(chap\\|\\(sub\\)+\\|major\\)?heading\\|appendix\\(\\(sub\\)*sec\\)?\\|\
49 centerchap\\|def\\(var\\|un\\|fn\\|opt\\)x?\\)"
50 "Regular expression used to find special sections in a Texinfo file.")
52 (defvar semantic-texi-name-field-list
61 "List of definition commands, and the field position.
62 The field position is the field number (based at 1) where the
63 name of this section is.")
66 (defun semantic-texi-parse-region (&rest ignore
)
67 "Parse the current texinfo buffer for semantic tags.
68 IGNORE any arguments, always parse the whole buffer.
69 Each tag returned is of the form:
70 (\"NAME\" section (:members CHILDREN))
74 It is an override of 'parse-region and must be installed by the
75 function `semantic-install-function-overrides'."
76 (mapcar 'semantic-texi-expand-tag
77 (semantic-texi-parse-headings)))
79 (defun semantic-texi-parse-changes ()
80 "Parse changes in the current texinfo buffer."
81 ;; NOTE: For now, just schedule a full reparse.
82 ;; To be implemented later.
83 (semantic-parse-tree-set-needs-rebuild))
85 (defun semantic-texi-expand-tag (tag)
86 "Expand the texinfo tag TAG."
87 (let ((chil (semantic-tag-components tag
)))
89 (semantic-tag-put-attribute
90 tag
:members
(mapcar 'semantic-texi-expand-tag chil
)))
91 (car (semantic--tag-expand tag
))))
93 (defun semantic-texi-parse-headings ()
94 "Parse the current texinfo buffer for all semantic tags now."
96 ;; First search and snarf.
98 (goto-char (point-min))
99 (let ((semantic--progress-reporter
100 (make-progress-reporter
101 (format "Parsing %s..."
102 (file-name-nondirectory buffer-file-name
))
103 (point-min) (point-max))))
104 (while (re-search-forward semantic-texi-super-regex nil t
)
105 (setq pass1
(cons (match-beginning 0) pass1
))
106 (progress-reporter-update semantic--progress-reporter
(point)))
107 (progress-reporter-done semantic--progress-reporter
)))
108 (setq pass1
(nreverse pass1
))
109 ;; Now, make some tags while creating a set of children.
110 (car (semantic-texi-recursive-combobulate-list pass1
0))
113 (defsubst semantic-texi-new-section-tag
(name members start end
)
114 "Create a semantic tag of class section.
115 NAME is the name of this section.
116 MEMBERS is a list of semantic tags representing the elements that make
118 START and END define the location of data described by the tag."
119 (append (semantic-tag name
'section
:members members
)
122 (defsubst semantic-texi-new-def-tag
(name start end
)
123 "Create a semantic tag of class def.
124 NAME is the name of this definition.
125 START and END define the location of data described by the tag."
126 (append (semantic-tag name
'def
)
129 (defun semantic-texi-set-endpoint (metataglist pnt
)
130 "Set the end point of the first section tag in METATAGLIST to PNT.
131 METATAGLIST is a list of tags in the intermediate tag format used by the
132 texinfo parser. PNT is the new point to set."
134 (while (and metataglist
135 (not (eq (semantic-tag-class (car metataglist
)) 'section
)))
136 (setq metataglist
(cdr metataglist
)))
137 (setq metatag
(car metataglist
))
139 (setcar (nthcdr (1- (length metatag
)) metatag
) pnt
)
142 (defun semantic-texi-recursive-combobulate-list (sectionlist level
)
143 "Rearrange SECTIONLIST to be a hierarchical tag list starting at LEVEL.
144 Return the rearranged new list, with all remaining tags from
145 SECTIONLIST starting at ELT 2. Sections not are not dealt with as soon as a
146 tag with greater section value than LEVEL is found."
154 (goto-char (car oldl
))
155 (if (looking-at "@\\(\\w+\\)")
156 (let* ((word (match-string 1))
157 (levelmatch (assoc word texinfo-section-list
))
160 ;; Set begin to the right location
162 ;; Get out of here if there if we made it that far.
163 (if (and levelmatch
(<= (car (cdr levelmatch
)) level
))
166 (semantic-texi-set-endpoint newl begin
))
167 (throw 'level-jump t
)))
170 (let ((end (match-end 1)))
171 ;; Levels sometimes have a @node just in front.
172 ;; That node statement should be included in the space
175 (skip-chars-backward "\n \t")
177 (when (looking-at "@node\\>")
178 (setq begin
(point))))
179 ;; When there is a match, the descriptive text
180 ;; consists of the rest of the line.
182 (skip-chars-forward " \t")
183 (setq text
(buffer-substring-no-properties
185 (progn (end-of-line) (point))))
186 ;; Next, recurse into the body to find the end.
187 (setq tmp
(semantic-texi-recursive-combobulate-list
188 (cdr oldl
) (car (cdr levelmatch
))))
190 (setq tag
(semantic-texi-new-section-tag
191 text
(car tmp
) begin
(point)))
192 ;; Before appending the newtag, update the previous tag
193 ;; if it is a section tag.
195 (semantic-texi-set-endpoint newl begin
))
196 ;; Append new tag to our master list.
197 (setq newl
(cons tag newl
))
199 (setq oldl
(cdr tmp
))
201 ;; No match means we have a def*, so get the name from
202 ;; it based on the type of thingy we found.
203 (setq levelmatch
(assoc word semantic-texi-name-field-list
)
204 tmp
(or (cdr levelmatch
) 1))
206 (skip-chars-forward " \t")
207 (setq text
(buffer-substring-no-properties
209 (progn (forward-sexp 1) (point))))
210 ;; Seek the end of this definition
212 (semantic-texi-forward-deffn)
213 (setq tag
(semantic-texi-new-def-tag text begin
(point))
214 newl
(cons tag newl
))
216 (setq oldl
(cdr oldl
)))
218 (error "Problem finding section in semantic/texi parser"))
219 ;; (setq oldl (cdr oldl))
221 ;; When oldl runs out, force a new endpoint as point-max
223 (semantic-texi-set-endpoint newl
(point-max)))
225 (cons (nreverse newl
) oldl
)))
227 (defun semantic-texi-forward-deffn ()
228 "Move forward over one deffn type definition.
229 The cursor should be on the @ sign."
230 (when (looking-at "@\\(\\w+\\)")
231 (let* ((type (match-string 1))
232 (seek (concat "^@end\\s-+" (regexp-quote type
))))
233 (re-search-forward seek nil t
))))
235 (define-mode-local-override semantic-tag-components
237 "Return components belonging to TAG."
238 (semantic-tag-get-attribute tag
:members
))
241 ;;; Overrides: Context Parsing
243 ;; How to treat texi as a language?
245 (defvar semantic-texi-environment-regexp
246 (if (string-match texinfo-environment-regexp
"@menu")
247 ;; Make sure our Emacs has menus in it.
248 texinfo-environment-regexp
249 ;; If no menus, then merge in the menu concept.
250 (when (string-match "cartouche" texinfo-environment-regexp
)
251 (concat (substring texinfo-environment-regexp
252 0 (match-beginning 0))
254 (substring texinfo-environment-regexp
255 (match-beginning 0)))))
256 "Regular expression for matching texinfo enviroments.
257 uses `texinfo-environment-regexp', but makes sure that it
258 can handle the @menu environment.")
260 (define-mode-local-override semantic-up-context texinfo-mode
()
261 "Handle texinfo constructs which do not use parenthetical nesting."
264 (let ((parenthetical (semantic-up-context-default))
266 (when (not parenthetical
)
267 ;; We are in parenthises. Are they the types of parens
268 ;; belonging to a texinfo construct?
270 (when (looking-at "@\\w+{")
271 (setq done
(point))))))
272 ;; If we are not in a parenthetical node, then find a block instead.
273 ;; Use the texinfo support to find block start/end constructs.
275 (while (and (not done
)
276 (re-search-backward semantic-texi-environment-regexp nil t
))
277 ;; For any hit, if we find an @end foo, then jump to the
278 ;; matching @foo. If it is not an end, then we win!
279 (if (not (looking-at "@end\\s-+\\(\\w+\\)"))
281 ;; Skip over this block
282 (let ((env (match-string 1)))
283 (re-search-backward (concat "@" env
))))
285 ;; All over, post what we find.
287 ;; We found something, so use it.
288 (progn (goto-char done
)
292 (define-mode-local-override semantic-beginning-of-context texinfo-mode
(&optional point
)
293 "Move to the beginning of the context surrounding POINT."
294 (if (semantic-up-context point
)
295 ;; If we can't go up, we can't do this either.
297 ;; We moved, so now we need to skip into whatever this thing is.
298 (forward-word 1) ;; skip the command
299 (if (looking-at "\\s-*{")
300 ;; In a short command. Go in.
302 ;; An environment. Go to the next line.
307 (define-mode-local-override semantic-ctxt-current-class-list
308 texinfo-mode
(&optional point
)
309 "Determine the class of tags that can be used at POINT.
310 For texinfo, there two possibilities returned.
311 1) 'function - for a call to a texinfo function
312 2) 'word - indicates an english word.
313 It would be nice to know function arguments too, but not today."
314 (let ((sym (semantic-ctxt-current-symbol)))
315 (if (and sym
(= (aref (car sym
) 0) ?
@))
320 ;;; Overrides : Formatting
322 ;; Various override to better format texi tags.
325 (define-mode-local-override semantic-format-tag-abbreviate
326 texinfo-mode
(tag &optional parent color
)
327 "Texinfo tags abbreviation."
328 (let ((class (semantic-tag-class tag
))
329 (name (semantic-format-tag-name tag parent color
))
331 (cond ((eq class
'function
)
333 (t (semantic-format-tag-abbreviate-default tag parent color
)))
336 (define-mode-local-override semantic-format-tag-prototype
337 texinfo-mode
(tag &optional parent color
)
338 "Texinfo tags abbreviation."
339 (semantic-format-tag-abbreviate tag parent color
))
342 ;;; Texi Unique Features
344 (defun semantic-tag-texi-section-text-bounds (tag)
345 "Get the bounds to the text of TAG.
346 The text bounds is the text belonging to this node excluding
347 the text of any child nodes, but including any defuns."
348 (let ((memb (semantic-tag-components tag
)))
349 ;; Members.. if one is a section, check it out.
350 (while (and memb
(not (semantic-tag-of-class-p (car memb
) 'section
)))
351 (setq memb
(cdr memb
)))
352 ;; No members? ... then a simple problem!
354 (semantic-tag-bounds tag
)
355 ;; Our end is their beginning...
356 (list (semantic-tag-start tag
) (semantic-tag-start (car memb
))))))
358 (defun semantic-texi-current-environment (&optional point
)
359 "Return as a string the type of the current environment.
360 Optional argument POINT is where to look for the environment."
362 (when point
(goto-char (point)))
363 (while (and (or (not (looking-at semantic-texi-environment-regexp
))
365 (not (semantic-up-context)))
367 (when (looking-at semantic-texi-environment-regexp
)
374 (require 'semantic
/analyze
))
376 (define-mode-local-override semantic-analyze-current-context
378 "Analysis context makes no sense for texinfo. Return nil."
379 (let* ((prefixandbounds (semantic-ctxt-current-symbol-and-bounds (point)))
380 (prefix (car prefixandbounds
))
381 (bounds (nth 2 prefixandbounds
))
382 (prefixclass (semantic-ctxt-current-class-list))
385 (require 'semantic
/analyze
)
386 (semantic-analyze-context
387 "Context-for-texinfo"
388 :buffer
(current-buffer)
393 :prefixclass prefixclass
)
397 (defvar semantic-texi-command-completion-list
398 (append (mapcar (lambda (a) (car a
)) texinfo-section-list
)
402 ;; XEmacs doesn't use the above. Split up its regexp
403 (split-string texinfo-environment-regexp
"\\\\|\\|\\^@\\\\(\\|\\\\)")
405 ;; Is there a better list somewhere? Here are few
406 ;; of the top of my head.
410 "defun" "deffn" "defoption" "defvar" "dfn"
412 "ifinfo" "iftex" "inforef" "item" "itemx"
416 "set" "setfilename" "settitle"
420 "List of commands that we might bother completing.")
422 (define-mode-local-override semantic-analyze-possible-completions
423 texinfo-mode
(context)
424 "List smart completions at point.
425 Since texinfo is not a programming language the default version is not
426 useful. Insted, look at the current symbol. If it is a command
427 do primitive texinfo built ins. If not, use ispell to lookup words
428 that start with that symbol."
429 (let ((prefix (car (oref context
:prefix
)))
431 (cond ((member 'function
(oref context
:prefixclass
))
432 ;; Do completion for texinfo commands
433 (let* ((cmd (substring prefix
1))
434 (lst (all-completions
435 cmd semantic-texi-command-completion-list
)))
436 (mapcar (lambda (f) (semantic-tag (concat "@" f
) 'function
))
439 ((member 'word
(oref context
:prefixclass
))
440 ;; Do completion for words via ispell.
442 (let ((word-list (lookup-words prefix
)))
443 (mapcar (lambda (f) (semantic-tag f
'word
)) word-list
))
451 (defun semantic-default-texi-setup ()
452 "Set up a buffer for parsing of Texinfo files."
453 ;; This will use our parser.
454 (semantic-install-function-overrides
455 '((parse-region . semantic-texi-parse-region
)
456 (parse-changes . semantic-texi-parse-changes
)))
457 (setq semantic-parser-name
"TEXI"
458 ;; Setup a dummy parser table to enable parsing!
459 semantic--parse-table t
460 imenu-create-index-function
'semantic-create-imenu-index
461 semantic-command-separation-character
"@"
462 semantic-type-relation-separator-character
'(":")
463 semantic-symbol-
>name-assoc-list
'((section .
"Section")
466 semantic-imenu-expandable-tag-classes
'(section)
467 semantic-imenu-bucketize-file nil
468 semantic-imenu-bucketize-type-members nil
469 senator-step-at-start-end-tag-classes
'(section)
470 semantic-stickyfunc-sticky-classes
'(section)
472 (local-set-key [(f9)] 'semantic-texi-update-doc-from-texi
)
475 (add-hook 'texinfo-mode-hook
'semantic-default-texi-setup
)
478 ;;; Special features of Texinfo tag streams
480 ;; This section provides specialized access into texinfo files.
481 ;; Because texinfo files often directly refer to functions and programs
482 ;; it is useful to access the texinfo file from the C code for document
484 (defun semantic-texi-associated-files (&optional buffer
)
485 "Find texinfo files associated with BUFFER."
487 (if buffer
(set-buffer buffer
))
488 (cond ((and (fboundp 'ede-documentation-files
)
489 ede-minor-mode
(ede-current-project))
490 ;; When EDE is active, ask it.
491 (ede-documentation-files)
493 ((and (featurep 'semantic
/db
) (semanticdb-minor-mode-p))
494 ;; See what texinfo files we have loaded in the database
495 (let ((tabs (semanticdb-get-database-tables
496 semanticdb-current-database
))
499 (if (eq (oref (car tabs
) major-mode
) 'texinfo-mode
)
500 (setq r
(cons (oref (car tabs
) file
) r
)))
501 (setq tabs
(cdr tabs
)))
504 (directory-files default-directory nil
"\\.texi$"))
507 ;; Turns out this might not be useful.
508 ;; Delete later if that is true.
509 (defun semantic-texi-find-documentation (name &optional type
)
510 "Find the function or variable NAME of TYPE in the texinfo source.
511 NAME is a string representing some functional symbol.
512 TYPE is a string, such as \"variable\" or \"Command\" used to find
513 the correct definition in case NAME qualifies as several things.
514 When this function exists, POINT is at the definition.
515 If the doc was not found, an error is thrown.
516 Note: TYPE not yet implemented."
517 (let ((f (semantic-texi-associated-files))
519 (while (and f
(not match
))
521 (with-current-buffer (find-file-noselect (car f
))
522 (setq stream
(semantic-fetch-tags))))
523 (setq match
(semantic-find-first-tag-by-name name stream
))
525 (set-buffer (semantic-tag-buffer match
))
526 (goto-char (semantic-tag-start match
)))
529 (defun semantic-texi-update-doc-from-texi (&optional tag
)
530 "Update the documentation in the texinfo deffn class tag TAG.
531 The current buffer must be a texinfo file containing TAG.
532 If TAG is nil, determine a tag based on the current position."
534 (unless (or (featurep 'semantic
/db
) (semanticdb-minor-mode-p))
535 (error "Texinfo updating only works when `semanticdb' is being used"))
536 (semantic-fetch-tags)
539 (setq tag
(semantic-current-tag)))
540 (unless (semantic-tag-of-class-p tag
'def
)
541 (error "Only deffns (or defun or defvar) can be updated"))
542 (let* ((name (semantic-tag-name tag
))
543 (tags (semanticdb-strip-find-results
544 (semanticdb-with-match-any-mode
545 (semanticdb-brute-deep-find-tags-by-name name
))
555 (while (and tags
(not docstring
))
556 (let ((sourcetag (car tags
)))
557 ;; There could be more than one! Come up with a better
559 (when (semantic-tag-buffer sourcetag
)
560 (set-buffer (semantic-tag-buffer sourcetag
))
561 (unless (eq major-mode
'texinfo-mode
)
562 (cond ((semantic-tag-get-attribute sourcetag
:prototype-flag
)
563 ;; If we found a match with doc that is a prototype, then store
564 ;; that, but don't exit till we find the real deal.
565 (setq docstringproto
(semantic-documentation-for-tag sourcetag
)
566 doctagproto sourcetag
))
567 ((eq (semantic-tag-class sourcetag
) 'variable
)
568 (setq docstringvar
(semantic-documentation-for-tag sourcetag
)
569 doctagvar sourcetag
))
570 ((semantic-tag-get-attribute sourcetag
:override-function-flag
)
573 (setq docstring
(semantic-documentation-for-tag sourcetag
))))
574 (setq doctag
(if docstring sourcetag nil
))))
575 (setq tags
(cdr tags
)))))
576 ;; If we found a prototype of the function that has some doc, but not the
577 ;; actual function, lets make due with that.
579 (cond ((stringp docstringvar
)
580 (setq docstring docstringvar
582 ((stringp docstringproto
)
583 (setq docstring docstringproto
584 doctag doctagproto
))))
585 ;; Test for doc string
587 (error "Could not find documentation for %s" (semantic-tag-name tag
)))
588 ;; If we have a string, do the replacement.
589 (delete-region (semantic-tag-start tag
)
590 (semantic-tag-end tag
))
591 ;; Use useful functions from the docaument library.
593 (document-insert-texinfo doctag
(semantic-tag-buffer doctag
))
596 (defun semantic-texi-update-doc-from-source (&optional tag
)
597 "Update the documentation for the source TAG.
598 The current buffer must be a non-texinfo source file containing TAG.
599 If TAG is nil, determine the tag based on the current position.
600 The current buffer must include TAG."
602 (when (eq major-mode
'texinfo-mode
)
603 (error "Not a source file"))
604 (semantic-fetch-tags)
606 (setq tag
(semantic-current-tag)))
607 (unless (semantic-documentation-for-tag tag
)
608 (error "Cannot find interesting documentation to use for %s"
609 (semantic-tag-name tag
)))
610 (let* ((name (semantic-tag-name tag
))
611 (texi (semantic-texi-associated-files))
614 (while (and texi
(not doctag
))
615 (set-buffer (find-file-noselect (car texi
)))
616 (setq doctag
(car (semantic-deep-find-tags-by-name
617 name
(semantic-fetch-tags)))
618 docbuff
(if doctag
(current-buffer) nil
))
619 (setq texi
(cdr texi
)))
621 (error "Tag %s is not yet documented. Use the `document' command"
623 ;; Ok, we should have everything we need. Do the deed.
624 (if (get-buffer-window docbuff
)
626 (switch-to-buffer docbuff
))
627 (goto-char (semantic-tag-start doctag
))
628 (delete-region (semantic-tag-start doctag
)
629 (semantic-tag-end doctag
))
630 ;; Use useful functions from the document library.
632 (document-insert-texinfo tag
(semantic-tag-buffer tag
))
635 (defun semantic-texi-update-doc (&optional tag
)
636 "Update the documentation for TAG.
637 If the current buffer is a texinfo file, then find the source doc, and
638 update it. If the current buffer is a source file, then get the
639 documentation for this item, find the existing doc in the associated
640 manual, and update that."
642 (cond ((eq major-mode
'texinfo-mode
)
643 (semantic-texi-update-doc-from-texi tag
))
645 (semantic-texi-update-doc-from-source tag
))))
647 (defun semantic-texi-goto-source (&optional tag
)
648 "Jump to the source for the definition in the texinfo file TAG.
649 If TAG is nil, it is derived from the deffn under POINT."
651 (unless (or (featurep 'semantic
/db
) (semanticdb-minor-mode-p))
652 (error "Texinfo updating only works when `semanticdb' is being used"))
653 (semantic-fetch-tags)
656 (setq tag
(semantic-current-tag)))
657 (unless (semantic-tag-of-class-p tag
'def
)
658 (error "Only deffns (or defun or defvar) can be updated"))
659 (let* ((name (semantic-tag-name tag
))
660 (tags (semanticdb-fast-strip-find-results
661 (semanticdb-with-match-any-mode
662 (semanticdb-brute-deep-find-tags-by-name name nil
'name
))
668 (while (and tags
(not done
))
669 (set-buffer (semantic-tag-buffer (car tags
)))
670 (unless (eq major-mode
'texinfo-mode
)
671 (switch-to-buffer (semantic-tag-buffer (car tags
)))
672 (goto-char (semantic-tag-start (car tags
)))
674 (setq tags
(cdr tags
)))
676 (error "Could not find tag for %s" (semantic-tag-name tag
)))
679 (provide 'semantic
/texi
)
681 ;;; semantic/texi.el ends here