1

I often find myself scrolling up in a code file to see which context I am in (class/method definition, yaml-key, etc). It would be super handy if I could somehow "freeze" the last lines of each indentation level that's lower than the last currently visible line.

For example:

class X:
  def a():
    # ...

  def b():
    # ...
    def some_local_function():
      # lots of code
      # ---- this is last line visible in the buffer/window
      # more visible code

In this case I would like the window to look like this:

class X:
  def b():
    def some_local_function():
      # some demarcation to indicate the lines above are separate from those below
      # ---- this is last line visible in the window
      # more visible code

Is there already a way to do this? If not, how would I go about implementing it?

3

I don't think anyone has already implemented exactly this, but you might look into code folding. Folding allows you to hide and show parts of the buffer on command. There are implementations of code folding for specific languages, as well as some which are more generic. Some are keyed off of indentation, others off of syntax elements (such as curly braces or parentheses).

If you do want to "freeze" text at the top of the window, Emacs does support rendering a single line of text as a header for the window which is visible no matter where in the buffer you are. Presumably you could change that header as the cursor moves.

| improve this answer | |
  • 1
    The header text was also my thought. Refer to C-h i g (elisp)Header Lines. Possibly in conjunction with (or deriving from) which-function-mode (but the requirement almost definitely necessitates custom code in practice). – phils Apr 23 at 12:05
  • Hmm. It really does seem to be "a single line" though, so maybe not actually useful? The details appear to be buried somewhere in the C code. – phils Apr 23 at 12:23
  • Yes, there is only one header line per window. You would not be able to use it to implement your exact idea, but you could implement something similar if you wanted. – db48x Apr 23 at 17:00
1

Have you tried the various code navigation bindings? I often set the mark to remember where I start and then do C-M-u repeatedly to go up in the code structure. Then C-x C-x gets me back to my mark (and C-g gets rid of the active region if necessary).

There are other such bindings: C-M-f goes forward one statement at the same level and C-M-b goes back. So you can do C-M-u repeatedly until you get to the def and then do C-M-f repeatedly to get to the end of each subsequent def.

That allows you to check where in the code you are, but you are still required to remember it: there is no "permanent record" the way that you want.

But it is still very useful and it works in any programming language that Emacs knows about. You can find more information in the Editing Programs section of the Emacs manual for much more information about these and related topics.

| improve this answer | |

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.