help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: List of major modes? // "switch-to-mode"??


From: Kevin Rodgers
Subject: Re: List of major modes? // "switch-to-mode"??
Date: Fri, 11 Nov 2005 12:30:51 -0700
User-agent: Mozilla Thunderbird 0.9 (X11/20041105)

David Reitter wrote:
> Thanks for your help with this.
>
> Here is my fusion of your code, not using the documentation strings,
> but using apropos-internal to speed things up:
>
> (apropos-internal
> "-mode\\'"
> (lambda (mode)
>    (and (commandp mode)
>     (not (string-match "\\`turn-\\(on\\|off\\)-"
>                (symbol-name mode)))
>     (not (assq mode minor-mode-alist)))))
>
> Here's why I wanted this:
>
> I'd like to come up with a function switch-to-buffer that could be  used
> interactively instead of M-x, with the advantage that completion  would
> only complete mode names, and it would be able to display a  list of
> modes (together with the first lines of their documentation  strings) .

In what way is "used interactively" different from `M-x'?

What does switch-to-buffer have to do with major modes?

You can modify the interactive behavior of any command like this:

(defadvice some-command (before weird-interaction activate)
  "Read arguments with `weird-interaction'."
  (interactive (weird-interaction)))

> Has anyone already written something like this? (or is there an Emacs
> function that I don't know of...?)

If you want to display the documentation in the *Completions* buffer, it
will be returned along with the mode name.  But you could then select
just the mode name from that:

(let* ((major-modes
        (apropos-internal "-mode\\'"
                          (lambda (mode)
                            (and (commandp mode)
                                 (string-match "\\`Major mode\\>"
                                               (documentation mode))))))
       (completions
        (mapcar (lambda (mode)
                  (let* ((doc-string (documentation mode))
                         (text (format "%s (%s)"
                                       mode
                                       (substring doc-string 0
                                                  (string-match "\n"

doc-string)))))
                    (list text)))
                major-modes))
       (completed-string (completing-read "Major mode (w/docstring): "
                                          completions)))
  (substring completed-string 0 (string-match " " completed-string)))

It would be better if you could associate the docstring as a tool tip
over the mode name in the *Completions* buffer, but text properties seem
to get removed when the completions are inserted into the buffer:

(let* ((major-modes
        (apropos-internal "-mode\\'"
                          (lambda (mode)
                            (and (commandp mode)
                                 (string-match "\\`Major mode\\>"
                                               (documentation mode))))))
       (completions
        (mapcar (lambda (mode)
                  (let* ((doc-string (documentation mode))
                         (tooltip (substring doc-string 0
(string-match "\n" doc-string)))
                         (mode-name (copy-sequence (symbol-name mode))))
                    (put-text-property 0 (1- (length mode-name)) 'help-echo
                                       mode-name)
                    (list mode-name)))
                major-modes)))
  (completing-read "Major mode (w/docstring): " completions))

--
Kevin Rodgers





reply via email to

[Prev in Thread] Current Thread [Next in Thread]