5.1 Stack Manipulation Commands

To duplicate the top object on the stack, press RET or SPC (two equivalent keys for the calc-enter command). Given a positive numeric prefix argument, these commands duplicate several elements at the top of the stack. Given a negative argument, these commands duplicate the specified element of the stack. Given an argument of zero, they duplicate the entire stack. For example, with ‘10 20 30’ on the stack, RET creates ‘10 20 30 30’, C-u 2 RET creates ‘10 20 30 20 30’, C-u - 2 RET creates ‘10 20 30 20’, and C-u 0 RET creates ‘10 20 30 10 20 30’.

The LFD (calc-over) command (on a key marked Line-Feed if you have it, else on C-j) is like calc-enter except that the sign of the numeric prefix argument is interpreted oppositely. Also, with no prefix argument the default argument is 2. Thus with ‘10 20 30’ on the stack, LFD and C-u 2 LFD are both equivalent to C-u - 2 RET, producing ‘10 20 30 20’.

To remove the top element from the stack, press DEL (calc-pop). The C-d key is a synonym for DEL. (If the top element is an incomplete object with at least one element, the last element is removed from it.) Given a positive numeric prefix argument, several elements are removed. Given a negative argument, the specified element of the stack is deleted. Given an argument of zero, the entire stack is emptied. For example, with ‘10 20 30’ on the stack, DEL leaves ‘10 20’, C-u 2 DEL leaves ‘10’, C-u - 2 DEL leaves ‘10 30’, and C-u 0 DEL leaves an empty stack.

The M-DEL (calc-pop-above) command is to DEL what LFD is to RET: It interprets the sign of the numeric prefix argument in the opposite way, and the default argument is 2. Thus M-DEL by itself removes the second-from-top stack element, leaving the first, third, fourth, and so on; M-3 M-DEL deletes the third stack element.

The above commands do not depend on the location of the cursor. If the customizable variable calc-context-sensitive-enter is non-nil (see Customizing Calc), these commands will become context sensitive. For example, instead of duplicating the top of the stack, RET will copy the element at the cursor to the top of the stack. With a positive numeric prefix, a copy of the element at the cursor and the appropriate number of preceding elements will be placed at the top of the stack. A negative prefix will still duplicate the specified element of the stack regardless of the cursor position. Similarly, DEL will remove the corresponding elements from the stack.

To exchange the top two elements of the stack, press TAB (calc-roll-down). Given a positive numeric prefix argument, the specified number of elements at the top of the stack are rotated downward. Given a negative argument, the entire stack is rotated downward the specified number of times. Given an argument of zero, the entire stack is reversed top-for-bottom. For example, with ‘10 20 30 40 50’ on the stack, TAB creates ‘10 20 30 50 40’, C-u 3 TAB creates ‘10 20 50 30 40’, C-u - 2 TAB creates ‘40 50 10 20 30’, and C-u 0 TAB creates ‘50 40 30 20 10’.

The command M-TAB (calc-roll-up) is analogous to TAB except that it rotates upward instead of downward. Also, the default with no prefix argument is to rotate the top 3 elements. For example, with ‘10 20 30 40 50’ on the stack, M-TAB creates ‘10 20 40 50 30’, C-u 4 M-TAB creates ‘10 30 40 50 20’, C-u - 2 M-TAB creates ‘30 40 50 10 20’, and C-u 0 M-TAB creates ‘50 40 30 20 10’.

A good way to view the operation of TAB and M-TAB is in terms of moving a particular element to a new position in the stack. With a positive argument n, TAB moves the top stack element down to level n, making room for it by pulling all the intervening stack elements toward the top. M-TAB moves the element at level n up to the top. (Compare with LFD, which copies instead of moving the element in level n.)

With a negative argument -n, TAB rotates the stack to move the object in level n to the deepest place in the stack, and the object in level n+1 to the top. M-TAB rotates the deepest stack element to be in level n, also putting the top stack element in level n+1.

See Selecting Sub-Formulas, for a way to apply these commands to any portion of a vector or formula on the stack.

The command C-x C-t (calc-transpose-lines) will transpose the stack object determined by the point with the stack object at the next higher level. For example, with ‘10 20 30 40 50’ on the stack and the point on the line containing ‘30’, C-x C-t creates ‘10 20 40 30 50’. More generally, C-x C-t acts on the stack objects determined by the current point (and mark) similar to how the text-mode command transpose-lines acts on lines. With argument n, C-x C-t will move the stack object at the level above the current point and move it past N other objects; for example, with ‘10 20 30 40 50’ on the stack and the point on the line containing ‘30’, C-u 2 C-x C-t creates ‘10 40 20 30 50’. With an argument of 0, C-x C-t will switch the stack objects at the levels determined by the point and the mark.