🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.emacs.bug
Article #98808

Re: Emacs Suggestion

#98808
From: Mike Williams
Date: Sun, 03 Mar 1991 20:25
123 lines
5289 bytes
+-- In article <9103011620.AA20347@rx7.ece.cmu.edu> hagerman@RX7.ECE.CMU.EDU (John Hagerman) writes:
| 
| This suggestion has to do with (interactive).  I think (interactive)
| should handle more of the cases I currently write myself.
| 
| Specifically, I am interested in functions that operate on an X, where
| an X is prompted for, with a default of "the X at point."  X's I have
| encountered include words, symbols, and variables; others are obvious.

    It's not too hard to write some useful functions to use in interactive
  specifications, if you find yourself doing the same thing a lot.
  Convincing people to make them standard, or to add explicit interactive
  spec. code letters for them, may be harder.

    In response to your posting, I hacked the following code together:

;;=== Useful stuff for interactive specs. =================================

(defun thing-at-point (THING)
  "Return the THING at point, where THING is an entity for which there is
a corresponding forward-THING operation, eg. 'word, 'sentence, etc."
  (let ((forward-op (intern (format "forward-%s" THING))))
    (condition-case ()
	(save-excursion
	  (let ((start (progn (funcall forward-op -1) (point)))
		(end (progn (funcall forward-op 1) (point))))
	    (buffer-substring start end)))
      (error nil))))

(defun word-at-point () (thing-at-point 'word))
(defun sexp-at-point () (thing-at-point 'sexp))
(defun sentence-at-point () (thing-at-point 'sentence))

(defun symbol-at-point ()
  (condition-case ()
      (save-excursion
	(forward-sexp -1)
	(skip-chars-forward "'")
	(let ((obj (read (current-buffer))))
	  (if (symbolp obj) obj)))
    (error nil)))

(defun prompt-for-string (prompt &optional default)
  "Read a string from the minibuffer, prompting with PROMPT.
Optional second argument DEFAULT specifies the default value."
  (let ((result
	 (read-string
	  (format "%s%s: " prompt 
		  (if default (format " (default %s)" default) "")))))
    (if (equal result "") default result)))

(defun prompt-for-symbol (prompt &optional default predicate)
  "Read a symbol from the minibuffer, prompting with PROMPT.
Optional second argument DEFAULT specifies the default symbol.
Optional third argument PREDICATE specifies a completion predicate to be
passed to completing-read."
  (let ((default
	  (if (and default (symbolp default)
		   (or (not predicate) (funcall predicate default)))
	      default))
	result)
    (setq result
	  (completing-read 
	   (format "%s%s: " prompt 
		   (if default (format " (default %s)" default) ""))
	   obarray predicate t))
    (if (equal result "") default result)))

;;=== end =================================================================

    These functions can be used to approximate the interactive code letter
  behaviour that you mentioned:

| Maybe new (interactive) code letters would suffice:
| 
| W -- Any string of word-syntax characters, default is the word at point.

    (interactive (list (prompt-for-string "Word" (word-at-point))))

    Note that this actually doesn't enforce your requirement that the
  string entered consist of word-syntax characters only.  That's a little
  harder. 

| I -- Any symbol ("identifier"), default is the symbol at point.

    (interactive (list (prompt-for-symbol "Symbol" (symbol-at-point) 'symbolp)))

| V -- Any variable, default is the variable at point.

    (interactive (list (prompt-for-symbol "Variable" 
                           (symbol-at-point) 'boundp)))

| (This choice of letters is meant to imply the addition of the letters
| `w' for word-syntax and `i' to replace `S'.)
| 
| Or maybe a special character (`.'?) following a code letter would be
| better, since capitalization is already overloaded.
| 
| Implementing this will require functions that will do minibuffer reads
| with defaults for all X's of interest.
| 
| Comments?

    I don't think that adding interactive code letters is feasible, given
  the number of "X's" which might possibly be "of interest".  The form 
  
		(interactive
		 (list
		  (input-function ...)
		  (input-function ...)
		  (input-function ...)
		  ...etc...
		  ))

  is more general, and allows the use of auxiliary functions like those
  given above to provide an easier and more readable programming interface.

--
      /-------------------- byrd@comp.vuw.ac.nz --------------------\ 
      | Mike Williams, Victoria University of Wellington, Aotearoa. |
      \--- It's not an optical illusion, it just looks like one. ---/

	(P.S. This account will probably disappear in early March.)
Technical Details
Message-ID: <BYRD.91Mar3152541@embassy.comp.vuw.ac.nz>
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!tut.cis.ohio-state.edu!unreplyable!garbage

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fb2f1,be27ae11527e70dc
X-Google-Attributes: gidfb2f1,public
X-Google-ArrivalTime: 1991-03-03 12:25:41 PST
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!tut.cis.ohio-state.edu!unreplyable!garbage
From: Mike.Williams@COMP.VUW.AC.NZ (Mike Williams)
Newsgroups: gnu.emacs.bug
Subject: Re: Emacs Suggestion
Message-ID: <BYRD.91Mar3152541@embassy.comp.vuw.ac.nz>
Date: 3 Mar 91 20:25:41 GMT
Sender: daemon@tut.cis.ohio-state.edu
Distribution: gnu
Organization: Dept. of Computer Science, Victoria Uni. of Wellington, NZ.
Lines: 123
Approved: bug-gnu-emacs@prep.ai.mit.edu