File: read.el

package info (click to toggle)
elib 1.0-8
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 336 kB
  • ctags: 263
  • sloc: lisp: 2,378; makefile: 102; sh: 51
file content (123 lines) | stat: -rw-r--r-- 3,646 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
;;;; $Id: read.el,v 0.8 1995/12/11 00:11:29 ceder Exp $
;;;; This file contains a number of functions for reading from the
;;;; minibuffer.

;; Copyright (C) 1991-1995 Free Software Foundation

;; Author: Inge Wallin <inge@lysator.liu.se>
;;	Lars Willfr <willfor@lysator.liu.se>
;; Maintainer: elib-maintainers@lysator.liu.se
;; Created: before 11 May 1991
;; Keywords: extensions, lisp

;;;;
;;;; This file is part of the GNU Emacs lisp library, Elib.
;;;;
;;;; GNU Elib 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.
;;;;
;;;; GNU Elib is distributed in the hope that it will be useful,
;;;; 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 Elib; see the file COPYING.  If not, write to
;;;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;;; Boston, MA 02111-1307, USA
;;;;
;;;; Authors: Inge Wallin, Lars Willf|r
;;;;


(provide 'read)

;;; Code:

(defun read-number (&optional prompt default)
  "Read a number from the minibuffer. 
Optional arguments: PROMPT DEFAULT.

If DEFAULT is non-nil, it is written within parenthesis after the prompt.
DEFAULT can be either a number, or of the type which `(interactive P)'
generates." 
  (let ((numdefault (cond ((null default) nil)
			  ((integerp default) default)
			  ((listp default) (car default))
			  (t nil)))
	(number nil)
	(numstr nil))

    (while (not number)
      (setq numstr (read-string
		    (concat (if prompt 
				prompt
			      "Enter a number: ")
			    (if numdefault 
				(format "(%d) " numdefault)))))
      (cond ((and (string= numstr "") 
		  numdefault)
	     (setq number numdefault))
	    ((string-match "\\`[0-9]+\\'" numstr)
	     (setq number (string-to-int numstr)))
	    (t (beep))))
    number))


(defun read-num-range (low high &optional prompt show-range)
  "Read a number within an interval from the minibuffer.
Args: LOW HIGH &optional PROMPT SHOW-RANGE.

The number read must be within the range [LOW HIGH].
If SHOW-RANGE is non-nil, the prompt will include the range for information
to the user."
  (let ((number (1- low)))
    (while (or (< number low)
	       (> number high))
      (setq number (read-number 
		    (concat (if prompt
				prompt
			      "Enter a number: ")
			    (if show-range
				(format "(%d-%d) " low high)
			      "")))))
    number))


(defun read-silent (prompt &optional outchar)
  "Read a string in the minibuffer without echoing.
Args: PROMPT &optional OUTCHAR.

For each character the user writes, one OUTCHAR is displayed."
  (message prompt)
  (let ((input-string "")
	(input-char)
	(cursor-in-echo-area t)
	(showstring ""))
    (while (not (or (eq (setq input-char (read-char)) ?\r)
		    (eq input-char ?\n)))
      (cond
       ((eq input-char ?\C-?)
	(if (equal (length input-string)
		   0)
	    nil
	  (setq showstring (substring showstring 0 -1))
	  (setq input-string (substring input-string 0 -1))))
       ((eq input-char ?\C-u)
	(setq showstring "")
	(setq input-string ""))
       (t
	(if outchar
	    (setq showstring 
		  (concat showstring (char-to-string outchar))))
	(setq input-string
	      (concat input-string (char-to-string input-char)))))
      (message (concat prompt (if outchar
				  showstring
				nil))))
    (message "")
    input-string))

;; read.el ends here