filter-edit.el 34 KB
Newer Older
1
;;;;; -*-coding: raw-text;-*-
2
3
;;;;;
;;;;; $Id$
4
;;;;; Copyright (C) 1994, 1996  Lysator Academic Computer Association.
5
6
7
8
9
;;;;;
;;;;; This file is part of the LysKOM server.
;;;;; 
;;;;; LysKOM is free software; you can redistribute it and/or modify it
;;;;; under the terms of the GNU General Public License as published by 
10
;;;;; the Free Software Foundation; either version 2, or (at your option) 
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
;;;;; any later version.
;;;;; 
;;;;; LysKOM 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 LysKOM; see the file COPYING.  If not, write to
;;;;; Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
;;;;; or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
;;;;; MA 02139, USA.
;;;;;
;;;;; Please mail bug reports to bug-lyskom@lysator.liu.se. 
;;;;;
;;;;============================================================
;;;;============================================================
;;;;
;;;; File: filter-edit.el
;;;;
;;;; This file contains the filter editor
;;;;

34
35
36
37
(setq lyskom-clientversion-long 
      (concat lyskom-clientversion-long
	      "$Id$\n"))

38
39
40

(defvar filter-edit-currently-edited-filter-entry-list nil
  "List of filters currently being edited in a filter editor")
41
42
43
44
(defvar filter-edit-change-flag nil)
(defvar filter-edit-filter-list nil)
(defvar filter-edit-list-start nil)
(defvar filter-edit-list-end nil)
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
(defun copy-filter-list (l)
  "Copy the filter list L"
  (cond ((null l) nil)
        (t (cons (copy-filter (car l))
                 (copy-filter-list (cdr l))))))

(defun lyskom-reverse-pairs (l)
  "Reverse the pairs in the assoc list L"
  (mapcar (function
           (lambda (e) (cons (cdr e) (car e)))) l))

;;;========================================
;;; Data types
;;;
;;; filter-entry
;;; Entry in the filter-entry list
;;;
;;; A filter entry is a vector consisting of
;;;
;;; [START END LINES PATTERN]
;;;
;;; START is the starting position (mark) in the buffer
;;; END is the ending position (mark) in the buffer
;;; LINES is a list of starting positions (marks) of the lines
;;; PATTERN is the filter displayed (type filter)
;;;

(defun filter-entry->start (e)
  "Get starting position of entry E."
  (aref e 0))

(defun set-filter-entry->start (e s)
  "Set starting position of entry E to S."
  (aset e 0 s))

(defun filter-entry->end (e)
  "Get end position of entry E."
  (aref e 1))

(defun set-filter-entry->end (e s)
  "Set end position of entry E to S."
  (aset e 1 s))

(defun filter-entry->lines (e)
  "Get line start list of entry E."
  (aref e 2))

(defun set-filter-entry->lines (e p)
  "Set line start list of entry E to P."
  (aset e 2 p))

(defun filter-entry->filter (e)
  "Get filter of entry E."
  (aref e 3))

(defun set-filter-entry->filter (e p)
  "Set filter of entry E to P."
  (aset e 3 p))

(defun make-filter-entry (start end lines pattern)
  "Create an filter entry with START, END, LINES and PATTERN as values."
  (vector start end lines pattern))



;;;============================================================
;;; Verification functions
;;;

(defun lyskom-verify-filter-list (filter)
  "Return non-nil if FILTER is a valid filter list"
  (cond ((null filter) t)
        ((not (listp filter)) nil)
        ((not (filter-p (car filter))) nil)
        (t (and (lyskom-verify-filter-pattern 
                 (filter->pattern (car filter)))
                (lyskom-verify-filter-list (cdr filter))))))

