2001-11-19 07:44:56 +00:00
|
|
|
;;; calc-yank.el --- kill-ring functionality for Calc
|
|
|
|
|
2005-07-14 20:05:30 +00:00
|
|
|
;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
|
2007-01-21 02:48:43 +00:00
|
|
|
;; 2005, 2006, 2007 Free Software Foundation, Inc.
|
2001-11-19 07:44:56 +00:00
|
|
|
|
|
|
|
;; Author: David Gillespie <daveg@synaptics.com>
|
2007-04-23 21:30:42 +00:00
|
|
|
;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
2007-03-19 20:59:53 +00:00
|
|
|
;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
;; any later version.
|
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2007-03-19 20:59:53 +00:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
;; Boston, MA 02110-1301, USA.
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2001-11-19 07:44:56 +00:00
|
|
|
;;; Commentary:
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2001-11-19 07:44:56 +00:00
|
|
|
;;; Code:
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;; This file is autoloaded from calc-ext.el.
|
|
|
|
|
2004-11-30 17:29:04 +00:00
|
|
|
(require 'calc-ext)
|
2001-11-06 18:59:06 +00:00
|
|
|
(require 'calc-macs)
|
|
|
|
|
|
|
|
;;; Kill ring commands.
|
|
|
|
|
|
|
|
(defun calc-kill (nn &optional no-delete)
|
|
|
|
(interactive "P")
|
|
|
|
(if (eq major-mode 'calc-mode)
|
|
|
|
(calc-wrapper
|
|
|
|
(calc-force-refresh)
|
|
|
|
(calc-set-command-flag 'no-align)
|
|
|
|
(let ((num (max (calc-locate-cursor-element (point)) 1))
|
|
|
|
(n (prefix-numeric-value nn)))
|
|
|
|
(if (< n 0)
|
|
|
|
(progn
|
|
|
|
(if (eobp)
|
|
|
|
(setq num (1- num)))
|
|
|
|
(setq num (- num n)
|
|
|
|
n (- n))))
|
|
|
|
(let ((stuff (calc-top-list n (- num n -1))))
|
|
|
|
(calc-cursor-stack-index num)
|
|
|
|
(let ((first (point)))
|
|
|
|
(calc-cursor-stack-index (- num n))
|
|
|
|
(if (null nn)
|
|
|
|
(backward-char 1)) ; don't include newline for raw C-k
|
|
|
|
(copy-region-as-kill first (point))
|
|
|
|
(if (not no-delete)
|
|
|
|
(calc-pop-stack n (- num n -1))))
|
|
|
|
(setq calc-last-kill (cons (car kill-ring) stuff)))))
|
2001-11-14 09:09:09 +00:00
|
|
|
(kill-line nn)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-force-refresh ()
|
|
|
|
(if (or calc-executing-macro calc-display-dirty)
|
|
|
|
(let ((calc-executing-macro nil))
|
2001-11-14 09:09:09 +00:00
|
|
|
(calc-refresh))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-locate-cursor-element (pt)
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (point-max))
|
2001-11-14 09:09:09 +00:00
|
|
|
(calc-locate-cursor-scan (- calc-stack-top) calc-stack pt)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-locate-cursor-scan (n stack pt)
|
|
|
|
(if (or (<= (point) pt)
|
|
|
|
(null stack))
|
|
|
|
n
|
|
|
|
(forward-line (- (nth 1 (car stack))))
|
2001-11-14 09:09:09 +00:00
|
|
|
(calc-locate-cursor-scan (1+ n) (cdr stack) pt)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-kill-region (top bot &optional no-delete)
|
|
|
|
(interactive "r")
|
|
|
|
(if (eq major-mode 'calc-mode)
|
|
|
|
(calc-wrapper
|
|
|
|
(calc-force-refresh)
|
|
|
|
(calc-set-command-flag 'no-align)
|
|
|
|
(let* ((top-num (calc-locate-cursor-element top))
|
|
|
|
(bot-num (calc-locate-cursor-element (1- bot)))
|
|
|
|
(num (- top-num bot-num -1)))
|
|
|
|
(copy-region-as-kill top bot)
|
|
|
|
(setq calc-last-kill (cons (car kill-ring)
|
|
|
|
(calc-top-list num bot-num)))
|
|
|
|
(if (not no-delete)
|
|
|
|
(calc-pop-stack num bot-num))))
|
|
|
|
(if no-delete
|
|
|
|
(copy-region-as-kill top bot)
|
2001-11-14 09:09:09 +00:00
|
|
|
(kill-region top bot))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-copy-as-kill (n)
|
|
|
|
(interactive "P")
|
2001-11-14 09:09:09 +00:00
|
|
|
(calc-kill n t))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-copy-region-as-kill (top bot)
|
|
|
|
(interactive "r")
|
2001-11-14 09:09:09 +00:00
|
|
|
(calc-kill-region top bot t))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;;; This function uses calc-last-kill if possible to get an exact result,
|
|
|
|
;;; otherwise it just parses the yanked string.
|
|
|
|
;;; Modified to use Emacs 19 extended concept of kill-ring. -- daveg 12/15/96
|
|
|
|
(defun calc-yank ()
|
|
|
|
(interactive)
|
|
|
|
(calc-wrapper
|
|
|
|
(calc-pop-push-record-list
|
|
|
|
0 "yank"
|
|
|
|
(let ((thing (if (fboundp 'current-kill)
|
|
|
|
(current-kill 0 t)
|
|
|
|
(car kill-ring-yank-pointer))))
|
|
|
|
(if (eq (car-safe calc-last-kill) thing)
|
|
|
|
(cdr calc-last-kill)
|
|
|
|
(if (stringp thing)
|
|
|
|
(let ((val (math-read-exprs (calc-clean-newlines thing))))
|
|
|
|
(if (eq (car-safe val) 'error)
|
|
|
|
(progn
|
|
|
|
(setq val (math-read-exprs thing))
|
|
|
|
(if (eq (car-safe val) 'error)
|
|
|
|
(error "Bad format in yanked data")
|
|
|
|
val))
|
2001-11-14 09:09:09 +00:00
|
|
|
val))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-clean-newlines (s)
|
|
|
|
(cond
|
2003-02-04 13:24:35 +00:00
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
;; Omit leading/trailing whitespace
|
|
|
|
((or (string-match "\\`[ \n\r]+\\([^\001]*\\)\\'" s)
|
|
|
|
(string-match "\\`\\([^\001]*\\)[ \n\r]+\\'" s))
|
|
|
|
(calc-clean-newlines (math-match-substring s 1)))
|
|
|
|
|
|
|
|
;; Convert newlines to commas
|
|
|
|
((string-match "\\`\\(.*\\)[\n\r]+\\([^\001]*\\)\\'" s)
|
|
|
|
(calc-clean-newlines (concat (math-match-substring s 1) ","
|
|
|
|
(math-match-substring s 2))))
|
2003-02-04 13:24:35 +00:00
|
|
|
|
2001-11-14 09:09:09 +00:00
|
|
|
(t s)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun calc-do-grab-region (top bot arg)
|
2001-11-19 07:44:56 +00:00
|
|
|
(when (memq major-mode '(calc-mode calc-trail-mode))
|
|
|
|
(error "This command works only in a regular text buffer"))
|
2001-11-06 18:59:06 +00:00
|
|
|
(let* ((from-buffer (current-buffer))
|
|
|
|
(calc-was-started (get-buffer-window "*Calculator*"))
|
|
|
|
(single nil)
|
|
|
|
data vals pos)
|
|
|
|
(if arg
|
|
|
|
(if (consp arg)
|
|
|
|
(setq single t)
|
|
|
|
(setq arg (prefix-numeric-value arg))
|
|
|
|
(if (= arg 0)
|
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
|
|
|
(setq top (point))
|
|
|
|
(end-of-line)
|
|
|
|
(setq bot (point)))
|
|
|
|
(save-excursion
|
|
|
|
(setq top (point))
|
|
|
|
(forward-line arg)
|
|
|
|
(if (> arg 0)
|
|
|
|
(setq bot (point))
|
|
|
|
(setq bot top
|
|
|
|
top (point)))))))
|
|
|
|
(setq data (buffer-substring top bot))
|
|
|
|
(calc)
|
|
|
|
(if single
|
|
|
|
(setq vals (math-read-expr data))
|
|
|
|
(setq vals (math-read-expr (concat "[" data "]")))
|
|
|
|
(and (eq (car-safe vals) 'vec)
|
|
|
|
(= (length vals) 2)
|
|
|
|
(eq (car-safe (nth 1 vals)) 'vec)
|
|
|
|
(setq vals (nth 1 vals))))
|
|
|
|
(if (eq (car-safe vals) 'error)
|
|
|
|
(progn
|
|
|
|
(if calc-was-started
|
|
|
|
(pop-to-buffer from-buffer)
|
|
|
|
(calc-quit t)
|
|
|
|
(switch-to-buffer from-buffer))
|
|
|
|
(goto-char top)
|
|
|
|
(forward-char (+ (nth 1 vals) (if single 0 1)))
|
|
|
|
(error (nth 2 vals))))
|
|
|
|
(calc-slow-wrapper
|
2001-11-14 09:09:09 +00:00
|
|
|
(calc-enter-result 0 "grab" vals))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun calc-do-grab-rectangle (top bot arg &optional reduce)
|
|
|
|
(and (memq major-mode '(calc-mode calc-trail-mode))
|
2001-11-19 07:44:56 +00:00
|
|
|
(error "This command works only in a regular text buffer"))
|
2001-11-06 18:59:06 +00:00
|
|
|
(let* ((col1 (save-excursion (goto-char top) (current-column)))
|
|
|
|
(col2 (save-excursion (goto-char bot) (current-column)))
|
|
|
|
(from-buffer (current-buffer))
|
|
|
|
(calc-was-started (get-buffer-window "*Calculator*"))
|
|
|
|
data mat vals lnum pt pos)
|
|
|
|
(if (= col1 col2)
|
|
|
|
(save-excursion
|
2001-11-19 07:44:56 +00:00
|
|
|
(unless (= col1 0)
|
|
|
|
(error "Point and mark must be at beginning of line, or define a rectangle"))
|
2001-11-06 18:59:06 +00:00
|
|
|
(goto-char top)
|
|
|
|
(while (< (point) bot)
|
|
|
|
(setq pt (point))
|
|
|
|
(forward-line 1)
|
|
|
|
(setq data (cons (buffer-substring pt (1- (point))) data)))
|
|
|
|
(setq data (nreverse data)))
|
|
|
|
(setq data (extract-rectangle top bot)))
|
|
|
|
(calc)
|
|
|
|
(setq mat (list 'vec)
|
|
|
|
lnum 0)
|
2001-11-19 07:44:56 +00:00
|
|
|
(when arg
|
|
|
|
(setq arg (if (consp arg) 0 (prefix-numeric-value arg))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(while data
|
|
|
|
(if (natnump arg)
|
|
|
|
(progn
|
|
|
|
(if (= arg 0)
|
|
|
|
(setq arg 1000000))
|
|
|
|
(setq pos 0
|
|
|
|
vals (list 'vec))
|
|
|
|
(let ((w (length (car data)))
|
|
|
|
j v)
|
|
|
|
(while (< pos w)
|
|
|
|
(setq j (+ pos arg)
|
|
|
|
v (if (>= j w)
|
|
|
|
(math-read-expr (substring (car data) pos))
|
|
|
|
(math-read-expr (substring (car data) pos j))))
|
|
|
|
(if (eq (car-safe v) 'error)
|
|
|
|
(setq vals v w 0)
|
|
|
|
(setq vals (nconc vals (list v))
|
|
|
|
pos j)))))
|
|
|
|
(if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
|
|
|
|
(car data))
|
2005-05-02 19:45:21 +00:00
|
|
|
(setq vals (list 'vec (string-to-number (car data))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(if (and (null arg)
|
|
|
|
(string-match "[[{][^][{}]*[]}]" (car data)))
|
|
|
|
(setq pos (match-beginning 0)
|
|
|
|
vals (math-read-expr (math-match-substring (car data) 0)))
|
|
|
|
(let ((s (if (string-match
|
|
|
|
"\\`\\([0-9]+:[ \t]\\)?\\(.*[^, \t]\\)[, \t]*\\'"
|
|
|
|
(car data))
|
|
|
|
(math-match-substring (car data) 2)
|
|
|
|
(car data))))
|
|
|
|
(setq pos -1
|
|
|
|
vals (math-read-expr (concat "[" s "]")))
|
|
|
|
(if (eq (car-safe vals) 'error)
|
|
|
|
(let ((v2 (math-read-expr s)))
|
2001-11-19 07:44:56 +00:00
|
|
|
(unless (eq (car-safe v2) 'error)
|
|
|
|
(setq vals (list 'vec v2)))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(if (eq (car-safe vals) 'error)
|
|
|
|
(progn
|
|
|
|
(if calc-was-started
|
|
|
|
(pop-to-buffer from-buffer)
|
|
|
|
(calc-quit t)
|
|
|
|
(switch-to-buffer from-buffer))
|
|
|
|
(goto-char top)
|
|
|
|
(forward-line lnum)
|
|
|
|
(forward-char (+ (nth 1 vals) (min col1 col2) pos))
|
|
|
|
(error (nth 2 vals))))
|
2001-11-19 07:44:56 +00:00
|
|
|
(unless (equal vals '(vec))
|
|
|
|
(setq mat (cons vals mat)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(setq data (cdr data)
|
|
|
|
lnum (1+ lnum)))
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(if reduce
|
|
|
|
(calc-enter-result 0 "grb+" (list reduce '(var add var-add)
|
|
|
|
(nreverse mat)))
|
2001-11-14 09:09:09 +00:00
|
|
|
(calc-enter-result 0 "grab" (nreverse mat))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun calc-copy-to-buffer (nn)
|
|
|
|
"Copy the top of stack into an editing buffer."
|
|
|
|
(interactive "P")
|
|
|
|
(let ((thebuf (and (not (memq major-mode '(calc-mode calc-trail-mode)))
|
|
|
|
(current-buffer)))
|
|
|
|
(movept nil)
|
|
|
|
oldbuf newbuf)
|
|
|
|
(calc-wrapper
|
|
|
|
(save-excursion
|
|
|
|
(calc-force-refresh)
|
|
|
|
(let ((n (prefix-numeric-value nn))
|
|
|
|
(eat-lnums calc-line-numbering)
|
|
|
|
(big-offset (if (eq calc-language 'big) 1 0))
|
|
|
|
top bot)
|
|
|
|
(setq oldbuf (current-buffer)
|
|
|
|
newbuf (or thebuf
|
|
|
|
(calc-find-writable-buffer (buffer-list) 0)
|
|
|
|
(calc-find-writable-buffer (buffer-list) 1)
|
|
|
|
(error "No other buffer")))
|
|
|
|
(cond ((and (or (null nn)
|
|
|
|
(consp nn))
|
|
|
|
(= (calc-substack-height 0)
|
|
|
|
(- (1- (calc-substack-height 1)) big-offset)))
|
|
|
|
(calc-cursor-stack-index 1)
|
|
|
|
(if (looking-at
|
|
|
|
(if calc-line-numbering "[0-9]+: *[^ \n]" " *[^ \n]"))
|
|
|
|
(goto-char (1- (match-end 0))))
|
|
|
|
(setq eat-lnums nil
|
|
|
|
top (point))
|
|
|
|
(calc-cursor-stack-index 0)
|
|
|
|
(setq bot (- (1- (point)) big-offset)))
|
|
|
|
((> n 0)
|
|
|
|
(calc-cursor-stack-index n)
|
|
|
|
(setq top (point))
|
|
|
|
(calc-cursor-stack-index 0)
|
|
|
|
(setq bot (- (point) big-offset)))
|
|
|
|
((< n 0)
|
|
|
|
(calc-cursor-stack-index (- n))
|
|
|
|
(setq top (point))
|
|
|
|
(calc-cursor-stack-index (1- (- n)))
|
|
|
|
(setq bot (point)))
|
|
|
|
(t
|
|
|
|
(goto-char (point-min))
|
|
|
|
(forward-line 1)
|
|
|
|
(setq top (point))
|
|
|
|
(calc-cursor-stack-index 0)
|
|
|
|
(setq bot (point))))
|
|
|
|
(save-excursion
|
|
|
|
(set-buffer newbuf)
|
|
|
|
(if (consp nn)
|
|
|
|
(kill-region (region-beginning) (region-end)))
|
|
|
|
(push-mark (point) t)
|
|
|
|
(if (and overwrite-mode (not (consp nn)))
|
|
|
|
(calc-overwrite-string (save-excursion
|
|
|
|
(set-buffer oldbuf)
|
|
|
|
(buffer-substring top bot))
|
|
|
|
eat-lnums)
|
|
|
|
(or (bolp) (setq eat-lnums nil))
|
|
|
|
(insert-buffer-substring oldbuf top bot)
|
|
|
|
(and eat-lnums
|
|
|
|
(let ((n 1))
|
|
|
|
(while (and (> (point) (mark))
|
|
|
|
(progn
|
|
|
|
(forward-line -1)
|
|
|
|
(>= (point) (mark))))
|
|
|
|
(delete-char 4)
|
|
|
|
(setq n (1+ n)))
|
|
|
|
(forward-line n))))
|
2001-11-19 07:44:56 +00:00
|
|
|
(when thebuf
|
|
|
|
(setq movept (point)))
|
|
|
|
(when (get-buffer-window (current-buffer))
|
|
|
|
(set-window-point (get-buffer-window (current-buffer))
|
|
|
|
(point)))))))
|
|
|
|
(when movept
|
|
|
|
(goto-char movept))
|
|
|
|
(when (and (consp nn)
|
|
|
|
(not thebuf))
|
|
|
|
(calc-quit t)
|
|
|
|
(switch-to-buffer newbuf))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-overwrite-string (str eat-lnums)
|
2001-11-19 07:44:56 +00:00
|
|
|
(when (string-match "\n\\'" str)
|
|
|
|
(setq str (substring str 0 -1)))
|
|
|
|
(when eat-lnums
|
|
|
|
(setq str (substring str 4)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(if (and (string-match "\\`[-+]?[0-9.]+\\(e-?[0-9]+\\)?\\'" str)
|
|
|
|
(looking-at "[-+]?[0-9.]+\\(e-?[0-9]+\\)?"))
|
|
|
|
(progn
|
|
|
|
(delete-region (point) (match-end 0))
|
|
|
|
(insert str))
|
|
|
|
(let ((i 0))
|
|
|
|
(while (< i (length str))
|
|
|
|
(if (= (setq last-command-char (aref str i)) ?\n)
|
|
|
|
(or (= i (1- (length str)))
|
|
|
|
(let ((pt (point)))
|
|
|
|
(end-of-line)
|
|
|
|
(delete-region pt (point))
|
|
|
|
(if (eobp)
|
|
|
|
(insert "\n")
|
|
|
|
(forward-char 1))
|
|
|
|
(if eat-lnums (setq i (+ i 4)))))
|
|
|
|
(self-insert-command 1))
|
2001-11-14 09:09:09 +00:00
|
|
|
(setq i (1+ i))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;;; First, require that buffer is visible and does not begin with "*"
|
|
|
|
;;; Second, require only that it not begin with "*Calc"
|
|
|
|
(defun calc-find-writable-buffer (buf mode)
|
|
|
|
(and buf
|
|
|
|
(if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
|
|
|
|
(buffer-name (car buf)))
|
|
|
|
(and (= mode 0)
|
|
|
|
(or (string-match "\\`\\*.*" (buffer-name (car buf)))
|
|
|
|
(not (get-buffer-window (car buf))))))
|
|
|
|
(calc-find-writable-buffer (cdr buf) mode)
|
2001-11-14 09:09:09 +00:00
|
|
|
(car buf))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun calc-edit (n)
|
|
|
|
(interactive "p")
|
|
|
|
(calc-slow-wrapper
|
2001-11-19 07:44:56 +00:00
|
|
|
(when (eq n 0)
|
|
|
|
(setq n (calc-stack-size)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(let* ((flag nil)
|
|
|
|
(allow-ret (> n 1))
|
|
|
|
(list (math-showing-full-precision
|
|
|
|
(mapcar (if (> n 1)
|
|
|
|
(function (lambda (x)
|
|
|
|
(math-format-flat-expr x 0)))
|
|
|
|
(function
|
|
|
|
(lambda (x)
|
|
|
|
(if (math-vectorp x) (setq allow-ret t))
|
2001-11-13 07:30:58 +00:00
|
|
|
(math-format-nice-expr x (frame-width)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(if (> n 0)
|
|
|
|
(calc-top-list n)
|
|
|
|
(calc-top-list 1 (- n)))))))
|
|
|
|
(calc-edit-mode (list 'calc-finish-stack-edit (or flag n)) allow-ret)
|
|
|
|
(while list
|
|
|
|
(insert (car list) "\n")
|
|
|
|
(setq list (cdr list)))))
|
2001-11-14 09:09:09 +00:00
|
|
|
(calc-show-edit-buffer))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-alg-edit (str)
|
|
|
|
(calc-edit-mode '(calc-finish-stack-edit 0))
|
|
|
|
(calc-show-edit-buffer)
|
|
|
|
(insert str "\n")
|
|
|
|
(backward-char 1)
|
2001-11-14 09:09:09 +00:00
|
|
|
(calc-set-command-flag 'do-edit))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defvar calc-edit-mode-map nil "Keymap for use by the calc-edit command.")
|
|
|
|
(if calc-edit-mode-map
|
|
|
|
()
|
|
|
|
(setq calc-edit-mode-map (make-sparse-keymap))
|
|
|
|
(define-key calc-edit-mode-map "\n" 'calc-edit-finish)
|
|
|
|
(define-key calc-edit-mode-map "\r" 'calc-edit-return)
|
2001-11-14 09:09:09 +00:00
|
|
|
(define-key calc-edit-mode-map "\C-c\C-c" 'calc-edit-finish))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2004-11-26 22:34:23 +00:00
|
|
|
(defvar calc-original-buffer)
|
|
|
|
(defvar calc-return-buffer)
|
|
|
|
(defvar calc-one-window)
|
|
|
|
(defvar calc-edit-handler)
|
|
|
|
(defvar calc-restore-trail)
|
|
|
|
(defvar calc-allow-ret)
|
2005-01-02 17:04:37 +00:00
|
|
|
(defvar calc-edit-top)
|
2004-11-26 22:34:23 +00:00
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun calc-edit-mode (&optional handler allow-ret title)
|
|
|
|
"Calculator editing mode. Press RET, LFD, or C-c C-c to finish.
|
|
|
|
To cancel the edit, simply kill the *Calc Edit* buffer."
|
|
|
|
(interactive)
|
2001-11-19 07:44:56 +00:00
|
|
|
(unless handler
|
|
|
|
(error "This command can be used only indirectly through calc-edit"))
|
2001-11-06 18:59:06 +00:00
|
|
|
(let ((oldbuf (current-buffer))
|
|
|
|
(buf (get-buffer-create "*Calc Edit*")))
|
|
|
|
(set-buffer buf)
|
|
|
|
(kill-all-local-variables)
|
|
|
|
(use-local-map calc-edit-mode-map)
|
|
|
|
(setq buffer-read-only nil)
|
|
|
|
(setq truncate-lines nil)
|
|
|
|
(setq major-mode 'calc-edit-mode)
|
|
|
|
(setq mode-name "Calc Edit")
|
2005-05-26 13:27:35 +00:00
|
|
|
(run-mode-hooks 'calc-edit-mode-hook)
|
2001-11-06 18:59:06 +00:00
|
|
|
(make-local-variable 'calc-original-buffer)
|
|
|
|
(setq calc-original-buffer oldbuf)
|
|
|
|
(make-local-variable 'calc-return-buffer)
|
|
|
|
(setq calc-return-buffer oldbuf)
|
|
|
|
(make-local-variable 'calc-one-window)
|
|
|
|
(setq calc-one-window (and (one-window-p t) pop-up-windows))
|
|
|
|
(make-local-variable 'calc-edit-handler)
|
|
|
|
(setq calc-edit-handler handler)
|
|
|
|
(make-local-variable 'calc-restore-trail)
|
|
|
|
(setq calc-restore-trail (get-buffer-window (calc-trail-buffer)))
|
|
|
|
(make-local-variable 'calc-allow-ret)
|
|
|
|
(setq calc-allow-ret allow-ret)
|
2005-01-18 04:52:55 +00:00
|
|
|
(let ((inhibit-read-only t))
|
|
|
|
(erase-buffer))
|
2004-12-31 05:59:44 +00:00
|
|
|
(add-hook 'kill-buffer-hook (lambda ()
|
|
|
|
(let ((calc-edit-handler nil))
|
|
|
|
(calc-edit-finish t))
|
|
|
|
(message "(Cancelled)")) t t)
|
2005-01-02 07:39:20 +00:00
|
|
|
(insert (propertize
|
|
|
|
(concat
|
|
|
|
(or title title "Calc Edit Mode. ")
|
|
|
|
"Press `C-c C-c'"
|
|
|
|
(if allow-ret "" " or RET")
|
|
|
|
" to finish, `C-x k RET' to cancel.\n\n")
|
2005-01-02 17:04:37 +00:00
|
|
|
'font-lock-face 'italic 'read-only t 'rear-nonsticky t 'front-sticky t))
|
|
|
|
(make-local-variable 'calc-edit-top)
|
|
|
|
(setq calc-edit-top (point))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(put 'calc-edit-mode 'mode-class 'special)
|
|
|
|
|
|
|
|
(defun calc-show-edit-buffer ()
|
|
|
|
(let ((buf (current-buffer)))
|
|
|
|
(if (and (one-window-p t) pop-up-windows)
|
|
|
|
(pop-to-buffer (get-buffer-create "*Calc Edit*"))
|
|
|
|
(and calc-embedded-info (get-buffer-window (aref calc-embedded-info 1))
|
|
|
|
(select-window (get-buffer-window (aref calc-embedded-info 1))))
|
|
|
|
(switch-to-buffer (get-buffer-create "*Calc Edit*")))
|
|
|
|
(setq calc-return-buffer buf)
|
2001-11-13 07:30:58 +00:00
|
|
|
(if (and (< (window-width) (frame-width))
|
2001-11-06 18:59:06 +00:00
|
|
|
calc-display-trail)
|
|
|
|
(let ((win (get-buffer-window (calc-trail-buffer))))
|
|
|
|
(if win
|
|
|
|
(delete-window win))))
|
|
|
|
(set-buffer-modified-p nil)
|
2005-01-02 17:04:37 +00:00
|
|
|
(goto-char calc-edit-top)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-edit-return ()
|
|
|
|
(interactive)
|
|
|
|
(if (and (boundp 'calc-allow-ret) calc-allow-ret)
|
|
|
|
(newline)
|
2001-11-14 09:09:09 +00:00
|
|
|
(calc-edit-finish)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2004-11-19 22:17:25 +00:00
|
|
|
;; The variable calc-edit-disp-trail is local to calc-edit finish, but
|
|
|
|
;; is used by calc-finish-selection-edit and calc-finish-stack-edit.
|
|
|
|
(defvar calc-edit-disp-trail)
|
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun calc-edit-finish (&optional keep)
|
|
|
|
"Finish calc-edit mode. Parse buffer contents and push them on the stack."
|
|
|
|
(interactive "P")
|
|
|
|
(message "Working...")
|
|
|
|
(or (and (boundp 'calc-original-buffer)
|
|
|
|
(boundp 'calc-return-buffer)
|
|
|
|
(boundp 'calc-one-window)
|
|
|
|
(boundp 'calc-edit-handler)
|
|
|
|
(boundp 'calc-restore-trail)
|
|
|
|
(eq major-mode 'calc-edit-mode))
|
2001-11-19 07:44:56 +00:00
|
|
|
(error "This command is valid only in buffers created by calc-edit"))
|
2001-11-06 18:59:06 +00:00
|
|
|
(let ((buf (current-buffer))
|
|
|
|
(original calc-original-buffer)
|
|
|
|
(return calc-return-buffer)
|
|
|
|
(one-window calc-one-window)
|
2004-11-19 22:17:25 +00:00
|
|
|
(calc-edit-disp-trail calc-restore-trail))
|
2001-11-06 18:59:06 +00:00
|
|
|
(save-excursion
|
2001-11-19 07:44:56 +00:00
|
|
|
(when (or (null (buffer-name original))
|
|
|
|
(progn
|
|
|
|
(set-buffer original)
|
|
|
|
(not (eq major-mode 'calc-mode))))
|
|
|
|
(error "Original calculator buffer has been corrupted")))
|
2005-01-02 17:04:37 +00:00
|
|
|
(goto-char calc-edit-top)
|
2001-11-06 18:59:06 +00:00
|
|
|
(if (buffer-modified-p)
|
|
|
|
(eval calc-edit-handler))
|
2005-04-27 04:35:16 +00:00
|
|
|
(if (and one-window (not (one-window-p t)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(delete-window))
|
|
|
|
(if (get-buffer-window return)
|
|
|
|
(select-window (get-buffer-window return))
|
|
|
|
(switch-to-buffer return))
|
|
|
|
(if keep
|
|
|
|
(bury-buffer buf)
|
|
|
|
(kill-buffer buf))
|
2004-11-19 22:17:25 +00:00
|
|
|
(if calc-edit-disp-trail
|
2001-11-06 18:59:06 +00:00
|
|
|
(calc-wrapper
|
|
|
|
(calc-trail-display 1 t)))
|
2001-11-14 09:09:09 +00:00
|
|
|
(message "")))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-edit-cancel ()
|
|
|
|
"Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
|
|
|
|
(interactive)
|
|
|
|
(let ((calc-edit-handler nil))
|
|
|
|
(calc-edit-finish))
|
2001-11-14 09:09:09 +00:00
|
|
|
(message "(Cancelled)"))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-finish-stack-edit (num)
|
|
|
|
(let ((buf (current-buffer))
|
2005-01-02 17:04:37 +00:00
|
|
|
(str (buffer-substring calc-edit-top (point-max)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(start (point))
|
|
|
|
pos)
|
|
|
|
(if (and (integerp num) (> num 1))
|
|
|
|
(while (setq pos (string-match "\n." str))
|
|
|
|
(aset str pos ?\,)))
|
|
|
|
(switch-to-buffer calc-original-buffer)
|
|
|
|
(let ((vals (let ((calc-language nil)
|
|
|
|
(math-expr-opers math-standard-opers))
|
|
|
|
(and (string-match "[^\n\t ]" str)
|
|
|
|
(math-read-exprs str)))))
|
2001-11-19 07:44:56 +00:00
|
|
|
(when (eq (car-safe vals) 'error)
|
|
|
|
(switch-to-buffer buf)
|
|
|
|
(goto-char (+ start (nth 1 vals)))
|
|
|
|
(error (nth 2 vals)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(calc-wrapper
|
|
|
|
(if (symbolp num)
|
|
|
|
(progn
|
|
|
|
(set num (car vals))
|
|
|
|
(calc-refresh-evaltos num))
|
2004-11-19 22:17:25 +00:00
|
|
|
(if calc-edit-disp-trail
|
2001-11-06 18:59:06 +00:00
|
|
|
(calc-trail-display 1 t))
|
|
|
|
(and vals
|
|
|
|
(let ((calc-simplify-mode (if (eq last-command-char ?\C-j)
|
|
|
|
'none
|
|
|
|
calc-simplify-mode)))
|
|
|
|
(if (>= num 0)
|
|
|
|
(calc-enter-result num "edit" vals)
|
2001-11-14 09:09:09 +00:00
|
|
|
(calc-enter-result 1 "edit" vals (- num))))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2004-11-30 17:29:04 +00:00
|
|
|
(provide 'calc-yank)
|
|
|
|
|
2003-09-01 15:45:59 +00:00
|
|
|
;;; arch-tag: ca61019e-caca-4daa-b32c-b6afe372d5b5
|
2001-11-14 09:09:09 +00:00
|
|
|
;;; calc-yank.el ends here
|