1 ;;; document.el --- Use the semantic parser to generate documentation.
3 ;;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007 Eric M. Ludlam
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; X-RCS: $Id: document.el,v 1.33 2007/03/17 21:21:01 zappo Exp $
9 ;; Semantic is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This software is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
26 ;; Document provides the ability to create a documentation framework
27 ;; for functions and variables. It uses `semantic' to find the
28 ;; current function, and to provide additional context information for
29 ;; generating the documentation.
31 ;; Document then provides some rules for creating English Text based
32 ;; on the name of a given function, it's return type, or variable
33 ;; type. It also has output rules for texinfo, or comments.
35 ;; NOTE: Some of the user level commands in document.el dealing with
36 ;; texinfo files have been obsoleted commands in semantic-texi, which
37 ;; can not insert foriegn tokens.
40 ;; This contains most variable settings for auto-comment generation.
41 (require 'document-vars)
44 (require 'semantic-util)
48 ;;; User customizations
50 (defgroup document nil
51 "File and tag browser frame."
56 (defcustom document-my-initials (user-login-name)
57 "*The initials to use when commenting something."
61 (defcustom document-copyright-holder (user-full-name)
62 "*The string to use as the copyright holder in the file comment."
66 (defvar document-runflags nil
67 "Flags collected while updating a comment.
68 This is used to create a history element.")
72 (defvar document-current-output-file nil
73 "A marker for the current output file used for documentation.")
77 (defun document (&optional resetfile)
78 "Document in a texinfo file the function or variable the cursor is in.
79 Optional argument RESETFILE is provided w/ universal argument.
80 When non-nil, query for a new documentation file.
81 To document a function in a source file, use `document-inline'."
82 (interactive (if current-prefix-arg
84 (list (document-locate-file
85 (current-buffer) t)))))
86 ;; First, garner some information from Semantic.
88 (let ((cdi (semantic-current-tag))
89 (cdib (current-buffer)))
90 ;; Make sure we have a file.
91 (document-locate-file (current-buffer))
92 ;; Now go and create the documentation
93 (if (not document-current-output-file)
94 (error "No file found for your documentation"))
95 (set-buffer (marker-buffer document-current-output-file))
96 (goto-char document-current-output-file)
98 (document-insert-texinfo cdi cdib)
100 (setq document-current-output-file (point-marker))
103 (defun document-inline ()
104 "Document the current function with an inline comment."
106 (semantic-fetch-tags)
107 (let ((cf (semantic-current-tag)))
108 (document-insert-defun-comment cf (current-buffer))))
110 ;;; Documentation insertion functions
112 (defun document-insert-texinfo (tag buffer)
113 "Insert texinfo documentation about TAG from BUFFER."
114 (let ((tt (semantic-tag-class tag)))
116 (cond ((eq tt 'variable)
117 (if (semantic-tag-get-attribute tag :user-visible-flag)
121 (if (semantic-tag-get-attribute tag :user-visible-flag)
126 (t (error "Don't know how to document that")))
128 (semantic-tag-name tag))
129 (if (eq tt 'function)
130 (let ((args (semantic-tag-function-arguments tag)))
133 (if (stringp (car args))
135 (insert (semantic-tag-name (car args))))
136 (setq args (cdr args)))))
137 (insert "\n@anchor{" (semantic-tag-name tag) "}\n")
138 (insert (document-massage-to-texinfo
141 (document-generate-documentation tag buffer)))
143 (cond ((eq tt 'variable)
144 (if (semantic-tag-get-attribute tag :user-visible-flag)
148 (if (semantic-tag-get-attribute tag :user-visible-flag)
155 (defun document-insert-defun-comment (tag buffer)
156 "Insert mode-comment documentation about TAG from BUFFER."
158 (let ((document-runflags nil)
159 (tt (semantic-tag-class tag)))
162 (if (semantic-documentation-for-tag tag t)
163 (document-update-comment tag)
164 (document-insert-function-comment-new tag))
167 (error "Type %S is not yet managed by document `document-inline'" tt)))))
169 (defun document-update-comment (tag)
170 "Update an existing comment for TAG."
171 (let ((comment (semantic-documentation-for-tag tag 'lex)))
173 (document-update-paramlist tag comment))
174 (semantic-fetch-tags)
175 (let ((ct (semantic-brute-find-tag-by-position
176 (point) (current-buffer))))
177 (setq comment (semantic-documentation-for-tag tag 'lex))
178 (document-update-history comment (document-get-history-elt "")))))
180 (defun document-insert-new-file-header (header)
181 "Insert a new header file into this buffer. Add reference to HEADER.
182 Used by `prototype' if this file doesn't have an introductory comment."
186 (insert (document-new-file-header header))
190 ;;; Top level comment things.
192 (defun document-new-file-header (&optional header)
193 "Return a comment string customized for the current buffer.
194 Optional HEADER is the header file to use under Token."
195 (Sformat (list (list ?B '(lambda () (document-file-brief-comment)))
198 '(lambda () (setq pnt (Sformat-point)) "")
201 (document-copyright-notice)))
202 (list ?O document-copyright-holder)
203 (list ?Y (document-get-date-time-string "%Y"))
209 (semantic-prototype-file (current-buffer))))))
210 (list ?H (document-get-history-elt "Created"))
211 (list ?b (document-comment-start))
212 (list ?m (document-comment-line-prefix))
213 (list ?e (document-comment-end)))
214 ;; This is lame! Fix it sometime soon.
215 (if (string-match "\\.c$" (buffer-file-name))
216 document-file-comment
217 document-header-comment)))
219 (defun document-set-copyright-file (f)
220 "Interactively find the file name with the copyright blurb.
221 Argument F is the file to use."
222 (interactive "FCopyright Notice File (RET for none): ")
223 (if (string= f (buffer-file-name))
224 (setq document-copyright-notice-file "")
225 (setq document-copyright-notice-file f)))
227 (defun document-copyright-notice ()
228 "Create, or find a copyright notice.
229 Adds the comment line PREFIX to each line."
230 (if (not document-copyright-notice-file)
231 (call-interactively 'document-set-copyright-file))
232 (if (= (length document-copyright-notice-file) 0)
233 "??Public Domain Software??"
234 (let* ((b (get-buffer-create "DOCUMENT TEMP"))
236 (plen (Sformat-column))
237 (pstr (substring (concat (document-comment-line-prefix)
243 (insert-file-contents document-copyright-notice-file)
244 ;; Now put comment marks all over.
255 (if (equal (following-char) ?\n)
257 (set-buffer-modified-p nil)
262 (defun document-file-brief-comment ()
263 "Make a brief comment about the file we are currently editing."
264 (Sformat (list (list ?F (file-name-nondirectory (buffer-file-name)))
266 (read-string "Brief Description of file: "))))
267 document-file-brief-comment))
269 ;;; Documentatation generation functions
271 (defun document-generate-documentation (tag buffer)
272 "Return a plain string documenting TAG from BUFFER."
275 (let ((doc ;; Second, does this thing have docs in the source buffer which
276 ;; an override method might be able to find?
277 (semantic-documentation-for-tag tag)
280 (document-generate-new-documentation tag buffer)
281 ;; Ok, now lets see what sort of formatting there might be,
282 ;; and see about removing some of it.. (Tables of arguments,
283 ;; and that sort of thing.)
285 ;; Return the string.
288 (defun document-generate-new-documentation (tag buffer)
289 "Look at elements of TAG in BUFFER to make documentation.
290 This will create a new documentation string from scratch."
291 ;; We probably want more than this, but for now it's close.
292 (document-function-name-comment tag))
294 ;;; Inline comment mangling.
296 (defun document-insert-function-comment-new (tag)
297 "Insert a new comment which explains the function found in TAG."
298 (let ((hist (document-get-history-elt ""))
303 (fname (semantic-tag-name tag))
304 (returns (semantic-tag-type tag))
305 (params (semantic-tag-function-arguments tag))
308 ;; convert a type list into a long string to analyze.
309 (setq returns (car returns)))
310 ;; tag should always be correct.
311 (goto-char (semantic-tag-start tag))
313 (insert (Sformat (list (list ?F fname)
314 (list ?f '(lambda () (setq zpnt (Sformat-point)) ""))
315 (list ?p '(lambda () (setq pnt (Sformat-point)) ""))
316 (list ?D (document-function-name-comment tag))
317 (list ?R (document-insert-return returns))
319 (document-insert-parameters params)))
320 (list ?H (concat hist document-new-hist-comment))
321 (list ?b (document-comment-start))
322 (list ?m (document-comment-line-prefix))
323 (list ?e (document-comment-end)))
324 document-function-comment))
325 (goto-char (+ zpnt st))
326 (message "Setting fill prefix to: \"%s\""
328 (concat (document-comment-line-prefix)
331 (length (document-comment-line-prefix)))
333 (goto-char (+ pnt st))
338 (defun document-function-name-comment (tag)
339 "Create documentation for the function defined in TAG.
340 If we can identify a verb in the list followed by some
341 name part then check the return value to see if we can use that to
342 finish off the sentence. ie. any function with 'alloc' in it will be
343 allocating something based on its type."
344 (let ((al document-autocomment-return-first-alist)
348 (fname (semantic-tag-name tag))
349 (retval (or (semantic-tag-type tag) "")))
351 ;; convert a type list into a long string to analyze.
352 (setq retval (car retval)))
353 ;; check for modifiers like static
355 (if (string-match (car (car al)) (downcase retval))
357 (setq news (concat news (cdr (car al))))
361 ;; check for verb parts!
362 (setq al document-autocomment-function-alist)
364 (if (string-match (car (car al)) (downcase fname))
367 (concat news (if dropit (downcase (cdr (car al)))
369 ;; if we end in a space, then we are expecting a potential
371 (if (= ? (aref news (1- (length news))))
375 ;; check for noun parts!
376 (setq al document-autocomment-common-nouns-abbrevs)
378 (if (string-match (car (car al)) (downcase fname))
381 (concat news (if dropit (downcase (cdr (car al)))
385 ;; add tailers to names which are obviously returning something.
388 (setq al document-autocomment-return-last-alist)
390 (if (string-match (car (car al)) (downcase retval))
394 ;; this one may use parts of the return value.
395 (format (cdr (car al))
396 (document-programmer->english
397 (substring retval (match-beginning 1)
400 (setq al (cdr al)))))
403 (defun document-insert-return (returnval)
404 "Take the return value, and return a string which is ready to be commented.
405 Argument RETURNVAL is the string representing the type to be returned."
408 (if (string-match "^\\(static +\\|STATIC +\\)?\\(void\\|VOID\\)" returnval)
410 (if (= (length returnval) 0)
412 (concat returnval " - ")))))
414 (defun document-insert-parameters (params &optional commentlist)
415 "Convert a parameter list PARAMS into a vertical list separated by -es.
416 Optional COMMENTLIST is a list of previously known parts with comments."
418 (let* ((col (if Sformat-formatting (Sformat-column) (current-column)))
420 ;; returns is local to the caller
421 (longest (document-longest-name newl))
425 (let* ((n (car newl))
426 (nn (if (stringp n) n (semantic-tag-name n)))
427 (al (if (stringp n) nil (semantic-tag-modifiers n)))
428 (nt (if (stringp n) "" (semantic-tag-type n))))
430 ;; make sure this is a string.
432 (setq numdfs (1+ numdfs))
433 (let ((nextp (Sformat
441 (list ?D (document-parameter-comment
444 document-param-element)))
448 ;; the following always assumes that there is
449 ;; always a comment starting with SPC * on
450 ;; every line. Mabee fix, but this is what I
451 ;; use, so tough noogies as of right now.
452 (concat "\n" (document-comment-line-prefix)
454 (- col (length (document-comment-line-prefix)))
456 (setq newl (cdr newl)))
457 (if (= (length newp) 0) (setq newp "None"))
458 (if (and document-extra-line-after-short-parameters (<= numdfs 1))
459 (setq newp (concat newp "\n *")))
463 (defun document-parameter-comment (param &optional commentlist)
464 "Convert tag or string PARAM into a name,comment pair.
465 Optional COMMENTLIST is list of previously existing comments to
466 use instead in alist form. If the name doesn't appear in the list of
467 standard names, then englishify it instead."
469 (aso document-autocomment-param-alist)
471 (name (if (stringp param) param (semantic-tag-name param)))
472 (tt (if (stringp param) nil (semantic-tag-type param))))
473 ;; Make sure the type is a string.
475 (setq tt (semantic-tag-name tt)))
476 ;; Find name description parts.
478 (if (string-match (car (car aso)) name)
481 (setq cmt (concat cmt (cdr (car aso))))))
482 (setq aso (cdr aso)))
483 (if (/= (length cmt) 0)
485 ;; finally check for array parts
486 (if (and (not (stringp param)) (semantic-tag-modifiers param))
487 (setq cmt (concat cmt "array of ")))
488 (setq aso document-autocomment-param-type-alist)
490 (if (string-match (car (car aso)) tt)
491 (setq cmt (concat cmt (cdr (car aso)))))
492 (setq aso (cdr aso))))
493 ;; Convert from programmer to english.
495 (setq cmt (concat cmt " "
496 (document-programmer->english name))))
500 (defun document-get-history-elt (changes)
501 "Return the history element with the change elt set to CHANGES."
502 (Sformat (list '(?U document-my-initials)
503 (list ?D (document-get-date))
504 '(?S document-change-number)
506 document-history-element))
508 (defun document-get-date-time-string (form)
509 "Return a string matching the format of `document-date-element'.
510 Argument FORM is the format string to use."
511 (let* ((date (current-time-string))
514 (concat "^\\([A-Z][a-z]*\\) *\\([A-Z][a-z]*\\) *\\([0-9]*\\)"
515 " \\([0-9]*\\):\\([0-9]*\\):\\([0-9]*\\)"
518 (wkdy (substring date (match-beginning 1) (match-end 1)))
519 (hour (string-to-number
520 (substring date (match-beginning 4) (match-end 4))))
521 (min (substring date (match-beginning 5) (match-end 5)))
522 (sec (substring date (match-beginning 6) (match-end 6)))
524 (cdr (assoc (substring date (match-beginning 2) (match-end 2))
525 '(("Jan" . 1) ("Feb" . 2) ("Mar" . 3) ("Apr" . 4)
526 ("May" . 5) ("Jun" . 6) ("Jul" . 7) ("Aug" . 8)
527 ("Sep" . 9) ("Oct" . 10) ("Nov" . 11) ("Dec" . 12)))))
528 (ms (substring date (match-beginning 2) (match-end 2)))
529 (day (substring date (match-beginning 3) (match-end 3)))
530 (year (substring date (match-beginning 7) (match-end 7))))
531 (Sformat (list (list ?H (% hour 12))
533 (list ?a (if (> hour 12) "pm" "am"))
540 (list ?y (substring year 2))
544 (defun document-get-date ()
545 "Return a string which is the current date."
546 (document-get-date-time-string document-date-element))
548 ;;; Updating utilities
550 (defun document-update-history (comment history)
551 "Update COMMENT with the text HISTORY.
552 COMMENT is a `semantic-lex' token."
555 (goto-char (semantic-lex-token-end comment))
556 (if (not (re-search-backward (regexp-quote (document-comment-start))
557 (semantic-lex-token-start comment) t))
558 (error "Comment confuses me"))
559 (let ((s (document-just-after-token-regexp ?H document-function-comment)))
560 (if (not s) (error "Can't find where to enter new history element"))
561 (if (re-search-forward (concat "\\(" s "\\)")
562 (1+ (semantic-lex-token-end comment)) t)
564 (goto-char (match-beginning 1))
565 (insert (concat "\n" (document-comment-line-prefix) " "))
567 (setq endpos (point))))
569 (while document-runflags
570 (let ((p (assoc (car (car document-runflags))
571 document-autocomment-modify-alist)))
572 (if p (insert (format (cdr p) (cdr (car document-runflags))))))
573 (setq document-runflags (cdr document-runflags)))))
575 (defun document-argument-name (arg)
576 "Return a string representing the name of ARG.
577 Arguments can be semantic tokens, or strings."
578 (cond ((semantic-tag-p arg)
579 (semantic-tag-name arg))
582 (t (format "%s" arg))))
584 (defun document-update-paramlist (tag comment)
585 "Update TAG's comment found in the `semantic-lex' token COMMENT."
586 (let ((endpos 0) st en (il nil)
587 (case-fold-search nil)
588 (l (semantic-tag-function-arguments tag)))
590 (goto-char (semantic-lex-token-start comment))
591 (let ((s (document-just-after-token-regexp ?P document-function-comment))
592 (s2 (document-just-before-token-regexp ?P document-function-comment)))
593 (if (or (not s) (not s2))
594 (error "Cannot break format string into findable begin and end tokens"))
595 (if (not (re-search-forward (concat "\\(" s "\\)")
596 (1+ (semantic-lex-token-end comment)) t))
597 (error "Comment is not formatted correctly for param check"))
598 (goto-char (match-beginning 1))
600 (goto-char (semantic-lex-token-start comment))
601 (if (not (re-search-forward s2 (semantic-lex-token-end comment) t))
602 (error "Comment is not formatted correctly for param check"))
604 ;; At this point we have the beginning and end of the
605 ;; parameter list in the comment. Now lets search through
606 ;; it and generate a list (name . commentpart) so we can
607 ;; re-build it if it doesn't match L
608 (while (re-search-forward
609 (concat "\\(\\(\\sw\\|\\s_+\\)+\\)\\s-*-[ \t]*")
611 (let ((n (buffer-substring (match-beginning 1) (match-end 1)))
614 (re-search-forward "$" (semantic-lex-token-end comment) t)
615 (setq c (buffer-substring c (point)))
616 (setq il (cons (cons n c) il))))
617 ;; run verify on two lists of parameters to make sure they
619 (let ((tl l) (stop nil))
620 (while (and tl (not stop))
621 (if (not (assoc (document-argument-name (car tl)) il))
626 ;; check if we want to modify the parameter list.
627 (if (not (and il (y-or-n-p "Parameter list changed. Fix? ")))
628 (message "Not fixing.")
629 ;; delete what was there, and insert the new stuff.
634 (if (not (assoc (document-argument-name (car ntl)) il))
637 (setq cs1 (concat cs1 (if cs1 ", ")
638 (document-argument-name (car ntl))))))
639 (setq ntl (cdr ntl)))
642 (setq cs1 (concat "Added parameter " cs1))
643 (setq cs1 (concat "Added parameters " cs1)))
644 (setq cs1 "Removed parameters."))
645 (setq document-runflags (cons (cons 'document-newparam cs1)
647 (let ((dif (- en st))
649 (delete-region st en)
650 (setq newc (document-insert-parameters l il))
651 (setq dif (- (length newc) dif))
655 ;;; Conversion utilities
657 (defun document-longest-name (list)
658 "Go through LIST, and return the length of the longest name."
662 (setq nn (if (stringp (car list)) (car list)
663 (semantic-tag-name (car list))))
664 (if (< longest (length nn))
665 (setq longest (length nn)))
666 (setq list (cdr list)))
669 (defun document-programmer->english (programmer)
670 "Takes PROGRAMMER and converts it into English.
671 Works with the following rules:
672 1) convert all _ into spaces.
673 2) inserts spaces in front of all lowerUpper case combos
674 3) expands noun names based on common programmer nouns.
676 This function is designed for variables, not functions. This does
677 not account for verb parts."
678 (let ((ind 0) ;index in string
679 (llow nil) ;lower/upper case flag
680 (wlist nil) ;list of words after breaking
681 (newstr nil) ;new string being generated
682 (al nil)) ;autocomment list
684 ;; 1) Convert underscores
686 (while (< ind (length programmer))
687 (setq newstr (concat newstr
688 (if (= (aref programmer ind) ?_)
689 " " (char-to-string (aref programmer ind)))))
691 (setq programmer newstr
695 ;; 2) Find word brakes between case changes
697 (while (< ind (length programmer))
700 (let ((tc (aref programmer ind)))
701 (if (and (>= tc ?a) (<= tc ?z))
708 (concat " " (char-to-string tc)))
709 (char-to-string tc))))))
712 ;; 3) Expand the words if possible
718 (while (string-match (concat "^\\s-*\\([^ \t\n]+\\)") programmer)
719 (let ((ts (substring programmer (match-beginning 1) (match-end 1)))
721 (setq al document-autocomment-common-nouns-abbrevs)
724 (if (string-match (car (car al)) (downcase ts))
726 (setq newstr (concat newstr (cdr (car al))))
727 ;; don't terminate because we may actuall have 2 words
728 ;; next to eachother we didn't identify before
731 (if (not llow) (setq newstr (concat newstr ts)))
732 (setq newstr (concat newstr " "))
733 (setq programmer (substring programmer end))))
737 ;;; Sformat string managers
739 ;; These two routines find the string between different % tokens, and
740 ;; returns them as regular expressions vie regexp-quote. The result
741 ;; will allow a program to find text surrounding major parts within a
742 ;; comment, thus, the parts in a comment that need to be changed.
744 (defun document-just-before-token-regexp (token format)
745 "Return a search expression for text before TOKEN in FORMAT.
746 This search string can be used to find the text residing in TOKEN
747 if it were inserted with FORMAT in the past."
748 (setq format (document-format-for-native-comments format))
749 (sformat-just-before-token-regexp token format))
751 (defun document-just-after-token-regexp (token format)
752 "Return a search expression for text after TOKEN in FORMAT.
753 This search string can be used to find the text residing in TOKEN
754 if it were inserted with FORMAT in the past."
755 (setq format (document-format-for-native-comments format))
756 (sformat-just-after-token-regexp token format))
758 ;; This function adds in the comment thingies so that the above
759 ;; functions can work some magic.
760 (defun document-format-for-native-comments (formatstr)
761 "Return FORMATSTR with the comment formatters filled in.
762 Leaves other formatting elements the way they are."
763 (Sformat (list (list ?b (document-comment-start))
764 (list ?m (document-comment-line-prefix))
765 (list ?e (document-comment-end)))
769 ;;; Texinfo mangling.
771 (defun document-massage-to-texinfo (tag buffer string)
772 "Massage TAG's documentation from BUFFER as STRING.
773 This is to take advantage of TeXinfo's markup symbols."
774 (let ((mode (with-current-buffer buffer (semantic-tag-mode tag))))
775 (when (eq mode 'emacs-lisp-mode)
776 ;; Elisp has a few advantages. Hack it in.
777 (setq string (document-texify-elisp-docstring string)))
778 ;; Else, other languages are simpler. Also, might as well
779 ;; run the elisp version through also.
780 (let ((case-fold-search nil)
783 "\\(^\\|[^{]\\)\\<\\([A-Z0-9_-]+\\)\\>\\($\\|[^}]\\)"
785 (let ((ms (match-string 2 string)))
786 (when (eq mode 'emacs-lisp-mode)
787 (setq ms (downcase ms)))
789 (when (not (or (string= ms "A")
792 (setq string (concat (substring string 0 (match-beginning 2))
796 (substring string (match-end 2))))))
797 (setq start (match-end 2)))
801 ;; This FN was taken from EIEIO and modified. Maybe convert later.
802 (defun document-texify-elisp-docstring (string)
803 "Take STRING, (a normal doc string), and convert it into a texinfo string.
804 For instances where CLASS is the class being referenced, do not Xref
807 `function' => @dfn{function}
808 `variable' => @code{variable}
809 `class' => @code{class} @xref{class}
810 `unknown' => @code{unknonwn}
812 'quoteme => @code{quoteme}
813 non-nil => non-@code{nil}
816 [ stuff ] => @code{[ stuff ]}
817 Key => @kbd{Key} (key is C\\-h, M\\-h, SPC, RET, TAB and the like)
819 (while (string-match "`\\([-a-zA-Z0-9<>.]+\\)'" string)
820 (let* ((vs (substring string (match-beginning 1) (match-end 1)))
821 (v (intern-soft vs)))
824 (replace-match (concat
829 (while (string-match "\\( \\|^\\)\\(nil\\|t\\|'[-a-zA-Z0-9]+\\|:[-a-zA-Z0-9]+\\)\\([. ,]\\|$\\)" string)
830 (setq string (replace-match "@code{\\2}" t nil string 2)))
831 (while (string-match "\\( \\|^\\)\\(\\(non-\\)\\(nil\\)\\)\\([. ,]\\|$\\)" string)
832 (setq string (replace-match "\\3@code{\\4}" t nil string 2)))
833 (while (string-match "\\( \\|^\\)\\(\\[[^]]+\\]\\)\\( \\|$\\)" string)
834 (setq string (replace-match "@code{\\2}" t nil string 2)))
835 (while (string-match "\\( \\|^\\)\\(\\(\\(C-\\|M-\\|S-\\)+\\([^ \t\n]\\|RET\\|SPC\\|TAB\\)\\)\\|\\(RET\\|SPC\\|TAB\\)\\)\\( \\|\\s.\\|$\\)" string)
836 (setq string (replace-match "@kbd{\\2}" t nil string 2)))
837 (while (string-match "\"\\(.+\\)\"" string)
838 (setq string (replace-match "``\\1''" t nil string 0)))
839 (while (string-match "\\.\\.\\." string)
840 (setq string (replace-match "@dots{}" t nil string 0)))
843 ;;; Buffer finding and managing
845 (defun document-find-file (file)
846 "Load up the document file FILE.
847 Make it current, and return a marker for the location of newly inserted
849 (set-buffer (find-file-noselect file))
850 ;; Theoretically, we should add some smarts here for positioning
851 ;; the cursor. For now, do some simple stuff.
852 (if (eq (point) (point-min))
854 (switch-to-buffer (current-buffer))
855 (error "Position cursor in %s, and try inserting documentation again"
859 (defun document-locate-file (buffer &optional override)
860 "Return a file in which documentation belonging to BUFFER should be placed.
861 Optional argument OVERRIDE indicates to override the last used location."
862 (if (and document-current-output-file (not override))
863 document-current-output-file
864 ;; Else, perform some default behaviors
865 (let ((files (if (and (fboundp 'ede-documentation-files) ede-minor-mode)
868 (ede-documentation-files))
872 (setq choice (cons (list (car files)) choice)
876 (completing-read "Documentation File: "
879 (read-file-name "Documentation File: "
881 (setq document-current-output-file (document-find-file choice)))))
886 ;;; document.el ends here