aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/misc/ses.texi29
-rw-r--r--lisp/ses.el172
2 files changed, 194 insertions, 7 deletions
diff --git a/doc/misc/ses.texi b/doc/misc/ses.texi
index cac874d0f02..fc79b027a1d 100644
--- a/doc/misc/ses.texi
+++ b/doc/misc/ses.texi
@@ -292,7 +292,13 @@ Self-insert an expression. The right-parenthesis is inserted for you
292(@code{ses-read-cell}). To access another cell's value, just use its 292(@code{ses-read-cell}). To access another cell's value, just use its
293identifier in your expression. Whenever the other cell is changed, 293identifier in your expression. Whenever the other cell is changed,
294this cell's formula will be reevaluated. While typing in the 294this cell's formula will be reevaluated. While typing in the
295expression, you can use @kbd{M-@key{TAB}} to complete symbol names. 295expression, you can use the following keys:
296@table @kbd
297@item M-@key{TAB}
298to complete symbol names, and
299@item C-h C-n
300to list the named cells symbols in a help buffer.
301@end table
296 302
297@item ' @r{(apostrophe)} 303@item ' @r{(apostrophe)}
298Enter a symbol (ses-read-symbol). @acronym{SES} remembers all symbols that have 304Enter a symbol (ses-read-symbol). @acronym{SES} remembers all symbols that have
@@ -458,11 +464,22 @@ Enter the default printer for the spreadsheet
458(@code{ses-read-default-printer}). 464(@code{ses-read-default-printer}).
459@end table 465@end table
460 466
461The @code{ses-read-@var{xxx}-printer} commands have their own 467The @code{ses-read-@var{xxx}-printer} allows the following commands during editing:
462minibuffer history, which is preloaded with the set of all printers 468
463used in this spreadsheet, plus the standard printers (@pxref{Standard 469@table @kbd
464printer functions}) and the local printers (@pxref{Local printer 470@item @key{arrow-up}
465functions}). 471@itemx @key{arrow-down}
472To browse history: the @code{ses-read-@var{xxx}-printer} commands have
473their own minibuffer history, which is preloaded with the set of all
474printers used in this spreadsheet, plus the standard printers
475(@pxref{Standard printer functions}) and the local printers
476(@pxref{Local printer functions}).
477@item @key{TAB}
478To complete the local printer symbols, and
479@item C-h C-p
480To list the local printers in a help buffer.
481@end table
482
466 483
467@node Standard printer functions 484@node Standard printer functions
468@subsection Standard printer functions 485@subsection Standard printer functions
diff --git a/lisp/ses.el b/lisp/ses.el
index 5c560efb703..2e214348eb9 100644
--- a/lisp/ses.el
+++ b/lisp/ses.el
@@ -167,12 +167,32 @@ Each function is called with ARG=1."
167 ["Export values" ses-export-tsv t] 167 ["Export values" ses-export-tsv t]
168 ["Export formulas" ses-export-tsf t])) 168 ["Export formulas" ses-export-tsf t]))
169 169
170(defconst ses-completion-keys '("\M-\C-i" "\C-i")
171 "List for keys that can be used for completion while editing.")
172
173(defvar ses--completion-table nil
174 "Set globally to what completion table to use depending on type
175 of completion (local printers, cells, etc.). We need to go
176 through a local variable to pass the SES buffer local variable
177 to completing function while the current buffer is the
178 minibuffer.")
179
180(defvar ses--list-orig-buffer nil
181 "Calling buffer for SES listing help. Used for listing local
182 printers or renamed cells.")
183
184
170(defconst ses-mode-edit-map 185(defconst ses-mode-edit-map
171 (let ((keys '("\C-c\C-r" ses-insert-range 186 (let ((keys '("\C-c\C-r" ses-insert-range
172 "\C-c\C-s" ses-insert-ses-range 187 "\C-c\C-s" ses-insert-ses-range
173 [S-mouse-3] ses-insert-range-click 188 [S-mouse-3] ses-insert-range-click
174 [C-S-mouse-3] ses-insert-ses-range-click 189 [C-S-mouse-3] ses-insert-ses-range-click
175 "\M-\C-i" lisp-complete-symbol)) ; FIXME obsolete 190 "\C-h\C-p" ses-list-local-printers
191 "\C-h\C-n" ses-list-named-cells
192 "\M-\C-i" lisp-complete-symbol)) ; redefined
193 ; dynamically in
194 ; editing
195 ; functions
176 (newmap (make-sparse-keymap))) 196 (newmap (make-sparse-keymap)))
177 (set-keymap-parent newmap minibuffer-local-map) 197 (set-keymap-parent newmap minibuffer-local-map)
178 (while keys 198 (while keys
@@ -2447,6 +2467,42 @@ to are recalculated first."
2447;;---------------------------------------------------------------------------- 2467;;----------------------------------------------------------------------------
2448;; Input of cell formulas 2468;; Input of cell formulas
2449;;---------------------------------------------------------------------------- 2469;;----------------------------------------------------------------------------
2470(defun ses-edit-cell-complete-symbol ()
2471 (interactive)
2472 (let ((completion-at-point-functions (cons 'ses--edit-cell-completion-at-point-function
2473 completion-at-point-functions)))
2474 (completion-at-point)))
2475
2476(defun ses--edit-cell-completion-at-point-function ()
2477 (and
2478 ses--completion-table
2479 (let* ((bol (save-excursion (move-beginning-of-line nil) (point)))
2480 start end collection
2481 (prefix
2482 (save-excursion
2483 (setq end (point))
2484 (backward-sexp)
2485 (if (< (point) bol)
2486 (progn
2487 (setq start bol)
2488 (buffer-substring start end))
2489 (setq start (point))
2490 (forward-sexp)
2491 (if (>= (point) end)
2492 (progn
2493 (setq end (point))
2494 (buffer-substring start end))
2495 nil))))
2496 prefix-length)
2497 (when (and prefix (null (string= prefix "")))
2498 (setq prefix-length (length prefix))
2499 (maphash (lambda (key val)
2500 (let ((key-name (symbol-name key)))
2501 (when (and (>= (length key-name) prefix-length)
2502 (string= prefix (substring key-name 0 prefix-length)))
2503 (push key-name collection))))
2504 ses--completion-table)
2505 (and collection (list start end collection))))))
2450 2506
2451(defun ses-edit-cell (row col newval) 2507(defun ses-edit-cell (row col newval)
2452 "Display current cell contents in minibuffer, for editing. Returns nil if 2508 "Display current cell contents in minibuffer, for editing. Returns nil if
@@ -2468,6 +2524,10 @@ cell formula was unsafe and user declined confirmation."
2468 (if (stringp formula) 2524 (if (stringp formula)
2469 ;; Position cursor inside close-quote. 2525 ;; Position cursor inside close-quote.
2470 (setq initial (cons initial (length initial)))) 2526 (setq initial (cons initial (length initial))))
2527 (dolist (key ses-completion-keys)
2528 (define-key ses-mode-edit-map key 'ses-edit-cell-complete-symbol))
2529 ;; make it globally visible, so that it can be visbile from the minibuffer.
2530 (setq ses--completion-table ses--named-cell-hashmap)
2471 (list row col 2531 (list row col
2472 (read-from-minibuffer (format "Cell %s: " ses--curcell) 2532 (read-from-minibuffer (format "Cell %s: " ses--curcell)
2473 initial 2533 initial
@@ -2562,6 +2622,40 @@ cells."
2562;;---------------------------------------------------------------------------- 2622;;----------------------------------------------------------------------------
2563;; Input of cell-printer functions 2623;; Input of cell-printer functions
2564;;---------------------------------------------------------------------------- 2624;;----------------------------------------------------------------------------
2625(defun ses-read-printer-complete-symbol ()
2626 (interactive)
2627 (let ((completion-at-point-functions (cons 'ses--read-printer-completion-at-point-function
2628 completion-at-point-functions)))
2629 (completion-at-point)))
2630
2631(defun ses--read-printer-completion-at-point-function ()
2632 (let* ((bol (save-excursion (move-beginning-of-line nil) (point)))
2633 start end collection
2634 (prefix
2635 (save-excursion
2636 (setq end (point))
2637 (backward-sexp)
2638 (if (< (point) bol)
2639 (progn
2640 (setq start bol)
2641 (buffer-substring start end))
2642 (setq start (point))
2643 (forward-sexp)
2644 (if (>= (point) end)
2645 (progn
2646 (setq end (point))
2647 (buffer-substring start end))
2648 nil))))
2649 prefix-length)
2650 (when prefix
2651 (setq prefix-length (length prefix))
2652 (maphash (lambda (key val)
2653 (let ((key-name (symbol-name key)))
2654 (when (and (>= (length key-name) prefix-length)
2655 (string= prefix (substring key-name 0 prefix-length)))
2656 (push key-name collection))))
2657 ses--completion-table)
2658 (and collection (list start end collection)))))
2565 2659
2566(defun ses-read-printer (prompt default) 2660(defun ses-read-printer (prompt default)
2567 "Common code for functions `ses-read-cell-printer', `ses-read-column-printer', 2661 "Common code for functions `ses-read-cell-printer', `ses-read-column-printer',
@@ -2574,6 +2668,10 @@ canceled."
2574 (setq prompt (format "%s (default %S): " 2668 (setq prompt (format "%s (default %S): "
2575 (substring prompt 0 -2) 2669 (substring prompt 0 -2)
2576 default))) 2670 default)))
2671 (dolist (key ses-completion-keys)
2672 (define-key ses-mode-edit-map key 'ses-read-printer-complete-symbol))
2673 ;; make it globally visible, so that it can be visbile from the minibuffer.
2674 (setq ses--completion-table ses--local-printer-hashmap)
2577 (let ((new (read-from-minibuffer prompt 2675 (let ((new (read-from-minibuffer prompt
2578 nil ; Initial contents. 2676 nil ; Initial contents.
2579 ses-mode-edit-map 2677 ses-mode-edit-map
@@ -3282,6 +3380,78 @@ is non-nil. Newlines and tabs in the export text are escaped."
3282 (setq result (apply #'concat (nreverse result))) 3380 (setq result (apply #'concat (nreverse result)))
3283 (kill-new result))) 3381 (kill-new result)))
3284 3382
3383;;----------------------------------------------------------------------------
3384;; Interactive help on symbols
3385;;----------------------------------------------------------------------------
3386
3387(defun ses-list-local-printers (&optional local-printer-hashmap)
3388 "List local printers in a help buffer. Can be called either
3389during editing a printer or a formula, or while in the SES
3390buffer."
3391 (interactive
3392 (list (cond
3393 ((derived-mode-p 'ses-mode) ses--local-printer-hashmap)
3394 ((minibufferp) ses--completion-table)
3395 ((derived-mode-p 'help-mode) nil)
3396 (t (error "Not in a SES buffer")))))
3397 (when local-printer-hashmap
3398 (let ((ses--list-orig-buffer (or ses--list-orig-buffer (current-buffer))))
3399 (help-setup-xref
3400 (list (lambda (local-printer-hashmap buffer)
3401 (let ((ses--list-orig-buffer
3402 (if (buffer-live-p buffer) buffer)))
3403 (ses-list-local-printers local-printer-hashmap)))
3404 local-printer-hashmap ses--list-orig-buffer)
3405 (called-interactively-p 'interactive))
3406
3407 (save-excursion
3408 (with-help-window (help-buffer)
3409 (if (= 0 (hash-table-count local-printer-hashmap))
3410 (princ "No local printers defined.")
3411 (princ "List of local printers definitions:\n")
3412 (maphash (lambda (key val)
3413 (princ key)
3414 (princ " as ")
3415 (prin1 (ses--locprn-def val))
3416 (princ "\n"))
3417 local-printer-hashmap))
3418 (with-current-buffer standard-output
3419 (buffer-string)))))))
3420
3421(defun ses-list-named-cells (&optional named-cell-hashmap)
3422 "List named cells in a help buffer. Can be called either
3423during editing a printer or a formula, or while in the SES
3424buffer."
3425 (interactive
3426 (list (cond
3427 ((derived-mode-p 'ses-mode) ses--named-cell-hashmap)
3428 ((minibufferp) ses--completion-table)
3429 ((derived-mode-p 'help-mode) nil)
3430 (t (error "Not in a SES buffer")))))
3431 (when named-cell-hashmap
3432 (let ((ses--list-orig-buffer (or ses--list-orig-buffer (current-buffer))))
3433 (help-setup-xref
3434 (list (lambda (named-cell-hashmap buffer)
3435 (let ((ses--list-orig-buffer
3436 (if (buffer-live-p buffer) buffer)))
3437 (ses-list-named-cells named-cell-hashmap)))
3438 named-cell-hashmap ses--list-orig-buffer)
3439 (called-interactively-p 'interactive))
3440
3441 (save-excursion
3442 (with-help-window (help-buffer)
3443 (if (= 0 (hash-table-count named-cell-hashmap))
3444 (princ "No cell was renamed.")
3445 (princ "List of named cells definitions:\n")
3446 (maphash (lambda (key val)
3447 (princ key)
3448 (princ " for ")
3449 (prin1 (ses-create-cell-symbol (car val) (cdr val)))
3450 (princ "\n"))
3451 named-cell-hashmap))
3452 (with-current-buffer standard-output
3453 (buffer-string)))))))
3454
3285 3455
3286;;---------------------------------------------------------------------------- 3456;;----------------------------------------------------------------------------
3287;; Other user commands 3457;; Other user commands