Source

gnats / gnats-admin.el

Full commit
  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
;; gnats administration code
;; display the pr's in a buffer, 
;; dired-style commands to edit & view them.

;; this version is known to work in XEmacs.

;; author: Roger Hayes, roger.hayes@sun.com

;; copyright: You are welcome to use this software as you see fit.
;; Neither the author nor his employer make any representation about
;; the suitability of this software for any purpose whatsoever.

(defconst gnats-admin-copyright 
"Copyright (c) 1996 Roger Hayes.

Permission to use, copy, modify and distribute this software and
documentation for any purpose and without fee is hereby granted in
perpetuity, provided that this COPYRIGHT AND LICENSE NOTICE appears in
its entirety in all copies of the software and supporting
documentation.

The names of the author or Sun Microsystems, Inc. shall not be used in
advertising or publicity pertaining to distribution of the software
and documentation without specific, written prior permission.

ANY USE OF THE SOFTWARE AND DOCUMENTATION SHALL BE GOVERNED BY
CALIFORNIA LAW.  THE AUTHOR AND SUN MICROSYSTEMS, INC. MAKE NO
REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE OR
DOCUMENTATION FOR ANY PURPOSE.  THEY ARE PROVIDED *AS IS* WITHOUT
EXPRESS OR IMPLIED WARRANTY OF ANY KIND.  THE AUTHOR AND SUN
MICROSYSTEMS, INC. SEVERALLY AND INDIVIDUALLY DISCLAIM ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE AND DOCUMENTATION, INCLUDING THE
WARRANTIES OF MERCHANTABILITY, DESIGN, FITNESS FOR A PARTICULAR
PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL
THE AUTHOR OR SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY SPECIAL,
INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN
ACTION ARISING OUT OF CONTRACT, NEGLIGENCE, PRODUCT LIABILITY, OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE OR DOCUMENTATION."

"Copyright and disclaimer notice")

;; magic words are highlighted using font-lock

;; data structures: a pr is represented as an alist
;; the list of known pr's is represented as a vector
;; pr references are often done by number
;; (pr N) takes either a number, and returns the pr assoc list,
;; or the pr assoc list, returning the same.
;; regions are tagged with the number of the pr; the indirection lets
;; updates to pr's happen without disturbing the region.

(defvar pr-list nil "Vector of parsed problem reports.")

