Major and minor modes

classic Classic list List threaded Threaded
37 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Major and minor modes

Matthew Pritchard
Can anyone explain to me clearly how Major modes and minor modes work
together. Can any minor mode work within any major mode? How do these
work together?
Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Jean-Christophe Helary

> On Jun 14, 2017, at 8:58, M.R.P. <[hidden email]> wrote:
>
> Can anyone explain to me clearly how Major modes and minor modes work together. Can any minor mode work within any major mode? How do these work together?

Everything is explained in Chapter 20 of the Emacs Manual.

Jean-Christophe
Reply | Threaded
Open this post in threaded view
|

RE: Major and minor modes

Drew Adams
> > Can anyone explain to me clearly how Major modes and minor modes work
> together. Can any minor mode work within any major mode? How do these work
> together?
>
> Everything is explained in Chapter 20 of the Emacs Manual.

Depends what Emacs version you use.  For versions 24 and 25
it is chapter 23, "Major and Minor Modes".  For version 23
it is chapter 27, "Major Modes" and section and 57.1, "Minor
Modes".  It is here:
http://www.gnu.org/software/emacs/manual/html_node/emacs/Modes.html

In sum, a major mode is buffer-specific.
As such, it typically does something appropriate for that
buffer content.

A minor mode can be buffer-specific (local) or not (global).
Minor-mode key bindings override major-mode key bindings.
A minor mode can do anything at all.  What it does need not
be specific for the content of any particular buffer, and
hence for any particular major mode.

Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Emanuel Berg-4
In reply to this post by Matthew Pritchard
M.R.P. wrote:

> Can anyone explain to me clearly how Major
> modes and minor modes work together. Can any
> minor mode work within any major mode?

In general, the major mode is the big frame
and there is one and only one major mode to
help you edit the text in the buffer. So for C,
it is C/l defined in cc-mode.el, and so on.

The minor mode is a small piece of
functionality, like Line-Number to have the
current line shown in the mode bar. It is
a good thing it can be used in all but whatever
major mode so it doesn't have to be duplicated.

In principle I suppose you can have as many
minor modes as you want for any major mode but
it isn't difficult to predict a scenario where
the minor modes contradict each other and/or
work in opposite ways to what makes sense for
the underlying major mode.

I never heard of anyone putting up fences in
their code to prevent that, and it is a good
thing they didn't as that would make all code
interdependent and besides people aren't that
stupid to setup a system that doesn't make
sense anyway. And if they are, let's go
nuts already.

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Jean-Christophe Helary
In reply to this post by Drew Adams

> On Jun 14, 2017, at 10:49, Drew Adams <[hidden email]> wrote:
>
>>> Can anyone explain to me clearly how Major modes and minor modes work
>> together. Can any minor mode work within any major mode? How do these work
>> together?
>>
>> Everything is explained in Chapter 20 of the Emacs Manual.
>
> Depends what Emacs version you use.  For versions 24 and 25
> it is chapter 23, "Major and Minor Modes".  For version 23
> it is chapter 27, "Major Modes" and section and 57.1, "Minor
> Modes".

Interesting. I just noticed that the HTML and the PDF do not have the same chapter numbering.

The PDF for 24.5 and for 26.0.50 (that I built this morning) both use chapter 20 for modes.

Jean-Christophe
Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Emanuel Berg-4
Jean-Christophe Helary
<[hidden email]> writes:

> Interesting. I just noticed that the HTML and the
> PDF do not have the same chapter numbering.
>
> The PDF for 24.5 and for 26.0.50 (that I built this
> morning) both use chapter 20 for modes.

Better to use the built-in info Emacs manual. Then you
can't go wrong :)

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Emanuel Berg-4
In reply to this post by Drew Adams
Drew Adams <[hidden email]> writes:

> A minor mode can be buffer-specific (local) or not
> (global). Minor-mode key bindings override
> major-mode key bindings.

Good point. This goes against the intuition of some
people who think what is bigger should be the boss,
period.

One can think of it like a compass needle. It works on
the magneticism of Earth itself. However put a small
magnet next to it, and it will override the entire
planet! It is thoughtworthy.

I suppose the "size" in this matter isn't the size
alone but also the *proximity*, just like with Emacs
major and minor modes, the minor mode being closer to
the user...

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Krishnakant
In reply to this post by Drew Adams

Read my blog at https://digitallyfreeblog.wordpress.com/

