-
Notifications
You must be signed in to change notification settings - Fork 0
/
yatexlib.el
1598 lines (1484 loc) · 55.3 KB
/
yatexlib.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
;;; yatexlib.el --- YaTeX and yahtml common libraries
;;;
;;; (c)1994-2013 by HIROSE Yuuji.[[email protected]]
;;; Last modified Mon Apr 1 22:44:06 2013 on firestorm
;;; $Id$
;;; Code:
;; General variables
(defvar YaTeX-dos (memq system-type '(ms-dos windows-nt OS/2)))
(defvar YaTeX-macos (memq system-type '(darwin)))
(defvar YaTeX-emacs-19 (>= (string-to-int emacs-version) 19))
(defvar YaTeX-emacs-20 (>= (string-to-int emacs-version) 20))
(defvar YaTeX-emacs-21 (>= (string-to-int emacs-version) 21))
(defvar YaTeX-user-completion-table
(if YaTeX-dos "~/_yatexrc" "~/.yatexrc")
"*Default filename in which user completion table is saved.")
(defvar YaTeX-display-color-p
(or (and (fboundp 'display-color-p) (display-color-p))
(and (fboundp 'device-class)
(eq 'color (device-class (selected-device))))
window-system) ; falls down lazy check..
"Current display's capability of expressing colors.")
(defvar YaTeX-japan
(or (boundp 'NEMACS)
(boundp 'MULE)
(and (boundp 'current-language-environment)
(string-match "[Jj]apanese" current-language-environment)))
"Whether yatex mode is running on Japanese environment or not.")
;; autoload from yahtml.el
(autoload 'yahtml-inner-environment-but "yahtml" "yahtml internal func." t)
(defvar latex-message-kanji-code 2
"*Kanji coding system latex command types out.
1 = Shift JIS, 2 = JIS, 3 = EUC. 4 = UTF-8")
(defvar YaTeX-kanji-code-alist
(cond
((boundp '*junet*)
(list '(0 . *noconv*)
(cons
1
(cond
(YaTeX-dos (if (boundp '*sjis-dos*) *sjis-dos* *sjis*dos))
(YaTeX-macos (if (boundp '*sjis-mac*) *sjis-mac* *sjis*mac))
(t *sjis*)))
'(2 . *junet*) '(3 . *euc-japan*)))
((and YaTeX-emacs-20 (featurep 'mule))
;;(cdr-safe(assq 'coding-system (assoc "Japanese" language-info-alist)))
(list '(0 . no-conversion)
(cons
1 (cond (YaTeX-dos 'shift_jis-dos)
(YaTeX-macos 'shift_jis-mac)
((member 'shift_jis (coding-system-list)) 'shift_jis-unix)
(t 'sjis)))
'(2 . iso-2022-jp-unix)
'(3 . euc-jp-unix)
'(4 . utf-8))))
"Kanji-code expression translation table.")
(defvar YaTeX-inhibit-prefix-letter nil
"*T for changing key definitions from [prefix] Letter to [prefix] C-Letter.")
(defvar YaTeX-no-begend-shortcut nil
"*T for disabling shortcut of begin-type completion, [prefix] b d, etc.")
(defvar YaTeX-default-pop-window-height 10
"Default typesetting buffer height.
If integer, sets the window-height of typesetting buffer.
If string, sets the percentage of it.
If nil, use default pop-to-buffer.")
(defvar YaTeX-create-file-prefix-g nil
"*Non-nil creates new file when [prefix] g on \\include{foo}.")
(defvar YaTeX-nervous t
"*If you are nervous about maintenance of yatexrc, set this value to T.
And you will have the local dictionary.")
(defvar YaTeX-use-italic-bold (string< "20" emacs-version)
"*Non-nil tries to find italic/bold fontset.
This variable is effective when font-lock is used.
\it, \bf 内部での日本語が□になってしまう場合はこれをnilにして下さい。")
;----------- work variables ----------------------------------------
(defvar YaTeX-minibuffer-completion-map nil
"Minibuffer completion key map that allows comma completion.")
(if YaTeX-minibuffer-completion-map nil
(setq YaTeX-minibuffer-completion-map
(copy-keymap minibuffer-local-completion-map))
(define-key YaTeX-minibuffer-completion-map " "
'YaTeX-minibuffer-complete)
(define-key YaTeX-minibuffer-completion-map "\t"
'YaTeX-minibuffer-complete))
(defvar YaTeX-typesetting-mode-map nil
"Keymap used in YaTeX typesetting buffer")
(if YaTeX-typesetting-mode-map nil
(setq YaTeX-typesetting-mode-map (make-keymap))
;(suppress-keymap YaTeX-typesetting-mode-map t)
(define-key YaTeX-typesetting-mode-map " " 'YaTeX-jump-error-line)
(define-key YaTeX-typesetting-mode-map "\C-m" 'YaTeX-send-string)
(define-key YaTeX-typesetting-mode-map "1" 'delete-other-windows)
(define-key YaTeX-typesetting-mode-map "0" 'delete-window)
(define-key YaTeX-typesetting-mode-map "q" 'delete-window))
(defvar YaTeX-parent-file nil
"*Main LaTeX source file name used when %#! expression doesn't exist.")
(make-variable-buffer-local 'YaTeX-parent-file)
;---------- Define default key bindings on YaTeX mode map ----------
;;;###autoload
(defun YaTeX-define-key (key binding &optional map)
"Define key on YaTeX-prefix-map."
(if YaTeX-inhibit-prefix-letter
(let ((c (aref key 0)))
(cond
((and (>= c ?a) (<= c ?z)) (aset key 0 (1+ (- c ?a))))
((and (>= c ?A) (<= c ?Z) (numberp YaTeX-inhibit-prefix-letter))
(aset key 0 (1+ (- c ?A))))
(t nil))))
(define-key (or map YaTeX-prefix-map) key binding))
;;;###autoload
(defun YaTeX-local-table-symbol (symbol)
"Return the lisp symbol which keeps local completion table of SYMBOL."
(intern (concat "YaTeX$"
default-directory
(symbol-name symbol))))
;;;###autoload
(defun YaTeX-sync-local-table (symbol)
"Synchronize local variable SYMBOL.
Copy its corresponding directory dependent completion table to SYMBOL."
(if (boundp (YaTeX-local-table-symbol symbol))
(set symbol (symbol-value (YaTeX-local-table-symbol symbol)))))
(defvar YaTeX-user-table-is-read nil
"Flag that means whether user completion table has been read or not.")
;;;###autoload
(defun YaTeX-read-user-completion-table (&optional forcetoread)
"Append user completion table of LaTeX macros"
(interactive)
(let*((user-table (expand-file-name YaTeX-user-completion-table))
(local-table (expand-file-name (file-name-nondirectory user-table)))
var localvar localbuf (curbuf (current-buffer)) sexp)
(if YaTeX-user-table-is-read nil
(message "Loading user completion table")
(if (file-exists-p user-table) (load-file user-table)
(message "Welcome to the field of YaTeX. I'm glad to see you!")))
(setq YaTeX-user-table-is-read t)
(cond
((file-exists-p local-table)
(set-buffer (setq localbuf (find-file-noselect local-table)))
(widen)
(goto-char (point-min))
(while (re-search-forward "(setq \\([^ \t\n]+\\)" nil t)
(setq var (intern (buffer-substring
(match-beginning 1) (match-end 1)))
localvar (YaTeX-local-table-symbol var))
(goto-char (match-beginning 0))
(setq sexp (buffer-substring (point)
(progn (forward-sexp) (point))))
(set-buffer curbuf)
(or (assq var (buffer-local-variables)) (make-local-variable var))
(eval (read sexp))
(or (and (boundp localvar)
(symbol-value localvar)
(not forcetoread))
(set localvar (symbol-value var)))
(set-buffer localbuf))
(kill-buffer localbuf)))
(set-buffer curbuf)))
;;;###autoload
(defun YaTeX-reload-dictionary ()
"Reload local dictionary.
Use this function after editing ./.yatexrc."
(interactive)
(let ((YaTeX-user-table-is-read nil))
(YaTeX-read-user-completion-table t)))
;;;###autoload
(defun YaTeX-lookup-table (word type)
"Lookup WORD in completion table whose type is TYPE.
This function refers the symbol tmp-TYPE-table, user-TYPE-table, TYPE-table.
Typically, TYPE is one of 'env, 'section, 'fontsize, 'singlecmd."
(if (symbolp type) (setq type (symbol-name type)))
(or (assoc word (symbol-value (intern (concat "tmp-" type "-table"))))
(assoc word (symbol-value (intern (concat "user-" type "-table"))))
(assoc word (symbol-value (intern (concat type "-table"))))))
;;;###autoload
(defun YaTeX-update-table (vallist default-table user-table local-table)
"Update completion table if the car of VALLIST is not in current tables.
Second argument DEFAULT-TABLE is the quoted symbol of default completion
table, third argument USER-TABLE is user table which will be saved in
YaTeX-user-completion-table, fourth argument LOCAL-TABLE should have the
completion which is valid during current Emacs's session. If you
want to make LOCAL-TABLE valid longer span (but restrict in this directory)
create the file in current directory which has the same name with
YaTeX-user-completion-table."
(let ((car-v (car vallist)) key answer
(file (file-name-nondirectory YaTeX-user-completion-table)))
(cond
((assoc car-v (symbol-value default-table))
nil) ;Nothing to do
((setq key (assoc car-v (symbol-value user-table)))
(if (equal (cdr vallist) (cdr key)) nil
;; if association hits, but contents differ.
(message
"%s's attributes turned into %s" (car vallist) (cdr vallist))
(set user-table (delq key (symbol-value user-table)))
(set user-table (cons vallist (symbol-value user-table)))
(YaTeX-update-dictionary
YaTeX-user-completion-table user-table "user")))
((setq key (assoc car-v (symbol-value local-table)))
(if (equal (cdr vallist) (cdr key)) nil
(message
"%s's attributes turned into %s" (car vallist) (cdr vallist))
(set local-table (delq key (symbol-value local-table)))
(set local-table (cons vallist (symbol-value local-table)))
(set (YaTeX-local-table-symbol local-table) (symbol-value local-table))
(YaTeX-update-dictionary file local-table)))
;; All of above cases, there are some completion in tables.
;; Then update tables.
(t
(if (not YaTeX-nervous)
(setq answer "u")
(message
(cond
(YaTeX-japan
"`%s'の登録先: U)ユーザ辞書 L)ローカル辞書 N)メモリ D)しない")
(t
"Register `%s' into: U)serDic L)ocalDic N)one D)iscard"))
(if (> (length car-v) 23)
(concat (substring car-v 0 10) "..." (substring car-v -9))
car-v))
(setq answer (char-to-string (read-char))))
(cond
((string-match answer "uy")
(set user-table (cons vallist (symbol-value user-table)))
(YaTeX-update-dictionary YaTeX-user-completion-table user-table "user")
)
((string-match answer "tl")
(set local-table (cons vallist (symbol-value local-table)))
(set (YaTeX-local-table-symbol local-table) (symbol-value local-table))
(YaTeX-update-dictionary file local-table))
((string-match answer "d") nil) ;discard it
(t (set default-table
(cons vallist (symbol-value default-table)))))))))
;;;###autoload
(defun YaTeX-cplread-with-learning
(prom default-table user-table local-table
&optional pred reqmatch init hsym)
"Completing read with learning.
Do a completing read with prompt PROM. Completion table is what
DEFAULT-TABLE, USER-TABLE, LOCAL table are appended in reverse order.
Note that these tables are passed by the symbol.
Optional arguments PRED, REQMATH and INIT are passed to completing-read
as its arguments PREDICATE, REQUIRE-MATCH and INITIAL-INPUT respectively.
If optional 8th argument HSYM, history symbol, is passed, use it as
history list variable."
(YaTeX-sync-local-table local-table)
(let*((table (append (symbol-value local-table)
(symbol-value user-table)
(symbol-value default-table)))
(word (completing-read-with-history
prom table pred reqmatch init hsym)))
(if (and (string< "" word) (not (assoc word table)))
(YaTeX-update-table (list word) default-table user-table local-table))
word))
;;;###autoload
(defun YaTeX-update-dictionary (file symbol &optional type)
(let ((local-table-buf (find-file-noselect file))
(name (symbol-name symbol))
(value (symbol-value symbol)))
(save-excursion
(message "Updating %s dictionary..." (or type "local"))
(set-buffer local-table-buf)
(goto-char (point-max))
(search-backward (concat "(setq " name) nil t)
(delete-region (point) (progn (forward-sexp) (point)))
(delete-blank-lines)
(insert "(setq " name " '(\n")
(mapcar '(lambda (s)
(insert (format "%s\n" (prin1-to-string s))))
value)
(insert "))\n\n")
(delete-blank-lines)
(basic-save-buffer)
(kill-buffer local-table-buf)
(message "Updating %s dictionary...Done" (or type "local")))))
;;;###autoload
(defun YaTeX-define-begend-key-normal (key env &optional map)
"Define short cut YaTeX-make-begin-end key."
(YaTeX-define-key
key
(list 'lambda '(arg) '(interactive "P")
(list 'YaTeX-insert-begin-end env 'arg))
map))
;;;###autoload
(defun YaTeX-define-begend-region-key (key env &optional map)
"Define short cut YaTeX-make-begin-end-region key."
(YaTeX-define-key key (list 'lambda nil '(interactive)
(list 'YaTeX-insert-begin-end env t)) map))
;;;###autoload
(defun YaTeX-define-begend-key (key env &optional map)
"Define short cut key for begin type completion.
Define both strokes for normal and region mode.
To customize YaTeX, user should use this function."
(YaTeX-define-begend-key-normal key env map)
(if YaTeX-inhibit-prefix-letter nil
(YaTeX-define-begend-region-key
(concat (upcase (substring key 0 1)) (substring key 1)) env)))
;;;###autoload
(defun YaTeX-search-active-forward (string cmntrx &optional bound err cnt func)
"Search STRING which is not commented out by CMNTRX.
Optional arguments after BOUND, ERR, CNT are passed literally to search-forward
or search-backward.
Optional sixth argument FUNC changes search-function."
(let ((sfunc (or func 'search-forward)) found md)
(while (and (prog1
(setq found (funcall sfunc string bound err cnt))
(setq md (match-data)))
(or
(and (eq major-mode 'yatex-mode)
(YaTeX-in-verb-p (match-beginning 0)))
(save-excursion
(goto-char (match-beginning 0))
(beginning-of-line)
(re-search-forward cmntrx (match-beginning 0) t)))))
(store-match-data md)
found))
(defun YaTeX-re-search-active-forward (regexp cmntrx &optional bound err cnt)
"Search REGEXP backward which is not commented out by regexp CMNTRX.
See also YaTeX-search-active-forward."
(YaTeX-search-active-forward regexp cmntrx bound err cnt 're-search-forward))
(defun YaTeX-search-active-backward (string cmntrx &optional bound err cnt)
"Search STRING backward which is not commented out by regexp CMNTRX.
See also YaTeX-search-active-forward."
(YaTeX-search-active-forward string cmntrx bound err cnt 'search-backward))
(defun YaTeX-re-search-active-backward (regexp cmntrx &optional bound err cnt)
"Search REGEXP backward which is not commented out by regexp CMNTRX.
See also YaTeX-search-active-forward."
(YaTeX-search-active-forward
regexp cmntrx bound err cnt 're-search-backward))
(defun YaTeX-relative-path-p (path)
"Return non-nil if PATH is not absolute one."
(let ((md (match-data)))
(unwind-protect
(not (string-match "^\\(/\\|[a-z]:\\|\\\\\\).*/" file))
(store-match-data md))))
;;;###autoload
(defun YaTeX-switch-to-buffer (file &optional setbuf)
"Switch to buffer if buffer exists, find file if not.
Optional second arg SETBUF t make use set-buffer instead of switch-to-buffer."
(interactive "Fswitch to file: ")
(if (bufferp file)
(setq file (buffer-file-name file))
(and (YaTeX-relative-path-p file)
(eq major-mode 'yatex-mode)
YaTeX-search-file-from-top-directory
(save-excursion
(YaTeX-visit-main t)
(setq file (expand-file-name file)))))
(let (buf (hilit-auto-highlight (not setbuf)))
(cond
((setq buf (get-file-buffer file))
(funcall (if setbuf 'set-buffer 'switch-to-buffer)
(get-file-buffer file))
buf)
((or YaTeX-create-file-prefix-g (file-exists-p file))
(or ;find-file returns nil but set current-buffer...
(if setbuf (set-buffer (find-file-noselect file))
(find-file file))
(current-buffer)))
(t (message "%s was not found in this directory." file)
nil))))
;;;###autoload
(defun YaTeX-switch-to-buffer-other-window (file)
"Switch to buffer if buffer exists, find file if not."
(interactive "Fswitch to file: ")
(and (eq major-mode 'yatex-mode)
(stringp file)
(YaTeX-relative-path-p file)
YaTeX-search-file-from-top-directory
(save-excursion
(YaTeX-visit-main t)
(setq file (expand-file-name file))))
(if (bufferp file) (setq file (buffer-file-name file)))
(cond
((get-file-buffer file)
(switch-to-buffer-other-window (get-file-buffer file))
t)
((or YaTeX-create-file-prefix-g (file-exists-p file))
(find-file-other-window file) t)
(t (message "%s was not found in this directory." file)
nil)))
(defun YaTeX-get-file-buffer (file)
"Return the FILE's buffer.
Base directory is that of main file or current directory."
(let (dir main (cdir default-directory))
(or (and (eq major-mode 'yatex-mode)
YaTeX-search-file-from-top-directory
(save-excursion
(YaTeX-visit-main t)
(get-file-buffer file)))
(get-file-buffer file))))
(defun YaTeX-replace-format-sub (string format repl)
(let ((beg (or (string-match (concat "^\\(%" format "\\)") string)
(string-match (concat "[^%]\\(%" format "\\)") string)))
(len (length format)))
(if (null beg) string ;no conversion
(concat
(substring string 0 (match-beginning 1)) (or repl "")
(substring string (match-end 1))))))
;;;###autoload
(defun YaTeX-replace-format (string format repl)
"In STRING, replace first appearance of FORMAT to REPL as if
function `format' does. FORMAT does not contain `%'"
(let ((ans string) (case-fold-search nil))
(while (not (string=
ans (setq string (YaTeX-replace-format-sub ans format repl))))
(setq ans string))
string))
;;;###autoload
(defun YaTeX-replace-formats (string replace-list)
(let ((list replace-list))
(while list
(setq string (YaTeX-replace-format
string (car (car list)) (cdr (car list)))
list (cdr list)))
string))
;;;###autoload
(defun YaTeX-replace-format-args (string &rest args)
"Translate the argument mark #1, #2, ... #n in the STRING into the
corresponding real arguments ARGS."
(let ((argp 1))
(while args
(setq string
(YaTeX-replace-format string (int-to-string argp) (car args)))
(setq args (cdr args) argp (1+ argp))))
string)
;;;###autoload
(defun rindex (string char)
"Return the last position of STRING where character CHAR found."
(let ((pos (1- (length string)))(index -1))
(catch 'rindex
(while (>= pos 0)
(cond
((= (aref string pos) char)
(throw 'rindex pos))
(t (setq pos (1- pos))))))))
;;;###autoload
(defun point-beginning-of-line ()
(save-excursion (beginning-of-line)(point)))
;;;###autoload
(defun point-end-of-line ()
(save-excursion (end-of-line)(point)))
;;;###autoload
(defun YaTeX-showup-buffer (buffer &optional func select)
"Make BUFFER show up in certain window (but current window)
that gives the maximum value by the FUNC. FUNC should take an argument
of its window object. Non-nil for optional third argument SELECT selects
that window. This function never selects minibuffer window."
(or (and (if (and YaTeX-emacs-19 select window-system)
(get-buffer-window buffer t)
(get-buffer-window buffer))
(progn
(if select
(goto-buffer-window buffer))
t))
(let ((window (selected-window))
(wlist (YaTeX-window-list)) win w (x 0))
(cond
((> (length wlist) 2)
(if func
(while wlist
(setq w (car wlist))
(if (and (not (eq window w))
(> (funcall func w) x))
(setq win w x (funcall func w)))
(setq wlist (cdr wlist)))
(setq win (get-lru-window)))
(select-window win)
(switch-to-buffer buffer)
(or select (select-window window)))
((= (length wlist) 2)
;(other-window 1);This does not work properly on Emacs-19
(select-window (get-lru-window))
(switch-to-buffer buffer)
(or select (select-window window)))
(t ;if one-window
(cond
((and YaTeX-emacs-19 window-system (get-buffer-window buffer t))
nil) ;if found in other frame
(YaTeX-default-pop-window-height
(split-window-calculate-height YaTeX-default-pop-window-height)
;;(pop-to-buffer buffer) ;damn! emacs-19.30
(select-window (next-window nil 1))
(switch-to-buffer (get-buffer-create buffer))
(or select (select-window window)))
(t nil)))
))))
(cond
((fboundp 'screen-height)
(fset 'YaTeX-screen-height 'screen-height)
(fset 'YaTeX-screen-width 'screen-width)
(fset 'YaTeX-set-screen-height 'set-screen-height)
(fset 'YaTeX-set-screen-width 'set-screen-width))
((fboundp 'frame-height)
(fset 'YaTeX-screen-height 'frame-height)
(fset 'YaTeX-screen-width 'frame-width)
(fset 'YaTeX-set-screen-height 'set-frame-height)
(fset 'YaTeX-set-screen-width 'set-frame-width))
(t (error "I don't know how to run YaTeX on this Emacs...")))
;;;###autoload
(defun split-window-calculate-height (height)
"Split current window wight specified HEIGHT.
If HEIGHT is number, make a new window that has HEIGHT lines.
If HEIGHT is string, make a new window that occupies HEIGT % of screen height.
Otherwise split window conventionally."
(if (one-window-p t)
(split-window
(selected-window)
(max
(min
(- (YaTeX-screen-height)
(if (numberp height)
(+ height 2)
(/ (* (YaTeX-screen-height)
(string-to-int height))
100)))
(- (YaTeX-screen-height) window-min-height 1))
window-min-height))))
;;;###autoload
(defun YaTeX-window-list ()
(let*((curw (selected-window)) (win curw) (wlist (list curw)))
(while (not (eq curw (setq win (next-window win))))
(or (eq win (minibuffer-window))
(setq wlist (cons win wlist))))
wlist))
(if YaTeX-emacs-21
;; Emacs-21's next-window returns other frame's window even if called
;; with argument ALL-FRAMES nil, when called from minibuffer context.
;; Therefore, check frame identity here.
(defun YaTeX-window-list ()
(let*((curw (selected-window)) (win curw) (wlist (list curw))
(curf (window-frame curw)))
(while (and (not (eq curw (setq win (next-window win))))
(eq curf (window-frame win)))
(or (eq win (minibuffer-window))
(setq wlist (cons win wlist))))
wlist)))
;;;###autoload
(defun substitute-all-key-definition (olddef newdef keymap)
"Replace recursively OLDDEF with NEWDEF for any keys in KEYMAP now
defined as OLDDEF. In other words, OLDDEF is replaced with NEWDEF
where ever it appears."
(if YaTeX-emacs-19
(substitute-key-definition olddef newdef keymap global-map)
(mapcar
(function (lambda (key) (define-key keymap key newdef)))
(where-is-internal olddef keymap))))
;;;###autoload
(defun YaTeX-match-string (n &optional m)
"Return (buffer-substring (match-beginning n) (match-beginning m))."
(if (match-beginning n)
(YaTeX-buffer-substring (match-beginning n)
(match-end (or m n)))))
;;;###autoload
(defun YaTeX-minibuffer-complete ()
"Complete in minibuffer.
If the symbol 'delim is bound and is string, its value is assumed to be
the character class of delimiters. Completion will be performed on
the last field separated by those delimiters.
If the symbol 'quick is bound and is 't, when the try-completion results
in t, exit minibuffer immediately."
(interactive)
(save-restriction
(narrow-to-region
(if (fboundp 'field-beginning) (field-beginning (point-max)) (point-min))
(point-max))
(let ((md (match-data)) beg word compl
(quick (and (boundp 'quick) (eq quick t)))
(displist ;function to display completion-list
(function
(lambda ()
(with-output-to-temp-buffer "*Completions*"
(display-completion-list
(all-completions word minibuffer-completion-table)))))))
(setq beg (if (and (boundp 'delim) (stringp delim))
(save-excursion
(skip-chars-backward (concat "^" delim))
(point))
(point-min))
word (buffer-substring beg (point-max))
compl (try-completion word minibuffer-completion-table))
(cond
((eq compl t)
(if quick (exit-minibuffer)
(let ((p (point)) (max (point-max)))
(unwind-protect
(progn
(goto-char max)
(insert " [Sole completion]")
(goto-char p)
(sit-for 1))
(delete-region max (point-max))
(goto-char p)))))
((eq compl nil)
(ding)
(save-excursion
(let (p)
(unwind-protect
(progn
(goto-char (setq p (point-max)))
(insert " [No match]")
(goto-char p)
(sit-for 2))
(delete-region p (point-max))))))
((string= compl word)
(funcall displist))
(t (delete-region beg (point-max))
(insert compl)
(if quick
(if (eq (try-completion compl minibuffer-completion-table) t)
(exit-minibuffer)
(funcall displist)))))
(store-match-data md))))
(defun YaTeX-minibuffer-quick-complete ()
"Set 'quick to 't and call YaTeX-minibuffer-complete.
See documentation of YaTeX-minibuffer-complete."
(interactive)
(let ((quick t))
(self-insert-command 1)
(YaTeX-minibuffer-complete)))
(defun YaTeX-yatex-buffer-list ()
(save-excursion
(delq nil (mapcar (function (lambda (buf)
(set-buffer buf)
(if (eq major-mode 'yatex-mode) buf)))
(buffer-list)))))
(defun foreach-buffers (pattern job)
"For each buffer which matches with PATTERN, do JOB."
(let ((list (buffer-list)))
(save-excursion
(while list
(set-buffer (car list))
(if (or (and (stringp pattern)
(buffer-file-name)
(string-match pattern (buffer-file-name)))
(and (symbolp pattern) major-mode (eq major-mode pattern)))
(eval job))
(setq list (cdr list))))))
(defun goto-buffer-window (buffer)
"Select window which is bound to BUFFER.
If no such window exist, switch to buffer BUFFER."
(interactive "BGoto buffer: ")
(if (stringp buffer)
(setq buffer (or (get-file-buffer buffer) (get-buffer buffer))))
(if (get-buffer buffer)
(cond
((get-buffer-window buffer)
(select-window (get-buffer-window buffer)))
((and YaTeX-emacs-19 (get-buffer-window buffer t))
(let*((win (get-buffer-window buffer t))
(frame (window-frame win)))
(select-frame frame)
(raise-frame frame)
(focus-frame frame)
(select-window win)
(set-mouse-position frame 0 0)
(and (featurep 'windows) (fboundp 'win:adjust-window)
(win:adjust-window))))
((and (featurep 'windows) (fboundp 'win:get-buffer-window)
(let ((w (win:get-buffer-window buffer)))
(and w (win:switch-window w))))
(select-window (get-buffer-window buffer)))
(t (switch-to-buffer buffer)))))
;; Here starts the functions which support gmhist-vs-Emacs19 compatible
;; reading with history.
;;;###autoload
(defun completing-read-with-history
(prompt table &optional predicate must-match initial hsym)
"Completing read with general history: gmhist, Emacs-19."
(let ((minibuffer-history
(or (symbol-value hsym)
(and (boundp 'minibuffer-history) minibuffer-history)))
(minibuffer-history-symbol (or hsym 'minibuffer-history)))
(prog1
(if (fboundp 'completing-read-with-history-in)
(completing-read-with-history-in
minibuffer-history-symbol prompt table predicate must-match initial)
(completing-read prompt table predicate must-match initial))
(if (and YaTeX-emacs-19 hsym) (set hsym minibuffer-history)))))
;;;###autoload
(defun read-from-minibuffer-with-history (prompt &optional init map read hsym)
"Read from minibuffer with general history: gmhist, Emacs-19."
(cond
(YaTeX-emacs-19
(read-from-minibuffer prompt init map read hsym))
(t
(let ((minibuffer-history-symbol hsym))
(read-from-minibuffer prompt init map read)))))
;;;###autoload
(defun read-string-with-history (prompt &optional init hsym)
"Read string with history: gmhist(Emacs-18) and Emacs-19."
(cond
(YaTeX-emacs-19
(read-from-minibuffer prompt init minibuffer-local-map nil hsym))
((featurep 'gmhist-mh)
(read-with-history-in hsym prompt init))
(t (read-string prompt init))))
(defvar YaTeX-skip-next-reader-char ?\C-j)
(defun YaTeX-read-string-or-skip (&rest args)
"Read string, or skip if last input char is \C-j."
(if (equal (if (boundp 'last-input-event) last-input-event last-input-char)
YaTeX-skip-next-reader-char)
""
(apply 'read-string args)))
(defun YaTeX-completing-read-or-skip (&rest args)
"Do completing-read, or skip if last input char is \C-j."
(if (equal (if (boundp 'last-input-event) last-input-event last-input-char)
YaTeX-skip-next-reader-char)
""
(apply 'completing-read args)))
;;;###autoload
(fset 'YaTeX-rassoc
(if (and nil (fboundp 'rassoc) (subrp (symbol-function 'rassoc)))
(symbol-function 'rassoc)
(function
(lambda (key list)
(let ((l list))
(catch 'found
(while l
(if (equal key (cdr (car l)))
(throw 'found (car l)))
(setq l (cdr l)))))))))
(defun YaTeX-insert-file-contents (file visit &optional beg end)
(cond
((and (string< "19" emacs-version) (not (featurep 'xemacs)))
(insert-file-contents file visit beg end))
((string-match "unix\\|linux" (symbol-name system-type))
(let ((default-process-coding-system
(and (boundp '*noconv*) (list '*noconv*)))
(file-coding-system (and (boundp '*noconv*) '*noconv*))
kanji-fileio-code
(default-process-kanji-code 0))
(call-process shell-file-name file (current-buffer) nil
(or (and (boundp 'shell-command-option)
shell-command-option)
"-c")
(format "dd bs=1 count=%d | tail -c +%d" end beg))))
(t (insert-file-contents file))))
(defun YaTeX-split-string (str &optional sep null)
"Split string STR by every occurrence of SEP(regexp).
If the optional second argument SEP is nil, it defaults to \"[ \f\t\n\r\v]+\".
Do not include null string by default. Non-nil for optional third argument
NULL includes null string in a list."
(let ((sep (or sep "[ \f\t\n\r\v]+"))
list m)
(while str
(if (setq m (string-match sep str))
(progn
(if (or (> m 0) null)
(setq list (cons (substring str 0 m) list)))
(setq str (substring str (match-end 0))))
(if (or null (string< "" str))
(setq list (cons str list)))
(setq str nil)))
(nreverse list)))
;;;###autoload
(defun YaTeX-delete1 (elt list)
"Delete"
(let (e)
(while (setq e (YaTeX-member elt list))
(setq list (delq (car e) list))))
list)
(if (fboundp 'delete)
(fset 'YaTeX-delete (symbol-function 'delete))
(fset 'YaTeX-delete (symbol-function 'YaTeX-delete1)))
(defun YaTeX-member1 (elt list)
(catch 'found
(while list
(if (equal elt (car list))
(throw 'found list))
(setq list (cdr list)))))
(if (and (fboundp 'member) (subrp (symbol-function 'member)))
(fset 'YaTeX-member (symbol-function 'member))
(fset 'YaTeX-member (symbol-function 'YaTeX-member1)))
;;;
;; Interface function for windows.el
;;;
;;;###autoload
(fset 'YaTeX-last-key
(if (fboundp 'win:last-key)
'win:last-key
'(lambda () (if (boundp 'last-command-char)
last-command-char
last-command-event))))
(defun YaTeX-switch-to-window ()
"Switch to windows.el's window decided by last pressed key."
(interactive)
(or (featurep 'windows) (error "Why don't you use `windows.el'?"))
(win-switch-to-window 1 (- (YaTeX-last-key) win:base-key)))
;;;###autoload
(defun YaTeX-command-to-string (cmd)
(if (fboundp 'shell-command-to-string)
(funcall 'shell-command-to-string cmd)
(let ((tbuf " *tmpout*"))
(if (get-buffer-create tbuf) (kill-buffer tbuf))
(let ((standard-output (get-buffer-create tbuf)))
(unwind-protect
(save-excursion
(call-process
shell-file-name nil tbuf nil YaTeX-shell-command-option cmd)
(set-buffer tbuf)
(buffer-string))
(kill-buffer tbuf))))))
;;;###autoload
(defun YaTeX-reindent (col)
"Remove current indentation and reindento to COL column."
(save-excursion
(beginning-of-line)
(skip-chars-forward " \t")
(if (/= col (current-column))
(progn
(delete-region (point) (progn (beginning-of-line) (point)))
(indent-to col))))
(skip-chars-forward " \t" (point-end-of-line)))
(defun YaTeX-inner-environment (&optional quick)
"Return current inner-most environment.
Non-nil for optional argument QUICK restricts search bound to most
recent sectioning command. Matching point is stored to property 'point
of 'YaTeX-inner-environment, which can be referred by
(get 'YaTeX-inner-environment 'point)."
(put 'YaTeX-inner-environment 'point (point-min))
(put 'YaTeX-inner-environment 'indent 0)
(let*((nest 0)
(beg (YaTeX-replace-format-args
(regexp-quote YaTeX-struct-begin)
;YaTeX-struct-begin ;=== TENTATIVE!! ==
YaTeX-struct-name-regexp
(if (eq major-mode 'yahtml-mode) "\\s *.*" "")
""))
(end (YaTeX-replace-format-args
(regexp-quote YaTeX-struct-end)
YaTeX-struct-name-regexp "" ""))
(begend (concat "\\(" beg "\\)\\|\\(" end "\\)"))
bound m0
(htmlp (eq major-mode 'yahtml-mode))
(open
(concat "^" (or (cdr (assq major-mode '((yahtml-mode . "<")))) "{")))
(close
(concat "^"
(or (cdr(assq major-mode '((yahtml-mode . "\n\t >")))) "}"))))
(save-excursion
(if quick
(setq bound
(save-excursion
(if htmlp
;;(re-search-backward YaTeX-sectioning-regexp nil 1)
;;(goto-char (point-min)) ;Is this enough? 97/6/26
(re-search-backward yahtml-indentation-boundary nil 1)
(YaTeX-re-search-active-backward
(concat YaTeX-ec-regexp
"\\(" YaTeX-sectioning-regexp "\\)\\*?{")
YaTeX-comment-prefix nil 1))
(or (bobp) (end-of-line))
(point))))
(if (catch 'begin
(if (and (numberp bound) (< (point) bound)) (throw 'begin nil))
(while (YaTeX-re-search-active-backward
begend YaTeX-comment-prefix bound t)
(setq m0 (match-beginning 0))
(if (looking-at end) ;;(match-beginning 2)
(setq nest (1+ nest))
(setq nest (1- nest)))
(if (< nest 0)
(progn
(put 'YaTeX-inner-environment 'point m0)
(goto-char m0)
(put 'YaTeX-inner-environment 'indent (current-column))
(throw 'begin t)))))
(YaTeX-buffer-substring
(progn (skip-chars-forward open) (1+ (point)))
(progn (skip-chars-forward close) (point)))))))
(defun YaTeX-goto-corresponding-environment (&optional allow-mismatch noerr)
"Go to corresponding begin/end enclosure.
Optional argument ALLOW-MISMATCH allows mismatch open/clese. Use this
for \left(, \right).
Optional third argument NOERR causes no error for unballanced environment."
(interactive)
(if (not (YaTeX-on-begin-end-p)) nil
(let ((p (match-end 0)) b0 b1 env (nest 0) regexp re-s (op (point))
(m0 (match-beginning 0)) ;whole matching
(m1 (match-beginning 1)) ;environment in \begin{}
(m2 (match-beginning 2)) ;environment in \end{}
(m3 (match-beginning 3))) ;environment in \[ \] \( \)
;(setq env (regexp-quote (buffer-substring p (match-beginning 0))))
(if (cond
(m1 ;if begin{xxx}
(setq env
(if allow-mismatch YaTeX-struct-name-regexp
(regexp-quote (buffer-substring m1 (match-end 1)))))
; (setq regexp (concat "\\(\\\\end{" env "}\\)\\|"
; "\\(\\\\begin{" env "}\\)"))
(setq regexp
(concat
"\\("
(YaTeX-replace-format-args
(regexp-quote YaTeX-struct-end) env "" "")
"\\)\\|\\("
(YaTeX-replace-format-args
(regexp-quote YaTeX-struct-begin) env "" "")
"\\)"))
(setq re-s 're-search-forward))
(m2 ;if end{xxx}
(setq env
(if allow-mismatch YaTeX-struct-name-regexp
(regexp-quote (buffer-substring m2 (match-end 2)))))
; (setq regexp (concat "\\(\\\\begin{" env "}\\)\\|"
; "\\(\\\\end{" env "}\\)"))
(setq regexp
(concat
"\\("
(YaTeX-replace-format-args
(regexp-quote YaTeX-struct-begin) env "" "")
"\\)\\|\\("
(YaTeX-replace-format-args
(regexp-quote YaTeX-struct-end) env "" "")
"\\)"))
(setq re-s 're-search-backward))
(m3 ;math environment
(setq env (char-after (1+ m3))
regexp (format "\\(%s%s\\)\\|\\(%s%s\\)"
YaTeX-ec-regexp
(regexp-quote
(cdr (assq env '((?( . ")") (?) . "(")
(?[ . "]") (?] . "[")))))
YaTeX-ec-regexp
(regexp-quote (char-to-string env)))
re-s (if (memq env '(?\( ?\[))
're-search-forward
're-search-backward)))
(t (if noerr nil (error "Corresponding environment not found."))))