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: Drew Adams
Subject: RE: List of major modes? // "switch-to-mode"??
Date: Fri, 11 Nov 2005 09:00:18 -0800

    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) .

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

I don't really understand what you said about switch-to-buffer or M-x (did
you maybe mean `switch-to-mode'?). But, IIUC, the following is close to the
rest of what you describe. Users will need library `icicles.el':
http://www.emacswiki.org/cgi-bin/emacs/icicles.el. It provides pretty much
what you're asking for, out of the box (you need only one line of code).
Here's the user interaction:

M-x help-on-major-modes prompts you for a mode name. Anything you type is
matched against (only) major-mode names. Matching can be literal prefix
matching (like normal Emacs completion) or regexp matching. Use `TAB' for
the former, `S-TAB' for the latter.

That is, hit `S-TAB' or `TAB' at any time to complete your minibuffer input
and display the names of all major modes that match it. If the minibuffer is
empty (no input) when you do this, then _all_ major-mode names are
displayed.

The mode names are displayed as completion candidates in buffer
*Completions*. You can use the up and down (arrow) keys or the next and
prior keys (Page Up/Down) to cycle among the mode names in *Completions*. Up
and down use the normal, prefix-completion matching; next and prior use
regexp matching.

What you're interested in is this:

Use `C-o' to display the help on the current candidate (as determined by
completion or cycling). That is, use up, down, next, or prior to cycle among
the mode names; then use `C-o' to display the help for the mode-name
currently highlighted in the list. Or type part of a name, complete it with
`TAB' or `S-TAB', and then use `C-o' to display its help.

Or, just use `C-up', `C-down', `C-next', or `C-prior' to cycle among the
mode names that match your input, showing the help for each one, in turn.
For example, press and hold Control while using the down arrow, to display
the help for each mode, in turn.

If you are interested only in modes whose names contain "font" (anywhere in
the name), then type "font" in the minibuffer and hit `S-TAB' (then cycle
among the modes with `C-down'). This is similar to the name-matching that
`apropos' does, so I call this "apropos completion".

If you are interested only in modes whose names start with "toggle", then
type "tog" and hit `TAB'. This is the standard, literal prefix completion of
Emacs.

You asked for just the first line of doc. The code below gives the entire
doc for the mode (in buffer *Help*) - but that's close. You can use Icicles
to define a command that gives you only the first line of help, if you
really need that, but the whole help is available out of the box.

If you had not needed to filter the function names to just major-mode names,
_no coding at all_ would have been needed. Just use `C-h f', and then cycle
among the doc strings of all functions, as described above. To cycle among
the functions with "-mode" in their name, just use `C-h f', type "-mode" (or
"-mode$", if you want a match only at the end), then `S-TAB', then cycle
with `C-down'. The code below just adds your major-mode filter predicate to
`completing-read'.

Note that command `help-on-major-modes' doesn't do anything interesting with
the mode name it reads via `completing-read' - it just shows its help. You
can make it do something more interesting, such as activate that mode, and
still get the show-help functionality described above. That is, the `C-o'
and `C-down' show-help-on-mode-names is independent of what you finally do
if the user hits `RET' to choose a mode name. Replace `describe-function' by
`funcall' in the code below, and the chosen mode will be activated.

For more info on Icicles: http://www.emacswiki.org/cgi-bin/wiki/Icicles. For
info on the show-help functionality described here:
http://www.emacswiki.org/cgi-bin/wiki/Icicles#HelpOnCandidates.

HTH,

  Drew

--------8<-----------

;; Your filter predicate
;;
(defun major-mode-p (fn)
  (and (commandp fn)
       (string-match "-mode\\'" (symbol-name fn))
       (not (string-match "\\`turn-\\(on\\|off\\)-" (symbol-name fn)))
       (not (assq fn minor-mode-alist))))

;; One-liner command - it just calls `completing-read' with your filter.
;;
(defun help-on-major-modes ()
  "Show help on major modes. `S-TAB' to see all modes matching input.
Input-candidate completion and cycling are available.  While cycling,
these keys show help on the current candidate mode name:

`C-o'     - Show help on current candidate mode name only
`C-down'  - Show current, then move to next (prefix-completion)
`C-up'    - Show current, then move to previous (prefix-completion)
`C-next'  - Show current, then move to next (apropos-completion)
`C-prior' - Show current, then move to previous (apropos-completion)

Use `RET' or `C-g' to quit."
  (interactive)
  (describe-function
   (intern (completing-read "Mode: " obarray 'major-mode-p))))





reply via email to

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