(require 'cl)

;;; (require 'match-string)
;;; inline match-string
(if (not (fboundp 'match-string))
    (defun match-string (n &optional target)
      "Return the text of the NTH match in optional TARGET."
      (let* ((m-data (match-data))
	     (idx  	(* 2 n))
	     (m-beg (elt m-data idx))
	     (m-end (elt m-data (1+ idx))))
	(cond 
	 ((markerp m-beg)
	  (buffer-substring m-beg m-end))
	 ((integerp m-beg)
	  (substring target m-beg m-end))
	 (t
	  (error "Bad argument N to match-string"))))))

;;; (require 'edit-expr)
;; edit-expr -- pop up a buffer to edit an expression
(if (not (fboundp 'edit-expr))
    (defun edit-expr (e &optional explain)
      "Pop up a buffer to edit the EXPRESSION; return the edited value.
Buffer gets optional EXPLANATION."
      (with-output-to-temp-buffer "*expr-buffer*"
	(let ((buffer standard-output)
	      (val nil)
	      emark)
	  
	  (save-excursion
	    (pop-to-buffer (buffer-name buffer))
	    (emacs-lisp-mode)
	    (delete-region (point-min) (point-max))
	    (insert (or explain ";; Edit this value"))
	    (insert "\n")
	    (setq emark (point-marker))
	    (prin1 e) 
	    (goto-char emark)
	    (message "recursive edit -- M-C-c when done")
	    (recursive-edit)
	    (goto-char emark)
	    (setq val (read buffer)))
	  val))))
    
;; (require 'regret)    ;; creates regression tests in my environment [rh]

(provide 'gnats-admin)

;; add stuff for font-lock; harmless if you don't use font-lock.
;; beware -- font-lock uses the first match; hence longer words must
;; precede shorter words, if they both match.
(defconst gnats-admin::font-lock-keywords
  '(;; severity
    ("non-critical" . non-critical)
    ("critical" . critical)
    ("serious" .  serious)

    ;; priority
    ("high" . high)
    ("medium" . medium)
    ("low" . low)

    ;; state
    ("open" . open)
    ("analyzed" . analyzed)
    ("suspended" . suspended)
    ("feedback" . feedback)
    ("closed\\*" . closed*)
    ("closed\\?" . closed?)
    ("closed" . closed)

    ;; class
    ("sw-bug" . sw-bug) 
    ("doc-bug" . doc-bug)
    ("support" . support)
    ("change-request" . change-request) 
    ("mistaken" . mistaken) 
    ("duplicate" . duplicate)))

(defvar gnats-admin::popup-menu
  '("Gnats-Admin"
    ["Edit"	'gnats-admin:pr-edit t]
    ["View"	'gnats-admin:pr-view t]
    )
  "Local popup menu.")

(defvar gnats-admin-query-hook nil
  "Hooklist for post-query processing in gnats-admin mode.")
(defvar gnats-admin:query-list nil
  "Results of one query -- may be altered by query hook to change results of query.")
(defvar gnats-admin-refresh-hook nil
  "Hooklist for post-refresh processing in gnats-admin mode.
Run after a refresh -- gnats-admin::dirty-list may contain list of re-read pr numbers")

(defvar gnats-admin::selector nil
  "Function of one argument that governs if a PR should be diplayed.")

(add-hook 'gnats-admin-refresh-hook
	  (function (lambda ()
		      (font-lock-fontify-buffer))))

;;;
;; first, how do we get & parse the pr's?
(defun gnats-admin::run-query ()
  "Run a default query, setting pr-list to the result."
  (setq pr-list (apply 'vector (gnats-admin::query)))
  (length pr-list))

(defun trim (s)
  "trim the leading & trailing blanks from a string"
  (if (string-match "^\\s-*\\(\\S-.*\\S-\\|\\S-\\)\\s-*$" s)
      (substring s (match-beginning 1) (match-end 1))
    "")
  )

;; parse one pr -- moves point.
(defun gnats-admin::parse-pr ()
  "parse one pr, moving point"
  (let ((pr nil))
    (let ((flv (make-vector 13 nil))
	  (lim 0)
	  (bol 0)
	  (index 0))
      (end-of-line)
      (setq lim (point))
      (beginning-of-line)
      (setq bol (point))
      (while (and (< (point) lim)
		  (re-search-forward "\\([^|]*\\)|" lim t))
	(aset flv index (trim (match-string 1)))
	(setq index (1+ index)))
      (if (not (= index 13))
	  (error "Bad PR inquiry: %s" (buffer-substring bol lim)))
      (setq pr (gnats-admin::vec->pr flv)))
    (if (not (bolp))
	(forward-line 1))
    pr))

(defun gnats-admin::query (&rest args)
  (let ((prl (list))
	(buf (get-buffer-create "**gnats-query*")))
    (save-excursion
      (set-buffer buf)
      (delete-region (point-min) (point-max))
      (message "Running query")
      (setq args
	    (mapcar (function (lambda (x) (format "%s" x))) args))
      (apply 'call-process "query-pr" nil t nil "--sql" args)
      (message "Query completed")
      ;; now parse the output
      (goto-char (point-min))
      (while (not (eobp))
	(setq prl (cons (gnats-admin::parse-pr) prl)))
      )
    (message "Result parsed")
    ;; lots of stuff to apply the proper hook
    (setq gnats-admin:query-list prl)
    (run-hooks 'gnats-admin-query-hook)
    (setq prl gnats-admin:query-list)
    (setq gnats-admin:query-list nil)
    (nreverse prl)))

;; 
;; 
(defun gnats-admin::vec->pr (v)
  "massage a 13-element vector into the internal pr representation.
fields are as described in query-pr documentation."
  (if (not (and (vectorp v)
		(= (length v) 13)))
      (error "Not a valid PR intermediate form"))

  ;;; 0 - pr number
  (aset v 0 (read (aref v 0)))
  ;;; 1 - category
  (aset v 1 (read (aref v 1)))
  ;;; 2 - synopsis
  ; leave as string
  ;;; 3 - confidential
  (aset v 3 (if (equal "no" (aref v 3)) nil (aref v 3)))
  ;;; 4 - severity
  (let ((num (read (aref v 4))))
    (aset v 4 (aref 
	       [null critical serious non-critical] 
	       num)))
  ;;; 5 - priority
  (let ((num (read (aref v 5))))
    (aset v 5 (aref 
	       [null high medium low]
	       num)))
  ;;; 6 - responsible
  ; leave as string
  ;;; 7 - state
  (let ((num (read (aref v 7))))
    (aset v 7 (aref
	       [null open analyzed suspended feedback closed]
	       num)))
  ;;; 8 - class
  (let ((num (read (aref v 8))))
    (aset v 8 (aref
	       [null sw-bug doc-bug support change-request mistaken duplicate]
	       num)))
  ;;; 9 - submitter-id
  ; leave as string
  ;;; 10 - arrival-date
  ; leave as string
  ;;; 11 - originator
  ; leave as string
  ;;; 12 - release
  ; leave as string

  ;; the fields of v have been transformed; now map them into an alist
  (do
      ((vx 0 (1+ vx))			; v index
       (an				; field names (in order!)
	(gnats-admin::pr-field-names)
	(cdr an))
       (al nil)				; assoc list
       )
      ((null an) (nreverse al))		; <- here's where the result comes from
    (setq al (cons (cons (car an) (aref v vx)) al))
    ))

(defun gnats-admin::pr-get (pr field)
  "Get, from PR, value of slot named FIELD (a symbol)."
  (let ((p (assq field (gnats-admin:pr pr))))
    (if p
	(cdr p)
      nil)))

(defun nset-assq (alist key val)
  "destructively set key'v association in the alist to val.  returns the original
list, unless it was null"
  (let ((p (assq key alist)))
    (if p
	(progn 
	  (setcdr p val)
	  alist)
      (nconc alist (list (cons key val))))))

(defun gnats-admin::pr-set! (pr field val)
  "Set, in PR, slot named FIELD to VAL.  Slot name is a symbol."
  (nset-assq pr field val))

;; fast version of field name->index mapper
;; also tests if a symbol is a field name present in sql report.
(defun gnats-admin::pr-field-index (feild)
  (case feild
    (Number 0)
    (Category 1)
    (Synopsis 2)
    (Confidential  3)
    (Severity  4)
    (Priority 5)
    (Responsible 6)
    (State 7)
    (Class 8)
    (Submitter-Id 9)
    (Arrival-Date 10)
    (Originator 11) 
    (Release 12)))

;; next is for completing-read
;; order must be the same as indices
;; content is (name index type width)
;; width is field width, not counting space
(defconst gnats-admin::pr-field-alist
  '(("Number" 0 integer 3)
    ("Category" 1 symbol 15)
    ("Synopsis" 2 string 80)
    ("Confidential"  3 boolean 1)
    ("Severity"  4 symbol 12)
    ("Priority" 5 symbol 6)
    ("Responsible" 6 string 7)
    ("State" 7 symbol 8)
    ("Class" 8 symbol 14)
    ("Submitter-Id" 9 string 7)
    ("Arrival-Date" 10 string 14)
    ("Originator" 11 string 32) 
    ("Release" 12 string 48))
  "Alist that maps field-name->(name index type width)")

(defun gnats-admin::pr-field-names ()
  "List of symbols that are field keys in pr.  Must be in order."
  (mapcar (function (lambda (pr) (intern (car pr))))
	  gnats-admin::pr-field-alist))

;; format control template for PR display
(defconst gnats-admin::pr-long-format
  '((4 Category Class) (35 Priority Severity) (60 Responsible) (70 State ) nl
    4 "Synopsis:" Synopsis 
    ))
(defconst gnats-admin::pr-short-format 
  '((4 Category Class) (35 Priority Severity) (60 Responsible) (70 State )))

(defvar gnats-admin::pr-format gnats-admin::pr-short-format
  "Format list for printing a pr.")

;; hook that sets extent etc for XEmacs
(defun gnats-admin::pr-display-hook (b e pr buf)
  "Set extent around pr."
  (let ((ext (make-extent b e buf)))
    (set-extent-begin-glyph-layout ext 'outside-margin)
    (set-extent-property ext 'pr pr)
    (set-extent-property ext 'start-open t)
    (set-extent-property ext 'end-open t)
    (set-extent-property ext 'highlight t)))

;; gnats uses one face for each element of the enumerated fields,
;; to give maximum flexibility in display.

;;; symbol->(face foreground background) alist
(defvar gnats-admin::face-color
  '((critical "firebrick" nil)
    (serious "goldenrod" nil)
    (non-critical "blue3" nil)

    (high "firebrick" nil) 
    (medium "goldenrod" nil) 
    (low "blue3" nil)

    (open "firebrick" nil) 
    (analyzed "goldenrod" nil) 
    (suspended "turquoise" nil) 
    (feedback "blue3" nil) 
    (closed "ForestGreen" nil)
    (closed* "HotPink" nil)
    (closed? "blue3" nil)

    (sw-bug nil nil) 
    (doc-bug nil nil) 
    (support nil nil) 
    (change-request nil nil) 
    (mistaken nil nil) 
    (duplicate nil nil))
  "Alist of font properties")

(defun gnats-admin::field-display (pr fld buf)
  "Display value field on specified stream."
  (let
      ((fv (gnats-admin::pr-get pr fld)))
    (princ fv buf)))

(defun gnats-admin::pr-print-func (f pr buf)
  "Printer for pr.  Depends on free variable pr-did-indent."
  (cond
   ((eq 'nl f)
    (princ "\n " buf)
    (setq pr-did-indent t))
   ((listp f)
    (do
	((fmt f (cdr fmt)))
	((null fmt))
      (gnats-admin::pr-print-func (car fmt) pr buf)))
   ((numberp f)
    (indent-to-column f 1)
    (setq pr-did-indent t))
   ((symbolp f)
    (if (not pr-did-indent)
	(princ " " buf))
    (gnats-admin::field-display pr f buf)
    (setq pr-did-indent nil))
   ((stringp f)
    (if (not pr-did-indent)
	(princ " " buf))
    (princ f buf)
    (setq pr-did-indent nil))
   )
  t)
  
(defun gnats-admin::display-pr (pr &optional buf)
  ;; always print the number first
  (if (not buf)
      (setq buf (current-buffer)))
  (let 
      ((b (point))
       (buffer-read-only nil)
       (pr-did-indent nil))
    (princ (gnats-admin::pr-number pr) buf)
    ;; now print according to the pr-format list
    (do
	((fmt gnats-admin::pr-format (cdr fmt)))
	((null fmt))
      (gnats-admin::pr-print-func (car fmt) pr buf))
    (gnats-admin::pr-display-hook 
     b 
     (point) 
     (gnats-admin::pr-number pr)
     buf)
    (newline)))

(defun gnats-admin::pr-buffer-extent (pr)
  "Find the extent for this PR."
  (extent-at (1+ (gnats-admin::pr-buffer-begin pr))
	     (gnats-admin::pr-buffer pr)
	     'pr))
  
(defun gnats-admin::pr-reread (pr)
  "Rerun query for one PR in the pr-list."
  (let
      ((num (gnats-admin::pr-number pr)))
    (let
	((repl (gnats-admin::query num)))
      (if (and (listp repl)
	       (= (length repl) 1)
	       (= (gnats-admin::pr-get (car repl) 'Number) num))
	  (gnats-admin::pr-replace! num (car repl))
	(error "Query failed for pr %s" num)))
    ))

(defun gnats-admin::selection (loprs)
  "Return the elements of LOPRS (list of PR's) which satify PREDicate."
  (let ((pred gnats-admin::selector))
    (if (not pred)
	loprs
      ;; else use the common-lisp loop appropriate to the type of loprs
      (cond
       ((arrayp loprs)
	(loop
	 for pr across loprs
	 if (apply pred pr '())
	   collect pr
	 ))
       ((listp loprs)
	(loop
	 for pr in loprs
	 if (apply pred pr '())
	   collect pr
	 ))
       (t
	(error "Bad type for PR collection")))
      )))

(defun gnats-admin::selected? (pr)
  "Test to see if one pr meets the selection criterion."
  (or (null gnats-admin::selector)
      (apply gnats-admin::selector pr '())))

(defun gnats-admin::pr-replace! (oldpr newpr)
  "Replace the old pr with the new one"
  (if (not (consp newpr))
      (error "Replacement pr must be a full PR value"))
  (let ((pr-num (gnats-admin::pr-number newpr)))
    (if (not (= (gnats-admin::pr-number oldpr)
		pr-num))
	(error "Cannot replace PR with one of different number"))
    (if (< (length pr-list) pr-num)
	(setq pr-list
	      (vconcat pr-list (make-vector (- pr-num (length pr-list)) nil))))
    (aset pr-list (1- pr-num) newpr)))

(defvar gnats-admin::dirty-list nil
  "List of PRs which may be out of date and need refreshing.")

(defun gnats-admin:reset () 
  "Reset the cached data for gnats-admin."
  (setq pr-list nil)
  (setq gnats-admin::dirty-list nil)	; it's everything now
  )

(defun gnats-admin:regret ()
  "Create or edit the regression test for the current problem report."
  (interactive)
  (let* ((pr (gnats-admin::pr-at (point)))
	 (num (gnats-admin::pr-number pr)))
    (pr-regret num)))

(defun gnats-admin:refresh (&optional force)
  (interactive "P")
  (if force
      (gnats-admin:reset))
  (if (not pr-list)
      (progn
	(gnats-admin::run-query))
    (progn
      (mapc (function (lambda (p) (gnats-admin::pr-reread p)))
	    gnats-admin::dirty-list)))
  (setq gnats-admin::dirty-list nil)
  (set-buffer (gnats-admin::buffer))
  (let ((standard-output (current-buffer))
	(buffer-read-only nil)
	(this-pr-num (gnats-admin::pr-num-at (point))))
    ; is this overkill; could we save our extents unless force?
    (if nil
	(map-extents (function (lambda (ext data) (delete-extent ext) nil))
		     (current-buffer)))
    (delete-region (point-min) (point-max))
    (beginning-of-buffer)
    (message "Redisplay")
    (mapc (function (lambda (pr) 
		      (if (gnats-admin::selected? pr)
			  (gnats-admin::display-pr pr))))
	  pr-list)
    (message nil)
    ;; catch search errors in case the current pr no longer exists--
    ;; if so, go to end of buffer
    (condition-case err
	(if (numberp this-pr-num)
	    (gnats-admin:goto-pr this-pr-num))
      (search-failed
       (goto-char (point-max)))))
  (run-hooks 'gnats-admin-refresh-hook)
  t)

(defun gnats-admin::pr-number (pr)
  "Get the number of this pr -- which can be either a pr datum or a number."
  (or (and (numberp pr) pr)
      (gnats-admin::pr-get pr 'Number)))

;; this must not depend on pr-at, because that depends on this.
(defun gnats-admin::pr-num-at (pos)
  "Find the pr number for the pr at POS"
  (let*
      ((ext (extent-at pos nil 'pr))
       (pr-prop (and ext (extent-property ext 'pr))))
    (if pr-prop
	(gnats-admin::pr-number pr-prop)
      (save-excursion
	(goto-char pos)
	(if (not (looking-at "^\\s-*[0-9]"))
	    (backward-paragraph))
	(if (looking-at "\\s-*[0-9]+[^0-9]")
	    (read (match-string 0))
	  nil)))))

(defun gnats-admin::pr-by-number (num)
  "Find the pr numbered N."
  ;; first try the easy way
  (let
      ((pr (elt pr-list (1- num))))
    (if (and pr (= num (gnats-admin::pr-number pr)))
	pr
      ;; easy way didnt work; scan the list
      (loop
       for prx across pr-list
       if (= num (gnats-admin::pr-number prx))
       return prx))))

;; use face alist to set face colors
(defun gnats-admin::setup-faces ()
  "Set up the faces for gnats admin mode."
  (mapc
   (function (lambda (l) (make-face (car l))))
   gnats-admin::face-color)
  (if (memq (device-class) '(color grayscale))
      (mapc
       (function (lambda (l) 
		   (if (cadr l)
		       (set-face-foreground (car l) (cadr l)))
		   (if (caddr l)
		       (set-face-background (car l) (caddr l)))))
       gnats-admin::face-color))
  (setq font-lock-keywords gnats-admin::font-lock-keywords)
  ;; this is too slow -- instead, do explicit fontification after modify
  ; (turn-on-font-lock)
  )

(defvar gnats-admin::pr-mark-glyph nil
  "Glyph used to mark the current PR in display.")

(defun gnats-admin::buffer ()
  "Find or create gnats admin buffer."
  (or (get-buffer "*gnats*")
      (let ((buf (get-buffer-create "*gnats*")))
	(set-buffer buf)
	(make-local-variable 'paragraph-start)
	(make-local-variable 'paragraph-separate)
	(setq paragraph-start "^\\(\\S-\\|[ \t\n]*$\\)")
	(setq paragraph-separate "^[ \t\n]*$")
	(setq buffer-read-only t)
	(setq buffer-undo-list t)		; disable undo info
	(setq gnats-admin::pr-mark-glyph (make-pixmap "target"))
	(gnats-admin::setup-faces)
	buf)
      ))
    
(defun gnats-admin:pr (pr-or-num)
  "If PR-OR-NUM is a pr, return it; if it's a number, 
return the pr with that number."
  (cond
   ((numberp pr-or-num)
    (gnats-admin::pr-by-number pr-or-num))
   ((consp pr-or-num)
    pr-or-num)
   (t
    (error "Not a valid PR: %s" pr-or-num))
   ))
    
(defun gnats-admin::pr-at (pos)
  "PR at POSITION"
  (or
   (let ((ext (extent-at pos nil 'pr)))
     (if ext (extent-property ext 'pr)))
   (gnats-admin::pr-num-at pos)))

;; next should, ideally, run a 1-pr query then splice that into
;; pr-list to update the current pr.
;; however, there's a race condition with gnats; so put the edited
;; pr on the dirty list to be inquired later.
(defun gnats-admin:pr-edit ()
  (interactive)
  (let*
      ((pr (gnats-admin::pr-at (point)))
       (num (gnats-admin::pr-number pr))
       (num-str (format "%d" num)))
    (pr-edit num-str)
    (setq gnats-admin::dirty-list (cons pr gnats-admin::dirty-list))))

(defun gnats-admin:pr-view ()
  (interactive)
  (pr-view (format "%d" (gnats-admin::pr-num-at (point)))))
(defun gnats-admin:pr-synopsis ()
  (interactive)
  (let*
      ((pr (gnats-admin::pr-at (point)))
       (syn (gnats-admin::pr-get pr 'Synopsis)))
    (message "Synopsis: %s" syn)))

(defun gnats-admin:pr-originator ()
  (interactive)
  (let*
      ((pr (gnats-admin::pr-at (point)))
       (syn (gnats-admin::pr-get pr 'Originator)))
    (message "Originator: %s" syn)))

(defun gnats-admin:pr-field (fld)
  "Show any pr field FLD of current pr."
  (interactive
   (list (completing-read "Field: " gnats-admin::pr-field-alist
			   nil t)))
  (let*
      ((pr (gnats-admin::pr-at (point)))
       (val (gnats-admin::pr-get pr (intern fld))))
    (message "%s: %s" fld val)))

(defvar gnats-admin::current-pr nil "Current pr")
(defun gnats-admin::highlight (pr)
  "Hilight the current PR -- may unhilight the previous."
  (condition-case err
      (progn
	(if gnats-admin::current-pr
	    (highlight-extent
	     (gnats-admin::pr-buffer-extent gnats-admin::current-pr)
	     nil))
	(highlight-extent (gnats-admin::pr-buffer-extent pr) t)
	(setq gnats-admin::current-pr pr))
    (error (setq gnats-admin::current-pr nil))))

(defun gnats-admin::highlight-point ()
  "Highlight the pr at point"
  ;; make point visible
  (or
   (pos-visible-in-window-p)
   (recenter '(t)))
  (gnats-admin::highlight (gnats-admin::pr-at (point))))

(defun gnats-admin:next ()
  "Next pr"
  (interactive)
  (forward-paragraph)
  (gnats-admin::highlight-point))
(defun gnats-admin:prev ()
  "Prev pr."
  (interactive)
  (backward-paragraph)
  (gnats-admin::highlight-point))
(defun gnats-admin:this ()
  "Activate pr at point."
  (interactive)
  (end-of-line)
  (backward-paragraph)
  (gnats-admin::highlight-point))

(defun gnats-admin:mouse-set (ev)
  (interactive "e")
  (mouse-set-point ev)
  (gnats-admin::highlight-point))

(defun gnats-admin:mouse-synopsis (ev)
  (interactive "e")
  (gnats-admin:mouse-set ev)
  (gnats-admin:pr-synopsis))

(defun gnats-admin:mouse-menu (ev)
  (interactive "e")
  (gnats-admin:mouse-set ev)
  (popup-mode-menu))

(defun gnats-admin:refresh-this-pr ()
  "Reread and refresh the display of the current PR."
  (interactive)
  (let* ((pr (gnats-admin::pr-at (point)))
	 (buffer-read-only nil)
	 (b (gnats-admin::pr-buffer-begin pr))
	 (e (gnats-admin::pr-buffer-end pr)))
    (goto-char b)
    (save-excursion
      (gnats-admin::pr-buffer-delete pr)
      (gnats-admin::pr-reread pr)
      (gnats-admin::display-pr pr (current-buffer))
      ;; had to dive pretty deep into font-lock to get this one...
      (let ((font-lock-mode t))
	(font-lock-after-change-function b e 1)))
    (gnats-admin::highlight-point)))

(defun gnats-admin:goto-pr (n)
  "Make pr number N the current pr."
  (interactive "nPR: ")
  (goto-char (gnats-admin::pr-n-pos n))
  (gnats-admin::highlight-point))

(defun gnats-admin::pr-n-pos (n)
  "Find the buffer position of pr numbered N in current buffer."
  (save-excursion
    (goto-char (point-min))
    (let ((re (format "^%s\\s-" n)))
      (re-search-forward re)
      (point)
      )))

(defun gnats-admin::pr-buffer-delete (pr)
  "Delete the display of the PR."
  (let* ((b (gnats-admin::pr-buffer-begin pr))
	 (e (gnats-admin::pr-buffer-end pr))
	 (mbuf (gnats-admin::pr-buffer pr))
	 (pr-ext (gnats-admin::pr-buffer-extent pr)))
    (set-buffer mbuf)
    (let ((buffer-read-only nil))
      (delete-region b e)
      (if (extentp pr-ext)
	  (delete-extent pr-ext)))
    ))

(defun gnats-admin:quit ()
  "Quit out of gnats admin mode."
  (interactive)
  (if (get-buffer "**gnats-query*")
      (kill-buffer "**gnats-query*"))
  (kill-buffer nil))
  
(defvar gnats-admin-mode-map nil "Key map for gnats admin mode.")

(defun gnats-admin::setup-keymap ()
  (if (not (keymapp gnats-admin-mode-map))
      (progn
	(setq gnats-admin-mode-map (make-keymap))
	(suppress-keymap gnats-admin-mode-map)
	(define-key gnats-admin-mode-map "e" 'gnats-admin:pr-edit)
	(define-key gnats-admin-mode-map "v" 'gnats-admin:pr-view)
	(define-key gnats-admin-mode-map "s" 'gnats-admin:pr-synopsis)
	(define-key gnats-admin-mode-map "o" 'gnats-admin:pr-originator)
	(define-key gnats-admin-mode-map "f" 'gnats-admin:pr-field)
	(define-key gnats-admin-mode-map "\C-l" 'gnats-admin:refresh)
	(define-key gnats-admin-mode-map "n" 'gnats-admin:next)
	(define-key gnats-admin-mode-map "p" 'gnats-admin:prev)
	(define-key gnats-admin-mode-map " " 'gnats-admin:this)
	(define-key gnats-admin-mode-map "q" 'gnats-admin:quit)
	(define-key gnats-admin-mode-map "g" 'gnats-admin:goto-pr)
	(define-key gnats-admin-mode-map "r" 'gnats-admin:refresh-this-pr)
	(define-key gnats-admin-mode-map "S" 'gnats-admin:edit-selection)
	(define-key gnats-admin-mode-map "R" 'gnats-admin:regret)
	(define-key gnats-admin-mode-map 'button1 'gnats-admin:mouse-set)
	(define-key gnats-admin-mode-map 'button2 'gnats-admin:mouse-synopsis)
	(define-key gnats-admin-mode-map 'button3 'gnats-admin:mouse-menu)
	))
  )

(defun gnats-admin-mode ()
  "Major mode for looking at a summary of gnats reports.
Stomps to gnats admin buffer!
Commands: \\{gnats-admin-mode-map}."
  (interactive)
  (switch-to-buffer (gnats-admin::buffer))
  (setq major-mode 'gnats-admin-mode)
  (setq mode-name "Gnats Admin")
  (gnats-admin::setup-keymap)
  (use-local-map gnats-admin-mode-map)
  (setq mode-popup-menu gnats-admin::popup-menu)
  (gnats-admin:refresh)
  )

(put 'gnats-admin-mode 'mode-class 'special)

(defvar gnats-admin::pr-mark-glyph nil "marker for current PR.")
           
(defun gnats-admin::pr-buffer (pr)
  "The buffer in which this pr is displayed."
  (gnats-admin::buffer))

(defun gnats-admin::pr-buffer-begin (pr)
  "Return the position of beginning of this PR."
  (let 
      ((pr-num (gnats-admin::pr-number pr)))
    (save-excursion 
      (set-buffer (gnats-admin::pr-buffer pr))
      ;; first try locally, then thru whole buffer
      (or
       (and (progn (backward-paragraph 1)
		   (re-search-forward (format "^%d " pr-num) nil t))
	    (progn 
	      (beginning-of-line 1)
	      (point)))
       (and (progn (goto-char (point-min))
		   (re-search-forward (format "^%d " pr-num) nil t))
	    (progn 
	      (beginning-of-line 1)
	      (point)))))))

(defun gnats-admin::pr-buffer-end (pr)
  "Return the position of the end of the PR."
  (save-excursion
    (set-buffer (gnats-admin::pr-buffer pr))
    (goto-char (gnats-admin::pr-buffer-begin pr))
    (forward-paragraph 1)
    (point)))

(defun gnats-admin::unclosed (pr)
  "A selector that chooses unclosed PR's."
  (not (eq 'closed (gnats-admin::pr-get pr 'State))))

(defvar gnats-admin:selexpr nil
  "Selection expression -- see eval-selexpr.")

(defun gnats-admin::eval-selexpr (pr)
  "Evaluate the selection expression, in an environment with
pr bound to the pr, and the field names bound to their value."
  (let
      ((Number (gnats-admin::pr-get pr 'Number))
       (Category (gnats-admin::pr-get pr 'Category))
       (Synopsis (gnats-admin::pr-get pr 'Synopsis))
       (Confidential (gnats-admin::pr-get pr 'Confidential))
       (Severity (gnats-admin::pr-get pr 'Severity))
       (Priority (gnats-admin::pr-get pr 'Priority))
       (Responsible (gnats-admin::pr-get pr 'Responsible))
       (State (gnats-admin::pr-get pr 'State))
       (Class (gnats-admin::pr-get pr 'Class))
       (Submitter-Id (gnats-admin::pr-get pr 'Submitter-Id))
       (Arrival-Date (gnats-admin::pr-get pr 'Arrival-Date))
       (Originator (gnats-admin::pr-get pr 'Originator))
       (Release (gnats-admin::pr-get pr 'Release)))
    (or (not gnats-admin:selexpr)
	(eval gnats-admin:selexpr))))

(defun gnats-admin:edit-selection ()
  "Edit the selection criteria."
  (interactive)
  (setq gnats-admin::selector 'gnats-admin::eval-selexpr)
  (setq gnats-admin:selexpr 
	(edit-expr gnats-admin:selexpr
";; Selection expression.  This is evaluated with Number, Category, Synopsis,
;; Confidential, Severity, Priority, Responsible, State, Class, Submitter-Id,
;; Arrival-Date, Originator, and Release set from the PR; if it's a non-null
;; expression that evaluates true, then that record is displayed.  Free-form
;; fields are strings, others are symbols or other atoms.
"
))
  (gnats-admin:refresh))