+;;; Button types used by apropos
+
+(define-button-type 'apropos-symbol
+ 'face apropos-symbol-face
+ 'help-echo "mouse-2, RET: Display more help on this symbol"
+ 'follow-link t
+ 'action #'apropos-symbol-button-display-help
+ 'skip t)
+
+(defun apropos-symbol-button-display-help (button)
+ "Display further help for the `apropos-symbol' button BUTTON."
+ (button-activate
+ (or (apropos-next-label-button (button-start button))
+ (error "There is nothing to follow for `%s'" (button-label button)))))
+
+(define-button-type 'apropos-function
+ 'apropos-label "Function"
+ 'help-echo "mouse-2, RET: Display more help on this function"
+ 'follow-link t
+ 'action (lambda (button)
+ (describe-function (button-get button 'apropos-symbol))))
+
+(define-button-type 'apropos-macro
+ 'apropos-label "Macro"
+ 'help-echo "mouse-2, RET: Display more help on this macro"
+ 'follow-link t
+ 'action (lambda (button)
+ (describe-function (button-get button 'apropos-symbol))))
+
+(define-button-type 'apropos-command
+ 'apropos-label "Command"
+ 'help-echo "mouse-2, RET: Display more help on this command"
+ 'follow-link t
+ 'action (lambda (button)
+ (describe-function (button-get button 'apropos-symbol))))
+
+;; We used to use `customize-variable-other-window' instead for a
+;; customizable variable, but that is slow. It is better to show an
+;; ordinary help buffer and let the user click on the customization
+;; button in that buffer, if he wants to.
+;; Likewise for `customize-face-other-window'.
+(define-button-type 'apropos-variable
+ 'apropos-label "Variable"
+ 'help-echo "mouse-2, RET: Display more help on this variable"
+ 'follow-link t
+ 'action (lambda (button)
+ (describe-variable (button-get button 'apropos-symbol))))
+
+(define-button-type 'apropos-face
+ 'apropos-label "Face"
+ 'help-echo "mouse-2, RET: Display more help on this face"
+ 'follow-link t
+ 'action (lambda (button)
+ (describe-face (button-get button 'apropos-symbol))))
+
+(define-button-type 'apropos-group
+ 'apropos-label "Group"
+ 'help-echo "mouse-2, RET: Display more help on this group"
+ 'follow-link t
+ 'action (lambda (button)
+ (customize-group-other-window
+ (button-get button 'apropos-symbol))))
+
+(define-button-type 'apropos-widget
+ 'apropos-label "Widget"
+ 'help-echo "mouse-2, RET: Display more help on this widget"
+ 'follow-link t
+ 'action (lambda (button)
+ (widget-browse-other-window (button-get button 'apropos-symbol))))
+
+(define-button-type 'apropos-plist
+ 'apropos-label "Plist"
+ 'help-echo "mouse-2, RET: Display more help on this plist"
+ 'follow-link t
+ 'action (lambda (button)
+ (apropos-describe-plist (button-get button 'apropos-symbol))))
+
+(defun apropos-next-label-button (pos)
+ "Return the next apropos label button after POS, or nil if there's none.
+Will also return nil if more than one `apropos-symbol' button is encountered
+before finding a label."
+ (let* ((button (next-button pos t))
+ (already-hit-symbol nil)
+ (label (and button (button-get button 'apropos-label)))
+ (type (and button (button-get button 'type))))
+ (while (and button
+ (not label)
+ (or (not (eq type 'apropos-symbol))
+ (not already-hit-symbol)))
+ (when (eq type 'apropos-symbol)
+ (setq already-hit-symbol t))
+ (setq button (next-button (button-start button)))
+ (when button
+ (setq label (button-get button 'apropos-label))
+ (setq type (button-get button 'type))))
+ (and label button)))
+
+\f
+(defun apropos-words-to-regexp (words wild)
+ "Make regexp matching any two of the words in WORDS."
+ (concat "\\("
+ (mapconcat 'identity words "\\|")
+ "\\)"
+ (if (cdr words)
+ (concat wild
+ "\\("
+ (mapconcat 'identity words "\\|")
+ "\\)")
+ "")))
+
+;;;###autoload
+(defun apropos-read-pattern (subject)
+ "Read an apropos pattern, either a word list or a regexp.
+Returns the user pattern, either a list of words which are matched
+literally, or a string which is used as a regexp to search for.
+
+SUBJECT is a string that is included in the prompt to identify what
+kind of objects to search."
+ (let ((pattern
+ (read-string (concat "Apropos " subject " (word list or regexp): "))))
+ (if (string-equal (regexp-quote pattern) pattern)
+ ;; Split into words
+ (split-string pattern "[ \t]+")
+ pattern)))
+
+(defun apropos-parse-pattern (pattern)
+ "Rewrite a list of words to a regexp matching all permutations.
+If PATTERN is a string, that means it is already a regexp.
+This updates variables `apropos-pattern', `apropos-pattern-quoted',
+`apropos-regexp', `apropos-words', and `apropos-all-words-regexp'."
+ (setq apropos-words nil
+ apropos-all-words nil)
+ (if (consp pattern)
+ ;; We don't actually make a regexp matching all permutations.
+ ;; Instead, for e.g. "a b c", we make a regexp matching
+ ;; any combination of two or more words like this:
+ ;; (a|b|c).*(a|b|c) which may give some false matches,
+ ;; but as long as it also gives the right ones, that's ok.
+ (let ((words pattern))
+ (setq apropos-pattern (mapconcat 'identity pattern " ")
+ apropos-pattern-quoted (regexp-quote apropos-pattern))
+ (dolist (word words)
+ (let ((syn apropos-synonyms) (s word) (a word))
+ (while syn
+ (if (member word (car syn))
+ (progn
+ (setq a (mapconcat 'identity (car syn) "\\|"))
+ (if (member word (cdr (car syn)))
+ (setq s a))
+ (setq syn nil))
+ (setq syn (cdr syn))))
+ (setq apropos-words (cons s apropos-words)
+ apropos-all-words (cons a apropos-all-words))))
+ (setq apropos-all-words-regexp
+ (apropos-words-to-regexp apropos-all-words ".+"))
+ (setq apropos-regexp
+ (apropos-words-to-regexp apropos-words ".*?")))
+ (setq apropos-pattern-quoted (regexp-quote pattern)
+ apropos-all-words-regexp pattern
+ apropos-pattern pattern
+ apropos-regexp pattern)))
+
+
+(defun apropos-calc-scores (str words)
+ "Return apropos scores for string STR matching WORDS.
+Value is a list of offsets of the words into the string."
+ (let (scores i)
+ (if words
+ (dolist (word words scores)
+ (if (setq i (string-match word str))
+ (setq scores (cons i scores))))
+ ;; Return list of start and end position of regexp
+ (and (string-match apropos-pattern str)
+ (list (match-beginning 0) (match-end 0))))))
+
+(defun apropos-score-str (str)
+ "Return apropos score for string STR."
+ (if str
+ (let* ((l (length str))
+ (score (- (/ l 10))))
+ (dolist (s (apropos-calc-scores str apropos-all-words) score)
+ (setq score (+ score 1000 (/ (* (- l s) 1000) l)))))
+ 0))
+
+(defun apropos-score-doc (doc)
+ "Return apropos score for documentation string DOC."
+ (let ((l (length doc)))
+ (if (> l 0)
+ (let ((score 0) i)
+ (when (setq i (string-match apropos-pattern-quoted doc))
+ (setq score 10000))
+ (dolist (s (apropos-calc-scores doc apropos-all-words) score)
+ (setq score (+ score 50 (/ (* (- l s) 50) l)))))
+ 0)))
+
+(defun apropos-score-symbol (symbol &optional weight)
+ "Return apropos score for SYMBOL."
+ (setq symbol (symbol-name symbol))
+ (let ((score 0)
+ (l (length symbol)))
+ (dolist (s (apropos-calc-scores symbol apropos-words) (* score (or weight 3)))
+ (setq score (+ score (- 60 l) (/ (* (- l s) 60) l))))))
+
+(defun apropos-true-hit (str words)
+ "Return t if STR is a genuine hit.
+This may fail if only one of the keywords is matched more than once.
+This requires that at least 2 keywords (unless only one was given)."
+ (or (not str)
+ (not words)
+ (not (cdr words))
+ (> (length (apropos-calc-scores str words)) 1)))
+
+(defun apropos-false-hit-symbol (symbol)
+ "Return t if SYMBOL is not really matched by the current keywords."
+ (not (apropos-true-hit (symbol-name symbol) apropos-words)))
+
+(defun apropos-false-hit-str (str)
+ "Return t if STR is not really matched by the current keywords."
+ (not (apropos-true-hit str apropos-words)))
+
+(defun apropos-true-hit-doc (doc)
+ "Return t if DOC is really matched by the current keywords."
+ (apropos-true-hit doc apropos-all-words))
+
+(define-derived-mode apropos-mode fundamental-mode "Apropos"