aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Belanger2009-07-29 03:42:19 +0000
committerJay Belanger2009-07-29 03:42:19 +0000
commit2845f6fd61b1945a54d336277252c9bb932858b5 (patch)
tree3aba4b89d0e1ed99cb721afddd0bf4de05ffc4a0
parent905fb90eb1149bca3a8ff21da413c7dd112273c2 (diff)
downloademacs-2845f6fd61b1945a54d336277252c9bb932858b5.tar.gz
emacs-2845f6fd61b1945a54d336277252c9bb932858b5.zip
(calc-transpose-lines): New function.
-rw-r--r--lisp/calc/calc-misc.el115
1 files changed, 114 insertions, 1 deletions
diff --git a/lisp/calc/calc-misc.el b/lisp/calc/calc-misc.el
index 7637a9e3260..7e89fecad83 100644
--- a/lisp/calc/calc-misc.el
+++ b/lisp/calc/calc-misc.el
@@ -57,7 +57,7 @@
57(declare-function math-pow-of-zero "calc-arith" (a b)) 57(declare-function math-pow-of-zero "calc-arith" (a b))
58(declare-function math-pow-zero "calc-arith" (a b)) 58(declare-function math-pow-zero "calc-arith" (a b))
59(declare-function math-pow-fancy "calc-arith" (a b)) 59(declare-function math-pow-fancy "calc-arith" (a b))
60 60(declare-function calc-locate-cursor-element "calc-yank" (pt))
61 61
62;;;###autoload 62;;;###autoload
63(defun calc-dispatch-help (arg) 63(defun calc-dispatch-help (arg)
@@ -452,6 +452,119 @@ Calc user interface as before (either C-x * C or C-x * K; initially C-x * C).
452 (t 452 (t
453 (calc-roll-up-stack (calc-stack-size) (- nn))))))) 453 (calc-roll-up-stack (calc-stack-size) (- nn)))))))
454 454
455;;;###autoload
456(defun calc-transpose-lines (&optional arg)
457 "Transpose previous line and current line.
458With argument ARG, move previous line past ARG lines.
459With argument 0, switch line point is in with line mark is in."
460 (interactive "p")
461 (setq arg (or arg 1))
462 (let (bot-line mid-line end-line
463 old-top-list new-top-list
464 bot-cell mid-cell
465 prev-mid-cell post-mid-cell post-bot-cell)
466 (calc-wrapper
467 (when (eq major-mode 'calc-mode)
468 (cond
469 ;; exchange point and mark
470 ((= 0 arg)
471 (setq bot-line (calc-locate-cursor-element (point))
472 mid-line (mark))
473 (if mid-line
474 (setq mid-line (calc-locate-cursor-element mid-line)
475 end-line (1+ mid-line))
476 (error "No mark set"))
477 (if (< bot-line mid-line)
478 (let ((temp mid-line))
479 (setq mid-line bot-line
480 bot-line temp))))
481 ;; move bot-line to mid-line that is above bot-line on stack (that is
482 ;; to say mid-line displayed below bot-line in *Calculator* buffer)
483 ((> arg 0)
484 (setq bot-line (1+ (calc-locate-cursor-element (point)))
485 mid-line (- bot-line arg)
486 end-line mid-line))
487 ;; move bot-line to mid-line that is above bot-line on stack (that is
488 ;; to say mid-line displayed below bot-line in *Calculator* buffer)
489 ((< arg 0)
490 (setq mid-line (1+ (calc-locate-cursor-element (point)))
491 bot-line (- mid-line arg)
492 end-line bot-line)))
493 (calc-check-stack bot-line)
494 (if (= 0 mid-line)
495 (error "Can't transpose beyond top"))
496 (setq old-top-list (nreverse (calc-top-list bot-line)))
497 ;; example: (arg = 2)
498 ;; old-top-list =
499 ;; 1 <-- top of stack (bottom of *Calculator* buffer)
500 ;; 2
501 ;; 3 <-- mid-line = 3
502 ;; 4 <-- point
503 ;; 5 <-- bot-line = 5
504 (dotimes (i mid-line)
505 (setq mid-cell old-top-list
506 old-top-list (cdr old-top-list))
507 (setcdr mid-cell new-top-list)
508 (setq new-top-list mid-cell))
509 ;; example follow-up:
510 ;; old-top-list =
511 ;; 4
512 ;; 5
513 ;; new-top-list =
514 ;; 3 <-- mid-cell
515 ;; 2
516 ;; 1
517 (setq prev-mid-cell old-top-list)
518 (dotimes (i (- bot-line mid-line))
519 (setq bot-cell old-top-list
520 old-top-list (cdr old-top-list))
521 (setcdr bot-cell new-top-list)
522 (setq new-top-list bot-cell))
523 (setq post-mid-cell (cdr mid-cell)
524 post-bot-cell (cdr bot-cell))
525 ;; example follow-up:
526 ;; new-top-list =
527 ;; 5 <-- bot-cell
528 ;; 4 <-- prev-mid-cell & post-bot-cell
529 ;; 3 <-- mid-cell
530 ;; 2 <-- post-mid-cell
531 ;; 1
532 (cond
533 ((= 0 arg); swap bot and mid
534 (setcdr mid-cell post-bot-cell)
535 (setcdr bot-cell post-mid-cell)
536 (setcdr prev-mid-cell bot-cell)
537 ;; example follow-up:
538 ;; 3 <-- mid-cell
539 ;; 4 <-- post-bot-cell & prev-mid-cell
540 ;; 5 <-- bot-cell
541 ;; 2 <-- post-mid-cell
542 ;; 1
543 (setq new-top-list mid-cell))
544 ((< 0 arg) ; move bot just after mid
545 (setcdr mid-cell bot-cell)
546 (setcdr bot-cell post-mid-cell)
547 ;; example follow-up:
548 ;; new-top-list =
549 ;; 4 <-- post-bot-cell
550 ;; 3 <-- mid-cell
551 ;; 5 <-- bot-cell
552 ;; 2 <-- post-mid-cell
553 ;; 1
554 (setq new-top-list post-bot-cell))
555 ((> 0 arg) ; move mid just before bot
556 (setcdr mid-cell bot-cell)
557 (setcdr prev-mid-cell post-mid-cell)
558 ;; example follow-up:
559 ;; new-top-list =
560 ;; 3 <-- mid-cell
561 ;; 5 <-- bot-cell
562 ;; 4 <-- prev-mid-cell
563 ;; 2 <-- post-mid-cell
564 ;; 1
565 (setq new-top-list mid-cell)))
566 (calc-pop-push-list bot-line new-top-list)))
567 (calc-cursor-stack-index (1- end-line))))
455 568
456 569
457 570