2
$\begingroup$

When you hit enter inside an item styled cell (e.g. Cell["", "Outline1"]), a new cell of the same type is created underneath the cell you are currently editing.

My question is: How can I alter this behavior so that when I type the return key the new cell is be created above it instead of below?

Motivation: A to-do list. I'd like keep adding items at the top of the notebook section.

$\endgroup$
5
  • $\begingroup$ Admittedly not a full answer, but this may help: NotebookPut[ Notebook[Reverse[{Cell["number 1"], Cell["number 2"], Cell["number 3"]}]]]; $\endgroup$ Jan 16, 2015 at 17:09
  • $\begingroup$ Let's say you are editing Cell which is "Outline1" style. Let's say you've typed "test" what is expected layout after hiting Enter? $\endgroup$
    – Kuba
    Jan 16, 2015 at 17:50
  • $\begingroup$ Then a new cell would be created directly above that one and the cursor moves into it... $\endgroup$
    – M.R.
    Jan 16, 2015 at 22:37
  • $\begingroup$ And if that cell had a dingbat with a counter it would be incremented on hitting Enter $\endgroup$
    – M.R.
    Jan 16, 2015 at 22:51
  • $\begingroup$ This is the default behavior of all to-do list apps! $\endgroup$
    – M.R.
    Jan 16, 2015 at 22:52

1 Answer 1

4
$\begingroup$

To add a cell below is trivial because we have the built in option ReturnCreatesNewCell which can be set to True for a given cell style. To add a cell above when you hit Return you can use CellEventActions like this:

Cell[StyleData["ToDo", StyleDefinitions -> StyleData["Text"]],
 CellDingbat->StyleBox["\[FilledSmallSquare]", Alignment -> Baseline, 
   RGBColor[0.8, 0.043, 0.008]],
 CounterIncrements->"ToDo",
 CellEventActions->{
"ReturnKeyDown":>(
SelectionMove[EvaluationNotebook[], Before, Cell];
NotebookWrite[EvaluationNotebook[], Cell["New Item", "ToDo"]];
SelectionMove[EvaluationNotebook[], Previous,Cell];
SelectionMove[EvaluationNotebook[],Before,CellContents])},
MenuSortingValue->3,
StyleMenuListing->True]

enter image description here

$\endgroup$
3
  • $\begingroup$ Nice! This tweak helps: Cell[StyleData["ToDo", StyleDefinitions -> StyleData["Text"]], CellDingbat->StyleBox["\[FilledSmallSquare]", Alignment -> Baseline, RGBColor[0.8, 0.043, 0.008]], GeneratedCell->True, CellAutoOverwrite->True, CellEventActions->{"ReturnKeyDown" :> (SelectionMove[ EvaluationNotebook[], Before, Cell]; NotebookWrite[ EvaluationNotebook[], Cell["", "ToDo"]];SelectionMove[ EvaluationNotebook[], Previous, CellContents]; )}, CellChangeTimes->{3.630533038640272*^9}, CounterIncrements->"ToDo", MenuSortingValue->3, StyleMenuListing->True] $\endgroup$
    – M.R.
    Jan 18, 2015 at 1:28
  • $\begingroup$ @M.R. agree. and also SelectionMove[EvaluationNotebook[],Before,CellContents] $\endgroup$ Jan 18, 2015 at 1:40
  • $\begingroup$ @M.R. thanks for taking your time and correcting my misinterpretation... $\endgroup$
    – Kuba
    Jan 18, 2015 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.