Hopefully you will get more clarity on that subject.

Happy hacking.

Krishnakant.


On Wednesday 14 June 2017 07:19 AM, Drew Adams wrote:

>>> Can anyone explain to me clearly how Major modes and minor modes work
>> together. Can any minor mode work within any major mode? How do these work
>> together?
>>
>> Everything is explained in Chapter 20 of the Emacs Manual.
> Depends what Emacs version you use.  For versions 24 and 25
> it is chapter 23, "Major and Minor Modes".  For version 23
> it is chapter 27, "Major Modes" and section and 57.1, "Minor
> Modes".  It is here:
> http://www.gnu.org/software/emacs/manual/html_node/emacs/Modes.html
>
> In sum, a major mode is buffer-specific.
> As such, it typically does something appropriate for that
> buffer content.
>
> A minor mode can be buffer-specific (local) or not (global).
> Minor-mode key bindings override major-mode key bindings.
> A minor mode can do anything at all.  What it does need not
> be specific for the content of any particular buffer, and
> hence for any particular major mode.
>


Reply | Threaded
Open this post in threaded view
|

RE: Major and minor modes

Phillip Lord-3
In reply to this post by Drew Adams
On Wed, June 14, 2017 1:49 am, Drew Adams wrote:
> A minor mode can be buffer-specific (local) or not (global).
> Minor-mode key bindings override major-mode key bindings.
> A minor mode can do anything at all.  What it does need not
> be specific for the content of any particular buffer, and hence for any
> particular major mode.


I have thought for a long time that there are two forms of minor mode
which should be handled differently. Originally minor modes were clearly
meant to be global (auto-fill, line-number mode that sort of thing). But
there are lots of "single major mode" minor modes. Examples would be some
of the electric minor modes, things like Slime, CIDER or ensime which are
all minor modes more or less specific.

My own feeling is that minor modes have poor usability for these purposes.

Phil


Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Héctor Lahoz
In reply to this post by Emanuel Berg-4
Emanuel Berg wrote:

> In principle I suppose you can have as many
> minor modes as you want for any major mode but
> it isn't difficult to predict a scenario where
> the minor modes contradict each other and/or
> work in opposite ways to what makes sense for
> the underlying major mode.
>
> I never heard of anyone putting up fences in
> their code to prevent that, and it is a good
> thing they didn't as that would make all code
> interdependent and besides people aren't that
> stupid to setup a system that doesn't make
> sense anyway. And if they are, let's go
> nuts already.

Interesting matter. I have never thought of it
thoroughly but it seems it is more difficult
to write a minor mode. Because a minor mode
doesn't know what other minor modes are in
effect when it is running (environment).

Then I have this question. One major mode is
suitable for a type of data. Everything is text
but it is formatted differently. Usually in the
same buffer you have several sections with
different types. So, for example, you could have
some C code, then some text and then an ASCII
picture. What I do is changing the major mode
as I edit the different sections. Is there
some way to tell Emacs what major mode should
be used for each section so it can switch the mode
automatically when point enters that section?

Besides, major modes usually handle the buffer
as a whole thing for parsing and that stuff.

Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Emanuel Berg-4
Héctor Lahoz <[hidden email]> writes:

> Interesting matter. I have never thought of it
> thoroughly but it seems it is more difficult to
> write a minor mode. Because a minor mode doesn't
> know what other minor modes are in effect when it is
> running (environment).

They typically don't need to care about that because
it isn't that complicated. It is often a small piece
of functionality and the more isolated it can be the
better, and often it isn't difficult to do as it is
completely natural so you don't even think about it.

> Then I have this question. One major mode is
> suitable for a type of data. Everything is text but
> it is formatted differently. Usually in the same
> buffer you have several sections with different
> types. So, for example, you could have some C code,
> then some text and then an ASCII picture. What I do
> is changing the major mode as I edit the different
> sections. Is there some way to tell Emacs what major
> mode should be used for each section so it can
> switch the mode automatically when point enters
> that section?

There are solutions to deal with that but those are
exceptions. You keep the C source and the ASCII
picture apart. This is the same as above. It is sound
preceding. It makes everything more easy,
including Emacs.

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Tomas Zerolo
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, Jun 14, 2017 at 10:19:01AM +0200, Emanuel Berg wrote:

[...]

> > Then I have this question. One major mode is
> > suitable for a type of data. Everything is text but
> > it is formatted differently. Usually in the same
> > buffer you have several sections with different
> > types [...]

