forked from emacs-w3m/emacs-w3m
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw3m-util.el
1617 lines (1473 loc) · 54.7 KB
/
w3m-util.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
;;; w3m-util.el --- Utility macros and functions for emacs-w3m
;; Copyright (C) 2001-2014, 2016-2021 TSUCHIYA Masatoshi <tsuchiya@namazu.org>
;; Authors: TSUCHIYA Masatoshi <tsuchiya@namazu.org>,
;; Shun-ichi GOTO <gotoh@taiyo.co.jp>,
;; Satoru Takabayashi <satoru-t@is.aist-nara.ac.jp>,
;; Hideyuki SHIRAI <shirai@meadowy.org>,
;; Keisuke Nishida <kxn30@po.cwru.edu>,
;; Yuuichi Teranishi <teranisi@gohome.org>,
;; Akihiro Arisawa <ari@mbf.sphere.ne.jp>,
;; Katsumi Yamaoka <yamaoka@jpl.org>
;; Keywords: w3m, WWW, hypermedia
;; This file is a part of emacs-w3m.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; This module is a part of emacs-w3m which provides utility macros
;; and inline functions. Visit <URL:http://emacs-w3m.namazu.org/> for
;; more details of emacs-w3m.
;;; Code:
(eval-when-compile (require 'subr-x)) ;; string-empty-p
;; Variables and functions which are used in the following inline
;; functions. They should be defined in the other module at run-time.
(defvar w3m-current-process)
(defvar w3m-current-refresh)
(defvar w3m-current-title)
(defvar w3m-current-url)
(defvar w3m-fb-list-buffers-frame)
(defvar w3m-fb-mode)
(defvar w3m-mode-hook)
(defvar w3m-pop-up-frames)
(defvar w3m-pop-up-windows)
(defvar w3m-popup-frame-parameters)
(defvar w3m-previous-session-buffer)
(defvar w3m-refresh-timer)
(defvar w3m-select-buffer-name)
(defvar w3m-use-refresh)
(defvar w3m-use-tab)
(defvar w3m-work-buffer-list)
(defvar w3m-use-japanese-menu)
(defvar w3m-mode-map)
(defvar w3m-use-title-buffer-name)
(defvar w3m-buffer-unseen)
(defvar w3m-puny-utf-16be)
;;; Things should be defined in advance:
(declare-function w3m-detect-coding-region "w3m-ems" (start end &optional priority-list))
(declare-function w3m-force-window-update "w3m-ems" (&optional window))
(declare-function w3m-history-restore-position "w3m-hist")
(declare-function w3m-mode "w3m")
(declare-function w3m-url-to-file-name "w3m" (url))
;; ;;; Control structures:
;; (defmacro w3m-static-if (cond then &rest else)
;; "Like `if', except that it evaluates COND at compile-time."
;; (if (eval cond) then `(progn ,@else)))
;; (put 'w3m-static-if 'lisp-indent-function 2)
;; (def-edebug-spec w3m-static-if (&rest def-form))
;; (put 'w3m-static-when 'lisp-indent-function 1)
;; (defmacro w3m-static-when (cond &rest body)
;; "Like `when', but evaluate COND at compile time."
;; (if (eval cond)
;; `(progn ,@body)))
;; (def-edebug-spec w3m-static-when (&rest def-form))
;; (put 'w3m-static-unless 'lisp-indent-function 1)
;; (defmacro w3m-static-unless (cond &rest body)
;; "Like `unless', but evaluate COND at compile time."
;; (if (eval cond)
;; nil
;; `(progn ,@body)))
;; (def-edebug-spec w3m-static-unless (&rest def-form))
;; (defmacro w3m-static-cond (&rest clauses)
;; "Like `cond', except that it evaluates CONDITION part of each clause at
;; compile-time."
;; (while (and clauses
;; (not (eval (car (car clauses)))))
;; (setq clauses (cdr clauses)))
;; (if clauses
;; (cons 'progn (cdr (car clauses)))))
;; (def-edebug-spec w3m-static-cond (&rest (&rest def-form)))
(put 'w3m-condition-case 'lisp-indent-function 2)
(defmacro w3m-condition-case (var bodyform &rest handlers)
"Like `condition-case', except that signal an error if `debug-on-error'
or `debug-on-quit' is non-nil."
`(if (or debug-on-error debug-on-quit)
,bodyform
(condition-case ,var
,bodyform
,@handlers)))
;;; Functions used in common:
(defvar w3m-coding-system)
(defvar w3m-default-coding-system)
(defun w3m-decode-coding-string-with-priority (str coding)
"Decode the string STR which is encoded in CODING.
If CODING is a list, look for the coding system using it as a priority
list."
(with-temp-buffer
(set-buffer-multibyte nil)
(insert str)
(decode-coding-string
(buffer-string)
(or (if (listp coding)
(w3m-detect-coding-region (point-min) (point-max) coding)
coding)
w3m-default-coding-system
w3m-coding-system
'iso-2022-7bit))))
;;; Text props:
(defmacro w3m-add-text-properties (start end props &optional object)
"Like `add-text-properties' but always add a non-sticky property."
`(add-text-properties
,start ,end
;; front-nonsticky and rear-sticky are enabled by default.
(append '(rear-nonsticky t) ,props)
,object))
(defun w3m-add-face-property (start end name &optional object)
"Add face NAME to the face text property of the text from START to END.
The value of the existing text property should be a list.
If the optional fourth argument OBJECT is a buffer (or nil, which means
the current buffer), START and END are buffer positions (integers or
markers). If OBJECT is a string, START and END are 0-based indices
into it."
(let ((pos start)
next prop)
(while (< pos end)
(setq prop (get-text-property pos 'face object)
next (next-single-property-change pos 'face object end))
(w3m-add-text-properties pos next (list 'face (cons name prop)) object)
(setq pos next))))
(defun w3m-remove-face-property (start end name &optional object)
"Remove face NAME from the face text property of text from START to END.
The value of the existing text property should be a list.
If the optional fourth argument OBJECT is a buffer (or nil, which means
the current buffer), START and END are buffer positions (integers or
markers). If OBJECT is a string, START and END are 0-based indices
into it."
(let ((pos start)
next prop new-prop elem)
(while (< pos end)
(setq prop (get-text-property pos 'face object))
(setq next (next-single-property-change pos 'face object end))
(setq new-prop (cond
((listp prop)
(setq new-prop (remove name prop))
(if (>= 1 (length new-prop))
(car new-prop)
new-prop))
((equal name prop) nil)
(t prop)))
(remove-text-properties pos next 'face object)
(when new-prop
(add-text-properties pos next (list 'face new-prop) object))
(setq pos next))))
(defmacro w3m-get-text-property-around (prop)
"Search for the text property PROP in one character before and behind
the current position. Return the value corresponding to PROP or nil.
If PROP is not found at the current position, point will move to the
position where PROP exists."
`(let ((position (point))
value)
(or (get-text-property position ,prop)
(and (not (bolp))
(setq value (get-text-property (1- position) ,prop))
(goto-char (1- position))
value)
(and (not (eolp))
(setq value (get-text-property (1+ position) ,prop))
(goto-char (1+ position))
value))))
(defmacro w3m-action (&optional position)
"Return the value of the `w3m-action' property at the given POSITION.
NOTE: If POSITION is omitted, it searches for the property in one
character before and behind the current position, and point will move
to the position where the property exists."
(if position
`(get-text-property ,position 'w3m-action)
`(w3m-get-text-property-around 'w3m-action)))
(defmacro w3m-anchor (&optional position)
"Return the value of the `w3m-href-anchor' property at the given POSITION.
NOTE: If POSITION is omitted, it searches for the property in one
character before and behind the current position, and point will move
to the position where the property exists."
(if position
`(get-text-property ,position 'w3m-href-anchor)
`(w3m-get-text-property-around 'w3m-href-anchor)))
(defmacro w3m-image (&optional position)
"Return the value of the `w3m-image' property at the given POSITION.
NOTE: If POSITION is omitted, it searches for the property in one
character before and behind the current position, and point will move
to the position where the property exists."
(if position
`(get-text-property ,position 'w3m-image)
`(w3m-get-text-property-around 'w3m-image)))
(defmacro w3m-image-alt (&optional position)
"Return the value of the `w3m-image-alt' property at the given POSITION.
NOTE: If POSITION is omitted, it searches for the property in one
character before and behind the current position, and point will move
to the position where the property exists."
(if position
`(get-text-property ,position 'w3m-image-alt)
`(w3m-get-text-property-around 'w3m-image-alt)))
(defmacro w3m-anchor-title (&optional position)
"Return the value of the `w3m-anchor-title' property at the given POSITION.
NOTE: If POSITION is omitted, it searches for the property in one
character before and behind the current position, and point will move
to the position where the property exists."
(if position
`(get-text-property ,position 'w3m-anchor-title)
`(w3m-get-text-property-around 'w3m-anchor-title)))
(defmacro w3m-submit (&optional position)
"Return the value of the `w3m-submit' property at the given POSITION.
NOTE: If POSITION is omitted, it searches for the property in one
character before and behind the current position, and point will move
to the position where the property exists."
(if position
`(get-text-property ,position 'w3m-submit)
`(w3m-get-text-property-around 'w3m-submit)))
(defmacro w3m-anchor-sequence (&optional position)
"Return the value of the `w3m-anchor-sequence' property at POSITION.
If POSITION is omitted, the current position is assumed."
(if position
`(get-text-property ,position 'w3m-anchor-sequence)
'(get-text-property (point) 'w3m-anchor-sequence)))
;;; Attributes:
(eval-and-compile
;; `eval-and-compile' is necessary since the value of the constant
;; is referred to at the compile time.
(defconst w3m-html-string-regexp
"\\(\"\\([^\"]+\\)\"\\|'\\([^']+\\)'\\|[^\"'<> \t\r\f\n]*\\)"
"Regexp matching a string of the field-value like <a href=\"VALUE\">."))
(put 'w3m-parse-attributes 'lisp-indent-function '1)
(def-edebug-spec w3m-parse-attributes
((&rest &or (symbolp &optional symbolp) symbolp) body))
(defmacro w3m-parse-attributes (attributes &rest forms)
"Extract ATTRIBUTES, KEYWORD=\"VALUE\" pairs, in a tag and run FORMS.
ATTRIBUTES is a list of symbols that looks like `(KEYWORD KEYWORD...)'.
A symbol KEYWORD, that will express a value extracted from a tag, can
be used as a Lisp variable within FORMS. The point has to be within
a tag initially, and only attributes that follow the point will be
extracted.
The value of KEYWORD is a string by default, or is nil if the KEYWORD
is not found in a tag. KEYWORD can be `(KEYWORD TYPE)', where TYPE is
one of `:case-ignore', `:integer', `:bool', and `:decode-entity'.
Those types mean converting the value into a lower-case string,
an integer, a boolean (t or nil), and a decoded string respectively."
`(let (,@(mapcar (lambda (attr)
(if (listp attr)
(car attr)
attr))
attributes))
(skip-chars-forward " \t\r\f\n")
(while
(cond
,@(mapcar
(lambda (attr)
(or (symbolp attr)
(and (listp attr)
(<= (length attr) 2)
(symbolp (car attr)))
(error "Internal error, type mismatch"))
(let ((sexp (quote
(w3m-remove-redundant-spaces
(or (match-string-no-properties 2)
(match-string-no-properties 3)
(match-string-no-properties 1)))))
type)
(when (listp attr)
(setq type (nth 1 attr))
(cond
((eq type :case-ignore)
(setq sexp (list 'downcase sexp)))
((eq type :integer)
(setq sexp (list 'string-to-number sexp)))
((eq type :bool)
(setq sexp t))
((eq type :decode-entity)
(setq sexp (list 'w3m-decode-entities-string sexp)))
((nth 1 attr)
(error "Internal error, unknown modifier")))
(setq attr (car attr)))
`((looking-at
,(if (eq type :bool)
(format "%s\\(?:[ \t\r\f\n]*=[ \t\r\f\n]*%s\\)?"
(symbol-name attr)
w3m-html-string-regexp)
(format "%s[ \t\r\f\n]*=[ \t\r\f\n]*%s"
(symbol-name attr)
w3m-html-string-regexp)))
(setq ,attr ,sexp))))
attributes)
((looking-at ,(concat "[A-Za-z]*[ \t\r\f\n]*=[ \t\r\f\n]*"
w3m-html-string-regexp)))
((looking-at "[^<> \t\r\f\n]+")))
(goto-char (match-end 0))
(skip-chars-forward " \t\r\f\n"))
(skip-chars-forward "^>")
(forward-char)
,@forms))
;;; Working buffers:
(defun w3m-get-buffer-create (name)
"Return the buffer named NAME, or create such a buffer and return it."
(or (get-buffer name)
(let ((buf (get-buffer-create name)))
(setq w3m-work-buffer-list (cons buf w3m-work-buffer-list))
(buffer-disable-undo buf)
buf)))
(defun w3m-kill-buffer (buffer)
"Kill the buffer BUFFER and remove it from `w3m-work-buffer-list'.
The argument may be a buffer or may be the name of a buffer.
An argument of nil means kill the current buffer."
(unless buffer
(setq buffer (current-buffer)))
(when (stringp buffer)
(setq buffer (get-buffer buffer)))
(when (buffer-live-p buffer)
(kill-buffer buffer))
(setq w3m-work-buffer-list (delq buffer w3m-work-buffer-list))
nil)
(defun w3m-kill-all-buffer ()
"Kill all working buffer."
(dolist (buf w3m-work-buffer-list)
(when (buffer-live-p buf)
(kill-buffer buf)))
(setq w3m-work-buffer-list nil))
(defun w3m-current-title ()
"Return the title of the current buffer."
(cond
(w3m-current-process
"<retrieving>")
((and (stringp w3m-current-title)
(not (string= w3m-current-title "<no-title>")))
w3m-current-title)
((stringp w3m-current-url)
(directory-file-name
(if (string-match "\\`[^/:]+:/+" w3m-current-url)
(substring w3m-current-url (match-end 0))
w3m-current-url)))
(t "<no-title>")))
(defun w3m-buffer-title (buffer)
"Return the title of the buffer BUFFER."
(with-current-buffer buffer
(w3m-current-title)))
(defun w3m-buffer-number (buffer)
(when (and (bufferp buffer)
(string-match "\\*w3m\\*\\(<\\([0-9]+\\)>\\)?\\'"
(buffer-name buffer)))
(if (match-beginning 1)
(string-to-number (match-string 2 (buffer-name buffer)))
1))) ;; `1' should not be represented in the buffer name.
(defun w3m-buffer-set-number (buffer number)
(with-current-buffer buffer
(let ((newname (if w3m-use-title-buffer-name
(if (= number 1)
(format "%s *w3m*" (w3m-current-title))
(format "%s *w3m*<%d>" (w3m-current-title) number))
(if (= number 1)
"*w3m*"
(format "*w3m*<%d>" number)))))
(if (eq (w3m-buffer-number buffer) number)
(when w3m-use-title-buffer-name
(unless (get-buffer newname)
(rename-buffer newname)))
(unless (get-buffer newname)
(rename-buffer newname))))))
(defun w3m-buffer-name-add-title ()
"Add current tile to buffer name."
(when w3m-use-title-buffer-name
(let ((number (w3m-buffer-number (current-buffer)))
newname)
(if (= number 1)
(setq newname (format "%s *w3m*" (w3m-current-title)))
(setq newname (format "%s *w3m*<%d>" (w3m-current-title) number)))
(rename-buffer newname))))
(defun w3m-generate-new-buffer (name &optional next)
"Create and return a buffer with a name based on NAME.
Make the new buffer the next of the current buffer if NEXT is non-nil."
(when (string-match "\\*w3m\\*\\(<\\([0-9]+\\)>\\)\\'" name)
(setq name (substring name 0 (match-beginning 1))))
(let* ((w3m-fb-mode nil)
(all (w3m-list-buffers))
(num (1+ (length all)))
(tail (if next
(memq (current-buffer) all)
(last all)))
new prev)
(when tail
(setq new (1+ (or (w3m-buffer-number (car tail)) 1))
prev (current-buffer))
(dolist (buf (nreverse (cdr tail)))
(w3m-buffer-set-number buf num)
(setq num (1- num))))
(with-current-buffer
(setq new (generate-new-buffer (if new
(format "%s<%d>" name new)
name)))
(w3m-mode)
(setq w3m-previous-session-buffer prev))
new))
(defun w3m-buffer-name-lessp (x y)
"Return t if first arg buffer's name is less than second."
(when (bufferp x)
(setq x (buffer-name x)))
(when (bufferp y)
(setq y (buffer-name y)))
(if (and (string-match "\\*w3m\\*\\(<\\([0-9]+\\)>\\)?\\'" x)
(setq x (cons x
(if (match-beginning 1)
(string-to-number (match-string 2 x))
0))))
(if (string-match "\\*w3m\\*\\(<\\([0-9]+\\)>\\)?\\'" y)
(< (cdr x)
(if (match-beginning 1)
(string-to-number (match-string 2 y))
0))
(string< (car x) y))
(string< x y)))
(defun w3m-list-buffers (&optional nosort)
"Return a list of buffers in which emacs-w3m sessions are open.
If the optional NOSORT is nil, the list is sorted in the order of
buffer names."
(let ((buffers (buffer-list))
buffer rest)
(save-current-buffer
(while buffers
(set-buffer (setq buffer (pop buffers)))
(when (eq major-mode 'w3m-mode)
(push buffer rest))))
(setq buffers (if nosort
(nreverse rest)
(sort rest #'w3m-buffer-name-lessp)))
(when (and (boundp 'w3m-fb-mode)
w3m-fb-mode
(if (or w3m-pop-up-frames
(not (memq 'w3m-fb-add w3m-mode-hook)))
;; `w3m-fb-mode' might have been set by something
;; other than the `w3m-fb-mode' function.
(setq w3m-fb-mode nil)
t))
;; Don't just return `w3m-fb-buffer-list' for the selected frame
;; because `buffers' may have been sorted.
(let ((fbs (frame-parameter w3m-fb-list-buffers-frame
'w3m-fb-buffer-list)))
(setq rest buffers)
(while rest
(unless (memq (setq buffer (pop rest)) fbs)
(setq buffers (delq buffer buffers))))))
buffers))
;;; Pop up and delete buffers, windows or frames:
(defmacro w3m-popup-frame-p ()
"Return non-nil if `w3m-pop-up-frames' is non-nil and it is effective."
'(and w3m-pop-up-frames (display-graphic-p)))
(defun w3m-lefttab-exist-p (&optional buffer)
(not (eq (or buffer (current-buffer)) (car (w3m-list-buffers)))))
(defun w3m-righttab-exist-p (&optional buffer)
(let ((bufs (w3m-list-buffers))
(cbuf (or buffer (current-buffer)))
buf)
(catch 'exist
(while (setq buf (car bufs))
(setq bufs (cdr bufs))
(when (eq cbuf buf)
(throw 'exist bufs))))))
(defmacro w3m-popup-window-p ()
"Return non-nil if `w3m-pop-up-windows' is non-nil and the present
situation allows it."
'(and w3m-pop-up-windows
(not (get-buffer-window w3m-select-buffer-name))))
(defvar w3m-initial-frames nil
"Variable used to keep a list of the frame-IDs when emacs-w3m sessions
are popped-up as new frames. This variable is used for the control
for not deleting frames made for aims other than emacs-w3m sessions.")
(make-variable-buffer-local 'w3m-initial-frames)
(defun w3m-popup-buffer (buffer)
"Pop up BUFFER as a new window or a new frame, per the `w3m-display-mode'."
(let ((window (get-buffer-window buffer t))
oframe popup-frame-p frame pop-up-frames buffers other)
(unless (eq window (selected-window))
(setq oframe (selected-frame)
popup-frame-p (w3m-popup-frame-p))
(if (setq
pop-up-frames
(if window ;; The window showing BUFFER already exists.
;; Don't pop up a new frame if it is just the current frame.
(not (eq (setq frame (window-frame window)) oframe))
;; There is no window for BUFFER, so look for the existing
;; emacs-w3m window if the tabs line is enabled or the
;; selection window exists (i.e., we can reuse it).
(if (or w3m-use-tab
(get-buffer-window w3m-select-buffer-name t))
(progn
(setq buffers (delq buffer (w3m-list-buffers t)))
(while (and (not window)
buffers)
(setq window
(get-buffer-window (setq other (pop buffers)) t)))
(if window ;; The window showing another buffer exists.
(not (eq (setq frame (window-frame window)) oframe))
(setq other nil)
;; There is no window after all, so leave to the value
;; of `w3m-pop-up-frames' whether to pop up a new frame.
popup-frame-p))
;; Ditto.
popup-frame-p)))
(progn
(cond (other
;; Pop up another emacs-w3m buffer and switch to BUFFER.
(pop-to-buffer other)
;; Change the value for BUFFER's `w3m-initial-frames'.
(setq w3m-initial-frames
(prog1
(copy-sequence w3m-initial-frames)
(switch-to-buffer buffer))))
(frame
;; Pop up the existing frame which shows BUFFER.
(pop-to-buffer buffer))
(t
;; Pop up a new frame.
(let ((pop-up-frame-alist (or w3m-popup-frame-parameters
pop-up-frame-alist)))
(pop-to-buffer buffer))
(setq frame (window-frame (get-buffer-window buffer t)))))
(select-frame-set-input-focus frame))
;; Simply switch to BUFFER in the current frame.
(let ((cd default-directory))
(if (w3m-popup-window-p)
(switch-to-buffer-other-window buffer)
(switch-to-buffer buffer))
(setq default-directory cd))
(w3m-history-restore-position)))))
(defun w3m-add-w3m-initial-frames (&optional frame)
"Add FRAME to `w3m-initial-frames', the buffer-local variable.
It is done when FRAME is newly created for the emacs-w3m session.
This function will be added to `after-make-frame-functions' to run."
(unless frame
(setq frame (selected-frame)))
;; Share the opened frame in `w3m-initial-frames' over all emacs-w3m
;; buffers if using a tabbed display mode. Otherwise, the frame is
;; appended to `w3m-initial-frames' only in the current buffer.
(with-current-buffer (window-buffer (frame-first-window frame))
(when (eq major-mode 'w3m-mode)
(unless (memq frame w3m-initial-frames)
(push frame w3m-initial-frames))
(when w3m-use-tab
(dolist (buffer (delq (current-buffer) (w3m-list-buffers t)))
(set-buffer buffer)
(unless (memq frame w3m-initial-frames)
(push frame w3m-initial-frames)))))))
(add-hook 'after-make-frame-functions 'w3m-add-w3m-initial-frames)
(defun w3m-delete-w3m-initial-frames (frame)
"Delete FRAME from `w3m-initial-frames', the buffer-local variable.
It is done when the FRAME in which emacs-w3m is running is deleted.
This function is added to `delete-frame-functions' or merged into
the `delete-frame' function using `defadvice'."
(save-current-buffer
(dolist (buffer (w3m-list-buffers t))
(set-buffer buffer)
(setq w3m-initial-frames (delq frame w3m-initial-frames)))))
(add-hook 'delete-frame-functions 'w3m-delete-w3m-initial-frames)
(defun w3m-delete-frames-and-windows (&optional exception)
"Delete all frames and windows related to emacs-w3m buffers.
If EXCEPTION is a buffer, a window or a frame, it and related visible
objects will not be deleted. There are special cases; the following
objects will not be deleted:
1. The sole frame in the display device.
2. Frames created not for emacs-w3m sessions.
3. Frames showing not only emacs-w3m sessions but also other windows.\
"
(let ((buffers (delq exception (w3m-list-buffers t)))
buffer windows window frame one-window-p flag)
(save-current-buffer
(while buffers
(setq buffer (pop buffers)
windows (delq exception
(get-buffer-window-list buffer 'no-minibuf t)))
(set-buffer buffer)
(while windows
(setq window (pop windows)
frame (and (window-live-p window) (window-frame window)))
(when (and frame
(not (eq frame exception)))
(setq flag nil)
(setq one-window-p
;; This is similar to the `one-window-p' function
;; but works for even unselected windows in addition
;; to the selected window in all frames.
(catch 'two
(walk-windows (lambda (w)
(when (eq (window-frame w) frame)
(if flag
(throw 'two nil)
(setq flag t))))
'no-minibuf t)
flag))
(when (and
(or
;; A frame having only windows for emacs-w3m
;; sessions or the buffer selection can be deleted.
(progn
(setq flag t)
(walk-windows
(lambda (w)
(when flag
(if (eq w exception)
(setq flag nil)
(set-buffer (window-buffer w))
(when (setq flag
(or
(memq major-mode
'(w3m-mode
w3m-select-buffer-mode
w3m-session-select-mode))
(string-match "\\` ?\\*w3m[ -]"
(buffer-name))))
(if (eq (next-window w 'no-minibuf) w)
(bury-buffer)
(delete-window w))))))
'no-minibuf)
(set-buffer buffer)
flag)
;; Also a frame having the sole window can be deleted.
one-window-p)
(memq frame w3m-initial-frames)
(not (eq (next-frame frame) frame)))
(let ((delete-frame-functions
(delq 'w3m-fb-delete-frame-buffers
(copy-sequence delete-frame-functions))))
(delete-frame frame)))))))))
;;; Navigation:
(defmacro w3m-goto-next-defun (name property)
"Create function w3m-goto-next- NAME.
Return position of the first occurence of PROPERTY.
If currently over such PROPERTY, find next such occurence."
`(defun ,(intern (concat "w3m-goto-next-" (symbol-name name)))
(&optional pos)
,(concat "Return position of next " (symbol-name name)
" starting from POS or point.")
(setq pos (or pos (point)))
(if (get-char-property pos ',property) ; currently over such element
(setq pos (next-single-property-change pos ',property)))
(if (or (get-char-property pos ',property)
(setq pos (next-single-property-change pos ',property)))
pos)))
(w3m-goto-next-defun link w3m-href-anchor)
(w3m-goto-next-defun image2 w3m-image)
(defun w3m-goto-next-anchor-or-image (&optional pos)
"Return position of next anchor or image starting from POS or point."
(setq pos (or pos (point)))
(cond ; currently on anchor or image
((w3m-anchor-sequence pos)
(setq pos (next-single-property-change pos 'w3m-anchor-sequence)))
((w3m-image pos)
(setq pos (next-single-property-change pos 'w3m-image))))
(or (w3m-anchor-sequence pos)
(w3m-image pos)
(let ((image-pos (next-single-property-change pos 'w3m-image)))
(setq pos (next-single-property-change pos
'w3m-anchor-sequence))
(and image-pos
(or (not pos) (> pos image-pos))
(setq pos image-pos))))
(if pos
(let ((hseq (w3m-anchor-sequence pos)))
(if (and hseq (text-property-any ; multiline anchors
(point-min) pos 'w3m-anchor-sequence hseq))
(w3m-goto-next-anchor-or-image pos)
pos))))
;;; Miscellaneous:
(eval-and-compile
(defconst w3m-url-invalid-base "http:///"
"A url base used to make url absolutely invalid.
`w3m-expand-url' will use it as a last resort if no other appropriate
base is given."))
(defsubst w3m-url-valid (url)
"Return URL if it is not marked invalid, otherwise nil.
This function is intended only to reject a url that `w3m-expand-url'
marks invalid purposely (using `w3m-url-invalid-base'), does not
necessarily guarantee that URL to return is valid in a general sense."
;; cf. [emacs-w3m:04095], [emacs-w3m:04101] (Japanese),
;; and [emacs-w3m:12761] (summarized in English).
(and url
(not (string-match (eval-when-compile
(concat "\\`" w3m-url-invalid-base))
url))
url))
(defmacro w3m-substitute-key-definitions (new-map old-map &rest keys)
"In NEW-MAP substitute cascade of OLD-MAP KEYS.
KEYS is alternating list of key-value."
(let ((n-map new-map)
(o-map old-map))
`(progn
,@(let ((res nil))
(while keys
(push `(substitute-key-definition
,(car keys) ,(cadr keys) ,n-map ,o-map)
res)
(setq keys (cddr keys)))
(nreverse res)))))
(defun w3m-search-tag-1 (regexp)
"Subroutine used by `w3m-search-tag'."
(let ((start (point))
begin end)
(if (and (re-search-forward regexp nil t)
(setq begin (match-beginning 0)
end (match-end 0))
(or (looking-at "/?>")
(and (looking-at "[ \t\f\n]")
(search-forward ">" nil t))))
(prog1
(goto-char (match-end 0))
(set-match-data
(cond ((= end (match-beginning 0))
(list begin (match-end 0)
(1+ begin) end))
((eq (char-before (match-beginning 0)) ?/)
(if (= end (1- (match-beginning 0)))
(list begin (match-end 0)
(1+ begin) end)
(list begin (match-end 0)
(1+ begin) end
end (- (match-end 0) 2))))
(t
(list begin (match-end 0)
(1+ begin) end
end (1- (match-end 0)))))))
(set-match-data nil)
(goto-char start)
nil)))
(defmacro w3m-search-tag (&rest names)
"Search forward for a tag which begins with one of NAMES.
This macro generates the form equivalent to:
(re-search-forward \"<\\\\(NAMES\\\\)\\\\([ \\t\\f\\n]+[^>]*\\\\)?/?>\" nil t)
but it works even if the tag is considerably large.
Note: this macro allows only strings for NAMES, that is, a form
something like `(if foo \"bar\" \"baz\")' cannot be used."
`(w3m-search-tag-1 ,(concat "<" (regexp-opt names t))))
(defun w3m-beginning-of-tag (&optional tag include-whitespace)
"Move point to the beginning of tag. Inner nested tags are skipped.
If TAG, which is a name of the tag, is given, this function moves point
backward from the closing-tag </TAG> (point has to exist after or within
it initially) to the beginning point of the open-tag <TAG ...>. For
example, in the following two situations, point moves backward from the
rightmost tag to the beginning point of the leftmost tag:
<TAG ...>...<TAG ...>...<TAG ...>...</TAG>...</TAG>...</TAG>
<TAG ...>...<TAG ...>...</TAG>...<TAG ...>...</TAG>...</TAG>
If TAG is omitted or nil, this function moves point backward to the
beginning point of the tag in which point exists. In this case, point
has to initially exist between the end position of the closing-tag and
the previous tag as follows:
<!-- foo <bar ...<baz ...>...> -->
^^^
If INCLUDE-WHITESPACE is non-nil, include leading and trailing
whitespace. Return the end-point and set the match-data #0, #1, #2,
and #3 as follows (\"___\" shows whitespace):
The case where TAG is spefified:
___<TAG ...>___...___</TAG>___
0 1 2 2 1 0 INCLUDE-WHITESPACE=nil
0 1 2 3 3 2 1 0 INCLUDE-WHITESPACE=non-nil
The case where TAG is nil:
___<TAG ...>___
0 0 INCLUDE-WHITESPACE=nil
0 1 1 0 INCLUDE-WHITESPACE=non-nil"
(let ((init (point))
(num 1)
(md (match-data))
(case-fold-search t)
end regexp nd1 nd2 nd3 st1 st2 st3 st0 nd0)
(condition-case nil
(progn
(if tag
(progn
(setq end (point)
tag (regexp-quote tag))
(if (and (re-search-backward (concat "\
\\(<[\t\n\r ]*/[\t\n\r ]*" tag "\
\\(?:[\t\n\r ]*\\|[\t\n\r ]+[^>]+\\)>\\)[\t\n\r ]*") nil t)
(eq end (match-end 0)))
(progn
(setq nd1 (nth 3 (match-data)) ;; (match-end 1)
nd2 (nth 2 (match-data))) ;; (match-beginning 1)
(skip-chars-backward "\t\n\r ")
(setq nd3 (point-marker))
(goto-char end))
(goto-char end)
(search-forward ">")
(setq end (point))
(if (and (re-search-backward (concat "\
<[\t\n\r ]*/[\t\n\r ]*" tag "\\(?:[\t\n\r ]*\\|[\t\n\r ]+[^>]+\\)>"))
(eq end (match-end 0)))
(progn
(setq nd1 (nth 1 (match-data)) ;; (match-end 0)
nd2 (car (match-data))) ;; (match-beginning 0)
(skip-chars-backward "\t\n\r ")
(setq nd3 (point-marker)))
(error "")))
(goto-char (1- nd2))
(setq regexp (concat "\\(<\\([\t\n\r ]*/\\)?[\t\n\r ]*" tag "\
\\(?:[\t\n\r ]*\\|[\t\n\r ]+[^>]+\\)>\\)[\t\n\r ]*"))
(while (and (> num 0)
(re-search-backward regexp))
(setq num (if (match-beginning 2)
(1+ num)
(1- num))))
(setq st1 (car (match-data)) ;; (match-beginning 0)
st2 (nth 3 (match-data)) ;; (match-end 1)
;; There may be only whitespace between <tag>...</tag>.
st3 (min nd3 (nth 1 (match-data))))) ;; (match-end 0)
(search-forward ">")
(setq nd1 (nth 1 (match-data))) ;; (match-end 0)
(goto-char init)
(while (and (> num 0)
(re-search-backward "\\(<\\)\\|>"))
(setq num (if (match-beginning 1)
(1- num)
(1+ num))))
(setq st1 (nth 2 (match-data)))) ;; (match-beginning 1)
(if include-whitespace
(progn
(skip-chars-backward "\t\n\r ")
(setq st0 (point-marker))
(goto-char nd1)
(skip-chars-forward "\t\n\r ")
(setq nd0 (point-marker))
(goto-char st0)
(set-match-data (if tag
(list st0 nd0 st1 nd1 st2 nd2 st3 nd3)
(list st0 nd0 st1 nd1))))
(set-match-data (if tag
(list st1 nd1 st2 nd2 st3 nd3)
(list st1 nd1))))
(point))
(error
(set-match-data md)
(goto-char init)
nil))))
(defun w3m-end-of-tag (&optional tag include-whitespace)
"Move point to the end of tag. Inner nested tags are skipped.
If TAG, which is a name of the tag, is given, this function moves point
from the open-tag <TAG ...> (point has to exist in front of or within
it initially) to the end point of the closing-tag </TAG>. For example,
in the following two situations, point moves from the leftmost tag to
the end point of the rightmost tag:
<TAG ...>...<TAG ...>...<TAG ...>...</TAG>...</TAG>...</TAG>
<TAG ...>...<TAG ...>...</TAG>...<TAG ...>...</TAG>...</TAG>
If TAG is omitted or nil, this function moves point to the end point of
the tag in which point exists. In this case, point has to initially
exist between the beginning position of the tag and the next tag as
follows:
<!-- foo <bar ...<baz ...>...> -->
^^^^^^^^
If INCLUDE-WHITESPACE is non-nil, include leading and trailing
whitespace. Return the end-point and set the match-data #0, #1, #2,
and #3 as follows (\"___\" shows whitespace):
The case where TAG is spefified:
___<TAG ...>___...___</TAG>___
0 1 2 2 1 0 INCLUDE-WHITESPACE=nil
0 1 2 3 3 2 1 0 INCLUDE-WHITESPACE=non-nil
The case where TAG is nil:
___<TAG ...>___
0 0 INCLUDE-WHITESPACE=nil
0 1 1 0 INCLUDE-WHITESPACE=non-nil"
(let ((init (point))
(num 1)
(md (match-data))
(case-fold-search t)
regexp st1 st2 st3 nd1 nd2 nd3 nd0 st0)
(condition-case nil
(progn
(if tag
(progn
(setq tag (regexp-quote tag))
(if (looking-at (concat "\
[\t\n\r ]*\\(<[\t\n\r ]*" tag "\\(?:[\t\n\r ]*\\|[\t\n\r ]+[^>]+\\)>\\)\
[\t\n\r ]*"))
(setq st1 (nth 2 (match-data)) ;; (match-beginning 1)
st2 (nth 3 (match-data)) ;; (match-end 1)
st3 (nth 1 (match-data))) ;; (match-end 0)
(search-backward "<")
(if (looking-at (concat "\
\\(<[\t\n\r ]*" tag "\\(?:[\t\n\r ]*\\|[\t\n\r ]+[^>]+\\)>\\)[\t\n\r ]*"))
(setq st1 (car (match-data)) ;; (match-beginning 0)
st2 (nth 3 (match-data)) ;; (match-end 1))
st3 (nth 1 (match-data))) ;; (match-end 0)
(error "")))
(goto-char (1+ st1))
(setq regexp (concat "\
[\t\n\r ]*\\(<\\([\t\n\r ]*/\\)?[\t\n\r ]*" tag "\
\\(?:[\t\n\r ]*\\|[\t\n\r ]+[^>]+\\)>\\)"))
(while (and (> num 0)
(re-search-forward regexp))
(setq num (if (match-beginning 2)
(1- num)
(1+ num))))
(setq nd1 (nth 3 (match-data)) ;; (match-end 1)
nd2 (nth 2 (match-data)) ;; (match-beginning 1)
;; There may be only whitespace between <tag>...</tag>.
nd3 (max st3 (car (match-data))))) ;; (match-beginning 0)
(search-backward "<")
(setq st1 (car (match-data))) ;; (match-beginning 0)
(goto-char init)
(while (and (> num 0)
(re-search-forward "\\(>\\)\\|<"))
(setq num (if (match-beginning 1)
(1- num)