| 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 |
(defgroup pages nil |
|---|
| 241 |
"Extended page-handling commands." |
|---|
| 242 |
:group 'extensions) |
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
(defcustom pages-directory-buffer-narrowing-p t |
|---|
| 246 |
"*If non-nil, `pages-directory-goto' narrows pages buffer to entry." |
|---|
| 247 |
:type 'boolean |
|---|
| 248 |
:group 'pages) |
|---|
| 249 |
|
|---|
| 250 |
(defcustom pages-directory-for-adding-page-narrowing-p t |
|---|
| 251 |
"*If non-nil, `add-new-page' narrows page buffer to new entry." |
|---|
| 252 |
:type 'boolean |
|---|
| 253 |
:group 'pages) |
|---|
| 254 |
|
|---|
| 255 |
(defcustom pages-directory-for-adding-new-page-before-current-page-p t |
|---|
| 256 |
"*If non-nil, `add-new-page' inserts new page before current page." |
|---|
| 257 |
:type 'boolean |
|---|
| 258 |
:group 'pages) |
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
(defcustom pages-addresses-file-name "~/addresses" |
|---|
| 264 |
"*Standard name for file of addresses. Entries separated by page-delimiter. |
|---|
| 265 |
Used by `pages-directory-for-addresses' function." |
|---|
| 266 |
:type 'file |
|---|
| 267 |
:group 'pages) |
|---|
| 268 |
|
|---|
| 269 |
(defcustom pages-directory-for-addresses-goto-narrowing-p t |
|---|
| 270 |
"*If non-nil, `pages-directory-goto' narrows addresses buffer to entry." |
|---|
| 271 |
:type 'boolean |
|---|
| 272 |
:group 'pages) |
|---|
| 273 |
|
|---|
| 274 |
(defcustom pages-directory-for-addresses-buffer-keep-windows-p t |
|---|
| 275 |
"*If nil, `pages-directory-for-addresses' deletes other windows." |
|---|
| 276 |
:type 'boolean |
|---|
| 277 |
:group 'pages) |
|---|
| 278 |
|
|---|
| 279 |
(defcustom pages-directory-for-adding-addresses-narrowing-p t |
|---|
| 280 |
"*If non-nil, `add-new-page' narrows addresses buffer to new entry." |
|---|
| 281 |
:type 'boolean |
|---|
| 282 |
:group 'pages) |
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
(global-unset-key "\C-x\C-p") |
|---|
| 288 |
|
|---|
| 289 |
(defvar ctl-x-ctl-p-map (make-sparse-keymap) |
|---|
| 290 |
"Keymap for subcommands of C-x C-p, which are for page handling.") |
|---|
| 291 |
|
|---|
| 292 |
(define-key ctl-x-map "\C-p" 'ctl-x-ctl-p-prefix) |
|---|
| 293 |
(fset 'ctl-x-ctl-p-prefix ctl-x-ctl-p-map) |
|---|
| 294 |
|
|---|
| 295 |
(define-key ctl-x-ctl-p-map "\C-n" 'next-page) |
|---|
| 296 |
(define-key ctl-x-ctl-p-map "\C-p" 'previous-page) |
|---|
| 297 |
(define-key ctl-x-ctl-p-map "\C-a" 'add-new-page) |
|---|
| 298 |
(define-key ctl-x-ctl-p-map "\C-m" 'mark-page) |
|---|
| 299 |
(define-key ctl-x-ctl-p-map "\C-s" 'search-pages) |
|---|
| 300 |
(define-key ctl-x-ctl-p-map "s" 'sort-pages-buffer) |
|---|
| 301 |
(define-key ctl-x-ctl-p-map "\C-l" 'set-page-delimiter) |
|---|
| 302 |
(define-key ctl-x-ctl-p-map "\C-d" 'pages-directory) |
|---|
| 303 |
(define-key ctl-x-ctl-p-map "d" 'pages-directory-for-addresses) |
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
(defun next-page (&optional count) |
|---|
| 309 |
"Move to the next page bounded by the `page-delimiter' variable. |
|---|
| 310 |
With arg (prefix if interactive), move that many pages." |
|---|
| 311 |
(interactive "p") |
|---|
| 312 |
(or count (setq count 1)) |
|---|
| 313 |
(widen) |
|---|
| 314 |
|
|---|
| 315 |
(while (and (> count 0) (not (eobp))) |
|---|
| 316 |
(if (re-search-forward page-delimiter nil t) |
|---|
| 317 |
nil |
|---|
| 318 |
(goto-char (point-max))) |
|---|
| 319 |
(setq count (1- count))) |
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
(while (and (< count 1) (not (bobp))) |
|---|
| 324 |
(if (re-search-backward page-delimiter nil t) |
|---|
| 325 |
(goto-char (match-beginning 0)) |
|---|
| 326 |
(goto-char (point-min))) |
|---|
| 327 |
(setq count (1+ count))) |
|---|
| 328 |
(narrow-to-page) |
|---|
| 329 |
(goto-char (point-min)) |
|---|
| 330 |
(recenter 0)) |
|---|
| 331 |
|
|---|
| 332 |
(defun previous-page (&optional count) |
|---|
| 333 |
"Move to the previous page bounded by the `page-delimiter' variable. |
|---|
| 334 |
With arg (prefix if interactive), move that many pages." |
|---|
| 335 |
(interactive "p") |
|---|
| 336 |
(or count (setq count 1)) |
|---|
| 337 |
(next-page (- count))) |
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
(defun add-new-page (header-line) |
|---|
| 343 |
"Insert new page. Prompt for header line. |
|---|
| 344 |
|
|---|
| 345 |
If point is in the pages directory buffer, insert the new page in the |
|---|
| 346 |
buffer associated with the directory. |
|---|
| 347 |
|
|---|
| 348 |
Insert the new page just before current page if |
|---|
| 349 |
pages-directory-for-adding-new-page-before-current-page-p variable |
|---|
| 350 |
is non-nil. Else insert at exact location of point. |
|---|
| 351 |
|
|---|
| 352 |
Narrow to new page if |
|---|
| 353 |
pages-directory-for-adding-page-narrowing-p variable |
|---|
| 354 |
is non-nil. |
|---|
| 355 |
|
|---|
| 356 |
Page begins with a `^L' as the default page-delimiter. |
|---|
| 357 |
Use \\[set-page-delimiter] to change the page-delimiter. |
|---|
| 358 |
Point is left in the body of page." |
|---|
| 359 |
(interactive "sHeader line: ") |
|---|
| 360 |
(widen) |
|---|
| 361 |
|
|---|
| 362 |
(if (eq major-mode 'pages-directory-mode) |
|---|
| 363 |
(progn |
|---|
| 364 |
|
|---|
| 365 |
(if pages-directory-for-adding-new-page-before-current-page-p |
|---|
| 366 |
(pages-directory-goto) |
|---|
| 367 |
(pages-directory-goto) |
|---|
| 368 |
(forward-page) |
|---|
| 369 |
(or (eobp) (forward-line -1))))) |
|---|
| 370 |
(widen) |
|---|
| 371 |
|
|---|
| 372 |
(and pages-directory-for-adding-new-page-before-current-page-p |
|---|
| 373 |
(if (re-search-backward page-delimiter nil t) |
|---|
| 374 |
(goto-char (match-beginning 0)) |
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
(goto-char (point-min)) |
|---|
| 378 |
(insert |
|---|
| 379 |
(format "%s\n" |
|---|
| 380 |
|
|---|
| 381 |
(if (eq '^ (car (read-from-string page-delimiter))) |
|---|
| 382 |
(substring page-delimiter 1)))) |
|---|
| 383 |
(goto-char (point-min)))) |
|---|
| 384 |
|
|---|
| 385 |
(if (not (looking-at "^.")) (forward-line 1)) |
|---|
| 386 |
(insert (format "%s\n%s\n\n\n" |
|---|
| 387 |
(if (eq '^ (car (read-from-string page-delimiter))) |
|---|
| 388 |
(substring page-delimiter 1)) |
|---|
| 389 |
header-line)) |
|---|
| 390 |
(forward-line -1) |
|---|
| 391 |
(and pages-directory-for-adding-page-narrowing-p (narrow-to-page))) |
|---|
| 392 |
|
|---|
| 393 |
(defvar pages-last-search nil |
|---|
| 394 |
"Value of last regexp searched for. Initially, nil.") |
|---|
| 395 |
|
|---|
| 396 |
(defun search-pages (regexp) |
|---|
| 397 |
"Search for REGEXP, starting from point, and narrow to page it is in." |
|---|
| 398 |
(interactive (list |
|---|
| 399 |
(read-string |
|---|
| 400 |
(format "Search for `%s' (end with RET): " |
|---|
| 401 |
(or pages-last-search "regexp"))))) |
|---|
| 402 |
(if (equal regexp "") |
|---|
| 403 |
(setq regexp pages-last-search) |
|---|
| 404 |
(setq pages-last-search regexp)) |
|---|
| 405 |
(widen) |
|---|
| 406 |
(re-search-forward regexp) |
|---|
| 407 |
(narrow-to-page)) |
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
(autoload 'sort-subr "sort" "Primary function for sorting." t nil) |
|---|
| 413 |
|
|---|
| 414 |
(defun sort-pages-in-region (reverse beg end) |
|---|
| 415 |
"Sort pages in region alphabetically. Prefix arg means reverse order. |
|---|
| 416 |
|
|---|
| 417 |
Called from a program, there are three arguments: |
|---|
| 418 |
REVERSE (non-nil means reverse order), BEG and END (region to sort)." |
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
(interactive "P\nr") |
|---|
| 425 |
(save-restriction |
|---|
| 426 |
(narrow-to-region beg end) |
|---|
| 427 |
(goto-char (point-min)) |
|---|
| 428 |
|
|---|
| 429 |
(sort-subr reverse |
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
(function (lambda () |
|---|
| 435 |
(re-search-forward page-delimiter nil t) |
|---|
| 436 |
(skip-chars-forward " \t\n") |
|---|
| 437 |
)) |
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 |
(function (lambda () |
|---|
| 442 |
(if (re-search-forward |
|---|
| 443 |
page-delimiter |
|---|
| 444 |
nil |
|---|
| 445 |
t) |
|---|
| 446 |
(goto-char (match-beginning 0)) |
|---|
| 447 |
(goto-char (point-max)))))))) |
|---|
| 448 |
|
|---|
| 449 |
(defun sort-pages-buffer (&optional reverse) |
|---|
| 450 |
"Sort pages alphabetically in buffer. Prefix arg means reverse order. |
|---|
| 451 |
\(Non-nil arg if not interactive.\)" |
|---|
| 452 |
|
|---|
| 453 |
(interactive "P") |
|---|
| 454 |
(or reverse (setq reverse nil)) |
|---|
| 455 |
(widen) |
|---|
| 456 |
(let ((beginning (point-min)) |
|---|
| 457 |
(end (point-max))) |
|---|
| 458 |
(sort-pages-in-region reverse beginning end))) |
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
|
|---|
| 463 |
(defvar pages-directory-previous-regexp nil |
|---|
| 464 |
"Value of previous regexp used by `pages-directory'. |
|---|
| 465 |
\(This regular expression may be used to select only those pages that |
|---|
| 466 |
contain matches to the regexp.\)") |
|---|
| 467 |
|
|---|
| 468 |
(defvar pages-buffer nil |
|---|
| 469 |
"The buffer for which the pages-directory function creates the directory.") |
|---|
| 470 |
|
|---|
| 471 |
(defvar pages-directory-prefix "*Directory for:" |
|---|
| 472 |
"Prefix of name of temporary buffer for pages-directory.") |
|---|
| 473 |
|
|---|
| 474 |
(defvar pages-pos-list nil |
|---|
| 475 |
"List containing the positions of the pages in the pages-buffer.") |
|---|
| 476 |
|
|---|
| 477 |
(defvar pages-target-buffer) |
|---|
| 478 |
|
|---|
| 479 |
(defvar pages-directory-mode-map |
|---|
| 480 |
(let ((map (make-sparse-keymap))) |
|---|
| 481 |
(define-key map "\C-c\C-c" 'pages-directory-goto) |
|---|
| 482 |
(define-key map "\C-c\C-p\C-a" 'add-new-page) |
|---|
| 483 |
(define-key map [mouse-2] 'pages-directory-goto-with-mouse) |
|---|
| 484 |
map) |
|---|
| 485 |
"Keymap for the pages-directory-buffer.") |
|---|
| 486 |
(defvaralias 'pages-directory-map 'pages-directory-mode-map) |
|---|
| 487 |
|
|---|
| 488 |
(defvar original-page-delimiter "^\f" |
|---|
| 489 |
"Default page delimiter.") |
|---|
| 490 |
|
|---|
| 491 |
(defun set-page-delimiter (regexp reset-p) |
|---|
| 492 |
"Set buffer local value of page-delimiter to REGEXP. |
|---|
| 493 |
Called interactively with a prefix argument, reset `page-delimiter' to |
|---|
| 494 |
its original value. |
|---|
| 495 |
|
|---|
| 496 |
In a program, non-nil second arg causes first arg to be ignored and |
|---|
| 497 |
resets the page-delimiter to the original value." |
|---|
| 498 |
|
|---|
| 499 |
(interactive |
|---|
| 500 |
(if current-prefix-arg |
|---|
| 501 |
(list original-page-delimiter "^\f") |
|---|
| 502 |
(list (read-string "Set page-delimiter to regexp: " page-delimiter) |
|---|
| 503 |
nil))) |
|---|
| 504 |
(make-local-variable 'original-page-delimiter) |
|---|
| 505 |
(make-local-variable 'page-delimiter) |
|---|
| 506 |
(setq original-page-delimiter |
|---|
| 507 |
(or original-page-delimiter page-delimiter)) |
|---|
| 508 |
(if (not reset-p) |
|---|
| 509 |
(setq page-delimiter regexp) |
|---|
| 510 |
(setq page-delimiter original-page-delimiter)) |
|---|
| 511 |
(if (interactive-p) |
|---|
| 512 |
(message "The value of `page-delimiter' is now: %s" page-delimiter))) |
|---|
| 513 |
|
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
(defun pages-directory |
|---|
| 518 |
(pages-list-all-headers-p count-lines-p &optional regexp) |
|---|
| 519 |
"Display a directory of the page headers in a temporary buffer. |
|---|
| 520 |
A header is the first non-blank line after the page-delimiter. |
|---|
| 521 |
\\[pages-directory-mode] |
|---|
| 522 |
You may move point to one of the lines in the temporary buffer, |
|---|
| 523 |
then use \\<pages-directory-goto> to go to the same line in the pages buffer. |
|---|
| 524 |
|
|---|
| 525 |
In interactive use: |
|---|
| 526 |
|
|---|
| 527 |
1. With no prefix arg, display all headers. |
|---|
| 528 |
|
|---|
| 529 |
2. With prefix arg, display the headers of only those pages that |
|---|
| 530 |
contain matches to a regular expression for which you are |
|---|
| 531 |
prompted. |
|---|
| 532 |
|
|---|
| 533 |
3. With numeric prefix arg, for every page, print the number of |
|---|
| 534 |
lines within each page. |
|---|
| 535 |
|
|---|
| 536 |
4. With negative numeric prefix arg, for only those pages that |
|---|
| 537 |
match a regular expression, print the number of lines within |
|---|
| 538 |
each page. |
|---|
| 539 |
|
|---|
| 540 |
When called from a program, non-nil first arg means list all headers; |
|---|
| 541 |
non-nil second arg means print numbers of lines in each page; if first |
|---|
| 542 |
arg is nil, optional third arg is regular expression. |
|---|
| 543 |
|
|---|
| 544 |
If the buffer is narrowed, the `pages-directory' command creates a |
|---|
| 545 |
directory for only the accessible portion of the buffer." |
|---|
| 546 |
|
|---|
| 547 |
(interactive |
|---|
| 548 |
(cond ((not current-prefix-arg) |
|---|
| 549 |
(list t nil nil)) |
|---|
| 550 |
((listp current-prefix-arg) |
|---|
| 551 |
(list nil |
|---|
| 552 |
nil |
|---|
| 553 |
(read-string |
|---|
| 554 |
(format "Select according to `%s' (end with RET): " |
|---|
| 555 |
(or pages-directory-previous-regexp "regexp"))))) |
|---|
| 556 |
((> (prefix-numeric-value current-prefix-arg) 0) |
|---|
| 557 |
(list t t nil)) |
|---|
| 558 |
((< (prefix-numeric-value current-prefix-arg) 0) |
|---|
| 559 |
(list nil |
|---|
| 560 |
t |
|---|
| 561 |
(read-string |
|---|
| 562 |
(format "Select according to `%s' (end with RET): " |
|---|
| 563 |
(or pages-directory-previous-regexp "regexp"))))))) |
|---|
| 564 |
|
|---|
| 565 |
(if (equal regexp "") |
|---|
| 566 |
(setq regexp pages-directory-previous-regexp) |
|---|
| 567 |
(setq pages-directory-previous-regexp regexp)) |
|---|
| 568 |
|
|---|
| 569 |
(if (interactive-p) |
|---|
| 570 |
(message "Creating directory for: %s " |
|---|
| 571 |
(buffer-name))) |
|---|
| 572 |
|
|---|
| 573 |
(let ((pages-target-buffer (current-buffer)) |
|---|
| 574 |
(pages-directory-buffer |
|---|
| 575 |
(concat pages-directory-prefix " " (buffer-name))) |
|---|
| 576 |
(linenum 1) |
|---|
| 577 |
(pages-buffer-original-position (point)) |
|---|
| 578 |
(pages-buffer-original-page 0)) |
|---|
| 579 |
|
|---|
| 580 |
|
|---|
| 581 |
|
|---|
| 582 |
|
|---|
| 583 |
(with-output-to-temp-buffer pages-directory-buffer |
|---|
| 584 |
(save-excursion |
|---|
| 585 |
(set-buffer standard-output) |
|---|
| 586 |
(pages-directory-mode) |
|---|
| 587 |
(insert |
|---|
| 588 |
"==== Pages Directory: use `C-c C-c' to go to page under cursor. ====" ?\n) |
|---|
| 589 |
(setq pages-buffer pages-target-buffer) |
|---|
| 590 |
(setq pages-pos-list nil)) |
|---|
| 591 |
|
|---|
| 592 |
(if pages-list-all-headers-p |
|---|
| 593 |
|
|---|
| 594 |
|
|---|
| 595 |
(save-excursion |
|---|
| 596 |
(goto-char (point-min)) |
|---|
| 597 |
|
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 |
(save-restriction |
|---|
| 601 |
|
|---|
| 602 |
(if (and (save-excursion |
|---|
| 603 |
(re-search-forward page-delimiter nil t)) |
|---|
| 604 |
(= 1 (match-beginning 0))) |
|---|
| 605 |
(goto-char (match-end 0))) |
|---|
| 606 |
(narrow-to-page) |
|---|
| 607 |
(pages-copy-header-and-position count-lines-p)) |
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 |
(while (re-search-forward page-delimiter nil t) |
|---|
| 611 |
(pages-copy-header-and-position count-lines-p))) |
|---|
| 612 |
|
|---|
| 613 |
|
|---|
| 614 |
(save-excursion |
|---|
| 615 |
|
|---|
| 616 |
(goto-char (point-min)) |
|---|
| 617 |
|
|---|
| 618 |
|
|---|
| 619 |
(save-restriction |
|---|
| 620 |
(narrow-to-page) |
|---|
| 621 |
|
|---|
| 622 |
(if (save-excursion (re-search-forward regexp nil t)) |
|---|
| 623 |
(pages-copy-header-and-position count-lines-p))) |
|---|
| 624 |
|
|---|
| 625 |
|
|---|
| 626 |
(while (re-search-forward page-delimiter nil t) |
|---|
| 627 |
(save-restriction |
|---|
| 628 |
(narrow-to-page) |
|---|
| 629 |
|
|---|
| 630 |
(if (save-excursion (re-search-forward regexp nil t)) |
|---|
| 631 |
(pages-copy-header-and-position count-lines-p) |
|---|
| 632 |
))))) |
|---|
| 633 |
|
|---|
| 634 |
(set-buffer standard-output) |
|---|
| 635 |
|
|---|
| 636 |
(setq pages-pos-list (nreverse pages-pos-list)) |
|---|
| 637 |
(if (interactive-p) |
|---|
| 638 |
(message "%d matching lines in: %s" |
|---|
| 639 |
(length pages-pos-list) (buffer-name pages-target-buffer)))) |
|---|
| 640 |
(pop-to-buffer pages-directory-buffer) |
|---|
| 641 |
(sit-for 0) |
|---|
| 642 |
(forward-line (if (= 0 pages-buffer-original-page) |
|---|
| 643 |
1 |
|---|
| 644 |
pages-buffer-original-page)))) |
|---|
| 645 |
(eval-when-compile |
|---|
| 646 |
(defvar pages-buffer-original-position) |
|---|
| 647 |
(defvar pages-buffer-original-page) |
|---|
| 648 |
(defvar pages-buffer-original-page)) |
|---|
| 649 |
|
|---|
| 650 |
(defun pages-copy-header-and-position (count-lines-p) |
|---|
| 651 |
"Copy page header and its position to the Pages Directory. |
|---|
| 652 |
Only arg non-nil, count lines in page and insert before header. |
|---|
| 653 |
Used by `pages-directory' function." |
|---|
| 654 |
|
|---|
| 655 |
(let (position line-count) |
|---|
| 656 |
|
|---|
| 657 |
(if count-lines-p |
|---|
| 658 |
(save-excursion |
|---|
| 659 |
(save-restriction |
|---|
| 660 |
(narrow-to-page) |
|---|
| 661 |
(setq line-count (count-lines (point-min) (point-max)))))) |
|---|
| 662 |
|
|---|
| 663 |
|
|---|
| 664 |
(if (<= (point) pages-buffer-original-position) |
|---|
| 665 |
(setq pages-buffer-original-page |
|---|
| 666 |
(1+ pages-buffer-original-page))) |
|---|
| 667 |
|
|---|
| 668 |
(save-excursion |
|---|
| 669 |
|
|---|
| 670 |
(skip-chars-forward " \t\n") |
|---|
| 671 |
|
|---|
| 672 |
|
|---|
| 673 |
(setq position (make-marker)) |
|---|
| 674 |
(set-marker position (point)) |
|---|
| 675 |
(let ((start (point)) |
|---|
| 676 |
(end (save-excursion (end-of-line) (point))) |
|---|
| 677 |
inserted-at) |
|---|
| 678 |
|
|---|
| 679 |
(set-buffer standard-output) |
|---|
| 680 |
|
|---|
| 681 |
(setq pages-pos-list (cons position pages-pos-list)) |
|---|
| 682 |
|
|---|
| 683 |
(setq inserted-at (point)) |
|---|
| 684 |
(insert-buffer-substring pages-target-buffer start end) |
|---|
| 685 |
(add-text-properties inserted-at (point) |
|---|
| 686 |
'(mouse-face highlight |
|---|
| 687 |
help-echo "mouse-2: go to this page")) |
|---|
| 688 |
(put-text-property inserted-at (point) 'rear-nonsticky 'highlight)) |
|---|
| 689 |
|
|---|
| 690 |
(if count-lines-p |
|---|
| 691 |
(save-excursion |
|---|
| 692 |
(beginning-of-line) |
|---|
| 693 |
(insert (format "%3d: " line-count)))) |
|---|
| 694 |
|
|---|
| 695 |
(terpri)) |
|---|
| 696 |
(end-of-line 1))) |
|---|
| 697 |
|
|---|
| 698 |
(defun pages-directory-mode () |
|---|
| 699 |
"Mode for handling the pages-directory buffer. |
|---|
| 700 |
|
|---|
| 701 |
Move point to one of the lines in this buffer, then use \\[pages-directory-goto] to go |
|---|
| 702 |
to the same line in the pages buffer." |
|---|
| 703 |
|
|---|
| 704 |
(kill-all-local-variables) |
|---|
| 705 |
(use-local-map pages-directory-mode-map) |
|---|
| 706 |
(setq major-mode 'pages-directory-mode) |
|---|
| 707 |
(setq mode-name "Pages-Directory") |
|---|
| 708 |
(make-local-variable 'pages-buffer) |
|---|
| 709 |
(make-local-variable 'pages-pos-list) |
|---|
| 710 |
(make-local-variable 'pages-directory-buffer-narrowing-p) |
|---|
| 711 |
(run-mode-hooks 'pages-directory-mode-hook)) |
|---|
| 712 |
|
|---|
| 713 |
(defun pages-directory-goto () |
|---|
| 714 |
"Go to the corresponding line in the pages buffer." |
|---|
| 715 |
|
|---|
| 716 |
|
|---|
| 717 |
|
|---|
| 718 |
(interactive) |
|---|
| 719 |
(if (or (not pages-buffer) |
|---|
| 720 |
(not (buffer-name pages-buffer))) |
|---|
| 721 |
(progn |
|---|
| 722 |
(setq pages-buffer nil |
|---|
| 723 |
pages-pos-list nil) |
|---|
| 724 |
(error "Buffer in which pages were found is deleted"))) |
|---|
| 725 |
(beginning-of-line) |
|---|
| 726 |
(let* ((pages-number (1- (count-lines (point-min) (point)))) |
|---|
| 727 |
(pos (nth pages-number pages-pos-list)) |
|---|
| 728 |
(end-of-directory-p (eobp)) |
|---|
| 729 |
(narrowing-p pages-directory-buffer-narrowing-p)) |
|---|
| 730 |
(pop-to-buffer pages-buffer) |
|---|
| 731 |
(widen) |
|---|
| 732 |
(if end-of-directory-p |
|---|
| 733 |
(goto-char (point-max)) |
|---|
| 734 |
(goto-char (marker-position pos))) |
|---|
| 735 |
(if narrowing-p (narrow-to-page)))) |
|---|
| 736 |
|
|---|
| 737 |
(defun pages-directory-goto-with-mouse (event) |
|---|
| 738 |
"Go to the corresponding line under the mouse pointer in the pages buffer." |
|---|
| 739 |
(interactive "e") |
|---|
| 740 |
(save-excursion |
|---|
| 741 |
(set-buffer (window-buffer (posn-window (event-end event)))) |
|---|
| 742 |
(save-excursion |
|---|
| 743 |
(goto-char (posn-point (event-end event))) |
|---|
| 744 |
(pages-directory-goto)))) |
|---|
| 745 |
|
|---|
| 746 |
|
|---|
| 747 |
|
|---|
| 748 |
(defun pages-directory-for-addresses (&optional filename) |
|---|
| 749 |
"Find addresses file and display its directory. |
|---|
| 750 |
By default, create and display directory of `pages-addresses-file-name'. |
|---|
| 751 |
Optional argument is FILENAME. In interactive use, with prefix |
|---|
| 752 |
argument, prompt for file name and provide completion. |
|---|
| 753 |
|
|---|
| 754 |
Move point to one of the lines in the displayed directory, |
|---|
| 755 |
then use \\[pages-directory-goto] to go to the same line |
|---|
| 756 |
in the addresses buffer. |
|---|
| 757 |
|
|---|
| 758 |
If pages-directory-for-addresses-goto-narrowing-p is non-nil, |
|---|
| 759 |
`pages-directory-goto' narrows addresses buffer to entry. |
|---|
| 760 |
|
|---|
| 761 |
If pages-directory-for-addresses-buffer-keep-windows-p is nil, |
|---|
| 762 |
this command deletes other windows when it displays the addresses |
|---|
| 763 |
directory." |
|---|
| 764 |
|
|---|
| 765 |
(interactive |
|---|
| 766 |
(list (if current-prefix-arg |
|---|
| 767 |
(read-file-name "Filename: " pages-addresses-file-name)))) |
|---|
| 768 |
|
|---|
| 769 |
(if (interactive-p) |
|---|
| 770 |
(message "Creating directory for: %s " |
|---|
| 771 |
(or filename pages-addresses-file-name))) |
|---|
| 772 |
(if (file-exists-p (or filename pages-addresses-file-name)) |
|---|
| 773 |
(progn |
|---|
| 774 |
(set-buffer |
|---|
| 775 |
(find-file-noselect |
|---|
| 776 |
(expand-file-name |
|---|
| 777 |
(or filename pages-addresses-file-name)))) |
|---|
| 778 |
(widen) |
|---|
| 779 |
(pages-directory t nil nil) |
|---|
| 780 |
|
|---|
| 781 |
|
|---|
| 782 |
|
|---|
| 783 |
(setq pages-directory-buffer-narrowing-p |
|---|
| 784 |
pages-directory-for-addresses-goto-narrowing-p) |
|---|
| 785 |
(or pages-directory-for-addresses-buffer-keep-windows-p |
|---|
| 786 |
(delete-other-windows)) |
|---|
| 787 |
(save-excursion |
|---|
| 788 |
(goto-char (point-min)) |
|---|
| 789 |
(delete-region (point) (save-excursion (end-of-line) (point))) |
|---|
| 790 |
(insert |
|---|
| 791 |
"=== Address List Directory: use `C-c C-c' to go to page under cursor. ===") |
|---|
| 792 |
(set-buffer-modified-p nil) |
|---|
| 793 |
)) |
|---|
| 794 |
(error "No addresses file found!"))) |
|---|
| 795 |
|
|---|
| 796 |
(define-derived-mode pages-directory-address-mode pages-directory-mode |
|---|
| 797 |
"Addresses Directory" |
|---|
| 798 |
"Mode for handling the Addresses Directory buffer. |
|---|
| 799 |
|
|---|
| 800 |
Move point to one of the lines in this buffer, |
|---|
| 801 |
then use \\[pages-directory-goto] to go |
|---|
| 802 |
to the same line in the pages buffer." |
|---|
| 803 |
:syntax-table nil) |
|---|
| 804 |
|
|---|
| 805 |
(provide 'page-ext) |
|---|
| 806 |
|
|---|
| 807 |
|
|---|
| 808 |
|
|---|
| 809 |
|
|---|