comparison lisp/calc/calc-sel.el @ 88155:d7ddb3e565de

sync with trunk
author Henrik Enberg <henrik.enberg@telia.com>
date Mon, 16 Jan 2006 00:03:54 +0000
parents 0d8b17d428b5
children
comparison
equal deleted inserted replaced
88154:8ce476d3ba36 88155:d7ddb3e565de
1 ;;; calc-sel.el --- data selection functions for Calc 1 ;;; calc-sel.el --- data selection functions for Calc
2 2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. 3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
4 5
5 ;; Author: David Gillespie <daveg@synaptics.com> 6 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainers: D. Goel <deego@gnufans.org> 7 ;; Maintainer: Jay Belanger <belanger@truman.edu>
7 ;; Colin Walters <walters@debian.org>
8 8
9 ;; This file is part of GNU Emacs. 9 ;; This file is part of GNU Emacs.
10 10
11 ;; GNU Emacs is distributed in the hope that it will be useful, 11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor 12 ;; but WITHOUT ANY WARRANTY. No author or distributor
26 ;;; Commentary: 26 ;;; Commentary:
27 27
28 ;;; Code: 28 ;;; Code:
29 29
30 ;; This file is autoloaded from calc-ext.el. 30 ;; This file is autoloaded from calc-ext.el.
31
31 (require 'calc-ext) 32 (require 'calc-ext)
32
33 (require 'calc-macs) 33 (require 'calc-macs)
34 34
35 (defun calc-Need-calc-sel () nil)
36
37
38 ;;; Selection commands. 35 ;;; Selection commands.
39 36
40 (defvar calc-keep-selection t) 37 (defvar calc-keep-selection t)
38
39 (defvar calc-selection-cache-entry nil)
40 (defvar calc-selection-cache-num)
41 (defvar calc-selection-cache-comp)
42 (defvar calc-selection-cache-offset)
43 (defvar calc-selection-true-num)
41 44
42 (defun calc-select-here (num &optional once keep) 45 (defun calc-select-here (num &optional once keep)
43 (interactive "P") 46 (interactive "P")
44 (calc-wrapper 47 (calc-wrapper
45 (calc-prepare-selection) 48 (calc-prepare-selection)
139 num))) 142 num)))
140 (if sel 143 (if sel
141 (calc-change-current-selection sel) 144 (calc-change-current-selection sel)
142 (error "%d is not a valid sub-formula index" num))))) 145 (error "%d is not a valid sub-formula index" num)))))
143 146
144 (defun calc-find-nth-part (expr num) 147 ;; The variables calc-fnp-op and calc-fnp-num are local to
148 ;; calc-find-nth-part (and calc-select-previous) but used by
149 ;; calc-find-nth-part-rec, which is called by them.
150 (defvar calc-fnp-op)
151 (defvar calc-fnp-num)
152
153 (defun calc-find-nth-part (expr calc-fnp-num)
145 (if (and calc-assoc-selections 154 (if (and calc-assoc-selections
146 (assq (car-safe expr) calc-assoc-ops)) 155 (assq (car-safe expr) calc-assoc-ops))
147 (let (op) 156 (let (calc-fnp-op)
148 (calc-find-nth-part-rec expr)) 157 (calc-find-nth-part-rec expr))
149 (if (eq (car-safe expr) 'intv) 158 (if (eq (car-safe expr) 'intv)
150 (and (>= num 1) (<= num 2) (nth (1+ num) expr)) 159 (and (>= calc-fnp-num 1) (<= calc-fnp-num 2) (nth (1+ calc-fnp-num) expr))
151 (and (not (Math-primp expr)) (>= num 1) (< num (length expr)) 160 (and (not (Math-primp expr)) (>= calc-fnp-num 1) (< calc-fnp-num (length expr))
152 (nth num expr))))) 161 (nth calc-fnp-num expr)))))
153 162
154 (defun calc-find-nth-part-rec (expr) ; uses num, op 163 (defun calc-find-nth-part-rec (expr) ; uses num, op
155 (or (if (and (setq op (assq (car-safe (nth 1 expr)) calc-assoc-ops)) 164 (or (if (and (setq calc-fnp-op (assq (car-safe (nth 1 expr)) calc-assoc-ops))
156 (memq (car expr) (nth 1 op))) 165 (memq (car expr) (nth 1 calc-fnp-op)))
157 (calc-find-nth-part-rec (nth 1 expr)) 166 (calc-find-nth-part-rec (nth 1 expr))
158 (and (= (setq num (1- num)) 0) 167 (and (= (setq calc-fnp-num (1- calc-fnp-num)) 0)
159 (nth 1 expr))) 168 (nth 1 expr)))
160 (if (and (setq op (assq (car-safe (nth 2 expr)) calc-assoc-ops)) 169 (if (and (setq calc-fnp-op (assq (car-safe (nth 2 expr)) calc-assoc-ops))
161 (memq (car expr) (nth 2 op))) 170 (memq (car expr) (nth 2 calc-fnp-op)))
162 (calc-find-nth-part-rec (nth 2 expr)) 171 (calc-find-nth-part-rec (nth 2 expr))
163 (and (= (setq num (1- num)) 0) 172 (and (= (setq calc-fnp-num (1- calc-fnp-num)) 0)
164 (nth 2 expr))))) 173 (nth 2 expr)))))
165 174
166 (defun calc-select-next (num) 175 (defun calc-select-next (num)
167 (interactive "p") 176 (interactive "p")
168 (if (< num 0) 177 (if (< num 0)
237 (calc-change-current-selection sel)) 246 (calc-change-current-selection sel))
238 (if (Math-primp (car entry)) 247 (if (Math-primp (car entry))
239 (calc-change-current-selection (car entry)) 248 (calc-change-current-selection (car entry))
240 (let ((len (if (and calc-assoc-selections 249 (let ((len (if (and calc-assoc-selections
241 (assq (car (car entry)) calc-assoc-ops)) 250 (assq (car (car entry)) calc-assoc-ops))
242 (let (op (num 0)) 251 (let (calc-fnp-op (calc-fnp-num 0))
243 (calc-find-nth-part-rec (car entry)) 252 (calc-find-nth-part-rec (car entry))
244 (- 1 num)) 253 (- 1 calc-fnp-num))
245 (length (car entry))))) 254 (length (car entry)))))
246 (calc-select-part (- len num))))))))) 255 (calc-select-part (- len num)))))))))
247 256
248 (defun calc-find-parent-formula (expr part) 257 (defun calc-find-parent-formula (expr part)
249 (cond ((eq expr part) t) 258 (cond ((eq expr part) t)
324 (calc-change-current-selection sel))))) 333 (calc-change-current-selection sel)))))
325 (message (if calc-show-selections 334 (message (if calc-show-selections
326 "Displaying only selected part of formulas" 335 "Displaying only selected part of formulas"
327 "Displaying all but selected part of formulas")))) 336 "Displaying all but selected part of formulas"))))
328 337
338 ;; The variables calc-final-point-line and calc-final-point-column
339 ;; are declared in calc.el, and are used throughout.
340 (defvar calc-final-point-line)
341 (defvar calc-final-point-column)
342
329 (defun calc-preserve-point () 343 (defun calc-preserve-point ()
330 (or (looking-at "\\.\n+\\'") 344 (or (looking-at "\\.\n+\\'")
331 (progn 345 (progn
332 (setq calc-final-point-line (+ (count-lines (point-min) (point)) 346 (setq calc-final-point-line (+ (count-lines (point-min) (point))
333 (if (bolp) 1 0)) 347 (if (bolp) 1 0))
355 (not calc-assoc-selections))) 369 (not calc-assoc-selections)))
356 (message (if calc-assoc-selections 370 (message (if calc-assoc-selections
357 "Selection treats a+b+c as a sum of three terms" 371 "Selection treats a+b+c as a sum of three terms"
358 "Selection treats a+b+c as (a+b)+c")))) 372 "Selection treats a+b+c as (a+b)+c"))))
359 373
360 (defvar calc-selection-cache-entry nil)
361 (defun calc-prepare-selection (&optional num) 374 (defun calc-prepare-selection (&optional num)
362 (or num (setq num (calc-locate-cursor-element (point)))) 375 (or num (setq num (calc-locate-cursor-element (point))))
363 (setq calc-selection-true-num num 376 (setq calc-selection-true-num num
364 calc-keep-selection t) 377 calc-keep-selection t)
365 (or (> num 0) (setq num 1)) 378 (or (> num 0) (setq num 1))
397 (while (setq x (cdr x)) 410 (while (setq x (cdr x))
398 (if (or (not (consp (car x))) 411 (if (or (not (consp (car x)))
399 (equal (car x) '(float 0 0))) 412 (equal (car x) '(float 0 0)))
400 (setcar x (list 'cplx (car x) 0)) 413 (setcar x (list 'cplx (car x) 0))
401 (calc-encase-atoms-rec (car x))))))) 414 (calc-encase-atoms-rec (car x)))))))
415
416 ;; The variable math-comp-sel-tag is local to calc-find-selected-part,
417 ;; but is used by math-comp-sel-flat-term and math-comp-add-string-sel
418 ;; in calccomp.el, which are called (indirectly) by calc-find-selected-part.
402 419
403 (defun calc-find-selected-part () 420 (defun calc-find-selected-part ()
404 (let* ((math-comp-sel-hpos (- (current-column) calc-selection-cache-offset)) 421 (let* ((math-comp-sel-hpos (- (current-column) calc-selection-cache-offset))
405 toppt 422 toppt
406 (lcount 0) 423 (lcount 0)
454 (if (nth 2 (car top)) 471 (if (nth 2 (car top))
455 (setq sel (if sel t (nth 2 (car top))))) 472 (setq sel (if sel t (nth 2 (car top)))))
456 (setq top (cdr top))) 473 (setq top (cdr top)))
457 sel)))) 474 sel))))
458 475
459 (defun calc-replace-sub-formula (expr old new) 476 ;; The variables calc-rsf-old and calc-rsf-new are local to
460 (setq new (calc-encase-atoms new)) 477 ;; calc-replace-sub-formula, but used by calc-replace-sub-formula-rec,
478 ;; which is called by calc-replace-sub-formula.
479 (defvar calc-rsf-old)
480 (defvar calc-rsf-new)
481
482 (defun calc-replace-sub-formula (expr calc-rsf-old calc-rsf-new)
483 (setq calc-rsf-new (calc-encase-atoms calc-rsf-new))
461 (calc-replace-sub-formula-rec expr)) 484 (calc-replace-sub-formula-rec expr))
462 485
463 (defun calc-replace-sub-formula-rec (expr) 486 (defun calc-replace-sub-formula-rec (expr)
464 (cond ((eq expr old) new) 487 (cond ((eq expr calc-rsf-old) calc-rsf-new)
465 ((Math-primp expr) expr) 488 ((Math-primp expr) expr)
466 (t 489 (t
467 (cons (car expr) 490 (cons (car expr)
468 (mapcar 'calc-replace-sub-formula-rec (cdr expr)))))) 491 (mapcar 'calc-replace-sub-formula-rec (cdr expr))))))
469 492
470 (defun calc-sel-error () 493 (defun calc-sel-error ()
471 (error "Illegal operation on sub-formulas")) 494 (error "Invalid operation on sub-formulas"))
472 495
473 (defun calc-replace-selections (n vals m) 496 (defun calc-replace-selections (n vals m)
474 (if (calc-top-selected n m) 497 (if (calc-top-selected n m)
475 (let ((num (length vals))) 498 (let ((num (length vals)))
476 (calc-preserve-point) 499 (calc-preserve-point)
579 (calc-top-list m (- n m -1)))) 602 (calc-top-list m (- n m -1))))
580 (sels (append (calc-top-list (- n m) 1 'sel) 603 (sels (append (calc-top-list (- n m) 1 'sel)
581 (calc-top-list m (- n m -1) 'sel)))) 604 (calc-top-list m (- n m -1) 'sel))))
582 (calc-pop-push-list n vals 1 sels))) 605 (calc-pop-push-list n vals 1 sels)))
583 606
607 ;; The variable calc-sel-reselect is local to several functions
608 ;; which call calc-auto-selection.
609 (defvar calc-sel-reselect)
610
584 (defun calc-auto-selection (entry) 611 (defun calc-auto-selection (entry)
585 (or (nth 2 entry) 612 (or (nth 2 entry)
586 (progn 613 (progn
587 (and (boundp 'reselect) (setq reselect nil)) 614 (setq calc-sel-reselect nil)
588 (calc-prepare-selection) 615 (calc-prepare-selection)
589 (calc-grow-assoc-formula (car entry) (calc-find-selected-part))))) 616 (calc-grow-assoc-formula (car entry) (calc-find-selected-part)))))
590 617
591 (defun calc-copy-selection () 618 (defun calc-copy-selection ()
592 (interactive) 619 (interactive)
609 (defun calc-enter-selection () 636 (defun calc-enter-selection ()
610 (interactive) 637 (interactive)
611 (calc-wrapper 638 (calc-wrapper
612 (calc-preserve-point) 639 (calc-preserve-point)
613 (let* ((num (max 1 (calc-locate-cursor-element (point)))) 640 (let* ((num (max 1 (calc-locate-cursor-element (point))))
614 (reselect calc-keep-selection) 641 (calc-sel-reselect calc-keep-selection)
615 (entry (calc-top num 'entry)) 642 (entry (calc-top num 'entry))
616 (expr (car entry)) 643 (expr (car entry))
617 (sel (or (calc-auto-selection entry) expr)) 644 (sel (or (calc-auto-selection entry) expr))
618 alg) 645 alg)
619 (let ((calc-dollar-values (list sel)) 646 (let ((calc-dollar-values (list sel))
624 (setq alg (calc-encase-atoms (car alg))) 651 (setq alg (calc-encase-atoms (car alg)))
625 (calc-pop-push-record-list 1 "repl" 652 (calc-pop-push-record-list 1 "repl"
626 (list (calc-replace-sub-formula 653 (list (calc-replace-sub-formula
627 expr sel alg)) 654 expr sel alg))
628 num 655 num
629 (list (and reselect alg)))))) 656 (list (and calc-sel-reselect alg))))))
630 (calc-handle-whys)))) 657 (calc-handle-whys))))
631 658
632 (defun calc-edit-selection () 659 (defun calc-edit-selection ()
633 (interactive) 660 (interactive)
634 (calc-wrapper 661 (calc-wrapper
635 (calc-preserve-point) 662 (calc-preserve-point)
636 (let* ((num (max 1 (calc-locate-cursor-element (point)))) 663 (let* ((num (max 1 (calc-locate-cursor-element (point))))
637 (reselect calc-keep-selection) 664 (calc-sel-reselect calc-keep-selection)
638 (entry (calc-top num 'entry)) 665 (entry (calc-top num 'entry))
639 (expr (car entry)) 666 (expr (car entry))
640 (sel (or (calc-auto-selection entry) expr)) 667 (sel (or (calc-auto-selection entry) expr))
641 alg) 668 alg)
642 (let ((str (math-showing-full-precision 669 (let ((str (math-showing-full-precision
643 (math-format-nice-expr sel (frame-width))))) 670 (math-format-nice-expr sel (frame-width)))))
644 (calc-edit-mode (list 'calc-finish-selection-edit 671 (calc-edit-mode (list 'calc-finish-selection-edit
645 num (list 'quote sel) reselect)) 672 num (list 'quote sel) calc-sel-reselect))
646 (insert str "\n")))) 673 (insert str "\n"))))
647 (calc-show-edit-buffer)) 674 (calc-show-edit-buffer))
648 675
676 (defvar calc-original-buffer)
677
678 ;; The variable calc-edit-disp-trail is local to calc-edit-finish,
679 ;; in calc-yank.el.
680 (defvar calc-edit-disp-trail)
681 (defvar calc-edit-top)
682
649 (defun calc-finish-selection-edit (num sel reselect) 683 (defun calc-finish-selection-edit (num sel reselect)
650 (let ((buf (current-buffer)) 684 (let ((buf (current-buffer))
651 (str (buffer-substring (point) (point-max))) 685 (str (buffer-substring calc-edit-top (point-max)))
652 (start (point))) 686 (start (point)))
653 (switch-to-buffer calc-original-buffer) 687 (switch-to-buffer calc-original-buffer)
654 (let ((val (math-read-expr str))) 688 (let ((val (math-read-expr str)))
655 (if (eq (car-safe val) 'error) 689 (if (eq (car-safe val) 'error)
656 (progn 690 (progn
657 (switch-to-buffer buf) 691 (switch-to-buffer buf)
658 (goto-char (+ start (nth 1 val))) 692 (goto-char (+ start (nth 1 val)))
659 (error (nth 2 val)))) 693 (error (nth 2 val))))
660 (calc-wrapper 694 (calc-wrapper
661 (calc-preserve-point) 695 (calc-preserve-point)
662 (if disp-trail 696 (if calc-edit-disp-trail
663 (calc-trail-display 1 t)) 697 (calc-trail-display 1 t))
664 (setq val (calc-encase-atoms (calc-normalize val))) 698 (setq val (calc-encase-atoms (calc-normalize val)))
665 (let ((expr (calc-top num 'full))) 699 (let ((expr (calc-top num 'full)))
666 (if (calc-find-sub-formula expr sel) 700 (if (calc-find-sub-formula expr sel)
667 (calc-pop-push-record-list 1 "edit" 701 (calc-pop-push-record-list 1 "edit"
675 (defun calc-sel-evaluate (arg) 709 (defun calc-sel-evaluate (arg)
676 (interactive "p") 710 (interactive "p")
677 (calc-slow-wrapper 711 (calc-slow-wrapper
678 (calc-preserve-point) 712 (calc-preserve-point)
679 (let* ((num (max 1 (calc-locate-cursor-element (point)))) 713 (let* ((num (max 1 (calc-locate-cursor-element (point))))
680 (reselect calc-keep-selection) 714 (calc-sel-reselect calc-keep-selection)
681 (entry (calc-top num 'entry)) 715 (entry (calc-top num 'entry))
682 (sel (or (calc-auto-selection entry) (car entry)))) 716 (sel (or (calc-auto-selection entry) (car entry))))
683 (calc-with-default-simplification 717 (calc-with-default-simplification
684 (let ((math-simplify-only nil)) 718 (let ((math-simplify-only nil))
685 (calc-modify-simplify-mode arg) 719 (calc-modify-simplify-mode arg)
686 (let ((val (calc-encase-atoms (calc-normalize sel)))) 720 (let ((val (calc-encase-atoms (calc-normalize sel))))
687 (calc-pop-push-record-list 1 "jsmp" 721 (calc-pop-push-record-list 1 "jsmp"
688 (list (calc-replace-sub-formula 722 (list (calc-replace-sub-formula
689 (car entry) sel val)) 723 (car entry) sel val))
690 num 724 num
691 (list (and reselect val)))))) 725 (list (and calc-sel-reselect val))))))
692 (calc-handle-whys)))) 726 (calc-handle-whys))))
693 727
694 (defun calc-sel-expand-formula (arg) 728 (defun calc-sel-expand-formula (arg)
695 (interactive "p") 729 (interactive "p")
696 (calc-slow-wrapper 730 (calc-slow-wrapper
697 (calc-preserve-point) 731 (calc-preserve-point)
698 (let* ((num (max 1 (calc-locate-cursor-element (point)))) 732 (let* ((num (max 1 (calc-locate-cursor-element (point))))
699 (reselect calc-keep-selection) 733 (calc-sel-reselect calc-keep-selection)
700 (entry (calc-top num 'entry)) 734 (entry (calc-top num 'entry))
701 (sel (or (calc-auto-selection entry) (car entry)))) 735 (sel (or (calc-auto-selection entry) (car entry))))
702 (calc-with-default-simplification 736 (calc-with-default-simplification
703 (let ((math-simplify-only nil)) 737 (let ((math-simplify-only nil))
704 (calc-modify-simplify-mode arg) 738 (calc-modify-simplify-mode arg)
711 (setq val (calc-encase-atoms val)) 745 (setq val (calc-encase-atoms val))
712 (calc-pop-push-record-list 1 "jexf" 746 (calc-pop-push-record-list 1 "jexf"
713 (list (calc-replace-sub-formula 747 (list (calc-replace-sub-formula
714 (car entry) sel val)) 748 (car entry) sel val))
715 num 749 num
716 (list (and reselect val)))))) 750 (list (and calc-sel-reselect val))))))
717 (calc-handle-whys)))) 751 (calc-handle-whys))))
718 752
719 (defun calc-sel-mult-both-sides (no-simp &optional divide) 753 (defun calc-sel-mult-both-sides (no-simp &optional divide)
720 (interactive "P") 754 (interactive "P")
721 (calc-wrapper 755 (calc-wrapper
722 (calc-preserve-point) 756 (calc-preserve-point)
723 (let* ((num (max 1 (calc-locate-cursor-element (point)))) 757 (let* ((num (max 1 (calc-locate-cursor-element (point))))
724 (reselect calc-keep-selection) 758 (calc-sel-reselect calc-keep-selection)
725 (entry (calc-top num 'entry)) 759 (entry (calc-top num 'entry))
726 (expr (car entry)) 760 (expr (car entry))
727 (sel (or (calc-auto-selection entry) expr)) 761 (sel (or (calc-auto-selection entry) expr))
728 (func (car-safe sel)) 762 (func (car-safe sel))
729 alg lhs rhs) 763 alg lhs rhs)
772 (list '* alg rhs)))))) 806 (list '* alg rhs))))))
773 (calc-pop-push-record-list 1 (if divide "div" "mult") 807 (calc-pop-push-record-list 1 (if divide "div" "mult")
774 (list (calc-replace-sub-formula 808 (list (calc-replace-sub-formula
775 expr sel alg)) 809 expr sel alg))
776 num 810 num
777 (list (and reselect alg))))) 811 (list (and calc-sel-reselect alg)))))
778 (calc-handle-whys)))) 812 (calc-handle-whys))))
779 813
780 (defun calc-sel-div-both-sides (no-simp) 814 (defun calc-sel-div-both-sides (no-simp)
781 (interactive "P") 815 (interactive "P")
782 (calc-sel-mult-both-sides no-simp t)) 816 (calc-sel-mult-both-sides no-simp t))
784 (defun calc-sel-add-both-sides (no-simp &optional subtract) 818 (defun calc-sel-add-both-sides (no-simp &optional subtract)
785 (interactive "P") 819 (interactive "P")
786 (calc-wrapper 820 (calc-wrapper
787 (calc-preserve-point) 821 (calc-preserve-point)
788 (let* ((num (max 1 (calc-locate-cursor-element (point)))) 822 (let* ((num (max 1 (calc-locate-cursor-element (point))))
789 (reselect calc-keep-selection) 823 (calc-sel-reselect calc-keep-selection)
790 (entry (calc-top num 'entry)) 824 (entry (calc-top num 'entry))
791 (expr (car entry)) 825 (expr (car entry))
792 (sel (or (calc-auto-selection entry) expr)) 826 (sel (or (calc-auto-selection entry) expr))
793 (func (car-safe sel)) 827 (func (car-safe sel))
794 alg lhs rhs) 828 alg lhs rhs)
816 (calc-normalize (list (if subtract '- '+) alg rhs))))) 850 (calc-normalize (list (if subtract '- '+) alg rhs)))))
817 (calc-pop-push-record-list 1 (if subtract "sub" "add") 851 (calc-pop-push-record-list 1 (if subtract "sub" "add")
818 (list (calc-replace-sub-formula 852 (list (calc-replace-sub-formula
819 expr sel alg)) 853 expr sel alg))
820 num 854 num
821 (list (and reselect alg))))) 855 (list (and calc-sel-reselect alg)))))
822 (calc-handle-whys)))) 856 (calc-handle-whys))))
823 857
824 (defun calc-sel-sub-both-sides (no-simp) 858 (defun calc-sel-sub-both-sides (no-simp)
825 (interactive "P") 859 (interactive "P")
826 (calc-sel-add-both-sides no-simp t)) 860 (calc-sel-add-both-sides no-simp t))
827 861
862 (provide 'calc-sel)
863
864 ;;; arch-tag: e5169792-777d-428f-bff5-acca66813fa2
828 ;;; calc-sel.el ends here 865 ;;; calc-sel.el ends here