File: vm-undo.el

package info (click to toggle)
vm 8.2.0b-11.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,220 kB
  • sloc: lisp: 50,545; sh: 560; makefile: 452; ansic: 298; python: 112
file content (688 lines) | stat: -rwxr-xr-x 26,374 bytes parent folder | download | duplicates (6)
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
;;; vm-undo.el --- Commands to undo message attribute changes in VM
;;
;; This file is part of VM
;;
;; Copyright (C) 1989-1995 Kyle E. Jones
;; Copyright (C) 2003-2006 Robert Widhopf-Fenk
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License along
;; with this program; if not, write to the Free Software Foundation, Inc.,
;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

;;; Code:

(provide 'vm-undo)

(eval-when-compile
  (require 'vm-misc)
  (require 'vm-menu)
  (require 'vm-minibuf)
  (require 'vm-folder)
  (require 'vm-summary)
  (require 'vm-window)
  (require 'vm-page)
  (require 'vm-motion)
  )

;; vm-undo-record-list is a buffer-local-variable containing
;; undo-records.
;; An undo-record has:
;; - action
;; - message
;; - args

(defun vm-undo-boundary ()
  (if (car vm-undo-record-list)
      (setq vm-undo-record-list (cons nil vm-undo-record-list))))

(defun vm-add-undo-boundaries ()
  (save-excursion
    (mapatoms (function
	       (lambda (b)
		 (setq b (get-buffer (symbol-name b)))
		 (when b
		   (set-buffer b)
		   (vm-undo-boundary))))
	      vm-buffers-needing-undo-boundaries)
    (fillarray vm-buffers-needing-undo-boundaries 0)))

(defun vm-clear-expunge-invalidated-undos ()
  (let ((udp vm-undo-record-list) udp-prev)
    (while udp
      (cond ((null (car udp))
	     (setq udp-prev udp))
	    ((and (not (eq (car (car udp)) 'vm-set-buffer-modified-p))
		  ;; delete flag == expunged is the
		  ;; indicator of an expunged message
		  (eq (vm-deleted-flag (car (cdr (car udp)))) 'expunged))
	     (cond (udp-prev (setcdr udp-prev (cdr udp)))
		   (t (setq vm-undo-record-list (cdr udp)))))
	    (t (setq udp-prev udp)))
      (setq udp (cdr udp))))
  (vm-clear-modification-flag-undos))
	    
(defun vm-clear-virtual-quit-invalidated-undos ()
  (let ((udp vm-undo-record-list) udp-prev)
    (while udp
      (cond ((null (car udp))
	     (setq udp-prev udp))
	    ((and (not (eq (car (car udp)) 'vm-set-buffer-modified-p))
		  ;; message-id-number == "Q" is the
		  ;; indicator of a dead message
		  (equal (vm-message-id-number-of (car (cdr (car udp)))) "Q"))
	     (cond (udp-prev (setcdr udp-prev (cdr udp)))
		   (t (setq vm-undo-record-list (cdr udp)))))
	    (t (setq udp-prev udp)))
      (setq udp (cdr udp))))
  (vm-clear-modification-flag-undos))
	    
(defun vm-clear-modification-flag-undos ()
  (let ((udp vm-undo-record-list) udp-prev)
    (while udp
      (cond ((null (car udp))
	     (setq udp-prev udp))
	    ((eq (car (car udp)) 'vm-set-buffer-modified-p)
	     (cond (udp-prev (setcdr udp-prev (cdr udp)))
		   (t (setq vm-undo-record-list (cdr udp)))))
	    (t (setq udp-prev udp)))
      (setq udp (cdr udp)))
    (vm-squeeze-consecutive-undo-boundaries)))

;; squeeze out consecutive record separators left by record deletions
(defun vm-squeeze-consecutive-undo-boundaries ()
  (let ((udp vm-undo-record-list) udp-prev)
    (while udp
      (cond ((and (null (car udp)) udp-prev (null (car udp-prev)))
	     (setcdr udp-prev (cdr udp)))
	    (t (setq udp-prev udp)))
      (setq udp (cdr udp)))
    (if (equal '(nil) vm-undo-record-list)
	(setq vm-undo-record-list nil)))
  ;; for the Undo button on the menubar, if present
  (when (and (null vm-undo-record-list)
	     (vm-menu-support-possible-p)
	     (vm-menu-xemacs-menus-p))
    (vm-menu-set-menubar-dirty-flag)))
	    
(defun vm-undo-record (sexp)
  ;; for the Undo button on the menubar, if present
  (when (and (null vm-undo-record-list)
	     (vm-menu-support-possible-p)
	     (vm-menu-xemacs-menus-p))
    (vm-menu-set-menubar-dirty-flag))
  (setq vm-undo-record-list (cons sexp vm-undo-record-list)))

(defun vm-undo-describe (record)
  (let ((cell
	 (assq (car record)
	       '((vm-set-new-flag "new" "old")
		 (vm-set-unread-flag "unread" "read")
		 (vm-set-deleted-flag "deleted" "undeleted")
		 (vm-set-forwarded-flag "forwarded" "unforwarded")
		 (vm-set-replied-flag "answered" "unanswered")
		 (vm-set-redistributed-flag "redistributed" "unredistributed")
		 (vm-set-filed-flag "filed" "unfiled")
		 (vm-set-written-flag "written" "unwritten"))))
	(m (nth 1 record))
	labels)
    (cond (cell
	   (vm-inform 1 "VM Undo! %s/%s %s -> %s"
		    (buffer-name (vm-buffer-of m))
		    (vm-number-of m)
		    (if (nth 2 record)
			(nth 2 cell)
		      (nth 1 cell))
		    (if (nth 2 record)
			(nth 1 cell)
		      (nth 2 cell))))
	  ((eq (car cell) 'vm-set-labels)
	   (setq labels (nth 2 record))
	   (vm-inform 1 "VM Undo! %s/%s %s%s"
		    (buffer-name (vm-buffer-of m))
		    (vm-number-of m)
		    (if (null labels)
			"lost all its labels"
		      "labels set to ")
		    (if (null labels)
			""
		      (mapconcat 'identity labels ", ")))))))

(defun vm-undo-set-message-pointer (record)
  (if (and (not (eq (car record) 'vm-set-buffer-modified-p))
	   (not (eq (nth 1 record) vm-message-pointer)))
      (progn
	(vm-record-and-change-message-pointer
	 vm-message-pointer
	 (or (cdr (vm-reverse-link-of (nth 1 record)))
	     vm-message-list))
	;; make folder read-only to avoid modifications when we
	;; do this.
	(let ((vm-folder-read-only t))
	  (vm-present-current-message)))))

;;;###autoload
(defun vm-undo ()
  "Undo last change to message attributes in the current folder.
Consecutive invocations of this command cause sequentially earlier
changes to be undone.  After an intervening command between undos,
the undos themselves become undoable."
  (interactive)
  (vm-select-folder-buffer-and-validate 0 (vm-interactive-p))
  (vm-error-if-folder-read-only)
  (vm-display nil nil '(vm-undo) '(vm-undo))
  (let ((modified (buffer-modified-p)))
    (unless (eq last-command 'vm-undo)
      (setq vm-undo-record-pointer vm-undo-record-list))
    (unless vm-undo-record-pointer
      (error "No further VM undo information available"))
    ;; skip current record boundary
    (setq vm-undo-record-pointer (cdr vm-undo-record-pointer))
    (while (car vm-undo-record-pointer)
      (vm-undo-set-message-pointer (car vm-undo-record-pointer))
      (vm-undo-describe (car vm-undo-record-pointer))
      (eval (car vm-undo-record-pointer))
      (setq vm-undo-record-pointer (cdr vm-undo-record-pointer)))
    (when (and modified (not (buffer-modified-p)))
      (delete-auto-save-file-if-necessary))
    (vm-update-summary-and-mode-line)))

;;;###autoload
(defun vm-set-message-attributes (string count)
  "Set message attributes.
Use this command to change attributes like `deleted' or
`replied'.  Interactively you will be prompted for the attributes
to be changed, and only the attributes you enter will be altered.
You can use completion to expand the attribute names.  The names
should be entered as a space separated list.

A numeric prefix argument COUNT causes the current message and
the next COUNT-1 message to have their attributes altered.  A
negative COUNT arg causes the current message and the previous
COUNT-1 messages to be altered.  COUNT defaults to one."
  (interactive
   (let ((last-command last-command)
	 (this-command this-command))
     ;; so the user can see what message they are about to
     ;; modify.
     (vm-follow-summary-cursor)
     (list
      (vm-read-string "Set attributes: " vm-supported-attribute-names t)
      (prefix-numeric-value current-prefix-arg))))
  (vm-follow-summary-cursor)
  (vm-select-folder-buffer-and-validate 1 (vm-interactive-p))
  (vm-error-if-folder-read-only)
  (vm-display nil nil '(vm-set-message-attributes)
	      '(vm-set-message-attributes))
  (let ((name-list (vm-parse string "[ \t]*\\([^ \t]+\\)"))
	(m-list (vm-select-operable-messages 
		 count (vm-interactive-p) "Set attributes of"))
	n-list name m)
    (while m-list
      (setq m (car m-list)
	    n-list name-list)
      (while n-list
	(setq name (car n-list))
	(cond ((string= name "new")
	       (vm-set-new-flag m t))
	      ((string= name "recent")
	       (vm-set-new-flag m t))
	      ((string= name "unread")
	       (vm-set-unread-flag m t))
	      ((string= name "unseen")
	       (vm-set-unread-flag m t))
	      ((string= name "read")
	       (vm-set-new-flag m nil)
	       (vm-set-unread-flag m nil))
	      ((string= name "deleted")
	       (vm-set-deleted-flag m t))
	      ((string= name "replied")
	       (vm-set-replied-flag m t))
	      ((string= name "answered")
	       (vm-set-replied-flag m t))
	      ((string= name "forwarded")
	       (vm-set-forwarded-flag m t))
	      ((string= name "redistributed")
	       (vm-set-redistributed-flag m t))
	      ((string= name "filed")
	       (vm-set-filed-flag m t))
	      ((string= name "written")
	       (vm-set-written-flag m t))
	      ((string= name "edited")
	       (vm-set-edited-flag-of m t))
	      ((string= name "undeleted")
	       (vm-set-deleted-flag m nil))
	      ((string= name "unreplied")
	       (vm-set-replied-flag m nil))
	      ((string= name "unanswered")
	       (vm-set-replied-flag m nil))
	      ((string= name "unforwarded")
	       (vm-set-forwarded-flag m nil))
	      ((string= name "unredistributed")
	       (vm-set-redistributed-flag m nil))
	      ((string= name "unfiled")
	       (vm-set-filed-flag m nil))
	      ((string= name "unwritten")
	       (vm-set-written-flag m nil))
	      ((string= name "unedited")
	       (vm-set-edited-flag-of m nil)))
	(setq n-list (cdr n-list)))
      (setq m-list (cdr m-list)))
    (vm-update-summary-and-mode-line)))

;;;###autoload
(defun vm-add-message-labels (string count)
  "Attach some labels to a message.
These are arbitrary user-defined labels, not to be confused with
message attributes like `new' and `deleted'.  Interactively you
will be prompted for the labels to be added.  You can use
completion to expand the label names, with the completion list
being all the labels that have ever been used in this folder.
The names should be entered as a space separated list.  Label
names are compared case-insensitively.

A numeric prefix argument COUNT causes the current message and
the next COUNT-1 message to have the labels added.  A
negative COUNT arg causes the current message and the previous
COUNT-1 messages to be altered.  COUNT defaults to one."
  (interactive
   (let ((last-command last-command)
	 (this-command this-command)
	 (vm-completion-auto-correct nil)
	 (completion-ignore-case t))
     ;; so the user can see what message they are about to
     ;; modify.
     (save-current-buffer
       (vm-follow-summary-cursor)
       (vm-select-folder-buffer)
       (list
	(vm-read-string "Add labels: "
			(vm-obarray-to-string-list vm-label-obarray) t)
	(prefix-numeric-value current-prefix-arg)))))
  (let ((m-list nil)
	(ignored-labels nil))
    (vm-follow-summary-cursor)
    (vm-select-folder-buffer-and-validate 1 (vm-interactive-p))
    (vm-error-if-folder-read-only)
    (setq m-list (vm-select-operable-messages
		  count (vm-interactive-p) "Add labels to"))
    (setq ignored-labels 
	  (vm-add-or-delete-message-labels string m-list 'all))
    (if ignored-labels
	(vm-inform 1 "Label %s could not be added" string))))

;;;###autoload
(defun vm-add-existing-message-labels (string count)
  "Attach some already existing labels to a message.
Only labels that are currently attached to some message in this
folder or labels that have previously been attached to messages
in this folder will be added.  Other labels will be silently
ignored.

These are arbitrary user-defined labels, not to be confused with
message attributes like `new' and `deleted'.  Interactively you
will be prompted for the labels to be added.  You can use
completion to expand the label names, with the completion list
being all the labels that have ever been used in this folder.
The names should be entered as a space separated list.  Label
names are compared case-insensitively.

A numeric prefix argument COUNT causes the current message and
the next COUNT-1 messages to have the labels added.  A
negative COUNT arg causes the current message and the previous
COUNT-1 messages to be altered.  COUNT defaults to one."
  (interactive
   (let ((last-command last-command)
	 (this-command this-command)
	 (vm-completion-auto-correct nil)
	 (completion-ignore-case t))
     ;; so the user can see what message they are about to
     ;; modify.
     (save-current-buffer
     (vm-follow-summary-cursor)
     (vm-select-folder-buffer)
     (list
      (vm-read-string "Add labels: "
		      (vm-obarray-to-string-list vm-label-obarray) t)
      (prefix-numeric-value current-prefix-arg)))))
  (vm-follow-summary-cursor)
  (vm-select-folder-buffer-and-validate 1 (vm-interactive-p))
  (vm-error-if-folder-read-only)
  (let* ((m-list (vm-select-operable-messages
		  count (vm-interactive-p) "Add labels to"))
	(ignored-labels
	 (vm-add-or-delete-message-labels string m-list 'existing-only)))
    (if ignored-labels
	(progn
	  (set-buffer (get-buffer-create "*Ignored Labels*"))
	  (erase-buffer)
	  (insert "These labels do not exist and were not added:\n\n")
	  (while ignored-labels
	    (insert (car ignored-labels) "\n")
	    (setq ignored-labels (cdr ignored-labels)))
	  (display-buffer (current-buffer))))))

;;;###autoload
(defun vm-delete-message-labels (string count)
  "Delete some labels from a message.
These are arbitrary user-defined labels, not to be confused with
message attributes like `new' and `deleted'.  Interactively you
will be prompted for the labels to be deleted.  You can use
completion to expand the label names, with the completion list
being all the labels that have ever been used in this folder.
The names should be entered as a space separated list.  Label
names are compared case-insensitively.

A numeric prefix argument COUNT causes the current message and
the next COUNT-1 message to have the labels deleted.  A
negative COUNT arg causes the current message and the previous
COUNT-1 messages to be altered.  COUNT defaults to one."
  (interactive
   (let ((last-command last-command)
	 (this-command this-command)
	 (vm-completion-auto-correct nil)
	 (completion-ignore-case t))
     ;; so the user can see what message they are about to
     ;; modify.
     (save-current-buffer
     (vm-follow-summary-cursor)
     (vm-select-folder-buffer)
     (list
      (vm-read-string "Delete labels: "
		      (vm-obarray-to-string-list vm-label-obarray) t)
      (prefix-numeric-value current-prefix-arg)))))
  (vm-follow-summary-cursor)
  (vm-select-folder-buffer-and-validate 1 (vm-interactive-p))
  (vm-error-if-folder-read-only)
  (let ((m-list (vm-select-operable-messages
		 count (vm-interactive-p) "Delete labels to")))
    (vm-add-or-delete-message-labels string m-list nil)))

(defun vm-add-or-delete-message-labels (string m-list add)
  "Add or delete the labels given in STRING for all messages in
M-LIST.  The third parameter ADD is one of:

nil	       delete the label
'all           add the label in all cases
'existing-only add the label only if it is already existing in the folder
							USR, 2010-12-20
"
  (vm-display nil nil '(vm-add-message-labels vm-delete-message-labels)
	      (list this-command))
  (setq string (downcase string))
  (let ((action-labels (vm-parse string
"[\000-\040,\177-\377]*\\([^\000-\040,\177-\377]+\\)[\000-\040,\177-\377]*"))
	(ignored-labels nil)
	labels act-labels m mm-list)
    (when (and add m-list)
	(if (eq add 'all)
	    (progn
	      (setq act-labels action-labels)
	      (while act-labels
		(intern (car act-labels) vm-label-obarray)
		(setq act-labels (cdr act-labels))))
	  (let ((newlist nil))
	    (setq act-labels action-labels)
	    (while act-labels
	      (if (intern-soft (car act-labels) vm-label-obarray)
		  (setq newlist (cons (car act-labels) newlist))
		(setq ignored-labels (cons (car act-labels) ignored-labels)))
	      (setq act-labels (cdr act-labels)))
	    (setq action-labels newlist))))
    (unless action-labels
	(setq m-list nil))
    (while m-list
      (setq m (car m-list))
      (when (and add (vm-virtual-message-p m))
	(let ((labels action-labels))
	  (with-current-buffer (vm-buffer-of (vm-real-message-of m))
	    (while labels
	      (intern (car labels) vm-label-obarray)
	      (setq labels (cdr labels))))))
      (when add
	(dolist (mm (vm-virtual-messages-of m))
	  (let ((labels action-labels))
	    (when (buffer-name (vm-buffer-of mm))
	      (with-current-buffer (vm-buffer-of mm)
		(while labels
		  (intern (car labels) vm-label-obarray)
		  (setq labels (cdr labels))))))))
      (setq act-labels action-labels
	    labels (copy-sequence (vm-labels-of (car m-list))))
      (if add
	  (while act-labels
	    (setq labels (cons (car act-labels) labels)
		  act-labels (cdr act-labels)))
	(while act-labels
	  (setq labels (vm-delqual (car act-labels) labels)
		act-labels (cdr act-labels))))
      (when add
	(setq labels (vm-delete-duplicates labels)))
      (vm-set-labels (car m-list) labels)
      (vm-set-attribute-modflag-of (car m-list) t) ; added by USR
      (setq m-list (cdr m-list)))
    (vm-update-summary-and-mode-line)
    ignored-labels))

(defun vm-set-xxxx-flag (m flag norecord function attr-index)
  "A generic function to set the message flag of M at ATTR-INDEX to
  the value FLAG.  The argument FUNCTION tells the specific
  non-generic function that invoked this one.  A boolean flag is
  returned indicating success or failure of the operation.
The flag is also set for all the virtual messages mirroring M as well
  as the real message underlying M. 
Normally, a record of the change is kept for the purpose of undo, and
  the changed attributes are stuffed into the folder, but NORECORD
  suppresses all of this.                             USR 2010-04-06" 
  (let ((m-list nil) vmp)
    (when (and (not vm-folder-read-only)
	       (or (not (vm-virtual-messages-of m))
		   (not (with-current-buffer
			    (vm-buffer-of
			     (vm-real-message-of m))
			  vm-folder-read-only)))
	       ;; do nothing it is is already set 
	       (not (eq flag (aref (vm-attributes-of m) attr-index))))
      (unless norecord
	(dolist (v-m (cons (vm-real-message-of m) (vm-virtual-messages-of m)))
	  (if (eq (vm-attributes-of m) (vm-attributes-of v-m))
	      (setq m-list (cons v-m m-list))))
	(if (null m-list)
	    (setq m-list (cons m m-list)))
	(save-excursion
	  (dolist (mm m-list)
	    (when (buffer-name (vm-buffer-of mm))
	      (set-buffer (vm-buffer-of mm))
	      (cond ((not (buffer-modified-p))
		     (vm-mark-folder-modified-p (vm-buffer-of mm))
		     (vm-undo-record (list 'vm-set-buffer-modified-p nil))))
	      (vm-undo-record (list function mm (not flag)))
	      ;; (vm-undo-boundary)
	      (vm-increment vm-modification-counter)))))
      (aset (vm-attributes-of m) attr-index flag)
      (vm-mark-for-summary-update m)
      (unless norecord
	    (vm-set-attribute-modflag-of m t)
	    (if (eq vm-flush-interval t)
		(vm-stuff-virtual-message-data m)
	      (vm-set-stuff-flag-of m t)))
      ;; return success result
      t)))

(defun vm-set-xxxx-cached-data-flag (m flag norecord function attr-index)
  "A generic function to set the cached-data flag of M at ATTR-INDEX to
  the value FLAG.  The argument FUNCTION tells the specific
  non-generic function that invoked this one.
The flag is also set for all the virtual messages mirroring M as well
  as the real message underlying M. 
Normally, a record of the change is kept for the purpose of undo, and
  the changed attributes are stuffed into the folder, but NORECORD
  suppresses all of this.                             USR 2010-04-06" 
  (let ((m-list nil) vmp)
    (when
     (and (not vm-folder-read-only)
	   (or (not (vm-virtual-messages-of m))
	       (not (with-current-buffer
		       (vm-buffer-of
			 (vm-real-message-of m))
		      vm-folder-read-only)))
           ;; do nothing it is is already set 
           (not (eq flag (aref (vm-cached-data-of m) attr-index))))
     (unless norecord
	(dolist (v-m (cons (vm-real-message-of m) (vm-virtual-messages-of m)))
	  (if (eq (vm-cached-data-of m) (vm-cached-data-of v-m))
	      (setq m-list (cons v-m m-list))))
	(if (null m-list)
	    (setq m-list (cons m m-list)))
	(save-excursion
	  (dolist (mm m-list)
	    (when (buffer-name (vm-buffer-of mm))
	      (set-buffer (vm-buffer-of mm))
	      (cond ((not (buffer-modified-p))
		     (vm-mark-folder-modified-p (vm-buffer-of mm))
		     (vm-undo-record (list 'vm-set-buffer-modified-p nil))))
	      (vm-undo-record (list function mm (not flag)))
	      ;; (vm-undo-boundary)
	      (vm-increment vm-modification-counter)))))
      (aset (vm-cached-data-of m) attr-index flag)
      (vm-mark-for-summary-update m)
      (unless norecord
	    (vm-set-attribute-modflag-of m t)
	    (if (eq vm-flush-interval t)
		(vm-stuff-virtual-message-data m)
	      (vm-set-stuff-flag-of m t))))))


(defun vm-set-labels (m labels)
  "Set the message labels of M to the value LABELS (a list of
  strings). 
The labels are also set for all the virtual messages mirroring M as
  well as the real message underlying M. 
A record of the change is kept for the purpose of undo, and the
  changed attributes are stuffed into the folder.        USR 2010-04-06" 
  (let ((m-list nil)
	(old-labels (vm-labels-of m)))
    (cond
     ((and (not vm-folder-read-only)
	   (or (not (vm-virtual-messages-of m))
	       (not (save-excursion
		      (set-buffer
		       (vm-buffer-of
			 (vm-real-message-of m)))
		      vm-folder-read-only))))
      (dolist (v-m (cons (vm-real-message-of m) (vm-virtual-messages-of m)))
	(if (eq (vm-attributes-of m) (vm-attributes-of v-m))
	    (setq m-list (cons v-m m-list))))
      (if (null m-list)
	  (setq m-list (cons m m-list)))
      (save-excursion
	(dolist (mm m-list)
	  (when (buffer-name (vm-buffer-of mm))
	    (set-buffer (vm-buffer-of mm))
	    (cond ((not (buffer-modified-p))
		   (vm-mark-folder-modified-p (vm-buffer-of mm))
		   (vm-undo-record (list 'vm-set-buffer-modified-p nil))))
	    (vm-undo-record (list 'vm-set-labels m old-labels))
	    ;; (vm-undo-boundary)
	    (vm-increment vm-modification-counter))))
      (vm-set-labels-of m labels)
      (vm-set-label-string-of m nil)
      (vm-mark-for-summary-update m)
      (if (eq vm-flush-interval t)
	  (vm-stuff-virtual-message-data m)
	(vm-set-stuff-flag-of m t))))))


;; This flag is defunct, replaced by body-to-be-discarded.  USR, 2010-06-08
(defun vm-set-headers-to-be-retrieved-flag (m flag &optional norecord)
  nil)

(defun vm-set-body-to-be-discarded-flag (m flag &optional norecord)
  (vm-set-xxxx-cached-data-flag 
   m flag norecord 'vm-set-body-to-be-discarded-flag 21))

(defun vm-set-body-to-be-retrieved-flag (m flag &optional norecord)
  (vm-set-xxxx-cached-data-flag 
   m flag norecord 'vm-set-body-to-be-retrieved-flag 22))

(defun vm-set-new-flag (m flag &optional norecord)
  (vm-set-xxxx-flag m flag norecord 'vm-set-new-flag 0))

(defun vm-set-unread-flag (m flag &optional norecord)
  (vm-set-xxxx-flag m flag norecord 'vm-set-unread-flag 1))

(defun vm-set-deleted-flag (m flag &optional norecord)
  (vm-set-xxxx-flag m flag norecord 'vm-set-deleted-flag 2))

(defun vm-set-filed-flag (m flag &optional norecord)
  (vm-set-xxxx-flag m flag norecord 'vm-set-filed-flag 3))

(defun vm-set-replied-flag (m flag &optional norecord)
  (vm-set-xxxx-flag m flag norecord 'vm-set-replied-flag 4))

(defun vm-set-written-flag (m flag &optional norecord)
  (vm-set-xxxx-flag m flag norecord 'vm-set-written-flag 5))

(defun vm-set-forwarded-flag (m flag &optional norecord)
  (vm-set-xxxx-flag m flag norecord 'vm-set-forwarded-flag 6))

(defun vm-set-redistributed-flag (m flag &optional norecord)
  (vm-set-xxxx-flag m flag norecord 'vm-set-redistributed-flag 8))

(defun vm-set-flagged-flag (m flag &optional norecord)
  (vm-set-xxxx-flag m flag norecord 'vm-set-redistributed-flag 9))

(defun vm-set-folded-flag (m flag &optional norecord)
  (vm-set-xxxx-flag m flag norecord 'vm-set-redistributed-flag 10))

(defun vm-set-watched-flag (m flag &optional norecord)
  (vm-set-xxxx-flag m flag norecord 'vm-set-redistributed-flag 11))

(defun vm-set-ignored-flag (m flag &optional norecord)
  (vm-set-xxxx-flag m flag norecord 'vm-set-redistributed-flag 12))

(defun vm-set-read-receipt-flag (m flag &optional norecord)
  (vm-set-xxxx-flag m flag norecord 'vm-set-redistributed-flag 13))

(defun vm-set-read-receipt-sent-flag (m flag &optional norecord)
  (vm-set-xxxx-flag m flag norecord 'vm-set-redistributed-flag 14))

(defun vm-set-attachments-flag (m flag &optional norecord)
  (vm-set-xxxx-flag m flag norecord 'vm-set-redistributed-flag 15))

;; use these to avoid undo and summary update.
(defun vm-set-new-flag-of (m flag) (aset (aref m 2) 0 flag))
(defun vm-set-unread-flag-of (m flag) (aset (aref m 2) 1 flag))
(defun vm-set-deleted-flag-of (m flag) (aset (aref m 2) 2 flag))
(defun vm-set-filed-flag-of (m flag) (aset (aref m 2) 3 flag))
(defun vm-set-replied-flag-of (m flag) (aset (aref m 2) 4 flag))
(defun vm-set-written-flag-of (m flag) (aset (aref m 2) 5 flag))
(defun vm-set-forwarded-flag-of (m flag) (aset (aref m 2) 6 flag))
(defun vm-set-redistributed-flag-of (m flag) (aset (aref m 2) 8 flag))
(defun vm-set-flagged-flag-of (m flag) (aset (aref m 2) 9 flag))
(defun vm-set-folded-flag-of (m flag) (aset (aref m 2) 10 flag))
(defun vm-set-watched-flag-of (m flag) (aset (aref m 2) 11 flag))
(defun vm-set-ignored-flag-of (m flag) (aset (aref m 2) 12 flag))
(defun vm-set-read-receipt-flag-of (m flag) (aset (aref m 2) 13 flag))
(defun vm-set-read-receipt-sent-flag-of (m flag) (aset (aref m 2) 14 flag))
(defun vm-set-attachments-flag-of (m flag) (aset (aref m 2) 15 flag))

;; this is solely for the use of vm-stuff-message-data and
;; appears here only because this function should be grouped with
;; others of its kind for maintenance purposes.
(defun vm-set-deleted-flag-in-vector (v flag)
  (aset v 2 flag))
;; ditto.  this is for vm-read-attributes.
(defun vm-set-new-flag-in-vector (v flag)
  (aset v 0 flag))

;;; vm-undo.el ends here