5c14f7efce67a832875c2e2511341a2a32c714be
[emacs.git] / custom / jao-anything.el
1
2 (jao-load-path "anything")
3
4 (require 'jao-mail)
5
6 (defconst anything-jao--tracker/mail-rx
7   (format "%s\\(.+\\)/\\([0-9]+\\)$" (regexp-quote mail-source-directory)))
8
9 (defconst anything-jao--tracker/music-rx "^/home/jao/var/lib/music")
10
11 (setq anything-jao--tracker-substs
12   '(("amp" . "&")
13     ("apos" . "'")
14     ("quot" . "'")
15     ("gt" . ">")
16     ("lt" . "<")))
17
18 (defsubst anything-jao--tracker-hilite (desc)
19   (replace-regexp-in-string "&\\([^;]+?\\);"
20                             (lambda (s)
21                               (or (cdr (assoc (match-string 1 s)
22                                               anything-jao--tracker-substs))
23                                   s))
24                             (replace-regexp-in-string "<b>\\(.+?\\)</b>"
25                                                       (lambda (s)
26                                                         (propertize (match-string 1 s)
27                                                                     'face 'bold))
28                                                       desc)))
29
30 (defsubst anything-jao--tracker-title (title)
31   (when title (put-text-property 0 (length title) 'face 'italic title))
32   (or title ""))
33
34 (defun anything-jao--tracker-candidate (item)
35   (when (string-match "\\[\\([^]]+\\)\\] *\\(.+\\)?$" item)
36     (let* ((path (match-string 1 item))
37            (desc (match-string 2 item))
38            (desc (if desc (format " (%s)" (anything-jao--tracker-hilite desc)) "")))
39       (cond ((string-match anything-jao--tracker/mail-rx path)
40              (let* ((mbox (replace-regexp-in-string "/" "." (match-string 1 path)))
41                     (mnumber (match-string 2 path))
42                     (name (anything-jao--tracker-title (format "%s in %s" mnumber mbox))))
43                (cons (format "Mail: %s%s" name desc)
44                      (format "gnus://nnml:%s/%s" mbox mnumber))))
45             ((string-match-p anything-jao--tracker/music-rx path)
46              (cons (format "Music: %s" (anything-jao--tracker-title
47                                         (file-name-sans-extension (file-name-nondirectory path))))
48                    (format "music:%s" path)))
49             (t
50              (let* ((kind (upcase (or (file-name-extension path) "dir")))
51                     (name (file-name-sans-extension (file-name-nondirectory path)))
52                     (name (anything-jao--tracker-title
53                            (subst-char-in-string ?- ?\  (capitalize name)))))
54                (cons (format "%s: %s%s" kind name desc)
55                      (format "file:%s" path))))))))
56
57 (defun anything-jao--tracker< (l r) (string< (car l) (car r)))
58
59 (defun anything-jao--tracker-match (lines)
60   (let (result)
61     (dolist (l lines (sort result 'anything-jao--tracker<))
62       (let ((candidate (anything-jao--tracker-candidate l)))
63         (when candidate (push candidate result))))))
64
65 (defvar anything-jao-tracker-limit "30")
66
67 (defvar anything-jao-source-tracker-search
68   '((name . "Tracker Search")
69     (candidates . (lambda ()
70                     (start-process "tracker-search-process" nil
71                                    "jao-tracker"
72                                    "-l" anything-jao-tracker-limit
73                                    anything-pattern)))
74     (candidate-transformer . anything-jao--tracker-match)
75     (action ("Open" . (lambda (url) (browse-url url anything-pattern))))
76     (requires-pattern . 3)
77     (delayed))
78   "Source for retrieving files matching the current input pattern
79 with the tracker desktop search.")
80
81 (defun anything-jao-tracker ()
82   (interactive)
83   (anything-other-buffer 'anything-jao-source-tracker-search "*anything tracker*"))
84
85 (defun jao-anything (arg)
86   (interactive "P")
87   (let ((sources '(anything-c-source-recentf
88                    anything-c-source-buffers+
89                    anything-jao-source-tracker-search
90                    anything-c-source-emms-streams
91                    anything-c-source-ffap-guesser
92                    anything-c-source-file-name-history
93                    anything-c-source-file-cache
94                    anything-c-source-info-cl
95                    anything-c-source-info-elisp
96                    anything-c-source-info-pages
97                    anything-c-source-man-pages
98                    ;;        anything-c-source-lacarte
99                    anything-c-source-locate
100                    anything-c-source-kill-ring
101                    anything-c-source-elisp-library-scan
102                    anything-c-source-buffer-not-found
103                    anything-c-source-bbdb
104                    )))
105     (anything sources)))
106
107 (and (require 'anything nil t)
108      (require 'anything-config nil t))
109
110
111 (provide 'jao-anything)