comparison lisp/net/xesam.el @ 97618:1f7e10e015fd

* net/xesam.el (xesam-minor-mode): New minor mode. (xesam-highlight-buffer): Remove. Code moved to `xesam-minor-mode'. (xesam-refresh-entry): Use `xesam-minor-mode'.
author Michael Albinus <michael.albinus@gmx.de>
date Fri, 22 Aug 2008 19:32:26 +0000
parents 9fb28094a3cf
children 48c669cb45f9
comparison
equal deleted inserted replaced
97617:50e32f7ae447 97618:1f7e10e015fd
98 ;; M-x xesam-search 98 ;; M-x xesam-search
99 ;; 99 ;;
100 ;; When several search engines are registered, the engine to be used 100 ;; When several search engines are registered, the engine to be used
101 ;; can be selected via minibuffer completion. Afterwards, the query 101 ;; can be selected via minibuffer completion. Afterwards, the query
102 ;; shall be entered in the minibuffer. 102 ;; shall be entered in the minibuffer.
103
104 ;; Search results are presented in a new buffer. This buffer has the
105 ;; major mode `xesam-mode', with the following keybindings:
106
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'
114
115 ;; The search results are represented by widgets. Navigation commands
116 ;; are the usual widget navigation commands:
117
118 ;; TAB `widget-forward'
119 ;; <backtab> `widget-backward'
120
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).
103 127
104 ;;; Code: 128 ;;; Code:
105 129
106 ;; D-Bus support in the Emacs core can be disabled with configuration 130 ;; D-Bus support in the Emacs core can be disabled with configuration
107 ;; option "--without-dbus". Declare used subroutines and variables. 131 ;; option "--without-dbus". Declare used subroutines and variables.
485 509
486 ;; The very first buffer created with `xesam-mode' does not have the 510 ;; The very first buffer created with `xesam-mode' does not have the
487 ;; keymap etc. So we create a dummy buffer. Stupid. 511 ;; keymap etc. So we create a dummy buffer. Stupid.
488 (with-temp-buffer (xesam-mode)) 512 (with-temp-buffer (xesam-mode))
489 513
514 (define-minor-mode xesam-minor-mode
515 "Toggle Xesam minor mode.
516 With no argument, this command toggles the mode.
517 Non-null prefix argument turns on the mode.
518 Null prefix argument turns off the mode.
519
520 When Xesam minor mode is enabled, all text which matches a
521 previous Xesam query in this buffer is highlighted."
522 :group 'xesam
523 :init-value nil
524 :lighter " Xesam"
525 (when (local-variable-p 'xesam-query)
526 ;; Run only if the buffer is related to a Xesam search.
527 (save-excursion
528 (if xesam-minor-mode
529 ;; Highlight hits.
530 (let ((query-regexp (regexp-opt (split-string xesam-query nil t) t))
531 (case-fold-search t))
532 ;; I have no idea whether people will like setting
533 ;; `isearch-case-fold-search' and `query-regexp'. Maybe
534 ;; this shall be controlled by a custom option.
535 (unless isearch-case-fold-search (isearch-toggle-case-fold))
536 (isearch-update-ring query-regexp t)
537 ;; Create overlays.
538 (goto-char (point-min))
539 (while (re-search-forward query-regexp nil t)
540 (overlay-put
541 (make-overlay
542 (match-beginning 0) (match-end 0)) 'face 'xesam-highlight)))
543 ;; Remove overlays.
544 (dolist (ov (overlays-in (point-min) (point-max)))
545 (delete-overlay ov))))))
546
490 (defun xesam-buffer-name (service search) 547 (defun xesam-buffer-name (service search)
491 "Return the buffer name where to present search results. 548 "Return the buffer name where to present search results.
492 SERVICE is the D-Bus unique service name of the Xesam search engine. 549 SERVICE is the D-Bus unique service name of the Xesam search engine.
493 SEARCH is the search identification in that engine. Both must be strings." 550 SEARCH is the search identification in that engine. Both must be strings."
494 (format "*%s/%s*" service search)) 551 (format "*%s/%s*" service search))
502 "%s%s%s" 559 "%s%s%s"
503 (match-string 1 string) 560 (match-string 1 string)
504 (propertize (match-string 3 string) 'face 'xesam-highlight) 561 (propertize (match-string 3 string) 'face 'xesam-highlight)
505 (match-string 5 string)))) 562 (match-string 5 string))))
506 string) 563 string)
507
508 (defun xesam-highlight-buffer (regexp &optional buffer)
509 "Highlight text matching REGEXP in BUFFER.
510 If BUFFER is nil, use the current buffer"
511 (with-current-buffer (or buffer (current-buffer))
512 (save-excursion
513 (let ((case-fold-search t))
514 (goto-char (point-min))
515 (while (re-search-forward regexp nil t)
516 (overlay-put
517 (make-overlay
518 (match-beginning 0) (match-end 0)) 'face 'xesam-highlight))))))
519 564
520 (defun xesam-refresh-entry (engine entry) 565 (defun xesam-refresh-entry (engine entry)
521 "Refreshes one entry in the search buffer." 566 "Refreshes one entry in the search buffer."
522 (let* ((result (nth (1- xesam-current) xesam-objects)) 567 (let* ((result (nth (1- xesam-current) xesam-objects))
523 widget) 568 widget)
606 widget :notify 651 widget :notify
607 (lambda (widget &rest ignore) 652 (lambda (widget &rest ignore)
608 (let ((query xesam-query)) 653 (let ((query xesam-query))
609 (find-file 654 (find-file
610 (url-filename (url-generic-parse-url (widget-value widget)))) 655 (url-filename (url-generic-parse-url (widget-value widget))))
611 (xesam-highlight-buffer (regexp-opt (split-string query nil t)))))) 656 (set (make-local-variable 'xesam-query) query)
657 (xesam-minor-mode 1))))
612 (widget-put 658 (widget-put
613 widget :value 659 widget :value
614 (url-filename (url-generic-parse-url (widget-get widget :xesam:url)))))) 660 (url-filename (url-generic-parse-url (widget-get widget :xesam:url))))))
615 661
616 ;; Third line: :doc. 662 ;; Third line: :doc.