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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
;;; wrolo-logic.el --- Performs logical retrievals on rolodex files
;; Copyright (C) 1989-1995, Free Software Foundation, Inc.
;; Developed with support from Motorola Inc.
;; Author: Bob Weiner, Brown U.
;; Maintainer: Mats Lidell <matsl@contactor.se>
;; Keywords: hypermedia, matching
;; This file is part of GNU Hyperbole.
;; GNU Hyperbole 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 Hyperbole 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, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; INSTALLATION:
;;
;; See also wrolo.el. These functions are separated from wrolo.el since many
;; users may never want or need them. They can be automatically loaded when
;; desired by adding the following to one of your Emacs init files:
;;
;; (autoload 'rolo-logic "wrolo-logic" "Logical rolodex search filters." t)
;;
;; FEATURES:
;;
;; 1. One command, 'rolo-logic' which takes a logical search expression as
;; an argument and displays any matching entries.
;;
;; 2. Logical 'and', 'or', 'not', and 'xor' rolodex entry retrieval filter
;; functions. They take any number of string or boolean arguments and
;; may be nested. NOTE THAT THESE FUNCTIONS SHOULD NEVER BE CALLED
;; DIRECTLY UNLESS THE FREE VARIABLES 'start' and 'end' ARE BOUND
;; BEFOREHAND.
;;
;; EXAMPLE:
;;
;; (rolo-logic (function
;; (lambda ()
;; (rolo-and
;; (rolo-not "Tool-And-Die")
;; "secretary"))))
;;
;; would find all non-Tool-And-Die Corp. secretaries in your rolodex.
;;
;; The logical matching routines are not at all optimal, but then most
;; rolodex files are not terribly lengthy either.
;;
;;; Code:
(require 'wrolo)
;;;###autoload
(defun rolo-logic (func &optional in-bufs count-only include-sub-entries
no-sub-entries-out)
"Apply FUNC to all entries in optional IN-BUFS, display entries where FUNC is non-nil.
If IN-BUFS is nil, 'rolo-file-list' is used. If optional COUNT-ONLY is
non-nil, don't display entries, return count of matching entries only. If
optional INCLUDE-SUB-ENTRIES flag is non-nil, FUNC will be applied across all
sub-entries at once. Default is to apply FUNC to each entry and sub-entry
separately. Entries are displayed with all of their sub-entries unless
INCLUDE-SUB-ENTRIES is nil and optional NO-SUB-ENTRIES-OUT flag is non-nil.
FUNC should use the free variables 'start' and 'end' which contain the limits
of the region on which it should operate. Returns number of applications of
FUNC that return non-nil."
(interactive "xLogic function of no arguments, (lambda () (<function calls>): ")
(let ((obuf (current-buffer))
(display-buf (if count-only
nil
(prog1 (set-buffer (get-buffer-create rolo-display-buffer))
(setq buffer-read-only nil)
(erase-buffer)))))
(let ((result
(mapcar
(function
(lambda (in-bufs)
(rolo-map-logic func in-bufs count-only include-sub-entries
no-sub-entries-out)))
(cond ((null in-bufs) rolo-file-list)
((listp in-bufs) in-bufs)
((list in-bufs))))))
(let ((total-matches (apply '+ result)))
(if (or count-only (= total-matches 0))
nil
(pop-to-buffer display-buf)
(goto-char (point-min))
(set-buffer-modified-p nil)
(setq buffer-read-only t)
(let ((buf (get-buffer-window obuf)))
(if buf (select-window buf) (switch-to-buffer buf))))
(if (interactive-p)
(message (concat (if (= total-matches 0) "No" total-matches)
" matching entr"
(if (= total-matches 1) "y" "ies")
" found in rolodex.")))
total-matches))))
(defun rolo-map-logic (func rolo-buf &optional count-only
include-sub-entries no-sub-entries-out)
"Apply FUNC to all entries in ROLO-BUF, write to buffer entries where FUNC is non-nil.
If optional COUNT-ONLY is non-nil, don't display entries, return count of
matching entries only. If optional INCLUDE-SUB-ENTRIES flag is non-nil, FUNC
will be applied across all sub-entries at once. Default is to apply FUNC to
each entry and sub-entry separately. Entries are displayed with all of their
sub-entries unless INCLUDE-SUB-ENTRIES is nil and optional NO-SUB-ENTRIES-OUT
flag is non-nil. FUNC should use the free variables 'start' and 'end' which
contain the limits of the region on which it should operate. Returns number
of applications of FUNC that return non-nil."
(if (or (bufferp rolo-buf)
(if (file-exists-p rolo-buf)
(setq rolo-buf (find-file-noselect rolo-buf t))))
(let* ((display-buf (set-buffer (get-buffer-create rolo-display-buffer)))
(buffer-read-only))
(let ((hdr-pos) (num-found 0))
(set-buffer rolo-buf)
(goto-char (point-min))
(if (re-search-forward rolo-hdr-regexp nil t 2)
(progn (forward-line)
(setq hdr-pos (cons (point-min) (point)))))
(let* ((start)
(end)
(end-entry-hdr)
(curr-entry-level))
(while (re-search-forward rolo-entry-regexp nil t)
(setq start (save-excursion (beginning-of-line) (point))
next-entry-exists nil
end-entry-hdr (point)
curr-entry-level (buffer-substring start end-entry-hdr)
end (rolo-to-entry-end include-sub-entries curr-entry-level))
(let ((fun (funcall func)))
(or count-only
(and fun (= num-found 0) hdr-pos
(append-to-buffer display-buf
(car hdr-pos) (cdr hdr-pos))))
(if fun
(progn (goto-char end)
(setq num-found (1+ num-found)
end (if (or include-sub-entries
no-sub-entries-out)
end
(goto-char (rolo-to-entry-end
t curr-entry-level))))
(or count-only
(append-to-buffer display-buf start end)))
(goto-char end-entry-hdr)))))
(rolo-kill-buffer rolo-buf)
num-found))
0))
;;
;; INTERNAL FUNCTIONS.
;;
;; Do NOT call the following functions directly.
;; Send them as parts of a lambda expression to 'rolo-logic'.
(defun rolo-not (&rest pat-list)
"Logical <not> rolodex entry filter. PAT-LIST is a list of pattern elements.
Each element may be t, nil, or a string."
(let ((pat))
(while (and pat-list
(or (not (setq pat (car pat-list)))
(and (not (eq pat t))
(goto-char start)
(not (search-forward pat end t)))))
(setq pat-list (cdr pat-list)))
(if pat-list nil t)))
(defun rolo-or (&rest pat-list)
"Logical <or> rolodex entry filter. PAT-LIST is a list of pattern elements.
Each element may be t, nil, or a string."
(if (memq t pat-list)
t
(let ((pat))
(while (and pat-list
(or (not (setq pat (car pat-list)))
(and (not (eq pat t))
(goto-char start)
(not (search-forward pat end t)))))
(setq pat-list (cdr pat-list)))
(if pat-list t nil))))
(defun rolo-xor (&rest pat-list)
"Logical <xor> rolodex entry filter. PAT-LIST is a list of pattern elements.
Each element may be t, nil, or a string."
(let ((pat)
(matches 0))
(while (and pat-list
(or (not (setq pat (car pat-list)))
(and (or (eq pat t)
(not (goto-char start))
(search-forward pat end t))
(setq matches (1+ matches)))
t)
(< matches 2))
(setq pat-list (cdr pat-list)))
(= matches 1)))
(defun rolo-and (&rest pat-list)
"Logical <and> rolodex entry filter. PAT-LIST is a list of pattern elements.
Each element may be t, nil, or a string."
(if (memq nil pat-list)
nil
(let ((pat))
(while (and pat-list
(setq pat (car pat-list))
(or (eq pat t)
(not (goto-char start))
(search-forward pat end t)))
(setq pat-list (cdr pat-list)))
(if pat-list nil t))))
;; Work with regular expression patterns rather than strings
(defun rolo-r-not (&rest pat-list)
"Logical <not> rolodex entry filter. PAT-LIST is a list of pattern elements.
Each element may be t, nil, or a string."
(let ((pat))
(while (and pat-list
(or (not (setq pat (car pat-list)))
(and (not (eq pat t))
(goto-char start)
(not (re-search-forward pat end t)))))
(setq pat-list (cdr pat-list)))
(if pat-list nil t)))
(defun rolo-r-or (&rest pat-list)
"Logical <or> rolodex entry filter. PAT-LIST is a list of pattern elements.
Each element may be t, nil, or a string."
(if (memq t pat-list)
t
(let ((pat))
(while (and pat-list
(or (not (setq pat (car pat-list)))
(and (not (eq pat t))
(goto-char start)
(not (re-search-forward pat end t)))))
(setq pat-list (cdr pat-list)))
(if pat-list t nil))))
(defun rolo-r-xor (&rest pat-list)
"Logical <xor> rolodex entry filter. PAT-LIST is a list of pattern elements.
Each element may be t, nil, or a string."
(let ((pat)
(matches 0))
(while (and pat-list
(or (not (setq pat (car pat-list)))
(and (or (eq pat t)
(not (goto-char start))
(re-search-forward pat end t))
(setq matches (1+ matches)))
t)
(< matches 2))
(setq pat-list (cdr pat-list)))
(= matches 1)))
(defun rolo-r-and (&rest pat-list)
"Logical <and> rolodex entry filter. PAT-LIST is a list of pattern elements.
Each element may be t, nil, or a string."
(if (memq nil pat-list)
nil
(let ((pat))
(while (and pat-list
(setq pat (car pat-list))
(or (eq pat t)
(not (goto-char start))
(re-search-forward pat end t)))
(setq pat-list (cdr pat-list)))
(if pat-list nil t))))
(provide 'wrolo-logic)
;;; wrolo-logic.el ends here
|