Function: rmail-set-label

rmail-set-label is a byte-compiled function defined in rmailkwd.el.gz.

Signature

(rmail-set-label LABEL STATE &optional MSG)

Documentation

Set LABEL as present or absent according to STATE in message MSG.

LABEL may be a symbol or string.

Source Code

;; Defined in /usr/src/emacs/lisp/mail/rmailkwd.el.gz
(defun rmail-set-label (label state &optional msg)
  "Set LABEL as present or absent according to STATE in message MSG.
LABEL may be a symbol or string."
  (or (stringp label) (setq label (symbol-name label)))
  (if (string-search "," label)
      (error "More than one label specified"))
  (with-current-buffer rmail-buffer
    (rmail-maybe-set-message-counters)
    (if (zerop (or msg (setq msg rmail-current-message)))
	(error "No message"))
    ;; Force recalculation of summary for this message.
    (aset rmail-summary-vector (1- msg) nil)
    (let (attr-index)
      ;; Is this label an attribute?
      (dotimes (i (length rmail-attr-array))
	(if (string= (cadr (aref rmail-attr-array i)) label)
	    (setq attr-index i)))
      (if attr-index
	  ;; If so, set it as an attribute.
	  (rmail-set-attribute attr-index state msg)
	;; Is this keyword already present in msg's keyword list?
	(let* ((header (rmail-get-keywords msg))
	       (regexp (concat ", " (regexp-quote label) ","))
	       (present (not (null
			      (string-match regexp (concat ", " header ","))))))
	  ;; If current state is not correct,
	  (unless (eq present state)
	    ;; either add it or delete it.
	    (rmail-set-header
	     rmail-keyword-header msg
	     (if state
		 ;; Add this keyword at the end.
		 (if (and header (not (string= header "")))
		     (concat header ", " label)
		   label)
	       ;; Delete this keyword.
	       (let ((before (substring header 0
					(max 0 (- (match-beginning 0) 2))))
		     (after (substring header
				       (min (length header)
					    (- (match-end 0) 1)))))
		 (cond ((string= before "")
			;; If before and after both empty, delete the header.
			(unless (string= after "")
			  after))
		       ((string= after "")
			before)
		       (t (concat before ", " after))))))))))
    (if (rmail-summary-exists)
	(rmail-select-summary (rmail-summary-update-line msg)))
    (if (= msg rmail-current-message)
	(rmail-display-labels))))