(defun lyskom-verify-filter-pattern (filter)
  "Return non-nil if FILTER is a valid filter pattern"
  (cond ((null filter) nil)
        ((and (= (length filter) 1)
              (symbolp (car filter))) t)
        ((not (listp (car filter))) nil)
        ((eq 'not (car (car filter)))
         (lyskom-verify-filter-pattern
          (cons (cdr (car filter)) (cdr filter))))
        ((or (eq 'author-re (car (car filter)))
             (eq 'recipient-re (car (car filter)))
             (eq 'subject-re (car (car filter)))
             (eq 'text-re (car (car filter))))
         (and
          (lyskom-filter-verify-regexp (cdr (car filter)))
          (lyskom-verify-filter-pattern (cdr filter))))
         
        ((or (eq 'author (car (car filter)))
             (eq 'subject (car (car filter)))
             (eq 'recipient (car (car filter)))
             (eq 'text (car (car filter))))
         (and
          (stringp (cdr (car filter)))
          (lyskom-verify-filter-pattern (cdr filter))))
        ((or (eq 'author-no (car (car filter)))
             (eq 'recipient-no (car (car filter))))
         (and
          (integerp (cdr (car filter)))
          (lyskom-verify-filter-pattern (cdr filter))))
        (t nil)))

(defun lyskom-filter-verify-regexp (re)
  "Return t if RE is a valid regexp."
157
  (condition-case nil
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
      (progn
        (string-match re "teststring")
        t)
    (error nil)))

;;;========================================
;;; Locator functions
;;;

(defun lyskom-filter-edit-lineno (where entry)
  "Return the line number at WHERE in the entry ENTRY.
-1 means the header. 0 is the first line. Any number higher than the number of lines means outside the pattern."
  (let* ((lines (append (filter-entry->lines entry)
                        (cons (filter-entry->end entry) nil)))
         (line -1)
         (current-line (length lines)))
    (if (>= where (filter-entry->start entry))
        (while lines
          (if (< where (car lines))
              (progn
                (setq current-line line)
                (setq lines nil))
            (progn
              (setq lines (cdr lines))
              (setq line (1+ line))))))
    current-line))


(defun lyskom-filter-edit-locate (where)
  "Locate the entry at character position WHERE.
nil means WHERE is not in any entry."
  (let ((entry-no (lyskom-filter-edit-locate-no where)))
    (if (= -1 entry-no)
        nil
192
      (elt filter-edit-currently-edited-filter-entry-list entry-no))))
193
194
195


(defun lyskom-filter-edit-locate-no (where)
196
197
198
  "Find the filter-edit-currently-edited-filter-entry-list index of the entry
covering the character position WHERE. -1 means WHERE is not covered y any
entry."
199
200
  (let ((i 0)
        (entry-no -1)
201
        (l filter-edit-currently-edited-filter-entry-list))
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
    (while l
      (if (and (>= where (filter-entry->start (car l)))
               (<= where (filter-entry->end (car l))))
          (progn (setq entry-no i)
                 (setq l nil))
        (progn (setq i (1+ i))
               (setq l (cdr l)))))
    entry-no))


;;;========================================
;;; Formatting functions
;;;

(defun lyskom-format-filter-list (filters)
  "Format the filter list FILTERS and insert the result into the
218
219
current buffer. The buffer variable 
FILTER-EDIT-CURRENTLY-EDITED-FILTER-ENTRY-LIST is also updated."
220
  (let ((inhibit-read-only t))
221
    (setq filter-edit-currently-edited-filter-entry-list nil)
222
    (lyskom-format-filter-list-2 filters)
223
224
    (setq filter-edit-currently-edited-filter-entry-list
	  (nreverse filter-edit-currently-edited-filter-entry-list))))
225
226
227
228
229
230


(defun lyskom-format-filter-list-2 (filters)
  "Format the filter list FILTERS and insert the result into the
current buffer."
  (cond ((null filters) nil)
231
        (t (setq filter-edit-currently-edited-filter-entry-list
232
                 (cons (lyskom-format-filter-pattern (car filters))
233
                       filter-edit-currently-edited-filter-entry-list))
234
235
236
237
238
239
240
           (lyskom-format-filter-list-2 (cdr filters)))))

  

(defun lyskom-format-filter-pattern (pat)
  "Format and insert the filter pattern PAT into the current buffer.
Returns an filter-entry structure representing the entry."
David Byers's avatar
David Byers committed
241
242
  (let ((inhibit-read-only t)
        start end lines)
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
   
    ;;
    ;; Insert text representation
    ;;

    (setq start (point-marker))
    (lyskom-filter-format-entry-header pat t)
    (setq lines (lyskom-format-filter-pattern-2 (filter->pattern pat) nil))
    (setq end (point-marker))
    (insert "\n")
    (make-filter-entry start end lines pat)))


(defun lyskom-format-filter-pattern-2 (pat lines &optional neg)
  "Format and insert the body of the filter pattern PAT into the 
current buffer. Accumulate line starts in LINES.
If NEG is non-nil, the first line will be negated."
  (cond ((or (null pat)) (nreverse lines))
        ((eq (car (car pat)) 'not)
         (lyskom-format-filter-pattern-2 (cons (cdr (car pat))
                                               (cdr pat))
                                         lines
                                         t))
        (t
         (setq lines (cons (point-marker) lines))
         (lyskom-format-filter-pattern-insert pat neg)
         (lyskom-format-filter-pattern-2 (cdr pat) lines))))



(defun lyskom-format-filter-pattern-insert (pat neg)
  "Format and insert the first pattern in PAT.
If NEG is non-nil, format the negation."
276
277
  (let (tmp)
    (insert
David Byers's avatar
X    
David Byers committed
278
     (format "    %s %s %S"
Niels Möller's avatar
Niels Möller committed
279
	     (cdr (assoc (car (car pat)) lyskom-filter-what))
280
281
282
283
284
285
286
287
288
289
290
291
	     (if neg
		 " != "
	       " = ")
	     (cdr (car pat))))
    (cond ((or (eq (car (car pat)) 'recipient-no)
	       (eq (car (car pat)) 'author-no))
	   (save-excursion
	     (if (boundp 'lyskom-buffer)
		 (set-buffer lyskom-buffer))
	     (setq tmp (blocking-do 'get-conf-stat (cdr (car pat)))))
	   (insert (format "   <%s>" (conf-stat->name tmp)))))
    (insert "\n")))
292
293
294
295
296
297
	


(defun lyskom-filter-format-entry-header (filter &optional newline action)
  "Format the header of a filter entry. FILTER is the filter to format. 
If NEWLINE is non-nil, insert a newline after the header."
298
299
300
301
302
303
  (let (permanent)
    (setq action (filter->attribute filter 'action)
	  permanent (null (filter->attribute filter 'expire)))
    (insert 
     (format "--- %s %s"
	     (cdr 
Niels Möller's avatar
Niels Möller committed
304
	      (assq action lyskom-filter-actions		    ))
305
306
307
	     (lyskom-get-string
	      (if permanent 'permanent 'temporary))))
    (if newline (insert "\n"))))
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323


;;;========================================
;;; User commands
;;;

(defun lyskom-filter-edit-next-pattern (arg)
  "Move to the ARGth next pattern line in the current buffer."
  (interactive "p")
  (if (null arg) (setq arg 1))
  (if (< arg 0) 
      (lyskom-filter-edit-prev-entry (- arg))
    (while (/= arg 0)
      (let ((entry-no (lyskom-filter-edit-locate-no (point)))
            tmp)
        (if (= -1 entry-no)
324
325
326
327
            (if (car filter-edit-currently-edited-filter-entry-list)
                (goto-char 
		 (filter-entry->start 
		  (car filter-edit-currently-edited-filter-entry-list)))
328
              (lyskom-error (lyskom-get-string 'filter-edit-empty-list)))
329
330
331
          (let* ((entry 
		  (elt filter-edit-currently-edited-filter-entry-list
		       entry-no))
332
333
334
335
336
337
338
                 (lineno (lyskom-filter-edit-lineno (point) entry)))
            (cond 
             ((= lineno -1)
              (if (filter-entry->lines entry)
                  (setq tmp (car (filter-entry->lines entry)))
                (progn
                  (setq tmp 
339
340
                        (elt filter-edit-currently-edited-filter-entry-list
			     (1+ entry-no)))
341
342
343
344
                  (if tmp 
                      (setq tmp (filter-entry->start tmp))
                    (lyskom-error (lyskom-get-string 'filter-edit-end-of-list))))))

345
              ((= lineno (1- (length (filter-entry->lines entry))))
346
347
               (setq tmp (elt filter-edit-currently-edited-filter-entry-list
			      (1+ entry-no)))
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
               (if tmp 
                   (setq tmp (filter-entry->start tmp))
                 (lyskom-error (lyskom-get-string 'filter-edit-end-of-list))))
              (t (setq tmp (elt (filter-entry->lines entry) (1+ lineno)))))
        (goto-char tmp)
        (setq arg (1- arg))))))))


(defun lyskom-filter-edit-prev-pattern (arg)
  "Move to the ARGth previous pattern line in the current buffer."
  (interactive "p")
  (if (null arg) (setq arg 1))
  (if (< arg 0) 
      (lyskom-filter-edit-next-entry (- arg))
    (while (/= arg 0)
      (let ((entry-no (lyskom-filter-edit-locate-no (point)))
            tmp)
        (if (= -1 entry-no)
366
367
368
369
            (if (car filter-edit-currently-edited-filter-entry-list)
                (goto-char
		 (filter-entry->start
		  (car filter-edit-currently-edited-filter-entry-list)))
370
              (lyskom-error (lyskom-get-string 'filter-edit-empty-list)))
371
372
373
          (let* ((entry 
		  (elt filter-edit-currently-edited-filter-entry-list
		       entry-no))
374
375
376
377
378
379
                 (lineno (lyskom-filter-edit-lineno (point) entry)))
            (cond 
             ((= lineno -1)
              (if (= 0 entry-no)
                  (lyskom-error (lyskom-get-string 'filter-edit-start-of-list))
                (progn
380
381
382
                  (setq entry 
			(elt filter-edit-currently-edited-filter-entry-list
			     (1- entry-no)))
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
                  (setq tmp (car 
                             (nthcdr (1- (length (filter-entry->lines entry)))
                                     (filter-entry->lines entry))))
                  (if (null tmp)
                      (setq tmp (filter-entry->start entry))))))

              ((= lineno 0)
               (setq tmp (filter-entry->start entry)))

              (t (setq tmp (elt (filter-entry->lines entry) (1- lineno)))))
        (goto-char tmp)
        (setq arg (1- arg))))))))



(defun lyskom-filter-edit-next-entry (arg)
  "Move to the ARGth next entry in the current buffer."
  (interactive "p")
  (if (null arg) (setq arg 1))
  (if (< arg 0) 
      (lyskom-filter-edit-prev-entry (- arg))
    (while (/= 0 arg)
      (let ((entry-no (lyskom-filter-edit-locate-no (point))))
        (if (= -1 entry-no)
407
408
409
410
            (if (car filter-edit-currently-edited-filter-entry-list)
                (goto-char 
		 (filter-entry->start
		  (car filter-edit-currently-edited-filter-entry-list)))
411
              (lyskom-error (lyskom-get-string 'filter-edit-empty-list)))
412
          (let ((tmp (elt filter-edit-currently-edited-filter-entry-list (1+ entry-no))))
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
            (if tmp
                (progn
                  (goto-char (filter-entry->start tmp))
                  (setq arg (1- arg)))
              (lyskom-error (lyskom-get-string 'filter-edit-end-of-list)))))))))


(defun lyskom-filter-edit-prev-entry (arg &optional noerror)
  "Move to the ARGth previous entry in the current buffer."
  (interactive "p")
  (not
   (catch 'fail
     (if (null arg) (setq arg 1))
     (if (< arg 0) 
         (lyskom-filter-edit-next-entry (- arg))
       (while (/= 0 arg)
         (let ((entry-no (lyskom-filter-edit-locate-no (point))))
           (if (= -1 entry-no)
431
432
               (if (car filter-edit-currently-edited-filter-entry-list)
                   (goto-char (filter-entry->start (car filter-edit-currently-edited-filter-entry-list)))
433
434
435
436
437
                 (if noerror (throw 'fail t) 
                   (lyskom-error (lyskom-get-string 'filter-edit-empty-list)))))
           (if (= 0 entry-no)
               (if noerror (throw 'fail t) 
                 (lyskom-error (lyskom-get-string 'filter-edit-start-of-list)))
438
             (let ((tmp (elt filter-edit-currently-edited-filter-entry-list (1- entry-no))))
439
440
441
442
443
444
445
               (setq arg (1- arg))
               (goto-char (filter-entry->start tmp))))))))))


(defun lyskom-filter-edit-beginning-of-list ()
  "Move to the first entry in the list"
  (interactive)
446
  (if (null filter-edit-currently-edited-filter-entry-list)
447
448
449
      (lyskom-error (lyskom-get-string 'filter-edit-empty-list))
    (progn
      (push-mark)
450
      (goto-char (filter-entry->start (car filter-edit-currently-edited-filter-entry-list))))))
451
452
453
454
455


(defun lyskom-filter-edit-end-of-list ()
  "Move to the last entry in the list"
  (interactive)
456
  (if (null filter-edit-currently-edited-filter-entry-list)
457
      (lyskom-error (lyskom-get-string 'filter-edit-empty-list))
458
    (let ((entry (elt filter-edit-currently-edited-filter-entry-list (1- (length filter-edit-currently-edited-filter-entry-list)))))
459
460
461
462
463
464
465
466
467
468
      (push-mark)
      (goto-char (or (elt (filter-entry->lines entry)
                          (1- (length (filter-entry->lines entry))))
                     (filter-entry->start entry))))))



(defun lyskom-filter-edit-insert-entry ()
  "Add an entry to the end of the list"
  (interactive)
469
  (let ((inhibit-read-only t)
470
471
        (completion-ignore-case t)
        (rev-actions 
Niels Möller's avatar
Niels Möller committed
472
         (lyskom-reverse-pairs lyskom-filter-actions))
473
        action permanent filter start end entry)
474
    (if filter-edit-currently-edited-filter-entry-list
475
        (progn
476
477
          (goto-char (filter-entry->end (elt filter-edit-currently-edited-filter-entry-list
                                             (1- (length filter-edit-currently-edited-filter-entry-list)))))
478
          (insert "\n"))
479
      (goto-char filter-edit-list-end))
480
481
482
483
484
485
      
      (setq action (completing-read (lyskom-get-string 'filter-edit-filter-how)
                                    rev-actions
                                    nil
                                    t))
      (setq permanent
David Byers's avatar
David Byers committed
486
            (lyskom-j-or-n-p (lyskom-get-string 'filter-permanent) t))
487
488
489
490
491
492
493
494
495
      (setq filter (make-filter nil
                                (list (cons 'action 
                                            (cdr (assoc action rev-actions)))
                                      (cons 'expire (not permanent)))))
      (setq start (point-marker))
      (lyskom-filter-format-entry-header filter t)
      (setq end (point-marker))
      (setq entry
            (make-filter-entry start end nil filter))
496
497
      (if filter-edit-currently-edited-filter-entry-list
          (setcdr (nthcdr (1- (length filter-edit-currently-edited-filter-entry-list)) filter-edit-currently-edited-filter-entry-list)
498
                  (cons entry nil))
499
        (setq filter-edit-currently-edited-filter-entry-list (cons entry nil)))
500
      (setq filter-edit-change-flag t)
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
      (goto-char start)))


(defun lyskom-filter-edit-insert-pattern ()
  "Add a pattern line to the current list entry."
  (interactive)
  (let ((entry-no (lyskom-filter-edit-locate-no (point))))
    (if (= -1 entry-no) 
        (lyskom-error (lyskom-get-string 'filter-edit-outside-entry))))
  (let ((what nil)
        (pred nil)
        (arg nil)
        (argstring nil)
        (lineno nil)
        (entry nil)
        (filter nil)
        (pat nil)
518
        (inhibit-read-only t)
519
        (completion-ignore-case t)
Niels Möller's avatar
Niels Möller committed
520
        (rev-what (lyskom-reverse-pairs lyskom-filter-what)))
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
    (setq what (completing-read
                (lyskom-get-string 'filter-edit-filter-what)
                rev-what
                nil t))
    (setq pred 
          (completing-read (lyskom-format 'filter-edit-insert-pred what)
                           lyskom-filter-predicate-list
                           nil t))
    (setq argstring (read-from-minibuffer
                     (lyskom-format 'filter-edit-insert-arg what pred)))
    (setq what (cdr (assoc what rev-what)))

    (if (not
         (cond ((or (eq what 'author)
                    (eq what 'subject)
                    (eq what 'text)
                    (eq what 'recipient)) (setq arg argstring))
               ((or (eq what 'author-no)
                    (eq what 'recipient-no)) (setq arg
                                                   (string-to-int argstring)))
               ((or (eq what 'author-re) 
                    (eq what 'subject-re)
                    (eq what 'text-re)
                    (eq what 'recipient-re)) 
                (setq arg argstring)
                (lyskom-filter-verify-regexp arg))))
        (lyskom-error (lyskom-get-string 'filter-edit-bad-argument)
                       argstring))

    ;;
    ;; Build pattern
    ;;

    (setq pat 
          (cons what arg))
    (if (cdr (assoc pred lyskom-filter-predicate-list))
        (setq pat (cons 'not pat)))

    ;;
    ;; Locate current entry
    ;; Locate current line
    ;;
    
    (setq entry (lyskom-filter-edit-locate (point)))
    (setq lineno (lyskom-filter-edit-lineno (point) entry))
    (setq filter (filter-entry->filter entry))

    ;;
    ;; Splice the new pattern into the old pattern at the
    ;; proper position. If lineno is -1 (the cursor is not in
    ;; the pattern area, append the pattern.
    ;;

    (if (= -1 lineno)
        (setq lineno (length (filter->pattern filter))))
    
    (cond ((= lineno 0) 
           (set-filter->pattern
            filter
            (cons pat (filter->pattern filter))))
          (t (if (filter->pattern filter)
                 (setcdr (nthcdr (1- lineno)
                             (filter->pattern filter))
                     (cons pat (nthcdr lineno
                                       (filter->pattern filter))))
               (set-filter->pattern filter pat))))
    
    ;;
    ;; Update display
    ;;
    
    (if (= lineno (length (filter-entry->lines entry)))
        (goto-char (filter-entry->end entry))
      (goto-char (elt (filter-entry->lines entry) lineno)))
    
    ;;
    ;; OK, right now, POINT is on top os a marker that represents the
    ;; NEXT line. This marker must be puched forward to avoid duplicating
    ;; it.
    ;;
    
    (forward-line -1)
    (end-of-line)
    (insert "\n")
    (lyskom-format-filter-pattern-insert
     (cons
      (if (eq (car pat) 'not)
          (cdr pat) 
        pat) nil)
     (eq (car pat) 'not))
    (forward-char -1) 
    (delete-char 1)
    (beginning-of-line)
    
    ;;
    ;; Update lines list in the entry
    ;;

    (cond ((= lineno 0)
           (set-filter-entry->lines
            entry
            (cons (point-marker) (filter-entry->lines entry))))
          (t (setcdr (nthcdr (1- lineno)
                                 (filter-entry->lines entry))
                     (cons (point-marker)
                           (nthcdr lineno 
                                   (filter-entry->lines entry))))))
628
    (setq filter-edit-change-flag t)))
629
630
631
632
633
634
635



(defun lyskom-filter-edit-delete-pattern (arg)
  "Delete ARG pattern lines, starting with the one at point.
Only lines in the current entry will be deleted."
  (interactive "p")
636
  (let* ((inhibit-read-only t)
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
         (entry (lyskom-filter-edit-locate (point)))
         (filter (filter-entry->filter entry)))
    (if (null entry)
        (lyskom-error (lyskom-get-string 'filter-edit-outside-list))
      (progn
        (while (/= 0 arg)
          (let ((lineno (lyskom-filter-edit-lineno (point) entry)))
            (if (null arg) (setq arg 1))
            (if (and (> lineno -1)
                     (< lineno (length (filter-entry->lines entry))))
                (progn 
                  (setq arg (1- arg))
                  (setq lineno (lyskom-filter-edit-lineno (point) entry))
                  (delete-region
                   (save-excursion (beginning-of-line) (point))
                   (1+ (save-excursion (end-of-line) (point))))
                  (if (= lineno (1- (length (filter-entry->lines entry))))
                      (forward-line -1))
                  (if (= lineno 0)
                      (progn
                        (set-filter-entry->lines 
                         entry 
                         (cdr (filter-entry->lines entry)))
                        (set-filter->pattern 
                         filter
                         (cdr (filter->pattern filter)))
663
                        (setq filter-edit-change-flag t))
664
665
666
667
668
669
670
671
672
673
674
                    (progn
                      (setcdr (nthcdr (1- lineno) 
                                      (filter-entry->lines entry))
                              (nthcdr (1+ lineno)
                                      (filter-entry->lines entry)))
                      (setcdr (nthcdr (1- lineno)
                                      (filter->pattern
                                       (filter-entry->filter entry)))
                              (nthcdr (1+ lineno)
                                      (filter->pattern
                                       (filter-entry->filter entry))))
675
                      (setq filter-edit-change-flag t))))
676
677
678
679
680
681
682
683
              (lyskom-error (lyskom-get-string 'filter-edit-end-of-pattern)))))))))


(defun lyskom-filter-edit-delete-entry (arg &optional which noerror)
  "Delete ARG entries, starting with the one covering point.
If optional WHICH is non-nil, start with entry number WHICH.
If NOERROR is non-nil, return nil instead of signaling an error."
  (interactive "p")
684
  (let ((inhibit-read-only t))
685
686
687
688
689
690
691
692
693
694
695
  (not
   (catch 'fail
     (if (null arg) (setq arg 1))
     (while (/= 0 arg)
       (let* ((entry-no (or which
                            (lyskom-filter-edit-locate-no (point))))
              (entry nil))
         (if (= -1 entry-no) 
             (if noerror
                 (throw 'fail t)
               (lyskom-error (lyskom-get-string 'filter-edit-end-of-list))))
696
697
         (setq entry (elt filter-edit-currently-edited-filter-entry-list
			  entry-no))
698
699
700
         (delete-region (filter-entry->start entry)
                        (1+ (filter-entry->end entry)))
         (cond ((= 0 entry-no)
701
702
                (setq filter-edit-currently-edited-filter-entry-list
		      (cdr filter-edit-currently-edited-filter-entry-list))
703
                (setq filter-edit-change-flag t))
704
705
706
707
708
               (t (setcdr
		   (nthcdr (1- entry-no)
			   filter-edit-currently-edited-filter-entry-list)
		   (nthcdr (1+ entry-no)
			   filter-edit-currently-edited-filter-entry-list))
709
                  (setq filter-edit-change-flag t)))
710
711
         (if (= entry-no
		(length filter-edit-currently-edited-filter-entry-list))
712
713
714
715
716
717
718
719
720
             (progn (if (lyskom-filter-edit-prev-entry 1 t)
                        (setq arg 0)
                      (throw 'fail t)))
           (setq arg (1- arg)))))))))
  
(defun lyskom-filter-edit-quit ()
  "Quit filter edit mode and ask to save changes (if any)"
  (interactive)
  (let ((save nil))
721
    (if filter-edit-change-flag
722
        (setq save (lyskom-j-or-n-p 
David Byers's avatar
David Byers committed
723
                    (lyskom-get-string 'filter-edit-save-p) t)))
724
725
726
727
728
729
730
    (if save
        (lyskom-filter-edit-save))
    (set-window-configuration lyskom-edit-return-to-configuration)))

(defun lyskom-filter-edit-expunge ()
  "Delete all entries that are completely empty"
  (interactive)
731
  (let ((e filter-edit-currently-edited-filter-entry-list)
732
733
734
735
736
737
738
739
740
        (index 0))
    (while e
      (if (null (filter-entry->lines (car e)))
          (lyskom-filter-edit-delete-entry 1 index t)
        (setq index (1+ index)))
      (setq e (cdr e)))))

(defun lyskom-filter-empty-patterns-p ()
  "Return non-nil if the entry list contains empty entries."
741
  (let ((e filter-edit-currently-edited-filter-entry-list)
742
743
744
745
746
747
748
749
750
751
752
753
754
755
        (result nil))
    (while e
      (if (null (filter-entry->lines (car e)))
          (progn (setq e nil) (setq result t))
        (setq e (cdr e))))
    result))


(defun lyskom-filter-edit-save ()
  "Save changes in filter edit mode"
  (interactive)
  (if (and 
       (lyskom-filter-empty-patterns-p)
       (lyskom-j-or-n-p
David Byers's avatar
David Byers committed
756
        (lyskom-get-string 'filter-edit-remove-empty) t))
757
758
      (lyskom-filter-edit-expunge))

759
  (let ((e filter-edit-currently-edited-filter-entry-list)
760
761
762
        (xpermanent-list nil)
        (xtemporary-list nil))
    (while e
David Byers's avatar
David Byers committed
763
764
765
766
      (set-filter->function 
       (filter-entry->filter (car e))
       (lyskom-create-compile-filter-function
	(filter->pattern (filter-entry->filter (car e)))))
767
768
769
770
771
772
773
774
775
776
777
      (set (if (filter->attribute (filter-entry->filter (car e))
                                  'expire)
               'xtemporary-list
             'xpermanent-list)
           (cons (filter-entry->filter (car e))
                 (if (filter->attribute (filter-entry->filter (car e))
                                        'expire)
                     xtemporary-list
                   xpermanent-list)))
      (setq e (cdr e)))
    (save-excursion
778
      (set-buffer lyskom-buffer)
779
780
781
      (setq lyskom-filter-list 
            (append
             (setq kom-permanent-filter-list (nreverse xpermanent-list))
782
783
784
785
786
787
788
             (setq kom-session-filter-list (nreverse xtemporary-list))))
      (setq filter-edit-change-flag nil)
      (lyskom-message "%s" (lyskom-get-string 'filter-edit-saving))
      (lyskom-save-options lyskom-buffer
                           (lyskom-get-string 'filter-edit-saving)
                           (lyskom-get-string 'filter-edit-saving-done)
                           (lyskom-get-string 'filter-edit-saving-error)))))
789
790
791
792
793


(defun lyskom-filter-edit-revert ()
  "Discard changes and restart editing"
  (interactive)
794
795
  (if (or (not filter-edit-change-flag)
          (and filter-edit-change-flag
David Byers's avatar
David Byers committed
796
               (lyskom-j-or-n-p (lyskom-get-string 'filter-edit-restart-p) t)))
797
      (let ((inhibit-read-only t))
798
        (setq filter-edit-currently-edited-filter-entry-list nil)
799
800
        (delete-region filter-edit-list-start filter-edit-list-end)
        (goto-char filter-edit-list-start)
801
802
        (insert "\n")
        (lyskom-format-filter-list (copy-filter-list lyskom-filter-list))
803
        (setq filter-edit-change-flag nil)
804
805
806
807
808
809
810
        (lyskom-filter-edit-beginning-of-list))))


(defun lyskom-filter-edit-toggle-permanent ()
  "Toggle the permanent flag of the current entry"
  (interactive)
  (let ((entry-no (lyskom-filter-edit-locate-no (point)))
811
        (inhibit-read-only t))
812
813
    (if (= -1 entry-no)
        (lyskom-error (lyskom-get-string 'filter-edit-outside-entry)))
814
815
    (let ((entry (elt filter-edit-currently-edited-filter-entry-list
		      entry-no)))
816
817
818
819
820
      (set-filter->attribute (filter-entry->filter entry)
                             'expire
                             (not (filter->attribute 
                                   (filter-entry->filter entry)
                                   'expire)))
821
      (setq filter-edit-change-flag t)
822
823
824
825
826
827
828
829
830
831
      (save-excursion
        (goto-char (filter-entry->start entry))
        (delete-region (point) (save-excursion (end-of-line) (point)))
        (lyskom-filter-format-entry-header (filter-entry->filter entry))))))


(defun lyskom-filter-edit-toggle-action ()
  "Toggle the filter action of the current entry."
  (interactive)
  (let ((entry-no (lyskom-filter-edit-locate-no (point)))
832
        (inhibit-read-only t))
833
834
    (if (= -1 entry-no)
        (lyskom-error (lyskom-get-string 'filter-edit-outside-entry)))
835
836
    (let* ((entry (elt filter-edit-currently-edited-filter-entry-list
		       entry-no))
Niels Möller's avatar
Niels Möller committed
837
           (action-list lyskom-filter-actions)
838
839
840
841
842
843
844
845
           (action (car
                    (cdr (memq 
                          (assq
                            (filter->attribute (filter-entry->filter entry)
                                               'action)
                           action-list)
                          action-list))))
           (new-action (or action (car action-list))))
846
      (setq filter-edit-change-flag t)
847
848
849
850
851
852
853
854
855
856
857
858
      (set-filter->attribute (filter-entry->filter entry)
                             'action
                             (car new-action))
      (save-excursion
        (goto-char (filter-entry->start entry))
        (delete-region (point) (save-excursion (end-of-line) (point)))
        (lyskom-filter-format-entry-header (filter-entry->filter entry))))))


(defun lyskom-filter-edit-brief-help ()
  "Display a help message in the minibuffer."
  (interactive)
859
  (lyskom-message "%s" (lyskom-get-string 'filter-edit-help)))
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


(defun lyskom-filter-edit-mode ()
  "\\<lyskom-filter-edit-map>Mode for editing LysKOM filters.
Entry to this mode runs lyskom-filter-edit-hook

Commands:
\\[lyskom-filter-edit-prev-pattern] Moves up in the pattern list.
\\[lyskom-filter-edit-next-pattern] Moves down in the pattern list.
\\[lyskom-filter-edit-prev-entry]   Moves up one filter in the list.
\\[lyskom-filter-edit-next-entry]   Moves down one filter in the list.
\\[lyskom-filter-edit-beginning-of-list]    Moves to the beginning of the list.
\\[lyskom-filter-edit-end-of-list]    Moves to the end of the list.
\\[lyskom-filter-edit-delete-pattern]   Deletes the pattern line at point.
\\[lyskom-filter-edit-delete-entry] Deletes the filter containing point.
\\[lyskom-filter-edit-insert-pattern]   Inserts a pattern line at point.
\\[lyskom-filter-edit-insert-entry] Inserts a new filter into the list.
\\[lyskom-filter-edit-expunge]  Deletes all empty filters from the list.
\\[lyskom-filter-edit-revert]   Discards all changes to the list.
\\[lyskom-filter-edit-toggle-action]    Toggles the filter action of the filter containing point.
\\[lyskom-filter-edit-toggle-permanent] Toggles between permanent and temporary.
\\[lyskom-filter-edit-save] Saves the filters.
\\[lyskom-filter-edit-quit] Quit and return to LysKOM.

All key bindings:
\\{lyskom-filter-edit-map}
"
  (interactive)
  (kill-all-local-variables)
889
  (make-local-variable 'filter-edit-currently-edited-filter-entry-list)
890
891
892
893
  (make-local-variable 'filter-edit-change-flag)
  (make-local-variable 'filter-edit-filter-list)
  (make-local-variable 'filter-edit-list-start)
  (make-local-variable 'filter-edit-list-end)
894
  (make-local-variable 'lyskom-buffer)
895
896
  (make-local-variable 'lyskom-edit-return-to-configuration)
  (setq buffer-read-only t)
897
  (setq filter-edit-change-flag nil)
898
  (buffer-disable-undo (current-buffer))
David Byers's avatar
X    
David Byers committed
899
  (lyskom-use-local-map lyskom-filter-edit-map)
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
  (setq mode-name "LysKOM Filter Edit")
  (setq major-mode 'lyskom-filter-edit-mode)
  (setq local-abbrev-table 'lyskom-filter-edit-abbrev-table)
  (run-hooks 'lyskom-filter-edit-hook))


;;;========================================
;;; LysKOM command to start editing
;;;

(defun kom-filter-edit ()
  (interactive)
  (let ((buf (current-buffer))
        (filters lyskom-filter-list)
        (server-name lyskom-server-name)
        (curwin (current-window-configuration)))
David Byers's avatar
X    
David Byers committed
916
917
918
919
920
921
922
    (lyskom-display-buffer
     (lyskom-get-buffer-create 'edit-filters
                               (concat (buffer-name buf)
                                       "-"
                                       (lyskom-get-string 
                                        'filter-edit-buffer-name))
                               t))
923
    (let ((inhibit-read-only t))
David Byers's avatar
X    
David Byers committed
924
      (erase-buffer))
925
    (lyskom-filter-edit-mode)
926
    (setq lyskom-buffer buf)
927
    (let ((inhibit-read-only t))
928
      (setq filter-edit-filter-list filters)
929
930
      (insert (format (lyskom-get-string 'filter-edit-header)
                      server-name))
931
      (setq filter-edit-list-start (point-marker))
932
      (insert "\n")
933
934
      (lyskom-format-filter-list (copy-filter-list filter-edit-filter-list))
      (setq filter-edit-list-end (point-max-marker)))
935
936
937
938
    (setq lyskom-edit-return-to-configuration curwin)
    (lyskom-filter-edit-beginning-of-list)))