all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Re: List of major modes? // "switch-to-mode"??
@ 2005-11-11 12:27 David Reitter
  2005-11-11 17:00 ` Drew Adams
  2005-11-11 19:30 ` Kevin Rodgers
  0 siblings, 2 replies; 6+ messages in thread
From: David Reitter @ 2005-11-11 12:27 UTC (permalink / raw)
  Cc: Kevin Rodgers, Edward O'Connor

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

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: List of major modes? // "switch-to-mode"??
  2005-11-11 12:27 David Reitter
@ 2005-11-11 17:00 ` Drew Adams
  2005-11-11 18:45   ` David Reitter
  2005-11-11 19:30 ` Kevin Rodgers
  1 sibling, 1 reply; 6+ messages in thread
From: Drew Adams @ 2005-11-11 17:00 UTC (permalink / raw)
  Cc: David Reitter

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: List of major modes? // "switch-to-mode"??
  2005-11-11 17:00 ` Drew Adams
@ 2005-11-11 18:45   ` David Reitter
  2005-11-11 19:04     ` Drew Adams
  0 siblings, 1 reply; 6+ messages in thread
From: David Reitter @ 2005-11-11 18:45 UTC (permalink / raw)
  Cc: Emacs help

Hi Drew,

thanks for your detailed explanations. Icicles is very useful indeed.

Too bad the package installs stuff right upon loading (and actually  
activates the mode as well!), which it shouldn't do...
I'll look more into configuration things later.

For now, I'll try to find a predicate that reliably determines a list  
of major modes. That isn't trivial, as the ongoing discussion shows.
In particular when only autoload information is available, it seems  
like it's pretty much impossible.
This is sad, because a novice user would certainly be interested in a  
list of installed major modes.

- D

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: List of major modes? // "switch-to-mode"??
  2005-11-11 18:45   ` David Reitter
@ 2005-11-11 19:04     ` Drew Adams
  0 siblings, 0 replies; 6+ messages in thread
From: Drew Adams @ 2005-11-11 19:04 UTC (permalink / raw)


    thanks for your detailed explanations. Icicles is very useful indeed.

    Too bad the package installs stuff right upon loading (and actually
    activates the mode as well!), which it shouldn't do...

It should do just what it does. It follows the conventions for a minor mode.
Those conventions were discussed recently in emacs-devel (e.g. wrt a non-nil
initial value for the mode), and I've confirmed that Icicles is doing things
the right way in this regard.

You can easily prevent activating the mode upon load. Simply put this in
your .emacs, before loading icicles.el.

 (setq icicle-mode nil)

The mode will then be inactive when you load the library, and remain so
until you explicitly activate it.

    I'll look more into configuration things later.

There's nothing to configure, unless you want to change some of the
user-option values.

    For now, I'll try to find a predicate that reliably determines a list
    of major modes. That isn't trivial, as the ongoing discussion shows.
    In particular when only autoload information is available, it seems
    like it's pretty much impossible.
    This is sad, because a novice user would certainly be interested in a
    list of installed major modes.

See my previous message - I doubt you will find a fool-proof test for
major-modeness that correctly excludes minor modes.

If you have control over the installation (e.g. Aquamacs), then why can't
you test against an explicit list of the known, installed major modes?

Another possibility (workaround) is to augment your predicate with a test
against a known list of minor modes that aren't in `minor-mode-alist' etc.

HTH - Drew

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: List of major modes? // "switch-to-mode"??
  2005-11-11 12:27 David Reitter
  2005-11-11 17:00 ` Drew Adams
@ 2005-11-11 19:30 ` Kevin Rodgers
  1 sibling, 0 replies; 6+ messages in thread
From: Kevin Rodgers @ 2005-11-11 19:30 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: List of major modes? // "switch-to-mode"??
@ 2005-11-12  2:09 David Reitter
  0 siblings, 0 replies; 6+ messages in thread
From: David Reitter @ 2005-11-12  2:09 UTC (permalink / raw)


>
> In what way is "used interactively" different from `M-x'?
>
> What does switch-to-buffer have to do with major modes?


thanks for those tips.
That was a typo. I meant "switch-to-mode" as in the subject line.  
That should make my question clearer.

D

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-11-12  2:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-11-12  2:09 List of major modes? // "switch-to-mode"?? David Reitter
  -- strict thread matches above, loose matches on Subject: below --
2005-11-11 12:27 David Reitter
2005-11-11 17:00 ` Drew Adams
2005-11-11 18:45   ` David Reitter
2005-11-11 19:04     ` Drew Adams
2005-11-11 19:30 ` Kevin Rodgers

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.