* lisp/dired-aux.el (dired-diff): Remove (require 'diff) since
[emacs.git] / lisp / net / xesam.el
blob585e5eed52d73bcd4d340ad891cb9ac4e5161ca6
1 ;;; xesam.el --- Xesam interface to search engines.
3 ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: tools, hypermedia
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This package provides an interface to Xesam, a D-Bus based "eXtEnsible
26 ;; Search And Metadata specification". It has been tested with
28 ;; xesam-glib 0.3.4, xesam-tools 0.6.1
29 ;; beagle 0.3.7, beagle-xesam 0.2
30 ;; strigi 0.5.11
32 ;; The precondition for this package is a D-Bus aware Emacs. This is
33 ;; configured per default, when Emacs is built on a machine running
34 ;; D-Bus. Furthermore, there must be at least one search engine
35 ;; running, which supports the Xesam interface. Beagle and strigi have
36 ;; been tested; tracker, pinot and recoll are also said to support
37 ;; Xesam. You can check the existence of such a search engine by
39 ;; (dbus-list-queued-owners :session "org.freedesktop.xesam.searcher")
41 ;; In order to start a search, you must load xesam.el:
43 ;; (require 'xesam)
45 ;; xesam.el supports two types of queries, which are explained *very* short:
47 ;; * Full text queries. Just search keys shall be given, like
49 ;; hello world
51 ;; A full text query in xesam.el is restricted to files.
53 ;; * Xesam End User Search Language queries. The Xesam query language
54 ;; is described at <http://xesam.org/main/XesamUserSearchLanguage>,
55 ;; which must be consulted for the whole features.
57 ;; A query string consists of search keys, collectors, selectors,
58 ;; and phrases. Search keys are words like in a full text query:
60 ;; hello word
62 ;; A selector is a tuple <keyword><relation>. <keyword> can be any
63 ;; predefined Xesam keyword, the most common keywords are "ext"
64 ;; (file name extension), "format " (mime type), "tag" (user
65 ;; keywords) and "type" (types of items, like "audio", "file",
66 ;; "picture", "attachment"). <relation> is a comparison to a value,
67 ;; which must be a string (relation ":" or "=") or number (relation
68 ;; "<=", ">=", "<", ">"):
70 ;; type:attachment ext=el
72 ;; A collector is one of the items "AND", "and", "&&", "OR", "or",
73 ;; "||", or "-". The default collector on multiple terms is "AND";
74 ;; "-" means "AND NOT".
76 ;; albinus -type:file
78 ;; A phrase is a string enclosed in quotes, with appended modifiers
79 ;; (single letters). Examples of modifiers are "c" (case
80 ;; sensitive), "C" (case insensitive), "e" (exact match), "r"
81 ;; (regular expression):
83 ;; "Hello world"c
85 ;; You can customize, whether you want to apply a Xesam user query, or
86 ;; a full text query. Note, that not every search engine supports
87 ;; both query types.
89 ;; (setq xesam-query-type 'fulltext-query)
91 ;; Another option to be customized is the number of hits to be
92 ;; presented at once.
94 ;; (setq xesam-hits-per-page 50)
96 ;; A search can be started by the command
98 ;; M-x xesam-search
100 ;; When several search engines are registered, the engine to be used
101 ;; can be selected via minibuffer completion. Afterwards, the query
102 ;; shall be entered in the minibuffer.
104 ;; Search results are presented in a new buffer. This buffer has the
105 ;; major mode `xesam-mode', with the following keybindings:
107 ;; SPC `scroll-up'
108 ;; DEL `scroll-down'
109 ;; < `beginning-of-buffer'
110 ;; > `end-of-buffer'
111 ;; q `quit-window'
112 ;; z `kill-this-buffer'
113 ;; g `revert-buffer'
115 ;; The search results are represented by widgets. Navigation commands
116 ;; are the usual widget navigation commands:
118 ;; TAB `widget-forward'
119 ;; <backtab> `widget-backward'
121 ;; Applying RET, <down-mouse-1>, or <down-mouse-2> on a URL belonging
122 ;; to the widget, brings up more details of the search hit. The way,
123 ;; how this hit is presented, depends on the type of the hit. HTML
124 ;; files are opened via `browse-url'. Local files are opened in a new
125 ;; buffer, with highlighted search hits (highlighting can be toggled
126 ;; by `xesam-minor-mode' in that buffer).
128 ;;; Code:
130 (require 'dbus)
132 ;; Widgets are used to highlight the search results.
133 (require 'widget)
134 (require 'wid-edit)
136 ;; `run-at-time' is used in the signal handler.
137 (require 'timer)
139 ;; The default search field is "xesam:url". It must be inspected.
140 (require 'url)
142 (defgroup xesam nil
143 "Xesam compatible interface to search engines."
144 :group 'extensions
145 :group 'comm
146 :version "23.1")
148 (defcustom xesam-query-type 'user-query
149 "Xesam query language type."
150 :group 'xesam
151 :type '(choice
152 (const :tag "Xesam user query" user-query)
153 (const :tag "Xesam fulltext query" fulltext-query)))
155 (defcustom xesam-hits-per-page 20
156 "Number of search hits to be displayed in the result buffer."
157 :group 'xesam
158 :type 'integer)
160 (defface xesam-mode-line '((t :inherit mode-line-emphasis))
161 "Face to highlight mode line."
162 :group 'xesam)
164 (defface xesam-highlight '((t :inherit match))
165 "Face to highlight query entries.
166 It will be overlaid by `widget-documentation-face', so it shall
167 be different at least in one face property not set in that face."
168 :group 'xesam)
170 (defvar xesam-debug nil
171 "Insert debug information in the help echo.")
173 (defconst xesam-service-search "org.freedesktop.xesam.searcher"
174 "The D-Bus name used to talk to Xesam.")
176 (defconst xesam-path-search "/org/freedesktop/xesam/searcher/main"
177 "The D-Bus object path used to talk to Xesam.")
179 ;; Methods: "NewSession", "SetProperty", "GetProperty",
180 ;; "CloseSession", "NewSearch", "StartSearch", "GetHitCount",
181 ;; "GetHits", "GetHitData", "CloseSearch" and "GetState".
182 ;; Signals: "HitsAdded", "HitsRemoved", "HitsModified", "SearchDone"
183 ;; and "StateChanged".
184 (defconst xesam-interface-search "org.freedesktop.xesam.Search"
185 "The D-Bus Xesam search interface.")
187 (defconst xesam-all-fields
188 '("xesam:35mmEquivalent" "xesam:aimContactMedium" "xesam:aperture"
189 "xesam:aspectRatio" "xesam:attachmentEncoding" "xesam:attendee"
190 "xesam:audioBitrate" "xesam:audioChannels" "xesam:audioCodec"
191 "xesam:audioCodecType" "xesam:audioSampleFormat" "xesam:audioSampleRate"
192 "xesam:author" "xesam:bcc" "xesam:birthDate" "xesam:blogContactURL"
193 "xesam:cameraManufacturer" "xesam:cameraModel" "xesam:cc" "xesam:ccdWidth"
194 "xesam:cellPhoneNumber" "xesam:characterCount" "xesam:charset"
195 "xesam:colorCount" "xesam:colorSpace" "xesam:columnCount" "xesam:comment"
196 "xesam:commentCharacterCount" "xesam:conflicts" "xesam:contactMedium"
197 "xesam:contactName" "xesam:contactNick" "xesam:contactPhoto"
198 "xesam:contactURL" "xesam:contains" "xesam:contenKeyword"
199 "xesam:contentComment" "xesam:contentCreated" "xesam:contentModified"
200 "xesam:contentType" "xesam:contributor" "xesam:copyright" "xesam:creator"
201 "xesam:definesClass" "xesam:definesFunction" "xesam:definesGlobalVariable"
202 "xesam:deletionTime" "xesam:depends" "xesam:description" "xesam:device"
203 "xesam:disclaimer" "xesam:documentCategory" "xesam:duration"
204 "xesam:emailAddress" "xesam:eventEnd" "xesam:eventLocation"
205 "xesam:eventStart" "xesam:exposureBias" "xesam:exposureProgram"
206 "xesam:exposureTime" "xesam:faxPhoneNumber" "xesam:fileExtension"
207 "xesam:fileSystemType" "xesam:flashUsed" "xesam:focalLength"
208 "xesam:focusDistance" "xesam:formatSubtype" "xesam:frameCount"
209 "xesam:frameRate" "xesam:freeSpace" "xesam:gender" "xesam:generator"
210 "xesam:generatorOptions" "xesam:group" "xesam:hash" "xesam:hash"
211 "xesam:height" "xesam:homeEmailAddress" "xesam:homePhoneNumber"
212 "xesam:homePostalAddress" "xesam:homepageContactURL"
213 "xesam:horizontalResolution" "xesam:icqContactMedium" "xesam:id"
214 "xesam:imContactMedium" "xesam:interests" "xesam:interlaceMode"
215 "xesam:isEncrypted" "xesam:isImportant" "xesam:isInProgress"
216 "xesam:isPasswordProtected" "xesam:isRead" "xesam:isoEquivalent"
217 "xesam:jabberContactMedium" "xesam:keyword" "xesam:language" "xesam:legal"
218 "xesam:license" "xesam:licenseType" "xesam:lineCount" "xesam:links"
219 "xesam:mailingPostalAddress" "xesam:maintainer" "xesam:md5Hash"
220 "xesam:mediaCodec" "xesam:mediaCodecBitrate" "xesam:mediaCodecType"
221 "xesam:meteringMode" "xesam:mimeType" "xesam:mountPoint"
222 "xesam:msnContactMedium" "xesam:name" "xesam:occupiedSpace"
223 "xesam:orientation" "xesam:originalLocation" "xesam:owner"
224 "xesam:pageCount" "xesam:permissions" "xesam:phoneNumber"
225 "xesam:physicalAddress" "xesam:pixelFormat" "xesam:primaryRecipient"
226 "xesam:programmingLanguage" "xesam:rating" "xesam:receptionTime"
227 "xesam:recipient" "xesam:related" "xesam:remoteUser" "xesam:rowCount"
228 "xesam:sampleBitDepth" "xesam:sampleFormat" "xesam:secondaryRecipient"
229 "xesam:sha1Hash" "xesam:size" "xesam:skypeContactMedium"
230 "xesam:sourceCreated" "xesam:sourceModified" "xesam:storageSize"
231 "xesam:subject" "xesam:supercedes" "xesam:title" "xesam:to"
232 "xesam:totalSpace" "xesam:totalUncompressedSize" "xesam:url"
233 "xesam:usageIntensity" "xesam:userComment" "xesam:userKeyword"
234 "xesam:uuid" "xesam:version" "xesam:verticalResolution"
235 "xesam:videoBitrate"
236 "xesam:videoCodec" "xesam:videoCodecType" "xesam:whiteBalance"
237 "xesam:width" "xesam:wordCount" "xesam:workEmailAddress"
238 "xesam:workPhoneNumber" "xesam:workPostalAddress"
239 "xesam:yahooContactMedium")
240 "All fields from the Xesam Core Ontology.
241 This defconst can be used to check for a new search engine, which
242 fields are supported.")
244 (defconst xesam-user-query
245 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
246 <request xmlns=\"http://freedesktop.org/standards/xesam/1.0/query\">
247 <userQuery>
249 </userQuery>
250 </request>"
251 "The Xesam user query XML.")
253 (defconst xesam-fulltext-query
254 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
255 <request xmlns=\"http://freedesktop.org/standards/xesam/1.0/query\">
256 <query content=\"xesam:Document\" source=\"xesam:File\">
257 <fullText>
258 <string>%s</string>
259 </fullText>
260 </query>
261 </request>"
262 "The Xesam fulltext query XML.")
264 (declare-function dbus-get-unique-name "dbusbind.c" (bus))
266 (defvar xesam-dbus-unique-names
267 (list (cons :system (dbus-get-unique-name :system))
268 (cons :session (dbus-get-unique-name :session)))
269 "The unique names, under which Emacs is registered at D-Bus.")
271 (defun xesam-dbus-call-method (&rest args)
272 "Apply a D-Bus method call.
273 `dbus-call-method' is preferred, because it performs better.
274 If the target D-Bus service is owned by Emacs, this
275 is not applicable, and `dbus-call-method-non-blocking' must be
276 used instead. ARGS are identical to the argument list of both
277 functions."
278 (apply
279 ;; The first argument is the bus, the second argument the targt service.
280 (if (string-equal (cdr (assoc (car args) xesam-dbus-unique-names))
281 (cadr args))
282 'dbus-call-method-non-blocking 'dbus-call-method)
283 args))
285 (defun xesam-get-property (engine property)
286 "Return the PROPERTY value of ENGINE."
287 ;; "GetProperty" returns a variant, so we must use the car.
288 (car (xesam-dbus-call-method
289 :session (car engine) xesam-path-search
290 xesam-interface-search "GetProperty"
291 (xesam-get-cached-property engine "session") property)))
293 (defun xesam-set-property (engine property value)
294 "Set the PROPERTY of ENGINE to VALUE.
295 VALUE can be a string, a non-negative integer, a boolean
296 value (nil or t), or a list of them. It returns the new value of
297 PROPERTY in the search engine. This new value can be different
298 from VALUE, depending on what the search engine accepts."
299 ;; "SetProperty" returns a variant, so we must use the car.
300 (car (xesam-dbus-call-method
301 :session (car engine) xesam-path-search
302 xesam-interface-search "SetProperty"
303 (xesam-get-cached-property engine "session") property
304 ;; The value must be a variant. It can be only a string, an
305 ;; unsigned int, a boolean, or an array of them. So we need
306 ;; no type keyword; we let the type check to the search
307 ;; engine.
308 (list :variant value))))
310 (defvar xesam-minibuffer-vendor-history nil
311 "Interactive vendor history.")
313 (defvar xesam-minibuffer-query-history nil
314 "Interactive query history.")
316 ;; Pacify byte compiler.
317 (defvar xesam-vendor nil)
318 (make-variable-buffer-local 'xesam-vendor)
319 (put 'xesam-vendor 'permanent-local t)
321 (defvar xesam-engine nil)
322 (defvar xesam-search nil)
323 (defvar xesam-type nil)
324 (defvar xesam-query nil)
325 (defvar xesam-xml-string nil)
326 (defvar xesam-objects nil)
327 (defvar xesam-current nil)
328 (defvar xesam-count nil)
329 (defvar xesam-to nil)
330 (defvar xesam-notify-function nil)
331 (defvar xesam-refreshing nil)
334 ;;; Search engines.
336 (defvar xesam-search-engines nil
337 "List of available Xesam search engines.
338 Every entry is an association list, with a car denoting the
339 unique D-Bus service name of the engine. The rest of the entry
340 are cached associations of engine attributes, like the session
341 identifier, and the display name. Example:
343 \(\(\":1.59\"
344 \(\"session\" . \"0t1214948020ut358230u0p2698r3912347765k3213849828\")
345 \(\"vendor.display\" . \"Tracker Xesam Service\"))
346 \(\":1.27\"
347 \(\"session\" . \"strigisession1369133069\")
348 \(\"vendor.display\" . \"Strigi Desktop Search\")))
350 A Xesam-compatible search engine is identified as a queued D-Bus
351 service of the known service `xesam-service-search'.")
353 (defun xesam-get-cached-property (engine property)
354 "Return the PROPERTY value of ENGINE from the cache.
355 If PROPERTY is not existing, retrieve it from ENGINE first."
356 ;; If the property has not been cached yet, we retrieve it from the
357 ;; engine, and cache it.
358 (unless (assoc property engine)
359 (xesam-set-cached-property
360 engine property (xesam-get-property engine property)))
361 (cdr (assoc property engine)))
363 (defun xesam-set-cached-property (engine property value)
364 "Set the PROPERTY of ENGINE to VALUE in the cache."
365 (setcdr engine (append (cdr engine) (list (cons property value)))))
367 (defun xesam-delete-search-engine (&rest args)
368 "Remove service from `xesam-search-engines'."
369 (setq xesam-search-engines
370 (delete (assoc (car args) xesam-search-engines) xesam-search-engines)))
372 (defvar dbus-debug)
374 (defun xesam-search-engines ()
375 "Return Xesam search engines, stored in `xesam-search-engines'.
376 The first search engine is the name owner of `xesam-service-search'.
377 If there is no registered search engine at all, the function returns `nil'."
378 (let ((services (dbus-ignore-errors
379 (dbus-list-queued-owners
380 :session xesam-service-search)))
381 engine vendor-id hit-fields)
382 (dolist (service services)
383 (unless (assoc-string service xesam-search-engines)
385 ;; Open a new session, and add it to the search engines list.
386 (add-to-list 'xesam-search-engines (list service) 'append)
387 (setq engine (assoc service xesam-search-engines))
389 ;; Add the session string.
390 (xesam-set-cached-property
391 engine "session"
392 (xesam-dbus-call-method
393 :session service xesam-path-search
394 xesam-interface-search "NewSession"))
396 ;; Unset the "search.live" property; we don't want to be
397 ;; informed by changed results.
398 (xesam-set-property engine "search.live" nil)
400 ;; Check the vendor properties.
401 (setq vendor-id (xesam-get-property engine "vendor.id")
402 hit-fields (xesam-get-property engine "hit.fields"))
404 ;; Usually, `hit.fields' shall describe supported fields.
405 ;; That is not the case now, so we set it ourselves.
406 ;; Hopefully, this will change later.
407 (setq hit-fields
408 (pcase (intern vendor-id)
409 (`Beagle
410 '("xesam:mimeType" "xesam:url"))
411 (`Strigi
412 '("xesam:author" "xesam:cc" "xesam:charset"
413 "xesam:contentType" "xesam:fileExtension"
414 "xesam:id" "xesam:lineCount" "xesam:links"
415 "xesam:mimeType" "xesam:name" "xesam:size"
416 "xesam:sourceModified" "xesam:subject" "xesam:to"
417 "xesam:url"))
418 (`TrackerXesamSession
419 '("xesam:relevancyRating" "xesam:url"))
420 (`Debbugs
421 '("xesam:keyword" "xesam:owner" "xesam:title"
422 "xesam:url" "xesam:sourceModified" "xesam:mimeType"
423 "debbugs:key"))
424 ;; xesam-tools yahoo service.
425 (_ '("xesam:contentModified" "xesam:mimeType" "xesam:summary"
426 "xesam:title" "xesam:url" "yahoo:displayUrl"))))
428 (xesam-set-property engine "hit.fields" hit-fields)
429 (xesam-set-property engine "hit.fields.extended" '("xesam:snippet"))
431 ;; Let us notify, when the search engine disappears.
432 (dbus-register-signal
433 :session dbus-service-dbus dbus-path-dbus
434 dbus-interface-dbus "NameOwnerChanged"
435 'xesam-delete-search-engine service))))
436 xesam-search-engines)
439 ;;; Search buffers.
441 (defvar xesam-mode-map
442 (let ((map (copy-keymap special-mode-map)))
443 (set-keymap-parent map widget-keymap)
444 map))
446 (define-derived-mode xesam-mode special-mode "Xesam"
447 "Major mode for presenting search results of a Xesam search.
448 In this mode, widgets represent the search results.
450 \\{xesam-mode-map}
451 Turning on Xesam mode runs the normal hook `xesam-mode-hook'. It
452 can be used to set `xesam-notify-function', which must a search
453 engine specific, widget :notify function to visualize xesam:url."
454 (set (make-local-variable 'xesam-notify-function) nil)
455 ;; Maybe we implement something useful, later on.
456 (set (make-local-variable 'revert-buffer-function) 'ignore)
457 ;; `xesam-engine', `xesam-search', `xesam-type', `xesam-query', and
458 ;; `xesam-xml-string' will be set in `xesam-new-search'.
459 (set (make-local-variable 'xesam-engine) nil)
460 (set (make-local-variable 'xesam-search) nil)
461 (set (make-local-variable 'xesam-type) "")
462 (set (make-local-variable 'xesam-query) "")
463 (set (make-local-variable 'xesam-xml-string) "")
464 (set (make-local-variable 'xesam-objects) nil)
465 ;; `xesam-current' is the last hit put into the search buffer,
466 (set (make-local-variable 'xesam-current) 0)
467 ;; `xesam-count' is the number of hits reported by the search engine.
468 (set (make-local-variable 'xesam-count) 0)
469 ;; `xesam-to' is the upper hit number to be presented.
470 (set (make-local-variable 'xesam-to) xesam-hits-per-page)
471 ;; `xesam-notify-function' can be a search engine specific function
472 ;; to visualize xesam:url. It can be overwritten in `xesam-mode'.
473 (set (make-local-variable 'xesam-notify-function) nil)
474 ;; `xesam-refreshing' is an indicator, whether the buffer is just
475 ;; being updated. Needed, because `xesam-refresh-search-buffer'
476 ;; can be triggered by an event.
477 (set (make-local-variable 'xesam-refreshing) nil)
478 ;; Mode line position returns hit counters.
479 (set (make-local-variable 'mode-line-position)
480 (list '(-3 "%p%")
481 '(10 (:eval (format " (%d/%d)" xesam-current xesam-count)))))
482 ;; Header line contains the query string.
483 (set (make-local-variable 'header-line-format)
484 (list '(20
485 (:eval
486 (list "Type: "
487 (propertize xesam-type 'face 'xesam-mode-line))))
488 '(10
489 (:eval
490 (list " Query: "
491 (propertize
492 xesam-query
493 'face 'xesam-mode-line
494 'help-echo (when xesam-debug xesam-xml-string)))))))
496 (when (not (called-interactively-p 'interactive))
497 ;; Initialize buffer.
498 (setq buffer-read-only t)
499 (let ((inhibit-read-only t))
500 (erase-buffer))))
502 ;; It doesn't make sense to call it interactively.
503 (put 'xesam-mode 'disabled t)
505 ;; The very first buffer created with `xesam-mode' does not have the
506 ;; keymap etc. So we create a dummy buffer. Stupid.
507 (with-temp-buffer (xesam-mode))
509 (define-minor-mode xesam-minor-mode
510 "Toggle Xesam minor mode.
511 With a prefix argument ARG, enable Xesam minor mode if ARG is
512 positive, and disable it otherwise. If called from Lisp, enable
513 the mode if ARG is omitted or nil.
515 When Xesam minor mode is enabled, all text which matches a
516 previous Xesam query in this buffer is highlighted."
517 :group 'xesam
518 :init-value nil
519 :lighter " Xesam"
520 (when (local-variable-p 'xesam-query)
521 ;; Run only if the buffer is related to a Xesam search.
522 (save-excursion
523 (if xesam-minor-mode
524 ;; Highlight hits.
525 (let ((query-regexp (regexp-opt (split-string xesam-query nil t) t))
526 (case-fold-search t))
527 ;; I have no idea whether people will like setting
528 ;; `isearch-case-fold-search' and `query-regexp'. Maybe
529 ;; this shall be controlled by a custom option.
530 (unless isearch-case-fold-search (isearch-toggle-case-fold))
531 (isearch-update-ring query-regexp t)
532 ;; Create overlays.
533 (goto-char (point-min))
534 (while (re-search-forward query-regexp nil t)
535 (overlay-put
536 (make-overlay
537 (match-beginning 0) (match-end 0)) 'face 'xesam-highlight)))
538 ;; Remove overlays.
539 (dolist (ov (overlays-in (point-min) (point-max)))
540 (delete-overlay ov))))))
542 (defun xesam-buffer-name (service search)
543 "Return the buffer name where to present search results.
544 SERVICE is the D-Bus unique service name of the Xesam search engine.
545 SEARCH is the search identification in that engine. Both must be strings."
546 (format "*%s/%s*" service search))
548 (defun xesam-highlight-string (string)
549 "Highlight text enclosed by <b> and </b>.
550 Return propertized STRING."
551 (while (string-match "\\(.*\\)\\(<b>\\)\\(.*\\)\\(</b>\\)\\(.*\\)" string)
552 (setq string
553 (format
554 "%s%s%s"
555 (match-string 1 string)
556 (propertize (match-string 3 string) 'face 'xesam-highlight)
557 (match-string 5 string))))
558 string)
560 (defun xesam-refresh-entry (engine entry)
561 "Refreshes one entry in the search buffer."
562 (let* ((result (nth (1- xesam-current) xesam-objects))
563 widget)
565 ;; Create widget.
566 (setq widget (widget-convert 'link))
567 (when xesam-debug
568 (widget-put widget :help-echo ""))
570 ;; Take all results.
571 (dolist (field (xesam-get-cached-property engine "hit.fields"))
572 (when (cond
573 ((stringp (caar result)) (not (zerop (length (caar result)))))
574 ((numberp (caar result)) (not (zerop (caar result))))
575 ((caar result) t))
576 (when xesam-debug
577 (widget-put
578 widget :help-echo
579 (format "%s%s: %s\n"
580 (widget-get widget :help-echo) field (caar result))))
581 (widget-put widget (intern (concat ":" field)) (caar result)))
582 (setq result (cdr result)))
584 ;; Strigi doesn't return URLs in xesam:url. We must fix this.
585 (when
586 (not (url-type (url-generic-parse-url (widget-get widget :xesam:url))))
587 (widget-put
588 widget :xesam:url (concat "file://" (widget-get widget :xesam:url))))
590 ;; Strigi returns xesam:size as string. We must fix this.
591 (when (and (widget-member widget :xesam:size)
592 (stringp (widget-get widget :xesam:size)))
593 (widget-put
594 widget :xesam:size (string-to-number (widget-get widget :xesam:url))))
596 ;; First line: :tag.
597 (cond
598 ((widget-member widget :xesam:title)
599 (widget-put widget :tag (widget-get widget :xesam:title)))
600 ((widget-member widget :xesam:subject)
601 (widget-put widget :tag (widget-get widget :xesam:subject)))
602 ((widget-member widget :xesam:mimeType)
603 (widget-put widget :tag (widget-get widget :xesam:mimeType)))
604 ((widget-member widget :xesam:name)
605 (widget-put widget :tag (widget-get widget :xesam:name))))
607 ;; Highlight the search items.
608 (when (widget-member widget :tag)
609 (widget-put
610 widget :tag (xesam-highlight-string (widget-get widget :tag))))
612 ;; Last Modified.
613 (when (and (widget-member widget :xesam:sourceModified)
614 (not
615 (zerop
616 (string-to-number (widget-get widget :xesam:sourceModified)))))
617 (widget-put
618 widget :tag
619 (format
620 "%s\nLast Modified: %s"
621 (or (widget-get widget :tag) "")
622 (format-time-string
623 "%d %B %Y, %T"
624 (seconds-to-time
625 (string-to-number (widget-get widget :xesam:sourceModified)))))))
627 ;; Second line: :value.
628 (widget-put widget :value (widget-get widget :xesam:url))
630 (cond
631 ;; A search engine can set `xesam-notify-function' via
632 ;; `xesam-mode-hooks'.
633 (xesam-notify-function
634 (widget-put widget :notify xesam-notify-function))
636 ;; In case of HTML, we use a URL link.
637 ((and (widget-member widget :xesam:mimeType)
638 (string-equal "text/html" (widget-get widget :xesam:mimeType)))
639 (setcar widget 'url-link))
641 ;; For local files, we will open the file as default action.
642 ((string-match "file"
643 (url-type (url-generic-parse-url
644 (widget-get widget :xesam:url))))
645 (widget-put
646 widget :notify
647 (lambda (widget &rest ignore)
648 (let ((query xesam-query))
649 (find-file
650 (url-filename (url-generic-parse-url (widget-value widget))))
651 (set (make-local-variable 'xesam-query) query)
652 (xesam-minor-mode 1))))
653 (widget-put
654 widget :value
655 (url-filename (url-generic-parse-url (widget-get widget :xesam:url))))))
657 ;; Third line: :doc.
658 (cond
659 ((widget-member widget :xesam:summary)
660 (widget-put widget :doc (widget-get widget :xesam:summary)))
661 ((widget-member widget :xesam:snippet)
662 (widget-put widget :doc (widget-get widget :xesam:snippet))))
664 (when (widget-member widget :doc)
665 (with-temp-buffer
666 (insert
667 (xesam-highlight-string (widget-get widget :doc)))
668 (fill-region-as-paragraph (point-min) (point-max))
669 (widget-put widget :doc (buffer-string)))
670 (widget-put widget :help-echo (widget-get widget :doc)))
672 ;; Format the widget.
673 (widget-put
674 widget :format
675 (format "%d. %s%%[%%v%%]\n%s\n" xesam-current
676 (if (widget-member widget :tag) "%{%t%}\n" "")
677 (if (widget-member widget :doc) "%h" "")))
679 ;; Write widget.
680 (goto-char (point-max))
681 (widget-default-create widget)
682 (set-buffer-modified-p nil)
683 (force-mode-line-update)
684 (redisplay)))
686 (defun xesam-get-hits (engine search hits)
687 "Retrieve hits from ENGINE."
688 (with-current-buffer (xesam-buffer-name (car engine) search)
689 (setq xesam-objects
690 (append xesam-objects
691 (xesam-dbus-call-method
692 :session (car engine) xesam-path-search
693 xesam-interface-search "GetHits" search hits)))))
695 (defun xesam-refresh-search-buffer (engine search)
696 "Refreshes the buffer, presenting results of SEARCH."
697 (with-current-buffer (xesam-buffer-name (car engine) search)
698 ;; Work only if nobody else is here.
699 (unless (or xesam-refreshing (>= xesam-current xesam-to))
700 (setq xesam-refreshing t)
701 (unwind-protect
702 (let (widget)
704 ;; Retrieve needed hits for visualization.
705 (while (> (min xesam-to xesam-count) (length xesam-objects))
706 (xesam-get-hits
707 engine search
708 (min xesam-hits-per-page
709 (- (min xesam-to xesam-count) (length xesam-objects)))))
711 ;; Add all result widgets.
712 (while (< xesam-current (min xesam-to xesam-count))
713 (setq xesam-current (1+ xesam-current))
714 (xesam-refresh-entry engine search))
716 ;; Add "NEXT" widget.
717 (when (> xesam-count xesam-to)
718 (goto-char (point-max))
719 (widget-create
720 'link
721 :notify
722 (lambda (widget &rest ignore)
723 (setq xesam-to (+ xesam-to xesam-hits-per-page))
724 (widget-delete widget)
725 (xesam-refresh-search-buffer xesam-engine xesam-search))
726 "NEXT")
727 (widget-beginning-of-line))
729 ;; Prefetch next hits.
730 (when (> (min (+ xesam-hits-per-page xesam-to) xesam-count)
731 (length xesam-objects))
732 (xesam-get-hits
733 engine search
734 (min xesam-hits-per-page
735 (- (min (+ xesam-hits-per-page xesam-to) xesam-count)
736 (length xesam-objects)))))
738 ;; Add "DONE" widget.
739 (when (= xesam-current xesam-count)
740 (goto-char (point-max))
741 (widget-create 'link :notify 'ignore "DONE")
742 (widget-beginning-of-line)))
744 ;; Return with save settings.
745 (setq xesam-refreshing nil)))))
748 ;;; Search functions.
750 (defun xesam-signal-handler (&rest args)
751 "Handles the different D-Bus signals of a Xesam search."
752 (let* ((service (dbus-event-service-name last-input-event))
753 (member (dbus-event-member-name last-input-event))
754 (search (nth 0 args))
755 (buffer (xesam-buffer-name service search)))
757 (when (get-buffer buffer)
758 (with-current-buffer buffer
759 (cond
761 ((string-equal member "HitsAdded")
762 (setq xesam-count (+ xesam-count (nth 1 args)))
763 ;; We use `run-at-time' in order to not block the event queue.
764 (run-at-time
765 0 nil
766 'xesam-refresh-search-buffer
767 (assoc service xesam-search-engines) search))
769 ((string-equal member "SearchDone")
770 (setq mode-line-process
771 (propertize " Done" 'face 'xesam-mode-line))
772 (force-mode-line-update)))))))
774 (defun xesam-kill-buffer-function ()
775 "Send the CloseSearch indication."
776 (when (and (eq major-mode 'xesam-mode) (stringp xesam-search))
777 (ignore-errors ;; The D-Bus service could have disappeared.
778 (xesam-dbus-call-method
779 :session (car xesam-engine) xesam-path-search
780 xesam-interface-search "CloseSearch" xesam-search))))
782 (defun xesam-new-search (engine type query)
783 "Create a new search session.
784 ENGINE identifies the search engine. TYPE is the query type, it
785 can be either `fulltext-query', or `user-query'. QUERY is a
786 string in the Xesam query language. A string, identifying the
787 search, is returned."
788 (let* ((service (car engine))
789 (session (xesam-get-cached-property engine "session"))
790 (xml-string
791 (format
792 (if (eq type 'user-query) xesam-user-query xesam-fulltext-query)
793 (url-insert-entities-in-string query)))
794 (search (xesam-dbus-call-method
795 :session service xesam-path-search
796 xesam-interface-search "NewSearch" session xml-string)))
798 ;; Let us notify for relevant signals. We ignore "HitsRemoved",
799 ;; "HitsModified" and "StateChanged"; there is nothing to do for
800 ;; us.
801 (dbus-register-signal
802 :session service xesam-path-search
803 xesam-interface-search "HitsAdded"
804 'xesam-signal-handler search)
805 (dbus-register-signal
806 :session service xesam-path-search
807 xesam-interface-search "SearchDone"
808 'xesam-signal-handler search)
810 ;; Create the search buffer.
811 (with-current-buffer
812 (generate-new-buffer (xesam-buffer-name service search))
813 (switch-to-buffer-other-window (current-buffer))
814 ;; Initialize buffer with `xesam-mode'. `xesam-vendor' must be
815 ;; set before calling `xesam-mode', because we want to give the
816 ;; hook functions a chance to identify their search engine.
817 (setq xesam-vendor (xesam-get-cached-property engine "vendor.id"))
818 (xesam-mode)
819 (setq xesam-engine engine
820 xesam-search search
821 ;; `xesam-type', `xesam-query' and `xesam-xml-string'
822 ;; are displayed in the header line.
823 xesam-type (symbol-name type)
824 xesam-query query
825 xesam-xml-string xml-string
826 xesam-objects nil
827 ;; The buffer identification shall indicate the search
828 ;; engine. The `help-echo' property is used for debug
829 ;; information, when applicable.
830 mode-line-buffer-identification
831 (if (not xesam-debug)
832 (list 12 (propertized-buffer-identification xesam-vendor))
833 (propertize
834 xesam-vendor
835 'help-echo
836 (mapconcat
837 (lambda (x)
838 (format "%s: %s" x (xesam-get-cached-property engine x)))
839 '("vendor.id" "vendor.version" "vendor.display" "vendor.xesam"
840 "vendor.ontology.fields" "vendor.ontology.contents"
841 "vendor.ontology.sources" "vendor.extensions"
842 "vendor.ontologies" "vendor.maxhits")
843 "\n"))))
844 (add-hook 'kill-buffer-hook 'xesam-kill-buffer-function)
845 (force-mode-line-update))
847 ;; Start the search.
848 (xesam-dbus-call-method
849 :session (car engine) xesam-path-search
850 xesam-interface-search "StartSearch" search)
852 ;; Return search id.
853 search))
855 ;;;###autoload
856 (defun xesam-search (engine query)
857 "Perform an interactive search.
858 ENGINE is the Xesam search engine to be applied, it must be one of the
859 entries of `xesam-search-engines'. QUERY is the search string in the
860 Xesam user query language. If the search engine does not support
861 the Xesam user query language, a Xesam fulltext search is applied.
863 The default search engine is the first entry in `xesam-search-engines'.
864 Example:
866 (xesam-search (car (xesam-search-engines)) \"emacs\")"
867 (interactive
868 (let* ((vendors (mapcar
869 (lambda (x) (xesam-get-cached-property x "vendor.display"))
870 (xesam-search-engines)))
871 (vendor
872 (if (> (length vendors) 1)
873 (completing-read
874 "Enter search engine: " vendors nil t
875 (try-completion "" vendors) 'xesam-minibuffer-vendor-history)
876 (car vendors))))
877 (list
878 ;; ENGINE.
879 (when vendor
880 (dolist (elt (xesam-search-engines) engine)
881 (when (string-equal
882 (xesam-get-cached-property elt "vendor.display") vendor)
883 (setq engine elt))))
884 ;; QUERY.
885 (when vendor
886 (read-from-minibuffer
887 "Enter search string: " nil nil nil
888 'xesam-minibuffer-query-history)))))
890 (if (null engine)
891 (message "No search engine running")
892 (if (zerop (length query))
893 (message "No query applied")
894 (xesam-new-search engine xesam-query-type query))))
896 (provide 'xesam)
898 ;;; TODO:
900 ;; * Buffer highlighting needs better analysis of query string.
901 ;; * Accept input while retrieving prefetched hits. `run-at-time'?
902 ;; * With prefix, let's choose search engine.
903 ;; * Minibuffer completion for user queries.
904 ;; * `revert-buffer-function' implementation.
906 ;; * Mid term
907 ;; - If available, use ontologies for field selection.
908 ;; - Search engines for Emacs bugs database, wikipedia, google,
909 ;; yahoo, ebay, ...
910 ;; - Construct complex queries via widgets, like in mairix.el.
912 ;;; xesam.el ends here