1

I'm new to Emacs. I'm trying to use the outline-mode with a specific requirement. I would like to have the number 1 corresponding to the level 1, the number 2 corresponding to level 2 and so on:

1keyword_a(first level)
2sub_keyword_aa(second level)
2sub_keyword_ab(second level)
3sub_sub_keyword_aba(third level)

1keyword_b(first level)
2sub_keyword_ba(second level)
3sub_sub_keyword_baa(third level)

I understand that outline levels works with the length of the match but I'm wondering whether my requirement is possible?

2

Drew already gave you the link to Outline Format in his answer. There follows a simple application example. You can copy the stuff into the *scratch*-buffer and call M-x eval-buffer RET.

(defcustom my-outline-regexp "\\([0-9]+\\)"
  "Regular expression to match the beginning of a heading with numbered level."
  :type 'regexp
  :group 'outline)

(defun my-outline-level ()
  "Return the outline level according to `my-outline-regexp'."
  (string-to-number (match-string 1)))

(define-derived-mode my-outline-mode outline-mode "myOutline"
  "Outline mode with numbered levels."
  (setq-local outline-regexp my-outline-regexp)
  (setq-local outline-level #'my-outline-level))

Afterwards it works as you describe it on a buffer with following content if you activate the major mode, e.g., with M-x my-outline-mode RET.

1keyword_a(first level)
2sub_keyword_aa(second level)
2sub_keyword_ab(second level)
3sub_sub_keyword_aba(third level)

1keyword_b(first level)
2sub_keyword_ba(second level)
3sub_sub_keyword_baa(third level)

If you really want to keep my-outline-mode you can copy the elisp code into your init file.

  • Waou! Thanks a lot. Exactly what I was looking for. It works. Just need now a liitle bit of time to understand everything. Thanks Drew and Tobias. – Alain Guillaume Apr 11 '18 at 7:59
0

Not too clear what you're asking. Are you asking how to use 1 instead of *, 2 instead of **, and so on? If so, have a look at variable outline-regexp. C-h v tells you:

Regular expression to match the beginning of a heading. Any line whose beginning matches this regexp is considered to start a heading. Note that Outline mode only checks this regexp at the start of a line, so the regexp need not (and usually does not) start with ^. The recommended way to set this is with a Local Variables: list in the file it applies to. See also outline-heading-end-regexp.

And from the Emacs manual, node Outline Format:

The length of the matching text determines the level of the heading; longer matches make a more deeply nested level. Thus, for example, if a text formatter has commands @chapter, @section and @subsection to divide the document into chapters and sections, you could make those lines count as heading lines by setting outline-regexp to "@chap\\|@\\(sub\\)*section".

Note the trick: the two words chapter and section are equally long, but by defining the regexp to match only chap we ensure that the length of the text matched on a chapter heading is shorter, so that Outline mode will know that sections are contained in chapters. This works as long as no other command starts with @chap.

You can explicitly specify a rule for calculating the level of a heading line by setting the variable outline-level. The value of outline-level should be a function that takes no arguments and returns the level of the current heading. The recommended ways to set this variable are in a major mode command or with a file local variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.