mship-edit.el 63.1 KB
Newer Older
David Byers's avatar
David Byers committed
1
2
;;; mship-edit.el --- Summary
;; TO DO
3
;; see tmp.el
4
;;
5
6
7
8
;; When showing hidden entries they are not put in the right position
;; in the buffer. lp--set-entry-visible needs to goto-char to the right
;; place in the buffer.
;;
9
10
11
;; Maybe move setting the membership priority to 
;; lyskom-change-membership-priority. 
;;
David Byers's avatar
David Byers committed
12
13
14
15
16
;; -------------------------------------------------------------------------
;; When prioritizing an entry we need to sort the read lists to put
;; the entries in the proper order. It's possible that we'll have to
;; change the prompt.
;;
17
18
;; Do this under lyskom-update-membership
;;
David Byers's avatar
David Byers committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
;; Test cases:
;;
;; Change the priority of the current conf to lower than one we have
;; unreads in. Should trigger prompt change.
;;
;; Change the priority of the current conf to higher than one that
;; was higher previously and had unreads. This should trigger a
;; prompt change.
;;
;; Re-order two confs we are not in by changing their priority. Check
;; that we get to read them in the correct order.
;;
;; Re-order two confs without changing their priority. Check that we
;; get to read them in the correct order.
;; -------------------------------------------------------------------------
;;
;; -------------------------------------------------------------------------
;; Changing priority might put the conference above or below the
;; current session priority. We need to fetch or delete maps.
;;
39
;; Do this under lyskom-update-membership. Done.
40
;;
David Byers's avatar
David Byers committed
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
;; Test cases:
;;
;; Prioritize a conf under the session priority to above the session
;; priority. Should give us more unreads. Might trigger prompt change.
;;
;; Prioritize a conf with unreads to under the session priority.
;; Should give us less unreads. Might trigger prompt change.
;;
;; While prefetching a conf, change its priority to below the session
;; priority. The prefetched maps should be discarded automatically.
;; -------------------------------------------------------------------------
;;          
;; What we need is a general function we use to change the priority of a
;; membership. Since we do all the updates in the background it's OK to
;; send an update off as soon as we change the priority of a membership.
;; 
;; This function should...
;; - Change the priority and placement of the membership in the server
;; - See if the priority has changed re the session priority and if so
;;   either start a prefetch for the conference or remove the unreads
;;   for the conference from all read lists.
;; - Remove and reinsert the unreads in the reading lists, then update
;;   the prompt.
;; - Sort the membership list.
;;
66
;; Done in lp--set-entry-pri-and-pos and lp--update-membership.
David Byers's avatar
David Byers committed
67
;;
68
69
70
71
72
73
74
75
76
;; TODO UNSORTED
;; Keep the unread counter updated. Hook in lyskom-mark-as-read
;; Keep the membership list update. Perhaps in lyskom-add-membership
;;      and associated functions to add, lyskom-remove-membership to
;;      remove.
;; Endast-function to all marked memberships
;; Uppskjut-function to all marked memberships
;; Remove and add membership functions.
;; 
David Byers's avatar
David Byers committed
77
78
79
80
81


;;; Commentary:
;; 

