-
Notifications
You must be signed in to change notification settings - Fork 0
/
yatexadd.el
2150 lines (2010 loc) · 68.3 KB
/
yatexadd.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; yatexadd.el --- YaTeX add-in functions
;;; yatexadd.el rev.20
;;; (c)1991-2013 by HIROSE Yuuji.[[email protected]]
;;; Last modified Mon Apr 1 22:43:00 2013 on firestorm
;;; $Id$
;;; Code:
;;;
;;Sample functions for LaTeX environment.
;;;
(defvar YaTeX:tabular-default-rule
"@{\\vrule width 1pt\\ }c|c|c@{\\ \\vrule width 1pt}"
"*Your favorite default rule format.")
(defvar YaTeX:tabular-thick-vrule "\\vrule width %s"
"*Vertical thick line format (without @{}). %s'll be replaced by its width.")
(defvar YaTeX:tabular-thick-hrule "\\noalign{\\hrule height %s}"
"*Horizontal thick line format. %s will be replaced by its width.")
(defun YaTeX:tabular ()
"YaTeX add-in function for tabular environment.
Notice that this function refers the let-variable `env' in
YaTeX-make-begin-end."
(let ((width "") bars (rule "") (and "") (j 1) loc ans (hline "\\hline"))
(if (string= YaTeX-env-name "tabular*")
(setq width (concat "{" (read-string "Width: ") "}")))
(setq loc (YaTeX:read-position "tb")
bars (string-to-int
(read-string "Number of columns(0 for default format): " "3")))
(if (<= bars 0)
(setq ;if 0, simple format
rule YaTeX:tabular-default-rule
and "& &")
(while (< j bars) ;repeat bars-1 times
(setq rule (concat rule "c|")
and (concat and "& ")
j (1+ j)))
(setq rule (concat rule "c"))
(message "(N)ormal-frame or (T)hick frame? [nt]")
(setq ans (read-char))
(cond
((or (equal ans ?t) (equal ans ?T))
(setq ans (read-string "Rule width: " "1pt")
rule (concat
"@{" (format YaTeX:tabular-thick-vrule ans) "}"
rule
"@{\\ " (format YaTeX:tabular-thick-vrule ans) "}")
hline (format YaTeX:tabular-thick-hrule ans)))
(t (setq rule (concat "|" rule "|")
hline "\\hline"))))
(setq rule (read-string "rule format: " rule))
(setq YaTeX-single-command "hline")
(format "%s%s{%s}" width loc rule)))
(fset 'YaTeX:tabular* 'YaTeX:tabular)
(fset 'YaTeX:supertabular 'YaTeX:tabular)
(defun YaTeX:alignat ()
(concat "{" (read-string "Number of columns: ") "}"))
(defun YaTeX:array ()
(concat (YaTeX:read-position "tb")
"{" (read-string "Column format: ") "}"))
(defun YaTeX:subequations ()
(message (if YaTeX-japan "分かりやすいコメントに変えるとref補完が楽よ"
"Changing comment string reduces effort at `ref' completion"))
(concat " " YaTeX-comment-prefix
(YaTeX::ref-default-label "%H:%M")
(if YaTeX-japan "の式群" "equations")))
(defun YaTeX:read-oneof (oneof &optional quick allow-dup)
(let ((pos "") loc (guide ""))
(and (boundp 'name) name (setq guide (format "%s " name)))
(catch 'quick
(while (not (string-match
(setq loc (read-key-sequence
(format "%s position (`%s') [%s]: "
guide oneof pos));name is in YaTeX-addin
loc (if (fboundp 'events-to-keys)
(events-to-keys loc) loc))
"\r\^g\n"))
(cond
((string-match loc oneof)
(if (or allow-dup (not (string-match loc pos)))
(setq pos (concat pos loc)))
(if quick (throw 'quick t)))
((and (string-match loc "\C-h\C-?") (> (length pos) 0))
(setq pos (substring pos 0 (1- (length pos)))))
(t
(ding)
(message "Please input one of `%s'." oneof)
(sit-for 3)))))
(message "")
pos))
(defun YaTeX:read-position (oneof)
"Read a LaTeX (optional) position format such as `[htbp]'."
(let ((pos (YaTeX:read-oneof oneof)))
(if (string= pos "") "" (concat "[" pos "]"))))
;;;
;; Functions for figure environemnt
;;;
(defvar YaTeX:figure-caption-first nil
"Non-nil indicates put caption before figure.")
(defun YaTeX:figure (&optional type firstp)
"YaTeX add-in function for figure(*) environment."
(setq YaTeX-section-name
(if YaTeX:figure-caption-first "caption" "includegraphics")
YaTeX-env-name "center")
(YaTeX:read-position "htbp"))
(fset 'YaTeX:figure* 'YaTeX:figure)
;;;
;; Functions for table environemnt
;;;
(defvar YaTeX:table-caption-first t
"*Non-nil indicates put caption before tabular.")
(defun YaTeX:table ()
"YaTeX add-in function for table environment."
(cond
((eq major-mode 'yatex-mode)
(setq YaTeX-section-name
(if YaTeX:table-caption-first "caption" "label")
YaTeX-env-name "tabular")
(YaTeX:read-position "htbp"))
((eq major-mode 'texinfo-mode)
(concat " "
(completing-read
"Highlights with: "
'(("@samp")("@kbd")("@code")("@asis")("@file")("@var"))
nil nil "@")))))
(fset 'YaTeX:table* 'YaTeX:table)
(defun YaTeX:description ()
"Truly poor service:-)"
(setq YaTeX-single-command "item[]")
"")
(defun YaTeX:itemize ()
"It's also poor service."
(setq YaTeX-single-command "item")
"")
(defun YaTeX:enumerate ()
(setq YaTeX-single-command "item"
YaTeX-section-name "label")
"")
(defun YaTeX:picture ()
"Ask the size of coordinates of picture environment."
(concat (YaTeX:read-coordinates "Picture size")
(YaTeX:read-coordinates "Initial position")))
(defun YaTeX:equation ()
(YaTeX-jmode-off)
(if (fboundp 'YaTeX-toggle-math-mode)
(YaTeX-toggle-math-mode t))) ;force math-mode ON.
(mapcar '(lambda (f) (fset f 'YaTeX:equation))
'(YaTeX:eqnarray YaTeX:eqnarray* YaTeX:align YaTeX:align*
YaTeX:split YaTeX:multline YaTeX:multline* YaTeX:gather YaTeX:gather*
YaTeX:aligned* YaTeX:gathered YaTeX:gathered*
YaTeX:alignat YaTeX:alignat* YaTeX:xalignat YaTeX:xalignat*
YaTeX:xxalignat YaTeX:xxalignat*))
(defun YaTeX:alignat ()
(YaTeX:equation)
(concat "{" (read-string "Number of cols: ") "}"))
(defun YaTeX:list ()
"%\n{} %default label\n{} %formatting parameter")
(defun YaTeX:minipage ()
(concat (YaTeX:read-position "cbt")
"{" (read-string "Width: ") "}"))
(defun YaTeX:thebibliography ()
(setq YaTeX-section-name "bibitem")
(concat "{" (read-string "Longest label: ") "}"))
(defun YaTeX:multicols ()
(concat "{" (read-string "Number of columns: ") "}"))
;;;
;;Sample functions for section-type command.
;;;
(defun YaTeX:multiput ()
(concat (YaTeX:read-coordinates "Pos")
(YaTeX:read-coordinates "Step")
"{" (read-string "How many times: ") "}"))
(defun YaTeX:put ()
(YaTeX:read-coordinates "Pos"))
(defun YaTeX:makebox ()
(cond
((YaTeX-in-environment-p "picture")
(concat (YaTeX:read-coordinates "Dimension")
(YaTeX:read-position "lsrtb")))
(t
(let ((width (read-string "Width: ")))
(if (string< "" width)
(progn
(or (equal (aref width 0) ?\[)
(setq width (concat "[" width "]")))
(concat width (YaTeX:read-position
(if YaTeX-use-LaTeX2e "lrs" "lr")))))))))
;; (defun YaTeX:framebox ()
;; (if (YaTeX-quick-in-environment-p "picture")
;; (YaTeX:makebox)))
(fset 'YaTeX:framebox 'YaTeX:makebox)
(defun YaTeX:parbox ()
(YaTeX:read-position "tbc"))
(defun YaTeX::parbox (argp)
(cond
((= argp 1) (read-string "Width: "))
((= argp 2) (read-string "Text: "))))
(defun YaTeX::dashbox ()
(concat "{" (read-string "Dash dimension: ") "}"
(YaTeX:read-coordinates "Dimension")))
(defun YaTeX::savebox (argp)
(cond
((= argp 1) (read-string "Saved into name: " "\\"))
((= argp 2) (read-string "Text: "))))
(defvar YaTeX-minibuffer-quick-map nil)
(if YaTeX-minibuffer-quick-map nil
(setq YaTeX-minibuffer-quick-map
(copy-keymap minibuffer-local-completion-map))
(let ((ch (1+ ? )))
(while (< ch 127)
(define-key YaTeX-minibuffer-quick-map (char-to-string ch)
'YaTeX-minibuffer-quick-complete)
(setq ch (1+ ch)))))
(defvar YaTeX:left-right-delimiters
'(("(" . ")") (")" . "(") ("[" . "]") ("]" . "[")
("\\{" . "\\}") ("\\}" . "\\{") ("|") ("\\|")
("\\lfloor" . "\\rfloor") ("\\lceil" . "\\rceil")
("\\langle" . "\\rangle") ("/") (".")
("\\rfloor" . "\\rfloor") ("\\rceil" . "\\lceil")
("\\rangle" . "\\langle") ("\\backslash")
("\\uparrow") ("\\downarrow") ("\\updownarrow") ("\\Updownarrow"))
"TeX math delimiter, which can be completed after \\right or \\left.")
(defvar YaTeX:left-right-default nil "Default string of YaTeX:right.")
(defun YaTeX:left ()
(let ((minibuffer-completion-table YaTeX:left-right-delimiters)
delimiter (leftp (string= YaTeX-single-command "left")))
(setq delimiter
(read-from-minibuffer
(format "Delimiter%s: "
(if YaTeX:left-right-default
(format "(default=`%s')" YaTeX:left-right-default)
"(SPC for menu)"))
nil YaTeX-minibuffer-quick-map))
(if (string= "" delimiter) (setq delimiter YaTeX:left-right-default))
(setq YaTeX-single-command (if leftp "right" "left")
YaTeX:left-right-default
(or (cdr (assoc delimiter YaTeX:left-right-delimiters)) delimiter))
delimiter))
(fset 'YaTeX:right 'YaTeX:left)
(defun YaTeX:langle ()
(setq YaTeX-single-command "rangle")
nil)
(defun YaTeX:read-coordinates (&optional mes varX varY)
(concat
"("
(read-string (format "%s %s: " (or mes "Dimension") (or varX "X")))
","
(read-string (format "%s %s: " (or mes "Dimension") (or varY "Y")))
")"))
(defun YaTeX:itembox ()
(concat "{" (read-string "Item heading string: ") "}"))
;;;
;;Sample functions for maketitle-type command.
;;;
(defun YaTeX:sum ()
"Read range of summation."
(YaTeX:check-completion-type 'maketitle)
(concat (YaTeX:read-boundary "_") (YaTeX:read-boundary "^")))
(fset 'YaTeX:int 'YaTeX:sum)
(defun YaTeX:lim ()
"Insert limit notation of \\lim."
(YaTeX:check-completion-type 'maketitle)
(let ((var (read-string "Variable: ")) limit)
(if (string= "" var) ""
(setq limit (read-string "Limit ($ means infinity): "))
(if (string= "$" limit) (setq limit "\\infty"))
(concat "_{" var " \\rightarrow " limit "}"))))
(defun YaTeX:gcd ()
"Add-in function for \\gcd(m,n)."
(YaTeX:check-completion-type 'maketitle)
(YaTeX:read-coordinates "\\gcd" "(?,)" "(,?)"))
(defun YaTeX:read-boundary (ULchar)
"Read boundary usage by _ or ^. _ or ^ is indicated by argument ULchar."
(let ((bndry (read-string (concat ULchar "{???} ($ for infinity): "))))
(if (string= bndry "") ""
(if (string= bndry "$") (setq bndry "\\infty"))
(concat ULchar "{" bndry "}"))))
(defun YaTeX:verb ()
"Enclose \\verb's contents with the same characters."
(let ((quote-char (read-string "Quoting char: " "|"))
(contents (read-string "Quoted contents: ")))
(concat quote-char contents quote-char)))
(fset 'YaTeX:verb* 'YaTeX:verb)
(defun YaTeX:footnotemark ()
(setq YaTeX-section-name "footnotetext")
nil)
(defun YaTeX:cite ()
(let ((comment (read-string "Comment for citation: ")))
(if (string= comment "") ""
(concat "[" comment "]"))))
(defun YaTeX:bibitem ()
(let ((label (read-string "Citation label for bibitem: ")))
(if (string= label "") ""
(concat "[" label "]"))))
(defun YaTeX:item ()
(cond
((eq major-mode 'yatex-mode)
(YaTeX-indent-line)
(setq YaTeX-section-name "label"))
((eq major-mode 'texinfo-mode)
(setq YaTeX-section-name "dots"))) ;??
" ")
(fset 'YaTeX:item\[\] 'YaTeX:item)
(fset 'YaTeX:subitem 'YaTeX:item)
(fset 'YaTeX:subsubitem 'YaTeX:item)
(defun YaTeX:linebreak ()
(let (obl)
(message "Break strength 0,1,2,3,4 (default: 4): ")
(setq obl (char-to-string (read-char)))
(if (string-match "[0-4]" obl)
(concat "[" obl "]")
"")))
(fset 'YaTeX:pagebreak 'YaTeX:linebreak)
;;;
;;Subroutine
;;;
(defun YaTeX:check-completion-type (type)
"Check valid completion type."
(if (not (eq type YaTeX-current-completion-type))
(error "This should be completed with %s-type completion." type)))
;;;
;;; [[Add-in functions for reading section arguments]]
;;;
;; All of add-in functions for reading sections arguments should
;; take an argument ARGP that specify the argument position.
;; If argument position is out of range, nil should be returned,
;; else nil should NOT be returned.
;;
; Label selection
;;
(defvar YaTeX-label-menu-other
(if YaTeX-japan "':他のバッファのラベル\n" "':LABEL IN OTHER BUFFER.\n"))
(defvar YaTeX-label-menu-repeat
(if YaTeX-japan ".:直前の\\refと同じ\n" "/:REPEAT LAST \ref{}\n"))
(defvar YaTeX-label-menu-any
(if YaTeX-japan "*:任意の文字列\n" "*:ANY STRING.\n"))
(defvar YaTeX-label-buffer "*Label completions*")
(defvar YaTeX-label-guide-msg "Select label and hit RETURN.")
(defvar YaTeX-label-select-map nil
"Key map used in label selection buffer.")
(defun YaTeX::label-setup-key-map ()
(if YaTeX-label-select-map nil
(message "Setting up label selection mode map...")
;(setq YaTeX-label-select-map (copy-keymap global-map))
(setq YaTeX-label-select-map (make-keymap))
(suppress-keymap YaTeX-label-select-map)
(substitute-all-key-definition
'previous-line 'YaTeX::label-previous YaTeX-label-select-map)
(substitute-all-key-definition
'next-line 'YaTeX::label-next YaTeX-label-select-map)
(define-key YaTeX-label-select-map "\C-n" 'YaTeX::label-next)
(define-key YaTeX-label-select-map "\C-p" 'YaTeX::label-previous)
(define-key YaTeX-label-select-map "<" 'beginning-of-buffer)
(define-key YaTeX-label-select-map ">" 'end-of-buffer)
(define-key YaTeX-label-select-map "\C-m" 'exit-recursive-edit)
(define-key YaTeX-label-select-map "\C-j" 'exit-recursive-edit)
(define-key YaTeX-label-select-map " " 'exit-recursive-edit)
(define-key YaTeX-label-select-map "\C-g" 'abort-recursive-edit)
(define-key YaTeX-label-select-map "/" 'isearch-forward)
(define-key YaTeX-label-select-map "?" 'isearch-backward)
(define-key YaTeX-label-select-map "'" 'YaTeX::label-search-tag)
(define-key YaTeX-label-select-map "." 'YaTeX::label-search-tag)
(define-key YaTeX-label-select-map "*" 'YaTeX::label-search-tag)
(message "Setting up label selection mode map...Done")
(let ((key ?A))
(while (<= key ?Z)
(define-key YaTeX-label-select-map (char-to-string key)
'YaTeX::label-search-tag)
(define-key YaTeX-label-select-map (char-to-string (+ key (- ?a ?A)))
'YaTeX::label-search-tag)
(setq key (1+ key))))))
(defun YaTeX::label-next ()
(interactive) (forward-line 1) (message YaTeX-label-guide-msg))
(defun YaTeX::label-previous ()
(interactive) (forward-line -1) (message YaTeX-label-guide-msg))
(defun YaTeX::label-search-tag ()
(interactive)
(let ((case-fold-search t)
(tag (regexp-quote (char-to-string (YaTeX-last-key)))))
(cond
((save-excursion
(forward-char 1)
(re-search-forward (concat "^" tag) nil t))
(goto-char (match-beginning 0)))
((save-excursion
(goto-char (point-min))
(re-search-forward (concat "^" tag) nil t))
(goto-char (match-beginning 0))))
(message YaTeX-label-guide-msg)))
; (defun YaTeX::ref (argp &optional labelcmd refcmd)
; (cond
; ((= argp 1)
; (let ((lnum 0) e0 label label-list (buf (current-buffer))
; (labelcmd (or labelcmd "label")) (refcmd (or refcmd "ref"))
; (p (point)) initl line cf)
; (message "Collecting labels...")
; (save-window-excursion
; (YaTeX-showup-buffer
; YaTeX-label-buffer (function (lambda (x) (window-width x))))
; (if (fboundp 'select-frame) (setq cf (selected-frame)))
; (if (eq (window-buffer (minibuffer-window)) buf)
; (progn
; (other-window 1)
; (setq buf (current-buffer))
; (set-buffer buf)
; ;(message "cb=%s" buf)(sit-for 3)
; ))
; (save-excursion
; (set-buffer (get-buffer-create YaTeX-label-buffer))
; (setq buffer-read-only nil)
; (erase-buffer))
; (save-excursion
; (goto-char (point-min))
; (let ((standard-output (get-buffer YaTeX-label-buffer)))
; (princ (format "=== LABELS in [%s] ===\n" (buffer-name buf)))
; (while (YaTeX-re-search-active-forward
; (concat "\\\\" labelcmd "\\b")
; (regexp-quote YaTeX-comment-prefix) nil t)
; (goto-char (match-beginning 0))
; (skip-chars-forward "^{")
; (setq label
; (buffer-substring
; (1+ (point))
; (prog2 (forward-list 1) (setq e0 (1- (point)))))
; label-list (cons label label-list))
; (or initl
; (if (< p (point)) (setq initl lnum)))
; (beginning-of-line)
; (skip-chars-forward " \t\n" nil)
; (princ (format "%c:{%s}\t<<%s>>\n" (+ (% lnum 26) ?A) label
; (buffer-substring (point) (point-end-of-line))))
; (setq lnum (1+ lnum))
; (message "Collecting \\%s{}... %d" labelcmd lnum)
; (goto-char e0))
; (princ YaTeX-label-menu-other)
; (princ YaTeX-label-menu-repeat)
; (princ YaTeX-label-menu-any)
; );standard-output
; (goto-char p)
; (or initl (setq initl lnum))
; (message "Collecting %s...Done" labelcmd)
; (if (fboundp 'select-frame) (select-frame cf))
; (YaTeX-showup-buffer YaTeX-label-buffer nil t)
; (YaTeX::label-setup-key-map)
; (setq truncate-lines t)
; (setq buffer-read-only t)
; (use-local-map YaTeX-label-select-map)
; (message YaTeX-label-guide-msg)
; (goto-line (1+ initl)) ;goto recently defined label line
; (switch-to-buffer (current-buffer))
; (unwind-protect
; (progn
; (recursive-edit)
; (set-buffer (get-buffer YaTeX-label-buffer)) ;assertion
; (beginning-of-line)
; (setq line (1- (count-lines (point-min)(point))))
; (cond
; ((= line -1) (setq label ""))
; ((= line lnum) (setq label (YaTeX-label-other)))
; ((= line (1+ lnum))
; (save-excursion
; (switch-to-buffer buf)
; (goto-char p)
; (if (re-search-backward
; (concat "\\\\" refcmd "{\\([^}]+\\)}") nil t)
; (setq label (YaTeX-match-string 1))
; (setq label ""))))
; ((>= line (+ lnum 2))
; (setq label (read-string (format "\\%s{???}: " refcmd))))
; (t (setq label (nth (- lnum line 1) label-list)))))
; (bury-buffer YaTeX-label-buffer)))
; label)))))
(defvar YaTeX-ref-default-label-string "%H%M%S_%d%b%y"
"*Default \\ref time string format.
This format is like strftime(3) but allowed conversion char are as follows;
%y -> Last 2 digit of year, %b -> Month name, %m -> Monthe number(1-12),
%d -> Day, %H -> Hour, %M -> Minute, %S -> Second,
%qx -> alphabetical-decimal conversion of yymmdd.
%qX -> alphabetical-decimal conversion of HHMMSS.
Beware defualt label-string should be always unique. So this format string
should have both time part (%H+%M+%S or %qX) and date
part (%y+(%b|%m)+%d or %qx).")
(defun YaTeX::ref-alphabex (n)
(let ((alphabex ""))
(while (> n 0)
(setq alphabex (concat (char-to-string (+ ?a (% n 26))) alphabex)
n (/ n 26)))
alphabex))
(defun YaTeX::ref-default-label (&optional format)
"Default auto-genarated label string."
;; We do not use (format-time-string) for emacs-19
(let*((ts (substring (current-time-string) 4))
(y (substring ts -2))
(b (substring ts 0 3))
(d (format "%d" (string-to-int (substring ts 4 6))))
(H (substring ts 7 9))
(M (substring ts 10 12))
(S (substring ts 13 15))
(HMS (+ (* 10000 (string-to-int H))
(* 100 (string-to-int M))
(string-to-int S)))
(talphabex (YaTeX::ref-alphabex HMS))
(mnames "JanFebMarAprMayJunJulAugSepOctNovDec")
(m (format "%02d" (/ (string-match b mnames) 3)))
(ymd (+ (* 10000 (string-to-int y))
(* 100 (string-to-int m))
(string-to-int d)))
(dalphabex (YaTeX::ref-alphabex ymd)))
(YaTeX-replace-formats
(or format YaTeX-ref-default-label-string)
(list (cons "y" y)
(cons "b" b)
(cons "m" m)
(cons "d" d)
(cons "H" H)
(cons "M" M)
(cons "S" S)
(cons "qX" talphabex)
(cons "qx" dalphabex)))))
(defvar YaTeX-ref-generate-label-function 'YaTeX::ref-generate-label
"*Function to generate default label for unnamed \\label{}s.
The function pointed to this value should take two arguments.
First argument is LaTeX macro's name, second is macro's argument.")
(defun YaTeX::ref-generate-label (command arg)
"Generate a label string which is unique in current buffer."
(let ((default (condition-case nil
(YaTeX::ref-default-label)
(error (substring (current-time-string) 4)))))
(read-string "Give a label for this line: "
(if YaTeX-emacs-19 (cons default 1) default))))
(defun YaTeX::ref-getset-label (buffer point &optional noset)
"Get label string in the BUFFER near the POINT.
Make \\label{xx} if no label.
If optional third argument NOSET is non-nil, do not generate new label."
;;Here, we rewrite the LaTeX source. Therefore we should be careful
;;to decide the location suitable for \label. Do straightforward!
(let (boundary inspoint cc newlabel (labelholder "label") mathp exp1 env
(r-escape (regexp-quote YaTeX-comment-prefix))
command arg alreadysought foundpoint)
(set-buffer buffer)
(save-excursion
(goto-char point)
(setq cc (current-column))
(if (= (char-after (point)) ?\\) (forward-char 1))
(cond
;; In each codition, 'inspoint and 'boundary should be set
((looking-at YaTeX-sectioning-regexp)
(setq command (YaTeX-match-string 0))
(skip-chars-forward "^{")
(setq arg (buffer-substring
(1+ (point))
(progn (forward-list 1) (1- (point)))))
(skip-chars-forward " \t\n")
;(setq boundary "[^\\]")
(setq inspoint (point))
(setq boundary
(save-excursion
(if (YaTeX-re-search-active-forward
(concat YaTeX-ec-regexp
"\\(" YaTeX-sectioning-regexp "\\|"
"begin\\|item\\)")
r-escape nil 1)
(match-beginning 0)
(1- (point))))))
((looking-at "item\\s ")
(setq command "item"
cc (+ cc 6))
;(setq boundary (concat YaTeX-ec-regexp "\\(item\\|begin\\|end\\)\\b"))
(setq boundary
(save-excursion
(if (YaTeX-re-search-active-forward
(concat YaTeX-ec-regexp "\\(item\\|begin\\|end\\)\\b")
r-escape nil 1)
(match-beginning 0)
(1- (point))))
inspoint boundary))
((looking-at "bibitem")
(setq labelholder "bibitem" ; label holder is bibitem itself
command "bibitem")
(setq boundary
(save-excursion
(if (YaTeX-re-search-active-forward
(concat YaTeX-ec-regexp "\\(bibitem\\|end\\)\\b")
r-escape nil 1)
(match-beginning 0)
(1- (point))))
inspoint boundary))
((string-match YaTeX::ref-nestable-counter-regexp
(setq env (or (YaTeX-inner-environment t) "document")))
(let ((curtop (get 'YaTeX-inner-environment 'point))
(end (point-max)) label)
(skip-chars-forward " \t\n")
(setq inspoint (point) ;initial candidate
cc (current-column)
command env
alreadysought t)
(if (condition-case nil
(progn
(goto-char curtop)
(YaTeX-goto-corresponding-environment))
(error nil))
(setq end (point)))
(goto-char inspoint)
(while (YaTeX-re-search-active-forward
(concat YaTeX-ec-regexp "label{\\([^}]+\\)}" )
r-escape end t)
(setq label (YaTeX-match-string 1))
(if (and (equal env (YaTeX-inner-environment t))
(= curtop (get 'YaTeX-inner-environment 'point)))
;;I found the label
(setq alreadysought label
foundpoint (match-end 0))))
))
((string-match YaTeX::ref-mathenv-regexp env) ;env is set in above case
(setq command env
mathp t
exp1 (string-match YaTeX::ref-mathenv-exp1-regexp env))
;;(setq boundary (concat YaTeX-ec-regexp "\\(\\\\\\|end{" env "}\\)"))
(setq boundary
(save-excursion
(or (catch 'bndry
(while (YaTeX-re-search-active-forward
(concat
YaTeX-ec-regexp "\\("
(if exp1 "" "\\\\\\|")
"\\(end{" env "\\)}\\)")
r-escape nil 1)
(setq foundpoint (match-beginning 0))
(if (or (match-beginning 2) ;end of outer math-env
(equal env (YaTeX-inner-environment t)))
;; YaTeX-inner-environment destroys match-data
(throw 'bndry foundpoint))))
(1- (point))))
inspoint boundary))
((looking-at "footnote\\s *{")
(setq command "footnote")
(skip-chars-forward "^{") ;move onto `{'
(setq boundary
(save-excursion
(condition-case err
(forward-list 1)
(error (error "\\\\footnote at point %s's brace not closed"
(point))))
(1- (point)))
inspoint boundary))
((looking-at "caption\\|\\(begin\\)")
(setq command (YaTeX-match-string 0))
(skip-chars-forward "^{")
;;;;;;(if (match-beginning 1) (forward-list 1))
;; caption can be treated as mathenv, is it right??
(setq arg (buffer-substring
(1+ (point))
(progn (forward-list 1) (1- (point)))))
;;(setq boundary (concat YaTeX-ec-regexp "\\(begin\\|end\\)\\b"))
(setq inspoint (point))
(setq boundary
(save-excursion
(if (YaTeX-re-search-active-forward
(concat YaTeX-ec-regexp "\\(begin\\|end\\)\\b")
r-escape nil 1)
(match-beginning 0)
(1- (point))))))
(t ))
;;cond by kind of labeling ends here.
(if (save-excursion (skip-chars-forward " \t") (looking-at "%"))
(forward-line 1))
(cond
((stringp alreadysought)
(put 'YaTeX::ref-getset-label 'foundpoint foundpoint) ;ugly...
alreadysought)
((and (null alreadysought)
(> boundary (point))
(save-excursion
(YaTeX-re-search-active-forward
;;(concat "\\(" labelholder "\\)\\|\\(" boundary "\\)")
labelholder
(regexp-quote YaTeX-comment-prefix)
boundary 1))
(match-beginning 0))
;; if \label{hoge} found, return it
(put 'YaTeX::ref-getset-label 'foundpoint (1- (match-beginning 0)))
(buffer-substring
(progn
(goto-char (match-end 0))
(skip-chars-forward "^{") (1+ (point)))
(progn
(forward-sexp 1) (1- (point)))))
;;else make a label
;(goto-char (match-beginning 0))
(noset nil) ;do not set label if noset
(t
(goto-char inspoint)
(skip-chars-backward " \t\n")
(save-excursion
(setq newlabel
(funcall YaTeX-ref-generate-label-function command arg)))
(delete-region (point) (progn (skip-chars-backward " \t") (point)))
(if mathp nil
(insert "\n")
(YaTeX-reindent cc))
(put 'YaTeX::ref-getset-label 'foundpoint (point))
(insert (format "\\label{%s}" newlabel))
newlabel)))))
(defvar YaTeX::ref-labeling-regexp-alist-default
'(("\\\\begin{\\(java\\|program\\)}{\\([^}]+\\)}" . 2)
("\\\\label{\\([^}]+\\)}" . 1))
"Alist of labeling regexp vs. its group number points to label string.
This alist is used in \\ref's argument's completion.")
(defvar YaTeX::ref-labeling-regexp-alist-private nil
"*Private extension to YaTeX::ref-labeling-regexp-alist.
See the documetation of YaTeX::ref-labeling-regexp-alist.")
(defvar YaTeX::ref-labeling-regexp-alist
(append YaTeX::ref-labeling-regexp-alist-default
YaTeX::ref-labeling-regexp-alist-private))
(defvar YaTeX::ref-labeling-regexp
(mapconcat 'car YaTeX::ref-labeling-regexp-alist "\\|"))
(defvar YaTeX::ref-mathenv-regexp
;; See also YaTeX-ams-math-begin-alist in yatex.el
;; Define only envs which has counter.(without *)
"equation\\|eqnarray\\|align\\(at\\)?\\|flalign\\|gather\\|xx?alignat\\|multline")
(defvar YaTeX::ref-mathenv-exp1-regexp
"\\(equation\\|multline\\)\\b"
"*Regexp of math-envname which has only one math-expression.")
(defvar YaTeX::ref-enumerateenv-regexp
"enumerate")
(defvar YaTeX::ref-nestable-counter-regexp
"subequations")
(defvar YaTeX::ref-labeling-section-level 2
"*ref補完で収集するセクショニングコマンドの下限レベル
YaTeX-sectioning-levelの数値で指定.")
(defun YaTeX::ref (argp &optional labelcmd refcmd predf)
(setplist 'YaTeX::ref-labeling-regexp nil) ;erase memory cache
(require 'yatexsec)
(cond
((= argp 1)
(let*((lnum 0) m0 e0 x cmd label match-point point-list boundary
(buf (current-buffer))
(llv YaTeX::ref-labeling-section-level)
(mathenvs YaTeX::ref-mathenv-regexp) envname endrx
(enums YaTeX::ref-enumerateenv-regexp)
(counter
(or labelcmd
(concat
YaTeX-ec-regexp "\\(\\("
(mapconcat
'concat
(delq nil
(mapcar
(function
(lambda (s)
(if (>= llv (cdr s))
(car s))))
YaTeX-sectioning-level))
"\\|")
"\\|caption\\(\\[[^]]+\\]\\)?\\|footnote\\){"
"\\|\\(begin{\\(" mathenvs "\\|" enums "\\)}\\)"
(if YaTeX-use-AMS-LaTeX
(concat
"\\|\\(begin{"
YaTeX::ref-nestable-counter-regexp "}\\)"))
"\\)")))
(regexp (concat "\\(" counter
"\\)\\|\\(" YaTeX::ref-labeling-regexp "\\)"))
(itemsep (concat YaTeX-ec-regexp
"\\(\\(bib\\)?item\\|begin\\|end\\)"))
(refcmd (or refcmd "\\(page\\)?ref"))
(p (point)) initl line cf
(percent (regexp-quote YaTeX-comment-prefix))
(output
(function
(lambda (label p)
(while (setq x (string-match "[\n\t]" label))
(aset label x ? ))
(while (setq x (string-match " +" label))
(setq label (concat
(substring label 0 (1+ (match-beginning 0)))
(substring label (match-end 0)))))
(princ (format "%c: <<%s>>\n" (+ (% lnum 26) ?A) label))
(setq point-list (cons p point-list))
(message "Collecting labels... %d" lnum)
(setq lnum (1+ lnum)))))
(me (if (boundp 'me) me 'YaTeX::ref))
)
(message "Collecting labels...")
(save-window-excursion
(YaTeX-showup-buffer
YaTeX-label-buffer (function (lambda (x) (window-width x))))
(if (fboundp 'select-frame) (setq cf (selected-frame)))
(if (eq (window-buffer (minibuffer-window)) buf)
(progn
(other-window 1)
(setq buf (current-buffer))
(set-buffer buf)))
(save-excursion
(set-buffer (get-buffer-create YaTeX-label-buffer))
(condition-case ()
(if (and YaTeX-use-font-lock (fboundp 'font-lock-mode))
(font-lock-mode 1))
(error nil))
(setq buffer-read-only nil)
(erase-buffer))
(save-excursion
(set-buffer buf)
(goto-char (point-min))
(let ((standard-output (get-buffer YaTeX-label-buffer)) existlabel)
(princ (format "=== LABELS in [%s] ===\n" (buffer-name buf)))
(while (YaTeX-re-search-active-forward
regexp ;;counter
percent nil t)
;(goto-char (match-beginning 0))
(setq e0 (match-end 0))
(cond
;;
;;2005/10/21 Skip it if predicate function returns nil
((and predf
(let ((md (match-data)))
(prog1
(condition-case nil
(not (funcall predf))
(error nil))
(store-match-data md)))))
((YaTeX-literal-p) nil)
((YaTeX-match-string 1)
;;if standard counter commands found
(setq cmd (YaTeX-match-string 2)
m0 (match-beginning 0))
(setq match-point (match-beginning 0))
(or initl
(if (< p (point)) (setq initl lnum)))
(cond
;; In any case, variables e0 should be set
((and YaTeX-use-AMS-LaTeX
(string-match YaTeX::ref-nestable-counter-regexp cmd))
(let (label)
(skip-chars-forward "}")
(setq label (buffer-substring
(point) (min (+ 80 (point)) (point-max))))
;; to skip (maybe)auto-generated comment
(skip-chars-forward " \t")
(if (looking-at YaTeX-comment-prefix)
(forward-line 1))
(setq e0 (point))
(skip-chars-forward " \t\n")
(if (looking-at "\\\\label{\\([^}]+\\)}")
(setq label (format "(labe:%s)" (YaTeX-match-string 1))
e0 (match-end 1)))
(funcall output (format "--subequation--%s" label) e0)))
((string-match mathenvs cmd) ;;if matches mathematical env
(skip-chars-forward "}")
(setq x (point)
envname (substring
cmd (match-beginning 0) (match-end 0)))
(save-restriction
(narrow-to-region
m0
(save-excursion
(YaTeX-re-search-active-forward
(setq endrx (format "%send{%s}" YaTeX-ec-regexp
(regexp-quote envname)))
percent nil t)))
(catch 'scan
(while (YaTeX-re-search-active-forward
(concat
"\\\\end{\\(" (regexp-quote envname) "\\)";;(1)
"\\|\\\\\\(notag\\)" ;;2
(if (string-match
YaTeX::ref-mathenv-exp1-regexp cmd)
"" "\\|\\(\\\\\\\\\\)$") ;;3
)
percent nil t)
(let*((quit (match-beginning 1))
(notag (match-beginning 2))
(newln (match-beginning 3))
(label ".......................") l2
(e (point)) (m0 (match-beginning 0))
(ln (YaTeX-string-width label)))
(cond
(notag
(YaTeX-re-search-active-forward
"\\\\\\\\" percent nil 1)
(setq x (point))) ;use x as \label search bound
((and newln ; `\\' found
(not (equal (YaTeX-inner-environment)
envname)))
(YaTeX-end-of-environment)
(goto-char (match-end 0)))
(t
(if (YaTeX-re-search-active-backward
YaTeX::ref-labeling-regexp
percent x t)
;; if \label{x} in math-expression, display it
;; because formula source is hard to recognize
(progn
(goto-char (match-end 0))
(setq l2 (format "\"label:%s\""
(buffer-substring
(1- (point))
(progn (forward-sexp -1)
(1+ (point))))))
(setq label
(if (< (YaTeX-string-width l2) ln)
(concat
l2
(substring
label
0 (- ln (YaTeX-string-width l2))))
l2))
(goto-char e)))
(funcall output
(concat
label " "
(buffer-substring x m0))
x)
(cond
((YaTeX-quick-in-environment-p
YaTeX-math-gathering-list)
;; if here is inner split/cases/gathered env.,
;; counter for here is only one.
;; Go out this environment and,
(YaTeX-end-of-environment)
;; search next expression unit boundary.
(YaTeX-re-search-active-forward
(concat endrx "\\|\\\\begin{")
percent nil 1)
(end-of-line)))
(if quit (throw 'scan t)))))
(setq x (point)))))
(setq e0 (point)))
((string-match enums cmd)
;(skip-chars-forward "} \t\n")
(save-restriction
(narrow-to-region
(point)
(save-excursion