> There are solutions to deal with that but those are
> exceptions [...]
 
This is a well-known problem for those poor souls dealing
with "things Web", where you have nested languages (CSS
and Javascript in HTML, HTML in PHP and things). Thus,
several approaches to the problem of "multiple major modes"
have developed. But yes, that's a point where we might
need a more "fundamental" solution.

See https://www.emacswiki.org/emacs/MultipleModes

For another, somewhat refreshing, but different approach
(embedding snippets of code in many languages in some text,
and getting the results of executing those snippets back
into the text, the whole literate programming kaboodle),
start, e.g. here: http://orgmode.org/worg/org-contrib/babel/

Careful! this one has rabbit-hole quality ;-)

Cheers
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAllA+RUACgkQBcgs9XrR2kZ4dACcCNbQMKUzRgcmjoqZSdiQcdOE
LpwAn35uKBxojdDn4WkwQ25/1Bb5GpNt
=ijLS
-----END PGP SIGNATURE-----

Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Emanuel Berg-4
> This is a well-known problem for those poor
> souls dealing with "things Web", where you
> have nested languages (CSS and Javascript in
> HTML, HTML in PHP and things)

You said it: poor souls.

Is it unavoidable to nest languages? PHP I see
the situation because it produces HTML from
inline code. With CSS tho isn't a reference in
the HTML file enough?

Another example are Perl scripts to do
databases and have SQL commands executed.
That shouldn't be necessary tho as Perl is
a programming language and surely can just as
well extract those command from designated
seekwell files...

JavaScript obviously I only did at computer
school and as soon as it was done I drank ten
beers to make sure I forgot all about it...

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Emanuel Berg-4
Another thing to note is that the major mode is
just a tool, a good tool, but not a necessary
tool for every lite code snippet.

Here (last) is an example. It is a zsh wrapper
to some awk code that I found on the net.
Of course, that made the need for awk-mode even
smaller. But I did edit the awk, in the
Shell-script[zsh] mode, without even thinking
about not being in awk-mode.

And that is actually a good rule of thumb.
When you experience not being in the right mode
is a disadvantage to your work, then you should
think of a way to factor out the foreign code.

numbers () {
    # thank you: https://unix.stackexchange.com/a/13779
    echo "items\tsum\tlow\thigh\tmedian\tmean"
    sort -n | awk '
          BEGIN {
            c = 0;
            sum = 0;
          }
          $1 ~ /^[0-9]*(\.[0-9]*)?$/ {
            a[c++] = $1;
            sum += $1;
          }
          END {
            ave = int(sum/c) + 1;
            if ( (c % 2) == 1 ) {
              median = a[ int(c/2) ];
            } else {
              median = ( a[c/2] + a[c/2-1] ) / 2;
            }
            OFS="\t";
            print c, sum, a[0], a[c-1], median, ave
          }
        '
}

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Tomas Zerolo
In reply to this post by Emanuel Berg-4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, Jun 14, 2017 at 11:25:07AM +0200, Emanuel Berg wrote:
> > This is a well-known problem for those poor
> > souls dealing with "things Web", where you
> > have nested languages (CSS and Javascript in
> > HTML, HTML in PHP and things)
>
> You said it: poor souls.

But very subjective anyway. I half-regret having said that.

> Is it unavoidable to nest languages?

No. Is it unavoidable to use computers? No. Some like doing
that, though.

> PHP I see the situation because it produces HTML
> from inline code. With CSS tho isn't a reference
> in the HTML file enough?

Yes. Does the standard allow embedding? Yes. Some people like
doing it differently, and luckily, neither you nor me are entitled
(or empowered) to impose that.

Moreover, in this case, if you want to understand some random
page you got from the Web (assuming you want to stay with your
beloved Emacs), mixed-mode will come in handy. I know I appreciate
it, for sure.

> Another example are Perl scripts to do
> databases and have SQL commands executed.

There are tons of those -- everything "domain specific language"
goes a bit in that direction.

> JavaScript obviously I only did at computer
> school and as soon as it was done I drank ten
> beers to make sure I forgot all about it...

Don't underestimate Javascript. There's a lot to learn from that
(in the negative, but also in the positive).

Actually... don't underestimate anything ;-D

