-
Notifications
You must be signed in to change notification settings - Fork 2
/
pjb-emacs.el
3015 lines (2636 loc) · 110 KB
/
pjb-emacs.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
;;;; -*- mode:emacs-lisp;coding:utf-8; lexical-binding:t -*-
;;;;****************************************************************************
;;;;FILE: pjb-emacs.el
;;;;LANGUAGE: emacs lisp
;;;;SYSTEM: emacs
;;;;USER-INTERFACE: emacs
;;;;DESCRIPTION
;;;;
;;;; This module exports various functions usefull only in interactive
;;;; emacs sessions.
;;;;
;;;;AUTHORS
;;;; <PJB> Pascal J. Bourguignon
;;;;MODIFICATIONS
;;;; 2010-10-30 <PJB> Renamed multifile-replace-string to recursive-replace-string,
;;;; Added recursive-replace-regexp and multifile-replace-regexp.
;;;; 2006-03-23 <PJB> Added fringe-width and scroll-bar-width for full-frame.
;;;; 2004-10-15 <PJB> Added maximize-window.
;;;; 2001-11-30 <PJB> Extracted from pjb-utilities.el.
;;;;
;;;;BUGS
;;;;LEGAL
;;;; LGPL
;;;;
;;;; Copyright Pascal J. Bourguignon 1990 - 2011
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 2 of the License, or (at your option) any later version.
;;;;
;;;; This library 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
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
;;;;
;;;;****************************************************************************
(require 'cl)
(require 'devices nil t)
(require 'font nil t)
(require 'browse-url)
(require 'picture) ;; (import picture-vertical-step picture-horizontal-step)
(require 'sgml-mode)
(require 'pjb-cl)
(require 'pjb-font)
(require 'pjb-sources)
(require 'pjb-strings)
(defvar html-quick-keys t)
(defvar html-mode-map
(let ((map (nconc (make-sparse-keymap) sgml-mode-map))
(menu-map (make-sparse-keymap "HTML")))
(define-key map "\C-c6" 'html-headline-6)
(define-key map "\C-c5" 'html-headline-5)
(define-key map "\C-c4" 'html-headline-4)
(define-key map "\C-c3" 'html-headline-3)
(define-key map "\C-c2" 'html-headline-2)
(define-key map "\C-c1" 'html-headline-1)
(define-key map "\C-c\r" 'html-paragraph)
(define-key map "\C-c\n" 'html-line)
;; (define-key map "\C-c\C-c-" 'html-horizontal-rule)
;; (define-key map "\C-c\C-co" 'html-ordered-list)
;; (define-key map "\C-c\C-cu" 'html-unordered-list)
;; (define-key map "\C-c\C-cr" 'html-radio-buttons)
;; (define-key map "\C-c\C-cc" 'html-checkboxes)
;; (define-key map "\C-c\C-cl" 'html-list-item)
;; (define-key map "\C-c\C-ch" 'html-href-anchor)
;; (define-key map "\C-c\C-cn" 'html-name-anchor)
;; (define-key map "\C-c\C-ci" 'html-image)
(if html-quick-keys
(progn
(define-key map "\C-c-" 'html-horizontal-rule)
(define-key map "\C-co" 'html-ordered-list)
(define-key map "\C-cu" 'html-unordered-list)
(define-key map "\C-cr" 'html-radio-buttons)
(define-key map "\C-cc" 'html-checkboxes)
(define-key map "\C-cl" 'html-list-item)
(define-key map "\C-ch" 'html-href-anchor)
(define-key map "\C-cn" 'html-name-anchor)
(define-key map "\C-ci" 'html-image)))
(define-key map "\C-c\C-s" 'html-autoview-mode)
(define-key map "\C-c\C-v" 'browse-url-of-buffer)
(define-key map [menu-bar html] (cons "HTML" menu-map))
(define-key menu-map [html-autoview-mode]
'("Toggle Autoviewing" . html-autoview-mode))
(define-key menu-map [browse-url-of-buffer]
'("View Buffer Contents" . browse-url-of-buffer))
(define-key menu-map [nil] '("--"))
;;(define-key menu-map "6" '("Heading 6" . html-headline-6))
;;(define-key menu-map "5" '("Heading 5" . html-headline-5))
;;(define-key menu-map "4" '("Heading 4" . html-headline-4))
(define-key menu-map "3" '("Heading 3" . html-headline-3))
(define-key menu-map "2" '("Heading 2" . html-headline-2))
(define-key menu-map "1" '("Heading 1" . html-headline-1))
(define-key menu-map "l" '("Radio Buttons" . html-radio-buttons))
(define-key menu-map "c" '("Checkboxes" . html-checkboxes))
(define-key menu-map "l" '("List Item" . html-list-item))
(define-key menu-map "u" '("Unordered List" . html-unordered-list))
(define-key menu-map "o" '("Ordered List" . html-ordered-list))
(define-key menu-map "-" '("Horizontal Rule" . html-horizontal-rule))
(define-key menu-map "\n" '("Line Break" . html-line))
(define-key menu-map "\r" '("Paragraph" . html-paragraph))
(define-key menu-map "i" '("Image" . html-image))
(define-key menu-map "h" '("Href Anchor" . html-href-anchor))
(define-key menu-map "n" '("Name Anchor" . html-name-anchor))
map)
"Keymap for commands for use in HTML mode.") ;;html-mode-map
;;;----------------------------------------------------------------------------
;;; Random emacs specific elisp functions:
;;;----------------------------------------------------------------------------
(unless (fboundp 'symbol-value-in-buffer)
(defun symbol-value-in-buffer (symbol buffer &optional default)
(save-excursion
(set-buffer buffer)
(if (boundp symbol)
(symbol-value symbol)
default)))
(defun set-symbol-value-in-buffer (symbol buffer value &optional default)
(save-excursion
(set-buffer buffer)
(make-local-variable symbol)
(setf (symbol-value symbol) value)))
(defsetf symbol-value-in-buffer set-symbol-value-in-buffer))
(defun recover-this-file ()
"Recovers the file of the current buffer, if any."
(interactive)
(let ((file-path (buffer-file-name)))
(if (and file-path (file-exists-p file-path) (file-regular-p file-path))
(recover-file file-path)
(message "This buffer has no associated file."))))
(defun delete-this-file (kill-buffer)
"Deletes the file of the current buffer, if any.
When KILL-BUFFER is true (command prefix), also kills the buffer."
(interactive "P")
(let ((file-path (buffer-file-name)))
(if (and file-path (file-exists-p file-path) (file-regular-p file-path))
(progn
(delete-file file-path)
(when kill-buffer
(kill-buffer)))
(message "This buffer has no associated file."))))
(defvar buffer-name-map nil)
(defvar buffer-list-cache nil)
(defun buffer-named (name)
"
RETURN: the buffer which has as name `name'.
"
(let ((bl (buffer-list)))
(unless (and buffer-list-cache buffer-name-map
(equal buffer-list-cache bl))
(setf buffer-list-cache (copy-seq bl))
(setf buffer-name-map (make-hash-table :test (function equal)))
(dolist (buffer buffer-list-cache)
(let ((name (buffer-name buffer)))
(when name (setf (gethash name buffer-name-map) buffer)))
(let ((name (buffer-file-name buffer)))
(when name (setf (gethash name buffer-name-map) buffer))))))
(or (gethash name buffer-name-map)
(gethash (truename name) buffer-name-map)))
(defun old-buffer-named (name)
"
RETURN: the buffer which has as name `name'.
"
(let ((buffers (buffer-list)) (result))
(while buffers
(when (or (when (buffer-name (car buffers))
(string-equal name (buffer-name (car buffers))))
(when (buffer-file-name (car buffers))
(string-equal name (buffer-file-name (car buffers))))
(when (and (truename name) (buffer-name (car buffers)))
(string-equal (truename name)
(buffer-name (car buffers))))
(when (and (truename name) (buffer-file-name (car buffers)))
(string-equal (truename name)
(buffer-file-name (car buffers)))))
(setq result (car buffers))
(setq buffers nil))
(setq buffers (cdr buffers)))
result))
(defun pjb-custom-set-variables (&rest l)
(while l
(custom-set-variables (append (car l) (list t)))
(setq l (cdr l))))
(defun set-default-directory (path)
(interactive "DDirectory for this buffer: ")
(setf default-directory path))
(defun reset-home-directory ()
"Set the default directory of all non-file buffers to ~/."
(interactive)
(dolist (buffer (buffer-list))
(unless (buffer-file-name buffer)
(with-current-buffer buffer
(set-default-directory "~/")))))
;;;----------------------------------------------------------------------------
;;; Editing functions:
;;;----------------------------------------------------------------------------
(defun replace-region (start end text)
"In the current buffer, delete the region from `start' to `end' and insert in its place the `text', saving the excursion."
(save-excursion
(goto-char start)
(delete-region start end)
(insert text)))
(defun delete-region-and-yank (&optional arg)
"Deletes region if mark is active and yanks the last kill.
Always replaces the region with the yank, whether the region was
selected via keyboard or mouse. Also works for normal
yank even with ARGS (thus it can be mapped to \C-y)"
(interactive "*P") ; raw, like yank.
(cond
(mark-active ; delete region
(let ((str (buffer-substring (point) (mark))))
(delete-region (point) (mark))
(if (cl:string= str (current-kill 0 1))
(let ((str2 (current-kill 1 1)))
(kill-new str2 t))))
(if arg
(yank arg)
(yank)))
;; else no region selected:
((consp arg) ; delete forward sexp
(set-mark (point))
(forward-sexp 1)
(delete-region-and-yank))
(arg (yank arg))
(t (yank))))
(defun exch-del-ctrl-h ()
"Exchange \C-h and <DEL>."
(interactive)
;; Translate `C-h' to <DEL>.
(keyboard-translate ?\C-h ?\C-?)
;; Translate <DEL> to `C-h'.
(keyboard-translate ?\C-? ?\C-h))
;;;----------------------------------------------------------------------------
;;; picture-mode commands:
;;;----------------------------------------------------------------------------
(defun picture-draw-pixels (pix-list &optional pixel)
"
DO: Draws the pixels of pix-list (a list of (cons x y))
from current position as origin.
Default pixel is '*'.
Coordinate system is : increasing x to the right,
increasing y to the bottom.
"
(let* ((sl (picture-current-line))
(sc (current-column))
;;(pvs picture-vertical-step)
;;(phs picture-horizontal-step)
;;(c1 (progn (goto-char start) (current-column)))
;;(r1 (picture-current-line))
;;(c2 (progn (goto-char end) (current-column)))
;;(r2 (picture-current-line))
;;(right (max c1 c2))
;;(left (min c1 c2))
;;(top (min r1 r2))
;;(bottom (max r1 r2))
)
(unless pixel (setq pixel (character "*")))
(dolist (point pix-list)
(goto-line (+ sl (cdr point))) ;; goto-line first for
(move-to-column (+ sc (car point)) t)
(picture-update-desired-column t)
(picture-insert pixel 1))
(goto-line sl)
(move-to-column sc t))
nil)
(defun ellipse-quart (a b)
"
RETURN: A list of integer coordinates approximating a quart (x>=0, y>=0) of
an ellipse of half width a and half height b.
"
(let ((step (/ pi 4 (sqrt (+ (* a a) (* b b)))))
(limit (/ pi 2))
(alpha 0.0)
(result (list (cons 0 0)))
x y )
(while (<= alpha limit)
(setq x (round (* a (cos alpha)))
y (round (* b (sin alpha))) )
(if (or (/= y (cdar result)) (/= x (caar result)))
(push (cons x y) result))
(setq alpha (+ alpha step)))
(cdr (nreverse result))))
(defun ellipse-full (a b)
"
RETURN: A list of integer coordinates approximating the whole ellipse
of half width a and half height b.
"
(let ((quart (ellipse-quart a b)))
(append
quart
(mapcar (lambda (item) (cons (- 0 (car item)) (cdr item))) quart)
(mapcar (lambda (item) (cons (car item) (- 0 (cdr item)))) quart)
(mapcar (lambda (item) (cons (- 0 (car item)) (- 0 (cdr item)))) quart))))
(defun picture-draw-function (start end fun plot-char)
"
DO: Draw a function in the given rectangle region.
"
(interactive "*r
xFunction f:[0,1]->[0,1]/x|-->f(x):
cPlot character: ") ;; start <= end
(let* ((sl (picture-current-line))
(sc (current-column))
(pvs picture-vertical-step)
(phs picture-horizontal-step)
(c1 (progn (goto-char start) (current-column)))
(r1 (picture-current-line))
(c2 (progn (goto-char end) (current-column)))
(r2 (picture-current-line))
(right (max c1 c2))
(left (min c1 c2))
(top (min r1 r2))
(bottom (max r1 r2))
(width (+ 0.0 (- right left)))
(height (+ 0.0 (- bottom top))))
(goto-line top)
(move-to-column left t)
(picture-update-desired-column t)
(flet ((fun (x) (funcall fun x)))
(picture-draw-pixels
(do* ((xi 0 (1+ xi))
(x) (y) (yi)
(pixels nil))
((> xi width) pixels)
(setq x (/ xi width))
(setq y (let ((y (unwind-protect (fun x))))
(if (< y 0.0) 0.0 (if (< 1.0 y) 1.0 y))))
(setq yi (round (* height (- 1.0 y))))
(push (cons xi yi) pixels))
plot-char))
(goto-line sl)
(move-to-column sc t)))
(defun picture-draw-ellipse (start end)
"
DO: Draw an ellipse around region.
BUG: Only draws ellipse of even width and height.
"
(interactive "*r") ; start will be less than end
(let* ((sl (picture-current-line))
(sc (current-column))
(pvs picture-vertical-step)
(phs picture-horizontal-step)
(c1 (progn (goto-char start) (current-column)))
(r1 (picture-current-line))
(c2 (progn (goto-char end) (current-column)))
(r2 (picture-current-line))
(right (max c1 c2))
(left (min c1 c2))
(top (min r1 r2))
(bottom (max r1 r2))
(a (/ (- right left) 2))
(b (/ (- bottom top) 2))
)
(goto-line (+ top b))
(move-to-column (+ left a) t)
(picture-update-desired-column t)
(picture-draw-pixels (ellipse-full a b) ?*)
(goto-line sl)
(move-to-column sc t)))
(defvar x-cell-size 7 "Width in pixel of one cell.")
(defvar y-cell-size 14 "Height in pixel of one cell.")
(defun picture-draw-circle (start end)
"Draw a circle centered on region."
(interactive "*r") ; start will be less than end
(let* ((sl (picture-current-line))
(sc (current-column))
(pvs picture-vertical-step)
(phs picture-horizontal-step)
(c1 (progn (goto-char start) (current-column)))
(r1 (picture-current-line))
(c2 (progn (goto-char end) (current-column)))
(r2 (picture-current-line))
(right (max c1 c2))
(left (min c1 c2))
(top (min r1 r2))
(bottom (max r1 r2))
(a (/ (- right left) 2))
(b (/ (- bottom top) 2))
(r (min (* a (float x-cell-size)) (* b (float y-cell-size))))
)
(goto-line (+ top b))
(move-to-column (+ left a) t)
(picture-update-desired-column t)
(picture-draw-pixels (ellipse-full (round (/ r x-cell-size))
(round (/ r y-cell-size)))?*)
(goto-line sl)
(move-to-column sc t)))
(defvar picture-fill-pixel ?*
"The default pixel used to fill forms.")
(defun picture-fill-rectangle (start end)
"Fills a rectangle with `picture-fill-pixel', or when a prefix
argument is given, with the character given in minibuf."
(interactive "*rP") ; start will be less than end
(let* ((sl (picture-current-line))
(sc (current-column))
(pvs picture-vertical-step)
(phs picture-horizontal-step)
(c1 (progn (goto-char start) (current-column)))
(r1 (picture-current-line))
(c2 (progn (goto-char end) (current-column)))
(r2 (picture-current-line))
(right (max c1 c2))
(left (min c1 c2))
(top (min r1 r2))
(bottom (max r1 r2))
(fill-pixel picture-fill-pixel)
(width (- right left -1))
)
(when current-prefix-arg
(setq fill-pixel (character (read-from-minibuffer
"What pixel: " "*" nil nil nil "*"))))
(picture-movement-right)
(do ((line top (1+ line)))
((< bottom line))
(goto-line line)
(move-to-column left t)
(picture-update-desired-column t)
(picture-insert fill-pixel width))
(picture-set-motion pvs phs)
(goto-line sl)
(move-to-column sc t)))
(defun picture-horizontal-segment (line left right)
(goto-line line)
(move-to-column right t)
(picture-update-desired-column t)
(buffer-substring (- (point) (- right left)) (1+ (point))))
(defun picture-draw-text (line column text)
"Draws given text from (line,column) toward the current picture-movement."
(let* ((sl (picture-current-line))
(sc (current-column))
)
(goto-line line)
(move-to-column column t)
(picture-update-desired-column t)
(do* ((i 0 (1+ i)))
((<= (length text) i))
(picture-insert (char text i) 1))
(goto-line sl)
(move-to-column sc t)))
(defun picture-mirror-vertical (start end)
"Replace the region by it's vertical mirror."
(interactive "*r")
(let* ((sl (picture-current-line))
(sc (current-column))
(pvs picture-vertical-step)
(phs picture-horizontal-step)
(c1 (progn (goto-char start) (current-column)))
(r1 (picture-current-line))
(c2 (progn (goto-char end) (current-column)))
(r2 (picture-current-line))
(right (max c1 c2))
(left (min c1 c2))
(top (min r1 r2))
(bottom (max r1 r2))
)
(picture-movement-left)
(do ((line top (1+ line)))
((< bottom line))
(do* ((segment (prog1 (picture-horizontal-segment line left right)
(move-to-column right t)
(picture-update-desired-column t)))
(i 0 (1+ i)))
((<= (length segment) i))
(picture-insert (char segment i) 1))
)
(picture-set-motion pvs phs)
(goto-line sl)
(move-to-column sc t)))
(defun picture-mirror-horizontal (start end)
"Replace the region by it's vertical mirror."
(interactive "*r")
(let* ((sl (picture-current-line))
(sc (current-column))
(pvs picture-vertical-step)
(phs picture-horizontal-step)
(c1 (progn (goto-char start) (current-column)))
(r1 (picture-current-line))
(c2 (progn (goto-char end) (current-column)))
(r2 (picture-current-line))
(right (max c1 c2))
(left (min c1 c2))
(top (min r1 r2))
(bottom (max r1 r2))
)
(picture-movement-right)
(do* ((lines (do ((line top (1+ line))
(result '()))
((< bottom line) result)
(push (picture-horizontal-segment line left right) result))
(cdr lines))
(line top (1+ line)))
((null lines))
(picture-draw-text line left (car lines)))
(picture-set-motion pvs phs)
(goto-line sl)
(move-to-column sc t)))
;;;----------------------------------------------------------------------------
;;; Various Editor commands:
;;;----------------------------------------------------------------------------
(defun pjb-scratch ()
"
DO: Goes to the *scratch* buffer, creating it if it does not exists.
"
(interactive)
(switch-to-buffer (get-buffer-create "*scratch*")))
(defun pjb-wc ()
"
DO: Apply wc on the file visited in the current buffer.
"
(interactive)
(let ((file-name (buffer-file-name (current-buffer))))
(when file-name
(shell-command (format "wc %s" (shell-quote-argument file-name))))))
(defun pjb-search-in-all-buffers (string)
(interactive "sString: ")
(let ( (list (buffer-list))
buffer )
(save-excursion
(while list
(setq buffer (car list)
list (cdr list))
(set-buffer buffer)
(goto-char (point-min))
(if (search-forward string nil t nil)
(setq list nil)
(setq buffer nil))))
(when buffer (switch-to-buffer buffer))))
(defun* tempfile (&key directory prefix suffix name mode)
(flet ((option (flag value)
(if value
(format "%s %s"
(shell-quote-argument flag)
(shell-quote-argument value)))))
(let ((lines (split-string (shell-command-to-string
(format "tempfile %s %s %s %s %s || echo $?"
(option "-d" directory)
(option "-p" prefix)
(option "-s" suffix)
(option "-n" name)
(option "-m" mode)))
"\n" t)))
(case (length lines)
((1) (first lines))
(otherwise (error "%s\nstatus %s"
(join (butlast lines) "\n")
(car (last lines))))))))
(defun* url-retrieve-as-string (url)
"RETURN: A string containing the data found at the url."
(if (fboundp 'url-retrieve-synchronously)
(with-current-buffer (url-retrieve-synchronously url)
(goto-char (point-min))
(prog1 (buffer-substring (search-forward "\n\n" nil t) (point-max))
(kill-buffer)))
(let ((tmpfile (or (ignore-errors (tempfile))
(format "/tmp/url-retrieve-as-string-%d-%d.data"
(emacs-pid) (random 10000000)))))
(unwind-protect
(progn
(loop
for fetch-command
in (list
(lambda ()
(format "wget --no-convert-links -q -nv -o /dev/null -t 3 -O %s %s 2>/dev/null"
(shell-quote-argument tmpfile)
(shell-quote-argument url)))
(lambda ()
(format "lynx -source %s > %s 2>/dev/null"
(shell-quote-argument url)
(shell-quote-argument tmpfile))))
for command = (format "%s && ( echo $? ; cat %s ) || echo $?"
(funcall fetch-command)
(shell-quote-argument tmpfile))
do (let* ((output (shell-command-to-string command))
(result (read-from-string output))
(status (car result)))
(when (zerop status)
(return (subseq output (1+ (cdr result))))))
finally (error "url-retrieve-as-string cannot find a command to fetch URLs.")))
(ignore-errors (delete-file tmpfile))))))
(defun pjb-browse-url-lynx-xterm (url &optional new-window)
;; new-window ignored
"Ask the Lynx WWW browser to load URL.
Default to the URL around or before point. A new Lynx process is run
in an Xterm window using the Xterm program named by `browse-url-xterm-program'
with possible additional arguments `browse-url-xterm-args'."
(interactive (browse-url-interactive-arg "Lynx URL: "))
(apply #'start-process `(,(concat "lynx" url) nil
"pjb-xterm" ; ,browse-url-xterm-program
,@browse-url-xterm-args
"-geometry" "80x40+10+0" "-bg" "#eeff99"
"-e" "lynx" ,url)))
(defun pjb-auto-scroll-up (speed)
"DO: Scroll down the current buffer until the end-of-buffer is visible,
at the specified speed. Depending on the data, and your reading speed,
speed values between 0.2 and 1 line/sec may be useful."
(interactive "nSpeed: ")
(let ((delay (/ 1.0 speed)))
(message "Auto-scrolling...")
(while (not (pos-visible-in-window-p (point-max)))
(sit-for delay)
(scroll-up 1)
(force-mode-line-update t))
(message "Done.")))
(defun pjb-regexp-nocase-region (start end)
(interactive "r")
(let* ( (s (string-to-vector (buffer-substring start end)))
(l (length s))
(r nil)
(i 0)
c C )
(while (< i l)
(setq c (aref s i))
(setq C (upcase c))
(setq c (downcase c))
(if (eq c C)
(setq r (cons (format "%c" c) r))
(setq r (cons (format "[%c%c]" C c) r)))
(setq i (1+ i))
) ;;while
(delete-region start end)
(insert (apply 'concat (nreverse r)))))
(defun pjb-animate (speed)
(interactive "nSpeed: ")
(let ((delay (/ 1.0 speed))
(done nil))
(widen)
(goto-char (point-min))
(message "Animating...")
(while (not done)
(widen)
(if (search-forward "\f" nil 'at-limit)
nil
(goto-char (point-max))
(setq done t))
(narrow-to-page)
(sit-for delay)
(force-mode-line-update t))
(message "Done.")))
(defun pjb-ansi-colorize-buffer ()
(interactive)
(ansi-color-apply-on-region (point-min) (point-max)))
(defvar pjb-listing-light "LightBlue"
"Background color of light listing area.") ;;pjb-listing-light
(defvar pjb-listing-dark "LightSteelBlue"
"Background color of dark listing area.") ;;pjb-listing-dark
(defun pjb-colorize-listing-region (arg)
"
DO: Colorize the region with group of lines (normaly 1 by 1)
with different background color).
"
(interactive "pGroup size: ")
(error "Sorry, it does not work yet.")
(setq arg (prefix-numeric-value arg))
(setq current-prefix-arg nil)
(let ( (lines-forward (1+ (or arg 1)))
(color (cons pjb-listing-light pjb-listing-dark))
(start (region-beginning))
(end (region-end)) )
;; round the end to the beginning of next line.
(goto-char end)
(when (/= end (line-beginning-position))
(beginning-of-line 2)
(setq end (point)))
;; round the start to the beginning of first line.
(goto-char start)
(when (/= start (line-beginning-position))
(beginning-of-line)
(setq start (point)))
(while (< start end)
(goto-char start)
;; (message "avant %S" (point))
(beginning-of-line lines-forward)
;; (message "apres %S" (point))
(if (< end (point))
(progn (goto-char end) (beginning-of-line 2)))
;;(message "%16s from %4d to %4d" (car color) start (point))
(set-mark start)
(goto-char (point))
(facemenu-set-background (car color) start (point))
(setq start (point))
(setq color (cons (cdr color) (car color))))))
(defun pjb-address (pattern)
"
DO: Search an address in my address book (~/private/info/personnes.form)
"
(interactive "MSearch address: ")
(let ((personnes-forms (buffer-named "personnes.forms")))
(if personnes-forms
(switch-to-buffer personnes-forms)
(forms-find-file (format "%sprivate/info/personnes.forms"
(namestring (user-homedir-pathname))))))
(forms-search-forward pattern))
(defvar pjb-cross-references-rejected-regexp
"\\(^\\.?#\\|~$\\|\\.\\(elc\\|o\\|a\\)$\\)"
"A regexp matching file names that should not be searched
for cross references.")
(defun pjb-cross-references ()
"
DO: Grep current directory for sources containing the current word.
"
(interactive)
(let ( (word (current-word))
(files (delete nil
(mapcar (lambda (name)
(cond
((file-directory-p name) nil)
((string-match pjb-cross-references-rejected-regexp
name) nil)
(t (shell-quote-argument name))) )
(directory-files "." nil nil t)))) )
(grep (format "grep -n -e %s %s"
(shell-quote-argument word) (unsplit-string files " ")))))
(defun pjb-backcolors ()
"
DO: Insert in the current buffer a list of colors and
facemenu-set-background them.
"
(interactive)
(let ((f (lambda (x) (+ 255 (* 6 (- x 15))))) )
(for
r 10 12
(for
g 10 12
(for
b 10 12
(let ((min (point)))
(set-mark min)
(printf " * Color : #%x%x%x * \n"
(funcall f r) (funcall f g) (funcall f b))
(facemenu-set-background
(format "#%x%x%x"
(funcall f r) (funcall f g) (funcall f b))
min (point))))))))
(defun reset-faces ()
"Search in ~/.emacs for a custom-set-faces toplevel form, and evaluates it."
;; Unfortunately, custom only updates toplevel forms, so we need to do the same.
(interactive)
(when (or custom-file init-file-user)
(save-window-excursion
(find-file (or custom-file user-init-file))
(goto-char (point-min))
(forward-sexp)
(while (and (< (point) (point-max))
(not
(let ((form (progn (backward-sexp) (sexp-at-point))))
(when (and (listp form)
(eq 'custom-set-faces (first form)))
(eval form)
t))))
(forward-sexp 2)))))
(defun emacs-time-to-universal-time (emacs-time)
(+ (* (first emacs-time) 65536.0)
(second emacs-time)
(/ (third emacs-time) 1000000.0)))
(defun timer-emacs-time (timer)
(list (timer--high-seconds timer)
(timer--low-seconds timer)
(timer--usecs timer)))
(defun timer-delete-function (function)
(cancel-timer (find function (append timer-list timer-idle-list)
:key (function timer--function))))
(defun alarm-ring (&optional name)
(message (if name
(format "Alarm %s!" name)
"Alarm!"))
(loop repeat 10 do (beep) (sleep 0.3)))
(defun set-alarm (delay-seconds name)
(interactive "NDelay (seconds):
sWhy: ")
(run-at-time delay-seconds nil 'alarm-ring
(if (string= name "") "Alarm!" name)))
(defun chronometre (lambda-body &optional outstream)
"
DO: Chronometre the execution of `lambda-body'.
Writes a message indicating the time it took.
RETURN: (cons seconds the result of `lambda-body').
"
(let* ((start (current-time))
(result (funcall lambda-body))
(stop (current-time))
(time (- (emacs-time-to-seconds stop)
(emacs-time-to-seconds start))) )
(printf outstream "Took %f seconds." time)
(cons time result)))
(defun fill-region (from to &optional justify nosqueeze to-eop)
"Fill each of the paragraphs in the region.
A prefix arg means justify as well.
Ordinarily the variable `fill-column' controls the width.
Noninteractively, the third argument JUSTIFY specifies which
kind of justification to do: `full', `left', `right', `center',
or `none' (equivalent to nil). t means handle each paragraph
as specified by its text properties.
The fourth arg NOSQUEEZE non-nil means to leave
whitespace other than line breaks untouched, and fifth arg TO-EOP
non-nil means to keep filling to the end of the paragraph (or next
hard newline, if `use-hard-newlines' is on).
If `sentence-end-double-space' is non-nil, then period followed by one
space does not end a sentence, so don't break a line there."
(interactive (list (region-beginning) (region-end)
(if current-prefix-arg 'full)))
(unless (memq justify '(t nil none full center left right))
(setq justify 'full))
(let (end beg)
(save-restriction
(goto-char (max from to))
(if to-eop
(progn (skip-chars-backward "\n")
(forward-paragraph)))
(setq end (point))
(goto-char (setq beg (min from to)))
(beginning-of-line)
(narrow-to-region (point) end)
(while (not (eobp))
(let ((initial (point))
end)
;; If using hard newlines, break at every one for filling
;; purposes rather than using paragraph breaks.
(if use-hard-newlines
(progn
(while (and (setq end (text-property-any (point) (point-max)
'hard t))
(not (= (character "\n") (char-after end)))
(not (= end (point-max))))
(goto-char (1+ end)))
(setq end (if end (min (point-max) (1+ end)) (point-max)))
(goto-char initial))
(forward-paragraph 1)
(setq end (point))
(forward-paragraph -1))
(if (< (point) beg)
(goto-char beg))
(if (>= (point) initial)
(fill-region-as-paragraph (point) end justify nosqueeze)
(goto-char end)))))))
(defun permutations (list)
"Retourne une liste de toutes les permutations de list."
(mapcan (lambda (item)
(if (= 1 (length list))
(list (list item))
(mapcar (lambda (rest) (cons item rest))
(permutations (remove* item list :count 1)))))
list))
(defun perm-words ()
"Insère après la ligne courrante toutes les permutations des mots de la ligne courrante."
(interactive)
(let ((words (car (read-from-string
(format "(%s)" (buffer-substring-no-properties
(progn (beginning-of-line) (point))
(progn (end-of-line) (point))))))))
(end-of-line)
(insert "\n")
(dolist (line (permutations words))
(dolist (word line)
(insert (format "%s "
(if (and (listp word) (eq 'quote (car word)))
(cadr word) word))))
(insert "\n"))))
(defvar *fortune-file* "/data/cookies/bopcs.cookies")
(when (require 'fortune nil t)
(setf fortune-program "cookie"
fortune-always-compile nil
fortune-dir (dirname *fortune-file*)