;; (assert (string= (tags-sort-tags-string "zee bee cee") "bee cee zee") t "Tags not sorted.") (defun tagging-sort-tags-string (string) "Return an alphabetically-sorted list of words in this string." (save-match-data (mapconcat 'identity (sort (mapcar 'downcase (split-string string)) 'string<) " "))) (defun tagging-sort-tags () "Find the first line beginning with Tags: tag tag tag and sort the tags alphabetically." (interactive) (goto-char (point-min)) (when (re-search-forward "^Tags: \\(.+\\)" nil t) (replace-match (tagging-sort-tags-string (match-string 1)) t t nil 1))) ;; (assert (string-match (tags-make-emacs-search-regexp '("bee")) "bee cee")) ;; (assert (string-match (tags-make-emacs-search-regexp '("cee")) "bee cee")) ;; (assert (string-match (tags-make-emacs-search-regexp '("cee" "bee")) "bee cee")) ;; (assert (null (string-match (tags-make-emacs-search-regexp '("zee" "cee")) "bee cee"))) ;; (assert (null (string-match (tags-make-emacs-search-regexp '("cee" "b")) "bee cee"))) (defun tagging-make-emacs-search-regexp (tags) "Convert TAGS to a regular expression." (concat "\\b" (mapconcat 'downcase (sort tags 'string<) "\\b+\\(.+\\b\\)*") "\\b")) ;;(tagging-grep-directory '("book") "~/howm") ;;(tagging-make-grep-search-regexp '("paper")) (defun tagging-grep-directory (tags directory) "Return a list files matching TAGS in DIRECTORY." (when (stringp tags) (setq tags (split-string tags))) (setq tags (sort tags 'string<)) (split-string (shell-command-to-string (concat "~/bin/find-tag.pl " directory " " (mapconcat 'shell-quote-argument tags " "))) "\n" t)) ;; (assert (equal (tagging-tag-cloud-from-list '("a" "b" "a")) '(("a" . 2) ("b" . 1)))) (defun tagging-tag-cloud-from-list (tags) "Create a tagcloud with elements (tag-name . count) from TAGS. TAGS is a list of tag strings." (let (result lookup) (while tags (setq lookup (assoc (downcase (car tags)) result)) (if lookup (setcdr lookup (1+ (cdr lookup))) (setq result (cons (cons (car tags) 1) result))) (setq tags (cdr tags))) (sort result (lambda (a b) (< (cdr b) (cdr a)))))) ;;(tagging-get-tagcloud-from-files (howm-files-in-directory howm-directory)) (defun tagging-get-tagcloud-from-files (files) "Return the tagcloud for this set of FILES." (let (tags) (while files (let ((tag-string (shell-command-to-string (concat "grep Tags: " (shell-quote-argument (car files)))))) (when (and tag-string (string-match "^Tags:\\(.+\\)" tag-string)) (setq tags (append (split-string (match-string 1 tag-string)) tags)))) (setq files (cdr files))) (tagging-tag-cloud-from-list tags)))