comparison lisp/textmodes/reftex-parse.el @ 88155:d7ddb3e565de

sync with trunk
author Henrik Enberg <henrik.enberg@telia.com>
date Mon, 16 Jan 2006 00:03:54 +0000
parents 5ade352e8d1c
children
comparison
equal deleted inserted replaced
88154:8ce476d3ba36 88155:d7ddb3e565de
1 ;;; reftex-parse.el --- parser functions for RefTeX 1 ;;; reftex-parse.el --- parser functions for RefTeX
2 ;; Copyright (c) 1997, 1998, 1999, 2000 Free Software Foundation, Inc. 2 ;; Copyright (c) 1997, 1998, 1999, 2000, 2003, 2004, 2005
3 ;; Free Software Foundation, Inc.
3 4
4 ;; Author: Carsten Dominik <dominik@science.uva.nl> 5 ;; Author: Carsten Dominik <dominik@science.uva.nl>
5 ;; Version: 4.18 6 ;; Version: VERSIONTAG
6 ;; 7 ;;
7 8
8 ;; This file is part of GNU Emacs. 9 ;; This file is part of GNU Emacs.
9 10
10 ;; GNU Emacs is free software; you can redistribute it and/or modify 11 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details. 19 ;; GNU General Public License for more details.
19 20
20 ;; You should have received a copy of the GNU General Public License 21 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the 22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02111-1307, USA. 24 ;; Boston, MA 02110-1301, USA.
24 25
25 ;;; Commentary: 26 ;;; Commentary:
26 27
27 ;;; Code: 28 ;;; Code:
28 29
31 (require 'reftex) 32 (require 'reftex)
32 33
33 (defmacro reftex-with-special-syntax (&rest body) 34 (defmacro reftex-with-special-syntax (&rest body)
34 `(let ((saved-syntax (syntax-table))) 35 `(let ((saved-syntax (syntax-table)))
35 (unwind-protect 36 (unwind-protect
36 (progn 37 (progn
37 (set-syntax-table reftex-syntax-table) 38 (set-syntax-table reftex-syntax-table)
38 (let ((case-fold-search nil)) 39 (let ((case-fold-search nil))
39 ,@body)) 40 ,@body))
40 (set-syntax-table saved-syntax)))) 41 (set-syntax-table saved-syntax))))
41 42
42 (defun reftex-parse-one () 43 (defun reftex-parse-one ()
43 "Re-parse this file." 44 "Re-parse this file."
44 (interactive) 45 (interactive)
66 67
67 ;; Do the scanning. 68 ;; Do the scanning.
68 69
69 (let* ((old-list (symbol-value reftex-docstruct-symbol)) 70 (let* ((old-list (symbol-value reftex-docstruct-symbol))
70 (master (reftex-TeX-master-file)) 71 (master (reftex-TeX-master-file))
71 (true-master (file-truename master)) 72 (true-master (file-truename master))
72 (master-dir (file-name-as-directory (file-name-directory master))) 73 (master-dir (file-name-as-directory (file-name-directory master)))
73 (file (or file (buffer-file-name))) 74 (file (or file (buffer-file-name)))
74 (true-file (file-truename file)) 75 (true-file (file-truename file))
75 (bibview-cache (assq 'bibview-cache old-list)) 76 (bibview-cache (assq 'bibview-cache old-list))
76 (index-tags (cdr (assq 'index-tags old-list))) 77 (index-tags (cdr (assq 'index-tags old-list)))
77 from-file appendix docstruct tmp) 78 from-file appendix docstruct tmp)
78 79
79 ;; Make sure replacement is really an option here 80 ;; Make sure replacement is really an option here
80 (when (and (eq rescan t) 81 (when (and (eq rescan t)
81 (not (and (member (list 'bof file) old-list) 82 (not (and (member (list 'bof file) old-list)
95 ;; Reset index-tags if we scan everything 96 ;; Reset index-tags if we scan everything
96 (if (equal rescan 1) (setq index-tags nil)) 97 (if (equal rescan 1) (setq index-tags nil))
97 98
98 ;; Find active toc entry and initialize section-numbers 99 ;; Find active toc entry and initialize section-numbers
99 (setq reftex-active-toc (reftex-last-assoc-before-elt 100 (setq reftex-active-toc (reftex-last-assoc-before-elt
100 'toc (list 'bof from-file) old-list) 101 'toc (list 'bof from-file) old-list)
101 appendix (reftex-last-assoc-before-elt 102 appendix (reftex-last-assoc-before-elt
102 'appendix (list 'bof from-file) old-list)) 103 'appendix (list 'bof from-file) old-list))
103 104
104 (reftex-init-section-numbers reftex-active-toc appendix) 105 (reftex-init-section-numbers reftex-active-toc appendix)
105 106
106 (if (eq rescan 1) 107 (if (eq rescan 1)
107 (message "Scanning entire document...") 108 (message "Scanning entire document...")
108 (message "Scanning document from %s..." from-file)) 109 (message "Scanning document from %s..." from-file))
109 110
110 (reftex-with-special-syntax 111 (reftex-with-special-syntax
111 (save-window-excursion 112 (save-window-excursion
112 (save-excursion 113 (save-excursion
113 (unwind-protect 114 (unwind-protect
114 (setq docstruct 115 (setq docstruct
115 (reftex-parse-from-file 116 (reftex-parse-from-file
116 from-file docstruct master-dir)) 117 from-file docstruct master-dir))
117 (reftex-kill-temporary-buffers))))) 118 (reftex-kill-temporary-buffers)))))
118 119
119 (message "Scanning document... done") 120 (message "Scanning document... done")
120 121
121 ;; Turn the list around. 122 ;; Turn the list around.
122 (setq docstruct (nreverse docstruct)) 123 (setq docstruct (nreverse docstruct))
139 (car (push (list 'is-multi is-multi) docstruct))))) 140 (car (push (list 'is-multi is-multi) docstruct)))))
140 (setcdr entry (cons is-multi nil))) 141 (setcdr entry (cons is-multi nil)))
141 (and index-tags (setq index-tags (sort index-tags 'string<))) 142 (and index-tags (setq index-tags (sort index-tags 'string<)))
142 (let ((index-tag-cell (assq 'index-tags docstruct))) 143 (let ((index-tag-cell (assq 'index-tags docstruct)))
143 (if index-tag-cell 144 (if index-tag-cell
144 (setcdr index-tag-cell index-tags) 145 (setcdr index-tag-cell index-tags)
145 (push (cons 'index-tags index-tags) docstruct))) 146 (push (cons 'index-tags index-tags) docstruct)))
146 (unless (assq 'xr docstruct) 147 (unless (assq 'xr docstruct)
147 (let* ((allxr (reftex-all-assq 'xr-doc docstruct)) 148 (let* ((allxr (reftex-all-assq 'xr-doc docstruct))
148 (alist (mapcar 149 (alist (mapcar
149 (lambda (x) 150 (lambda (x)
150 (if (setq tmp (reftex-locate-file (nth 2 x) "tex" 151 (if (setq tmp (reftex-locate-file (nth 2 x) "tex"
151 master-dir)) 152 master-dir))
152 (cons (nth 1 x) tmp) 153 (cons (nth 1 x) tmp)
153 (message "Can't find external document %s" 154 (message "Can't find external document %s"
154 (nth 2 x)) 155 (nth 2 x))
155 nil)) 156 nil))
156 allxr)) 157 allxr))
157 (alist (delq nil alist)) 158 (alist (delq nil alist))
158 (allprefix (delq nil (mapcar 'car alist))) 159 (allprefix (delq nil (mapcar 'car alist)))
159 (regexp (if allprefix 160 (regexp (if allprefix
160 (concat "\\`\\(" 161 (concat "\\`\\("
161 (mapconcat 'identity allprefix "\\|") 162 (mapconcat 'identity allprefix "\\|")
162 "\\)") 163 "\\)")
163 "\\\\\\\\\\\\"))) ; this will never match 164 "\\\\\\\\\\\\"))) ; this will never match
164 (push (list 'xr alist regexp) docstruct))) 165 (push (list 'xr alist regexp) docstruct)))
165 166
166 (set reftex-docstruct-symbol docstruct) 167 (set reftex-docstruct-symbol docstruct)
167 (put reftex-docstruct-symbol 'modified t))) 168 (put reftex-docstruct-symbol 'modified t)))
168 169
169 (defun reftex-everything-regexp () 170 (defun reftex-everything-regexp ()
170 (if reftex-support-index 171 (if reftex-support-index
171 reftex-everything-regexp 172 reftex-everything-regexp
172 reftex-everything-regexp-no-index)) 173 reftex-everything-regexp-no-index))
173 174
175 ;;;###autoload
174 (defun reftex-all-document-files (&optional relative) 176 (defun reftex-all-document-files (&optional relative)
175 "Return a list of all files belonging to the current document. 177 "Return a list of all files belonging to the current document.
176 When RELATIVE is non-nil, give file names relative to directory 178 When RELATIVE is non-nil, give file names relative to directory
177 of master file." 179 of master file."
178 (let* ((all (symbol-value reftex-docstruct-symbol)) 180 (let* ((all (symbol-value reftex-docstruct-symbol))
198 toc-entry index-entry next-buf buf) 200 toc-entry index-entry next-buf buf)
199 201
200 (catch 'exit 202 (catch 'exit
201 (setq file-found (reftex-locate-file file "tex" master-dir)) 203 (setq file-found (reftex-locate-file file "tex" master-dir))
202 (if (and (not file-found) 204 (if (and (not file-found)
203 (setq buf (reftex-get-buffer-visiting file))) 205 (setq buf (reftex-get-buffer-visiting file)))
204 (setq file-found (buffer-file-name buf))) 206 (setq file-found (buffer-file-name buf)))
205 207
206 (unless file-found 208 (unless file-found
207 (push (list 'file-error file) docstruct) 209 (push (list 'file-error file) docstruct)
208 (throw 'exit nil)) 210 (throw 'exit nil))
209 211
218 220
219 ;; Begin of file mark 221 ;; Begin of file mark
220 (setq file (buffer-file-name)) 222 (setq file (buffer-file-name))
221 (push (list 'bof file) docstruct) 223 (push (list 'bof file) docstruct)
222 224
223 (reftex-with-special-syntax 225 (reftex-with-special-syntax
224 (save-excursion 226 (save-excursion
225 (save-restriction 227 (save-restriction
226 (widen) 228 (widen)
227 (goto-char 1) 229 (goto-char 1)
228 230
229 (while (re-search-forward regexp nil t) 231 (while (re-search-forward regexp nil t)
230 232
231 (cond 233 (cond
232 234
233 ((match-end 1) 235 ((match-end 1)
234 ;; It is a label 236 ;; It is a label
235 (push (reftex-label-info (reftex-match-string 1) file bound) 237 (push (reftex-label-info (reftex-match-string 1) file bound)
236 docstruct)) 238 docstruct))
237 239
238 ((match-end 3) 240 ((match-end 3)
239 ;; It is a section 241 ;; It is a section
240 (setq bound (point)) 242 (setq bound (point))
241 243
242 ;; Insert in List 244 ;; Insert in List
243 (setq toc-entry (reftex-section-info file)) 245 (setq toc-entry (reftex-section-info file))
244 (when toc-entry 246 (when toc-entry
245 ;; It can happen that section info returns nil 247 ;; It can happen that section info returns nil
246 (setq level (nth 5 toc-entry)) 248 (setq level (nth 5 toc-entry))
247 (setq highest-level (min highest-level level)) 249 (setq highest-level (min highest-level level))
248 (if (= level highest-level) 250 (if (= level highest-level)
249 (message 251 (message
250 "Scanning %s %s ..." 252 "Scanning %s %s ..."
251 (car (rassoc level reftex-section-levels-all)) 253 (car (rassoc level reftex-section-levels-all))
252 (nth 6 toc-entry))) 254 (nth 6 toc-entry)))
253 255
254 (push toc-entry docstruct) 256 (push toc-entry docstruct)
255 (setq reftex-active-toc toc-entry))) 257 (setq reftex-active-toc toc-entry)))
256 258
257 ((match-end 7) 259 ((match-end 7)
258 ;; It's an include or input 260 ;; It's an include or input
259 (setq include-file (reftex-match-string 7)) 261 (setq include-file (reftex-match-string 7))
260 ;; Test if this file should be ignored 262 ;; Test if this file should be ignored
261 (unless (delq nil (mapcar 263 (unless (delq nil (mapcar
262 (lambda (x) (string-match x include-file)) 264 (lambda (x) (string-match x include-file))
263 reftex-no-include-regexps)) 265 reftex-no-include-regexps))
264 ;; Parse it 266 ;; Parse it
265 (setq docstruct 267 (setq docstruct
266 (reftex-parse-from-file 268 (reftex-parse-from-file
267 include-file 269 include-file
268 docstruct master-dir)))) 270 docstruct master-dir))))
269 271
270 ((match-end 9) 272 ((match-end 9)
271 ;; Appendix starts here 273 ;; Appendix starts here
272 (reftex-init-section-numbers nil t) 274 (reftex-init-section-numbers nil t)
273 (push (cons 'appendix t) docstruct)) 275 (push (cons 'appendix t) docstruct))
274 276
275 ((match-end 10) 277 ((match-end 10)
276 ;; Index entry 278 ;; Index entry
277 (when reftex-support-index 279 (when reftex-support-index
278 (setq index-entry (reftex-index-info file)) 280 (setq index-entry (reftex-index-info file))
279 (when index-entry 281 (when index-entry
280 (add-to-list 'index-tags (nth 1 index-entry)) 282 (add-to-list 'index-tags (nth 1 index-entry))
281 (push index-entry docstruct)))) 283 (push index-entry docstruct))))
282 284
283 ((match-end 11) 285 ((match-end 11)
284 ;; A macro with label 286 ;; A macro with label
285 (save-excursion 287 (save-excursion
286 (let* ((mac (reftex-match-string 11)) 288 (let* ((mac (reftex-match-string 11))
287 (label (progn (goto-char (match-end 11)) 289 (label (progn (goto-char (match-end 11))
288 (save-match-data 290 (save-match-data
289 (reftex-no-props 291 (reftex-no-props
290 (reftex-nth-arg-wrapper 292 (reftex-nth-arg-wrapper
291 mac))))) 293 mac)))))
292 (typekey (nth 1 (assoc mac reftex-env-or-mac-alist))) 294 (typekey (nth 1 (assoc mac reftex-env-or-mac-alist)))
293 (entry (progn (if typekey 295 (entry (progn (if typekey
294 ;; A typing macro 296 ;; A typing macro
295 (goto-char (match-end 0)) 297 (goto-char (match-end 0))
296 ;; A neutral macro 298 ;; A neutral macro
297 (goto-char (match-end 11)) 299 (goto-char (match-end 11))
298 (reftex-move-over-touching-args)) 300 (reftex-move-over-touching-args))
299 (reftex-label-info 301 (reftex-label-info
300 label file bound nil nil)))) 302 label file bound nil nil))))
301 (push entry docstruct)))) 303 (push entry docstruct))))
302 (t (error "This should not happen (reftex-parse-from-file)"))) 304 (t (error "This should not happen (reftex-parse-from-file)")))
303 ) 305 )
304 306
305 ;; Find bibliography statement 307 ;; Find bibliography statement
306 (when (setq tmp (reftex-locate-bibliography-files master-dir)) 308 (when (setq tmp (reftex-locate-bibliography-files master-dir))
307 (push (cons 'bib tmp) docstruct)) 309 (push (cons 'bib tmp) docstruct))
308 310
309 (goto-char 1) 311 (goto-char 1)
310 (when (re-search-forward 312 (when (re-search-forward
311 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t) 313 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t)
312 (push (cons 'thebib file) docstruct)) 314 (push (cons 'thebib file) docstruct))
313 315
314 ;; Find external document specifications 316 ;; Find external document specifications
315 (goto-char 1) 317 (goto-char 1)
316 (while (re-search-forward "[\n\r][ \t]*\\\\externaldocument\\(\\[\\([^]]*\\)\\]\\)?{\\([^}]+\\)}" nil t) 318 (while (re-search-forward "[\n\r][ \t]*\\\\externaldocument\\(\\[\\([^]]*\\)\\]\\)?{\\([^}]+\\)}" nil t)
317 (push (list 'xr-doc (reftex-match-string 2) 319 (push (list 'xr-doc (reftex-match-string 2)
318 (reftex-match-string 3)) 320 (reftex-match-string 3))
319 docstruct)) 321 docstruct))
320 322
321 ;; End of file mark 323 ;; End of file mark
322 (push (list 'eof file) docstruct))))) 324 (push (list 'eof file) docstruct)))))
323 325
324 ;; Kill the scanned buffer 326 ;; Kill the scanned buffer
325 (reftex-kill-temporary-buffers next-buf)) 327 (reftex-kill-temporary-buffers next-buf))
326 328
327 ;; Return the list 329 ;; Return the list
328 docstruct)) 330 docstruct))
329 331
330 (defun reftex-locate-bibliography-files (master-dir &optional files) 332 (defun reftex-locate-bibliography-files (master-dir &optional files)
331 ;; Scan buffer for bibliography macro and return file list. 333 ;; Scan buffer for bibliography macro and return file list.
332 334
333 (unless files 335 (unless files
334 (save-excursion 336 (save-excursion
335 (goto-char (point-min)) 337 (goto-char (point-min))
336 (if (re-search-forward 338 (if (re-search-forward
337 (concat 339 (concat
338 ; "\\(\\`\\|[\n\r]\\)[^%]*\\\\\\(" 340 ; "\\(\\`\\|[\n\r]\\)[^%]*\\\\\\("
339 "\\(^\\)[^%]*\\\\\\(" 341 "\\(^\\)[^%\n\r]*\\\\\\("
340 (mapconcat 'identity reftex-bibliography-commands "\\|") 342 (mapconcat 'identity reftex-bibliography-commands "\\|")
341 "\\){[ \t]*\\([^}]+\\)") nil t) 343 "\\){[ \t]*\\([^}]+\\)") nil t)
342 (setq files 344 (setq files
343 (split-string (reftex-match-string 3) 345 (split-string (reftex-match-string 3)
344 "[ \t\n\r]*,[ \t\n\r]*"))))) 346 "[ \t\n\r]*,[ \t\n\r]*")))))
345 (when files 347 (when files
346 (setq files 348 (setq files
347 (mapcar 349 (mapcar
348 (lambda (x) 350 (lambda (x)
349 (if (or (member x reftex-bibfile-ignore-list) 351 (if (or (member x reftex-bibfile-ignore-list)
350 (delq nil (mapcar (lambda (re) (string-match re x)) 352 (delq nil (mapcar (lambda (re) (string-match re x))
351 reftex-bibfile-ignore-regexps))) 353 reftex-bibfile-ignore-regexps)))
352 ;; excluded file 354 ;; excluded file
353 nil 355 nil
354 ;; find the file 356 ;; find the file
355 (reftex-locate-file x "bib" master-dir))) 357 (reftex-locate-file x "bib" master-dir)))
356 files)) 358 files))
357 (delq nil files))) 359 (delq nil files)))
358 360
359 (defun reftex-replace-label-list-segment (old insert &optional entirely) 361 (defun reftex-replace-label-list-segment (old insert &optional entirely)
360 ;; Replace the segment in OLD which corresponds to INSERT. 362 ;; Replace the segment in OLD which corresponds to INSERT.
361 ;; Works with side effects, directly changes old. 363 ;; Works with side effects, directly changes old.
384 (defun reftex-section-info (file) 386 (defun reftex-section-info (file)
385 ;; Return a section entry for the current match. 387 ;; Return a section entry for the current match.
386 ;; Carefull: This function expects the match-data to be still in place! 388 ;; Carefull: This function expects the match-data to be still in place!
387 (let* ((marker (set-marker (make-marker) (1- (match-beginning 3)))) 389 (let* ((marker (set-marker (make-marker) (1- (match-beginning 3))))
388 (macro (reftex-match-string 3)) 390 (macro (reftex-match-string 3))
389 (prefix (save-match-data 391 (prefix (save-match-data
390 (if (string-match "begin{\\([^}]+\\)}" macro) 392 (if (string-match "begin{\\([^}]+\\)}" macro)
391 (match-string 1 macro)))) 393 (match-string 1 macro))))
392 (level-exp (cdr (assoc macro reftex-section-levels-all))) 394 (level-exp (cdr (assoc macro reftex-section-levels-all)))
393 (level (if (symbolp level-exp) 395 (level (if (symbolp level-exp)
394 (save-match-data (funcall level-exp)) 396 (save-match-data (funcall level-exp))
395 level-exp)) 397 level-exp))
396 (star (= ?* (char-after (match-end 3)))) 398 (star (= ?* (char-after (match-end 3))))
397 (unnumbered (or star (< level 0))) 399 (unnumbered (or star (< level 0)))
398 (level (abs level)) 400 (level (abs level))
399 (section-number (reftex-section-number level unnumbered)) 401 (section-number (reftex-section-number level unnumbered))
400 (text1 (save-match-data 402 (text1 (save-match-data
401 (save-excursion 403 (save-excursion
402 (reftex-context-substring prefix)))) 404 (reftex-context-substring prefix))))
403 (literal (buffer-substring-no-properties 405 (literal (buffer-substring-no-properties
404 (1- (match-beginning 3)) 406 (1- (match-beginning 3))
405 (min (point-max) (+ (match-end 0) (length text1) 1)))) 407 (min (point-max) (+ (match-end 0) (length text1) 1))))
406 ;; Literal can be too short since text1 too short. No big problem. 408 ;; Literal can be too short since text1 too short. No big problem.
407 (text (reftex-nicify-text text1))) 409 (text (reftex-nicify-text text1)))
408 410
409 ;; Add section number and indentation 411 ;; Add section number and indentation
410 (setq text 412 (setq text
411 (concat 413 (concat
412 (make-string (* reftex-level-indent level) ?\ ) 414 (make-string (* reftex-level-indent level) ?\ )
413 (if (nth 1 reftex-label-menu-flags) ; section number flag 415 (if (nth 1 reftex-label-menu-flags) ; section number flag
414 (concat section-number " ")) 416 (concat section-number " "))
415 (if prefix (concat (capitalize prefix) ": ") "") 417 (if prefix (concat (capitalize prefix) ": ") "")
416 text)) 418 text))
417 (list 'toc "toc" text file marker level section-number 419 (list 'toc "toc" text file marker level section-number
418 literal (marker-position marker)))) 420 literal (marker-position marker))))
419 421
420 (defun reftex-ensure-index-support (&optional abort) 422 (defun reftex-ensure-index-support (&optional abort)
423 ;; will rescan the entire document. 425 ;; will rescan the entire document.
424 (cond 426 (cond
425 (reftex-support-index t) 427 (reftex-support-index t)
426 ((y-or-n-p "Turn on index support and rescan entire document? ") 428 ((y-or-n-p "Turn on index support and rescan entire document? ")
427 (setq reftex-support-index 'demanded 429 (setq reftex-support-index 'demanded
428 current-prefix-arg '(16))) 430 current-prefix-arg '(16)))
429 (t (if abort 431 (t (if abort
430 (error "No index support") 432 (error "No index support")
431 (message "No index support") 433 (message "No index support")
432 (ding) 434 (ding)
433 (sit-for 1))))) 435 (sit-for 1)))))
434 436
435 (defun reftex-index-info-safe (file) 437 (defun reftex-index-info-safe (file)
436 (reftex-with-special-syntax 438 (reftex-with-special-syntax
437 (reftex-index-info file))) 439 (reftex-index-info file)))
438 440
440 (defun reftex-index-info (file) 442 (defun reftex-index-info (file)
441 ;; Return an index entry for the current match. 443 ;; Return an index entry for the current match.
442 ;; Carefull: This function expects the match-data to be still in place! 444 ;; Carefull: This function expects the match-data to be still in place!
443 (catch 'exit 445 (catch 'exit
444 (let* ((macro (reftex-match-string 10)) 446 (let* ((macro (reftex-match-string 10))
445 (bom (match-beginning 10)) 447 (bom (match-beginning 10))
446 (boa (match-end 10)) 448 (boa (match-end 10))
447 (entry (or (assoc macro reftex-index-macro-alist) 449 (entry (or (assoc macro reftex-index-macro-alist)
448 (throw 'exit nil))) 450 (throw 'exit nil)))
449 (exclude (nth 3 entry)) 451 (exclude (nth 3 entry))
450 ;; The following is a test if this match should be excluded 452 ;; The following is a test if this match should be excluded
451 (test-dummy (and (fboundp exclude) 453 (test-dummy (and (fboundp exclude)
452 (funcall exclude) 454 (funcall exclude)
453 (throw 'exit nil))) 455 (throw 'exit nil)))
454 (itag (nth 1 entry)) 456 (itag (nth 1 entry))
455 (prefix (nth 2 entry)) 457 (prefix (nth 2 entry))
456 (index-tag 458 (index-tag
457 (cond ((stringp itag) itag) 459 (cond ((stringp itag) itag)
458 ((integerp itag) 460 ((integerp itag)
459 (progn (goto-char boa) 461 (progn (goto-char boa)
460 (or (reftex-nth-arg itag (nth 6 entry)) "idx"))) 462 (or (reftex-nth-arg itag (nth 6 entry)) "idx")))
461 (t "idx"))) 463 (t "idx")))
462 (arg (or (progn (goto-char boa) 464 (arg (or (progn (goto-char boa)
463 (reftex-nth-arg (nth 5 entry) (nth 6 entry))) 465 (reftex-nth-arg (nth 5 entry) (nth 6 entry)))
464 "")) 466 ""))
465 (end-of-args (progn (goto-char boa) 467 (end-of-args (progn (goto-char boa)
466 (reftex-move-over-touching-args) 468 (reftex-move-over-touching-args)
467 (point))) 469 (point)))
468 (end-of-context (progn (skip-chars-forward "^ \t\n\r") (point))) 470 (end-of-context (progn (skip-chars-forward "^ \t\n\r") (point)))
469 (begin-of-context 471 (begin-of-context
470 (progn (goto-char bom) 472 (progn (goto-char bom)
471 (skip-chars-backward "^ \t\r\n") 473 (skip-chars-backward "^ \t\r\n")
472 (point))) 474 (point)))
473 (context (buffer-substring-no-properties 475 (context (buffer-substring-no-properties
474 begin-of-context end-of-context)) 476 begin-of-context end-of-context))
475 (key-end (if (string-match reftex-index-key-end-re arg) 477 (key-end (if (string-match reftex-index-key-end-re arg)
476 (1+ (match-beginning 0)))) 478 (1+ (match-beginning 0))))
477 (rawkey (substring arg 0 key-end)) 479 (rawkey (substring arg 0 key-end))
478 480
479 (key (if prefix (concat prefix rawkey) rawkey)) 481 (key (if prefix (concat prefix rawkey) rawkey))
480 (sortkey (downcase key)) 482 (sortkey (downcase key))
481 (showkey (mapconcat 'identity 483 (showkey (mapconcat 'identity
482 (split-string key reftex-index-level-re) 484 (split-string key reftex-index-level-re)
483 " ! "))) 485 " ! ")))
484 (goto-char end-of-args) 486 (goto-char end-of-args)
485 ;; 0 1 2 3 4 5 6 7 8 9 487 ;; 0 1 2 3 4 5 6 7 8 9
486 (list 'index index-tag context file bom arg key showkey sortkey key-end)))) 488 (list 'index index-tag context file bom arg key showkey sortkey key-end))))
487 489
488 (defun reftex-short-context (env parse &optional bound derive) 490 (defun reftex-short-context (env parse &optional bound derive)
489 ;; Get about one line of useful context for the label definition at point. 491 ;; Get about one line of useful context for the label definition at point.
490 492
491 (if (consp parse) 493 (if (consp parse)
492 (setq parse (if derive (cdr parse) (car parse)))) 494 (setq parse (if derive (cdr parse) (car parse))))
513 (progn 515 (progn
514 (string-match "{\\([^}]*\\)" (nth 7 reftex-active-toc)) 516 (string-match "{\\([^}]*\\)" (nth 7 reftex-active-toc))
515 (match-string 1 (nth 7 reftex-active-toc))) 517 (match-string 1 (nth 7 reftex-active-toc)))
516 "SECTION HEADING NOT FOUND"))) 518 "SECTION HEADING NOT FOUND")))
517 (save-excursion 519 (save-excursion
518 (goto-char reftex-default-context-position) 520 (goto-char reftex-default-context-position)
519 (unless (eq (string-to-char env) ?\\) 521 (unless (eq (string-to-char env) ?\\)
520 (reftex-move-over-touching-args)) 522 (reftex-move-over-touching-args))
521 (reftex-context-substring)))) 523 (reftex-context-substring))))
522 524
523 ((stringp parse) 525 ((stringp parse)
524 (save-excursion 526 (save-excursion
525 (if (re-search-backward parse bound t) 527 (if (re-search-backward parse bound t)
541 (save-excursion 543 (save-excursion
542 (condition-case error-var 544 (condition-case error-var
543 (funcall parse env) 545 (funcall parse env)
544 (error (format "HOOK ERROR: %s" (cdr error-var)))))) 546 (error (format "HOOK ERROR: %s" (cdr error-var))))))
545 (t 547 (t
546 "ILLEGAL VALUE OF PARSE")))) 548 "INVALID VALUE OF PARSE"))))
547 549
548 (defun reftex-where-am-I () 550 (defun reftex-where-am-I ()
549 ;; Return the docstruct entry above point. Actually returns a cons 551 ;; Return the docstruct entry above point. Actually returns a cons
550 ;; cell in which the cdr is a flag indicating if the information is 552 ;; cell in which the cdr is a flag indicating if the information is
551 ;; exact (t) or approximate (nil). 553 ;; exact (t) or approximate (nil).
590 ;; Fits with marker position or recorded position 592 ;; Fits with marker position or recorded position
591 (setq rtn1 (car list) list nil)) 593 (setq rtn1 (car list) list nil))
592 ((looking-at (reftex-make-regexp-allow-for-ctrl-m 594 ((looking-at (reftex-make-regexp-allow-for-ctrl-m
593 (nth 7 (car list)))) 595 (nth 7 (car list))))
594 ;; Same title: remember, but keep looking 596 ;; Same title: remember, but keep looking
595 (setq rtn-if-no-other (car list))))) 597 (setq rtn-if-no-other (car list)))))
596 (pop list)) 598 (pop list))
597 rtn1)) 599 rtn1))
598 ((match-end 7) 600 ((match-end 7)
599 ;; Input or include... 601 ;; Input or include...
600 (car 602 (car
601 (member (list 'eof (reftex-locate-file 603 (member (list 'eof (reftex-locate-file
602 (reftex-match-string 7) "tex" 604 (reftex-match-string 7) "tex"
603 (cdr (assq 'master-dir docstruct)))) 605 (cdr (assq 'master-dir docstruct))))
604 docstruct))) 606 docstruct)))
605 ((match-end 9) 607 ((match-end 9)
606 (assq 'appendix (symbol-value reftex-docstruct-symbol))) 608 (assq 'appendix (symbol-value reftex-docstruct-symbol)))
607 ((match-end 10) 609 ((match-end 10)
608 ;; Index entry 610 ;; Index entry
609 (when reftex-support-index 611 (when reftex-support-index
610 (let* ((index-info (save-excursion 612 (let* ((index-info (save-excursion
611 (reftex-index-info-safe nil))) 613 (reftex-index-info-safe nil)))
612 (list (member (list 'bof (buffer-file-name)) 614 (list (member (list 'bof (buffer-file-name))
613 docstruct)) 615 docstruct))
614 (endelt (car (member (list 'eof (buffer-file-name)) 616 (endelt (car (member (list 'eof (buffer-file-name))
615 list))) 617 list)))
616 dist last-dist last (n 0)) 618 dist last-dist last (n 0))
617 ;; Check all index entries with equal text 619 ;; Check all index entries with equal text
618 (while (and list (not (eq endelt (car list)))) 620 (while (and list (not (eq endelt (car list))))
619 (when (and (eq (car (car list)) 'index) 621 (when (and (eq (car (car list)) 'index)
620 (string= (nth 2 index-info) 622 (string= (nth 2 index-info)
621 (nth 2 (car list)))) 623 (nth 2 (car list))))
622 (incf n) 624 (incf n)
623 (setq dist (abs (- (point) (nth 4 (car list))))) 625 (setq dist (abs (- (point) (nth 4 (car list)))))
624 (if (or (not last-dist) (< dist last-dist)) 626 (if (or (not last-dist) (< dist last-dist))
625 (setq last-dist dist last (car list)))) 627 (setq last-dist dist last (car list))))
626 (setq list (cdr list))) 628 (setq list (cdr list)))
627 ;; We are sure if we have only one, or a zero distance 629 ;; We are sure if we have only one, or a zero distance
628 (cond ((or (= n 1) (= dist 0)) last) 630 (cond ((or (= n 1) (equal dist 0)) last)
629 ((> n 1) (setq cnt 2) last) 631 ((> n 1) (setq cnt 2) last)
630 (t nil))))) 632 (t nil)))))
631 ((match-end 11) 633 ((match-end 11)
632 (save-excursion 634 (save-excursion
633 (goto-char (match-end 11)) 635 (goto-char (match-end 11))
634 (assoc (reftex-no-props 636 (assoc (reftex-no-props
635 (reftex-nth-arg-wrapper 637 (reftex-nth-arg-wrapper
636 (reftex-match-string 11))) 638 (reftex-match-string 11)))
638 (t 640 (t
639 (error "This should not happen (reftex-where-am-I)")))))) 641 (error "This should not happen (reftex-where-am-I)"))))))
640 ;; Check if there was only a by-name match for the section. 642 ;; Check if there was only a by-name match for the section.
641 (when (and (not rtn) rtn-if-no-other) 643 (when (and (not rtn) rtn-if-no-other)
642 (setq rtn rtn-if-no-other 644 (setq rtn rtn-if-no-other
643 cnt 2)) 645 cnt 2))
644 (cons rtn (eq cnt 1)))) 646 (cons rtn (eq cnt 1))))
645 647
646 (defun reftex-notice-new (&optional n force) 648 (defun reftex-notice-new (&optional n force)
647 "Hook to handshake with RefTeX after something new has been inserted." 649 "Hook to handshake with RefTeX after something new has been inserted."
648 ;; Add a new entry to the docstruct list. If it is a section, renumber 650 ;; Add a new entry to the docstruct list. If it is a section, renumber
649 ;; the following sections. 651 ;; the following sections.
650 ;; FIXME: Put in a WHAT parameter 652 ;; FIXME: Put in a WHAT parameter and search backward until one is found.
651 ;; When N is given, go back that many matches of reftex-everything-regexp 653 ;; When N is given, go back that many matches of reftex-everything-regexp
652 ;; When FORCE is non-nil, also insert if `reftex-where-am-I' was uncertain. 654 ;; When FORCE is non-nil, also insert if `reftex-where-am-I' was uncertain.
653 (condition-case nil 655 (condition-case nil
654 (catch 'exit 656 (catch 'exit
655 (unless reftex-mode (throw 'exit nil)) 657 (unless reftex-mode (throw 'exit nil))
656 (reftex-access-scan-info) 658 (reftex-access-scan-info)
657 (let* ((docstruct (symbol-value reftex-docstruct-symbol)) 659 (let* ((docstruct (symbol-value reftex-docstruct-symbol))
658 here-I-am appendix tail entry star level 660 here-I-am appendix tail entry star level
659 section-number context) 661 section-number context)
660 662
661 (save-excursion 663 (save-excursion
662 (when (re-search-backward (reftex-everything-regexp) nil t (or n 1)) 664 (when (re-search-backward (reftex-everything-regexp) nil t (or n 1))
663 665
664 ;; Find where we are 666 ;; Find where we are
665 (setq here-I-am (reftex-where-am-I)) 667 (setq here-I-am (reftex-where-am-I))
666 (or here-I-am (throw 'exit nil)) 668 (or here-I-am (throw 'exit nil))
667 (unless (or force (cdr here-I-am)) (throw 'exit nil)) 669 (unless (or force (cdr here-I-am)) (throw 'exit nil))
668 (setq tail (memq (car here-I-am) docstruct)) 670 (setq tail (memq (car here-I-am) docstruct))
669 (or tail (throw 'exit nil)) 671 (or tail (throw 'exit nil))
670 (setq reftex-active-toc (reftex-last-assoc-before-elt 672 (setq reftex-active-toc (reftex-last-assoc-before-elt
671 'toc (car here-I-am) docstruct) 673 'toc (car here-I-am) docstruct)
672 appendix (reftex-last-assoc-before-elt 674 appendix (reftex-last-assoc-before-elt
673 'appendix (car here-I-am) docstruct)) 675 'appendix (car here-I-am) docstruct))
674 676
675 ;; Initialize section numbers 677 ;; Initialize section numbers
676 (if (eq (car (car here-I-am)) 'appendix) 678 (if (eq (car (car here-I-am)) 'appendix)
677 (reftex-init-section-numbers nil t) 679 (reftex-init-section-numbers nil t)
678 (reftex-init-section-numbers reftex-active-toc appendix)) 680 (reftex-init-section-numbers reftex-active-toc appendix))
679 681
680 ;; Match the section command 682 ;; Match the section command
681 (when (re-search-forward (reftex-everything-regexp) nil t) 683 (when (re-search-forward (reftex-everything-regexp) nil t)
682 (cond 684 (cond
683 ((match-end 1) 685 ((match-end 1)
684 (push (reftex-label-info (reftex-match-string 1) buffer-file-name) 686 (push (reftex-label-info (reftex-match-string 1) buffer-file-name)
685 (cdr tail))) 687 (cdr tail)))
686 688
687 ((match-end 3) 689 ((match-end 3)
688 (setq star (= ?* (char-after (match-end 3))) 690 (setq star (= ?* (char-after (match-end 3)))
689 entry (reftex-section-info (buffer-file-name)) 691 entry (reftex-section-info (buffer-file-name))
690 level (nth 5 entry)) 692 level (nth 5 entry))
691 ;; Insert the section info 693 ;; Insert the section info
692 (push entry (cdr tail)) 694 (push entry (cdr tail))
693 695
694 ;; We are done unless we use section numbers 696 ;; We are done unless we use section numbers
695 (unless (nth 1 reftex-label-menu-flags) (throw 'exit nil)) 697 (unless (nth 1 reftex-label-menu-flags) (throw 'exit nil))
696 698
697 ;; Update the remaining toc items 699 ;; Update the remaining toc items
698 (setq tail (cdr tail)) 700 (setq tail (cdr tail))
699 (while (and (setq tail (memq (assq 'toc (cdr tail)) tail)) 701 (while (and (setq tail (memq (assq 'toc (cdr tail)) tail))
700 (setq entry (car tail)) 702 (setq entry (car tail))
701 (>= (nth 5 entry) level)) 703 (>= (nth 5 entry) level))
702 (setq star (string-match "\\*" (nth 6 entry)) 704 (setq star (string-match "\\*" (nth 6 entry))
703 context (nth 2 entry) 705 context (nth 2 entry)
704 section-number 706 section-number
705 (reftex-section-number (nth 5 entry) star)) 707 (reftex-section-number (nth 5 entry) star))
706 (when (string-match "\\`\\([ \t]*\\)\\([.0-9A-Z]+\\)\\(.*\\)" 708 (when (string-match "\\`\\([ \t]*\\)\\([.0-9A-Z]+\\)\\(.*\\)"
707 context) 709 context)
708 (when (and (not appendix) 710 (when (and (not appendix)
709 (>= (string-to-char (match-string 2)) ?A)) 711 (>= (string-to-char (match-string 2)) ?A))
710 ;; Just entered the appendex. Get out. 712 ;; Just entered the appendex. Get out.
711 (throw 'exit nil)) 713 (throw 'exit nil))
712 714
713 ;; Change the section number. 715 ;; Change the section number.
714 (setf (nth 2 entry) 716 (setf (nth 2 entry)
715 (concat (match-string 1 context) 717 (concat (match-string 1 context)
716 section-number 718 section-number
717 (match-string 3 context)))))) 719 (match-string 3 context))))))
718 ((match-end 10) 720 ((match-end 10)
719 ;; Index entry 721 ;; Index entry
720 (and reftex-support-index 722 (and reftex-support-index
721 (setq entry (reftex-index-info-safe buffer-file-name)) 723 (setq entry (reftex-index-info-safe buffer-file-name))
722 ;; FIXME: (add-to-list 'index-tags (nth 1 index-entry)) 724 ;; FIXME: (add-to-list 'index-tags (nth 1 index-entry))
723 (push entry (cdr tail)))))))))) 725 (push entry (cdr tail))))))))))
724 726
725 (error nil)) 727 (error nil))
726 ) 728 )
727 729
728 (defsubst reftex-move-to-previous-arg (&optional bound) 730 (defsubst reftex-move-to-previous-arg (&optional bound)
729 ;; Assuming that we are in front of a macro argument, 731 ;; Assuming that we are in front of a macro argument,
733 (cond 735 (cond
734 ;; Just to be quick: 736 ;; Just to be quick:
735 ((memq (preceding-char) '(?\] ?\}))) 737 ((memq (preceding-char) '(?\] ?\})))
736 ;; Do a search 738 ;; Do a search
737 ((and reftex-allow-detached-macro-args 739 ((and reftex-allow-detached-macro-args
738 (re-search-backward 740 (re-search-backward
739 "[]}][ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*\\=" bound t)) 741 "[]}][ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*\\=" bound t))
740 (goto-char (1+ (match-beginning 0))) 742 (goto-char (1+ (match-beginning 0)))
741 t) 743 t)
742 (t nil))) 744 (t nil)))
743 745
744 (defun reftex-what-macro-safe (which &optional bound) 746 (defun reftex-what-macro-safe (which &optional bound)
779 (progn (up-list -1) t) 781 (progn (up-list -1) t)
780 (error nil)) 782 (error nil))
781 (setq cnt 1 cnt-opt 0) 783 (setq cnt 1 cnt-opt 0)
782 ;; move back over any touching sexps 784 ;; move back over any touching sexps
783 (while (and (reftex-move-to-previous-arg bound) 785 (while (and (reftex-move-to-previous-arg bound)
784 (condition-case nil 786 (condition-case nil
785 (progn (backward-sexp) t) 787 (progn (backward-sexp) t)
786 (error nil))) 788 (error nil)))
787 (if (eq (following-char) ?\[) (incf cnt-opt)) 789 (if (eq (following-char) ?\[) (incf cnt-opt))
788 (incf cnt)) 790 (incf cnt))
789 (setq pos (point)) 791 (setq pos (point))
790 (when (and (or (= (following-char) ?\[) 792 (when (and (or (= (following-char) ?\[)
791 (= (following-char) ?\{)) 793 (= (following-char) ?\{))
792 (re-search-backward "\\\\[*a-zA-Z]+\\=" nil t)) 794 (re-search-backward "\\\\[*a-zA-Z]+\\=" nil t))
793 (setq cmd (reftex-match-string 0)) 795 (setq cmd (reftex-match-string 0))
794 (when (looking-at "\\\\begin{[^}]*}") 796 (when (looking-at "\\\\begin{[^}]*}")
795 (setq cmd (reftex-match-string 0) 797 (setq cmd (reftex-match-string 0)
796 cnt (1- cnt))) 798 cnt (1- cnt)))
797 ;; This does ignore optional arguments. Very hard to fix. 799 ;; This does ignore optional arguments. Very hard to fix.
798 (when (setq entry (assoc cmd reftex-env-or-mac-alist)) 800 (when (setq entry (assoc cmd reftex-env-or-mac-alist))
799 (if (> cnt (or (nth 4 entry) 100)) 801 (if (> cnt (or (nth 4 entry) 100))
800 (setq cmd nil))) 802 (setq cmd nil)))
801 (cond 803 (cond
802 ((null cmd)) 804 ((null cmd))
803 ((eq t which) 805 ((eq t which)
804 (push (cons cmd (point)) cmd-list)) 806 (push (cons cmd (point)) cmd-list))
805 ((or (eq 1 which) (member cmd which)) 807 ((or (eq 1 which) (member cmd which))
806 (throw 'exit (cons cmd (point)))))) 808 (throw 'exit (cons cmd (point))))))
807 (goto-char pos))) 809 (goto-char pos)))
808 (nreverse cmd-list))))) 810 (nreverse cmd-list)))))
809 811
810 (defun reftex-what-environment (which &optional bound) 812 (defun reftex-what-environment (which &optional bound)
811 ;; Find out if point is inside a LaTeX environment. 813 ;; Find out if point is inside a LaTeX environment.
834 bound t) 836 bound t)
835 (setq env (buffer-substring-no-properties 837 (setq env (buffer-substring-no-properties
836 (match-beginning 2) (match-end 2))) 838 (match-beginning 2) (match-end 2)))
837 (cond 839 (cond
838 ((string= (match-string 1) "end") 840 ((string= (match-string 1) "end")
839 (push env end-list)) 841 (push env end-list))
840 ((equal env (car end-list)) 842 ((equal env (car end-list))
841 (setq end-list (cdr end-list))) 843 (setq end-list (cdr end-list)))
842 ((eq t which) 844 ((eq t which)
843 (push (cons env (point)) env-list)) 845 (push (cons env (point)) env-list))
844 ((or (eq 1 which) (member env which)) 846 ((or (eq 1 which) (member env which))
864 (if (null reftex-special-env-parsers) (throw 'exit nil)) 866 (if (null reftex-special-env-parsers) (throw 'exit nil))
865 (if (null which) (throw 'exit nil)) 867 (if (null which) (throw 'exit nil))
866 (let ((bound (or bound (save-excursion (re-search-backward 868 (let ((bound (or bound (save-excursion (re-search-backward
867 reftex-section-regexp nil 1) 869 reftex-section-regexp nil 1)
868 (point)))) 870 (point))))
869 (fun-list (if (listp which) 871 (fun-list (if (listp which)
870 (mapcar (lambda (x) (if (memq x which) x nil)) 872 (mapcar (lambda (x) (if (memq x which) x nil))
871 reftex-special-env-parsers) 873 reftex-special-env-parsers)
872 reftex-special-env-parsers)) 874 reftex-special-env-parsers))
873 specials rtn) 875 specials rtn)
874 ;; Call all functions 876 ;; Call all functions
875 (setq specials (mapcar 877 (setq specials (mapcar
876 (lambda (fun) 878 (lambda (fun)
877 (save-excursion 879 (save-excursion
878 (setq rtn (and fun (funcall fun bound))) 880 (setq rtn (and fun (funcall fun bound)))
879 (if rtn (cons (symbol-name fun) rtn) nil))) 881 (if rtn (cons (symbol-name fun) rtn) nil)))
880 fun-list)) 882 fun-list))
881 ;; Delete the non-matches 883 ;; Delete the non-matches
882 (setq specials (delq nil specials)) 884 (setq specials (delq nil specials))
883 ;; Sort 885 ;; Sort
884 (setq specials (sort specials (lambda (a b) (> (cdr a) (cdr b))))) 886 (setq specials (sort specials (lambda (a b) (> (cdr a) (cdr b)))))
885 (if (eq which t) 887 (if (eq which t)
886 specials 888 specials
887 (car specials)))))) 889 (car specials))))))
888 890
889 (defsubst reftex-move-to-next-arg (&optional ignore) 891 (defsubst reftex-move-to-next-arg (&optional ignore)
890 ;; Assuming that we are at the end of a macro name or a macro argument, 892 ;; Assuming that we are at the end of a macro name or a macro argument,
891 ;; move forward to the opening parenthesis of the next argument. 893 ;; move forward to the opening parenthesis of the next argument.
892 ;; This function understands the splitting of macros over several lines 894 ;; This function understands the splitting of macros over several lines
894 (cond 896 (cond
895 ;; Just to be quick: 897 ;; Just to be quick:
896 ((memq (following-char) '(?\[ ?\{))) 898 ((memq (following-char) '(?\[ ?\{)))
897 ;; Do a search 899 ;; Do a search
898 ((and reftex-allow-detached-macro-args 900 ((and reftex-allow-detached-macro-args
899 (looking-at "[ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*[[{]")) 901 (looking-at "[ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*[[{]"))
900 (goto-char (1- (match-end 0))) 902 (goto-char (1- (match-end 0)))
901 t) 903 t)
902 (t nil))) 904 (t nil)))
903 905
904 (defun reftex-nth-arg-wrapper (key) 906 (defun reftex-nth-arg-wrapper (key)
913 (and (eq (following-char) ?\\) (skip-chars-forward "a-zA-Z*\\\\")) 915 (and (eq (following-char) ?\\) (skip-chars-forward "a-zA-Z*\\\\"))
914 916
915 (if (= n 1000) 917 (if (= n 1000)
916 ;; Special case: Skip all touching arguments 918 ;; Special case: Skip all touching arguments
917 (progn 919 (progn
918 (reftex-move-over-touching-args) 920 (reftex-move-over-touching-args)
919 (reftex-context-substring)) 921 (reftex-context-substring))
920 922
921 ;; Do the real thing. 923 ;; Do the real thing.
922 (let ((cnt 1)) 924 (let ((cnt 1))
923 925
924 (when (reftex-move-to-next-arg) 926 (when (reftex-move-to-next-arg)
925 927
926 (while (< cnt n) 928 (while (< cnt n)
927 (while (and (member cnt opt-args) 929 (while (and (member cnt opt-args)
928 (eq (following-char) ?\{)) 930 (eq (following-char) ?\{))
929 (incf cnt)) 931 (incf cnt))
930 (when (< cnt n) 932 (when (< cnt n)
931 (unless (and (condition-case nil 933 (unless (and (condition-case nil
932 (or (forward-list 1) t) 934 (or (forward-list 1) t)
933 (error nil)) 935 (error nil))
934 (reftex-move-to-next-arg) 936 (reftex-move-to-next-arg)
935 (incf cnt)) 937 (incf cnt))
936 (setq cnt 1000)))) 938 (setq cnt 1000))))
937 939
938 (while (and (memq cnt opt-args) 940 (while (and (memq cnt opt-args)
939 (eq (following-char) ?\{)) 941 (eq (following-char) ?\{))
940 (incf cnt))) 942 (incf cnt)))
941 (if (and (= n cnt) 943 (if (and (= n cnt)
942 (> (skip-chars-forward "{\\[") 0)) 944 (> (skip-chars-forward "{\\[") 0))
943 (reftex-context-substring) 945 (reftex-context-substring)
944 nil)))) 946 nil))))
945 947
946 (defun reftex-move-over-touching-args () 948 (defun reftex-move-over-touching-args ()
947 (condition-case nil 949 (condition-case nil
948 (while (memq (following-char) '(?\[ ?\{)) 950 (while (memq (following-char) '(?\[ ?\{))
949 (forward-list 1)) 951 (forward-list 1))
950 (error nil))) 952 (error nil)))
951 953
952 (defun reftex-context-substring (&optional to-end) 954 (defun reftex-context-substring (&optional to-end)
953 ;; Return up to 150 chars from point 955 ;; Return up to 150 chars from point
954 ;; When point is just after a { or [, limit string to matching parenthesis 956 ;; When point is just after a { or [, limit string to matching parenthesis
955 (cond 957 (cond
956 (to-end 958 (to-end
957 ;; Environment - find next \end 959 ;; Environment - find next \end
958 (buffer-substring-no-properties 960 (buffer-substring-no-properties
959 (point) 961 (point)
960 (min (+ (point) 150) 962 (min (+ (point) 150)
961 (save-match-data 963 (save-match-data
962 ;; FIXME: THis is not perfect 964 ;; FIXME: This is not perfect
963 (if (re-search-forward "\\\\end{" nil t) 965 (if (re-search-forward "\\\\end{" nil t)
964 (match-beginning 0) 966 (match-beginning 0)
965 (point-max)))))) 967 (point-max))))))
966 ((or (= (preceding-char) ?\{) 968 ((or (= (preceding-char) ?\{)
967 (= (preceding-char) ?\[)) 969 (= (preceding-char) ?\[))
968 ;; Inside a list - get only the list. 970 ;; Inside a list - get only the list.
969 (buffer-substring-no-properties 971 (buffer-substring-no-properties
970 (point) 972 (point)
975 (up-list 1) 977 (up-list 1)
976 (1- (point))) 978 (1- (point)))
977 (error (point-max)))))) 979 (error (point-max))))))
978 (t 980 (t
979 ;; no list - just grab 150 characters 981 ;; no list - just grab 150 characters
980 (buffer-substring-no-properties (point) 982 (buffer-substring-no-properties (point)
981 (min (+ (point) 150) (point-max)))))) 983 (min (+ (point) 150) (point-max))))))
982 984
983 ;; Variable holding the vector with section numbers 985 ;; Variable holding the vector with section numbers
984 (defvar reftex-section-numbers (make-vector reftex-max-section-depth 0)) 986 (defvar reftex-section-numbers (make-vector reftex-max-section-depth 0))
985 987
986 (defun reftex-init-section-numbers (&optional toc-entry appendix) 988 (defun reftex-init-section-numbers (&optional toc-entry appendix)
991 (depth (1- (length reftex-section-numbers))) 993 (depth (1- (length reftex-section-numbers)))
992 (i depth) number-string) 994 (i depth) number-string)
993 (while (>= i 0) 995 (while (>= i 0)
994 (if (> i level) 996 (if (> i level)
995 (aset reftex-section-numbers i 0) 997 (aset reftex-section-numbers i 0)
996 (setq number-string (or (car numbers) "0")) 998 (setq number-string (or (car numbers) "0"))
997 (if (string-match "\\`[A-Z]\\'" number-string) 999 (if (string-match "\\`[A-Z]\\'" number-string)
998 (aset reftex-section-numbers i 1000 (aset reftex-section-numbers i
999 (- (string-to-char number-string) ?A -1)) 1001 (- (string-to-char number-string) ?A -1))
1000 (aset reftex-section-numbers i (string-to-int number-string))) 1002 (aset reftex-section-numbers i (string-to-number number-string)))
1001 (pop numbers)) 1003 (pop numbers))
1002 (decf i))) 1004 (decf i)))
1003 (put 'reftex-section-numbers 'appendix appendix)) 1005 (put 'reftex-section-numbers 'appendix appendix))
1004 1006
1005 (defun reftex-section-number (&optional level star) 1007 (defun reftex-section-number (&optional level star)
1006 ;; Return a string with the current section number. 1008 ;; Return a string with the current section number.
1007 ;; When LEVEL is non-nil, increase section numbers on that level. 1009 ;; When LEVEL is non-nil, increase section numbers on that level.
1008 (let* ((depth (1- (length reftex-section-numbers))) idx n (string "") 1010 (let* ((depth (1- (length reftex-section-numbers))) idx n (string "")
1009 (appendix (get 'reftex-section-numbers 'appendix)) 1011 (appendix (get 'reftex-section-numbers 'appendix))
1010 (partspecial (and (not reftex-part-resets-chapter) 1012 (partspecial (and (not reftex-part-resets-chapter)
1011 (equal level 0)))) 1013 (equal level 0))))
1012 ;; partspecial means, this is a part statement. 1014 ;; partspecial means, this is a part statement.
1013 ;; Parts do not reset the chapter counter, and the part number is 1015 ;; Parts do not reset the chapter counter, and the part number is
1014 ;; not included in the numbering of other sectioning levels. 1016 ;; not included in the numbering of other sectioning levels.
1015 (when level 1017 (when level
1016 (when (and (> level -1) (not star)) 1018 (when (and (> level -1) (not star))
1017 (aset reftex-section-numbers 1019 (aset reftex-section-numbers
1018 level (1+ (aref reftex-section-numbers level)))) 1020 level (1+ (aref reftex-section-numbers level))))
1019 (setq idx (1+ level)) 1021 (setq idx (1+ level))
1020 (when (not star) 1022 (when (not star)
1021 (while (<= idx depth) 1023 (while (<= idx depth)
1022 (if (or (not partspecial) 1024 (if (or (not partspecial)
1023 (not (= idx 1))) 1025 (not (= idx 1)))
1024 (aset reftex-section-numbers idx 0)) 1026 (aset reftex-section-numbers idx 0))
1025 (incf idx)))) 1027 (incf idx))))
1026 (if partspecial 1028 (if partspecial
1027 (setq string (concat "Part " (reftex-roman-number 1029 (setq string (concat "Part " (reftex-roman-number
1028 (aref reftex-section-numbers 0)))) 1030 (aref reftex-section-numbers 0))))
1029 (setq idx (if reftex-part-resets-chapter 0 1)) 1031 (setq idx (if reftex-part-resets-chapter 0 1))
1030 (while (<= idx depth) 1032 (while (<= idx depth)
1031 (setq n (aref reftex-section-numbers idx)) 1033 (setq n (aref reftex-section-numbers idx))
1032 (if (not (and partspecial (not (equal string "")))) 1034 (if (not (and partspecial (not (equal string ""))))
1033 (setq string (concat string (if (not (string= string "")) "." "") 1035 (setq string (concat string (if (not (string= string "")) "." "")
1034 (int-to-string n)))) 1036 (int-to-string n))))
1035 (incf idx)) 1037 (incf idx))
1036 (save-match-data 1038 (save-match-data
1037 (if (string-match "\\`\\([@0]\\.\\)+" string) 1039 (if (string-match "\\`\\([@0]\\.\\)+" string)
1038 (setq string (replace-match "" nil nil string))) 1040 (setq string (replace-match "" nil nil string)))
1039 (if (string-match "\\(\\.0\\)+\\'" string) 1041 (if (string-match "\\(\\.0\\)+\\'" string)
1040 (setq string (replace-match "" nil nil string))) 1042 (setq string (replace-match "" nil nil string)))
1041 (if (and appendix 1043 (if (and appendix
1042 (string-match "\\`[0-9]+" string)) 1044 (string-match "\\`[0-9]+" string))
1043 (setq string 1045 (setq string
1044 (concat 1046 (concat
1045 (char-to-string 1047 (char-to-string
1046 (1- (+ ?A (string-to-int (match-string 0 string))))) 1048 (1- (+ ?A (string-to-number (match-string 0 string)))))
1047 (substring string (match-end 0)))))) 1049 (substring string (match-end 0))))))
1048 (if star 1050 (if star
1049 (concat (make-string (1- (length string)) ?\ ) "*") 1051 (concat (make-string (1- (length string)) ?\ ) "*")
1050 string)))) 1052 string))))
1051 1053
1052 (defun reftex-roman-number (n) 1054 (defun reftex-roman-number (n)
1053 ;; Return as a string the roman number equal to N. 1055 ;; Return as a string the roman number equal to N.
1054 (let ((nrest n) 1056 (let ((nrest n)
1055 (string "") 1057 (string "")
1056 (list '((1000 . "M") ( 900 . "CM") ( 500 . "D") ( 400 . "CD") 1058 (list '((1000 . "M") ( 900 . "CM") ( 500 . "D") ( 400 . "CD")
1057 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL") 1059 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
1058 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV") 1060 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
1059 ( 1 . "I"))) 1061 ( 1 . "I")))
1060 listel i s) 1062 listel i s)
1061 (while (>= nrest 1) 1063 (while (>= nrest 1)
1062 (setq listel (pop list) 1064 (setq listel (pop list)
1063 i (car listel) 1065 i (car listel)
1064 s (cdr listel)) 1066 s (cdr listel))
1065 (while (>= nrest i) 1067 (while (>= nrest i)
1066 (setq string (concat string s) 1068 (setq string (concat string s)
1067 nrest (- nrest i)))) 1069 nrest (- nrest i))))
1068 string)) 1070 string))
1069 1071
1072 ;;; arch-tag: 6a8168f7-abb9-4576-99dc-fcbc7ba901a3
1070 ;;; reftex-parse.el ends here 1073 ;;; reftex-parse.el ends here