Hide long copyright message at top of all files
Asked Answered
K

4

5

We have 15 line long copyright messages at the top of all our source-code files.

When I have them open in emacs, that wastes a lot of valuable space.
Is there any way to get emacs to always a hide a certain message but still leave it in the file?

Khosrow answered 7/2, 2011 at 16:4 Comment(0)
R
3

You could write a function that narrows your buffer to everything but the first 15 lines.

(defun hide-copyright-note ()
  "Narrows the current buffer so that the first 15 lines are
hidden."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (forward-line 15)
    (narrow-to-region (point) (point-max))))

Then all you need to do is make sure that this function is called for every file that contains a copyright note. This can be done by adding a hook, preferably to the major mode of your file. For instance you could add the above function definition and the following line to your .emacs file:

(add-hook 'c-mode-hook 'hide-copyright-note)

This would call the function 'hide-copyright-note whenever you open a C file.

In practice, you would probably want to make your hook-function more clever, either by checking if a copyright note to hide actually exists or by running hide-copyright-note only if a file is in a certain directory etc.

For instance, to stick with the C example, you could insert the following test into the above function:

(defun hide-copyright-note ()
  "Narrows the current buffer so that the first 15 lines are
hidden."
  (interactive)
  (when (copyright-message-p)
    (save-excursion
      (goto-char (point-min))
      (forward-line 15)
      (narrow-to-region (point) (point-max)))))

(defun copyright-message-p ()
  "Returns t when the current buffer starts with a Copyright
note inside a C-style comment"
  (save-excursion
    (goto-char (point-min))
    (looking-at "\\s */\\*\\(:?\\s \\|\\*\\)*Copyright\\b")))

As for your other concern:

When I have them open in emacs, that wastes a lot of valuable space.

...or you could just scroll down. To achieve this automatically, we could use the following function instead of hide-copyright-note:

(defun scroll-on-copyright ()
  "Scrolls down to the 16th line when the current buffer starts
with a copyright note."
  (interactive)
  (when (copyright-message-p)
    (goto-char (point-min))
    (beginning-of-line 16)
    (recenter 0)))

However, the reason I recommended the first variation is that if you merely scroll down automatically, then whenever you jump to the beginning of the buffer (M-<) you'll have to scroll down again manually. This problem does not occur with the narrowing solution.

Raymonraymond answered 7/2, 2011 at 17:41 Comment(4)
Sorry for the delay @Thomas, the function works great! Do you have any suggestions about how to make this only apply to when the file starts with a copyright notice? For example I'd be glad to have it apply to anything starting with "/*\n * Copyright". As for your other comment, is there any way to have emacs automatically scroll forward 15 lines on open? That might be a better solution for simplicity. Thanks again.Khosrow
@sligocki: The test whether a file actually contains a copyright notice can easily be achieved using regular expression search. I've edited my answer to work for "/*\n * Copyright" as per your request - however, are you sure that that won't make the notice 16 lines long? In any case, instead of using a fixed number of lines, you could just search for the end of the initial comment ("*/") and jump to the first non-empty line after that. Doing that would make the approach more flexible for Copyright notices of different lengths.Raymonraymond
Note that this solution conflicts with line numbers (they aren't offset correctly).Cercaria
@Cercaria Have a look at this workaround: emacs.stackexchange.com/questions/24833/…Raymonraymond
A
9

You can use hideshow minor mode which is a standard built-in package that has a generalized command called hs-hide-initial-comment-block that will do what you want without having to know how long the top comment section is. You can add it to the mode-hook of any language, but here's an example using C:

(add-hook 'c-mode-common-hook 'hs-minor-mode t)
(add-hook 'c-mode-common-hook 'hs-hide-initial-comment-block t)

Note, it does not hide specifically just the copyrights, but the full initial comment block which may hide useful documentation, as well.

Antimere answered 24/12, 2012 at 4:40 Comment(0)
R
3

You could write a function that narrows your buffer to everything but the first 15 lines.

(defun hide-copyright-note ()
  "Narrows the current buffer so that the first 15 lines are
hidden."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (forward-line 15)
    (narrow-to-region (point) (point-max))))

