3

I want to set the variable truncate-lines to t in order to have truncated lines, but only in programming modes. So, following this thread, I put the following code in my configuration file:

(defun my-prog-mode-hook ()
  (setq truncate-lines t))
(add-hook 'prog-mode-hook #'my-prog-mode-hook)

My problem is this does not work, because truncate-lines is still nil when I start Emacs in fortran-mode (for example). The lambda version of the above code gives the same result.

What am I missing?

  • 1
    Inspect the value of prog-mode-hook with F1 v to make sure the function has been added to the hook. – wasamasa May 23 '17 at 17:20
  • 1
    And then make sure that some mode that derives from prog-mode does not, itself, change the value of truncate-lines to nil. You can also use M-x debug-on-entry my-prog-mode-hook to follow along and see what happens when it is invoked. – Drew May 23 '17 at 22:38
  • The variable prog-mode-hook contains my-prog-mode-hook, so I guess it is ok. Then when I M-x debug-on-entry my-prog-mode-hook, a list of functions that are run is displayed, but after a quick look, I haven't find any occurrence of truncate in their definition. But something came to my mind: I have (global-visual-line-mode 1) in my config file, could it be the culprit? – Giuseppe May 24 '17 at 9:30
  • (global-visual-line-mode 1) was most probably the problem. Thanks a lot to both of you anyway. – Giuseppe May 24 '17 at 9:57
3

One possibility to achieve what I wanted is to have

    (add-hook 'text-mode-hook 'visual-line-mode)

in addition to

    (defun my-prog-mode-hook ()
      (setq truncate-lines t))
    (add-hook 'prog-mode-hook #'my-prog-mode-hook)

which wraps long lines in text-mode while truncating them in programming modes.

Beware not to have any (global-visual-line-mode 1) instruction elsewhere in the config file, otherwise the above hooks don't work (the lines are always wrapped).

  • 3
    More precisely the hooks do work (my-prog-mode-hook successfully sets truncate-lines to t), but the globalized mode global-visual-line-mode subsequently triggers visual-line-mode (during after-change-major-mode-hook, after the mode hooks have run), which sets truncate-lines to nil, effectively undoing the earlier change. – phils May 24 '17 at 10:08
  • @phils Thank you for the precision. – Giuseppe May 24 '17 at 12:17

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.