6

Inside org-mode, RETURN is bound to (org-return &optional INDENT). This command apparently ignores C-u numeric prefixes and so if you want to enter 10 newlines, you can't just type C-u 1 0 RET and have it work. You can do C-u 1 0 M-x newline and get expected behavior, but I would like to know if there's a simpler set of default keystrokes?

3

If you just want to insert newlines, without any automation for indentation or support for soft newlines, then you can use quoted-insert (C-q), which lets you enter any character including control characters. A newline character is C-j, but the Return key sends C-m, so you need to type C-u 1 0 C-q C-j (insert a newline, times 10).

If you want to call the newline function, you can advise org-return. Untested.

(defadvice org-return
  (around org-return-prefix-argument (&optional indent arg) activate compile)
  (interactive (list nil current-prefix-arg))
  (letf (((symbol-function 'true-newline) (symbol-function 'newline)
          (symbol-function 'newline) (lambda () (true-newline arg))))
    ad-do-it))
  • There shouldn't be any need to advise (or redefine) org-return in this case, unless I'm missing something. That's overkill - just define your own command to do what you want, and bind that to RET. – Drew Oct 23 '14 at 17:04
  • @Drew The point is to use the prefix argument only in the cases when org-return would insert a newline, and not affect the other cases (in tables, etc.) – Gilles Oct 23 '14 at 17:06
  • Is org-return used only as a command (not used in Lisp code)? Or if it is used also in Lisp code, should what is requested not affect also that code? If so, then why can't you just define a new command to do what you want, and not change org-return? The prefix-arg behavior should not enter into the question of what command to use, but only what the command should do. – Drew Oct 23 '14 at 17:09
  • @Drew I didn't check, I think it's only intended for interactive use. I chose to implement a semantics where C-u RET in a table would do the same thing as before and ignore the prefix argument. This requires going through the cond inside org-return. The other semantics where specifying a prefix argument forces the insertion of newlines makes sense and would not call for an advice-based implementation, it's just not what I chose to implement. – Gilles Oct 23 '14 at 17:16
  • @Gilles org-return is used in org-return-indent in org.el. But that shouldn't affect it as you added arg as the last argument, correct? – Kaushal Modi Oct 23 '14 at 17:51
3

Here's another solution:

(defun my-org-return (&optional arg indent)
  (interactive "*p")
  (dotimes (number arg) (org-return indent)))
(define-key org-mode-map (kbd "<return>") 'my-org-return)
  • This is probably better than the other rebinding since it won't require updating the redefined function with any changes that are made in future releases of org. – William Everett Oct 23 '14 at 16:59
  • It is always preferable to use your own command instead of advising or redefining another command - if you get the desired behavior by doing that, and if other things are equal. – Drew Oct 23 '14 at 17:02
1

You can redefine org-return and org-return-indent as follows in your init.el (I have redefined the function found in emacs/24.4/lisp.org/org.el:

After the redefinition, you can do C-u10RET.

(defun org-return (&optional arg indent)
  "Goto next table row or insert a newline.

Calls `org-table-next-row' or `newline', depending on context.
See the individual commands for more information.
With ARG, call `org-return' that many times."
  (interactive "P")
  (let (org-ts-what)
    (cond
     ((or (bobp) (org-in-src-block-p))
      (if indent (newline-and-indent) (newline arg)))
     ((org-at-table-p)
      (org-table-justify-field-maybe)
      (call-interactively 'org-table-next-row))
     ;; when `newline-and-indent' is called within a list, make sure
     ;; text moved stays inside the item.
     ((and (org-in-item-p) indent)
      (if (and (org-at-item-p) (>= (point) (match-end 0)))
          (progn
            (save-match-data (newline arg))
            (org-indent-line-to (length (match-string 0))))
    (let ((ind (org-get-indentation)))
      (newline arg)
      (if (org-looking-back org-list-end-re)
              (org-indent-line)
        (org-indent-line-to ind)))))
     ((and org-return-follows-link
       (org-at-timestamp-p t)
       (not (eq org-ts-what 'after)))
      (org-follow-timestamp-link))
     ((and org-return-follows-link
       (let ((tprop (get-text-property (point) 'face)))
         (or (eq tprop 'org-link)
         (and (listp tprop) (memq 'org-link tprop)))))
      (call-interactively 'org-open-at-point))
     ((and (org-at-heading-p)
       (looking-at
        (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
      (org-show-entry)
      (end-of-line 1)
      (newline arg))
     (t (if indent (newline-and-indent) (newline arg))))))

Note that in newline-and-indent, the ARG input of newline is set to nil. So the same is done in the renewed definition of org-return-indent.

(defun org-return-indent ()
  "Goto next table row or insert a newline and indent.
Calls `org-table-next-row' or `newline-and-indent', depending on
context.  See the individual commands for more information."
  (interactive)
  (org-return nil t))
  • Why redefine the command that RET is bound to by default in org-mode, instead of just binding RET to the command that you want? IOW, it the behavior you code is what is wanted, why not call that command my-org-return and bind it to RET in org-mode? – Drew Oct 23 '14 at 16:37
  • @Drew I agree, I made it unnecessary complicated :P – Kaushal Modi Oct 23 '14 at 16:38
  • @Drew Thanks for the nudge; I've added another answer that should do exactly the same as this one. I yet have to venture into using defadvices. – Kaushal Modi Oct 23 '14 at 16:56
  • You can also delete this answer and leave just the other one, if you like. (Or you can leave it, if you think it or the comments are helpful.) – Drew Oct 23 '14 at 17:01

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.