Then all you need to do is make sure that this function is called for every file that contains a copyright note. This can be done by adding a hook, preferably to the major mode of your file. For instance you could add the above function definition and the following line to your .emacs file:

(add-hook 'c-mode-hook 'hide-copyright-note)

This would call the function 'hide-copyright-note whenever you open a C file.

In practice, you would probably want to make your hook-function more clever, either by checking if a copyright note to hide actually exists or by running hide-copyright-note only if a file is in a certain directory etc.

For instance, to stick with the C example, you could insert the following test into the above function:

(defun hide-copyright-note ()
  "Narrows the current buffer so that the first 15 lines are
hidden."
  (interactive)
  (when (copyright-message-p)
    (save-excursion
      (goto-char (point-min))
      (forward-line 15)
      (narrow-to-region (point) (point-max)))))

(defun copyright-message-p ()
  "Returns t when the current buffer starts with a Copyright
note inside a C-style comment"
  (save-excursion
    (goto-char (point-min))
    (looking-at "\\s */\\*\\(:?\\s \\|\\*\\)*Copyright\\b")))

As for your other concern:

When I have them open in emacs, that wastes a lot of valuable space.

...or you could just scroll down. To achieve this automatically, we could use the following function instead of hide-copyright-note:

(defun scroll-on-copyright ()
  "Scrolls down to the 16th line when the current buffer starts
with a copyright note."
  (interactive)
  (when (copyright-message-p)
    (goto-char (point-min))
    (beginning-of-line 16)
    (recenter 0)))

However, the reason I recommended the first variation is that if you merely scroll down automatically, then whenever you jump to the beginning of the buffer (M-<) you'll have to scroll down again manually. This problem does not occur with the narrowing solution.

Raymonraymond answered 7/2, 2011 at 17:41 Comment(4)
Sorry for the delay @Thomas, the function works great! Do you have any suggestions about how to make this only apply to when the file starts with a copyright notice? For example I'd be glad to have it apply to anything starting with "/*\n * Copyright". As for your other comment, is there any way to have emacs automatically scroll forward 15 lines on open? That might be a better solution for simplicity. Thanks again.Khosrow
@sligocki: The test whether a file actually contains a copyright notice can easily be achieved using regular expression search. I've edited my answer to work for "/*\n * Copyright" as per your request - however, are you sure that that won't make the notice 16 lines long? In any case, instead of using a fixed number of lines, you could just search for the end of the initial comment ("*/") and jump to the first non-empty line after that. Doing that would make the approach more flexible for Copyright notices of different lengths.Raymonraymond
Note that this solution conflicts with line numbers (they aren't offset correctly).Cercaria
@Cercaria Have a look at this workaround: emacs.stackexchange.com/questions/24833/…Raymonraymond
S
2

Have a look at folding-mode. Basically, all you need is a way to identify the parts to be folded, and then use folding-top-mark and folding-bottom-mark to mark them. There are hacks to do that with EMACS elisp code by the way, so you should be easily able to find code that can be adapted.

Sadoc answered 7/2, 2011 at 16:8 Comment(1)
Use hideshow minor mode which is a built-in package instead of folding-mode.Antimere
S
2

Emacs comes with elide-head, specifically for that, since Emacs-21. In Emacs-29 we rename it to elide-head-mode.

Saulsauls answered 15/5, 2022 at 22:25 Comment(4)
It's not working for me in ruby-mode, what am I doing wrong?Dre
@EvDolzhenko: You don't provide enough info even for my crystal ball.Saulsauls
sorry, but can you explain how one is supposed to use it? "Not working" in my case meant that I'm editing Ruby file (stock Emacs 30.0.50), then I do M-x elide-head-mode it says "mode enabled" and then nothing happens, header in the file with copyright info stays on the screen.Dre
@EvDolzhenko: Did you read the docstring? Does your header match something in elide-head-headers-to-hide?Saulsauls

© 2022 - 2024 — McMap. All rights reserved.