Function: mh-normalize-folder-name

mh-normalize-folder-name is a byte-compiled function defined in mh-utils.el.gz.

Signature

(mh-normalize-folder-name FOLDER &optional EMPTY-STRING-OKAY DONT-REMOVE-TRAILING-SLASH RETURN-NIL-IF-FOLDER-EMPTY)

Documentation

Normalizes FOLDER name.

Makes sure that two / characters never occur next to each other. Also all occurrences of .. and . are suitably processed. So "+inbox/../news" will be normalized to "+news".

If optional argument EMPTY-STRING-OKAY is nil then a + is added at the front if FOLDER lacks one. If non-nil and FOLDER is the empty string then nothing is added.

If optional argument DONT-REMOVE-TRAILING-SLASH is non-nil then a trailing / if present is retained (if present), otherwise it is removed.

If optional argument RETURN-NIL-IF-FOLDER-EMPTY is non-nil, then return nil if FOLDER is "" or "+". This is useful when normalizing the folder for the folders command which displays the directories in / if passed "+". This is usually not desired. If this argument is non-nil, then EMPTY-STRING-OKAY has no effect.

Source Code

;; Defined in /usr/src/emacs/lisp/mh-e/mh-utils.el.gz
(defun mh-normalize-folder-name (folder &optional empty-string-okay
                                        dont-remove-trailing-slash
                                        return-nil-if-folder-empty)
  "Normalizes FOLDER name.

Makes sure that two `/' characters never occur next to each
other. Also all occurrences of `..' and `.' are suitably
processed. So \"+inbox/../news\" will be normalized to \"+news\".

If optional argument EMPTY-STRING-OKAY is nil then a `+' is added
at the front if FOLDER lacks one. If non-nil and FOLDER is the
empty string then nothing is added.

If optional argument DONT-REMOVE-TRAILING-SLASH is non-nil then a
trailing `/' if present is retained (if present), otherwise it is
removed.

If optional argument RETURN-NIL-IF-FOLDER-EMPTY is non-nil, then
return nil if FOLDER is \"\" or \"+\". This is useful when
normalizing the folder for the `folders' command which displays
the directories in / if passed \"+\". This is usually not
desired. If this argument is non-nil, then EMPTY-STRING-OKAY has
no effect."
  (cond
   ((if (and (or (equal folder "+") (equal folder ""))
             return-nil-if-folder-empty)
        (setq folder nil)))
   ((stringp folder)
    ;; Replace two or more consecutive '/' characters with a single '/'
    (while (string-match "//" folder)
      (setq folder (replace-match "/" nil t folder)))
    (let* ((length (length folder))
           (trailing-slash-present (and (> length 0)
                                        (equal (aref folder (1- length)) ?/)))
           (leading-slash-present (and (> length 0)
                                       (equal (aref folder 0) ?/))))
      (when (and (> length 0) (equal (aref folder 0) ?@)
                 (stringp mh-current-folder-name))
        (setq folder (format "%s/%s/" mh-current-folder-name
                             (substring folder 1))))
      ;; XXX: Purge empty strings from the list that split-string
      ;; returns. In the code it is assumed that the components list
      ;; has no empty strings.
      (let ((components (delete "" (split-string folder "/")))
            (result ()))
        ;; Remove .. and . from the pathname.
        (dolist (component components)
          (cond ((and (equal component "..") result)
                 (pop result))
                ((equal component ".."))
                ((equal component "."))
                (t (push component result))))
        (setq folder "")
        (dolist (component result)
          (setq folder (concat component "/" folder)))
        ;; Remove trailing '/' if needed.
        (unless (and trailing-slash-present dont-remove-trailing-slash)
          (when (not (equal folder ""))
            (setq folder (substring folder 0 (1- (length folder))))))
        (when leading-slash-present
          (setq folder (concat "/" folder)))))
    (cond ((and empty-string-okay (equal folder "")))
          ((equal folder "")
           (setq folder "+"))
          ((not (equal (aref folder 0) ?+))
           (setq folder (concat "+" folder))))))
  folder)