David Byers's avatar
David Byers committed
82
83
(require 'advice)

David Byers's avatar
David Byers committed
84
;;; Code:
85

David Byers's avatar
David Byers committed
86
(def-komtype lp--entry
David Byers's avatar
David Byers committed
87
88
89
90
91
92
93
94
95
  (start-marker                         ; Where the entry is in the buffer
   end-marker                           ; Where it ends in the buffer
   priority                             ; The saved priority of the membership
   membership                           ; The membership
   selected                             ; Selected or not
   state                                ; Expanded display or not
   visible                              ; Non-nil when visible
   extents                              ; Alist of extents/overlays
   ))
96

97
98
99
100
(defvar lp--last-format-string nil
  "The cached format string for entries. 
Use lp--compute-format-string when you need the format string. Do not
access this variable directly.")
101
102
103
(defvar lp--last-window-width -1)

;;; Local variables in the prioritize buffer
104
105
;;; There should be no reason to use these at all. There are functional
;;; abstractions that let you access their contents.
106
107
108
109
110

(defvar lp--entry-list nil)
(defvar lp--list-start-marker nil)
(defvar lp--list-end-marker nil)
(defvar lp--selected-entry-list nil)
David Byers's avatar
David Byers committed
111
(defvar lp--buffer-done nil)
112

113

114
115
;;; ============================================================
;;; Utility functions and really basic stuff
116
117

(defun lp--entry-update-extents (entry)
118
119
120
121
122
  "Update the start and end positions for extents of ENTRY.
Update the start and end positions of all extents or overlays listed
in the extent list of ENTRY to match the start and end markers. If the
start or end markers point nowhere, detatch the extents. If overlays
are used, set the start and end positions to zero."
123
124
125
126
127
128
129
130
131
132
133
134
  (let ((extents (lp--entry->extents entry)))
    (while extents
      (lyskom-xemacs-or-gnu 
       (progn (set-extent-property (cdr (car extents)) 'end-open t)
              (set-extent-property (cdr (car extents)) 'start-open t)
              (set-extent-endpoints (cdr (car extents))
                                    (lp--entry->start-marker entry)
                                    (lp--entry->end-marker entry)))
       (progn (move-overlay (cdr (car extents)) 
                            (or (lp--entry->start-marker entry) 0)
                            (or (lp--entry->end-marker entry) 0))))
      (setq extents (cdr extents)))))
David Byers's avatar
David Byers committed
135
136


137
138
139
140
141
142
143
144
145
146
147
(defun lyskom-change-membership-priority (conf-no new-priority)
  "Change the priority of memberhip for CONF-NO to NEW-POSITION.
This function does not tell the server about the change."
  (let* ((mship (lyskom-get-membership conf-no t))
         (old-priority (and mship (membership->priority mship))))
    (when mship
      (set-membership->priority mship new-priority)
      (lyskom-sort-membership)
      (cond
       ((and (>= old-priority lyskom-session-priority)
             (>= new-priority lyskom-session-priority))
148
149
        ; Don't (lyskom-sort-to-do-list) since lyskom-sort-membership will.
        )
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

       ((and (< old-priority lyskom-session-priority)
             (>= new-priority lyskom-session-priority))
        (let ((mship (lyskom-get-membership conf-no t)))
          (when mship (lyskom-prefetch-map conf-no mship))))

       ((and (>= old-priority lyskom-session-priority)
             (< new-priority lyskom-session-priority))
        (read-list-delete-read-info conf-no lyskom-to-do-list)
        (when (eq conf-no lyskom-current-conf)
          (lyskom-leave-current-conf))
        (lyskom-update-prompt t))
      ))))

(defun lyskom-change-membership-position (conf-no new-position)
  "Change the position of memberhip for CONF-NO to NEW-POSITION.
This function does not tell the server about the change."

  ;; FIXME: We have to update all positions in the membership 
169
  ;; FIXME: list, not just the one we changed.
170
171
172
173
174

  (let ((mship (lyskom-get-membership conf-no t)))
    (when mship
      (set-membership->position mship new-position)
      (setq lyskom-membership (lyskom-move-in-list mship lyskom-membership new-position))
175
      (lyskom-update-membership-positions))))
176

David Byers's avatar
David Byers committed
177
178

;;; ============================================================
David Byers's avatar
David Byers committed
179
;; Entry drawing
David Byers's avatar
David Byers committed
180

181
182
(defun lp--entry-set-background (entry color)
  "Use extents or overlays to set the background of ENTRY to COLOR."
David Byers's avatar
David Byers committed
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
  (save-excursion
    (set-buffer (marker-buffer (lp--entry->start-marker entry)))
    (if (null color)
        (let* ((extent (assq 'color (lp--entry->extents entry))))
          (when extent
            (lyskom-xemacs-or-gnu
             (delete-extent (cdr extent))
             (delete-overlay (cdr extent)))
            (set-lp--entry->extents entry 
                                    (delq extent (lp--entry->extents entry)))))

      (let* ((extent (cdr (assq 'color (lp--entry->extents entry))))
             (facename (intern (format "lyskom-%s-background" color)))
             (face (or (find-face facename) (lyskom-make-face facename t))))
        (unless extent
          (lyskom-xemacs-or-gnu
           (setq extent (make-extent (or (lp--entry->start-marker entry) 0)
                                     (or (lp--entry->end-marker entry) 0)))
           (setq extent (make-overlay (or (lp--entry->start-marker entry) 0)
                                      (or (lp--entry->end-marker entry) 0)
                                      nil t)))
          (set-lp--entry->extents entry (cons (cons 'color extent)
                                              (lp--entry->extents entry))))
        (set-face-background face color)
        (lyskom-xemacs-or-gnu (progn (set-extent-property extent 'end-open t)
                                     (set-extent-property extent 'start-open t)
                                     (set-extent-property extent 'priority 1000)
                                     (set-extent-property extent 'face face))
                              (progn (overlay-put extent 'priority 1000)
                                     (overlay-put extent 'face face)))))))

(defun lp--entry-set-foreground (entry color)
  "Use extents or overlays to set the foreground of ENTRY to COLOR."
  (save-excursion 
    (set-buffer (marker-buffer (lp--entry->start-marker entry)))
    (if (null color)
        (let* ((extent (assq 'fcolor (lp--entry->extents entry))))
          (when extent
            (lyskom-xemacs-or-gnu
             (delete-extent (cdr extent))
             (delete-overlay (cdr extent)))
            (set-lp--entry->extents entry 
                                    (delq extent (lp--entry->extents entry)))))

      (let* ((extent (cdr (assq 'fcolor (lp--entry->extents entry))))
             (facename (intern (format "lyskom-%s-foreground" color)))
             (face (or (find-face facename) (lyskom-make-face facename t))))
        (unless extent
231
          (lyskom-xemacs-or-gnu
David Byers's avatar
David Byers committed
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
           (setq extent (make-extent (or (lp--entry->start-marker entry) 0)
                                     (or (lp--entry->end-marker entry) 0)))
           (setq extent (make-overlay (or (lp--entry->start-marker entry) 0)
                                      (or (lp--entry->end-marker entry) 0)
                                      nil t)))
          (set-lp--entry->extents entry (cons (cons 'fcolor extent)
                                              (lp--entry->extents entry))))
        (set-face-foreground face color)
        (lyskom-xemacs-or-gnu (progn (set-extent-property extent 'end-open t)
                                     (set-extent-property extent 'start-open t)
                                     (set-extent-property extent 'priority 1000)
                                     (set-extent-property extent 'face face))
                              (progn (overlay-put extent 'priority 1000)
                                     (overlay-put extent 'face face)))))))

247
248


249
(defmacro lp--save-excursion (&rest body)
David Byers's avatar
David Byers committed
250
251
252
253
  "Evecute BODY and restore the current location of point.
The location of point is in relation to the entry it is in.  Point
will move as the entry moves.  The location of point is restored
even if the character at point is deleted."
254
255
  `(let* ((lp--saved-entry  (lp--entry-at (point)))
          (lp--saved-column (and lp--saved-entry
David Byers's avatar
David Byers committed
256
                                 (- (point)
257
258
259
260
261
                                    (lp--entry->start-marker
                                     lp--saved-entry)))))
     (save-excursion ,@body)
     (if (and lp--saved-entry
              (lp--entry->start-marker lp--saved-entry))
David Byers's avatar
David Byers committed
262
         (goto-char (+ lp--saved-column (lp--entry->start-marker
263
264
265
                                         lp--saved-entry))))))


266
(defun lp--compute-format-string ()
David Byers's avatar
David Byers committed
267
268
269
  "Compute the format string for an entry in the buffer.
To save time, the format string is cached in `lp--last-format-string'.  It is
only recomputed if the window width changes."
270
271
272
273
274
275
  (if (and lp--last-format-string
           (eq (window-width) lp--last-window-width))
      lp--last-format-string
    (let ((total (- (window-width) 1 3 3 2 12 2 5 2 3 1)))
      (setq lp--last-window-width (window-width))
      (setq lp--last-format-string
David Byers's avatar
David Byers committed
276
            (concat "%#1c %=3#2s %#9c %=-" (number-to-string total)
277
278
279
280
                    "#3M  %=-12#4s %[%#13@%=5#5s%]  %[%#10@%#6c%]%[%#11@%#7c%]%[%#12@%#8c%]")))))

(defun lp--format-insert-entry (entry)
  "Format ENTRY and insert it into the current buffer at point."
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
  (let* ((mship-conf-no (membership->conf-no (lp--entry->membership entry)))
         (adder-conf-no (membership->created-by (lp--entry->membership entry)))
         (mship-conf-stat (cache-get-conf-stat mship-conf-no))
         (adder-conf-stat (cache-get-conf-stat adder-conf-no)))

    (if (null mship-conf-stat)
        (lyskom-format-insert-at-point 
         "%#1D"
         (lyskom-create-defer-info 'get-conf-stat
                                   mship-conf-no
                                   'lp--format-entry
                                   (point-marker)
                                   (length lyskom-defer-indicator)
                                   "%#1s"
                                   entry))
      (lp--format-entry mship-conf-stat entry))

David Byers's avatar
David Byers committed
298
    (when (eq (lp--entry->state entry) 'expanded)
299
300
301
302
303
304
305
306
307
308
309
310
      (lyskom-insert-at-point "\n        ")
      (if (null adder-conf-stat)
          (lyskom-format-insert-at-point 
           "%#1D"
           (lyskom-create-defer-info 'get-conf-stat
                                     adder-conf-no
                                     'lp--format-entry-expansion
                                     (point-marker)
                                     (length lyskom-defer-indicator)
                                     "%#1s"
                                     entry))
        (lp--format-entry-expansion adder-conf-stat entry)))))
David Byers's avatar
David Byers committed
311
312
313


(defun lp--format-entry (conf-stat defer-info)
314
315
316
317
318
319
  (let ((entry nil))
    (if (lyskom-lp--entry-p defer-info)
        (progn (setq entry defer-info)
               (setq defer-info nil))
      (setq entry (defer-info->data defer-info)))
    (let* ((un (lyskom-find-unread (membership->conf-no
David Byers's avatar
David Byers committed
320
                                    (lp--entry->membership entry))))
321
322
323
324
325
326
327
328
329
330
331
332
333
           (string (lyskom-format
                    (lp--compute-format-string)
                    (if (lp--entry->selected entry) ?* ?\ )
                    (if (zerop (membership->priority
                                (lp--entry->membership entry)))
                        "-"
                      (int-to-string (membership->priority
                                      (lp--entry->membership entry))))
                    (if (null conf-stat)
                        (lyskom-format 'conference-does-not-exist 
                                       (membership->conf-no 
                                        (lp--entry->membership entry)))
                      conf-stat)
334
335
336
                    (lyskom-format-time
                     'date
                     (membership->last-time-read (lp--entry->membership entry)))
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
                    (if un (int-to-string un) "")
                    
                    (if (membership-type->invitation (membership->type (lp--entry->membership entry))) ?I ?.)
                    (if (membership-type->secret (membership->type (lp--entry->membership entry))) ?H ?.)
                    (if (membership-type->passive (membership->type (lp--entry->membership entry))) ?P ?.)
                    (if (and conf-stat (eq lyskom-pers-no (conf-stat->supervisor conf-stat))) ?O ?\ )
                    (lyskom-default-button 'prioritize-flag-menu
                                           (list entry 'invitation)
                                           (list "%#1s (%=#2M)"
                                                 (lyskom-get-string 'Invitation-mt-type)
                                                 (membership->conf-no
                                                  (lp--entry->membership entry))))
                    (lyskom-default-button 'prioritize-flag-menu
                                           (list entry 'secret)
                                           (list "%#1s (%=#2M)"
                                                 (lyskom-get-string 'Secret-mt-type)
                                                 (membership->conf-no
                                                  (lp--entry->membership entry))))
                    (lyskom-default-button 'prioritize-flag-menu
                                           (list entry 'passive)
                                           (list "%#1s (%=#2M)"
                                                 (lyskom-get-string 'Passive-mt-type)
                                                 (membership->conf-no
                                                  (lp--entry->membership entry))))
361
362
                    '(lp--unread t)
                    )))
363
364
      (if defer-info
          (lyskom-replace-deferred defer-info string)
365
        (lyskom-insert-at-point string))
David Byers's avatar
David Byers committed
366

David Byers's avatar
David Byers committed
367
368
      (if (membership-type->passive
           (membership->type (lp--entry->membership entry)))
369
          (lp--entry-set-foreground entry (lyskom-face-foreground-name kom-dim-face))
David Byers's avatar
David Byers committed
370
371
        (lp--entry-set-foreground entry nil))

372
      (if (lp--entry->selected entry)
373
          (lp--entry-set-background entry (lyskom-face-background-name kom-mark-face))
374
375
        (lp--entry-set-background entry nil)))
))
376
      
David Byers's avatar
David Byers committed
377
378

(defun lp--format-entry-expansion (conf-stat defer-info)
379
380
381
382
383
384
385
386
387
388
389
  (let ((entry nil))
    (if (lyskom-lp--entry-p defer-info)
        (progn (setq entry defer-info)
               (setq defer-info nil))
      (setq entry (defer-info->data defer-info)))

  (let* ((membership (lp--entry->membership entry))
         (string 
          (if (and (null conf-stat)
                   (eq 0 (time->sec (membership->created-at membership)))
                   (eq 0 (time->min (membership->created-at membership)))
390
                   (eq 1 (time->mon (membership->created-at membership)))
391
                   (eq 1 (time->mday (membership->created-at membership)))
392
                   (eq 1970 (time->year (membership->created-at membership))))
393
394
395
396
              (lyskom-format "Ingen information om när medlemskapet skapades")
            (lyskom-format "%#1s %#2s av %#3P"
                           (if (membership-type->invitation (membership->type (lp--entry->membership entry)))
                               "Inbjuden" "Adderad")
397
398
                           (lyskom-format-time
                            'date-and-time
399
400
401
402
403
404
405
406
                            (membership->created-at (lp--entry->membership entry)))
                           (if (null conf-stat)
                               (lyskom-format 'person-does-not-exist
                                              (membership->created-by (lp--entry->membership entry)))
                             conf-stat)))))
    (if defer-info
        (lyskom-replace-deferred defer-info string)
      (lyskom-insert-at-point string)))))
407

408
409
410
411

(defun lp--print-entry (entry)
  "Print the entry ENTRY at the current position in the buffer.
The start and end markers of the entry are adjusted"
412
413
414
415
  (let ((buffer-read-only nil))
    (insert-before-markers "\n")
    (forward-char -1)
    (set-lp--entry->start-marker entry (point-marker))
416
    (lp--format-insert-entry entry)
417
    (set-lp--entry->end-marker entry (point-marker))
418
    (lp--entry-update-extents entry)
419
    (forward-char 1)))
420
421

(defun lp--erase-entry (entry)
David Byers's avatar
David Byers committed
422
  "Erase the printed representation of the entry ENTRY in the buffer."
423
424
425
426
  (let ((buffer-read-only nil))
    (delete-region (lp--entry->start-marker entry)
                   (1+ (lp--entry->end-marker entry)))
    (set-lp--entry->start-marker entry nil)
427
428
    (set-lp--entry->end-marker entry nil)
    (lp--entry-update-extents entry)))
429
430
431

(defun lp--redraw-entry-mark (entry)
  "Redraw the mark for ENTRY."
432
433
434
435
  (let ((buffer-read-only nil))
    (lp--save-excursion
     (goto-char (lp--entry->start-marker entry))
     (insert (if (lp--entry->selected entry) ?* ?\ ))
436
     (if (lp--entry->selected entry)
437
         (lp--entry-set-background entry (lyskom-face-background-name kom-mark-face))
438
       (lp--entry-set-background entry nil))
David Byers's avatar
David Byers committed
439
440
     (delete-char 1)
     (lp--entry-update-extents entry))))
441
442
443

(defun lp--redraw-entry (entry)
  "Redraw the entry ENTRY."
David Byers's avatar
David Byers committed
444
  (lp--save-excursion
445
446
447
448
449
   (when (lp--entry->start-marker entry)
     (goto-char (lp--entry->start-marker entry))
     (lp--erase-entry entry))
   (when (lp--entry->visible entry)
     (lp--print-entry entry))))
David Byers's avatar
David Byers committed
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
(defun lp--perform-in-all-buffers (fn &rest args)
  "Perform FN in all prioritization buffers. ARGS are arguments for FN.
Normally there should only be one buffer, but who knows..."
  (when lyskom-buffer
    (save-excursion
      (set-buffer lyskom-buffer)
      (let ((prioritize-buffers (lyskom-buffers-of-category 'prioritize)))
        (lyskom-traverse buffer prioritize-buffers
          (when (buffer-live-p buffer)
            (set-buffer buffer)
            (apply fn args)))))))


(defun lp--maybe-update-entry-for-conf (conf-no)
  (lp--perform-in-all-buffers
   (lambda (conf-no)
     (let ((entry (lp--conf-no-entry conf-no)))
       (when entry (lp--redraw-entry entry))))
   conf-no))

(defun lp--maybe-update-unreads (conf-no)
    (lp--perform-in-all-buffers
     (lambda (conf-no unread)
       (let ((entry (lp--conf-no-entry conf-no)))
         (when entry
           (let ((bounds (next-text-property-bounds 
                          1 (lp--entry->start-marker entry) 'lp--unread)))
             (when bounds
               (lp--save-excursion
                (let ((buffer-read-only nil))
                  (delete-region (car bounds) (cdr bounds))
                  (goto-char (car bounds))
                  (insert (lyskom-format "%#2@%=5#1s"
                                         (if unread (int-to-string unread) "")
                                         '(lp--unread t))))))))))
     conf-no (lyskom-find-unread conf-no)))
487
488


David Byers's avatar
David Byers committed
489
;;; ============================================================
David Byers's avatar
David Byers committed
490
;; List management
David Byers's avatar
David Byers committed
491

492
(defun lp--set-entry-list (entries)
David Byers's avatar
David Byers committed
493
  "Set the list of entries to ENTRIES."
494
495
496
497
498
499
  (setq lp--entry-list entries))

(defun lp--all-entries ()
  "Return a list of all entries."
  lp--entry-list)

David Byers's avatar
David Byers committed
500
501

(defun lp--conf-no-entry (conf-no)
David Byers's avatar
David Byers committed
502
  "Find the entry for a membership in CONF-NO."
David Byers's avatar
David Byers committed
503
504
505
  (let ((entries (lp--all-entries))
        (found nil))
    (while (and entries (null found))
David Byers's avatar
David Byers committed
506
      (when (eq conf-no (membership->conf-no (lp--entry->membership
David Byers's avatar
David Byers committed
507
508
509
510
511
                                              (car entries))))
        (setq found (car entries)))
      (setq entries (cdr entries)))
    found))

512
(defun lp--find-new-position (entry priority &optional pos)
David Byers's avatar
David Byers committed
513
514
  "Find the new position for ENTRY it is were given priority PRIORITY.
If priority is lower than the entry priority this is the last position
David Byers's avatar
David Byers committed
515
516
currently occupied by an entry with a higher priority.  If priority is
higher, then it is the first position with a priority less than the
David Byers's avatar
David Byers committed
517
518
519
520
entry priority"
  (let ((entries (lp--all-entries))
        (result nil)
        (tmp nil))
David Byers's avatar
David Byers committed
521
    (cond
David Byers's avatar
David Byers committed
522
523
524

     ;; Moving down. Return the last entry spotted with a higher
     ;; than requested priority
525
526
527
     ((if priority 
          (> (lp--entry->priority entry) priority)
        (< (lp--entry-position entry) pos))
David Byers's avatar
David Byers committed
528
      (while (and entries (null result))
529
530
531
        (when (if priority
                  (<= (lp--entry->priority (car entries)) priority)
                (>= (lp--entry-position (car entries)) pos))
David Byers's avatar
David Byers committed
532
533
534
535
536
537
538
          (setq result tmp))
        (setq tmp (car entries))
        (setq entries (cdr entries)))
      (unless result (setq result tmp)))

     ;; Moving up. Return the first entry with a priority strictly
     ;; less than the requested one
539
540
541
     ((if priority
          (< (lp--entry->priority entry) priority)
        (> (lp--entry-position entry) pos))
David Byers's avatar
David Byers committed
542
      (while (and entries (null result))
543
544
545
546
547
        (when (if priority
                  (< (lp--entry->priority (car entries)) priority)
                (>= (lp--entry-position (car entries)) pos))
          (setq result (car entries)))
        (setq entries (cdr entries))))
David Byers's avatar
David Byers committed
548
549
550
551
     (t (setq result entry)))
    result))
  

552
553
554
555
(defun lp--get-last-visible-entry ()
  "Return the last visible entry in the list."
  (let* ((pos (1- (length (lp--all-entries))))
         (entry (lp--get-entry pos)))
David Byers's avatar
David Byers committed
556
557
558
559
    (while (and (> pos 0) entry (not (lp--entry->visible entry)))
      (setq pos (1- pos))
      (setq entry (lp--get-entry pos)))
    (if (lp--entry->visible entry) entry nil)))
David Byers's avatar
David Byers committed
560

561
562
563
564
(defun lp--get-entry (pos)
  "Return the entry at position POS in the list."
  (elt lp--entry-list pos))

David Byers's avatar
David Byers committed
565
(defun lp--entry-position (entry)
David Byers's avatar
David Byers committed
566
  "Return the position in the list for ENTRY."
David Byers's avatar
David Byers committed
567
568
569
570
  (- (length (lp--all-entries))
     (length (memq entry (lp--all-entries)))))


571
572
573
574
(defun lp--entry-at (where)
  "Return the entry at WHERE."
  (let ((entry-list (lp--all-entries))
        (found nil)
David Byers's avatar
David Byers committed
575
        (pos (save-excursion (goto-char where)
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
                             (beginning-of-line)
                             (point))))
    (while (and (not found) entry-list)
      (when (and (lp--entry->start-marker (car entry-list))
                 (lp--entry->end-marker (car entry-list))
                 (<= (lp--entry->start-marker (car entry-list)) pos)
                 (>= (lp--entry->end-marker (car entry-list)) pos))
        (setq found (car entry-list)))
      (setq entry-list (cdr entry-list)))
    found))

(defun lp--move-entry (entry to)
  "Move the entry ENTRY to position TO in the list."
  (when (or (< to 0) (> to (length (lp--all-entries))))
    (error "Position out of range: %d" to))
  (lp--erase-entry entry)
  (lp--set-entry-list (lp--remove-from-list entry (lp--all-entries)))
593
  (let ((cur (lp--get-entry (lp--next-visible-entry to))))
594
595
    (if cur
        (goto-char (lp--entry->start-marker cur))
596
      (goto-char lp--list-end-marker))
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
    (lp--set-entry-list (lp--add-to-list to entry (lp--all-entries)))
    (lp--print-entry entry)))


(defun lp--remove-from-list (elem l)
  "Destructively emove the element at index ELEM from the list L."
  (let* ((n (- (length l) (length (memq elem l)))))
    (cond ((= 0 n) (cdr l))
          ((= n (length l)) l)
          (t (setcdr (nthcdr (1- n) l) (nthcdr (1+ n) l))
             l))))

(defun lp--add-to-list (elem data l)
  "At the position ELEM, add DATA to the list L using side effects."
  (if (> elem (length l))
      (error "Args out of range: %S, %d" l elem))
  (if (= 0 elem)
      (cons data l)
    (setcdr (nthcdr (1- elem) l)
            (cons data (nthcdr elem l)))
    l))

David Byers's avatar
David Byers committed
619
620
621
(defun lp--list-move-element (el to list)
  "Move element EL by side effects so it appears at position TO in LIST."
  (lp--add-to-list to el (lp--remove-from-list el list)))
622

623
624
(defun lp--add-membership-callback (membership)
  (lp--update-buffer (membership->conf-no membership)))
David Byers's avatar
David Byers committed
625

626
627

(defun lp--update-buffer (conf-no)
David Byers's avatar
David Byers committed
628
  "Update the entry for CONF-NO in the buffer."
David Byers's avatar
David Byers committed
629
630
631
632
633
634
  (lp--save-excursion
    (let ((buffers (lyskom-buffers-of-category 'prioritize)))
      (mapcar (lambda (buffer)
                (set-buffer buffer)
                (let ((entry (lp--conf-no-entry conf-no))
                      (mship (lyskom-get-membership conf-no t)))
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649

                  ;; A new membership

                  (cond 
                   ((null entry)
                    (let* ((pos (or (membership->position mship)
                                    (- (length lyskom-membership)
                                       (length (memq mship lyskom-membership)))))
                           (elem (and pos (lp--get-entry pos)))
                           (entry (lyskom-create-lp--entry 
                                   nil
                                   nil
                                   (membership->priority mship)
                                   mship
                                   nil
David Byers's avatar
David Byers committed
650
651
652
653
                                   (if (memq (membership->created-by mship)
                                             (list lyskom-pers-no 0))
                                       'contracted
                                     'expanded)
654
                                   t
655
                                   nil)))
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
                      (when pos
                        (save-excursion
                          (goto-char (lp--entry->start-marker elem))
                          (lp--set-entry-list
                           (lp--add-to-list pos entry (lp--all-entries)))
                          (lp--print-entry entry)))))

                   ;; We have unsubscribed for good

                   ((null mship)
                    (when entry
                      (lp--set-entry-list
                       (lp--remove-from-list (lp--entry-position entry)
                                             (lp--all-entries)))
                      (lp--erase-entry entry)))

                   ;; The priority or position of a membership has changed

                   ((or (/= (lp--entry->priority entry)
                            (membership->priority mship))
                        (/= (lp--entry-position entry)
                            (membership->position mship)))
                    ;; FIXME: Move the entry
                    )

                   (t (set-lp--entry->membership entry mship)
                      (lp--redraw-entry entry)))))
David Byers's avatar
David Byers committed
683
684
685
686
              buffers))))



687
688
689
(defun lp--map-region (start end function &rest args)
  "For each element from START to END, apply FUNCTION.
Apply FUNCTION to each element in the region from START to END, returning
David Byers's avatar
David Byers committed
690
a list of results.  ARGS will be passed as additional arguments to FUNCTION.
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711

Args: START END FUNCTION ARGS."
  (let ((results nil)
        (entry nil))
    (save-excursion
      (goto-char (lp--entry->start-marker (lp--entry-at start)))
      (setq start (set-marker (make-marker) (point)))
      (goto-char (lp--entry->end-marker (lp--entry-at end)))
      (setq end (set-marker (make-marker) (point)))
      (goto-char start)

      (while (<= (point) end)
        (setq entry (lp--entry-at (point)))
        (setq results (cons (apply function entry args) results))
        (goto-char (lp--entry->end-marker entry))
        (forward-char 2)))
    results))



;;; ============================================================
David Byers's avatar
David Byers committed
712
;; Menu and button functions
713
714

;;; ------------------------------------------------------------
David Byers's avatar
David Byers committed
715
716
;; Flag menu
;; Argument is a list of entry and flag
717
718

(defun lp--flag-menu-get (entry flag)
David Byers's avatar
David Byers committed
719
720
  "For the membership in ENTRY, return the value of flag FLAG.
FLAG must be one of 'invitation, 'secret or 'passive."
721
722
723
724
725
726
727
  (funcall
   (cond ((eq flag 'invitation) 'membership-type->invitation)
         ((eq flag 'secret) 'membership-type->secret)
         ((eq flag 'passive) 'membership-type->passive))
   (membership->type (lp--entry->membership entry))))

(defun lp--flag-menu-set (entry flag value)
David Byers's avatar
David Byers committed
728
729
  "For the membership in ENTRY, set FLAG to VALUE.
FLAG must be one of 'invitation, 'secret or 'passive."
730
731
732
733
734
735
736
737
  (funcall
   (cond ((eq flag 'invitation) 'set-membership-type->invitation)
         ((eq flag 'secret) 'set-membership-type->secret)
         ((eq flag 'passive) 'set-membership-type->passive))
   (membership->type (lp--entry->membership entry))
   value))

(defun lyskom-prioritize-flag-clear (buf arg text)
David Byers's avatar
David Byers committed
738
739
740
741
742
743
744
745
  "Clear the membership flag the user clicked on.
This function should not be called directly.  It is called in response to
a mouse click.

BUF is the buffer in which the mouse click took place.  ARG is a list
of (ENTRY FLAG), where ENTRY is the entry the flag belongs to and FLAG
is one of invitation, secret or passive.  TEXT is the text that the user
clicked on."
746
747
748
749
750
751
752
  (interactive)
  (let ((entry (elt arg 0))
        (flag (elt arg 1)))
    (when (lp--flag-menu-get entry flag)
      (lyskom-prioritize-flag-toggle buf arg text))))

(defun lyskom-prioritize-flag-set (buf arg text)
David Byers's avatar
David Byers committed
753
754
755
756
757
758
759
760
  "Set the membership flag the user clicked on.
This function should not be called directly.  It is called in response to
a mouse click.

BUF is the buffer in which the mouse click took place.  ARG is a list
of (ENTRY FLAG), where ENTRY is the entry the flag belongs to and FLAG
is one of invitation, secret or passive.  TEXT is the text that the user
clicked on."
761
762
763
764
765
766
767
  (interactive)
  (let ((entry (elt arg 0))
        (flag (elt arg 1)))
    (unless (lp--flag-menu-get entry flag)
      (lyskom-prioritize-flag-toggle buf arg text))))

(defun lyskom-prioritize-flag-toggle (buf arg text)
David Byers's avatar
David Byers committed
768
769
770
771
772
773
774
775
  "Toggle the membership flag the user clicked on.
This function should not be called directly.  It is called in response to
a mouse click.

BUF is the buffer in which the mouse click took place.  ARG is a list
of (ENTRY FLAG), where ENTRY is the entry the flag belongs to and FLAG
is one of invitation, secret or passive.  TEXT is the text that the user
clicked on."
776
777
778
779
780
781
782
783
  (interactive)
  (let ((entry (elt arg 0))
        (flag (elt arg 1)))
    (save-excursion
      (set-buffer (marker-buffer (lp--entry->start-marker entry)))
      (lp--flag-menu-set entry flag (not (lp--flag-menu-get entry flag)))

      ;; Attempt to perform the change
David Byers's avatar
David Byers committed
784
      (save-excursion
785
        (set-buffer lyskom-buffer)
David Byers's avatar
David Byers committed
786
787
        (let ((result (blocking-do 'set-membership-type
                                   lyskom-pers-no
788
789
790
791
792
793
794
                                   (membership->conf-no (lp--entry->membership entry))
                                   (membership->type (lp--entry->membership entry)))))

          (unless result
            (message "Det gick inte: %s"
                     (lyskom-get-error-text lyskom-errno))))

David Byers's avatar
David Byers committed
795
        ;; Update the display
796
        (let ((mship
David Byers's avatar
David Byers committed
797
798
               (blocking-do 'query-read-texts
                            lyskom-pers-no
799
                            (membership->conf-no (lp--entry->membership entry)))))
David Byers's avatar
David Byers committed
800
          (lyskom-replace-membership mship)
801
802
803
804
805
806
          (set-lp--entry->membership entry mship)
          (when (eq flag 'passive)
            (cond ((membership-type->passive (membership->type mship))
                   (save-excursion
                     (set-buffer lyskom-buffer)
                     (when (eq (membership->conf-no mship) lyskom-current-conf)
807
                       (lyskom-leave-current-conf))
808
809
810
                     (read-list-delete-read-info (membership->conf-no mship)
                                                 lyskom-to-do-list)
                     (lyskom-update-prompt t)))
811
                  (t (lyskom-prefetch-map (membership->conf-no mship) mship))))
812
813
814
          (blocking-do 'get-conf-stat (membership->conf-no mship))))
      (lp--redraw-entry entry))))

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
(defun lp--toggle-invitation ()
  "Toggle the invitation bit of the current entry"
  (interactive)
  (lp--save-excursion
   (let ((cur (lp--entry-at (point))))
     (cond ((null cur) (error "No entry at point"))
           (t (lyskom-prioritize-flag-toggle (current-buffer)
                                             (list cur 'invitation)
                                             ""))))))

(defun lp--toggle-passive ()
  "Toggle the passive bit of the current entry"
  (interactive)
  (lp--save-excursion
   (let ((cur (lp--entry-at (point))))
     (cond ((null cur) (error "No entry at point"))
           (t (lyskom-prioritize-flag-toggle (current-buffer)
                                             (list cur 'passive)
                                             ""))))))

(defun lp--toggle-secret ()
  "Toggle the secret bit of the current entry"
  (interactive)
  (lp--save-excursion
   (let ((cur (lp--entry-at (point))))
     (cond ((null cur) (error "No entry at point"))
           (t (lyskom-prioritize-flag-toggle (current-buffer)
                                             (list cur 'secret)
                                             ""))))))

845
846
847


;;; ============================================================
David Byers's avatar
David Byers committed
848
849
;; Marking and unmarking memberships
;; 
850
851
852
853
854

(defun lp--select-entries (entry-list state)
  "Set the selection value of all entries in ENTRY-LIST to STATE.
Forces a mode line update"
  (lp--do-select-entries entry-list state)
855
  (lp--update-mode-line))
856
857
858

(defun lp--do-select-entries (entry-list state)
  "Set the selection value of all entries in ENTRY-LIST to STATE."
David Byers's avatar
David Byers committed
859
  (mapcar (lambda (entry)
860
            (when entry
861
              (if (and state (lp--entry->visible entry))
862
                  (add-to-list 'lp--selected-entry-list entry)
David Byers's avatar
David Byers committed
863
                (setq lp--selected-entry-list
864
865
866
867
868
                      (delq entry lp--selected-entry-list)))
              (set-lp--entry->selected entry state)
              (lp--redraw-entry-mark entry))) entry-list))

(defun lp--all-selected-entries ()
David Byers's avatar
David Byers committed
869
  "Return a list of all selected entries."
870
871
872
  lp--selected-entry-list)

(defun lp--set-selected-entries (entry-list)
David Byers's avatar
David Byers committed
873
  "Set the selected entries to exactly the entries in ENTRY-LIST.
874
875
876
Forces a mode line update"
  (lp--do-select-entries (lp--all-selected-entries) nil)
  (lp--do-select-entries entry-list t)
877
  (lp--update-mode-line))
878

879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
;;; ============================================================
;;; Hiding and unhiding entries
;;; FIXME: This doesn't work yet

(defun lp--entry-set-visible (entry-list state)
  "Set the visibility of all entries in ENTRY-LIST to STATE."
  (when (null state)
    (lp--select-entries entry-list nil))
  (mapcar (lambda (entry) 
            (unless (eq (lp--entry->visible entry) state)
              (let ((pos (lp--get-entry 
                          (lp--next-visible-entry
                           (lp--entry-position entry)))))
                (if pos
                    (goto-char (lp--entry->start-marker pos))
                  (goto-char lp--list-end-marker))
              (set-lp--entry->visible entry state)
              (lp--redraw-entry entry))))
          entry-list))


900

901
;;; ------------------------------------------------------------
David Byers's avatar
David Byers committed
902
;; Server update functions
903

904
(defun lp--update-membership (entry old-pri old-pos)
David Byers's avatar
David Byers committed
905
  "Update the server and local versions of membership in ENTRY."
906
  (save-excursion
907
908
909
    (let ((saved-pos (lp--entry-position entry)))
      (set-buffer lyskom-buffer)
      (let ((mship (lp--entry->membership entry)))
910
911
912
913
914
915
        (unless (eq old-pos saved-pos)
          (lyskom-change-membership-position (membership->conf-no mship)
                                             saved-pos))
        (unless (eq old-pri (lp--entry->priority mship))
          (lyskom-change-membership-priority (membership->conf-no mship)
                                             (lp--entry->priority mship)))
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
        (cond ((and (>= old-pri lyskom-session-priority)
                    (< (membership->priority mship) lyskom-session-priority))
               (when (eq lyskom-current-conf (membership->conf-no mship))
                 (lyskom-leave-current-conf))
               (read-list-delete-read-info (membership->conf-no mship)
                                           lyskom-to-do-list)
               (lyskom-update-prompt t))
              ((and (< old-pri lyskom-session-priority)
                    (>= (membership->priority mship) lyskom-session-priority))
               (lyskom-prefetch-map (membership->conf-no mship) mship)))

        (initiate-add-member 'background nil
                             (membership->conf-no mship)
                             lyskom-pers-no
                             (membership->priority mship)
                             (membership->position mship)
                             (membership->type mship))))))
933
934


935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
;;; ============================================================
;;; Support

(defun lp--next-visible-entry (pos)
  "Return the position of the first visible entry following POS."
  (let ((max (length (lp--all-entries)))
        (result nil))
  (while (and (< pos max) (null result))
    (if (lp--entry->visible (lp--get-entry pos))
        (setq result pos
              pos max)
      (setq pos (1+ pos))))
  (or result max)))
  

(defun lp--calculate-distance (pos delta)
  "Return one more than the number of entries between POS and POS+DELTA.
This includes visible and invisible entries. The sign of the
David Byers's avatar
David Byers committed
953
954
returned value has the same sign as DELTA. The result is clipped to the
size of the list."
955
956
957
958
959
  (let ((step (signum delta))
        (num (abs delta))
        (result 0))
    (while (> num 0)
      (setq pos (+ step pos))
David Byers's avatar
David Byers committed
960
961
962
963
964
965
      (if (< pos 0)
          (setq num 0)
        (setq result (1+ result))
        (let ((entry (lp--get-entry pos)))
          (cond ((null entry) (setq result (1+ result) num 0))
                ((lp--entry->visible (lp--get-entry pos)) (setq num (1- num)))))))
966
967
968
    (* result step)))


969
;;; ------------------------------------------------------------
David Byers's avatar
David Byers committed
970
;; User-level functions
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987


(defun lp--select-membership ()
  "Select the membership that point is on."
  (interactive)
  (let ((entry (lp--entry-at (point))))
    (when entry
      (lp--select-entries (list entry) t))))

(defun lp--deselect-membership ()
  "Deselect the membership that point is on."
  (interactive)
  (let ((entry (lp--entry-at (point))))
    (when entry
      (lp--select-entries (list entry) nil))))

(defun lp--toggle-membership-selection (where)
David Byers's avatar
David Byers committed
988
  "Toggle selection of the membership at WHERE."
989
990
  (interactive "d")
  (let ((entry (lp--entry-at where)))
991
992
993
994
    (when entry
      (lp--select-entries (list entry) (not (lp--entry->selected entry))))))

(defun lp--select-region (start end)
David Byers's avatar
David Byers committed
995
  "Select all entries in the region. With prefix arg, deselect.
David Byers's avatar
David Byers committed
996
START and END are the starting and ending points of the region."
997
998
  (interactive "r")
  (let ((entry-list (lp--map-region start end 'identity)))
David Byers's avatar
David Byers committed
999
    (lp--select-entries entry-list (not current-prefix-arg))))
1000

David Byers's avatar
David Byers committed
1001
(defun lp--select-priority (priority)
David Byers's avatar
David Byers committed
1002
  "Select all entries with a priority PRIORITY.
1003
1004
1005
1006
With numeric prefix argument select entries with that priority."
  (interactive "P")
  (lp--do-select-priority priority t))

David Byers's avatar
David Byers committed
1007
(defun lp--deselect-priority (priority)
David Byers's avatar
David Byers committed
1008
  "Deselect all entries with a priority PRIORITY.
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
With numeric prefix argument deselect entries with that priority."
  (interactive "P")
  (lp--do-select-priority priority nil))

(defun lp--do-select-priority (priority select)
  "Select or deselect all entries with priority PRIORITY.
SELECT specifies new select."
  (when (not (numberp priority))
    (let ((entry (lp--entry-at (point))))
      (unless entry (error "No membership at point"))
      (setq priority (membership->priority (lp--entry->membership entry)))))

  (lp--select-entries
   (mapcar (lambda (entry)
             (and (eq priority (membership->priority
                                (lp--entry->membership entry)))
                  entry))
           (lp--all-entries))
   select))

(defun lp--deselect-all ()
David Byers's avatar
David Byers committed
1030
  "Deselect all memberships."
1031
1032
1033
1034
1035
  (interactive)
  (lp--set-selected-entries nil))



David Byers's avatar
David Byers committed
1036
;;; ============================================================
David Byers's avatar
David Byers committed
1037
;; Reprioritization functions
David Byers's avatar
David Byers committed
1038

1039
(defun lp--set-entry-pri-and-pos (entry priority position)
1040
1041
1042
1043
1044
1045
  "Set the priority of ENTRY to PRIORITY and the position to POSITION.
If PRIORITY or POSITION is nil, the parameter is ignored.
If the position changes, lp--move-entry is called.
lp--update-membership is called automatically before this function exits."
  (let ((old-pri (lp--entry->priority entry))
        (old-pos (lp--entry-position entry))
1046
        (need-redraw nil))
1047
1048
1049
1050
1051
1052
1053
    (when (and priority (not (eq priority old-pri)))
      (set-lp--entry->priority entry priority)
      (set-membership->priority (lp--entry->membership entry) priority)
      (setq need-redraw t))
    (when (and position (not (eq position old-pos)))
      (lp--move-entry entry position)
      (setq need-redraw nil))
1054
    (sit-for 0)
1055
1056
1057
    (lp--update-membership entry old-pri old-pos)
    (when need-redraw (lp--redraw-entry entry))))

1058
1059
1060
(defun lp--yank ()
  "Insert all the selected memberships before the entry at point."
  (interactive)
1061
1062
1063
1064
1065
1066
1067
1068
1069
  (lp--save-excursion
   (let* ((cur (lp--entry-at (point)))
          (pos (and cur (lp--entry-position cur)))
          (priority (and cur (lp--entry->priority cur)))
          (entries (lp--all-selected-entries)))
     (cond ((null cur) (error "No entry at point"))
           ((null entries) (error "No entries selected"))
           (t (mapcar 
               (lambda (entry)
1070
                 (lp--set-entry-pri-and-pos
1071
1072
1073
                  entry priority
                  (lp--entry-position (lp--find-new-position entry nil pos))))
               entries))))))
1074
1075
1076
          


David Byers's avatar
David Byers committed
1077
(defun lp--set-priority (priority)
David Byers's avatar
David Byers committed
1078
  "Set the priority of selected memberships to PRIORITY.
David Byers's avatar
David Byers committed
1079
1080
1081
Memberships that must be moved will be moved the shortest distance
possible in the list."
  (interactive "P")
1082
  (let* ((entries (or (lp--all-selected-entries)
David Byers's avatar
David Byers committed
1083
1084
                      (list (lp--entry-at (point))))))
    (unless entries
David Byers's avatar
David Byers committed
1085
      (error "No entries selected"))
David Byers's avatar
David Byers committed
1086
1087
1088
1089
1090
1091
1092
1093
    (unless (numberp priority)
      (cond ((> (length entries) 1)
             (setq priority
                   (lyskom-read-num-range
                    0 255 (lyskom-get-string 'priority-prompt-marked) t)))
            (t
             (setq priority
                   (lyskom-read-num-range
David Byers's avatar
David Byers committed
1094
                    0 255 (lyskom-format 'priority-prompt
David Byers's avatar
David Byers committed
1095
                                         (membership->conf-no
David Byers's avatar
David Byers committed
1096
                                          (lp--entry->membership
David Byers's avatar
David Byers committed
1097
1098
1099
                                           (car entries)))) t)))))
    (lp--save-excursion
     (mapcar (lambda (entry)
David Byers's avatar
David Byers committed
1100
               (let ((new-pos (lp--entry-position
David Byers's avatar
David Byers committed
1101
                               (lp--find-new-position entry priority))))
1102
                 (lp--set-entry-pri-and-pos entry priority new-pos)))
1103
             entries))))
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127

(defun lp--bump-priority (amount)
  "Increase the priority of the current entry by one"
  (lp--save-excursion
   (let* ((cur (lp--entry-at (point)))
          (pri (and cur (lp--entry->priority cur)))
          (pos (lp--entry-position cur))
          (new-pri (+ pri amount)))
     (when (> new-pri 255) (setq new-pri 255))
     (when (< new-pri 0) (setq new-pri 0))

     (when (and cur
                (eq pri new-pri)
                (eq pri 0)
                (eq pos (1- (length (lp--all-entries)))))
       (error "Already at minimum priority"))

     (when (and cur
                (eq pri new-pri)
                (eq pri 255)
                (eq pos 0))
       (error "Already at maximum priority"))

     (cond ((null cur) (error "Nor on an entry"))
1128
1129
           (t (let ((new-pos (lp--entry-position 
                              (lp--find-new-position cur (+ pri amount)))))
1130
                (lp--set-entry-pri-and-pos cur new-pri new-pos)))))))
1131

1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148

(defun lp--increase-priority (arg)
  (interactive "p")
  (lp--bump-priority arg))

(defun lp--decrease-priority (arg)
  (interactive "p")
  (lp--bump-priority (- arg)))



(defun lp--move-up ()
  "Move the current entry up one notch."
  (interactive)
  (lp--save-excursion
   (let* ((cur (lp--entry-at (point)))
          (pos (and cur (lp--entry-position cur)))
1149
1150
          (place (and cur (> pos 0) (+ pos (lp--calculate-distance pos -1))))
          (prev (and place (lp--get-entry place))))
1151
1152
     (cond ((null cur) (error "Not on an entry"))
           ((null prev) (error "Beginning of list"))
1153
1154
1155
           (t (if (/= (lp--entry->priority cur)
                      (lp--entry->priority prev))
                  (progn
1156
                    (lp--set-entry-pri-and-pos cur
1157
                                               (lp--entry->priority prev)
1158
1159
1160
                                               (if (= pos (1+ place))
                                                   nil
                                                 (1+ place)))
1161
                    (lp--redraw-entry cur))
1162
                (lp--set-entry-pri-and-pos cur nil place)))))))
1163
1164
1165
1166
1167
1168
1169

(defun lp--move-down ()
  "Move the current entry up down notch."
  (interactive)
  (lp--save-excursion
   (let* ((cur (lp--entry-at (point)))
          (pos (and cur (lp--entry-position cur)))
1170
1171
          (place (and cur (+ pos (lp--calculate-distance pos 1))))
          (prev (and place (lp--get-entry place))))
1172
1173
     (cond ((null cur) (error "Not on an entry"))
           ((null prev) (error "End of list"))
1174
           (t (if (/= (lp--entry->priority cur)
1175
                      (lp--entry->priority prev))
1176
                  (progn
1177
                    (lp--set-entry-pri-and-pos cur
1178
                                               (lp--entry->priority prev)
1179
1180
1181
                                               (if (= pos (1- place))
                                                   nil
                                                 (1- place)))
1182
                    (lp--redraw-entry cur))
1183
                (lp--set-entry-pri-and-pos cur nil place)))))))
David Byers's avatar
David Byers committed
1184
1185
1186



1187
;;; ============================================================
David Byers's avatar
David Byers committed
1188
;; Motion commands
1189

David Byers's avatar
David Byers committed
1190
1191
1192
1193
1194
1195
1196
1197
(defmacro lp--save-column (&rest body)
  `(let ((lp--saved-column (current-column)))
     ,@body
     (end-of-line)
     (if (> (current-column) lp--saved-column)
         (progn (beginning-of-line)
                (forward-char lp--saved-column)))))

1198
1199
;;; FIXME: Deal with invisible entries

1200
1201
1202
1203
1204
(defun lp--previous-entry (count)
  "Move the cursor up COUNT lines.
The cursor will always move to the start of the target entry."
  (interactive "p")
  (let* ((entry (lp--entry-at (point)))
David Byers's avatar
David Byers committed
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
         (last-entry (lp--get-last-visible-entry))
         (first-entry (lp--next-visible-entry 0))
         (pos (lp--entry-position entry))
         (new-pos (cond ((null last-entry) nil)
                        ((and (null entry) (< (point)
                                              (lp--entry->start-marker 
                                               (lp--get-entry first-entry)))) 0)
                        ((and (null entry) (> (point) 
                                              (lp--entry->end-marker last-entry)))
                         (1- (length (lp--all-entries))))
                        (t (max 0 (+ pos (lp--calculate-distance pos (- count))))))))
David Byers's avatar
David Byers committed
1216
    (condition-case nil
David Byers's avatar
David Byers committed
1217
        (goto-char (lp--entry->start-marker (lp--get-entry new-pos)))
David Byers's avatar
David Byers committed
1218
      (error nil))))
1219
1220
1221
1222
1223
1224

(defun lp--next-entry (count)
  "Move the cursor down COUNT lines.
The cursor will always move to the start of the target entry."
  (interactive "p")
  (let* ((entry (lp--entry-at (point)))
David Byers's avatar
David Byers committed
1225
1226
1227
1228
1229
         (last-entry (lp--get-last-visible-entry))
         (first-entry (lp--next-visible-entry 0))
         (pos (lp--entry-position entry))
         (new-pos (cond ((null last-entry) nil)
                    ((and (null entry) (< (point) 
1230
1231
                                          (lp--entry->start-marker 
                                           (lp--get-entry
David Byers's avatar
David Byers committed
1232
                                            first-entry))))
David Byers's avatar
David Byers committed
1233
                     0)
1234
1235
                    ((and (null entry) (> (point) 
                                          (lp--entry->end-marker 
David Byers's avatar
David Byers committed
1236
                                           last-entry)))
David Byers's avatar
David Byers committed
1237
1238
                     (1- (length (lp--all-entries))))
                    (t (min (1- (length (lp--all-entries)))
David Byers's avatar
David Byers committed
1239
                            (+ pos (lp--calculate-distance pos count)))))))
1240
    (condition-case nil
David Byers's avatar
David Byers committed
1241
        (goto-char (lp--entry->start-marker (lp--get-entry new-pos)))
1242
1243
1244
1245
1246
1247
      (error nil))))

(defun lp--first-entry ()
  "Move point to the first entry in the membership list."
  (interactive)
  (condition-case nil
David Byers's avatar
David Byers committed
1248
1249
1250
1251
      (let ((entry (lp--get-entry (lp--next-visible-entry 0))))
        (cond ((null entry) (goto-char lp--list-end-marker))
              (t (goto-char (lp--entry->start-marker 
                             (lp--get-entry (lp--next-visible-entry 0)))))))
1252
    (error nil)))
1253

1254
1255
1256
1257
(defun lp--last-entry ()
  "Move point to the last entry in the membership list."
  (interactive)
  (condition-case nil
David Byers's avatar
David Byers committed
1258
1259
1260
      (let ((entry (lp--get-last-visible-entry)))
        (cond ((null entry) (goto-char lp--list-end-marker))
              (t (goto-char (lp--entry->start-marker entry)))))
1261
1262
1263
    (error nil)))

(defun lp--goto-priority (priority)
David Byers's avatar
David Byers committed
1264
1265
  "Move to the closest entry with priority PRIORITY.
If there is no entry with the specified priority, move to the nearest
1266
1267
1268
1269
1270
1271
entry with an adjacent priority."
  (interactive "P")
  (let* ((entry (lp--entry-at (point)))
         (seen-me nil)
         (done nil)
         (entry-list (lp--all-entries)))
1272

1273
    ;; Get the priority to move to
1274

1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
    (unless (numberp priority)
      (setq priority
            (lyskom-read-num-range 0 255 "Gå till prioritet: " t)))

    ;; Figure out where to move
    ;; Loop over all entries

    (while (and (not done) (cdr entry-list))
      (when (eq (car entry-list) entry) (setq seen-me t))
      (if (or (< (lp--entry->priority (car entry-list))
                 priority)
              (and seen-me (= (lp--entry->priority (car entry-list))
                              priority)))
          (setq done t)
        (setq entry-list (cdr entry-list))))

    (when entry-list
      (goto-char (lp--entry->start-marker (car entry-list))))))
    
(defun lp--toggle-entry-expansion ()
  "Toggle the expanded state of the current entry."
  (interactive)
  (let ((entry (lp--entry-at (point))))
    (when entry
David Byers's avatar
David Byers committed
1299
      (set-lp--entry->state
1300
1301
1302
       entry
       (if (eq (lp--entry->state entry) 'expanded) 'contracted 'expanded))
      (lp--redraw-entry entry))))
David Byers's avatar
David Byers committed
1303

David Byers's avatar
David Byers committed
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
(defun lp--expand-all (arg)
  "Expand all entries. 
With prefix arg, expand only those that were created by someone else."
  (interactive "P")
  (let ((hidden-list (list lyskom-pers-no 0)))
    (mapcar (lambda (entry)
              (when (and (or (null arg)
                             (not (memq (membership->created-by
                                         (lp--entry->membership entry))
                                        hidden-list)))
                         (not (eq (lp--entry->state entry) 'expanded)))
                (set-lp--entry->state entry 'expanded)
                (lp--redraw-entry entry)))
            (lp--all-entries))))

(defun lp--contract-all (arg)
  "Contract all entries.
With prefix arg, contract only those that were created by self."
  (interactive "P")
  (let ((hidden-list (list lyskom-pers-no 0)))
    (mapcar (lambda (entry)
              (when (and (or (null arg)
                             (memq (membership->created-by
                                    (lp--entry->membership entry))
                                   hidden-list))
                         (not (eq (lp--entry->state entry) 'contracted)))
                (set-lp--entry->state entry 'contracted)
                (lp--redraw-entry entry)))
            (lp--all-entries))))

1334
1335
1336
1337
1338
(defun lp--quit ()
  "Remove the membership buffer and quit"
  (interactive)
  (lyskom-undisplay-buffer))

1339

1340

David Byers's avatar
David Byers committed
1341
1342
1343
1344
(defun lp--scroll-advice (fn)
  (let ((cur (current-column)))
    (funcall fn)
    (when (and (boundp 'lyskom-buffer-category)
1345
               (eq lyskom-buffer-category 'prioritize))
David Byers's avatar
David Byers committed
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
      (let ((last-entry (lp--get-last-visible-entry ))
            (first-entry (lp--next-visible-entry 0)))
        (cond ((null last-entry)
               (goto-char lp--list-end-marker))
              ((> (point) (lp--entry->end-marker last-entry))
               (goto-char (lp--entry->end-marker last-entry))
               (when (> (current-column) cur)
                 (beginning-of-line)
                 (forward-char cur)))

              ((< (point) (lp--entry->start-marker
                           (lp--get-entry first-entry)))
               (goto-char (lp--entry->start-marker 
                           (lp--get-entry first-entry)))
               (end-of-line)
               (when (> (current-column) cur)
                 (beginning-of-line)
                 (forward-char cur))))))))
David Byers's avatar
David Byers committed
1364
1365
1366
1367
1368
1369
1370
1371
1372

(defadvice scroll-up-command (around lp--scroll-up-advice activate)