reddit / emacs

(global-linum-mode 1) causing issues with pdf viewing

Telefza · 2022-02-21 19:10
Hi, I set `(global-linum-mode 1)` in my `init.el` file, but it creates problems when viewing pdfs. Scrolling through them becomes sluggish. Removing the line numbering altogether fixes the problem. But I do want line numbering for when writing code/latex. I tried setting `(add-hook 'pdf-view-mode-hook (lambda() (linum-mode -1)))`, which I found [here](https://github.com/politza/pdf-tools/issues/189). But it doesn't seem to do anything. Can someone help me and if possible explain to me(noob) why it doesn't work. Edit: Thanks for the replies. What worked for me was setting `display-line-numbers-mode` for [prog-mode](https://www.emacswiki.org/emacs/ProgMode) and `text-mode`, which seem like really neat tricks. Code in my `init.el` file: (add-hook 'prog-mode-hook (lambda() (display-line-numbers-mode 1))) (add-hook 'text-mode-hook (lambda() (display-line-numbers-mode 1))) Bonus Question: I stumbled upon line numbering on the [emacs wiki](https://www.emacswiki.org/emacs/LineNumbers). It explains how to disable line numbering for certain modes, but I did not really understand the code -- don't know how to code lisp -- and felt more comfy with the solution that I do kind of understand. Would be cool if somebody could explain what this is doing and how it works. EditEdit: [PDF-tools](https://github.com/politza/pdf-tools) is weird. Apparently you cannot simply install from `list-packages` but have to put something like [here](http://alberto.am/2020-04-11-pdf-tools-as-default-pdf-viewer.html) in your `init.el` file and then install the OS packages for the [server-prerequisites](https://github.com/politza/pdf-tools#server-prerequisites). But then it works. Thanks all.
andreyorst · 2022-02-21 19:58
add hooks for specific modes. Also, check if the problem persist with the `global-display-line-numbers-mode` which is a much more robust line numbering package built into current stable emacs
Telefza · 2022-02-21 20:31
thanks for the reply. > add hooks for specific modes. so I would add a `(linum-mode 1)` for every hook I use ? > Also, check if the problem persist with the global-display-line-numbers-mode which is a much more robust line numbering package built into current stable emacs yes, line numbering is still there when viewing pdfs.
7890yuiop · 2022-02-21 20:36
> Can someone help me and if possible explain to me(noob) why it doesn't work. Your code works fine here, so I don't know what the issue is. However linum is old and relatively inefficient, so disable `global-linum-mode` and remove any `linum` config, and use this: (global-display-line-numbers-mode 1) (defun my-turn-off-line-numbers () "Disable line numbering in the current buffer." (display-line-numbers-mode -1)) (add-hook 'pdf-view-mode-hook #'my-turn-off-line-numbers) Alternatively ignore the global mode and only *enable* the buffer-local mode when you want it. Sounds like `prog-mode-hook` and the hook for whichever LaTeX mode you're using would cover most of it.
Telefza · 2022-02-21 21:03
thanks, but your code didn't work for me. Also I am not sure about the `#` in the last line of your code(it didn't work either way for me). What did however work is setting line numbering for `prog-mode-hook` and `text-mode-hook`. And special thanks for pointing this out because I did not know that you can set variables for modes from which a lot of other modes are derived from.
deaddyfreddy · 2022-02-21 21:22
do you really need the line numbering?
7890yuiop · 2022-02-21 22:16
Glad you found a working approach, but FYI something is potentially amiss in your config if that code wasn't working (given that you've successfully used `display-line-numbers-mode` in your solution). I presume you *are* using `pdf-tools` to view PDFs? Emacs will use DocView by default, but everything in your question is about `pdf-tools`. My other thought is that you're putting lambdas into your hook variables which isn't a good idea. Make sure you haven't left the hook variable littered with unwanted (yet still active) versions of those. Using named functions in hooks (in the manner I showed) avoids that problem (and a few others besides), so I strongly recommend always/only doing that. > I am not sure about the # in the last line of your code `'...` means `(quote ...)` and `#'...` means `(function ...)` where `function` is a variant of `quote` that provides additional hints to Emacs that the thing you're quoting is a function. Most of the time Emacs will treat function symbols quoted with `'` in much the same way; but using `#'` can help with byte-compilation, and when lexical-binding is in use there are some cases where the function syntax is *required*. It's good practice to use function-quoting whenever it's appropriate, though, regardless of whether it makes a difference.
flyin1501 · 2022-02-22 19:47
> I tried setting (add-hook 'pdf-view-mode-hook (lambda() (linum-mode -1))), which I found [here](https://github.com/politza/pdf-tools/issues/189). But it doesn't seem to do anything. This tells pdf-view-mode to turn off linum-mode. `(linum-mode -1)` would turn off linum-mode; in your case you're not using linum-mode in the first case, so this would never have an effect. When you open a file, Emacs detects its file type and runs an appropriate major mode for it. The major mode sets up the buffer for editing or viewing its content, but also runs a "hook" at the end of it. (A hook is basically just a list of functions.) - Are you really using pdf-view-mode? Press `C-h m` and skip through the first paragraph; there it tells you the major mode. - In that paragraph I said to skip, is linum-mode in there somewhere? Or is there a global-display-line-numbers-mode? (Most likely it's the latter) Assuming the answer is that you're not using pdf-view-mode to view PDFs (but the builtin doc-view) and you're using display-line-numbers-mode for line numbers, you have to tell doc-view-mode to disable display-line-numbers-mode instead: (add-hook 'doc-view-mode (lambda () (display-line-numbers-mode -1)))
Telefza · 2022-02-26 09:59
>I presume you are using pdf-tools to view PDFs? Emacs will use DocView by default, but everything in your question is about pdf-tools. I thought so. I installed it via list-packages. I then required it in my emacs `init.el` file, but every pdf file I open still opens in DocView. I don't get it.
7890yuiop · 2022-02-26 10:38
Well that explains why `pdf-view-mode-hook` had no effect. It always pays to verify assumptions. Check the pdf-tools documentation. It has external dependencies, for starters.