Cheers
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAllBCN4ACgkQBcgs9XrR2kYarQCdEM+KuWUrvyUA+xKXaCw5m3uM
vtMAnAy4qjoK6yW2FNau/YujixJMLmo/
=L3Vn
-----END PGP SIGNATURE-----

Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Emanuel Berg-4
> Don't underestimate Javascript. There's a lot
> to learn from that (in the negative, but also
> in the positive).

Sure - it is like "Old English". If you drink
enough of it, you start to like it...

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Emanuel Berg-4
In reply to this post by Tomas Zerolo
<[hidden email]> writes:

> Moreover, in this case, if you want to understand
> some random page you got from the Web (assuming you
> want to stay with your beloved Emacs), mixed-mode
> will come in handy. I know I appreciate it,
> for sure.

As for me, in the world of computers and in other
worlds as well, putting up with stuff that isn't to my
liking by tweaking tools only delay the inevitable
blow-up and failure.

If you don't like the way the dig is going, getting
the best shovels will not keep frustration at bay
for long.

> There are tons of those -- everything "domain
> specific language" goes a bit in that direction.

... - how exactly do you define that?

I always thought of Elisp, SQL, LaTeX, the dialects of
BASIC, etc. to be domain specific.

And I didn't mix up major modes every doing that!

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Emanuel Berg-4
Emanuel Berg <[hidden email]> writes:

>> There are tons of those -- everything "domain
>> specific language" goes a bit in that direction.
>
> ... - how exactly do you define that?
>
> I always thought of Elisp, SQL, LaTeX, the dialects
> of BASIC, etc. to be domain specific.

Perhaps one should make a distinction between
"platform specific" and "domain specific"?

LaTeX is domain specific to documents and SQL is
domain specific to databases.

But Elisp, while platform specific to Emacs, the
domain is limited only in the Turing sense, which
I never pretended to understand anyway (except for on
exam day).

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Tomas Zerolo
In reply to this post by Emanuel Berg-4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, Jun 14, 2017 at 01:39:55PM +0200, Emanuel Berg wrote:

> <[hidden email]> writes:
>
> > Moreover, in this case, if you want to understand
> > some random page you got from the Web (assuming you
> > want to stay with your beloved Emacs), mixed-mode
> > will come in handy. I know I appreciate it,
> > for sure.
>
> As for me, in the world of computers and in other
> worlds as well, putting up with stuff that isn't to my
> liking by tweaking tools only delay the inevitable
> blow-up and failure.

But you don't want to be at the mercy of websites out
there, do you? So best to have a sharp sword to slice
them in half, when they come, bouncing and throbbing
upon you (sorry for the somewhat disturbing imagery).

I haven't resigned to see the web out there *only*
through the browser, although it seems we are losing
this battle at the moment (the only serious browser
manufacturer out there is at the same time the dominant
ad placement seller: what can possibly go wrong?)

> If you don't like the way the dig is going, getting
> the best shovels will not keep frustration at bay
> for long.

Cannons! Big, loaded cannons. Not shovels. Some AI
to unravel the secret protocol going on between their
server and the disgusting blob of Javascript they just
sent me to execute on *my* computer ;-)

> > There are tons of those -- everything "domain
> > specific language" goes a bit in that direction.
>
> ... - how exactly do you define that?
>
> I always thought of Elisp, SQL, LaTeX, the dialects of
> BASIC, etc. to be domain specific.

Regular expressions. Date-time formats. Query languages à la
miniKanren. HTTP query strings. CSS selectors. GraphViz graph
descriptors. PGF/TiKZ. Do I really need to continue ;-)

Cheers
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAllBKdEACgkQBcgs9XrR2kZY9wCeMd03z0Zq/UwCrE4gHqEdLv90
GUMAnAramu6n1m2Fy+iJNyhQY6bzh6u6
=6l8F
-----END PGP SIGNATURE-----

Reply | Threaded
Open this post in threaded view
|

Re: Major and minor modes

Barry Margolin
In reply to this post by Matthew Pritchard
In article <[hidden email]>,
 "M.R.P." <[hidden email]> wrote:

> Can anyone explain to me clearly how Major modes and minor modes work
> together. Can any minor mode work within any major mode? How do these
> work together?

Some minor modes are modifications to a specific set of major modes, so
they can only work with those modes. For instance, electric mode
modifies a number of language modes.

Other minor modes are more general, and can work with any major mode.
For instance, auto-fill mode. But sometimes it's not very useful -- you
probably don't want to do auto filling when editing programs rather than
text.

--
Barry Margolin, [hidden email]
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
12