4

This may really be a basic emacs lisp question, but I'm just starting to learn some Clojure -- tiptoeing along the first steps of the newbie's path -- and don't know any elisp yet.

How can one set Word Wrap mode to indent to the first character of the previous line?

Word-wrap visual line mode mode is good, but the indents are lost on wrap, making it harder to scan code by eye.

Ideally I'd want to indent up to a point, say, 30 characters max for example, to avoid overdoing it...

I'm guessing that to get this programmatically I'd need a conditional elisp statement in my emacs file.

It seems many people would have done this. Or is there a reason not to? How can it be implemented?

Thanks.

Drew
  • 75,699
  • 9
  • 109
  • 225
Mallory-Erik
  • 265
  • 1
  • 7

2 Answers2

9

I stumbled across an answer to this... on StackOverflow...

You can use the package Adaptive-Wrap.

It's pretty nice, there's an "extra indent" option you can use for the wrapped line, though no max indent (but seems like it could be added with just a line or two of code).

The author of the Stack Overflow post, Francesco, gives us the basic commands, and adds the snippet below to his init.el file. I tried adding it to my .emacs file. So far so good! At least in JavaScript and Clojure.

After having installed the package, just run the following commands:

  • M-x visual-line-mode RET (to wrap long lines)
  • M-x adaptive-wrap-prefix-mode RET (to make wrapped lines indent nicely)
(when (fboundp 'adaptive-wrap-prefix-mode)
  (defun my-activate-adaptive-wrap-prefix-mode ()
    "Toggle `visual-line-mode' and `adaptive-wrap-prefix-mode' simultaneously."
    (adaptive-wrap-prefix-mode (if visual-line-mode 1 -1)))
  (add-hook 'visual-line-mode-hook 'my-activate-adaptive-wrap-prefix-mode))

There is more about it, comments etc., at the original post on Stack Overflow from 2012.

Mallory-Erik
  • 265
  • 1
  • 7
4

word-wrap is pretty special variable as it's a variable that controls how Emacs' display engine operates when showing a buffer in a window. It is accompanied by wrap-prefix which allows you to prepend space to a wrapped line for the entire buffer:

(setq word-wrap t)
(setq wrap-prefix (make-string 30 ?\s))

There is no convenient setting for the behaviour you've described to. It could probably be implemented in the form of a minor mode that adds the wrap-prefix text property with the desired value to each line after examining the indentation of the previous line though.

wasamasa
  • 21,803
  • 1
  • 65
  • 97