home *** CD-ROM | disk | FTP | other *** search
- x-gateway: rodan.UU.NET from help-lucid-emacs to alt.lucid-emacs.help; Mon, 14 Dec 1992 10:38:39 EST
- Return-Path: <sun001!mbattagl>
- Date: Mon, 14 Dec 1992 08:33:36 CST
- From: sun001!mbattagl@uunet.uu.net (M. Battagl)
- Message-ID: <9212141433.AA04038@enterprise-gw>
- Subject: mini-buffer recall
- Newsgroups: alt.lucid-emacs.help
- Path: sparky!uunet!wendy-fate.uu.net!help-lucid-emacs
- Sender: help-lucid-emacs-request@lucid.com
- Lines: 535
-
- A long time ago I wrote a mini-buffer recall. Since then I have been
- informed of other code that does this. However, I still found the
- functionality of the code I wrote more useful to me. So if anyone
- would like it here it is.
-
- Also to the few people posting about formatting text, I have written a
- formatter I think is more useful. However, since the introduction of
- lemacs 19.x it has developed some bugs. I will post that as soon as I
- think it has been updated for correctly.
-
- ;; mini-buffer.el
- ;; This file is part of GNU Emacs.
-
- ;; 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 1, or (at your option)
- ;; any later version.
-
- ;; GNU Emacs 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 Emacs; see the file COPYING. If not, write to
- ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
-
- ;;;======================== mini-buffer.el ===========================
- ;;; This file contains the routines used to provide defaults for calls
- ;;; using the minibuffer. Up and down arrow will recall the defaults
- ;;; If the variable 'mini-default-buffer' is nil
- ;;; then a default default buffer will be used. If you want seperate
- ;;; default sets, set 'mini-default-buffer' to the buffer name to hold
- ;;; the defaults for a function prior to calling the original function.
- ;;; In this way you can have search commands have one default and edit
- ;;; files have another.
- ;;; No matter how the minibuffer is exited (or aborted) the variable
- ;;; 'mini-default-buffer' will be set back to nil !!!!
-
- ;;; this does not (or should not) effect the execute extended command
- ;;; (M-x) function. However if you want defaults for that please use
- ;;; the function 'func-execute-ext-cmd'
-
- ;;; NOTE: DEFAULT BUFFER NAMES SHOULD START WITH A SPACE SO THEY WILL
- ;;; NOT SHOW UP IN THE BUFFER LIST!!!!!!!!
- ;;;===================================================================
-
- (defvar mini-default-buffer nil
- "The name of a buffer to retrieve defaults from.
- nil = use general defaults"
- )
- (defun func-get-mini-default(count)
- "this function will change the default in the minibuffer
- when called with a buffer to retreive them from. If the variable
- 'mini-default-buffer' is nil it will output an error message. If
- not it will retrieve defaults from that buffer. The paramater passed
- should be a '1' for down-arrow and a -1 for up-arrow"
- (if (eq mini-default-buffer nil)
- (setq mini-default-buffer " misc-defaults")
- )
- (erase-buffer)
- (let* ((a nil) (mark nil) (temp_buffer (buffer-name)))
- (if (not (get-buffer mini-default-buffer))
- (progn
- (get-buffer-create mini-default-buffer)
- (set-buffer mini-default-buffer)
- (newline 1)
- )
- (set-buffer mini-default-buffer)
- )
- (forward-line count)
- (setq mark (point))
- (end-of-line 1)
- (setq a (buffer-substring (point) mark))
- (set-buffer temp_buffer)
- (insert-string a)
- (end-of-line 1)
- )
- )
- (defun func-mini-get-up()
- "up arrow in minibuffer for defaults"
- (interactive)
- (func-get-mini-default -1)
- )
- (defun func-mini-get-down()
- "down arrow in minibuffer for defaults"
- (interactive)
- (func-get-mini-default 1)
- )
- ;;;===================================================================
- (defun func-mini-match-end() ;
- "return key in minibuffer for defaults" ;
- (interactive) ;
- (if (eq mini-default-buffer nil) ;set default if not defined
- (setq mini-default-buffer " misc-defaults") ;
- ) ;
- (let* ((mark nil) (temp_buffer (buffer-name)) ;
- (a (buffer-substring (point-min) (point-max))) ;
- (temp (try-completion ;
- a minibuffer-completion-table ;
- minibuffer-completion-predicate) ;
- ) ;
- ) ;
- (if (and temp ;if possible match exists(temp not nil)
- (or (eq temp t) ;if not a unique completion
- (equal temp "") ;if defaulted, go with it
- (if (sequencep minibuffer-completion-table) ;
- (if (vectorp minibuffer-completion-table) ;
- (intern-soft (setq a temp) minibuffer-completion-table) ;
- (assoc (setq a temp) minibuffer-completion-table) ;
- ) ;end-> "if (vectorp minibuf..."
- t ;
- ) ;end-> "if (sequencep minib..."
- ) ;end-> "or (eq temp t)"
- ) ;end-> "and temp"
- (progn ;if temp is a completion
- (if (string-match "[^ ]" a) ;
- (progn ;if not all whitespace
- (if (not (get-buffer mini-default-buffer)) ;
- (progn ;
- (get-buffer-create mini-default-buffer) ;
- (set-buffer mini-default-buffer) ;
- (newline 1) ;
- ) ;
- (set-buffer mini-default-buffer) ;
- ) ;
- (goto-char (point-min)) ;start of buffer
- (if (re-search-forward
- (concat "^" (regexp-quote a) "$") ;
- (point-max) 2)
- (progn ;if found exact match
- (let ((end (point))) ;save line end
- (beginning-of-line) ;go to start of line
- (delete-region end (point)) ;delete line
- (delete-char 1) ;delete newline
- ) ;end let
- ) ;end-> "progn"
- ) ;end-> "if (not (re-search-forward "
- (goto-char (point-max)) ;
- (insert-string a) ;save this one
- (newline) ;mark end of text
- (goto-char (point-max)) ;
- ) ;end-> "progn"
- ) ;end-> "if (string-match "[^ ]" (se..."
- (set-buffer temp_buffer) ;
- (setq mini-default-buffer nil) ;
- (minibuffer-complete-and-exit) ;
- ) ;end-> "progn"
- (minibuffer-complete) ;
- ) ;end-> "if (and temp"
- ) ;end-> "let* ((mark nil) (t..."
- ) ;end-> "defun func-mini-mat..."
- ;;;======================================================================
- (defun func-mini-end()
- "return key in minibuffer for defaults"
- (interactive)
- (if (eq mini-default-buffer nil)
- (setq mini-default-buffer " misc-defaults")
- )
- (let* ((a nil) (mark nil) (temp_buffer (buffer-name)))
- (goto-char (point-min))
- (setq mark (point))
- (end-of-line 1)
- (setq a (buffer-substring (point) mark))
- (if (string-match "[^ ]" a)
- (progn
- (if (not (get-buffer mini-default-buffer))
- (progn
- (get-buffer-create mini-default-buffer)
- (set-buffer mini-default-buffer)
- (newline 1)
- )
- (set-buffer mini-default-buffer)
- )
- (goto-char (point-max))
- (previous-line 1)
- (beginning-of-line)
- (if (not (re-search-forward
- (concat "^" (regexp-quote a) "$") (point-max) 2))
- (progn
- (insert-string a)
- (newline)
- )
- )
- (goto-char (point-max))
- )
- )
- (set-buffer temp_buffer)
- (setq mini-default-buffer nil)
- (exit-minibuffer)
- )
- )
- ;;;===================================================================
- (defun func-mini-abort()
- (interactive)
- (if (eq mini-default-buffer nil)
- (setq mini-default-buffer " misc-defaults")
- )
- (get-buffer-create mini-default-buffer)
- (set-buffer mini-default-buffer)
- (goto-char (point-max))
- (setq mini-default-buffer nil)
- (abort-recursive-edit)
- )
- (defun func-execute-ext-cmd(arg)
- "execute extended command with defaults"
- (interactive "p")
- (setq mini-default-buffer " extend-cmd-default")
- (if (/= arg 1)
- (setq arg (concat "C-u " arg " "))
- (setq arg "")
- )
- (call-interactively
- (intern
- (completing-read (concat arg "Cmd: ") obarray 'commandp t)))
- )
- (define-key minibuffer-local-map 'down 'func-mini-get-down) ;down arrow
- (define-key minibuffer-local-map 'up 'func-mini-get-up) ;up arrow
- (define-key minibuffer-local-map 'return 'func-mini-end) ;'return key'
- (define-key minibuffer-local-map '(control g) 'func-mini-abort) ;'return key'
- (define-key minibuffer-local-completion-map 'down 'func-mini-get-down) ;down arrow
- (define-key minibuffer-local-completion-map 'up 'func-mini-get-up) ;up arrow
- (define-key minibuffer-local-completion-map 'return 'func-mini-end) ;'return key'
- (define-key minibuffer-local-completion-map '(control g) 'func-mini-abort) ;'return key'
- (define-key minibuffer-local-must-match-map 'down 'func-mini-get-down) ;down arrow
- (define-key minibuffer-local-must-match-map 'up 'func-mini-get-up) ;up arrow
- (define-key minibuffer-local-must-match-map 'return 'func-mini-match-end) ;'return key'
- (define-key minibuffer-local-must-match-map '(control g) 'func-mini-abort) ;'return key'
- (define-key minibuffer-local-must-match-map '(control space) 'lisp-complete-symbol) ;'return key'
-
- ;;; ====== end of mini-buffer.el
-
- Here's an example of find-file code using it. It also is a little
- smarter than the normal find-file...
-
- (defun edit-file()
- "(edit-file) - Edit a file. If the current buffer is empty, read the file into
- the current window. If there is data, read the file into the other buffer.
-
- Also see command: visit-file - \\[visit-file]"
- (interactive) ;
- (file-sub "Edit file: " nil) ;
- ) ;end->"(defun edit-file()"
-
- (defun visit-file()
- "(visit-file) - Visit (read only) a file.
- If the current buffer is empty, read the file into the current window.
- If there is data, read the file into the other buffer
-
- Also see command: edit-file - \\[edit-file]"
- (interactive) ;
- (file-sub "Browse file: " t) ;
- ) ;end->"(defun visit-file()"
-
- (defun file-sub(prompt read-only)
- (setq mini-default-buffer " mini-edit-file-default")
- (let* ((file-name (read-file-name prompt nil t)))
- (if (and (equal (buffer-size) 0)
- (not (string-match "^\\*.*\\*$" (buffer-name)))
- ) ;end->"(and (equal (buffer..."
- (condition-case error
- (find-alternate-file file-name)
- (error
- (find-file file-name)
- ) ;end->"(error"
- ) ;end->"(condition-case error"
- (if (and (string-match "^\\*scratch\\*$" (buffer-name))
- (equal (buffer-size) 0))
- (find-file file-name)
- (find-file-other-window file-name)
- ) ;end->"(if (and (string-ma..."
- ) ;end->"(if (and (equal (bu..."
- ) ;end->"(let* ((file-name (..."
- (setq buffer-read-only read-only)
- ) ;end->"(defun edit-file()"
-
- ========= end of example ===================================
-
- As long as I'm at it, for all you elisp-ers here is a auto-commenter.
- it help keeping track of 'end' parenthesis.
-
- ;;;****************************************************************************
- ;;;** AUTO-COMMENT.EL - Routines to automatically comment code lines
- ;;;** which contain only the symbols for the type parenthesis close.
- ;;;** (whitespace, and old auto-comments are ignored)
- ;;;** This function is activated as a minor mode and using blink-paren-hook
- ;;;** When on close-paren-link is used, when off blink-matching-open is used.
- ;;;** The local variable auto-comment-mode is the final control used.
- ;;;** When auto-comment-mode is not nil and a close parenthesis type code
- ;;;** (defined by the active character syntax table) is typed on a line
- ;;;** by itself or with whitespace and/or an auto-comment, the matching
- ;;;** open parenthesis is located and the text from it to the end of its
- ;;;** line is used to form an auto-comment:
- ;;;** the close parenthesis is placed on an indented line by itself
- ;;;** (kill-comment) is used to set to dot comment column and add
- ;;;** the active comment character/sequence.
- ;;;** The user defined auto-close-prefix is added and the comment
- ;;;** is processed to fit before fill-column
- ;;;** If additional levels of close-parenthesis were on the original
- ;;;** line they are processed in the same manner.
- ;;;****************************************************************************
- (defvar auto-comment-mode nil
- "to automatically comment at function close
- t = do auto comment
- nil = don't do auto comment
- The function 'auto-comment-mode' will toggle this and update the display"
- ) ;end->"(defvar auto-comment-mode nil"
- (make-variable-buffer-local 'auto-comment-mode)
- (defvar auto-close-prefix "end->"
- "*defines the prefix inserted to start an auto-commented close parenthesis"
- ) ;end->"(defvar auto-close-prefix 'e..."
- (or (assq 'auto-comment-mode minor-mode-alist)
- (setq minor-mode-alist ;define as minor mode
- (cons '(auto-comment-mode " Auto_cmt") minor-mode-alist) ;text for line
- ) ;end->"(setq minor-mode-alist ;"
- ) ;end->"(or (assq 'auto-comment-mode..."
- (defvar old-lisp-indent-line nil
- "symbol function for lisp-indent-line prior to intercept for
- auto-comment-close being substituted"
- ) ;end->"(defvar old-lisp-indent-line nil"
- (if (not old-lisp-indent-line) ;define only if nil
- (progn ;
- (fset 'old-lisp-indent-line (symbol-function 'lisp-indent-line)) ;
- (defun lisp-indent-line(&optional exp) ;
- "Tab response of auto-comment-close if only closes (without non-auto comment)
- on line where tab is called, else use normal tab function"
- (interactive) ;
- (if (and auto-comment-mode ;
- (interactive-p) ;
- (save-excursion ;
- (beginning-of-line) ;
- (looking-at "[ ]*\\s)+[ ]*\\s<?") ;
- ) ;end->"(save-excursion ;"
- ) ;end->"(and auto-comment-mode ;"
- (progn ;
- (beginning-of-line) ;
- (re-search-forward "\\s)" (point-max) nil 1) ;
- (auto-comment-close) ;
- ) ;end->"(progn ;"
- (old-lisp-indent-line exp) ;not comment time so call old function
- ) ;end->"(if (and auto-comment-mode ;"
- ) ;end->"(defun lisp-indent-line(&opt..."
- ) ;end->"(progn ;"
- ) ;end->"(if (not old-lisp-indent-lin..."
- (defun auto-comment-mode(val)
- "Function to toggle auto commenter when a function close is entered
- from the keyboard on and off"
- (interactive "p") ;allow execution as extended command
- (setq blink-paren-hook 'close-paren-link) ;link in automatic comment close
- (if (or (interactive-p) (eq val 0)) ;if extended command or parameter=0
- (if auto-comment-mode ;if defined
- (setq auto-comment-mode nil) ;if defined turn off
- (setq auto-comment-mode t) ;if not defined turn on
- ) ;end->"(if auto-comment-mode ;if d..."
- (if (< val 0) ;if parameter was negative
- (setq auto-comment-mode nil) ;if parameter was negative turn off
- (setq auto-comment-mode t) ;if parameter was positive turn on
- ) ;end->"(if (< val 0) ;"
- ) ;end->"(if (or (interactive-p) (eq ..."
- (set-buffer-modified-p (buffer-modified-p)) ;
- ) ;end->"(defun auto-comment-mode(val)"
- (defun close-paren-link()
- "Handle close parenthesis: If matching end-of-defun, re-comment defun.
- If only closes and whitespace are on the current line, auto-comment-close.
- In any other case the matching open parenthesis will be displayed"
- (if (and auto-comment-mode ;
- (save-excursion ;
- (beginning-of-line) ;
- (looking-at "\\s-*\\s)+\\s-*$") ;
- ) ;end->"(save-excursion ;"
- ) ;end->"(and auto-comment-mode ;"
- (progn ;
- (beginning-of-line) ;
- (re-search-forward "\\s)" (point-max) nil 1) ;
- (if (save-excursion (end-of-line) ;if line end ends function:
- (eq 0 (nth 0 ;
- (parse-partial-sexp 1 (dot)) ;
- ) ;end->"(nth 0 ;"
- ) ;end->"(eq 0 (nth 0 ;"
- ) ;end->"(save-excursion (end-of-line..."
- (re-comment-close) ;re do entire function
- (auto-comment-close) ;else line only
- ) ;end->"(if (save-excursion (end-of-..."
- ) ;end->"(progn ;"
- (blink-matching-open) ;use normal hook
- ) ;end->"(if (and auto-comment-mode ;"
- ) ;end->"(defun close-paren-link()"
- (defun auto-comment-close ()
- "(auto-comment-close) - reallign the current line and comment and then
- automatically comment the initial close parenthesis type characters at the
- start of current line if there is not a manual comment present.
-
- Warning: due to the parser's failure to recognize a comment as a comment,
- quotes(\") within the open text will be changed to single quotes(')"
- (interactive)
- (old-lisp-indent-line) ;set proper indention for line
- (indent-for-comment) ;goto comment, realign or add
- ;;; if looking at a line with a manual comment, do NOT process further
- (if (looking-at ;if blank comment or auto-comment
- (concat "\\(\\s-*$\\)\\|\\(" (regexp-quote auto-close-prefix) "\\)")
- ) ;end->"(looking-at ;if blank com..."
- (progn
- (beginning-of-line)
- (if (re-search-forward ;find starting close
- "\\s)" (save-excursion (end-of-line) (dot)) nil) ;try whole line
- (progn
- (forward-char -1) ;align as though found by back search
- (let ((loc)(string)(key 1)(room)
- (acpsz (1+ (length auto-close-prefix)))
- ) ;end->"((loc)(string)(key 1)(room)"
- (save-excursion ;keep track of close location
- (end-of-line) ;force to end of line
- (kill-comment 1) ;kill any previous comment
- )
- (while (> (setq key (1- key)) -1) ;loop to handle multiple ")"s
- (setq loc (dot)) ;save location of ")"
- (match-paren) ;goto matching open
- (setq string ;get text
- (buffer-substring (dot) (progn (end-of-line) (dot)))
- ) ;end->"(setq string ;get text"
- (goto-char (1+ loc)) ;return to char after original ")"
- (delete-horizontal-space) ;remove white-space after ")"
- (if (looking-at "\\s)") ;if a another ")"
- (save-excursion ;save cursor position
- (insert-char ?\n 1) ;force line break
- (old-lisp-indent-line) ;indent new ")" line
- (setq key (1+ key)) ;force loop to handle multiple ")"s
- ) ;end->"(save-excursion ;save curs..."
- ) ;end->"(if (looking-at '\\s)') ;if..."
- (end-of-line) ;force to end of line
- (indent-for-comment) ;set to comment column & force comment
- (if (< (setq room (- fill-column (current-column) acpsz)) 10) ;room?
- (progn ;if insuffient room for comment
- (newline) ;break line
- (end-of-line) ;force to end of line
- (indent-for-comment) ;set to comment column & force comment
- (setq room (- fill-column (current-column) acpsz)) ;new size
- ) ;end->"(progn ;if insuffient room..."
- ) ;end->"(if (< (setq room (- fill-c..."
- (if (> (length string) room) ;if trim needed
- (setq string ;trim and add trailing ...
- (concat (substring string 0 (- room 4)) "...")
- ) ;end->"(setq string"
- ) ;end->"(if (> (length string) room..."
- (insert auto-close-prefix "\"") ;insert prefix and quote
- (setq loc (dot)) ;save location
- (insert string) ;Insert comment string
- (subst-char-in-region loc (dot) ?\" ?') ;get rid of quotes
- (subst-char-in-region loc (dot) ? ? ) ;get rid of tabs
- (insert "\"") ;insert final quote
- (if (> key 0) ;if more ")"s to process
- (progn ;find next ")"
- (re-search-forward "\\s)" (point-max) nil)
- (backward-char 1) ;point to ")"
- ) ;end->"(progn ;find next ')'"
- ) ;end->"(if (> key 0) ;if more ')'..."
- ) ;end->"(while (> (setq key (1- key..."
- ) ;end->"(let ((loc)(string)(key 1)(..."
- (if (interactive-p)
- (message "Comment done")
- ) ;end->"(if (interactive-p)"
- ) ;end->"(progn"
- ) ;end->"(if (re-search-forward ;fi..."
- ) ;end->"(progn"
- ) ;end->"(if (looking-at ;if blank..."
- ) ;end->"(defun auto-comment-close ()"
- (defun re-comment-close ()
- "(re-comment-close) for entire function process each line outside of quotes
- using auto-comment-close"
- (interactive)
- (save-excursion
- (save-restriction
- (end-of-defun) ;locate end of function
- (newline 1) ;insert line for kill comment bug
- (let ((mark (dot))) ;save for end test
- (beginning-of-defun) ;locate start of function
- (narrow-to-region (dot) mark) ;select function as if entire buffer
- (delete-eol-white-space) ;remove excess white space after lines
- (while (not (progn (forward-line 1) (looking-at "\\'"))) ;while more lines
- (if (not (nth 3 (parse-partial-sexp (point-min)(dot)))) ;if not in quote
- (if (looking-at "\\s-*\\s)+") ;if line starts with close
- (auto-comment-close) ;align/comment process line
- (if (not (looking-at ;if not a blank line or quote
- "\\($\\)\\|\\(\\s-*\"\\)"))
- (progn ;if not at comment or quote start
- (old-lisp-indent-line) ;set proper line indention
- (indent-for-comment) ;goto comment, realign or add
- ) ;end->"(progn"
- ) ;end->"(if (not (looking-at '\\s<'..."
- ) ;end->"(if (looking-at '\\s-*\\s)+..."
- ) ;end->"(if (not (nth 3 (parse-part..."
- ) ;end->"(while (< (dot) (point-max)..."
- ) ;end->"(let ((mark (dot))) ;save ..."
- (end-of-defun) ;locate end of function
- (delete-char -1 nil) ;kill inserted line (eol)
- ) ;end-> "(save-restriction"
- ) ;end-> "(save-excursion"
- (if (interactive-p)
- (message "Re-commented")
- ) ;end-> "(if (interactive-p)"
- ) ;end-> "(defun re-comment-close ()"
- (defun tab-link()
- "Handle tab by auto-comment-close if only closes and whitespace
- are on line the current line, otherwise old-lisp-indent-line"
- (if (and auto-comment-mode
- (save-excursion
- (beginning-of-line)
- (looking-at "[ ]*\\s)+[ ]*$")
- ) ;end-> "(save-excursion "
- ) ;end-> "(and auto-comment-mode"
- (progn
- (beginning-of-line)
- (re-search-forward "\\s)" (point-max) nil 1)
- (auto-comment-close)
- ) ;end-> "(progn"
- (old-lisp-indent-line) ;use normal tab function
- ) ;end-> "(if (and auto-comment-mode"
- ) ;end-> "(defun close-paren-link()"
-
- --
- Mike Battaglia
- DSC Communications Corp
-
- (214) 519-3253
- mbattagl@dsccc.com
-
-
- ==================================================================
-
- The universe never did make sense; I suspect that it was built on
- government contract.
-
- *** heinlein ****
-
- ==================================================================
-