Add some notes related to continuous build on Hydra
[bpt/emacs.git] / lisp / woman.el
CommitLineData
077decde
EZ
1;;; woman.el --- browse UN*X manual pages `wo (without) man'
2
ab422c4d 3;; Copyright (C) 2000-2013 Free Software Foundation, Inc.
077decde 4
2d4c3c85 5;; Author: Francis J. Wright <F.J.Wright@qmul.ac.uk>
b25dc41f 6;; Maintainer: FSF
2d4c3c85 7;; Keywords: help, unix
aff379a6 8;; Adapted-By: Eli Zaretskii <eliz@gnu.org>
bd78fa1d 9;; Version: 0.551
2d4c3c85 10;; URL: http://centaur.maths.qmul.ac.uk/Emacs/WoMan/
077decde
EZ
11
12;; This file is part of GNU Emacs.
13
eb3fa2cf 14;; GNU Emacs is free software: you can redistribute it and/or modify
077decde 15;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
16;; the Free Software Foundation, either version 3 of the License, or
17;; (at your option) any later version.
077decde
EZ
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
eb3fa2cf 25;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
077decde
EZ
26
27;;; Commentary:
28
29;; WoMan implements a subset of the formatting performed by the Emacs
30;; `man' (or `manual-entry') command to format a UN*X manual `page'
31;; for display, but without calling any external programs. It is
32;; intended to emulate the whole of the -man macro package, plus those
33;; ?roff requests that are most commonly used in man pages. However,
34;; the emulation is modified to include the reformatting done by the
35;; Emacs `man' command. No hyphenation is performed.
36
37;; Advantages
38
39;; Much more direct, does not require any external programs.
40;; Supports completion on man page names.
41
42;; Disadvantages
43
44;; Not a complete emulation. Currently no support for eqn or tbl.
45;; Slightly slower for large man pages (but usually faster for
46;; small- and medium-size pages).
47
48;; This browser works quite well on simple well-written man files. It
49;; works less well on idiosyncratic files that `break the rules' or
50;; use the more obscure ?roff requests directly. Current test results
51;; are available in the file woman.status.
52
53;; WoMan supports the use of compressed man files via
54;; `auto-compression-mode' by turning it on if necessary. But you may
55;; need to adjust the user option `woman-file-compression-regexp'.
56
57;; Read on for (currently) the only documentation for WoMan!
58
59;; See also the documentation for the WoMan interactive commands and
60;; user option variables, all of which begin with the prefix `woman-'.
61;; This can be done most easily by loading WoMan and then running the
62;; command `woman-mini-help', or selecting the WoMan menu option `Mini
63;; Help' when WoMan is running.
64
65;; WoMan is still under development! Please let me know what doesn't
66;; work -- I am adding and improving functionality as testing shows
67;; that it is necessary. See below for guidance on reporting bugs.
68
077decde
EZ
69;; Recommended use
70;; ===============
71
72;; Put this in your .emacs:
73;; (autoload 'woman "woman"
74;; "Decode and browse a UN*X man page." t)
75;; (autoload 'woman-find-file "woman"
76;; "Find, decode and browse a specific UN*X man-page file." t)
77
78;; Then either (1 -- *RECOMMENDED*): If the `MANPATH' environment
79;; variable is set then WoMan will use it; otherwise you may need to
80;; reset the Lisp variable `woman-manpath', and you may also want to
81;; set the Lisp variable `woman-path'. Please see the online
82;; documentation for these variables. Now you can execute the
83;; extended command `woman', enter or select a manual entry topic,
84;; using completion, and if necessary select a filename, using
85;; completion. By default, WoMan suggests the word nearest to the
86;; cursor in the current buffer as the topic.
87
88;; Or (2): Execute the extended command `woman-find-file' and enter a
89;; filename, using completion. This mode of execution may be useful
90;; for temporary files outside the standard UN*X manual directory
91;; structure.
92
93;; Or (3): Put the next two sexpr's in your .emacs:
94;; (autoload 'woman-dired-find-file "woman"
95;; "In dired, run the WoMan man-page browser on this file." t)
96;; (add-hook 'dired-mode-hook
1edbbf8a
EZ
97;; (lambda ()
98;; (define-key dired-mode-map "W" 'woman-dired-find-file)))
077decde
EZ
99;; and open the directory containing the man page file using dired,
100;; put the cursor on the file, and press `W'.
101
102;; In each case, the result should (!) be a buffer in Man mode showing
103;; a formatted manual entry. When called from WoMan, Man mode should
104;; work as advertised, but modified where necessary in the context of
105;; WoMan. (However, `Man' will still invoke the standard Emacs
106;; manual-browsing facility rather than `WoMan' -- this is
107;; intentional!)
108
109;; (By default, WoMan will automatically define the dired keys "W" and
110;; "w" when it loads, but only if they are not already defined. This
fffa137c 111;; behavior is controlled by the user option `woman-dired-keys'.
077decde
EZ
112;; Note that the `dired-x' (dired extra) package binds
113;; `dired-copy-filename-as-kill' to the key "w" (as pointed out by Jim
114;; Davidson), although "W" appears to be really unused. The `dired-x'
115;; package will over-write the WoMan binding to "w", whereas (by
116;; default) WoMan will not overwrite the `dired-x' binding.)
117
72f0712b
EZ
118;; Using the word at point as the default topic
119;; ============================================
077decde 120
72f0712b
EZ
121;; The `woman' command uses the word nearest to point in the current
122;; buffer as the default topic to look up if it matches the name of a
123;; manual page installed on the system. The default topic can also be
124;; used without confirmation by setting the user-option
125;; `woman-use-topic-at-point' to t; thanks to Benjamin Riefenstahl for
126;; suggesting this functionality.
077decde 127
72f0712b
EZ
128;; The variable `woman-use-topic-at-point' can be rebound locally,
129;; which may be useful to provide special private key bindings, e.g.
077decde
EZ
130
131;; (global-set-key "\C-cw"
1edbbf8a 132;; (lambda ()
077decde 133;; (interactive)
72f0712b 134;; (let ((woman-use-topic-at-point t))
077decde
EZ
135;; (woman)))))
136
137
138;; Customization, Hooks and Imenu
139;; ==============================
140
141;; WoMan supports the GNU Emacs 20+ customization facility, and puts
142;; a customization group called `WoMan' in the `Help' group under the
143;; top-level `Emacs' group. In order to be able to customize WoMan
144;; without first loading it, add the following sexp to your .emacs:
145
146;; (defgroup woman nil
147;; "Browse UNIX manual pages `wo (without) man'."
148;; :tag "WoMan" :group 'help :load "woman")
149
150
151;; WoMan currently runs two hooks: `woman-pre-format-hook' immediately
152;; before formatting a buffer and `woman-post-format-hook' immediately
153;; after formatting a buffer. These hooks can be used for special
154;; customizations that require code to be executed, etc., although
155;; most customization should be possible by setting WoMan user option
156;; variables, e.g. in `.emacs' and should NOT require the use of the
157;; hooks. `woman-pre-format-hook' might be appropriate for face
158;; customization, whereas `woman-post-format-hook' might be
159;; appropriate for installing a dynamic menu using `imenu' (although
160;; it is better to use the built-in WoMan imenu support).
161
162;; The WoMan menu provides an option to make a contents menu for the
163;; current man page (using imenu). Alternatively, if you set the
164;; variable `woman-imenu' to `t' then WoMan will do it automatically
165;; for every man page. The menu title is the value of the variable
166;; `woman-imenu-title', which is "CONTENTS" by default. By default,
167;; the menu shows manual sections and subsections, but you can change
168;; this by changing the value of `woman-imenu-generic-expression'.
169;; This facility is not yet widely tested and may be fooled by obscure
170;; man pages that `break the rules'.
171
172;; WoMan is configured not to replace spaces in an imenu *Completion*
173;; buffer. For further documentation of the use of imenu, such as
174;; menu sorting, see the source file imenu.el, which is distributed
175;; with GNU Emacs.
176
177;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
178
179;; Howard Melman made (essentially) the following suggestions, which
180;; are slightly different from the expression that I currently use.
181;; You may prefer one of Howard's suggestions, which I think assume
182;; that `case-fold-search' is `t' (which it is by default):
183
184;; (setq woman-imenu-generic-expression
185;; '((nil "^\\( \\)?\\([A-Z][A-Z ]+[A-Z]\\)[ \t]*$" 2)))
186
187;; will give support for .SH and .SS, though it won't show the heading
188;; name hierarchy. If you just want .SH in the imenu then use:
189
190;; (setq woman-imenu-generic-expression
191;; '((nil "^\\([A-Z][A-Z ]+[A-Z]\\)[ \t]*$" 1)))
192
193;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
194
195
196;; Vertical spacing and blank lines
197;; ================================
198
199;; The number of consecutive blank lines in the formatted buffer
200;; should be either 0 or 1. A blank line should leave a space like
201;; .sp 1 (p. 14). Current policy is to output vertical space only
202;; immediately before text is output.
203
204
205;; Horizontal and vertical spacing and resolution
206;; ==============================================
207
208;; WoMan currently assumes 10 characters per inch horizontally, hence
209;; a horizontal resolution of 24 basic units, and 5 lines per inch
210;; vertically, hence a vertical resolution of 48 basic units. (nroff
211;; uses 240 per inch).
212
213
214;; The *WoMan-Log* buffer
215;; ======================
216
5618fbd2 217;; This is modeled on the byte-compiler. It logs all files formatted
077decde
EZ
218;; by WoMan, and if WoMan finds anything that it cannot handle then it
219;; writes a warning to this buffer. If the variable `woman-show-log'
220;; is non-nil (by default it is `nil') then WoMan automatically
221;; displays this buffer. Many WoMan warnings can be completely
222;; ignored, because they are reporting the fact that WoMan has ignored
223;; requests that it is correct to ignore. In some future version this
224;; level of paranoia will be reduced, but not until WoMan is more
225;; reliable. At present, all warnings should be treated with some
226;; suspicion. Uninterpreted escape sequences are also logged (in some
227;; cases).
228
229;; Uninterpreted ?roff requests can optionally be left in the
230;; formatted buffer to indicate precisely where they occur by
231;; resetting the variable `woman-ignore' to `nil' (by default it is
232;; `t').
233
234;; Automatic initiation of woman decoding
235
236;; (Probably not a good idea. If you use it, be careful!)
237
238;; Put something like this in your .emacs. The call to
239;; set-visited-file-name is to avoid font-locking triggered by
240;; automatic major mode selection.
241
242;; (autoload 'woman-decode-region "woman")
243
244;; (setq format-alist
245;; (cons
246;; '(man "UN*X man-page source format" "\\.\\(TH\\|ig\\) "
247;; woman-decode-region nil nil
1edbbf8a
EZ
248;; (lambda (arg)
249;; set-visited-file-name
250;; (file-name-sans-extension buffer-file-name)))))
077decde
EZ
251;; format-alist))
252
253
254;; Reporting Bugs
255;; ==============
256
257;; If WoMan fails completely, or formats a file incorrectly
258;; (i.e. obviously wrongly or significantly differently from man) or
259;; inelegantly, then please
260
261;; (a) check that you are running the latest version of woman.el
262;; available from my web site (see above),
263
264;; (b) check that the problem is not already described in the file
265;; woman.status, also available from my web site.
266
267;; If both of the above are true then please email me the entry from
268;; the *WoMan-Log* buffer relating to the problem file, together with
269;; a brief description of the problem. Please indicate where you got
270;; the source file from, but do not send it to me unless I ask you to!
271;; Thanks. (There is at present no automated bug-reporting facility
272;; for WoMan.)
273
274;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
275
276;; NOTE:
277
278;; CASE-DEPENDENCE OF FILENAMES. By default, WoMan ignores case in
279;; file pathnames only when it seems appropriate. MS-Windows users
280;; who want complete case independence should set the NTEmacs variable
281;; `w32-downcase-file-names' to `t' and use all lower case when
282;; setting WoMan file paths.
283
284;; (1) INCOMPATIBLE CHANGE! WoMan no longer uses a persistent topic
285;; cache by default. (It caused too much confusion!) Explicitly set
286;; the variable `woman-cache-filename' to save the cache between Emacs
287;; sessions. This is recommended only if the command `woman' is too
288;; slow the first time that it is run in an Emacs session, while it
289;; builds its cache in main memory, which MAY be VERY slow.
290
291;; (2) The user option `woman-cache-level' controls the amount of
292;; information cached (in main memory and, optionally, saved to disc).
293
294;; (3) UPDATING THE CACHE. A prefix argument always causes the
295;; `woman' command (only) to rebuild its topic cache, and to re-save
296;; it to `woman-cache-filename' if this variable has a non-nil value.
297;; This is necessary if the NAMES (not contents) of any of the
298;; directories or files in the paths specified by `woman-manpath' or
299;; `woman-path' change. If WoMan user options that affect the cache
300;; are changed then WoMan will automatically update its cache file on
301;; disc (if one is in use) the next time it is run in a new Emacs
302;; session.
303
304;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
305
306
307;; TO DO
308;; =====
309
1edbbf8a 310;; Reconsider case sensitivity of file names.
077decde
EZ
311;; MUST PROCESS .if, .nr IN ORDER ENCOUNTERED IN FILE! (rcsfile, mf).
312;; Allow general delimiter in `\v', cf. `\h'.
313;; Improve major-mode documentation.
314;; Pre-process conditionals in macro bodies if possible for speed?
2d4c3c85
JB
315;; Emulate more complete preprocessor support for tbl (.TS/.TE)
316;; Emulate some preprocessor support for eqn (.EQ/.EN)
077decde
EZ
317;; Re-write filling and adjusting code!
318;; Allow word wrap at comma (for long option lists)?
319;; Buffer list handling not quite right.
320;; Make 10 or 12 pitch (cpi) optional -- 12 => ll = 78
321;; Use unpaddable space for tabbing?
322;; Tidy up handling of fonts when filling and adjusting
323;; -- see text/text properties?
324;; Improve speed
325;; Add font-lock support (for quoted strings, etc.)?
326;; Optionally save large files in enriched format?
327;; Add apropos facility by searching NAME (?) entry in man files?
328;; Documentation -- optional auto-display of formatted WoMan man page?
329;; Implement a bug reporter?
330;; Support diversion and traps (to some extent) - for Tcl/tk pages?
331;; Add a menu of WoMan buffers?
1edbbf8a 332;; Fix .fc properly?
077decde
EZ
333
334
335;; Implementation strategy [this description is now well out of date!]
336;; -- three main passes, each to process respectively:
337
338;; 1) non-breaking `.' requests including font macros
339;; 2) \ escape sequences, mainly special characters and font changes
340;; 3) breaking `.' requests, mainly filling and justification
341
342;; For each pass, a control function finds and pre-processes the
343;; escape or request and then calls the appropriate function to
344;; perform the required formatting. Based originally on enriched.el
345;; and format.el.
346
1edbbf8a
EZ
347;; The background information that made this project possible is
348;; freely available courtesy of Bell Labs from
349;; http://cm.bell-labs.com/7thEdMan/
077decde
EZ
350
351
09ae5da1
PE
352;; Acknowledgments
353;; ===============
077decde
EZ
354
355;; For Heather, Kathryn and Madelyn, the women in my life
356;; (although they will probably never use it)!
357
358;; I also thank the following for helpful suggestions, bug reports,
359;; code fragments, general interest, etc.:
360;; Jari Aalto <jari.aalto@cs.tpu.fi>
361;; Dean Andrews <dean@dra.com>
2b54c197 362;; Juanma Barranquero <lekktu@gmail.com>
077decde
EZ
363;; Karl Berry <kb@cs.umb.edu>
364;; Jim Chapman <jchapman@netcomuk.co.uk>
2d4c3c85 365;; Kin Cho <kin@neoscale.com>
077decde
EZ
366;; Frederic Corne <frederic.corne@erli.fr>
367;; Peter Craft <craft@alacritech.com>
368;; Charles Curley <ccurley@trib.com>
369;; Jim Davidson <jdavidso@teknowledge.com>
370;; Kevin D'Elia <Kevin.DElia@mci.com>
371;; John Fitch <jpff@maths.bath.ac.uk>
372;; Hans Frosch <jwfrosch@rish.b17c.ingr.com>
373;; Guy Gascoigne-Piggford <ggp@informix.com>
374;; Brian Gorka <gorkab@sanchez.com>
375;; Nicolai Henriksen <nhe@lyngso-industri.dk>
376;; Thomas Herchenroeder <the@software-ag.de>
377;; Alexander Hinds <ahinds@thegrid.net>
378;; Stefan Hornburg <sth@hacon.de>
379;; Theodore Jump <tjump@cais.com>
d0923e43 380;; David Kastrup <dak@gnu.org>
077decde
EZ
381;; Paul Kinnucan <paulk@mathworks.com>
382;; Jonas Linde <jonas@init.se>
383;; Andrew McRae <andrewm@optimation.co.nz>
384;; Howard Melman <howard@silverstream.com>
385;; Dennis Pixton <dennis@math.binghamton.edu>
386;; T. V. Raman <raman@Adobe.COM>
387;; Bruce Ravel <bruce.ravel@nist.gov>
388;; Benjamin Riefenstahl <benny@crocodial.de>
389;; Kevin Ruland <kruland@seistl.com>
390;; Tom Schutter <tom@platte.com>
391;; Wei-Xue Shi <wxshi@ma.neweb.ne.jp>
392;; Fabio Somenzi <fabio@joplin.colorado.edu>
393;; Karel Sprenger <ks@ic.uva.nl>
394;; Chris Szurgot <szurgot@itribe.net>
395;; Paul A. Thompson <pat@po.cwru.edu>
1edbbf8a 396;; Arrigo Triulzi <arrigo@maths.qmw.ac.uk>
077decde 397;; Geoff Voelker <voelker@cs.washington.edu>
f64b6c63 398;; Eli Zaretskii <eliz@gnu.org>
077decde 399
077decde
EZ
400\f
401;;; Code:
402
2d4c3c85
JB
403(defvar woman-version "0.551 (beta)" "WoMan version information.")
404
077decde 405(require 'man)
50071f01 406(require 'button)
2b54c197 407(define-button-type 'WoMan-xref-man-page
50071f01 408 :supertype 'Man-abstract-xref-man-page
707f55b0
DN
409 'func (lambda (arg)
410 (woman
411 ;; `woman' cannot deal with arguments that contain a
412 ;; section name, like close(2), so strip the section name.
413 (if (string-match Man-reference-regexp arg)
2b54c197 414 (substring arg 0 (match-end 1))
707f55b0 415 arg))))
50071f01 416
077decde
EZ
417(eval-when-compile ; to avoid compiler warnings
418 (require 'dired)
a464a6c7 419 (require 'cl-lib)
077decde
EZ
420 (require 'apropos))
421
1edbbf8a 422(defun woman-mapcan (fn x)
f471ea57 423 "Return concatenated list of FN applied to successive `car' elements of X.
1edbbf8a
EZ
424FN must return a list, cons or nil. Useful for splicing into a list."
425 ;; Based on the Standard Lisp function MAPCAN but with args swapped!
d0923e43
DK
426 ;; More concise implementation than the recursive one. -- dak
427 (apply #'nconc (mapcar fn x)))
1edbbf8a 428
2d4c3c85
JB
429(defun woman-parse-colon-path (paths)
430 "Explode search path string PATHS into a list of directory names.
431Allow Cygwin colon-separated search paths on Microsoft platforms.
f471ea57 432Replace null components by calling `woman-parse-man.conf'.
2d4c3c85
JB
433As a special case, if PATHS is nil then replace it by calling
434`woman-parse-man.conf'."
f471ea57 435 ;; Based on suggestions by Jari Aalto and Eli Zaretskii.
2d4c3c85
JB
436 ;; parse-colon-path returns nil for a null path component and
437 ;; an empty substring of MANPATH denotes the default list.
438 (if (memq system-type '(windows-nt ms-dos))
439 (cond ((null paths)
440 (mapcar 'woman-Cyg-to-Win (woman-parse-man.conf)))
441 ((string-match ";" paths)
442 ;; Assume DOS-style path-list...
443 (woman-mapcan ; splice list into list
444 (lambda (x)
445 (if x
446 (list x)
447 (mapcar 'woman-Cyg-to-Win (woman-parse-man.conf))))
448 (parse-colon-path paths)))
449 ((string-match "\\`[a-zA-Z]:" paths)
450 ;; Assume single DOS-style path...
00460cfd 451 (list paths))
2d4c3c85
JB
452 (t
453 ;; Assume UNIX/Cygwin-style path-list...
454 (woman-mapcan ; splice list into list
455 (lambda (x)
456 (mapcar 'woman-Cyg-to-Win
457 (if x (list x) (woman-parse-man.conf))))
458 (let ((path-separator ":"))
459 (parse-colon-path paths)))))
460 ;; Assume host-default-style path-list...
461 (woman-mapcan ; splice list into list
462 (lambda (x) (if x (list x) (woman-parse-man.conf)))
463 (parse-colon-path (or paths "")))))
464
465(defun woman-Cyg-to-Win (file)
466 "Convert an absolute filename FILE from Cygwin to Windows form."
ba7529b6
DK
467 ;; MANPATH_MAP conses are not converted since they presumably map
468 ;; Cygwin to Cygwin form.
469 (if (consp file)
470 file
471 ;; Code taken from w32-symlinks.el
472 (if (eq (aref file 0) ?/)
473 ;; Try to use Cygwin mount table via `cygpath.exe'.
474 (condition-case nil
475 (with-temp-buffer
476 ;; cygpath -m file
477 (call-process "cygpath" nil t nil "-m" file)
478 (buffer-substring 1 (buffer-size)))
479 (error
480 ;; Assume no `cygpath' program available.
481 ;; Hack /cygdrive/x/ or /x/ or (obsolete) //x/ to x:/
482 (when (string-match "\\`\\(/cygdrive\\|/\\)?/./" file)
b616a212 483 (if (match-beginning 1) ; /cygdrive/x/ or //x/ -> /x/
ba7529b6
DK
484 (setq file (substring file (match-end 1))))
485 (aset file 0 (aref file 1)) ; /x/ -> xx/
486 (aset file 1 ?:)) ; xx/ -> x:/
487 file))
488 file)))
077decde
EZ
489
490\f
491;;; User options:
492
493;; NB: Group identifiers must be lowercase!
494
495(defgroup woman nil
496 "Browse UNIX manual pages `wo (without) man'."
497 :tag "WoMan"
498 :group 'help)
499
500(defcustom woman-show-log nil
86970dbd 501 "If non-nil then show the *WoMan-Log* buffer if appropriate.
077decde
EZ
502I.e. if any warning messages are written to it. Default is nil."
503 :type 'boolean
504 :group 'woman)
505
506(defcustom woman-pre-format-hook nil
86970dbd 507 "Hook run by WoMan immediately before formatting a buffer.
077decde
EZ
508Change only via `Customization' or the function `add-hook'."
509 :type 'hook
510 :group 'woman)
511
512(defcustom woman-post-format-hook nil
86970dbd 513 "Hook run by WoMan immediately after formatting a buffer.
077decde
EZ
514Change only via `Customization' or the function `add-hook'."
515 :type 'hook
516 :group 'woman)
517
518\f
519;; Interface options
520
521(defgroup woman-interface nil
522 "Interface options for browsing UNIX manual pages `wo (without) man'."
523 :tag "WoMan Interface"
524 :group 'woman)
525
1edbbf8a 526(defcustom woman-man.conf-path
2d4c3c85 527 (let ((path '("/usr/lib" "/etc")))
edfda783
AR
528 (cond ((eq system-type 'windows-nt)
529 (mapcar 'woman-Cyg-to-Win path))
530 ((eq system-type 'darwin)
531 (cons "/usr/share/misc" path))
532 (t path)))
86970dbd 533 "List of dirs to search and/or files to try for man config file.
ba7529b6
DK
534A trailing separator (`/' for UNIX etc.) on directories is
535optional, and the filename is used if a directory specified is
536the first to start with \"man\" and has an extension starting
537with \".conf\". If MANPATH is not set but a config file is found
538then it is parsed instead to provide a default value for
539`woman-manpath'."
1edbbf8a
EZ
540 :type '(repeat string)
541 :group 'woman-interface)
542
543(defun woman-parse-man.conf ()
2d4c3c85 544 "Parse if possible configuration file for man command.
f471ea57 545Used only if MANPATH is not set or contains null components.
1edbbf8a
EZ
546Look in `woman-man.conf-path' and return a value for `woman-manpath'.
547Concatenate data from all lines in the config file of the form
2d4c3c85 548 MANPATH /usr/man
02df8750 549or
372713e6
EZ
550 MANDATORY_MANPATH /usr/man
551or
ba7529b6
DK
552 OPTIONAL_MANPATH /usr/man
553or
554 MANPATH_MAP /opt/bin /opt/man"
1edbbf8a
EZ
555 ;; Functionality suggested by Charles Curley.
556 (let ((path woman-man.conf-path)
557 file manpath)
558 (while (and
559 path
560 (not (and
561 (file-readable-p (setq file (car path)))
562 ;; If not a file then find the file:
563 (or (not (file-directory-p file))
564 (and
565 (setq file
ba7529b6 566 (directory-files file t "\\`man.*\\.conf[a-z]*\\'" t))
1edbbf8a
EZ
567 (file-readable-p (setq file (car file)))))
568 ;; Parse the file -- if no MANPATH data ignore it:
569 (with-temp-buffer
570 (insert-file-contents file)
571 (while (re-search-forward
2d4c3c85
JB
572 ;; `\(?: ... \)' is a "shy group"
573 "\
ba7529b6
DK
574^[ \t]*\\(?:\\(?:MANDATORY_\\|OPTIONAL_\\)?MANPATH[ \t]+\\(\\S-+\\)\\|\
575MANPATH_MAP[ \t]+\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\)" nil t)
576 (add-to-list 'manpath
577 (if (match-beginning 1)
1909cca0 578 (match-string 1)
ba7529b6
DK
579 (cons (match-string 2)
580 (match-string 3)))))
1edbbf8a
EZ
581 manpath))
582 ))
583 (setq path (cdr path)))
584 (nreverse manpath)))
585
a7e65ac4
GM
586;; Autoload so set-locale-environment can operate on it.
587;;;###autoload
588(defcustom woman-locale nil
589 "String specifying a manual page locale, or nil.
590If a manual page is available in the specified locale
591\(e.g. \"sv_SE.ISO8859-1\"), it will be offered in preference to the
592default version. Normally, `set-locale-environment' sets this at startup."
593 :type '(choice string (const nil))
594 :group 'woman-interface
595 :version "23.1")
596
597;; FIXME Is this a sensible list of alternatives?
598(defun woman-expand-locale (locale)
599 "Expand a locale into a list suitable for man page lookup.
600Expands a locale of the form LANGUAGE_TERRITORY.CHARSET into the list:
601LANGUAGE_TERRITORY.CHARSET LANGUAGE_TERRITORY LANGUAGE.CHARSET LANGUAGE.
602The TERRITORY and CHARSET portions may be absent."
603 (string-match "\\([^._]*\\)\\(_[^.]*\\)?\\(\\..*\\)?" locale)
604 (let ((lang (match-string 1 locale))
605 (terr (match-string 2 locale))
606 (charset (match-string 3 locale)))
607 (delq nil (list locale
608 (and charset terr (concat lang terr))
609 (and charset terr (concat lang charset))
610 (if (or charset terr) lang)))))
611
612(defun woman-manpath-add-locales (manpath)
613 "Add locale-specific subdirectories to the elements of MANPATH.
614MANPATH is a list of the form of `woman-manpath'. Returns a list
615with those locale-specific subdirectories specified by the action
616of `woman-expand-locale' on `woman-locale' added, where they exist."
617 (if (zerop (length woman-locale))
618 manpath
619 (let ((subdirs (woman-expand-locale woman-locale))
620 lst dir)
621 (dolist (elem manpath (nreverse lst))
622 (dolist (sub subdirs)
623 (when (file-directory-p
624 (setq dir
625 ;; Use f-n-a-d because parse-colon-path does.
626 (file-name-as-directory
627 (expand-file-name sub (substitute-in-file-name
628 (if (consp elem)
629 (cdr elem)
630 elem))))))
631 (add-to-list 'lst (if (consp elem)
632 (cons (car elem) dir)
633 dir))))
634 ;; Non-locale-specific has lowest precedence.
635 (add-to-list 'lst elem)))))
636
077decde 637(defcustom woman-manpath
a7e65ac4
GM
638 ;; Locales could also be added in woman-expand-directory-path.
639 (or (woman-manpath-add-locales
640 (woman-parse-colon-path (getenv "MANPATH")))
2d4c3c85 641 '("/usr/man" "/usr/share/man" "/usr/local/man"))
86970dbd 642 "List of DIRECTORY TREES to search for UN*X manual files.
077decde
EZ
643Each element should be the name of a directory that contains
644subdirectories of the form `man?', or more precisely subdirectories
645selected by the value of `woman-manpath-man-regexp'. Non-directory
1edbbf8a
EZ
646and unreadable files are ignored.
647
ba7529b6
DK
648Elements can also be a cons cell indicating a mapping from PATH
649to manual trees: if such an element's car is equal to a path
650element of the environment variable PATH, the cdr of the cons
651cell is included in the directory tree search.
652
1edbbf8a
EZ
653If not set then the environment variable MANPATH is used. If no such
654environment variable is found, the default list is determined by
2d4c3c85
JB
655consulting the man configuration file if found, which is determined by
656the user option `woman-man.conf-path'. An empty substring of MANPATH
657denotes the default list.
077decde 658
1edbbf8a 659Any environment variables (names must have the UN*X-style form $NAME,
2d4c3c85 660e.g. $HOME, $EMACSDATA, $emacs_dir) are evaluated first but each
077decde
EZ
661element must evaluate to a SINGLE directory name. Trailing `/'s are
662ignored. (Specific directories in `woman-path' are also searched.)
663
664Microsoft platforms:
665I recommend including drive letters explicitly, e.g.
666
2d4c3c85 667 (\"C:/Cygwin/usr/man/\" \"C:/Cygwin/usr/local/man\").
077decde
EZ
668
669The MANPATH environment variable may be set using DOS semi-colon-
2d4c3c85 670separated or UN*X/Cygwin colon-separated syntax (but not mixed)."
ba7529b6 671 :type '(repeat (choice string (cons string string)))
a7e65ac4 672 :version "23.1" ; added woman-manpath-add-locales
077decde
EZ
673 :group 'woman-interface)
674
675(defcustom woman-manpath-man-regexp "[Mm][Aa][Nn]"
676 "Regexp to match man directories UNDER `woman-manpath' directories.
677These normally have names of the form `man?'. Its default value is
678\"[Mm][Aa][Nn]\", which is case-insensitive mainly for the benefit of
679Microsoft platforms. Its purpose is to avoid `cat?', `.', `..', etc."
680 ;; Based on a suggestion by Wei-Xue Shi.
681 :type 'string
682 :group 'woman-interface)
683
684(defcustom woman-path
685 (if (eq system-type 'ms-dos) '("$DJDIR/info" "$DJDIR/man/cat[1-9onlp]"))
86970dbd 686 "List of SPECIFIC DIRECTORIES to search for UN*X manual files.
077decde
EZ
687For example
688
689 (\"/emacs/etc\").
690
691These directories are searched in addition to the directory trees
692specified in `woman-manpath'. Each element should be a directory
693string or nil, which represents the current directory when the path is
694expanded and cached. However, the last component (only) of each
695directory string is treated as a regexp \(Emacs, not shell) and the
696string is expanded into a list of matching directories. Non-directory
697and unreadable files are ignored. The default value is nil.
698
699Any environment variables (which must have the UN*X-style form $NAME,
2d4c3c85 700e.g. $HOME, $EMACSDATA, $emacs_dir) are evaluated first but each
077decde
EZ
701element must evaluate to a SINGLE directory name (regexp, see above).
702For example
703
2d4c3c85 704 (\"$EMACSDATA\") [or equivalently (\"$emacs_dir/etc\")].
077decde
EZ
705
706Trailing `/'s are discarded. (The directory trees in `woman-manpath'
707are also searched.) On Microsoft platforms I recommend including
708drive letters explicitly."
709 :type '(repeat (choice string (const nil)))
710 :group 'woman-interface)
711
712(defcustom woman-cache-level 2
86970dbd 713 "The level of topic caching.
077decde
EZ
7141 - cache only the topic and directory lists
715 (the only level before version 0.34 - only for compatibility);
7162 - cache also the directories for each topic
717 (faster, without using much more memory);
7183 - cache also the actual filenames for each topic
719 (fastest, but uses twice as much memory).
720The default value is currently 2, a good general compromise.
721If the `woman' command is slow to find files then try 3, which may be
722particularly beneficial with large remote-mounted man directories.
723Run the `woman' command with a prefix argument or delete the cache
724file `woman-cache-filename' for a change to take effect.
725\(Values < 1 behave like 1; values > 3 behave like 3.)"
726 :type '(choice (const :tag "Minimal" 1)
727 (const :tag "Default" 2)
728 (const :tag "Maximal" 3))
729 :group 'woman-interface)
730
731(defcustom woman-cache-filename nil
86970dbd 732 "The full pathname of the WoMan directory and topic cache file.
077decde
EZ
733It is used to save and restore the cache between sessions. This is
734especially useful with remote-mounted man page files! The default
735value of nil suppresses this action. The `standard' non-nil
736filename is \"~/.wmncach.el\". Remember that a prefix argument forces
737the `woman' command to update and re-write the cache."
738 :type '(choice (const :tag "None" nil)
739 (const :tag "~/.wmncach.el" "~/.wmncach.el")
740 file)
741 :group 'woman-interface)
742
743(defcustom woman-dired-keys t
86970dbd 744 "List of `dired' mode keys to define to run WoMan on current file.
077decde
EZ
745E.g. '(\"w\" \"W\"), or any non-null atom to automatically define
746\"w\" and \"W\" if they are unbound, or nil to do nothing.
747Default is t."
748 :type '(choice (const :tag "None" nil)
749 (repeat string)
750 (other :tag "Auto" t))
751 :group 'woman-interface)
752
753(defcustom woman-imenu-generic-expression
754 '((nil "\n\\([A-Z].*\\)" 1) ; SECTION, but not TITLE
755 ("*Subsections*" "^ \\([A-Z].*\\)" 1))
86970dbd 756 "Imenu support for Sections and Subsections.
077decde
EZ
757An alist with elements of the form (MENU-TITLE REGEXP INDEX) --
758see the documentation for `imenu-generic-expression'."
759 :type 'sexp
760 :group 'woman-interface)
761
762(defcustom woman-imenu nil
86970dbd 763 "If non-nil then WoMan adds a Contents menu to the menubar.
1edbbf8a 764It does this by calling `imenu-add-to-menubar'. Default is nil."
077decde
EZ
765 :type 'boolean
766 :group 'woman-interface)
767
768(defcustom woman-imenu-title "CONTENTS"
86970dbd 769 "The title to use if WoMan adds a Contents menu to the menubar.
077decde
EZ
770Default is \"CONTENTS\"."
771 :type 'string
772 :group 'woman-interface)
773
72f0712b
EZ
774(defcustom woman-use-topic-at-point-default nil
775 ;; `woman-use-topic-at-point' may be let-bound when woman is loaded,
776 ;; in which case its global value does not get defined.
077decde 777 ;; `woman-file-name' sets it to this value if it is unbound.
86970dbd 778 "Default value for `woman-use-topic-at-point'."
077decde 779 :type '(choice (const :tag "Yes" t)
72f0712b 780 (const :tag "No" nil))
077decde
EZ
781 :group 'woman-interface)
782
72f0712b 783(defcustom woman-use-topic-at-point woman-use-topic-at-point-default
86970dbd 784 "Control use of the word at point as the default topic.
72f0712b
EZ
785If non-nil the `woman' command uses the word at point automatically,
786without interactive confirmation, if it exists as a topic."
077decde 787 :type '(choice (const :tag "Yes" t)
72f0712b 788 (const :tag "No" nil))
077decde
EZ
789 :group 'woman-interface)
790
791(defvar woman-file-regexp nil
792 "Regexp used to select (possibly compressed) man source files, e.g.
4c964351 793\"\\.\\([0-9lmnt]\\w*\\)\\(\\.\\(g?z\\|bz2\\|xz\\)\\)?\\'\".
077decde
EZ
794Built automatically from the customizable user options
795`woman-uncompressed-file-regexp' and `woman-file-compression-regexp'.")
796
797(defvar woman-uncompressed-file-regexp) ; for the compiler
798(defvar woman-file-compression-regexp) ; for the compiler
799
800(defun set-woman-file-regexp (symbol value)
801 "Bind SYMBOL to VALUE and set `woman-file-regexp' as per user customizations.
1edbbf8a 802Used as :set cookie by Customize when customizing the user options
077decde
EZ
803`woman-uncompressed-file-regexp' and `woman-file-compression-regexp'."
804 (set-default symbol value)
805 (and (boundp 'woman-uncompressed-file-regexp)
806 (boundp 'woman-file-compression-regexp)
807 (setq woman-file-regexp
808 (concat woman-uncompressed-file-regexp
809 "\\("
810 (substring woman-file-compression-regexp 0 -2)
811 "\\)?\\'"))))
812
813(defcustom woman-uncompressed-file-regexp
814 "\\.\\([0-9lmnt]\\w*\\)" ; disallow no extension
86970dbd 815 "Do not change this unless you are sure you know what you are doing!
077decde
EZ
816Regexp used to select man source files (ignoring any compression extension).
817
818The SysV standard man pages use two character suffixes, and this is
819becoming more common in the GNU world. For example, the man pages
820in the ncurses package include `toe.1m', `form.3x', etc.
821
822Note: an optional compression regexp will be appended, so this regexp
823MUST NOT end with any kind of string terminator such as $ or \\'."
824 :type 'regexp
825 :set 'set-woman-file-regexp
826 :group 'woman-interface)
827
828(defcustom woman-file-compression-regexp
4c964351 829 "\\.\\(g?z\\|bz2\\|xz\\)\\'"
86970dbd 830 "Do not change this unless you are sure you know what you are doing!
077decde 831Regexp used to match compressed man file extensions for which
1edbbf8a 832decompressors are available and handled by auto-compression mode,
4c964351 833e.g. \"\\\\.\\\\(g?z\\\\|bz2\\\\|xz\\\\)\\\\'\" for `gzip', `bzip2', or `xz'.
077decde 834Should begin with \\. and end with \\' and MUST NOT be optional."
1edbbf8a
EZ
835 ;; Should be compatible with car of
836 ;; `jka-compr-file-name-handler-entry', but that is unduly
837 ;; complicated, includes an inappropriate extension (.tgz) and is
838 ;; not loaded by default!
4c964351 839 :version "24.1" ; added xz
077decde
EZ
840 :type 'regexp
841 :set 'set-woman-file-regexp
842 :group 'woman-interface)
843
25c6f63e 844(defcustom woman-use-own-frame nil
86970dbd 845 "If non-nil then use a dedicated frame for displaying WoMan windows.
1edbbf8a
EZ
846Only useful when run on a graphic display such as X or MS-Windows."
847 :type 'boolean
848 :group 'woman-interface)
849
077decde
EZ
850\f
851;; Formatting options
852
853(defgroup woman-formatting nil
854 "Formatting options for browsing UNIX manual pages `wo (without) man'."
855 :tag "WoMan Formatting"
856 :group 'woman)
857
858(defcustom woman-fill-column 65
86970dbd 859 "Right margin for formatted text -- default is 65."
077decde
EZ
860 :type 'integer
861 :group 'woman-formatting)
862
863(defcustom woman-fill-frame nil
864 ;; Based loosely on a suggestion by Theodore Jump:
86970dbd 865 "If non-nil then most of the window width is used."
077decde
EZ
866 :type 'boolean
867 :group 'woman-formatting)
868
869(defcustom woman-default-indent 5
86970dbd 870 "Default prevailing indent set by -man macros -- default is 5.
2d4c3c85 871Set this variable to 7 to emulate GNU man formatting."
077decde
EZ
872 :type 'integer
873 :group 'woman-formatting)
874
875(defcustom woman-bold-headings t
86970dbd 876 "If non-nil then embolden section and subsection headings. Default is t.
c35dcd7e 877Heading emboldening is NOT standard `man' behavior."
077decde
EZ
878 :type 'boolean
879 :group 'woman-formatting)
880
881(defcustom woman-ignore t
fd9c6792 882 "If non-nil then unrecognized requests etc. are ignored. Default is t.
c35dcd7e 883This gives the standard ?roff behavior. If nil then they are left in
077decde
EZ
884the buffer, which may aid debugging."
885 :type 'boolean
886 :group 'woman-formatting)
887
1239851c 888(defcustom woman-preserve-ascii t
86970dbd 889 "If non-nil, preserve ASCII characters in the WoMan buffer.
1239851c
EZ
890Otherwise, to save time, some backslashes and spaces may be
891represented differently (as the values of the variables
892`woman-escaped-escape-char' and `woman-unpadded-space-char'
893respectively) so that the buffer content is strictly wrong even though
894it should display correctly. This should be irrelevant unless the
895buffer text is searched, copied or saved to a file."
896 ;; This option should probably be removed!
077decde
EZ
897 :type 'boolean
898 :group 'woman-formatting)
899
1edbbf8a 900(defcustom woman-emulation 'nroff
86970dbd 901 "WoMan emulation, currently either nroff or troff. Default is nroff.
1edbbf8a
EZ
902Troff emulation is experimental and largely untested.
903\(Add groff later?)"
904 :type '(choice (const nroff) (const troff))
905 :group 'woman-formatting)
906
077decde
EZ
907\f
908;; Faces:
909
910(defgroup woman-faces nil
911 "Face options for browsing UNIX manual pages `wo (without) man'."
912 :tag "WoMan Faces"
913 :group 'woman
914 :group 'faces)
915
916(defcustom woman-fontify
917 (or (and (fboundp 'display-color-p) (display-color-p))
918 (and (fboundp 'display-graphic-p) (display-graphic-p))
919 (x-display-color-p))
86970dbd 920 "If non-nil then WoMan assumes that face support is available.
077decde
EZ
921It defaults to a non-nil value if the display supports either colors
922or different fonts."
923 :type 'boolean
924 :group 'woman-faces)
925
b66e8575 926(defface woman-italic
25c6f63e 927 '((t :inherit italic))
e01cd227 928 "Face for italic font in man pages."
077decde 929 :group 'woman-faces)
c4f6e489 930(define-obsolete-face-alias 'woman-italic-face 'woman-italic "22.1")
077decde 931
b66e8575 932(defface woman-bold
25c6f63e 933 '((t :inherit bold))
e01cd227 934 "Face for bold font in man pages."
077decde 935 :group 'woman-faces)
c4f6e489 936(define-obsolete-face-alias 'woman-bold-face 'woman-bold "22.1")
077decde 937
b66e8575 938(defface woman-unknown
25c6f63e 939 '((t :inherit font-lock-warning-face))
e01cd227 940 "Face for all unknown fonts in man pages."
077decde 941 :group 'woman-faces)
c4f6e489 942(define-obsolete-face-alias 'woman-unknown-face 'woman-unknown "22.1")
077decde 943
b66e8575 944(defface woman-addition
25c6f63e 945 '((t :inherit font-lock-builtin-face))
2d4c3c85 946 "Face for all WoMan additions to man pages."
077decde 947 :group 'woman-faces)
c4f6e489 948(define-obsolete-face-alias 'woman-addition-face 'woman-addition "22.1")
077decde 949
e01cd227 950(defun woman-default-faces ()
c35dcd7e 951 "Set foreground colors of italic and bold faces to their default values."
433212bf 952 (declare (obsolete choose-completion-guess-base-position "23.2"))
077decde 953 (interactive)
b66e8575
MB
954 (face-spec-set 'woman-italic (face-user-default-spec 'woman-italic))
955 (face-spec-set 'woman-bold (face-user-default-spec 'woman-bold)))
077decde 956
e01cd227 957(defun woman-monochrome-faces ()
c35dcd7e 958 "Set foreground colors of italic and bold faces to that of the default face.
e01cd227 959This is usually either black or white."
433212bf 960 (declare (obsolete choose-completion-guess-base-position "23.2"))
077decde 961 (interactive)
b66e8575
MB
962 (set-face-foreground 'woman-italic 'unspecified)
963 (set-face-foreground 'woman-bold 'unspecified))
077decde
EZ
964
965;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1edbbf8a
EZ
966;; Experimental font support, initially only for MS-Windows.
967(defconst woman-font-support
f471ea57 968 (eq window-system 'w32) ; Support X later!
1edbbf8a
EZ
969 "If non-nil then non-ASCII characters and symbol font supported.")
970
971(defun woman-select-symbol-fonts (fonts)
972 "Select symbol fonts from a list FONTS of font name strings."
973 (let (symbol-fonts)
974 ;; With NTEmacs 20.5, the PATTERN option to `x-list-fonts' does
975 ;; not seem to work and fonts may be repeated, so ...
86970dbd
SM
976 (dolist (font fonts)
977 (and (string-match "-Symbol-" font)
978 (not (member font symbol-fonts))
979 (setq symbol-fonts (cons font symbol-fonts))))
1edbbf8a
EZ
980 symbol-fonts))
981
aa360da1
GM
982(declare-function x-list-fonts "xfaces.c"
983 (pattern &optional face frame maximum width))
984
1edbbf8a 985(when woman-font-support
b66e8575 986 (make-face 'woman-symbol)
077decde 987
1edbbf8a 988 ;; Set the symbol font only if `woman-use-symbol-font' is true, to
077decde
EZ
989 ;; avoid unnecessarily upsetting the line spacing in NTEmacs 20.5!
990
1edbbf8a 991 (defcustom woman-use-extended-font t
86970dbd 992 "If non-nil then may use non-ASCII characters from the default font."
1edbbf8a
EZ
993 :type 'boolean
994 :group 'woman-faces)
995
996 (defcustom woman-use-symbol-font nil
86970dbd 997 "If non-nil then may use the symbol font.
9198ee0e
EZ
998It is off by default, mainly because it may change the line spacing
999\(in NTEmacs 20.5)."
077decde 1000 :type 'boolean
077decde
EZ
1001 :group 'woman-faces)
1002
1003 (defconst woman-symbol-font-list
1edbbf8a
EZ
1004 (or (woman-select-symbol-fonts (x-list-fonts "*" 'default))
1005 (woman-select-symbol-fonts (x-list-fonts "*")))
1006 "Symbol font(s), preferably same size as default when WoMan was loaded.")
077decde
EZ
1007
1008 (defcustom woman-symbol-font (car woman-symbol-font-list)
86970dbd 1009 "A string describing the symbol font to use for special characters.
077decde
EZ
1010It should be compatible with, and the same size as, the default text font.
1011Under MS-Windows, the default is
1012 \"-*-Symbol-normal-r-*-*-*-*-96-96-p-*-ms-symbol\"."
1013 :type `(choice
1edbbf8a 1014 ,@(mapcar (lambda (x) (list 'const x))
077decde
EZ
1015 woman-symbol-font-list)
1016 string)
077decde
EZ
1017 :group 'woman-faces)
1018
1019 )
1020
1edbbf8a
EZ
1021;; For non windows-nt ...
1022(defvar woman-use-extended-font nil)
1023(defvar woman-use-symbol-font nil)
1024(defvar woman-symbol-font nil)
077decde
EZ
1025;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1026
1027\f
1028;;; Internal variables:
1029
1030(defconst woman-justify-list
1031 '(left right center full)
1032 "Justify styles for `fill-region-as-paragraph'.")
1033(defconst woman-adjust-left 0 ; == adjust off, noadjust
1034 "Adjustment indicator `l' -- adjust left margin only.")
1035(defconst woman-adjust-right 1
1036 "Adjustment indicator `r' -- adjust right margin only.")
1037(defconst woman-adjust-center 2
1038 "Adjustment indicator `c' -- center.")
1039(defconst woman-adjust-both 3 ; default -- adj,both
1040 "Adjustment indicator `b' or `n' -- adjust both margins.")
1041
1042(defvar woman-adjust woman-adjust-both
1043 "Current adjustment number-register value.")
1044(defvar woman-adjust-previous woman-adjust
1045 "Previous adjustment number-register value.")
1046(defvar woman-justify
1047 (nth woman-adjust woman-justify-list) ; use vector?
1048 "Current justification style for `fill-region-as-paragraph'.")
1049(defvar woman-justify-previous woman-justify
1050 "Previous justification style for `fill-region-as-paragraph'.")
1051
1052(defvar woman-left-margin woman-default-indent
1053 "Current left margin.")
1054(defvar woman-prevailing-indent woman-default-indent
1055 "Current prevailing indent.")
1056(defvar woman-interparagraph-distance 1
1057 "Interparagraph distance in lines.
1058Set by .PD; used by .SH, .SS, .TP, .LP, .PP, .P, .IP, .HP.")
1059(defvar woman-leave-blank-lines nil
1060 "Blank lines to leave as vertical space.")
1061(defconst woman-tab-width 5
1062 "Default tab width set by -man macros.")
1063(defvar woman-nofill nil
1064 "Current fill mode: nil for filling.")
1065(defvar woman-RS-left-margin nil
1066 "Left margin stack for nested use of `.RS/.RE'.")
1067(defvar woman-RS-prevailing-indent nil
1068 "Prevailing indent stack for nested use of `.RS/.RE'.")
1069(defvar woman-nospace nil
1070 "Current no-space mode: nil for normal spacing.
1071Set by `.ns' request; reset by any output or `.rs' request")
06b60517
JB
1072;; Used for message logging
1073(defvar WoMan-current-file nil) ; bound in woman-really-find-file
1074(defvar WoMan-Log-header-point-max nil)
077decde
EZ
1075
1076(defsubst woman-reset-nospace ()
1edbbf8a 1077 "Set `woman-nospace' to nil."
077decde
EZ
1078 (setq woman-nospace nil))
1079
077decde
EZ
1080(defconst woman-request-regexp "^[.'][ \t]*\\(\\S +\\) *"
1081 ;; Was "^\\.[ \t]*\\([a-z0-9]+\\) *" but cvs.1 uses a macro named
1082 ;; "`" and CGI.man uses a macro named "''"!
1083 ;; CGI.man uses ' as control character in places -- it *should*
1084 ;; suppress breaks!
1085 ;; Could end with "\\( +\\|$\\)" instead of " *"
1086 "Regexp to match a ?roff request plus trailing white space.")
1087
1088(defvar woman-imenu-done nil
1edbbf8a 1089 "Buffer-local: set to true if function `woman-imenu' has been called.")
077decde
EZ
1090(make-variable-buffer-local 'woman-imenu-done)
1091
1092;; From imenu.el -- needed when reformatting a file in its old buffer.
1093;; The latest buffer index used to update the menu bar menu.
1094(eval-when-compile
1095 (require 'imenu))
1096(make-variable-buffer-local 'imenu--last-menubar-index-alist)
1097
1098(defvar woman-buffer-alist nil
1edbbf8a
EZ
1099 "An alist representing WoMan buffers that are already decoded.
1100Each element is of the form (FILE-NAME . BUFFER-NAME).")
077decde
EZ
1101
1102(defvar woman-buffer-number 0
1103 "Ordinal number of current buffer entry in `woman-buffer-alist'.
1104The ordinal numbers start from 0.")
1105
1edbbf8a
EZ
1106(defvar woman-if-conditions-true '(?n ?e ?o)
1107 "List of one-character built-in condition names that are true.
1108Should include ?e, ?o (page even/odd) and either ?n (nroff) or ?t (troff).
1109Default is '(?n ?e ?o). Set via `woman-emulation'.")
1110
077decde
EZ
1111\f
1112;;; Specialized utility functions:
1113
1114;;; Fast deletion without saving on the kill ring (cf. simple.el):
1115
1116(defun woman-delete-line (&optional arg)
1edbbf8a 1117 "Delete rest of current line; if all blank then delete thru newline.
077decde
EZ
1118With a numeric argument ARG, delete that many lines from point.
1119Negative arguments delete lines backward."
1120 ;; This is a non-interactive version of kill-line in simple.el that
1121 ;; deletes instead of killing and assumes kill-whole-line is nil,
1122 ;; which is essential!
1123 (delete-region (point)
1124 (progn
1125 (if arg
1126 (forward-line arg)
1127 (if (eobp)
1128 (signal 'end-of-buffer nil))
1129 (if (looking-at "[ \t]*$")
1130 (forward-line 1)
1131 (end-of-line)))
1132 (point))))
1133
1134(defsubst woman-delete-whole-line ()
1135 "Delete current line from beginning including eol."
1136 (beginning-of-line)
1137 (woman-delete-line 1))
1138
1139(defsubst woman-delete-following-space ()
1140 "Delete all spaces and tabs FOLLOWING point (cf. `delete-horizontal-space')."
1141 ;; cf. delete-horizontal-space in simple.el:
1142 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
1143
1144(defsubst woman-delete-match (subexp)
1145 "Delete subexpression SUBEXP of buffer text matched by last search."
1146 (delete-region (match-beginning subexp) (match-end subexp)))
1147
1148;; delete-char does not kill by default
1149;; delete-backward-char does not kill by default
1150;; delete-horizontal-space does not kill
1151;; delete-blank-lines does not kill
1152
1153\f
1154;;; File handling:
1155
1156(defvar woman-expanded-directory-path nil
1157 "Expanded directory list cache. Resetting to nil forces update.")
1158
1159(defvar woman-topic-all-completions nil
1160 "Expanded topic alist cache. Resetting to nil forces update.")
1161
1162;;;###autoload
1163(defun woman (&optional topic re-cache)
1edbbf8a 1164 "Browse UN*X man page for TOPIC (Without using external Man program).
077decde
EZ
1165The major browsing mode used is essentially the standard Man mode.
1166Choose the filename for the man page using completion, based on the
1167topic selected from the directories specified in `woman-manpath' and
1168`woman-path'. The directory expansions and topics are cached for
1169speed, but a non-nil interactive argument forces the caches to be
1170updated (e.g. to re-interpret the current directory).
1171
1edbbf8a
EZ
1172Used non-interactively, arguments are optional: if given then TOPIC
1173should be a topic string and non-nil RE-CACHE forces re-caching."
077decde
EZ
1174 (interactive (list nil current-prefix-arg))
1175 ;; The following test is for non-interactive calls via gnudoit etc.
ce5568bf 1176 (if (or (not (stringp topic)) (string-match "\\S " topic))
077decde
EZ
1177 (let ((file-name (woman-file-name topic re-cache)))
1178 (if file-name
1179 (woman-find-file file-name)
1180 (message
1181 "WoMan Error: No matching manual files found in search path")
25c6f63e 1182 (ding)))
077decde 1183 (message "WoMan Error: No topic specified in non-interactive call")
25c6f63e 1184 (ding)))
077decde 1185
1edbbf8a 1186;; Allow WoMan to be called via the standard Help menu:
077decde
EZ
1187(define-key-after menu-bar-manuals-menu [woman]
1188 '(menu-item "Read Man Page (WoMan)..." woman
1189 :help "Man-page documentation Without Man") t)
1190
1191(defvar woman-cached-data nil
1192 "A list of cached data used to determine cache validity.
1193Set from the cache by `woman-read-directory-cache'.")
1194
1195(defun woman-cached-data ()
1196 "Generate a list of data used to determine cache validity.
1197Called both to generate and to check the cache!"
1198 ;; Must use substituted paths because values of env vars may change!
1199 (list woman-cache-level
ba7529b6
DK
1200 (let (lst path)
1201 (dolist (dir woman-manpath (nreverse lst))
1202 (when (consp dir)
1203 (unless path
1204 (setq path
1205 (split-string (getenv "PATH") path-separator t)))
1206 (setq dir (and (member (car dir) path) (cdr dir))))
1207 (when dir (add-to-list 'lst (substitute-in-file-name dir)))))
077decde
EZ
1208 (mapcar 'substitute-in-file-name woman-path)))
1209
1210(defun woman-read-directory-cache ()
1211 "Load the directory and topic cache.
1edbbf8a
EZ
1212It is loaded from the file named by the variable `woman-cache-filename'.
1213Return t if the file exists, nil otherwise."
077decde
EZ
1214 (and
1215 woman-cache-filename
1216 (load woman-cache-filename t nil t) ; file exists
1217 (equal woman-cached-data (woman-cached-data)))) ; cache valid
1218
1219(defun woman-write-directory-cache ()
1220 "Save the directory and topic cache.
1edbbf8a 1221It is saved to the file named by the variable `woman-cache-filename'."
077decde 1222 (if woman-cache-filename
86970dbd 1223 (with-current-buffer (generate-new-buffer "WoMan tmp buffer")
077decde 1224 ;; Make a temporary buffer; name starting with space "hides" it.
86970dbd 1225 (let ((standard-output (current-buffer))
077decde
EZ
1226 (backup-inhibited t))
1227 ;; (switch-to-buffer standard-output t) ; only for debugging
1228 (buffer-disable-undo standard-output)
1229 (princ
1230 ";;; WoMan directory and topic cache -- generated automatically\n")
1231 (print
1232 ;; For data validity check:
1233 `(setq woman-cached-data ',(woman-cached-data)))
1234 (print
1235 `(setq woman-expanded-directory-path
1236 ',woman-expanded-directory-path))
1237 (print
1238 `(setq woman-topic-all-completions
1239 ',woman-topic-all-completions))
1240 (write-file woman-cache-filename) ; write CURRENT buffer
1241 (kill-buffer standard-output)
1242 ))))
1243
607acb94 1244(defvaralias 'woman-topic-history 'Man-topic-history)
077decde
EZ
1245(defvar woman-file-history nil "File-name read history.")
1246
1247(defun woman-file-name (topic &optional re-cache)
1248 "Get the name of the UN*X man-page file describing a chosen TOPIC.
72f0712b
EZ
1249When `woman' is called interactively, the word at point may be
1250automatically used as the topic, if the value of the user option
1251`woman-use-topic-at-point' is non-nil. Return nil if no file can
1252be found. Optional argument RE-CACHE, if non-nil, forces the
1253cache to be re-read."
077decde 1254 ;; Handle the caching of the directory and topic lists:
25c6f63e
CY
1255 (unless (and (not re-cache)
1256 (or
1257 (and woman-expanded-directory-path woman-topic-all-completions)
1258 (woman-read-directory-cache)))
077decde
EZ
1259 (message "Building list of manual directory expansions...")
1260 (setq woman-expanded-directory-path
1261 (woman-expand-directory-path woman-manpath woman-path))
1262 (message "Building completion list of all manual topics...")
1263 (setq woman-topic-all-completions
1264 (woman-topic-all-completions woman-expanded-directory-path))
1265 (woman-write-directory-cache))
1266 ;; There is a problem in that I want to offer case-insensitive
1267 ;; completions, but to return only a case-sensitive match. This
1268 ;; does not seem to work properly by default, so I re-do the
1269 ;; completion if necessary.
06b60517 1270 (let (files)
077decde 1271 (or (stringp topic)
72f0712b
EZ
1272 (and (if (boundp 'woman-use-topic-at-point)
1273 woman-use-topic-at-point
1274 ;; Was let-bound when file loaded, so ...
1275 (setq woman-use-topic-at-point woman-use-topic-at-point-default))
1276 (setq topic (or (current-word t) "")) ; only within or adjacent to word
1277 (test-completion topic woman-topic-all-completions))
077decde 1278 (setq topic
72f0712b
EZ
1279 (let* ((word-at-point (current-word))
1280 (default
1281 (when (and word-at-point
1282 (test-completion
1283 word-at-point woman-topic-all-completions))
1284 word-at-point)))
1285 (completing-read
1286 (if default
5b76833f 1287 (format "Manual entry (default %s): " default)
72f0712b
EZ
1288 "Manual entry: ")
1289 woman-topic-all-completions nil 1
1290 nil
1291 'woman-topic-history
1292 default))))
077decde 1293 ;; Note that completing-read always returns a string.
25c6f63e 1294 (unless (= (length topic) 0)
077decde
EZ
1295 (cond
1296 ((setq files (woman-file-name-all-completions topic)))
1297 ;; Complete topic more carefully, i.e. use the completion
1298 ;; rather than the string entered by the user:
1299 ((setq files (all-completions topic woman-topic-all-completions))
1300 (while (/= (length topic) (length (car files)))
1301 (setq files (cdr files)))
1302 (setq files (woman-file-name-all-completions (car files)))))
1303 (cond
1304 ((null files) nil) ; no file found for topic.
1305 ((null (cdr files)) (car (car files))) ; only 1 file for topic.
1306 (t
1307 ;; Multiple files for topic, so must select 1.
fa470af0
SM
1308 ;; Run the command `minibuffer-complete' in order to automatically
1309 ;; complete the minibuffer contents as far as possible.
1310 (minibuffer-with-setup-hook
1311 (lambda () (let ((this-command this-command)) (minibuffer-complete)))
1312 (completing-read "Manual file: " files nil 1
1313 (try-completion "" files) 'woman-file-history)))))))
077decde
EZ
1314
1315(defun woman-select (predicate list)
1316 "Select unique elements for which PREDICATE is true in LIST.
1317\(Note that this function changes the value of LIST.)"
1318 ;; Intended to be fast by avoiding recursion and list copying.
1319 (while (and list
1320 (or
1321 (member (car list) (cdr list))
1322 (not (funcall predicate (car list)))))
1323 (setq list (cdr list)))
1324 (if list
1325 (let ((newlist list) cdr_list)
1326 (while (setq cdr_list (cdr list))
1327 (if (and
1328 (not (member (car cdr_list) (cdr cdr_list)))
1329 (funcall predicate (car cdr_list)))
1330 (setq list cdr_list)
25c6f63e 1331 (setcdr list (cdr cdr_list))))
077decde
EZ
1332 newlist)))
1333
1334(defun woman-file-readable-p (dir)
1335 "Return t if DIR is readable, otherwise log a warning."
1336 (or (file-readable-p dir)
1337 (WoMan-warn "Ignoring unreadable `manpath' directory tree `%s'!" dir)))
1338
1339(defun woman-directory-files (head dir)
1edbbf8a 1340 "Return a sorted list of files in directory HEAD matching regexp in DIR.
077decde
EZ
1341Value is a sorted list of the absolute pathnames of all the files in
1342directory HEAD, or the current directory if HEAD is nil, that match the
1343regexp that is the final component of DIR. Log a warning if list is empty."
1344 (or (directory-files
1345 (or head (directory-file-name default-directory)) ; was "."
1346 t
1347 (file-name-nondirectory dir))
1348 (WoMan-warn "No directories match `woman-path' entry `%s'!" dir)))
1349
1350(defun woman-file-accessible-directory-p (dir)
1351 "Return t if DIR is accessible, otherwise log a warning."
1352 (or (file-accessible-directory-p dir)
1353 (WoMan-warn "Ignoring inaccessible `man-page' directory `%s'!" dir)))
1354
06b60517
JB
1355(defun woman-expand-directory-path (path-dirs path-regexps)
1356 "Expand the manual directories in PATH-DIRS and PATH-REGEXPS.
1357PATH-DIRS should be a list of general manual directories (like
1358`woman-manpath'), while PATH-REGEXPS should be a list of specific
1359manual directory regexps (like `woman-path').
077decde
EZ
1360Ignore any paths that are unreadable or not directories."
1361 ;; Allow each path to be a single string or a list of strings:
06b60517
JB
1362 (if (not (listp path-dirs)) (setq path-dirs (list path-dirs)))
1363 (if (not (listp path-regexps)) (setq path-regexps (list path-regexps)))
86970dbd 1364 (let (head dirs path)
06b60517 1365 (dolist (dir path-dirs)
ba7529b6
DK
1366 (when (consp dir)
1367 (unless path
1368 (setq path (split-string (getenv "PATH") path-separator t)))
1369 (setq dir (and (member (car dir) path)
1370 (cdr dir))))
077decde
EZ
1371 (if (and dir (woman-file-readable-p dir))
1372 ;; NB: `parse-colon-path' creates null elements for
1373 ;; redundant (semi-)colons and trailing `/'s!
1374 ;; If does not actually matter here if dir ends with `/'.
1375 ;; Need regexp "man" here to avoid "cat?", `.', `..', etc.
1376 (setq dir (woman-canonicalize-dir dir)
1377 dirs (nconc dirs (directory-files
1378 dir t woman-manpath-man-regexp)))))
06b60517 1379 (dolist (dir path-regexps)
077decde
EZ
1380 (if (or (null dir)
1381 (null (setq dir (woman-canonicalize-dir dir)
1382 head (file-name-directory dir)))
1383 (woman-file-readable-p head))
1384 (setq dirs
1385 (if dir
1386 (nconc dirs (woman-directory-files head dir))
1387 (cons (directory-file-name default-directory) dirs))
1388 ;; was "." -- at head of list for later filtering
1389 )))
1390 (woman-select 'woman-file-accessible-directory-p dirs)))
1391
1392(defun woman-canonicalize-dir (dir)
1edbbf8a 1393 "Canonicalize the directory name DIR.
077decde
EZ
1394Any UN*X-style environment variables are evaluated first."
1395 (setq dir (expand-file-name (substitute-in-file-name dir)))
1396 ;; A path that ends with / matches all directories in it,
1397 ;; including `.' and `..', so remove any trailing / !!!
1398 (if (string= (substring dir -1) "/")
1399 (setq dir (substring dir 0 -1)))
c60ee5e7 1400 (if (memq system-type '(windows-nt ms-dos cygwin)) ; what else?
077decde
EZ
1401 ;; Match capitalization used by `file-name-directory':
1402 (setq dir (concat (file-name-directory dir)
1403 (file-name-nondirectory dir))))
1404 dir)
1405
1406(defsubst woman-not-member (dir path)
1edbbf8a 1407 "Return t if DIR is not a member of the list PATH, nil otherwise.
077decde
EZ
1408If DIR is `.' it is first replaced by the current directory."
1409 (not (member dir path)))
1410
1411(defun woman-topic-all-completions (path)
1412 "Return an alist of the man files in all man directories in the list PATH.
1413The cdr of each alist element is the path-index / filename."
1414 ;; Support 3 levels of caching: each element of the alist `files'
1415 ;; will be a list of the first `woman-cache-level' elements of the
1416 ;; following list: (topic path-index filename). This alist `files'
1417 ;; is re-processed by `woman-topic-all-completions-merge'.
b616a212 1418 (let (dir files (path-index 0)) ; indexing starts at zero
077decde 1419 (while path
d0923e43 1420 (setq dir (pop path))
077decde 1421 (if (woman-not-member dir path) ; use each directory only once!
d0923e43
DK
1422 (push (woman-topic-all-completions-1 dir path-index)
1423 files))
077decde 1424 (setq path-index (1+ path-index)))
8350f087 1425 ;; Uniquify topics:
fe3c5669 1426 ;; Concatenate all lists with a single nconc call to
d0923e43
DK
1427 ;; avoid retraversing the first lists repeatedly -- dak
1428 (woman-topic-all-completions-merge
1429 (apply #'nconc files))))
077decde 1430
077decde 1431(defun woman-topic-all-completions-1 (dir path-index)
f471ea57
EZ
1432 "Return an alist of the man topics in directory DIR with index PATH-INDEX.
1433A topic is a filename sans type-related extensions.
1434Support 3 levels of caching: each element of the alist will be a list
1435of the first `woman-cache-level' elements from the following list:
1436\(topic path-index filename)."
1437 ;; This function used to check that each file in the directory was
1438 ;; not itself a directory, but this is very slow and should be
1439 ;; unnecessary. So let us assume that `woman-file-regexp' will
1440 ;; filter out any directories, which probably should not be there
1441 ;; anyway, i.e. it is a user error!
d0923e43
DK
1442 ;;
1443 ;; Don't sort files: we do that when merging, anyway. -- dak
1444 (let (newlst (lst (directory-files dir nil woman-file-regexp t))
1445 ;; Make an explicit regexp for stripping extension and
1446 ;; compression extension: file-name-sans-extension is a
1447 ;; far too costly function. -- dak
1448 (ext (format "\\(\\.[^.\\/]*\\)?\\(%s\\)?\\'"
1449 woman-file-compression-regexp)))
1450 ;; Use a loop instead of mapcar in order to avoid the speed
1451 ;; penalty of binding function arguments. -- dak
1452 (dolist (file lst newlst)
1453 (push
1454 (cons
1455 (if (string-match ext file)
1456 (substring file 0 (match-beginning 0))
1457 file)
1458 (and (> woman-cache-level 1)
1459 (cons
1460 path-index
1461 (and (> woman-cache-level 2)
1462 (list file)))))
1463 newlst))))
077decde
EZ
1464
1465(defun woman-topic-all-completions-merge (alist)
1466 "Merge the alist ALIST so that the keys are unique.
1edbbf8a 1467Also make each path-info component into a list.
077decde 1468\(Note that this function changes the value of ALIST.)"
d0923e43
DK
1469 ;; Replaces unreadably "optimized" O(n^2) implementation.
1470 ;; Instead we use sorting to merge stuff efficiently. -- dak
86970dbd 1471 (let (newalist)
d0923e43
DK
1472 ;; Sort list into reverse order
1473 (setq alist (sort alist (lambda(x y) (string< (car y) (car x)))))
1474 ;; merge duplicate keys.
1475 (if (> woman-cache-level 1)
86970dbd 1476 (dolist (elt alist)
d0923e43
DK
1477 (if (equal (car elt) (caar newalist))
1478 (unless (member (cdr elt) (cdar newalist))
1479 (setcdr (car newalist) (cons (cdr elt)
1480 (cdar newalist))))
1481 (setcdr elt (list (cdr elt)))
1482 (push elt newalist)))
86970dbd
SM
1483 ;; woman-cache-level = 1 => elements are single-element lists ...
1484 (dolist (elt alist)
d0923e43
DK
1485 (unless (equal (car elt) (caar newalist))
1486 (push elt newalist))))
1487 newalist))
077decde
EZ
1488
1489(defun woman-file-name-all-completions (topic)
1490 "Return an alist of the files in all man directories that match TOPIC."
1491 ;; Support 3 levels of caching: each element of
1492 ;; woman-topic-all-completions is a list of one of the forms:
1493 ;; (topic)
1494 ;; (topic (path-index) (path-index) ... )
1495 ;; (topic (path-index filename) (path-index filename) ... )
88dbda51 1496 ;; where there are no duplicates in the value lists.
077decde
EZ
1497 ;; Topic must match first `word' of filename, so ...
1498 (let ((topic-regexp
1499 (concat
1500 "\\`" (regexp-quote topic) ; first `word'
1501 "\\(\\..+\\)*" ; optional subsequent `words'
1502 woman-file-regexp)) ; extension
1503 (topics woman-topic-all-completions)
1504 (path woman-expanded-directory-path)
1505 dir files)
1506 (if (cdr (car topics))
1507 ;; Use cached path-info to locate files for each topic:
1508 (let ((path-info (cdr (assoc topic topics)))
1509 filename)
86970dbd
SM
1510 (dolist (elt path-info)
1511 (setq dir (nth (car elt) path)
1512 filename (car (cdr elt))
077decde
EZ
1513 files (nconc files
1514 ;; Find the actual file name:
1515 (if filename
1516 (list (concat dir "/" filename))
1517 (directory-files dir t topic-regexp)
1518 )))))
1519 ;; Search path for the files for each topic:
1520 (while path
1521 (setq dir (car path)
1522 path (cdr path))
1523 (if (woman-not-member dir path) ; use each directory only once!
1524 (setq files (nconc files
25c6f63e
CY
1525 (directory-files dir t topic-regexp))))))
1526 (mapcar 'list files)))
077decde 1527
f471ea57 1528\f
077decde
EZ
1529;;; dired support
1530
1531(defun woman-dired-define-key (key)
1532 "Bind the argument KEY to the command `woman-dired-find-file'."
1533 (define-key dired-mode-map key 'woman-dired-find-file))
1534
1535(defsubst woman-dired-define-key-maybe (key)
1536 "If KEY is undefined in Dired, bind it to command `woman-dired-find-file'."
f79b331a
EZ
1537 (if (or (eq (lookup-key dired-mode-map key) 'undefined)
1538 (null (lookup-key dired-mode-map key)))
077decde
EZ
1539 (woman-dired-define-key key)))
1540
1541(defun woman-dired-define-keys ()
1542 "Define dired keys to run WoMan according to `woman-dired-keys'."
1543 (if woman-dired-keys
1544 (if (listp woman-dired-keys)
1909cca0 1545 (mapc 'woman-dired-define-key woman-dired-keys)
077decde
EZ
1546 (woman-dired-define-key-maybe "w")
1547 (woman-dired-define-key-maybe "W")))
1548 (define-key-after (lookup-key dired-mode-map [menu-bar immediate])
1549 [woman] '("Read Man Page (WoMan)" . woman-dired-find-file) 'view))
1550
1551(if (featurep 'dired)
1552 (woman-dired-define-keys)
1553 (add-hook 'dired-mode-hook 'woman-dired-define-keys))
1554
d7fe6352
JB
1555(declare-function dired-get-filename "dired"
1556 (&optional localp no-error-if-not-filep))
1557
077decde
EZ
1558;;;###autoload
1559(defun woman-dired-find-file ()
1560 "In dired, run the WoMan man-page browser on this file."
1561 (interactive)
077decde
EZ
1562 (woman-find-file (dired-get-filename)))
1563
1564
1565;;; tar-mode support
1566
06b60517
JB
1567(defvar global-font-lock-mode) ; defined in font-core.el
1568
077decde
EZ
1569(defun woman-tar-extract-file ()
1570 "In tar mode, run the WoMan man-page browser on this file."
1571 (interactive)
1572 (or (eq major-mode 'tar-mode)
1573 (error "`woman-tar-extract-file' can be used only in `tar-mode'"))
1574 (buffer-disable-undo)
1575 (let (global-font-lock-mode)
1576 (funcall (symbol-function 'tar-extract)) ; defined in tar-mode
1577 (let ((WoMan-current-file buffer-file-name)) ; used for message logging
1578 (rename-buffer
1579 (woman-make-bufname (file-name-nondirectory buffer-file-name)))
1580 (woman-process-buffer)
1581 (goto-char (point-min)))))
1582
077decde
EZ
1583(defvar woman-last-file-name nil
1584 "The full pathname of the last file formatted by WoMan.")
1585
1586(defun woman-reformat-last-file ()
1587 "Reformat last file, e.g. after changing fill column."
1588 (interactive)
1589 (if woman-last-file-name
1590 (woman-find-file woman-last-file-name t)
1591 (call-interactively 'woman-find-file)))
1592
1593;;;###autoload
1594(defun woman-find-file (file-name &optional reformat)
1595 "Find, decode and browse a specific UN*X man-page source file FILE-NAME.
1596Use existing buffer if possible; reformat only if prefix arg given.
1597When called interactively, optional argument REFORMAT forces reformatting
1edbbf8a 1598of an existing WoMan buffer formatted earlier.
077decde
EZ
1599No external programs are used, except that `gunzip' will be used to
1600decompress the file if appropriate. See the documentation for the
1601`woman' command for further details."
1602 (interactive "fBrowse UN*X manual file: \nP")
1603 (setq woman-last-file-name
1604 (setq file-name (expand-file-name file-name))) ; to canonicalize
1605 (let ((alist-tail woman-buffer-alist) exists)
1606 (setq woman-buffer-number 0)
1607 (while (and alist-tail (not (string= file-name (car (car alist-tail)))))
1608 (setq alist-tail (cdr alist-tail)
1609 woman-buffer-number (1+ woman-buffer-number)))
1610 (or (and (setq exists
1611 (and alist-tail (WoMan-find-buffer))) ; buffer exists
1612 (not reformat))
1613 ;; Format new buffer or reformat current buffer:
1614 (let* ((bufname (file-name-nondirectory file-name))
1615 (case-fold-search t)
1616 (compressed
1617 (not (not (string-match woman-file-compression-regexp bufname)))))
1618 (if compressed
1619 (setq bufname (file-name-sans-extension bufname)))
1620 (setq bufname (if exists
1621 (buffer-name)
1622 (woman-make-bufname bufname)))
1623 (woman-really-find-file file-name compressed bufname)
1624 (or exists
1625 (setq woman-buffer-alist
1626 (cons (cons file-name bufname) woman-buffer-alist)
25c6f63e 1627 woman-buffer-number 0)))))
077decde
EZ
1628 (Man-build-section-alist)
1629 (Man-build-references-alist)
1630 (goto-char (point-min)))
1631
1632(defun woman-make-bufname (bufname)
1633 "Create an unambiguous buffer name from BUFNAME."
c79fb976
GM
1634 ;; See Bug#5038. Any compression extension has already been removed.
1635 ;; Go from eg "host.conf.5" to "5 host.conf".
34b1d750 1636 (let ((dot (string-match "\\.[^.]*\\'" bufname)))
077decde
EZ
1637 (if dot (setq bufname (concat
1638 (substring bufname (1+ dot)) " "
1639 (substring bufname 0 dot))))
1640 (generate-new-buffer-name ; ensure uniqueness
1641 (concat "*WoMan " bufname "*"))))
1642
1edbbf8a
EZ
1643(defvar woman-frame nil
1644 "Dedicated frame used for displaying WoMan windows.")
1645
077decde
EZ
1646(defun woman-really-find-file (filename compressed bufname)
1647 "Find, decompress, and decode a UN*X man page FILENAME.
1edbbf8a
EZ
1648If COMPRESSED is non-nil, turn on auto-compression mode to decompress
1649the file if necessary. Set buffer name BUFNAME and major mode.
077decde
EZ
1650Do not call directly!"
1651 (let ((WoMan-current-file filename)) ; used for message logging
1edbbf8a
EZ
1652 (if woman-use-own-frame
1653 (select-frame
1654 (or (and (frame-live-p woman-frame) woman-frame)
1655 (setq woman-frame (make-frame)))))
86970dbd
SM
1656 (set-buffer (get-buffer-create bufname))
1657 (condition-case nil
1658 (switch-to-buffer (current-buffer))
1659 (error (pop-to-buffer (current-buffer))))
077decde
EZ
1660 (buffer-disable-undo)
1661 (setq buffer-read-only nil)
1662 (erase-buffer) ; NEEDED for reformat
1663 (woman-insert-file-contents filename compressed)
1664 ;; Set buffer's default directory to that of the file.
1665 (setq default-directory (file-name-directory filename))
1666 (set (make-local-variable 'backup-inhibited) t)
1667 (set-visited-file-name "")
1668 (woman-process-buffer)))
1669
1670(defun woman-process-buffer ()
1671 "The second half of `woman-really-find-file'!"
1672 (interactive)
1673 ;; Check (crudely) that this really is likely to be in UN*X
1674 ;; man-page source format, assuming we are at point-min:
1675 (goto-char (point-min))
1676 (if (re-search-forward "^[.']" 1000 t)
1677 (woman-decode-buffer)
1678 (message
1679 "File appears to be pre-formatted -- using source file may be better.")
1680 (woman-man-buffer))
1681 (woman-mode))
1682
1683(defun woman-man-buffer ()
1684 "Post-process an nroff-preformatted man buffer."
1685 ;; Kill all leading whitespace:
1686 (if (looking-at "\\s-+") (woman-delete-match 0))
1687 ;; Delete all page footer/header pairs:
1688 (re-search-forward ".*") ; match header
1689 ;; Footer conventionally has page number at right, so ...
1690 (let ((regex (concat
1691 "^.*[0-9]\n\\s-*" ; footer and following blank lines
1692 (regexp-quote (match-string 0)) ; header
1693 "\\s-*\n"))) ; following blank lines
1694 (while (re-search-forward regex nil 1) ; finish at eob
1695 (woman-delete-match 0)))
1696 ;; Delete last text line (footer) and all following blank lines:
1697 (re-search-backward "\\S-")
1698 (beginning-of-line)
1699 (if (looking-at ".*[0-9]$")
1700 (delete-region (point) (point-max)))
1701
1702 ;; Squeeze multiple blank lines:
1703 (goto-char (point-min))
1704 (while (re-search-forward "^[ \t]*\n\\([ \t]*\n\\)+" nil t)
1705 (replace-match "\n" t t))
1706
a0dd66ef
EZ
1707 ;; CJK characters are underlined by double-sized "__".
1708 ;; (Code lifted from man.el, with trivial changes.)
1709 (if (< (buffer-size) (position-bytes (point-max)))
1710 ;; Multibyte characters exist.
1711 (progn
1712 (goto-char (point-min))
1713 (while (search-forward "__\b\b" nil t)
1714 (backward-delete-char 4)
b66e8575 1715 (woman-set-face (point) (1+ (point)) 'woman-italic))
a0dd66ef
EZ
1716 (goto-char (point-min))
1717 (while (search-forward "\b\b__" nil t)
1718 (backward-delete-char 4)
b66e8575 1719 (woman-set-face (1- (point)) (point) 'woman-italic))))
a0dd66ef 1720
077decde
EZ
1721 ;; Interpret overprinting to indicate bold face:
1722 (goto-char (point-min))
a0dd66ef 1723 (while (re-search-forward "\\(.\\)\\(\\(\b+\\1\\)+\\)" nil t)
077decde 1724 (woman-delete-match 2)
b66e8575 1725 (woman-set-face (1- (point)) (point) 'woman-bold))
077decde
EZ
1726
1727 ;; Interpret underlining to indicate italic face:
1728 ;; (Must be AFTER emboldening to interpret bold _ correctly!)
1729 (goto-char (point-min))
1730 (while (search-forward "_\b" nil t)
1731 (delete-char -2)
b66e8575 1732 (woman-set-face (point) (1+ (point)) 'woman-italic))
077decde
EZ
1733
1734 ;; Leave any other uninterpreted ^H's in the buffer for now! (They
1735 ;; might indicate composite special characters, which could be
1736 ;; interpreted if I knew what to expect.)
1737
1738 ;; Optionally embolden section and subsection headings
1739 ;; (cf. `woman-imenu-generic-expression'):
1740 (cond
1741 (woman-bold-headings
1742 (goto-char (point-min))
1743 (forward-line)
1744 (while (re-search-forward "^\\( \\)?\\([A-Z].*\\)" nil t)
25c6f63e 1745 (woman-set-face (match-beginning 2) (match-end 2) 'woman-bold)))))
077decde
EZ
1746
1747(defun woman-insert-file-contents (filename compressed)
1748 "Insert file FILENAME into the current buffer.
1749If COMPRESSED is t, or is non-nil and the filename implies compression,
1edbbf8a 1750then turn on auto-compression mode to decompress the file.
077decde
EZ
1751Leave point at end of new text. Return length of inserted text."
1752 ;; Leaves point at end of inserted text in GNU Emacs 20.3, but at
1753 ;; start in 19.34!
1754 (save-excursion
1755 (let ((case-fold-search t))
1756 ;; Co-operate with auto-compression mode:
1757 (if (and compressed
1758 (or (eq compressed t)
1759 (string-match woman-file-compression-regexp filename))
1760 ;; (not auto-compression-mode)
1761 (not (rassq 'jka-compr-handler file-name-handler-alist)) )
1762 ;; (error "Compressed file requires Auto File Decompression turned on")
1763 (auto-compression-mode 1))
1764 (nth 1
1765 (condition-case ()
1766 (insert-file-contents filename nil)
1767 (file-error
1768 ;; Run find-file-not-found-hooks until one returns non-nil.
1769 ;; (run-hook-with-args-until-success 'find-file-not-found-hooks)
25c6f63e 1770 (insert "\n***** File " filename " not found! *****\n\n")))))))
077decde
EZ
1771
1772\f
1773;;; Major mode (Man) interface:
1774
70165841
SM
1775(defvar woman-mode-map
1776 (let ((map (make-sparse-keymap)))
1777 (set-keymap-parent map Man-mode-map)
1778
1779 (define-key map "R" 'woman-reformat-last-file)
1780 (define-key map "w" 'woman)
1781 (define-key map "\en" 'WoMan-next-manpage)
1782 (define-key map "\ep" 'WoMan-previous-manpage)
1783 (define-key map [M-mouse-2] 'woman-follow-word)
1784
1785 ;; We don't need to call `man' when we are in `woman-mode'.
1786 (define-key map [remap man] 'woman)
1787 (define-key map [remap man-follow] 'woman-follow)
1788 map)
1789 "Keymap for woman mode.")
6081889e
CY
1790
1791(defun woman-follow (topic)
1792 "Get a Un*x manual page of the item under point and put it in a buffer."
1793 (interactive (list (Man-default-man-entry)))
1794 (if (or (not topic)
1795 (string= topic ""))
1796 (error "No item under point")
1797 (woman (if (string-match Man-reference-regexp topic)
1798 (substring topic 0 (match-end 1))
1799 topic))))
077decde 1800
e664497b 1801(defun woman-follow-word (event)
077decde 1802 "Run WoMan with word under mouse as topic.
077decde
EZ
1803Argument EVENT is the invoking mouse event."
1804 (interactive "e") ; mouse event
e664497b 1805 (goto-char (posn-point (event-start event)))
7cff4809 1806 (woman (or (current-word t) "")))
077decde
EZ
1807
1808;; WoMan menu bar and pop-up menu:
31e1a094
MR
1809(easy-menu-define
1810 woman-menu ; (SYMBOL MAPS DOC MENU)
1811 ;; That comment was moved after the symbol `woman-menu' to make
1812 ;; find-function-search-for-symbol work. -- rost
077decde
EZ
1813 woman-mode-map
1814 "WoMan Menu"
1815 `("WoMan"
1816 ["WoMan..." woman t] ; [NAME CALLBACK ENABLE]
1817 "--"
1818 ["Next Section" Man-next-section t]
1819 ["Previous Section" Man-previous-section t]
1820 ["Goto Section..." Man-goto-section t]
1821 ["Goto See-Also Section" Man-goto-see-also-section t]
1822 ["Follow Reference..." Man-follow-manual-reference t]
1823 "--"
1824 ["Previous WoMan Buffer" WoMan-previous-manpage t]
1825 ["Next WoMan Buffer" WoMan-next-manpage t]
1826 ["Bury WoMan Buffer" Man-quit t]
1827 ["Kill WoMan Buffer" Man-kill t]
1828 "--"
1829 ;; ["Toggle Fill Frame Width" woman-toggle-fill-frame t]
1830 ["Use Full Frame Width" woman-toggle-fill-frame
1831 :active t :style toggle :selected woman-fill-frame]
1edbbf8a 1832 ["Reformat Last Man Page" woman-reformat-last-file t]
077decde
EZ
1833 ["Make Contents Menu" (woman-imenu t) (not woman-imenu-done)]
1834 "--"
1835 ["Describe (Wo)Man Mode" describe-mode t]
1836 ["Mini Help" woman-mini-help t]
1837 ,@(if (fboundp 'customize-group)
1838 '(["Customize..." (customize-group 'woman) t]))
1839 ["Show Version" (message "WoMan %s" woman-version) t]
1edbbf8a
EZ
1840 "--"
1841 ("Advanced"
1842 ["View Source" (view-file woman-last-file-name) woman-last-file-name]
1843 ["Show Log" (switch-to-buffer-other-window "*WoMan-Log*" t) t]
1844 ["Extended Font" woman-toggle-use-extended-font
1845 :included woman-font-support
1846 :active t :style toggle :selected woman-use-extended-font]
1847 ["Symbol Font" woman-toggle-use-symbol-font
1848 :included woman-font-support
1849 :active t :style toggle :selected woman-use-symbol-font]
1850 ["Font Map" woman-display-extended-fonts
1851 :included woman-font-support
1852 :active woman-use-symbol-font]
1853 "--"
1854 "Emulation"
1855 ["nroff" (woman-reset-emulation 'nroff)
1856 :active t :style radio :selected (eq woman-emulation 'nroff)]
1857 ["troff" (woman-reset-emulation 'troff)
1858 :active t :style radio :selected (eq woman-emulation 'troff)]
1859 )
077decde
EZ
1860 ))
1861
1edbbf8a
EZ
1862(defun woman-toggle-use-extended-font ()
1863 "Toggle `woman-use-extended-font' and reformat, for menu use."
1864 (interactive)
1865 (setq woman-use-extended-font (not woman-use-extended-font))
1866 (woman-reformat-last-file))
1867
1868(defun woman-toggle-use-symbol-font ()
1869 "Toggle `woman-use-symbol-font' and reformat, for menu use."
1870 (interactive)
1871 (setq woman-use-symbol-font (not woman-use-symbol-font))
1872 (woman-reformat-last-file))
1873
1874(defun woman-reset-emulation (value)
1875 "Reset `woman-emulation' to VALUE and reformat, for menu use."
1876 (interactive)
1877 (setq woman-emulation value)
1878 (woman-reformat-last-file))
1879
398a825b 1880(defvar bookmark-make-record-function)
63e9aa8e
LK
1881(put 'woman-mode 'mode-class 'special)
1882
077decde
EZ
1883(defun woman-mode ()
1884 "Turn on (most of) Man mode to browse a buffer formatted by WoMan.
1885WoMan is an ELisp emulation of much of the functionality of the Emacs
1886`man' command running the standard UN*X man and ?roff programs.
1887WoMan author: F.J.Wright@Maths.QMW.ac.uk
1888WoMan version: see `woman-version'.
1889See `Man-mode' for additional details."
1890 (let ((Man-build-page-list (symbol-function 'Man-build-page-list))
1891 (Man-strip-page-headers (symbol-function 'Man-strip-page-headers))
1892 (Man-unindent (symbol-function 'Man-unindent))
1893 (Man-goto-page (symbol-function 'Man-goto-page)))
1894 ;; Prevent inappropriate operations:
1895 (fset 'Man-build-page-list 'ignore)
1896 (fset 'Man-strip-page-headers 'ignore)
1897 (fset 'Man-unindent 'ignore)
1898 (fset 'Man-goto-page 'ignore)
1899 (unwind-protect
63e9aa8e 1900 (delay-mode-hooks (Man-mode))
077decde
EZ
1901 ;; Restore the status quo:
1902 (fset 'Man-build-page-list Man-build-page-list)
1903 (fset 'Man-strip-page-headers Man-strip-page-headers)
1904 (fset 'Man-unindent Man-unindent)
1efaf74b
EZ
1905 (fset 'Man-goto-page Man-goto-page)
1906 (setq tab-width woman-tab-width)))
63e9aa8e
LK
1907 (setq major-mode 'woman-mode
1908 mode-name "WoMan")
1909 ;; Don't show page numbers like Man-mode does. (Online documents do
1910 ;; not have pages)
1911 (kill-local-variable 'mode-line-buffer-identification)
1912 (use-local-map woman-mode-map)
1913 ;; Imenu support:
1914 (set (make-local-variable 'imenu-generic-expression)
1915 ;; `make-local-variable' in case imenu not yet loaded!
1916 woman-imenu-generic-expression)
1917 (set (make-local-variable 'imenu-space-replacement) " ")
45be326a
TV
1918 ;; Bookmark support.
1919 (set (make-local-variable 'bookmark-make-record-function)
1920 'woman-bookmark-make-record)
63e9aa8e
LK
1921 ;; For reformat ...
1922 ;; necessary when reformatting a file in its old buffer:
1923 (setq imenu--last-menubar-index-alist nil)
1924 ;; necessary to avoid re-installing the same imenu:
1925 (setq woman-imenu-done nil)
1926 (if woman-imenu (woman-imenu))
86970dbd 1927 (let ((inhibit-read-only t))
50071f01 1928 (Man-highlight-references 'WoMan-xref-man-page))
63e9aa8e
LK
1929 (set-buffer-modified-p nil)
1930 (run-mode-hooks 'woman-mode-hook))
077decde
EZ
1931
1932(defun woman-imenu (&optional redraw)
1933 "Add a \"Contents\" menu to the menubar.
1934Optional argument REDRAW, if non-nil, forces mode line to be updated."
1935 (interactive)
1936 (if woman-imenu-done
1937 ;; This is PRIMARILY to avoid a bug in imenu-add-to-menubar that
1938 ;; causes it to corrupt the menu bar if it is run more than once
1939 ;; in the same buffer.
1940 ()
1941 (setq woman-imenu-done t)
1942 (imenu-add-to-menubar woman-imenu-title)
1943 (if redraw (force-mode-line-update))))
1944
1945(defun woman-toggle-fill-frame ()
1946 "Toggle formatting to fill (most of) the width of the current frame."
1947 (interactive)
1948 (setq woman-fill-frame (not woman-fill-frame))
1949 (message "Woman fill column set to %s."
25c6f63e 1950 (if woman-fill-frame "frame width" woman-fill-column)))
077decde 1951
d7fe6352
JB
1952(declare-function apropos-print "apropos"
1953 (do-keys spacing &optional text nosubst))
1954
077decde
EZ
1955(defun woman-mini-help ()
1956 "Display WoMan commands and user options in an `apropos' buffer."
1957 ;; Based on apropos-command in apropos.el
1958 (interactive)
1959 (require 'apropos)
1960 (let ((message
1961 (let ((standard-output (get-buffer-create "*Apropos*")))
d5d105e8 1962 (help-print-return-message 'identity))))
077decde
EZ
1963 (setq apropos-accumulator
1964 (apropos-internal "woman"
1edbbf8a 1965 (lambda (symbol)
b616a212
DK
1966 (and
1967 (or (commandp symbol)
b4d3bc10 1968 (custom-variable-p symbol))
b616a212 1969 (not (get symbol 'apropos-inhibit))))))
077decde
EZ
1970 ;; Find documentation strings:
1971 (let ((p apropos-accumulator)
1972 doc symbol)
1973 (while p
1974 (setcar p (list ; must have 3 elements:
1975 (setq symbol (car p)) ; 1. name
1976 (if (functionp symbol) ; 2. command doc
1977 (if (setq doc (documentation symbol t))
1978 (substring doc 0 (string-match "\n" doc))
1979 "(not documented)"))
b4d3bc10 1980 (if (custom-variable-p symbol) ; 3. variable doc
077decde
EZ
1981 (if (setq doc (documentation-property
1982 symbol 'variable-documentation t))
1983 (substring doc 0 (string-match "\n" doc))))))
1984 (setq p (cdr p))))
1985 ;; Output the result:
1986 (and (apropos-print t nil)
1987 message
8a26c165 1988 (message "%s" message))))
077decde
EZ
1989
1990
1991(defun WoMan-getpage-in-background (topic)
1992 "Use TOPIC to start WoMan from `Man-follow-manual-reference'."
1993 ;; topic is a string, generally of the form "section topic"
1994 (let ((s (string-match " " topic)))
1995 (if s (setq topic (substring topic (1+ s))))
1996 (woman topic)))
1997
1998(defvar WoMan-Man-start-time nil
1999 "Used to record formatting time used by the `man' command.")
2000
553f03bc
MA
2001;; Both advices are disabled because "a file in Emacs should not put
2002;; advice on a function in Emacs" (see Info node "(elisp)Advising
2003;; Functions"). Counting the formatting time is useful for
4c36be58 2004;; developing, but less applicable for daily use. The advice for
553f03bc
MA
2005;; `Man-getpage-in-background' can be discarded, because the
2006;; key-binding in `woman-mode-map' has been remapped to call `woman'
2007;; but `man'. Michael Albinus <michael.albinus@gmx.de>
2008
2009;; (defadvice Man-getpage-in-background
2010;; (around Man-getpage-in-background-advice (topic) activate)
2011;; "Use WoMan unless invoked outside a WoMan buffer or invoked explicitly.
2012;; Otherwise use Man and record start of formatting time."
2013;; (if (and (eq major-mode 'woman-mode)
2014;; (not (eq (caar command-history) 'man)))
2015;; (WoMan-getpage-in-background topic)
2016;; ;; Initiates man processing
2017;; (setq WoMan-Man-start-time (current-time))
2018;; ad-do-it))
2019
2020;; (defadvice Man-bgproc-sentinel
2021;; (after Man-bgproc-sentinel-advice activate)
2022;; ;; Terminates man processing
2023;; "Report formatting time."
2024;; (let* ((time (current-time))
2025;; (time (+ (* (- (car time) (car WoMan-Man-start-time)) 65536)
2026;; (- (cadr time) (cadr WoMan-Man-start-time)))))
2027;; (message "Man formatting done in %d seconds" time)))
077decde 2028
077decde
EZ
2029\f
2030;;; Buffer handling:
2031
2032(defun WoMan-previous-manpage ()
2033 "Find the previous WoMan buffer."
2034 ;; Assumes currently in a WoMan buffer!
2035 (interactive)
2036 (WoMan-find-buffer) ; find current existing buffer
2037 (if (null (cdr woman-buffer-alist))
2038 (error "No previous WoMan buffer"))
2039 (if (>= (setq woman-buffer-number (1+ woman-buffer-number))
2040 (length woman-buffer-alist))
2041 (setq woman-buffer-number 0))
2042 (if (WoMan-find-buffer)
2043 ()
2044 (if (< (setq woman-buffer-number (1- woman-buffer-number)) 0)
2045 (setq woman-buffer-number (1- (length woman-buffer-alist))))
2046 (WoMan-previous-manpage)))
2047
2048(defun WoMan-next-manpage ()
2049 "Find the next WoMan buffer."
2050 ;; Assumes currently in a WoMan buffer!
2051 (interactive)
2052 (WoMan-find-buffer) ; find current existing buffer
2053 (if (null (cdr woman-buffer-alist))
2054 (error "No next WoMan buffer"))
2055 (if (< (setq woman-buffer-number (1- woman-buffer-number)) 0)
2056 (setq woman-buffer-number (1- (length woman-buffer-alist))))
2057 (if (WoMan-find-buffer)
2058 ()
2059 (WoMan-next-manpage)))
2060
2061(defun WoMan-find-buffer ()
2062 "Switch to buffer corresponding to `woman-buffer-number' and return it.
1edbbf8a
EZ
2063If such a buffer does not exist then remove its association from the
2064alist in `woman-buffer-alist' and return nil."
077decde
EZ
2065 (if (zerop woman-buffer-number)
2066 (let ((buffer (get-buffer (cdr (car woman-buffer-alist)))))
2067 (if buffer
2068 (switch-to-buffer buffer)
2069 ;; Delete alist element:
2070 (setq woman-buffer-alist (cdr woman-buffer-alist))
2071 nil))
2072 (let* ((prev-ptr (nthcdr (1- woman-buffer-number) woman-buffer-alist))
2073 (buffer (get-buffer (cdr (car (cdr prev-ptr))))))
2074 (if buffer
2075 (switch-to-buffer buffer)
2076 ;; Delete alist element:
2077 (setcdr prev-ptr (cdr (cdr prev-ptr)))
2078 (if (>= woman-buffer-number (length woman-buffer-alist))
2079 (setq woman-buffer-number 0))
25c6f63e 2080 nil))))
077decde
EZ
2081
2082\f
2083;;; Syntax and display tables:
2084
2085(defconst woman-escaped-escape-char ?\1c
2086 ;; An arbitrary unused control character
2087 "Internal character representation of escaped escape characters.")
2088(defconst woman-escaped-escape-string
2089 (char-to-string woman-escaped-escape-char)
2090 "Internal string representation of escaped escape characters.")
2091
2092(defconst woman-unpadded-space-char ?\1d
2093 ;; An arbitrary unused control character
2094 "Internal character representation of unpadded space characters.")
2095(defconst woman-unpadded-space-string
2096 (char-to-string woman-unpadded-space-char)
2097 "Internal string representation of unpadded space characters.")
2098
70165841
SM
2099(defvar woman-syntax-table
2100 (let ((st (make-syntax-table)))
2101 ;; The following internal chars must NOT have whitespace syntax:
2102 (modify-syntax-entry woman-unpadded-space-char "." st)
2103 (modify-syntax-entry woman-escaped-escape-char "." st)
2104 st)
077decde
EZ
2105 "Syntax table to support special characters used internally by WoMan.")
2106
077decde
EZ
2107(defun woman-set-buffer-display-table ()
2108 "Set up a display table for a WoMan buffer.
2109This display table is used for displaying internal special characters, but
2110does not interfere with any existing display table, e.g. for displaying
2111European characters."
2112 (setq buffer-display-table
2113 ;; The following test appears to be necessary on some
2114 ;; non-Windows platforms, e.g. Solaris 2.6 when running on a
2115 ;; tty. Thanks to T. V. Raman <raman@Adobe.COM>.
2116 ;; The MS-DOS terminal also sets standard-display-table to
2117 ;; a non-nil value.
2118 (if standard-display-table ; default is nil !!!
2119 (copy-sequence standard-display-table)
2120 (make-display-table)))
2121 ;; Display the following internal chars correctly:
081e8d65 2122 (aset buffer-display-table woman-unpadded-space-char [?\s])
077decde
EZ
2123 (aset buffer-display-table woman-escaped-escape-char [?\\]))
2124
2125\f
2126;;; The main decoding driver:
2127
2128(defvar font-lock-mode) ; for the compiler
2129
2130(defun woman-decode-buffer ()
2131 "Decode a buffer in UN*X man-page source format.
2132No external programs are used."
2133 (interactive) ; mainly for testing
2134 (WoMan-log-begin)
2135 (run-hooks 'woman-pre-format-hook)
2136 (and (boundp 'font-lock-mode) font-lock-mode (font-lock-mode -1))
2137 ;; (fundamental-mode)
b9444d97
PE
2138 (let ((start-time (current-time))
2139 time)
077decde
EZ
2140 (message "WoMan formatting buffer...")
2141; (goto-char (point-min))
2142; (cond
2143; ((re-search-forward "^\\.[ \t]*TH" nil t) ; wrong format if not found?
2144; (beginning-of-line)
2145; (delete-region (point-min) (point))) ; potentially dangerous!
2146; (t (message "WARNING: .TH request not found -- not man-page format?")))
2147 (woman-decode-region (point-min) (point-max))
b9444d97
PE
2148 (setq time (float-time (time-since start-time)))
2149 (message "WoMan formatting buffer...done in %g seconds" time)
077decde
EZ
2150 (WoMan-log-end time))
2151 (run-hooks 'woman-post-format-hook))
2152
2153(defvar woman-string-alist ; rebound in woman-decode-region
2154 '(("S" . "") ("R" . "(Reg.)") ("Tm" . "(TM)")
2155 ("lq" . "\"") ("rq" . "\"")
2156 ("''" . "\"") ; needed for gcc.1
2157 (".T" . "") ; output device from -T option?
2158 )
2159 "Alist of strings predefined in the -man macro package `tmac.an'.")
2160
2161(defvar woman-negative-vertical-space nil ; rebound in woman-decode-region
2162 "Set to t if .sp N with N < 0 encountered.")
2163
2164(defun woman-pre-process-region (from to)
2165 "Pre-process escapes and comments in the region of text between FROM and TO.
2166To be called on original buffer and any .so insertions."
2167 ;; Hide escaped escapes \\ and printable escapes \e very early
2168 ;; (to be re-instated as just \ very late!):
2169 (goto-char from)
2170 ;; .eo turns off escape character processing
2171 (while (re-search-forward "\\(\\\\[\\e]\\)\\|^\\.eo" to t) ; \\
b616a212 2172 (if (match-beginning 1)
077decde
EZ
2173 (replace-match woman-escaped-escape-string t t)
2174 (woman-delete-whole-line)
2175 ;; .ec turns on escape character processing (and sets the
2176 ;; escape character to its argument, if any, which I'm ignoring
2177 ;; for now!)
2178 (while (and (re-search-forward "\\(\\\\\\)\\|^\\.ec" to t) ; \
b616a212 2179 (match-beginning 1))
077decde
EZ
2180 (replace-match woman-escaped-escape-string t t))
2181 ;; ***** Need test for .ec arg and warning here! *****
2182 (woman-delete-whole-line)))
2183
2d4c3c85
JB
2184 ;; Delete comments .\"<anything>, \"<anything> and null requests.
2185 ;; (However, should null . requests cause a break?)
077decde
EZ
2186 (goto-char from)
2187 (while (re-search-forward "^[.'][ \t]*\\(\\\\\".*\\)?\n\\|\\\\\".*" to t)
2d4c3c85 2188 (woman-delete-match 0)))
077decde
EZ
2189
2190(defun woman-non-underline-faces ()
2191 "Prepare non-underlined versions of underlined faces."
2192 (let ((face-list (face-list)))
86970dbd
SM
2193 (dolist (face face-list)
2194 (let ((face-name (symbol-name face)))
077decde
EZ
2195 (if (and (string-match "\\`woman-" face-name)
2196 (face-underline-p face))
2197 (let ((face-no-ul (intern (concat face-name "-no-ul"))))
2198 (copy-face face face-no-ul)
c9ed8e3d 2199 (set-face-underline face-no-ul nil)))))))
077decde 2200
2d4c3c85
JB
2201;; Preprocessors
2202;; =============
2203
2204;; This information is based on documentation for the man command by
2205;; Graeme W. Wilford <G.Wilford@ee.surrey.ac.uk>
2206
2207;; First, the environment variable $MANROFFSEQ is interrogated, and if
2208;; not set then the initial line of the nroff file is parsed for a
2209;; preprocessor string. To contain a valid preprocessor string, the
2210;; first line must resemble
2211;;
2212;; '\" <string>
2213;;
2214;; where string can be any combination of the following letters that
2215;; specify the sequence of preprocessors to run before nroff or
2216;; troff/groff. Not all installations will have a full set of
2217;; preprocessors. Some of the preprocessors and the letters used to
2218;; designate them are: eqn (e), grap (g), pic (p), tbl (t), vgrind
2219;; (v), refer (r). This option overrides the $MANROFFSEQ environment
2220;; variable. zsoelim is always run as the very first preprocessor.
2221
2222(defvar woman-emulate-tbl nil
2223 "True if WoMan should emulate the tbl preprocessor.
2224This applies to text between .TE and .TS directives.
2225Currently set only from '\" t in the first line of the source file.")
2226
06b60517 2227(defun woman-decode-region (from _to)
077decde
EZ
2228 "Decode the region between FROM and TO in UN*X man-page source format."
2229 ;; Suitable for use in format-alist.
2230 ;; But this requires care to control major mode implied font locking.
2231 ;; Must return the new end of file. See format.el for details.
2232 ;; NB: The `to' argument is bogus: it is not currently used, and if
2233 ;; it were it would need to be a marker rather than a position!
2234 ;; First force the correct environment:
2235 (let ((case-fold-search nil) ; This is necessary!
2236 (woman-string-alist woman-string-alist)
2237 (woman-fill-column woman-fill-column)
2238 woman-negative-vertical-space)
2239 (setq woman-left-margin woman-default-indent
2240 woman-prevailing-indent woman-default-indent
2241 woman-interparagraph-distance 1
2242 woman-leave-blank-lines nil
2243 woman-RS-left-margin nil
2244 woman-RS-prevailing-indent nil
2245 woman-adjust woman-adjust-both
2246 woman-justify (nth woman-adjust woman-justify-list)
2247 woman-nofill nil)
2248
1edbbf8a
EZ
2249 (setq woman-if-conditions-true
2250 (cons (string-to-char (symbol-name woman-emulation)) '(?e ?o)))
2251
077decde
EZ
2252 ;; Prepare non-underlined versions of underlined faces:
2253 (woman-non-underline-faces)
b66e8575 2254 ;; Set font of `woman-symbol' face to `woman-symbol-font' if
1edbbf8a
EZ
2255 ;; `woman-symbol-font' is well defined.
2256 (and woman-use-symbol-font
2257 (stringp woman-symbol-font)
b66e8575 2258 (set-face-font 'woman-symbol woman-symbol-font
1edbbf8a 2259 (and (frame-live-p woman-frame) woman-frame)))
077decde 2260
c9cac599
SM
2261 (setq-local adaptive-fill-mode nil) ; No special "%" "#" etc filling.
2262
2263 ;; Set syntax and display tables:
077decde
EZ
2264 (set-syntax-table woman-syntax-table)
2265 (woman-set-buffer-display-table)
2266
2267 ;; Based loosely on a suggestion by Theodore Jump:
2268 (if (or woman-fill-frame
2269 (not (and (integerp woman-fill-column) (> woman-fill-column 0))))
bd48e7c6 2270 (setq woman-fill-column (- (window-width) woman-default-indent)))
077decde 2271
2d4c3c85
JB
2272 ;; Check for preprocessor requests:
2273 (goto-char from)
2274 (if (looking-at "'\\\\\"[ \t]*\\([a-z]+\\)")
2275 (let ((letters (append (match-string 1) nil)))
2276 (if (memq ?t letters)
2277 (setq woman-emulate-tbl t
2278 letters (delete ?t letters)))
2279 (if letters
2280 (WoMan-warn "Unhandled preprocessor request letters %s"
2281 (concat letters)))
2282 (woman-delete-line 1)))
2283
077decde
EZ
2284 (woman-pre-process-region from nil)
2285 ;; Process ignore requests, macro definitions,
2286 ;; conditionals and switch source requests:
2287 (woman0-roff-buffer from)
2288
69c8eea2
CY
2289 ;; Check for macro sets that woman cannot handle. We can only
2290 ;; because do this after processing source-switch directives.
c7d39723
CY
2291 (goto-char (point-min))
2292 (let ((case-fold-search nil))
2293 (unless (and (re-search-forward "^\\.SH[ \n]" (point-max) t)
2294 (progn (goto-char (point-min))
2295 (re-search-forward "^\\.TH[ \n]" (point-max) t))
2296 (progn (goto-char (point-min))
2297 (not (re-search-forward "^\\.\\([pnil]p\\|sh\\)[ \n]"
2298 (point-max) t))))
2299 (error "WoMan can only format man pages written with the usual `-man' macros")))
2300
077decde
EZ
2301 ;; Process \k escapes BEFORE changing tab width (?):
2302 (goto-char from)
2303 (woman-mark-horizonal-position)
2304
2305 ;; Set buffer-local variables:
2306 (setq fill-column woman-fill-column
2307 tab-width woman-tab-width)
2308
2309 ;; Hide unpaddable and digit-width spaces \(space) and \0:
2310 (goto-char from)
2311 (while (re-search-forward "\\\\[ 0]" nil t)
2312 (replace-match woman-unpadded-space-string t t))
2313
2314 ;; Discard optional hyphen \%; concealed newlines \<newline>;
2315 ;; point-size change function \sN,\s+N, \s-N:
2316 (goto-char from)
2317 (while (re-search-forward "\\\\\\([%\n]\\|s[-+]?[0-9]+\\)" nil t)
2318 (woman-delete-match 0))
2319
2320 ;; BEWARE: THIS SHOULD PROBABLY ALL BE DONE MUCH LATER!!!!!
2321 ;; Process trivial escapes \-, \`, \.
2322 ;; (\' must be done after tab processing!):
2323 (goto-char from)
2324 (while (re-search-forward "\\\\\\([-`.]\\)" nil t)
2325 (replace-match "\\1"))
2326 ;; NB: Must keep ALL zero-width characters \&, \|, and \^ until
2327 ;; ALL requests processed!
2328
2329 ;; Process no-break requests and macros (including font-change macros):
2330 (goto-char from)
2331 (woman1-roff-buffer)
2332
2333 ;; Process strings and special character escapes \(xx:
2334 ;; (Must do this BEFORE fontifying!)
2335 (goto-char from)
2336 (woman-strings)
2337 ;; Special chars moved after translation in
2338 ;; `woman2-process-escapes' (for pic.1):
2339; (goto-char from)
2340; (woman-special-characters)
2341
2342 ;; Process standard font-change requests and escapes:
2343 (goto-char from)
2344 (woman-change-fonts)
2345
2346 ;; 1/2 em vertical motion \d, \u and general local vertical motion
2347 ;; \v'+/-N' simulated using TeX ^ and _ symbols for now.
2348 (goto-char from)
2349 (let ((first t)) ; assume no nesting!
2350 (while (re-search-forward "\\\\\\([du]\\|v'[^']*'\\)" nil t)
2351 (let* ((esc (match-string 1))
2352 (repl (if (or (= (aref esc 0) ?u)
2353 (and (>= (length esc) 2) (= (aref esc 2) ?-)))
2354 "^" "_")))
2355 (cond (first
2356 (replace-match repl nil t)
b66e8575 2357 (put-text-property (1- (point)) (point) 'face 'woman-addition)
077decde
EZ
2358 (WoMan-warn
2359 "Initial vertical motion escape \\%s simulated" esc)
2360 (WoMan-log
2361 " by TeX `%s' in woman-addition-face!" repl))
2362 (t
2363 (woman-delete-match 0)
2364 (WoMan-warn
2365 "Terminal vertical motion escape \\%s ignored!" esc)))
25c6f63e 2366 (setq first (not first)))))
077decde
EZ
2367
2368 ;; Process formatting macros
2369 (goto-char from)
2370 (woman2-roff-buffer)
2371
2372 ;; Go back and process negative vertical space if necessary:
2373 (if woman-negative-vertical-space
2374 (woman-negative-vertical-space from))
2375
f0ecdfea
KR
2376 (when woman-preserve-ascii
2377 ;; Re-instate escaped escapes to just `\' and unpaddable spaces
2378 ;; to just `space'. This is not necessary for display since
2379 ;; there are display table entries for the escaped chars, but it
2380 ;; is necessary if the buffer might be saved as ASCII.
2381 ;;
2382 ;; `subst-char-in-region' preserves text properties on the
2383 ;; characters, which is necessary for bold, underline, etc on
2384 ;; \e. There's usually no face on spaces, but if there is then
2385 ;; it's good to keep that too.
2386 (subst-char-in-region from (point-max)
2387 woman-escaped-escape-char ?\\)
2388 (subst-char-in-region from (point-max)
2389 woman-unpadded-space-char ?\s))
077decde
EZ
2390
2391 ;; Must return the new end of file if used in format-alist.
2392 (point-max)))
2393
2394(defun woman-horizontal-escapes (to)
1edbbf8a
EZ
2395 "Process \\h'+/-N' local horizontal motion escapes upto TO.
2396Implements arbitrary forward and non-overlapping backward motion.
2397Preserves location of `point'."
077decde
EZ
2398 ;; Moved from `woman-decode-region' for version 0.50.
2399 ;; N may include width escape \w'...' (but may already be processed!
2400 (let ((from (point)))
2401 (while (re-search-forward
2402 ;; Delimiter can be a special char escape sequence \(.. or
2403 ;; a single normal char (usually '):
2404 "\\\\h\\(\\\\(..\\|.\\)\\(|\\)?"
2405 to t)
2406 (let ((from (match-beginning 0))
2407 (delim (regexp-quote (match-string 1)))
b616a212 2408 (absolute (match-beginning 2)) ; absolute position?
077decde
EZ
2409 (N (woman-parse-numeric-arg)) ; distance
2410 to
2411 msg) ; for warning
2412 (if (not (looking-at delim))
2413 ;; Warn but leave escape in buffer unprocessed:
2414 (WoMan-warn
2415 "Local horizontal motion (%s) delimiter error!"
2416 (buffer-substring from (1+ (point)))) ; point at end of arg
2417 (setq to (match-end 0)
2418 ;; For possible warning -- save before deleting:
2419 msg (buffer-substring from to))
2420 (delete-region from to)
2421 (if absolute ; make relative
2422 (setq N (- N (current-column))))
2423 (if (>= N 0)
2424 ;; Move forward by inserting hard spaces:
2425 (insert-char woman-unpadded-space-char N)
2426 ;; Move backwards by deleting space,
2427 ;; first backwards then forwards:
2428 (while (and
2429 (<= (setq N (1+ N)) 0)
081e8d65 2430 (cond ((memq (preceding-char) '(?\s ?\t))
d355a0b7 2431 (delete-char -1) t)
081e8d65 2432 ((memq (following-char) '(?\s ?\t))
077decde
EZ
2433 (delete-char 1) t)
2434 (t nil))))
2435 (if (<= N 0)
2436 (WoMan-warn
25c6f63e 2437 "Negative horizontal motion (%s) would overwrite!" msg))))))
077decde
EZ
2438 (goto-char from)))
2439
2440
2441\f
1edbbf8a
EZ
2442;; Process ignore requests (.ig), conditionals (.if etc.),
2443;; source-switch (.so), macro definitions (.de etc.) and macro
2444;; expansions.
077decde
EZ
2445
2446(defvar woman0-if-to) ; marker bound in woman0-roff-buffer
2447(defvar woman0-macro-alist) ; bound in woman0-roff-buffer
2448(defvar woman0-search-regex) ; bound in woman0-roff-buffer
2449(defvar woman0-search-regex-start ; bound in woman0-roff-buffer
2450 "^[.'][ \t]*\\(ig\\|if\\|ie\\|el\\|so\\|rn\\|de\\|am")
2451(defconst woman0-search-regex-end "\\)\\([ \t]+\\|$\\)")
2452;; May need other terminal characters, e.g. \, but NOT \n!
2453;; Alternatively, force maximal match (Posix?)
2454
2455(defvar woman0-rename-alist) ; bound in woman0-roff-buffer
2456
2457(defun woman0-roff-buffer (from)
1edbbf8a
EZ
2458 "Process conditional-type requests and user-defined macros.
2459Start at FROM and re-scan new text as appropriate."
077decde
EZ
2460 (goto-char from)
2461 (let ((woman0-if-to (make-marker))
f7fe4837 2462 woman-request woman0-macro-alist
077decde
EZ
2463 (woman0-search-regex-start woman0-search-regex-start)
2464 (woman0-search-regex
2465 (concat woman0-search-regex-start woman0-search-regex-end))
40e22d80 2466 processed-first-hunk
077decde 2467 woman0-rename-alist)
8cc8239e 2468 (set-marker-insertion-type woman0-if-to t)
077decde 2469 (while (re-search-forward woman0-search-regex nil t)
f7fe4837 2470 (setq woman-request (match-string 1))
40e22d80
CY
2471
2472 ;; Process escape sequences prior to first request (Bug#7843).
2473 (unless processed-first-hunk
2474 (setq processed-first-hunk t)
2475 (let ((process-escapes-to-marker (point-marker)))
2476 (set-marker-insertion-type process-escapes-to-marker t)
2477 (save-match-data
2478 (save-excursion
2479 (goto-char from)
2480 (woman2-process-escapes process-escapes-to-marker)))))
2481
f7fe4837
GM
2482 (cond ((string= woman-request "ig") (woman0-ig))
2483 ((string= woman-request "if") (woman0-if "if"))
2484 ((string= woman-request "ie") (woman0-if "ie"))
2485 ((string= woman-request "el") (woman0-el))
2486 ((string= woman-request "so") (woman0-so))
2487 ((string= woman-request "rn") (woman0-rn))
2488 ((string= woman-request "de") (woman0-de))
2489 ((string= woman-request "am") (woman0-de 'append))
2490 (t (woman0-macro woman-request))))
077decde
EZ
2491 (set-marker woman0-if-to nil)
2492 (woman0-rename)
2493 ;; Should now re-run `woman0-roff-buffer' if any renaming was
2494 ;; done, but let's just hope this is not necessary for now!
2495 ))
2496
2497(defun woman0-ig ()
2498 ".ig yy -- Discard input up to `.yy', which defaults to `..')."
2499 ;; The terminal request MUST begin with . (not ')!
2500 (looking-at "\\(\\S +\\)?")
2501 (beginning-of-line)
2502 (let ((yy (or (match-string 1) "."))
2503 (from (point)))
2504 (if (re-search-forward
2505 (concat "^\\.[ \t]*" (regexp-quote yy) ".*\n") nil t)
2506 (delete-region from (point))
2507 (WoMan-warn
2508 "ig request ignored -- terminator `.%s' not found!" yy)
25c6f63e 2509 (woman-delete-line 1))))
077decde
EZ
2510
2511(defsubst woman0-process-escapes (from to)
2512 "Process escapes within an if/ie condition between FROM and TO."
2513 (woman-strings to)
2514 (goto-char from) ; necessary!
2515 ;; Strip font-change escapes:
2d4c3c85 2516 (while (re-search-forward "\\\\f\\(\\[[^]]+\\]\\|(..\\|.\\)" to t)
077decde
EZ
2517 (woman-delete-match 0))
2518 (goto-char from) ; necessary!
2519 (woman2-process-escapes to 'numeric))
2520
f7fe4837 2521;; request does not appear to be used dynamically by any callees.
077decde
EZ
2522(defun woman0-if (request)
2523 ".if/ie c anything -- Discard unless c evaluates to true.
2524Remember condition for use by a subsequent `.el'.
2525REQUEST is the invoking directive without the leading dot."
2526 ;; c evaluates to a one-character built-in condition name or
2527 ;; 'string1'string2' or a number > 0, prefix ! negates.
2528 ;; \{ ... \} for multi-line use.
2529 ;; Leaves point at start of new text.
2530 (woman-delete-match 0)
2531 ;; (delete-horizontal-space)
2532 ;; Process escapes in condition:
2533 (let ((from (point)) negated n (c 0))
2534 (set-marker woman0-if-to
2535 (save-excursion (skip-syntax-forward "^ ") (point)))
2536 ;; Process condition:
2537 (if (setq negated (= (following-char) ?!)) (delete-char 1))
2538 (cond
1edbbf8a
EZ
2539 ;; ((looking-at "[no]") (setq c t)) ; accept n(roff) and o(dd page)
2540 ;; ((looking-at "[te]") (setq c nil)) ; reject t(roff) and e(ven page)
8b2e00a3 2541 ;; Per groff ".if v" is recognized as false (it means -Tversatec).
54eea618 2542 ((looking-at "[ntoev]")
1edbbf8a 2543 (setq c (memq (following-char) woman-if-conditions-true)))
e1dbe924 2544 ;; Unrecognized letter so reject:
077decde 2545 ((looking-at "[A-Za-z]") (setq c nil)
adf79d27 2546 (WoMan-warn "%s %s -- unrecognized condition name rejected!"
077decde
EZ
2547 request (match-string 0)))
2548 ;; Accept strings if identical:
2549 ((save-restriction
2550 (narrow-to-region from woman0-if-to)
2551 ;; String delimiter can be any non-numeric character,
2552 ;; including a special character escape:
2553 (looking-at "\\(\\\\(..\\|[^0-9]\\)\\(.*\\)\\1\\(.*\\)\\1\\'"))
8cc8239e 2554 (let ((end1 (copy-marker (match-end 2) t))) ; End of first string.
077decde
EZ
2555 ;; Delete 2nd and 3rd delimiters to avoid processing them:
2556 (delete-region (match-end 3) woman0-if-to)
2557 (delete-region (match-end 2) (match-beginning 3))
2558 (goto-char (match-end 1))
2559 (woman0-process-escapes (point) woman0-if-to)
2560 (setq c (string= (buffer-substring (point) end1)
2561 (buffer-substring end1 woman0-if-to)))
2562 (set-marker end1 nil)
2563 (goto-char from)))
2564 ;; Accept numeric value if > 0:
2565 ((numberp (setq n (progn
2566 (woman0-process-escapes from woman0-if-to)
2567 (woman-parse-numeric-arg))))
2568 (setq c (> n 0))
25c6f63e 2569 (goto-char from)))
077decde
EZ
2570 (if (eq c 0)
2571 (woman-if-ignore woman0-if-to request) ; ERROR!
25c6f63e 2572 (woman-if-body request woman0-if-to (eq c negated)))))
077decde 2573
f7fe4837 2574;; request is not used dynamically by any callees.
077decde 2575(defun woman-if-body (request to delete) ; should be reversed as `accept'?
1edbbf8a
EZ
2576 "Process if-body, including \\{ ... \\}.
2577REQUEST is the invoking directive without the leading dot.
2578If TO is non-nil then delete the if-body.
2579If DELETE is non-nil then delete from point."
077decde
EZ
2580 ;; Assume concealed newlines already processed.
2581 (let ((from (point)))
2582 (if to (delete-region (point) to))
2583 (delete-horizontal-space)
2584 (cond (;;(looking-at "[^{\n]*\\\\{\\s *") ; multi-line
2585 ;; allow escaped newlines:
2586 (looking-at "[^{\n]*\\(\\\\\n\\)*\\\\{\\s *\\(\\\\\n\\)*") ; multi-line
2587 ;; including preceding .if(s) and following newline
2588 (let ((from (point)))
2589 (woman-delete-match 0)
2590 ;; Allow for nested \{ ... \} -- BUT BEWARE that this
2591 ;; algorithm only supports one level of nesting!
2592 (while
2593 (and (re-search-forward
2594 ;; "\\(\\\\{\\)\\|\\(\n[.']\\)?[ \t]*\\\\}[ \t]*"
2595 ;; Interpret bogus `el \}' as `el \{',
2596 ;; especially for Tcl/Tk man pages:
2597 "\\(\\\\{\\|el[ \t]*\\\\}\\)\\|\\(\n[.']\\)?[ \t]*\\\\}[ \t]*")
b616a212 2598 (match-beginning 1))
077decde
EZ
2599 (re-search-forward "\\\\}"))
2600 (delete-region (if delete from (match-beginning 0)) (point))
2601 (if (looking-at "^$") (delete-char 1))
2602 ))
25c6f63e 2603 (delete (woman-delete-line 1))) ; single-line
077decde 2604 ;; Process matching .el anything:
25c6f63e 2605 (cond ((string= request "ie")
077decde 2606 ;; Discard unless previous .ie c `evaluated to false'.
28c45130
GM
2607 ;; IIUC, an .ie must be followed by an .el.
2608 ;; (An if with no else uses .if rather than .ie.)
2609 ;; TODO warn if no .el found?
2610 ;; The .el should come immediately after the .ie (modulo
2611 ;; comments etc), but this searches to eob.
077decde
EZ
2612 (cond ((re-search-forward "^[.'][ \t]*el[ \t]*" nil t)
2613 (woman-delete-match 0)
2614 (woman-if-body "el" nil (not delete)))))
28c45130
GM
2615;;; FIXME neither the comment nor the code here make sense to me.
2616;;; This branch was executed for an else (any else, AFAICS).
2617;;; At this point, the else in question has already been processed above.
2618;;; The re-search will find the _next_ else, if there is one, and
2619;;; delete it. If there is one, it belongs to another if block. (Bug#9447)
2620;;; woman0-el does not need this bit either.
077decde
EZ
2621 ;; Got here after processing a single-line `.ie' as a body
2622 ;; clause to be discarded:
28c45130
GM
2623;;; ((string= request "el")
2624;;; (cond ((re-search-forward "^[.'][ \t]*el[ \t]*" nil t)
2625;;; (woman-delete-match 0)
2626;;; (woman-if-body "el" nil t)))))
2627 )
25c6f63e 2628 (goto-char from)))
077decde
EZ
2629
2630(defun woman0-el ()
2631 "Isolated .el request -- should not happen!"
2632 (WoMan-warn "el request without matching `ie' rejected!")
2633 (cond (woman-ignore
2634 (woman-delete-match 0)
2635 (delete-horizontal-space)
2636 (woman-if-body "el" nil t))
2637 (t ; Ignore -- leave in buffer
2638 ;; This does not work too well, but it's only for debugging!
2639 (skip-chars-forward "^ \t")
2640 (if (looking-at "[ \t]*\\{") (search-forward "\\}"))
2641 (forward-line 1))))
2642
f7fe4837 2643;; request is not used dynamically by any callees.
077decde 2644(defun woman-if-ignore (to request)
1edbbf8a 2645 "Ignore but warn about an if request ending at TO, named REQUEST."
077decde
EZ
2646 (WoMan-warn-ignored request "ignored -- condition not handled!")
2647 (if woman-ignore
2648 (woman-if-body request to t)
2649 ;; Ignore -- leave in buffer
2650 ;; This does not work too well, but it's only for debugging!
2651 (skip-chars-forward "^ \t")
2652 (if (looking-at "[ \t]*\\{") (search-forward "\\}"))
2653 (forward-line 1)))
2654
2655(defun woman0-so ()
2656 ".so filename -- Switch source file. `.so' requests may be nested."
2657 ;; Leaves point at start of new text.
2658 ;; (skip-chars-forward " \t")
2659 (let* ((beg (point))
2660 (end (progn (woman-forward-arg 'unquote) (point)))
2661 (name (buffer-substring beg end))
2662 (filename name))
2663 ;; If the specified file does not exist in this ...
2664 (or (file-exists-p filename)
2665 ;; or the parent directory ...
2666 (file-exists-p
2667 (setq filename (concat "../" name)))
2668 ;; then use the WoMan search mechanism to find the filename ...
2669 (setq filename
2670 (woman-file-name
d2c32364 2671 (file-name-base name)))
077decde
EZ
2672 ;; Cannot find the file, so ...
2673 (kill-buffer (current-buffer))
2674 (error "File `%s' not found" name))
2675 (beginning-of-line)
2676 (woman-delete-line 1)
da37d01f
JB
2677 (let* ((from (point))
2678 (length (woman-insert-file-contents filename 0))
2679 (to (copy-marker (+ from length) t)))
077decde
EZ
2680 (woman-pre-process-region from to)
2681 (set-marker to nil)
25c6f63e 2682 (goto-char from))))
077decde
EZ
2683
2684\f
2685;;; Process macro definitions:
2686
2687(defun woman0-rn ()
2688 "Process .rn xx yy -- rename macro xx to yy."
2689 ;; For now, done backwards AFTER all macro expansion.
2690 ;; Should also allow requests and strings to be renamed!
2691 (if (eolp) ; ignore if no argument
2692 ()
2693 (let* ((beg (point))
2694 (end (progn (woman-forward-arg 'unquote 'concat) (point)))
2695 (old (buffer-substring beg end))
2696 new)
2697 (if (eolp) ; ignore if no argument
2698 ()
2699 (setq beg (point)
2700 end (progn (woman-forward-arg 'unquote) (point))
2701 new (buffer-substring beg end)
25c6f63e 2702 woman0-rename-alist (cons (cons new old) woman0-rename-alist)))))
077decde
EZ
2703 (woman-delete-whole-line))
2704
2705(defun woman0-rename ()
2706 "Effect renaming required by .rn requests."
2707 ;; For now, do this backwards AFTER all macro expansion.
8d660cdf 2708 (dolist (new woman0-rename-alist)
86970dbd
SM
2709 (let ((old (cdr new))
2710 (new (car new)))
077decde
EZ
2711 (goto-char (point-min))
2712 (setq new (concat "^[.'][ \t]*" (regexp-quote new)))
2713 (setq old (concat "." old))
2714 (while (re-search-forward new nil t)
2715 (replace-match old nil t)))))
2716
2717(defconst woman-unescape-regex
2718 (concat woman-escaped-escape-string
2719 "\\(" woman-escaped-escape-string "\\)?"))
2720
2721(defsubst woman-unescape (macro)
1edbbf8a 2722 "Replace escape sequences in the body of MACRO.
077decde
EZ
2723Replaces || by |, but | by \, where | denotes the internal escape."
2724 (let (start)
2725 (while (setq start (string-match woman-unescape-regex macro start))
2726 (setq macro
b616a212 2727 (if (match-beginning 1)
077decde
EZ
2728 (replace-match "" t t macro 1)
2729 (replace-match "\\" t t macro))
2730 start (1+ start)))
2731 macro))
2732
2733(defun woman0-de (&optional append)
2734 "Process .de/am xx yy -- (re)define/append macro xx; end at `..'.
2735\(Should be up to call of yy, which defaults to `.')
2736Optional argument APPEND, if non-nil, means append macro."
6772c8e1 2737 ;; Modeled on woman-strings. BEWARE: Processing of .am is a hack!
077decde
EZ
2738 ;; Add support for .rm?
2739 ;; (skip-chars-forward " \t")
2740 (if (eolp) ; ignore if no argument
2741 ()
2742 (looking-at "[^ \t\n]+") ; macro name
2743 (let* ((macro (match-string 0)) from
2744 (previous (assoc macro woman0-macro-alist)))
2745 (if (not previous)
2746 (setq woman0-search-regex-start
2747 (concat woman0-search-regex-start "\\|" (regexp-quote macro))
2748 woman0-search-regex
2749 (concat woman0-search-regex-start woman0-search-regex-end)
2750 ))
2751 ;; Macro body runs from start of next line to line
2752 ;; beginning with `..'."
2753 ;; The terminal request MUST begin with `.' (not ')!
2754 (forward-line)
2755 (setq from (point))
2756 (re-search-forward "^\\.[ \t]*\\.")
2757 (beginning-of-line)
2758 (let ((body (woman-unescape (buffer-substring from (point)))))
2759 (if (and append previous)
2760 (setq previous (cdr previous)
2761 body (concat body (cdr previous))
2762 append (car previous)
2763 ))
2764 (setq macro (cons macro (cons append body))))
2765 ;; This should be an update, but consing a new string
2766 ;; onto the front of the alist has the same effect:
2767 (setq woman0-macro-alist (cons macro woman0-macro-alist))
2768 (forward-line)
2769 (delete-region from (point))
25c6f63e 2770 (backward-char))) ; return to end of .de/am line
077decde
EZ
2771 (beginning-of-line) ; delete .de/am line
2772 (woman-delete-line 1))
2773
f7fe4837
GM
2774;; request may be used dynamically (woman-interpolate-macro calls
2775;; woman-forward-arg).
2776(defun woman0-macro (woman-request)
2777 "Process the macro call named WOMAN-REQUEST."
077decde 2778 ;; Leaves point at start of new text.
f7fe4837 2779 (let ((macro (assoc woman-request woman0-macro-alist)))
077decde
EZ
2780 (if macro
2781 (woman-interpolate-macro (cdr macro))
2782 ;; SHOULD DELETE THE UNINTERPRETED REQUEST!!!!!
2783 ;; Output this message once only per call (cf. strings)?
f7fe4837 2784 (WoMan-warn "Undefined macro %s not interpolated!" woman-request))))
077decde
EZ
2785
2786(defun woman-interpolate-macro (macro)
1edbbf8a 2787 "Interpolate (.de) or append (.am) expansion of MACRO into the buffer."
077decde
EZ
2788 ;; Could make this more efficient by checking which arguments are
2789 ;; actually used in the expansion!
2790 (skip-chars-forward " \t")
2791 ;; Process arguments:
2792 (let ((argno 0) (append (car macro))
2793 argno-string formal-arg from actual-arg start)
2794 (setq macro (cdr macro))
2795 (while (not (eolp))
2796 ;; Get next actual arg:
2797 (setq argno (1+ argno))
2798 (setq argno-string (format "%d" argno))
2799 (setq formal-arg (concat "\\\\\\$" argno-string)) ; regexp
2800 (setq from (point))
2801 (woman-forward-arg 'unquote 'noskip)
2802 (setq actual-arg (buffer-substring from (point)))
2803 (skip-chars-forward " \t") ; now skip following whitespace!
2804 ;; Replace formal arg with actual arg:
2805 (setq start nil)
2806 (while (setq start (string-match formal-arg macro start))
25c6f63e 2807 (setq macro (replace-match actual-arg t t macro))))
077decde
EZ
2808 ;; Delete any remaining formal arguments:
2809 (setq start nil)
2810 (while
2811 (setq start (string-match "\\\\\\$." macro start))
2812 (setq macro (replace-match "" t t macro)))
2813 ;; Replace .$ number register with actual arg:
2814 ;; (Do this properly via register mechanism later!)
2815 (setq start nil)
2816 (while
2817 (setq start (string-match "\\\\n(\\.\\$" macro start)) ; regexp
2818 (setq macro (replace-match argno-string t t macro)))
2819 (if append
2820 (forward-char)
2821 (beginning-of-line)
2822 (woman-delete-line 1))
2823 (save-excursion ; leave point at start of new text
2824 (insert macro))))
2825
2826\f
2827;;; Process strings:
2828
2d4c3c85
JB
2829(defun woman-match-name ()
2830 "Match and move over name of form: x, (xx or [xxx...].
2831Applies to number registers, fonts, strings/macros/diversions, and
2832special characters."
2833 (cond ((= (following-char) ?\[ )
2834 (forward-char)
2835 (re-search-forward "[^]]+")
2836 (forward-char)) ; skip closing ]
2837 ((= (following-char) ?\( )
2838 (forward-char)
2839 (re-search-forward ".."))
2840 (t (re-search-forward "."))))
2841
077decde 2842(defun woman-strings (&optional to)
1edbbf8a
EZ
2843 "Process ?roff string requests and escape sequences up to buffer position TO.
2844Strings are defined/updated by `.ds xx string' requests and
2845interpolated by `\*x' and `\*(xx' escapes."
077decde
EZ
2846 ;; Add support for .as and .rm?
2847 (while
2848 ;; Find .ds requests and \* escapes:
2849 (re-search-forward "\\(^[.'][ \t]*ds\\)\\|\\\\\\*" to t)
b616a212 2850 (cond ((match-beginning 1) ; .ds
077decde
EZ
2851 (skip-chars-forward " \t")
2852 (if (eolp) ; ignore if no argument
2853 ()
2854 (re-search-forward "[^ \t\n]+")
2855 (let ((string (match-string 0)))
2856 (skip-chars-forward " \t")
f0ecdfea
KR
2857 (if (= ?\" (following-char))
2858 ;; Double-quote starts a string, eg.
2859 ;; .ds foo "blah...
2860 ;; is value blah... through to newline. There's no
2861 ;; closing " (per the groff manual), but rather any
2862 ;; further " is included literally in the string. Eg.
2863 ;; .ds foo ""
2864 ;; sets foo to a single " character.
2865 (forward-char))
2866 (setq string (cons string
2867 (buffer-substring (point)
2868 (line-end-position))))
077decde
EZ
2869 ;; This should be an update, but consing a new string
2870 ;; onto the front of the alist has the same effect:
2871 (setq woman-string-alist (cons string woman-string-alist))
2872 ))
2873 (beginning-of-line)
2874 (woman-delete-line 1))
2875 (t ; \*
2876 (let ((beg (match-beginning 0)))
2d4c3c85 2877 (woman-match-name)
077decde
EZ
2878 (let* ((stringname (match-string 0))
2879 (string (assoc stringname woman-string-alist)))
2880 (cond (string
2881 (delete-region beg (point))
2882 ;; Temporary hack in case string starts with a
2883 ;; control character:
2884 (if (bolp) (insert-before-markers "\\&"))
2885 (insert-before-markers (cdr string)))
2886 (t
2887 (WoMan-warn "Undefined string %s not interpolated!"
2888 stringname)
2889 (cond (woman-ignore
2890 ;; Output above message once only per call
2891 (delete-region beg (point))
2892 (setq woman-string-alist
2893 (cons (cons stringname "")
25c6f63e 2894 woman-string-alist))))))))))))
077decde
EZ
2895
2896\f
2897;;; Process special character escapes \(xx:
2898
2899(defconst woman-special-characters
2900 ;; To be built heuristically as required!
2901 ;; MUST insert all characters as strings for correct conversion to
2902 ;; multibyte representation!
2903 '(("em" "--" "\276" . t) ; 3/4 Em dash
2904 ("bu" "*" "\267" . t) ; bullet
2905 ("fm" "'") ; foot mark
2906 ("co" "(C)" "\251") ; copyright
2907
2908 ("pl" "+" "+" . t) ; math plus
2909 ("mi" "-" "-" . t) ; math minus
2910 ("**" "*" "*" . t) ; math star
2911 ("aa" "'" "\242" . t) ; acute accent
2912 ("ul" "_") ; underrule
2913
2914 ("*S" "Sigma" "S" . t) ; Sigma
2915
2916 (">=" ">=" "\263" . t) ; >=
2917 ("<=" "<=" "\243" . t) ; <=
2918 ("->" "->" "\256" . t) ; right arrow
2919 ("<-" "<-" "\254" . t) ; left arrow
2920 ("mu" " x " "\264" . t) ; multiply
2921 ("+-" "+/-" "\261" . t) ; plus-minus
2922 ("bv" "|") ; bold vertical
2923
2924 ;; groff etc. extensions:
8aa187e7 2925 ;; List these via eg man -Tdvi groff_char > groff_char.dvi.
077decde
EZ
2926 ("lq" "\"")
2927 ("rq" "\"")
2928 ("aq" "'")
2929 ("ha" "^")
2930 ("ti" "~")
8aa187e7
GM
2931 ("oq" "‘") ; u2018
2932 ("cq" "’") ; u2019
69f4b618 2933 ("hy" "‐") ; u2010
077decde
EZ
2934 )
2935 "Alist of special character codes with ASCII and extended-font equivalents.
2936Each alist elements has the form
2937 (input-string ascii-string extended-font-string . use-symbol-font)
2938where
2939 * `\\(input-string' is the ?roff encoding,
2940 * `ascii-string' is the (multi-character) ASCII simulation,
2941 * `extended-font-string' is the single-character string representing
2942 the character position in the extended 256-character font, and
2943 * `use-symbol-font' is t to indicate use of the symbol font or nil,
2944 i.e. omitted, to indicate use of the default font.
2945Any element may be nil. Avoid control character codes (0 to \\37, \\180
2946to \\237) in `extended-font-string' for now, since they can be
2947displayed only with a modified display table.
2948
1edbbf8a 2949Use the WoMan command `woman-display-extended-fonts' or a character
077decde
EZ
2950map accessory to help construct this alist.")
2951
1edbbf8a
EZ
2952(defsubst woman-replace-match (newtext &optional face)
2953 "Replace text matched by last search with NEWTEXT and return t.
2954Set NEWTEXT in face FACE if specified."
2955 (woman-delete-match 0)
2956 (insert-before-markers newtext)
b66e8575 2957 (if face (put-text-property (1- (point)) (point) 'face 'woman-symbol))
1edbbf8a
EZ
2958 t)
2959
077decde 2960(defun woman-special-characters (to)
2d4c3c85 2961 "Process special character escapes \\(xx, \\[xxx] up to buffer position TO.
1edbbf8a 2962\(This must be done AFTER translation, which may use special characters.)"
2d4c3c85
JB
2963 (while (re-search-forward "\\\\\\(?:(\\(..\\)\\|\\[\\([[^]]+\\)\\]\\)" to t)
2964 (let* ((name (or (match-string-no-properties 1)
2965 (match-string-no-properties 2)))
2966 (replacement (assoc name woman-special-characters)))
2967 (unless
2968 (and
077decde 2969 replacement
1edbbf8a
EZ
2970 (cond ((and (cddr replacement)
2971 (if (nthcdr 3 replacement)
2972 ;; Need symbol font:
2973 (if woman-use-symbol-font
2974 (woman-replace-match (nth 2 replacement)
b66e8575 2975 'woman-symbol))
1edbbf8a
EZ
2976 ;; Need extended font:
2977 (if woman-use-extended-font
2978 (woman-replace-match (nth 2 replacement))))))
077decde 2979 ((cadr replacement) ; Use ASCII simulation
1edbbf8a 2980 (woman-replace-match (cadr replacement)))))
2d4c3c85 2981 (WoMan-warn (concat "Special character "
b616a212 2982 (if (match-beginning 1) "\\(%s" "\\[%s]")
2d4c3c85 2983 " not interpolated!") name)
25c6f63e 2984 (if woman-ignore (woman-delete-match 0))))))
077decde
EZ
2985
2986(defun woman-display-extended-fonts ()
1edbbf8a
EZ
2987 "Display table of glyphs of graphic characters and their octal codes.
2988All the octal codes in the ranges [32..127] and [160..255] are displayed
077decde 2989together with the corresponding glyphs from the default and symbol fonts.
1edbbf8a 2990Useful for constructing the alist variable `woman-special-characters'."
077decde
EZ
2991 (interactive)
2992 (with-output-to-temp-buffer "*WoMan Extended Font Map*"
86970dbd 2993 (with-current-buffer standard-output
077decde
EZ
2994 (let ((i 32))
2995 (while (< i 256)
2996 (insert (format "\\%03o " i) (string i) " " (string i))
2997 (put-text-property (1- (point)) (point)
b66e8575 2998 'face 'woman-symbol)
077decde
EZ
2999 (insert " ")
3000 (setq i (1+ i))
3001 (when (= i 128) (setq i 160) (insert "\n"))
25c6f63e 3002 (if (zerop (% i 8)) (insert "\n")))))
d5d105e8 3003 (help-print-return-message)))
077decde
EZ
3004
3005\f
3006;;; Formatting macros that do not cause a break:
3007
f7fe4837
GM
3008;; Bound locally by woman[012]-roff-buffer, and also, annoyingly and
3009;; confusingly, as a function argument. Use dynamically in
3010;; woman-unquote and woman-forward-arg.
3011(defvar woman-request)
077decde
EZ
3012
3013(defun woman-unquote (to)
3014 "Delete any double-quote characters between point and TO.
3015Leave point at TO (which should be a marker)."
3016 (let (in-quote)
3017 (while (search-forward "\"" to 1)
3018 (if (and in-quote (looking-at "\""))
3019 ;; Repeated double-quote represents single double-quote
3020 (delete-char 1)
3021 (if (or in-quote (looking-at ".*\"")) ; paired
3022 (delete-char -1))
3023 (setq in-quote (not in-quote))
3024 ))
3025 (if in-quote
f7fe4837 3026 (WoMan-warn "Unpaired \" in .%s arguments." woman-request))))
077decde
EZ
3027
3028(defsubst woman-unquote-args ()
3029 "Delete any double-quote characters up to the end of the line."
3030 (woman-unquote (save-excursion (end-of-line) (point-marker))))
3031
3032(defun woman1-roff-buffer ()
3033 "Process non-breaking requests."
3034 (let ((case-fold-search t)
f7fe4837 3035 woman-request fn woman1-unquote)
077decde
EZ
3036 (while
3037 ;; Find next control line:
3038 (re-search-forward woman-request-regexp nil t)
3039 (cond
3040 ;; Construct woman function to call:
3041 ((setq fn (intern-soft
3042 (concat "woman1-"
f7fe4837 3043 (setq woman-request (match-string 1)))))
077decde
EZ
3044 (if (get fn 'notfont) ; not a font-change request
3045 (funcall fn)
3046 ;; Delete request or macro name:
3047 (woman-delete-match 0)
3048 ;; If no args then apply to next line else unquote args
f7fe4837
GM
3049 ;; (woman1-unquote is used by called function):
3050 (setq woman1-unquote (not (eolp)))
077decde
EZ
3051 (if (eolp) (delete-char 1))
3052; ;; Hide leading control character in unquoted argument:
3053; (cond ((memq (following-char) '(?. ?'))
3054; (insert "\\&")
3055; (beginning-of-line)))
3056 ;; Call the appropriate function:
3057 (funcall fn)
3058 ;; Hide leading control character in quoted argument (only):
f7fe4837 3059 (if (and woman1-unquote (memq (following-char) '(?. ?')))
25c6f63e 3060 (insert "\\&"))))))))
077decde
EZ
3061
3062;;; Font-changing macros:
3063
3064(defun woman1-B ()
3065 ".B -- Set words of current line in bold font."
3066 (woman1-B-or-I ".ft B\n"))
3067
3068(defun woman1-I ()
3069 ".I -- Set words of current line in italic font."
3070 (woman1-B-or-I ".ft I\n"))
3071
f7fe4837
GM
3072(defvar woman1-unquote) ; bound locally by woman1-roff-buffer
3073
077decde
EZ
3074(defun woman1-B-or-I (B-or-I)
3075 ".B/I -- Set words of current line in bold/italic font.
1edbbf8a 3076B-OR-I is the appropriate complete control line."
077decde
EZ
3077 ;; Should NOT concatenate the arguments!
3078 (insert B-or-I) ; because it might be a control line
3079 ;; Return to bol to process .SM/.B, .B/.if etc.
3080 ;; or start of first arg to hide leading control char.
3081 (save-excursion
f7fe4837 3082 (if woman1-unquote
077decde
EZ
3083 (woman-unquote-args)
3084 (while (looking-at "^[.']") (forward-line))
3085 (end-of-line)
3086 (delete-horizontal-space))
3087 (insert "\\fR")))
3088
3089(defun woman1-SM ()
3090 ".SM -- Set the current line in small font, i.e. IGNORE!"
3091 nil)
3092
3093(defalias 'woman1-SB 'woman1-B)
3094;; .SB -- Set the current line in small bold font, i.e. just embolden!
1edbbf8a 3095;; (This is what /usr/local/share/groff/tmac/tmac.an does. The
077decde
EZ
3096;; Linux man.7 is wrong about this!)
3097
3098(defun woman1-BI ()
3099 ".BI -- Join words of current line alternating bold and italic fonts."
3100 (woman1-alt-fonts (list "\\fB" "\\fI")))
3101
3102(defun woman1-BR ()
3103 ".BR -- Join words of current line alternating bold and Roman fonts."
3104 (woman1-alt-fonts (list "\\fB" "\\fR")))
3105
3106(defun woman1-IB ()
3107 ".IB -- Join words of current line alternating italic and bold fonts."
3108 (woman1-alt-fonts (list "\\fI" "\\fB")))
3109
3110(defun woman1-IR ()
3111 ".IR -- Join words of current line alternating italic and Roman fonts."
3112 (woman1-alt-fonts (list "\\fI" "\\fR")))
3113
3114(defun woman1-RB ()
3115 ".RB -- Join words of current line alternating Roman and bold fonts."
3116 (woman1-alt-fonts (list "\\fR" "\\fB")))
3117
3118(defun woman1-RI ()
3119 ".RI -- Join words of current line alternating Roman and italic fonts."
3120 (woman1-alt-fonts (list "\\fR" "\\fI")))
3121
3122(defun woman1-alt-fonts (fonts)
3123 "Join words using alternating fonts in FONTS, which MUST be a dynamic list."
3124 (nconc fonts fonts) ; circular list!
3125 (insert (car fonts))
3126 ;; Return to start of first arg to hide leading control char:
3127 (save-excursion
3128 (setq fonts (cdr fonts))
f7fe4837
GM
3129 ;; woman1-unquote is bound in woman1-roff-buffer.
3130 (woman-forward-arg woman1-unquote 'concat)
077decde
EZ
3131 (while (not (eolp))
3132 (insert (car fonts))
3133 (setq fonts (cdr fonts))
f7fe4837 3134 (woman-forward-arg woman1-unquote 'concat))
25c6f63e 3135 (insert "\\fR")))
077decde
EZ
3136
3137(defun woman-forward-arg (&optional unquote concat)
1edbbf8a
EZ
3138 "Move forward over one ?roff argument, optionally unquoting and/or joining.
3139If optional arg UNQUOTE is non-nil then delete any argument quotes.
3140If optional arg CONCAT is non-nil then join arguments."
077decde
EZ
3141 (if (eq (following-char) ?\")
3142 (progn
3143 (if unquote (delete-char 1) (forward-char))
3144 (re-search-forward "\"\\|$")
3145 ;; Repeated double-quote represents single double-quote
3146 (while (eq (following-char) ?\") ; paired
3147 (if unquote (delete-char 1) (forward-char))
3148 (re-search-forward "\"\\|$"))
3149 (if (eq (preceding-char) ?\")
d355a0b7 3150 (if unquote (delete-char -1))
f7fe4837 3151 (WoMan-warn "Unpaired \" in .%s arguments." woman-request)))
077decde
EZ
3152 ;; (re-search-forward "[^\\\n] \\|$") ; inconsistent
3153 (skip-syntax-forward "^ "))
3154 (cond ((null concat) (skip-chars-forward " \t")) ; don't skip eol!
3155 ((eq concat 'noskip)) ; do not skip following whitespace
25c6f63e 3156 (t (woman-delete-following-space))))
077decde
EZ
3157
3158
1edbbf8a
EZ
3159;; The following requests are not explicit font-change requests and
3160;; so are flagged `notfont' to turn off automatic request deletion
3161;; and further processing.
077decde
EZ
3162
3163(put 'woman1-TP 'notfont t)
3164(defun woman1-TP ()
3165 ".TP -- After tag line, reset font to Roman for paragraph body."
3166 ;; Same for .IP, but forward only 1 line?
3167 (save-excursion
3168 ;; May be an `irrelevant' control line in the way, so ...
3169 (forward-line)
3170 (forward-line (if (looking-at "\\.\\S-+[ \t]*$") 2 1))
3171 ;; May be looking at control line, so ...
3172 (insert ".ft R\n")))
3173
3174(put 'woman1-ul 'notfont t)
3175(defun woman1-ul ()
3176 ".ul N -- Underline (italicize) the next N input lines, default N = 1."
3177 (let ((N (if (eolp) 1 (woman-parse-numeric-arg)))) ; woman-get-numeric-arg ?
3178 (woman-delete-whole-line)
3179 (insert ".ft I\n")
3180 (forward-line N)
25c6f63e 3181 (insert ".ft R\n")))
077decde 3182
1edbbf8a 3183;;; Other non-breaking requests:
077decde
EZ
3184
3185;; Hyphenation
3186;; Warnings commented out.
3187
3188(put 'woman1-nh 'notfont t)
3189(defun woman1-nh ()
3190 ".nh -- No hyphenation, i.e. IGNORE!"
3191 ;; Must be handled here to avoid breaking!
3192 ;; (WoMan-log-1 ".nh request ignored -- hyphenation not supported!")
3193 (woman-delete-whole-line))
3194
3195(put 'woman1-hy 'notfont t)
3196(defun woman1-hy ()
3197 ".hy N -- Set hyphenation mode to N, i.e. IGNORE!"
3198 ;; (WoMan-log-1 ".hy request ignored -- hyphenation not supported!")
3199 (woman-delete-whole-line))
3200
3201(put 'woman1-hc 'notfont t)
3202(defun woman1-hc ()
3203 ".hc c -- Set hyphenation character to c, i.e. delete it!"
3204 (let ((c (char-to-string (following-char))))
3205 ;; (WoMan-log
3206 ;; "Hyphenation character %s deleted -- hyphenation not supported!" c)
3207 (woman-delete-whole-line)
3208 (setq c (concat "\\(" c "\\)\\|^[.'][ \t]*hc"))
3209 (save-excursion
3210 (while (and (re-search-forward c nil t)
b616a212 3211 (match-beginning 1))
25c6f63e 3212 (delete-char -1)))))
077decde
EZ
3213
3214(put 'woman1-hw 'notfont t)
3215(defun woman1-hw ()
3216 ".hw words -- Set hyphenation exception words, i.e. IGNORE!"
3217 ;; (WoMan-log-1 ".hw request ignored -- hyphenation not supported!")
3218 (woman-delete-whole-line))
3219
1edbbf8a 3220;;; Other non-breaking requests correctly ignored by nroff:
077decde
EZ
3221
3222(put 'woman1-ps 'notfont t)
3223(defalias 'woman1-ps 'woman-delete-whole-line)
3224 ;; .ps -- Point size -- IGNORE!
3225
3226(put 'woman1-ss 'notfont t)
3227(defalias 'woman1-ss 'woman-delete-whole-line)
3228 ;; .ss -- Space-character size -- IGNORE!
3229
3230(put 'woman1-cs 'notfont t)
3231(defalias 'woman1-cs 'woman-delete-whole-line)
3232 ;; .cs -- Constant character space (width) mode -- IGNORE!
3233
3234(put 'woman1-ne 'notfont t)
3235(defalias 'woman1-ne 'woman-delete-whole-line)
3236 ;; .ne -- Need vertical space -- IGNORE!
3237
3238(put 'woman1-vs 'notfont t)
3239(defalias 'woman1-vs 'woman-delete-whole-line)
3240 ;; .vs -- Vertical base line spacing -- IGNORE!
3241
3242(put 'woman1-bd 'notfont t)
3243(defalias 'woman1-bd 'woman-delete-whole-line)
3244 ;; .bd -- Embolden font -- IGNORE!
3245
1edbbf8a 3246;;; Non-breaking SunOS-specific macros:
077decde
EZ
3247
3248(defun woman1-TX ()
3249 ".TX t p -- Resolve SunOS abbrev t and join to p (usually punctuation)."
3250 (insert "SunOS ")
3251 (woman-forward-arg 'unquote 'concat))
3252
3253(put 'woman1-IX 'notfont t)
3254(defalias 'woman1-IX 'woman-delete-whole-line)
3255 ;; .IX -- Index macro, for Sun internal use -- IGNORE!
3256
3257\f
3258;;; Direct font selection:
3259
3260(defconst woman-font-alist
3261 '(("R" . default)
b66e8575
MB
3262 ("I" . woman-italic)
3263 ("B" . woman-bold)
077decde
EZ
3264 ("P" . previous)
3265 ("1" . default)
b66e8575
MB
3266 ("2" . woman-italic)
3267 ("3" . woman-bold) ; used in bash.1
077decde
EZ
3268 )
3269 "Alist of ?roff font indicators and woman font variables and names.")
3270
3271(defun woman-change-fonts ()
3272 "Process font changes."
3273 ;; ***** NEEDS REVISING IF IT WORKS OK *****
3274 ;; Paragraph .LP/PP/HP/IP/TP and font .B/.BI etc. macros reset font.
3275 ;; Should .SH/.SS reset font?
3276 ;; Font size setting macros (?) should reset font.
692feebd 3277 (let ((font-alist woman-font-alist) ; for local updating
077decde
EZ
3278 (previous-pos (point))
3279 (previous-font 'default)
3280 (current-font 'default))
3281 (while
3282 ;; Find font requests, paragraph macros and font escapes:
3283 (re-search-forward
3284 "^[.'][ \t]*\\(\\(\\ft\\)\\|\\(.P\\)\\)\\|\\(\\\\f\\)" nil 1)
a353f3f4 3285 (let (font beg notfont fescape)
077decde 3286 ;; Match font indicator and leave point at end of sequence:
b616a212 3287 (cond ((match-beginning 2)
077decde
EZ
3288 ;; .ft request found
3289 (setq beg (match-beginning 0))
3290 (skip-chars-forward " \t")
3291 (if (eolp) ; default is previous font
3292 (setq font previous-font)
3293 (looking-at "[^ \t\n]+"))
3294 (forward-line)) ; end of control line and \n
b616a212 3295 ((match-beginning 3)
077decde
EZ
3296 ;; Macro that resets font found
3297 (setq font 'default))
b616a212 3298 ((match-beginning 4)
077decde 3299 ;; \f escape found
a353f3f4
GM
3300 (setq beg (match-beginning 0)
3301 fescape t)
2d4c3c85 3302 (woman-match-name))
077decde 3303 (t (setq notfont t)))
25c6f63e 3304 (unless notfont
077decde
EZ
3305 ;; Get font name:
3306 (or font
3307 (let ((fontstring (match-string 0)))
692feebd
EZ
3308 (setq font (assoc fontstring font-alist)
3309 ;; NB: font-alist contains VARIABLE NAMES.
077decde
EZ
3310 font (if font
3311 (cdr font)
3312 (WoMan-warn "Unknown font %s." fontstring)
3313 ;; Output this message once only per call ...
692feebd 3314 (setq font-alist
b66e8575 3315 (cons (cons fontstring 'woman-unknown)
692feebd 3316 font-alist))
b66e8575 3317 'woman-unknown)
077decde
EZ
3318 )))
3319 ;; Delete font control line or escape sequence:
3320 (cond (beg (delete-region beg (point))
3321 (if (eq font 'previous) (setq font previous-font))))
a353f3f4
GM
3322 ;; Deal with things like \fB.cvsrc\fR at the start of a line.
3323 ;; After removing the font control codes, this would
3324 ;; otherwise match woman-request-regexp. The "\\&" which is
3325 ;; inserted to prevent this is removed by woman2-process-escapes.
3326 (and fescape
11c5be91 3327 (looking-at woman-request-regexp)
a353f3f4 3328 (insert "\\&"))
077decde
EZ
3329 (woman-set-face previous-pos (point) current-font)
3330 (if beg
3331 ;; Explicit font control
3332 (setq previous-pos (point)
3333 previous-font current-font)
3334 ;; Macro that resets font
3335 ;; (forward-line) ; DOES NOT WORK! but unnecessary?
3336 ;; Must process font changes in any paragraph tag!
3337 (setq previous-pos (point)
3338 previous-font 'default))
3339 (setq current-font font)
3340 )))
3341 ;; Set font after last request up to eob:
25c6f63e 3342 (woman-set-face previous-pos (point) current-font)))
077decde
EZ
3343
3344(defun woman-set-face (from to face)
3345 "Set the face of the text from FROM to TO to face FACE.
3346Ignore the default face and underline only word characters."
3347 (or (eq face 'default) ; ignore
3348 (not woman-fontify)
3349 (if (face-underline-p face)
3350 (save-excursion
3351 (let ((face-no-ul (intern (concat (symbol-name face) "-no-ul"))))
3352 (goto-char from)
3353 (while (< (point) to)
3354 (skip-syntax-forward "w" to)
3355 (put-text-property from (point) 'face face)
3356 (setq from (point))
3357 (skip-syntax-forward "^w" to)
3358 (put-text-property from (point) 'face face-no-ul)
3359 (setq from (point))
3360 )))
25c6f63e 3361 (put-text-property from to 'face face))))
077decde
EZ
3362
3363\f
3364;;; Output translation:
3365
f7fe4837
GM
3366;; This is only set by woman2-tr. It is bound locally in woman2-roff-buffer.
3367;; It is also used by woman-translate. woman-translate may be called
3368;; outside the scope of woman2-roff-buffer (by experiment). Therefore
3369;; this used to be globally bound to nil, to avoid an error. Instead
3370;; we can use bound-and-true-p in woman-translate.
3371(defvar woman-translations)
081e8d65 3372;; A list of the form (\"[ace]\" (a . b) (c . d) (e . ?\s)) or nil.
077decde
EZ
3373
3374(defun woman-get-next-char ()
3375 "Return and delete next char in buffer, including special chars."
3376 (if ;;(looking-at "\\\\(\\(..\\)")
2d4c3c85
JB
3377 ;; Match special \(xx and strings \*[xxx], \*(xx, \*x:
3378 (looking-at "\\\\\\((..\\|\\*\\(\\[[^]]+\\]\\|(..\\|.\\)\\)")
077decde
EZ
3379 (prog1 (match-string 0)
3380 (woman-delete-match 0))
3381 (prog1 (char-to-string (following-char))
3382 (delete-char 1))))
3383
3384(defun woman2-tr (to)
3385 ".tr abcde -- Translate a -> b, c -> d, ..., e -> space.
1edbbf8a
EZ
3386Format paragraphs upto TO. Supports special chars.
3387\(Breaks, but should not.)"
077decde
EZ
3388 ;; This should be an update, but consing onto the front of the alist
3389 ;; has the same effect and match duplicates should not matter.
3390 ;; Initialize translation data structures:
f7fe4837
GM
3391 (let ((matches (car woman-translations))
3392 (alist (cdr woman-translations))
077decde
EZ
3393 a b)
3394 ;; `matches' must be a string:
3395 (setq matches
3396 (concat (if matches (substring matches 1 -1)) "]"))
3397 ;; Process .tr arguments:
3398 (while (not (eolp)) ; (looking-at "[ \t]*$") ???
3399 (setq a (woman-get-next-char))
3400 (if (eolp)
3401 (setq b " ")
3402 (setq b (woman-get-next-char)))
3403 (setq matches
3404 (if (= (length a) 1)
3405 (concat a matches)
3406 (concat matches "\\|\\" a))
3407 alist (cons (cons a b) alist)))
3408 (delete-char 1) ; no blank line
3409 ;; Rebuild translations list:
3410 (setq matches
3411 (if (= (string-to-char matches) ?\])
3412 (substring matches 3)
3413 (concat "[" matches))
f7fe4837 3414 woman-translations (cons matches alist))
077decde 3415 ;; Format any following text:
25c6f63e 3416 (woman2-format-paragraphs to)))
077decde
EZ
3417
3418(defsubst woman-translate (to)
3419 "Translate up to marker TO. Do this last of all transformations."
f7fe4837
GM
3420 (if (bound-and-true-p woman-translations)
3421 (let ((matches (car woman-translations))
3422 (alist (cdr woman-translations))
bb25c8d7
GM
3423 ;; Translations are case-sensitive, eg ".tr ab" does not
3424 ;; affect "A" (bug#6849).
3425 (case-fold-search nil))
077decde
EZ
3426 (while (re-search-forward matches to t)
3427 ;; Done like this to retain text properties and
3428 ;; support translation of special characters:
3429 (insert-before-markers-and-inherit
3430 (cdr (assoc
3431 (buffer-substring-no-properties
3432 (match-beginning 0) (match-end 0))
3433 alist)))
25c6f63e 3434 (woman-delete-match 0)))))
077decde
EZ
3435
3436\f
3437;;; Registers:
3438
3439(defvar woman-registers ; these are all read-only
3440 '((".H" 24) (".V" 48) ; resolution in basic units
3441 (".g" 0) ; not groff
3442 ;; (Iff emulating groff need to implement groff italic correction
3443 ;; \/, e.g. for pic.1)
3444 (".i" left-margin) ; current indent
3445 (".j" woman-adjust) ; current adjustment
3446 (".l" fill-column) ; current line length
3447 (".s" 12) ; current point size
3448 (".u" (if woman-nofill 0 1)) ; 1/0 in fill/nofill mode
3449 (".v" 48) ; current vertical line spacing
3450 )
3451 "Register alist: the key is the register name as a string.
3452Each element has the form (KEY VALUE . INC) -- inc may be nil.
3453Also bound locally in `woman2-roff-buffer'.")
3454
3455(defun woman-mark-horizonal-position ()
3456 "\\kx -- Store current horizontal position in INPUT LINE in register x."
3457 (while (re-search-forward "\\\\k\\(.\\)" nil t)
3458 (goto-char (match-beginning 0))
3459 (setq woman-registers
3460 (cons (list (match-string 1) (current-column))
3461 woman-registers))
3462 (woman-delete-match 0)))
3463
3464(defsubst woman2-process-escapes-to-eol (&optional numeric)
3465 "Process remaining escape sequences up to eol.
3466Handle numeric arguments specially if optional argument NUMERIC is non-nil."
8cc8239e 3467 (woman2-process-escapes (copy-marker (line-end-position) t) numeric))
077decde
EZ
3468
3469(defun woman2-nr (to)
1edbbf8a 3470 ".nr R +/-N M -- Assign +/-N (wrt to previous value, if any) to register R.
077decde 3471The increment for auto-incrementing is set to M.
1edbbf8a 3472Format paragraphs upto TO. (Breaks, but should not!)"
077decde
EZ
3473 (let* ((name (buffer-substring
3474 (point)
3475 (progn (skip-syntax-forward "^ ") (point))))
3476 (pm (progn ; increment
3477 (skip-chars-forward " \t")
3478 (when (memq (char-after) '(?+ ?-))
3479 (forward-char) (char-before))))
3480 (value (if (eolp) ; no value
3481 nil ; to be interpreted as zero
3482 (woman2-process-escapes-to-eol 'numeric)
3483 (woman-parse-numeric-arg)))
3484 (inc (progn ; auto-increment
3485 (skip-chars-forward " \t")
3486 (if (eolp) ; no value
3487 nil ; to be interpreted as zero ???
3488 (woman-parse-numeric-arg))))
3489 (oldvalue (assoc name woman-registers)))
3490 (when oldvalue
3491 (setq oldvalue (cdr oldvalue)) ; (value . inc)
3492 (unless inc (setq inc (cdr oldvalue))))
3493 (cond ((null value)
3494 (setq value 0) ; correct?
3495 (WoMan-warn "nr %s -- null value assigned as zero!" name))
3496 ((symbolp value)
3497 (setq value (list 'quote value))))
3498 (if pm ; increment old value
3499 (setq oldvalue (if oldvalue (car oldvalue) 0)
3500 value (if (eq pm ?+)
3501 (+ oldvalue value)
3502 (- oldvalue value))))
3503 (setq woman-registers
3504 (cons (cons name (cons value inc)) woman-registers))
3505 (woman-delete-whole-line)
3506 (woman2-format-paragraphs to)))
3507
3508\f
3509;;; Numeric (and "non-text") request arguments:
3510
3511(defsubst woman-get-numeric-arg ()
1edbbf8a 3512 "Get the value of a numeric argument at or after point.
077decde 3513The argument can include the width function and scale indicators.
1edbbf8a 3514Assumes 10 characters per inch. Does not move point."
077decde
EZ
3515 (woman2-process-escapes-to-eol 'numeric)
3516 (save-excursion (woman-parse-numeric-arg)))
3517
3518(defun woman-parse-numeric-arg ()
3519 "Get the value of a numeric expression at or after point.
3520Unlike `woman-get-numeric-arg', leaves point after the argument.
3521The expression may be an argument in quotes."
3522 (if (= (following-char) ?\") (forward-char))
3523 ;; Allow leading +/-:
3524 (let ((value (if (looking-at "[+-]") 0 (woman-parse-numeric-value)))
3525 op)
3526 (while (cond
3527 ((looking-at "[+-/*%]") ; arithmetic operators
3528 (forward-char)
3529 (setq op (intern-soft (match-string 0)))
3530 (setq value (funcall op value (woman-parse-numeric-value))))
3531 ((looking-at "[<=>]=?") ; relational operators
3532 (goto-char (match-end 0))
1e1cbbbf
GM
3533 (setq op (intern-soft
3534 (if (string-equal (match-string 0) "==")
3535 "="
3536 (match-string 0))))
077decde
EZ
3537 (setq value (if (funcall op value (woman-parse-numeric-value))
3538 1 0)))
3539 ((memq (setq op (following-char)) '(?& ?:)) ; Boolean and / or
3540 (forward-char)
3541 (setq value
3542 ;; and / or are special forms, not functions, in ELisp
3543 (if (eq op ?&)
3544 ;; and
3545 (if (> value 0)
3546 (if (> (woman-parse-numeric-value) 0) 1 0)
3547 ;; skip second operand
3548 (prog1 0 (woman-parse-numeric-value)))
3549 ;; or
3550 (if (> value 0)
3551 ;; skip second operand
3552 (prog1 1 (woman-parse-numeric-value))
3553 (if (> (woman-parse-numeric-value) 0) 1 0))
3554 )))
3555 ))
3556; (if (looking-at "[ \t\nRC\)\"]") ; R, C are tab types
3557; ()
3558; (WoMan-warn "Unimplemented numerical operator `%c' in %s"
3559; (following-char)
3560; (buffer-substring
5ed619e0
GM
3561; (line-beginning-position)
3562; (line-end-position)))
077decde
EZ
3563; (skip-syntax-forward "^ "))
3564 value
3565 ))
3566
3567(defun woman-parse-numeric-value ()
3568 "Get a single numeric value at or after point.
1edbbf8a
EZ
3569The value can be a number register or width function (which assumes 10
3570characters per inch) and can include scale indicators. It may be an
3571expression in parentheses. Leaves point after the value."
077decde
EZ
3572 ;; Must replace every \' by some different single character first
3573 ;; before calling this function by calling
3574 ;; (woman2-process-escapes-to-eol 'numeric)
3575 (if (eq (following-char) ?\()
3576 ;; Treat parenthesized expression as a single value.
3577 (let (n)
3578 (forward-char)
3579 (setq n (woman-parse-numeric-arg))
54eea618 3580 (skip-syntax-forward " " (line-end-position))
077decde
EZ
3581 (if (eq (following-char) ?\))
3582 (forward-char)
3583 (WoMan-warn "Parenthesis confusion in numeric expression!"))
3584 n)
3585 (let ((n (cond ((looking-at "[-+]?[.0-9]+") ; single number
3586 ;; currently needed to set match-end, even though
3587 ;; string-to-number returns 0 if number not parsed.
3588 (string-to-number (match-string 0)))
2d4c3c85
JB
3589 ((looking-at "\\\\n\\([-+]\\)?\\(?:\
3590\\[\\([^]]+\\)\\]\\|\(\\(..\\)\\|\\(.\\)\\)")
077decde
EZ
3591 ;; interpolate number register, maybe auto-incremented
3592 (let* ((pm (match-string-no-properties 1))
2d4c3c85
JB
3593 (name (or (match-string-no-properties 2)
3594 (match-string-no-properties 3)
077decde
EZ
3595 (match-string-no-properties 4)))
3596 (value (assoc name woman-registers)))
3597 (if value
3598 (let (inc)
3599 (setq value (cdr value) ; (value . inc)
3600 inc (cdr value)
3601 ;; eval internal (.X) registers
3602 ;; stored as lisp variable names:
3603 value (eval (car value)))
3604 (if (and pm inc) ; auto-increment
3605 (setq value
3606 (funcall (intern-soft pm) value inc)
3607 woman-registers
3608 (cons (cons name (cons value inc))
3609 woman-registers)))
3610 value)
3611 (WoMan-warn "Undefined register %s defaulted to 0."
3612 name)
3613 0) ; default to zero
3614 ))
3615 ((re-search-forward
2d4c3c85
JB
3616 ;; Delimiter can be special char escape \[xxx],
3617 ;; \(xx or single normal char (usually '):
3618 "\\=\\\\w\\(\\\\\\[[^]]+\\]\\|\\\\(..\\|.\\)" nil t)
077decde
EZ
3619 (let ((from (match-end 0))
3620 (delim (regexp-quote (match-string 1))))
3621 (if (re-search-forward delim nil t)
3622 ;; Return width of string:
3623 (- (match-beginning 0) from)
25c6f63e 3624 (WoMan-warn "Width escape delimiter error!")))))))
077decde
EZ
3625 (if (null n)
3626 ;; ERROR -- should handle this better!
3627 (progn
3628 (WoMan-warn "Numeric/register argument error: %s"
3629 (buffer-substring
3630 (point)
5ed619e0 3631 (line-end-position)))
54eea618 3632 (skip-syntax-forward "^ " (line-end-position))
077decde
EZ
3633 0)
3634 (goto-char (match-end 0))
3635 ;; Check for scale factor:
3636 (if
3637 (cond
3638 ((looking-at "\\s ") nil) ; stay put!
3639 ((looking-at "[mnuv]")) ; ignore for now
3640 ((looking-at "i") (setq n (* n 10))) ; inch
3641 ((looking-at "c") (setq n (* n 3.9))) ; cm
45745577
KR
3642 ((let ((case-fold-search nil))
3643 (looking-at "P"))
3644 (setq n (* n 1.7))) ; Pica
077decde
EZ
3645 ((looking-at "p") (setq n (* n 0.14))) ; point
3646 ;; NB: May be immediately followed by + or -, etc.,
3647 ;; in which case do nothing and return nil.
3648 )
3649 (goto-char (match-end 0)))
25c6f63e 3650 (if (numberp n) (round n) n)))))
077decde
EZ
3651
3652\f
3653;;; VERTICAL FORMATTING -- Formatting macros that cause a break:
3654
1edbbf8a
EZ
3655;; Vertical spacing philosophy:
3656;; Delete all vertical space as it is encountered. Then insert
3657;; vertical space only before text, as required.
077decde
EZ
3658
3659(defun woman2-roff-buffer ()
3660 "Process breaks. Format paragraphs and headings."
3661 (let ((case-fold-search t)
3662 (to (make-marker))
3663 (canonically-space-region
3664 (symbol-function 'canonically-space-region))
3665 (insert-and-inherit (symbol-function 'insert-and-inherit))
3666 (set-text-properties (symbol-function 'set-text-properties))
3667 (woman-registers woman-registers)
f7fe4837 3668 fn woman-request woman-translations
077decde 3669 tab-stop-list)
8cc8239e 3670 (set-marker-insertion-type to t)
077decde
EZ
3671 ;; ?roff does not squeeze multiple spaces, but does fill, so...
3672 (fset 'canonically-space-region 'ignore)
3673 ;; Try to avoid spaces inheriting underlines from preceding text!
3674 (fset 'insert-and-inherit (symbol-function 'insert))
3675 (fset 'set-text-properties 'ignore)
3676 (unwind-protect
3677 (while
3678 ;; Find next control line:
a353f3f4
GM
3679 (re-search-forward woman-request-regexp nil t)
3680 (cond
3681 ;; Construct woman function to call:
3682 ((setq fn (intern-soft
3683 (concat "woman2-"
f7fe4837 3684 (setq woman-request (match-string 1)))))
a353f3f4
GM
3685 ;; Delete request or macro name:
3686 (woman-delete-match 0))
e1dbe924 3687 ;; Unrecognized request:
a353f3f4 3688 ((prog1 nil
f7fe4837
GM
3689 ;; (WoMan-warn ".%s request ignored!" woman-request)
3690 (WoMan-warn-ignored woman-request "ignored!")
a353f3f4
GM
3691 ;; (setq fn 'woman2-LP)
3692 ;; AVOID LEAVING A BLANK LINE!
3693 ;; (setq fn 'woman2-format-paragraphs)
3694 ))
3695 ;; .LP assumes it is at eol and leaves a (blank) line,
3696 ;; so leave point at end of line before paragraph:
3697 ((or (looking-at "[ \t]*$") ; no argument
3698 woman-ignore) ; ignore all
3699 ;; (beginning-of-line) (kill-line)
3700 ;; AVOID LEAVING A BLANK LINE!
3701 (beginning-of-line) (woman-delete-line 1))
3702 (t (end-of-line) (insert ?\n))
3703 )
3704 (if (not (or fn
3705 (and (not (memq (following-char) '(?. ?')))
3706 (setq fn 'woman2-format-paragraphs))))
3707 ()
3708 ;; Find next control line:
081e8d65
MV
3709 (if (equal woman-request "TS")
3710 (set-marker to (woman-find-next-control-line "TE"))
3711 (set-marker to (woman-find-next-control-line)))
a353f3f4
GM
3712 ;; Call the appropriate function:
3713 (funcall fn to)))
077decde 3714 (if (not (eobp)) ; This should not happen, but ...
8cc8239e
SM
3715 (woman2-format-paragraphs (copy-marker (point-max) t)
3716 woman-left-margin))
077decde
EZ
3717 (fset 'canonically-space-region canonically-space-region)
3718 (fset 'set-text-properties set-text-properties)
3719 (fset 'insert-and-inherit insert-and-inherit)
3720 (set-marker to nil))))
3721
081e8d65
MV
3722(defun woman-find-next-control-line (&optional pat)
3723 "Find and return start of next control line.
3724PAT, if non-nil, specifies an additional component of the control
3725line regexp to search for, which is appended to the default
3726regexp, \"\\(\\\\c\\)?\\n[.']\"."
3727 (let ((pattern (concat "\\(\\\\c\\)?\n[.']" pat))
3728 to)
077decde
EZ
3729 (save-excursion
3730 ;; Must handle
3731 ;; ...\c
3732 ;; .br (and other requests?)
3733 ;; by deleting both the \c and the following request.
3734 ;; BEWARE THAT THIS CODE MAY BE UNRELIABLE!!!!!
3735 (while
3736 (and
081e8d65 3737 (setq to (re-search-forward pattern nil t))
b616a212 3738 (match-beginning 1)
077decde
EZ
3739 (looking-at "br"))
3740 (goto-char (match-beginning 0))
3741 (woman-delete-line 2)))
081e8d65
MV
3742 (if to
3743 (- to (+ 1 (length pat)))
3744 (point-max))))
077decde
EZ
3745
3746(defun woman2-PD (to)
3747 ".PD d -- Set the interparagraph distance to d.
1edbbf8a 3748Round to whole lines, default 1 line. Format paragraphs upto TO.
2d4c3c85 3749\(Breaks, but should not.)"
077decde
EZ
3750 ;; .ie \\n[.$] .nr PD (v;\\$1)
3751 ;; .el .nr PD .4v>?\n[.V]
3752 (woman-set-interparagraph-distance)
3753 (woman2-format-paragraphs to))
3754
3755(defun woman-set-interparagraph-distance ()
1edbbf8a 3756 "Set the interparagraph distance from a .PD request at point."
077decde
EZ
3757 (setq woman-interparagraph-distance
3758 (if (eolp) 1 (woman-get-numeric-arg)))
3759 ;; Should allow .PD 0 to set zero line spacing
3760 (woman-delete-line 1)) ; ignore remaining args
3761
3762(defsubst woman-interparagraph-space ()
1edbbf8a 3763 "Set variable `woman-leave-blank-lines' from `woman-interparagraph-distance'."
25c6f63e 3764 (setq woman-leave-blank-lines woman-interparagraph-distance))
077decde
EZ
3765
3766(defun woman2-TH (to)
1edbbf8a
EZ
3767 ".TH n c x v m -- Begin a man page. Format paragraphs upto TO.
3768n is the name of the page in chapter c\; x is extra commentary\;
3769v alters page foot left; m alters page head center.
3770\(Should set prevailing indent and tabs to 5.)"
077decde
EZ
3771 (woman-forward-arg 'unquote 'concat)
3772 (insert ?\()
3773 (woman-forward-arg 'unquote 'concat)
3774 (insert ?\))
3775 (let ((start (point)) here)
3776 (while (not (eolp))
3777 (cond ((looking-at "\"\"[ \t]")
25c6f63e 3778 (delete-char 2)))
077decde
EZ
3779 (delete-horizontal-space)
3780 (setq here (point))
3781 (insert " -- ")
3782 (woman-forward-arg 'unquote 'concat)
3783 ;; Delete repeated arguments:
532a0f4c 3784 (if (string-equal (buffer-substring here (point))
077decde 3785 (buffer-substring start here))
e1cd9b5b 3786 (delete-region here (point)))))
077decde 3787 ;; Embolden heading (point is at end of heading):
5ed619e0 3788 (woman-set-face (line-beginning-position) (point) 'woman-bold)
077decde
EZ
3789 (forward-line)
3790 (delete-blank-lines)
3791 (setq woman-left-margin woman-default-indent)
3792 (setq woman-prevailing-indent woman-default-indent)
3793 (woman2-format-paragraphs to woman-left-margin))
3794
3795(defun woman2-SH (to)
1edbbf8a
EZ
3796 ".SH -- Sub-head. Leave blank line and subhead.
3797Format paragraphs upto TO. Set prevailing indent to 5."
077decde
EZ
3798 (if (eolp) ; If no args then
3799 (delete-char 1) ; apply to next line
3800 (woman-unquote-args) ; else unquote to end of heading
3801 (beginning-of-line))
3802 (woman2-process-escapes-to-eol)
3803 (woman-leave-blank-lines woman-interparagraph-distance)
3804 (setq woman-leave-blank-lines nil)
3805 ;; Optionally embolden heading (point is at beginning of heading):
3806 (if woman-bold-headings
5ed619e0 3807 (woman-set-face (point) (line-end-position) 'woman-bold))
077decde
EZ
3808 (forward-line)
3809 (setq woman-left-margin woman-default-indent
3810 woman-nofill nil) ; fill output lines
3811 (setq woman-prevailing-indent woman-default-indent)
3812 (woman2-format-paragraphs to woman-left-margin))
3813
3814(defun woman2-SS (to)
1edbbf8a
EZ
3815 ".SS -- Sub-sub-head. Like .SH but indent heading 3 spaces.
3816Format paragraphs upto TO."
077decde
EZ
3817 (if (eolp) ; If no args then
3818 (delete-char 1)) ; apply to next line.
3819 (insert " ")
3820 (beginning-of-line)
3821 (woman2-SH to))
3822
3823(defun woman2-LP (to)
1edbbf8a
EZ
3824 ".LP,.PP -- Begin paragraph. Set prevailing indent to 5.
3825Leave 1 blank line. Format paragraphs upto TO."
077decde
EZ
3826 (woman-delete-line 1) ; ignore any arguments
3827 (woman-interparagraph-space)
3828 (setq woman-prevailing-indent woman-default-indent)
3829 (woman2-format-paragraphs to woman-left-margin))
3830
3831(defalias 'woman2-PP 'woman2-LP)
3832(defalias 'woman2-P 'woman2-LP)
3833
3834(defun woman2-ns (to)
1edbbf8a 3835 ".ns -- Turn on no-space mode. Format paragraphs upto TO."
077decde
EZ
3836 ;; Should not cause a break!
3837 (woman-delete-line 1) ; ignore argument(s)
3838 (setq woman-nospace t)
3839 (woman2-format-paragraphs to))
3840
3841(defun woman2-rs (to)
1edbbf8a 3842 ".rs -- Turn off no-space mode. Format paragraphs upto TO."
077decde
EZ
3843 ;; Should not cause a break!
3844 (woman-delete-line 1) ; ignore argument(s)
3845 (setq woman-nospace nil)
3846 (woman2-format-paragraphs to))
3847
3848(defun woman2-sp (to)
1edbbf8a 3849 ".sp N -- If N > 0 then leave 1 blank line. Format paragraphs upto TO."
077decde
EZ
3850 (let ((N (if (eolp) 1 (woman-get-numeric-arg))))
3851 (if (>= N 0)
3852 (woman-delete-line 1) ; ignore argument(s)
3853 (setq woman-negative-vertical-space t)
3854 (insert ".sp ")
3855 (forward-line))
3856 (setq woman-leave-blank-lines N)
3857 (woman2-format-paragraphs to)))
3858
3859(defun woman-negative-vertical-space (from)
3860 ".sp N with N < 0 => overlap following with preceding lines at FROM."
3861 ;; Run by woman-decode-region if necessary -- not usually required.
3862 (WoMan-warn "Negative vertical spacing support is experimental!")
3863 (goto-char from)
3864 (while
3865 ;; Find next control line:
3866 (re-search-forward "^\\.sp " nil t)
3867 (let ((N (woman-get-numeric-arg))
3868 overlap overwritten)
3869 (woman-delete-whole-line)
3870 (setq from (point)
3871 overlap (buffer-substring from
3872 (progn (forward-line (- N)) (point))))
3873 (delete-region from (point))
3874 (forward-line N)
3875 (let ((imax (length overlap))
3876 (i 0) c)
3877 (while (< i imax)
3878 (setq c (aref overlap i))
3879 (cond ((eq c ?\n) ; skip
3880 (forward-line))
3881 ((eolp) ; extend line
3882 ;; Insert character INCLUDING TEXT PROPERTIES:
3883 ;; (insert (substring overlap i (1+ i)))
3884 (let ((eol (string-match "\n" overlap i)))
3885 (insert (substring overlap i eol))
3886 (setq i (or eol imax)))
3887 )
081e8d65 3888 ((eq c ?\s) ; skip
077decde
EZ
3889 (forward-char))
3890 ((eq c ?\t) ; skip
3891 (if (eq (following-char) ?\t)
3892 (forward-char) ; both tabs, just skip
86970dbd
SM
3893 (dotimes (i woman-tab-width)
3894 (if (eolp)
081e8d65 3895 (insert ?\s) ; extend line
86970dbd 3896 (forward-char)) ; skip
077decde
EZ
3897 )))
3898 (t
081e8d65 3899 (if (or (eq (following-char) ?\s) ; overwrite OK
077decde
EZ
3900 overwritten) ; warning only once per ".sp -"
3901 ()
3902 (setq overwritten t)
3903 (WoMan-warn
3904 "Character(s) overwritten by negative vertical spacing in line %d"
3905 (count-lines 1 (point))))
3906 (delete-char 1) (insert (substring overlap i (1+ i)))))
25c6f63e 3907 (setq i (1+ i)))))))
077decde
EZ
3908
3909\f
3910;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3911;; The following function should probably do ALL width and number
3912;; register interpolation.
3913;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3914
3915(defun woman2-process-escapes (to &optional numeric)
3916 "Process remaining escape sequences up to marker TO, preserving point.
3917Optional argument NUMERIC, if non-nil, means the argument is numeric."
a464a6c7 3918 (cl-assert (and (markerp to) (marker-insertion-type to)))
077decde
EZ
3919 ;; The first two cases below could be merged (maybe)!
3920 (let ((from (point)))
3921 ;; Discard zero width filler character used to hide leading dots
d5704d66
CY
3922 ;; and zero width characters.
3923 (while (re-search-forward "\\\\[&|^]" to t)
3924 (woman-delete-match 0)
3925 ;; If on a line by itself, consume newline as well (Bug#3651).
87a92845
KR
3926 ;; But not in a .nf region, preserve all newlines in that case.
3927 (and (not woman-nofill)
3928 (eq (char-before (match-beginning 0)) ?\n)
d5704d66
CY
3929 (eq (char-after (match-beginning 0)) ?\n)
3930 (delete-char 1)))
3931
077decde
EZ
3932 (goto-char from)
3933 ;; Interrupt text processing -- CONTINUE current text with the
3934 ;; next text line (after any control lines, unless processing to
3935 ;; eol):
3936 (while (re-search-forward "\\\\c.*\n?" to t)
3937 (woman-delete-match 0))
3938 ;; but do not delete the final newline ...
3939 (if (and (or (eobp) (= (point) to)) (not (bolp)))
3940 (insert-before-markers ?\n))
3941 (goto-char from)
3942 (woman-translate to)
3943 (goto-char from)
3944 (woman-special-characters to)
3945 (goto-char from)
3946 ;; Printable version of the current escape character, ASSUMED to be `\'
3947 ;; This must be done LAST of all escape processing!
3948 ;; Done like this to preserve any text properties of the `\'
3949 (while (search-forward "\\" to t)
3950 (let ((c (following-char)))
c940224f
CY
3951 ;; Some other escapes, such as \f, are handled in
3952 ;; `woman0-process-escapes'.
077decde
EZ
3953 (cond ((eq c ?') ; \' -> '
3954 (delete-char -1)
3955 (cond (numeric ; except in numeric args, \' -> `
3956 (delete-char 1)
3957 (insert ?`))))
3958 ((eq c ?\( )) ; uninterpreted special character
25c6f63e 3959 ; \(.. -- do nothing
077decde
EZ
3960 ((eq c ?t) ; non-interpreted tab \t
3961 (delete-char 1)
3962 (delete-char -1)
3963 (insert "\t"))
3964 ((and numeric
3965 (memq c '(?w ?n ?h)))) ; leave \w, \n, \h (?????)
c940224f 3966 ((eq c ?l) (woman-horizontal-line)))))
077decde
EZ
3967 (goto-char from)
3968 ;; Process non-default tab settings:
3969 (cond (tab-stop-list
3970 (while (search-forward "\t" to t)
3971 (woman-tab-to-tab-stop))
3972 (goto-char from)))
3973
3974 ;; Must replace \' by something before processing \w, done above.
3975
3976 ;; Replace all `\w' and `\n' escapes:
3977 ;; (This may be a bit too recursive!)
3978 (while (re-search-forward "\\\\[nw]" to t)
3979 (let ((from (match-beginning 0)) N)
3980 (goto-char from)
3981 (setq N (woman-parse-numeric-value))
3982 (delete-region from (point))
3983 ;; Interpolate value:
3984 (insert-before-markers (number-to-string N))))
3985 (goto-char from)))
3986
3987(defun woman-horizontal-line ()
3988 "\\l'Nc' -- Draw a horizontal line of length N using character c, default _."
3989 (delete-char -1)
3990 (delete-char 1)
3991 (looking-at "\\(.\\)\\(.*\\)\\1")
8cc8239e 3992 (forward-char 1)
da37d01f 3993 (let* ((to (match-end 2))
8cc8239e
SM
3994 (from (match-beginning 0))
3995 (N (woman-parse-numeric-arg))
3996 (c (if (< (point) to) (following-char) ?_)))
077decde
EZ
3997 (delete-region from to)
3998 (delete-char 1)
25c6f63e 3999 (insert (make-string N c))))
077decde
EZ
4000
4001;;; 4. Text Filling, Adjusting, and Centering
4002
4003(defun woman2-br (to)
1edbbf8a 4004 ".br -- Break. Leave no blank line. Format paragraphs upto TO."
077decde
EZ
4005 (woman-delete-line 1) ; ignore any arguments
4006 (woman2-format-paragraphs to))
4007
4008(defun woman2-fi (to)
1edbbf8a
EZ
4009 ".fi -- Fill subsequent output lines. Leave no blank line.
4010Format paragraphs upto TO."
077decde
EZ
4011 (setq woman-nofill nil)
4012 (woman-delete-line 1) ; ignore any arguments
4013 ;; Preserve any final blank line in the nofill region:
4014 (save-excursion
4015 (forward-line -1)
4016 (if (looking-at "[ \t]*$") (setq woman-leave-blank-lines 1)))
4017 (woman2-format-paragraphs to))
4018
4019(defun woman2-nf (to)
1edbbf8a
EZ
4020 ".nf -- Nofill. Subsequent lines are neither filled nor adjusted.
4021Input text lines are copied directly to output lines without regard
25c6f63e 4022for the current line length. Format paragraphs up to TO."
077decde
EZ
4023 (setq woman-nofill t)
4024 (woman-delete-line 1) ; ignore any arguments
4025 (woman2-format-paragraphs to))
4026
4027(defun woman2-ad (to)
1edbbf8a
EZ
4028 ".ad c -- Line adjustment is begun (once fill mode is on).
4029Set justification mode to c if specified.
4030Format paragraphs upto TO. (Breaks, but should not.)"
077decde
EZ
4031 ;; c = l -- left, r -- right, c -- center, b or n -- both,
4032 ;; absent -- unchanged. Initial mode adj,both.
4033 (setq woman-adjust
4034 (cond ((eolp) woman-adjust-previous)
4035 ((eq (following-char) ?l) woman-adjust-left)
4036 ((eq (following-char) ?r) woman-adjust-right)
4037 ((eq (following-char) ?c) woman-adjust-center)
4038 ((memq (following-char) '(?b ?n)) woman-adjust-both)
4039 (t (woman-get-numeric-arg))
4040 )
4041 woman-justify (nth woman-adjust woman-justify-list))
4042 (woman-delete-line 1) ; ignore any remaining arguments
4043 (woman2-format-paragraphs to))
4044
4045(defun woman2-na (to)
1edbbf8a 4046 ".na -- No adjusting. Format paragraphs upto TO.
2d4c3c85 4047\(Breaks, but should not.)"
077decde
EZ
4048 (setq woman-adjust-previous woman-adjust
4049 woman-justify-previous woman-justify
4050 woman-adjust woman-adjust-left ; fill but do not adjust
4051 woman-justify (nth woman-adjust woman-justify-list))
4052 (woman-delete-line 1) ; ignore any arguments
4053 (woman2-format-paragraphs to))
4054
4055;;; The main formatting functions:
4056
4057(defun woman-leave-blank-lines (&optional leave)
4058 "Delete all blank lines around point.
1edbbf8a
EZ
4059Leave one blank line if optional argument LEAVE is non-nil and
4060non-zero, or if LEAVE is nil and variable `woman-leave-blank-lines' is
4061non-nil and non-zero."
077decde
EZ
4062 ;; ***** It may suffice to delete only lines ABOVE point! *****
4063 ;; NOTE: Function arguments are evaluated left to right
4064 ;; (*note (elisp)Function Forms::.).
4065 (delete-region
4066 (save-excursion
4067 (if (not (eq (skip-syntax-backward " ") 0))
4068 (forward-line)) ; forward-char ?
4069 (point))
4070 (progn (skip-syntax-forward " ")
4071 (beginning-of-line)
4072 (point)))
25c6f63e 4073 (unless woman-nospace
077decde
EZ
4074 (if (or (null leave) (eq leave 0))
4075 ;; output any `pending' vertical space ...
4076 (setq leave woman-leave-blank-lines))
25c6f63e
CY
4077 (if (and leave (> leave 0)) (insert-before-markers ?\n)))
4078 (setq woman-leave-blank-lines nil))
077decde
EZ
4079
4080;; `fill-region-as-paragraph' in `fill.el' appears to be the principal
4081;; text filling function, so that is what I use here.
4082
4083(defvar woman-temp-indent nil)
4084
4085(defun woman2-format-paragraphs (to &optional new-left)
1edbbf8a
EZ
4086 "Indent, fill and adjust paragraphs upto TO to current left margin.
4087If optional arg NEW-LEFT is non-nil then reset current left margin.
4088If `woman-nofill' is non-nil then indent without filling or adjusting."
077decde
EZ
4089 ;; Blank space should only ever be output before text.
4090 (if new-left (setq left-margin new-left))
4091 (if (looking-at "^\\s *$")
4092 ;; A blank line should leave a space like .sp 1 (p. 14).
4093 (setq woman-leave-blank-lines 1))
4094 (skip-syntax-forward " ")
4095 ;; Successive control lines are sufficiently common to be worth a
4096 ;; special case (maybe):
25c6f63e 4097 (unless (>= (point) to)
077decde 4098 (woman-reset-nospace)
077decde
EZ
4099 (woman2-process-escapes to 'numeric)
4100 (if woman-nofill
4101 ;; Indent without filling or adjusting ...
4102 (progn
4103 (woman-leave-blank-lines)
25c6f63e
CY
4104 (when woman-temp-indent
4105 (indent-to woman-temp-indent)
4106 (forward-line))
077decde 4107 (indent-rigidly (point) to left-margin)
25c6f63e 4108 (woman-horizontal-escapes to))
077decde
EZ
4109 ;; Fill and justify ...
4110 ;; Blank lines and initial spaces cause a break.
077decde
EZ
4111 (while (< (point) to)
4112 (woman-leave-blank-lines)
4113 (let ((from (point)))
4114 ;; Indent first lin of paragraph:
4115 (indent-to (or woman-temp-indent left-margin))
4116 (woman-horizontal-escapes to) ; 7 October 1999
4117 ;; Find the beginning of the next paragraph:
4118 (forward-line)
077decde
EZ
4119 (and (re-search-forward "\\(^\\s *$\\)\\|\\(^\\s +\\)" to 1)
4120 ;; A blank line should leave a space like .sp 1 (p. 14).
4121 (eolp)
4122 (skip-syntax-forward " ")
4123 (setq woman-leave-blank-lines 1))
7e69f904
CY
4124 ;; This shouldn't happen, but in case it does (e.g. for
4125 ;; badly-formatted manfiles with no terminating newline),
4126 ;; avoid an infinite loop.
4127 (unless (and (eolp) (eobp))
4128 (beginning-of-line))
077decde
EZ
4129 ;; If a single short line then just leave it.
4130 ;; This is necessary to preserve some table layouts.
4131 ;; PROBABLY NOT NECESSARY WITH SQUEEZE MODIFICATION !!!!!
25c6f63e
CY
4132 (when (or (> (count-lines from (point)) 1)
4133 (save-excursion
4134 (backward-char)
4135 (> (current-column) fill-column)))
4136 ;; NOSQUEEZE has no effect if JUSTIFY is full, so redefine
4137 ;; canonically-space-region, see above.
4138 (if (and woman-temp-indent (< woman-temp-indent left-margin))
4139 (let ((left-margin woman-temp-indent))
4140 (fill-region-as-paragraph from (point) woman-justify)
077decde 4141 (save-excursion
25c6f63e
CY
4142 (goto-char from)
4143 (forward-line)
4144 (setq from (point)))))
4145 (fill-region-as-paragraph from (point) woman-justify)))))
4146 (setq woman-temp-indent nil)))
077decde
EZ
4147
4148\f
4149;;; Tagged, indented and hanging paragraphs:
4150
4151(defun woman2-TP (to)
1edbbf8a 4152 ".TP i -- Set prevailing indent to i. Format paragraphs upto TO.
077decde
EZ
4153Begin indented paragraph with hanging tag given by next text line.
4154If tag doesn't fit, place it on a separate line."
4155 (let ((i (woman2-get-prevailing-indent)))
4156 (woman-leave-blank-lines woman-interparagraph-distance)
4157 (woman2-tagged-paragraph to i)))
4158
4159(defun woman2-IP (to)
1edbbf8a 4160 ".IP x i -- Same as .TP with tag x. Format paragraphs upto TO."
077decde
EZ
4161 (woman-interparagraph-space)
4162 (if (eolp) ; no args
4163 ;; Like LP without resetting prevailing indent
4164 (woman2-format-paragraphs to (+ woman-left-margin
4165 woman-prevailing-indent))
4166 (woman-forward-arg 'unquote)
4167 (let ((i (woman2-get-prevailing-indent 'leave-eol)))
4168 (beginning-of-line)
4169 (woman-leave-blank-lines) ; must be here,
a353f3f4
GM
4170 ;;
4171 ;; The cvs.1 manpage contains some (possibly buggy) syntax that
4172 ;; confuses woman, although the man program displays it ok.
4173 ;; Most problems are caused by IP followed by another request on
4174 ;; the next line. Without the following hack, the second request
4175 ;; gets displayed raw in the output. Note that
4176 ;; woman2-tagged-paragraph also contains a hack for similar
4177 ;; issues (eg IP followed by SP).
4178 ;;
4179 ;; i) For IP followed by one or more IPs, we ignore all but the
4180 ;; last (mimic man). The hack in w-t-p would only work for two
4181 ;; consecutive IPs, and would use the first.
4182 ;; ii) For IP followed by SP followed by one or more requests,
4183 ;; do nothing. At least in cvs.1, there is usually another IP in
4184 ;; there somewhere.
4185 (unless (or (looking-at "^\\.IP")
4186 (and (looking-at "^\\.sp")
4187 (save-excursion
4188 (and (zerop (forward-line 1))
4189 (looking-at woman-request-regexp)))))
4190 (woman2-tagged-paragraph to i)))))
077decde
EZ
4191
4192(defun woman-find-next-control-line-carefully ()
4193 "Find and return start of next control line, even if already there!"
4194 (if (looking-at "^[.']")
4195 (point)
4196 (woman-find-next-control-line)))
4197
4198(defun woman2-tagged-paragraph (to i)
1edbbf8a
EZ
4199 "Begin indented paragraph with hanging tag given by current text line.
4200If tag doesn't fit, leave it on separate line.
4201Format paragraphs upto TO. Set prevailing indent to I."
077decde
EZ
4202 (if (not (looking-at "\\s *$")) ; non-empty tag
4203 (setq woman-leave-blank-lines nil))
4204
a353f3f4
GM
4205 ;; Temporary hack for bash.1, cvs.1 and groff_mmse.7 until code is revised
4206 ;; to process all requests uniformly.
4207 ;; This hack deals with IP requests followed by other requests (eg
4208 ;; SP) on the very next line. We skip over the SP, otherwise it gets
4209 ;; inserted raw in the rendered output.
4210 (cond ((and (= (point) to)
4211 (looking-at "^[.'][ \t]*\\(PD\\|br\\|ta\\|sp\\) *"))
4212 (if (member (match-string 1) '("br" "sp"))
4213 (woman-delete-line 1)
4214 (woman-delete-match 0)
4215 (if (string= (match-string 1) "ta") ; for GetInt.3
4216 (woman2-ta to)
4217 (woman-set-interparagraph-distance)))
25c6f63e 4218 (set-marker to (woman-find-next-control-line-carefully))))
077decde
EZ
4219
4220 (let ((tag (point)))
4221 (woman-reset-nospace)
4222 ;; Format the tag:
4223 (woman2-process-escapes-to-eol)
4224 ;; TIDY UP THE FOLLOWING CODE
4225 ;; (indent-to woman-left-margin)
4226 (setq left-margin woman-left-margin)
4227 (forward-line)
4228 (fill-region-as-paragraph (save-excursion (forward-line -1) (point))
4229 (point) woman-justify)
4230
4231 ;; Temporary hack for bash.1 until all requests processed uniformly:
4232 (cond ((and (= (point) to) (looking-at "^[.'][ \t]*PD *"))
4233 (woman-delete-match 0)
4234 (woman-set-interparagraph-distance)
4235 (set-marker to (woman-find-next-control-line-carefully))
4236 ))
4237
4238 ;; Format the paragraph body, if there is one! Set indented left
4239 ;; margin anyway, because the paragraph body may begin with a
4240 ;; control line:
4241 (setq left-margin (+ woman-left-margin i))
4242 (cond ((< (point) to)
4243 (woman2-format-paragraphs to)
4244 (goto-char tag) (end-of-line)
4245 (cond ((> (setq i (- left-margin (current-column))) 0)
4246 (delete-char 1)
4247 (delete-horizontal-space)
4248 ;; Necessary to avoid spaces inheriting underlines.
4249 ;; Cannot simply delete (current-column) whitespace
4250 ;; characters because some may be tabs!
86970dbd 4251 (insert-char ?\s i)))
25c6f63e 4252 (goto-char to)))))
077decde
EZ
4253
4254(defun woman2-HP (to)
1edbbf8a 4255 ".HP i -- Set prevailing indent to i. Format paragraphs upto TO.
077decde
EZ
4256Begin paragraph with hanging indent."
4257 (let ((i (woman2-get-prevailing-indent)))
4258 (woman-interparagraph-space)
4259 (setq woman-temp-indent woman-left-margin)
25c6f63e 4260 (woman2-format-paragraphs to (+ woman-left-margin i))))
077decde
EZ
4261
4262(defun woman2-get-prevailing-indent (&optional leave-eol)
1edbbf8a
EZ
4263 "Set prevailing indent to integer argument at point, and return it.
4264If no argument then return the existing prevailing indent.
077decde
EZ
4265Delete line from point and eol unless LEAVE-EOL is non-nil."
4266 (if (eolp)
4267 (or leave-eol (delete-char 1))
4268 (let ((i (woman-get-numeric-arg)))
4269 (woman-delete-line) (or leave-eol (delete-char 1))
4270 ;; i = 0 if the argument was not a number
a353f3f4 4271 ;; FIXME should this be >= 0? How else to reset to 0 indent?
077decde
EZ
4272 (if (> i 0) (setq woman-prevailing-indent i))))
4273 woman-prevailing-indent)
4274
4275(defmacro woman-push (value stack)
4276 "Push VALUE onto STACK."
4277 `(setq ,stack (cons ,value ,stack)))
4278
4279(defmacro woman-pop (variable stack)
1edbbf8a 4280 "Pop into VARIABLE the value at the top of STACK.
077decde
EZ
4281Allow for mismatched requests!"
4282 `(if ,stack
4283 (setq ,variable (car ,stack)
4284 ,stack (cdr ,stack))))
4285
4286(defun woman2-RS (to)
1edbbf8a
EZ
4287 ".RS i -- Start relative indent, move left margin in distance i.
4288Set prevailing indent to 5 for nested indents. Format paragraphs upto TO."
077decde
EZ
4289 (woman-push woman-left-margin woman-RS-left-margin)
4290 (woman-push woman-prevailing-indent woman-RS-prevailing-indent)
4291 (setq woman-left-margin (+ woman-left-margin
4292 (woman2-get-prevailing-indent))
4293 woman-prevailing-indent woman-default-indent)
4294 (woman2-format-paragraphs to woman-left-margin))
4295
4296(defun woman2-RE (to)
1edbbf8a 4297 ".RE -- End of relative indent. Format paragraphs upto TO.
077decde
EZ
4298Set prevailing indent to amount of starting .RS."
4299 (woman-pop woman-left-margin woman-RS-left-margin)
4300 (woman-pop woman-prevailing-indent woman-RS-prevailing-indent)
4301 (woman-delete-line 1) ; ignore any arguments
4302 (woman2-format-paragraphs to woman-left-margin))
4303
4304\f
4305;;; Line Length and Indenting:
4306
4307(defun woman-set-arg (arg &optional previous)
1edbbf8a
EZ
4308 "Reset, increment or decrement argument ARG, which must be quoted.
4309If no argument then use value of optional arg PREVIOUS if non-nil,
4310otherwise set PREVIOUS. Delete the whole remaining control line."
077decde
EZ
4311 (if (eolp) ; space already skipped
4312 (set arg (if previous (eval previous) 0))
4313 (if previous (set previous (eval arg)))
4314 (woman2-process-escapes-to-eol 'numeric)
4315 (let ((pm (if (looking-at "[+-]")
4316 (prog1 (following-char)
4317 (forward-char 1))))
4318 (i (woman-parse-numeric-arg)))
4319 (cond ((null pm) (set arg i))
4320 ((= pm ?+) (set arg (+ (eval arg) i)))
4321 ((= pm ?-) (set arg (- (eval arg) i)))
4322 ))
4323 (beginning-of-line))
4324 (woman-delete-line 1)) ; ignore any remaining arguments
4325
4326;; NEED TO RATIONALIZE NAMES FOR PREVIOUS VALUES!
4327(defvar woman-ll-fill-column woman-fill-column)
4328(defvar woman-in-left-margin woman-left-margin)
4329
4330(defun woman2-ll (to)
1edbbf8a
EZ
4331 ".ll +/-N -- Set, increment or decrement line length.
4332Format paragraphs upto TO. (Breaks, but should not.)"
077decde
EZ
4333 (woman-set-arg 'fill-column 'woman-ll-fill-column)
4334 (woman2-format-paragraphs to))
4335
4336(defun woman2-in (to)
1edbbf8a
EZ
4337 ".in +/-N -- Set, increment or decrement the indent.
4338Format paragraphs upto TO."
077decde
EZ
4339 (woman-set-arg 'left-margin 'woman-in-left-margin)
4340 (woman2-format-paragraphs to))
4341
4342(defun woman2-ti (to)
1edbbf8a 4343 ".ti +/-N -- Temporary indent. Format paragraphs upto TO."
077decde
EZ
4344 ;; Ignore if no argument.
4345 ;; Indent next output line only wrt current indent.
4346 ;; Current indent is not changed.
4347 (setq woman-temp-indent left-margin)
4348 (woman-set-arg 'woman-temp-indent)
4349 (woman2-format-paragraphs to nil))
4350
4351\f
4352;;; Tabs, Leaders, and Fields:
4353
4354(defun woman2-ta (to)
1edbbf8a
EZ
4355 ".ta Nt ... -- Set tabs, left type, unless t=R(right), C(centered).
4356\(Breaks, but should not.) The tab stops are separated by spaces\;
4357a value preceded by + represents an increment to the previous stop value.
4358Format paragraphs upto TO."
077decde
EZ
4359 (setq tab-stop-list nil)
4360 (woman2-process-escapes-to-eol 'numeric)
4361 (save-excursion
4362 (let ((tab-stop 0))
4363 (while (not (eolp))
4364 (let ((plus (cond ((eq (following-char) ?+) (forward-char 1) t)))
4365 (i (woman-parse-numeric-arg)))
4366 (setq tab-stop (if plus (+ tab-stop i) i)))
4367 (if (memq (following-char) '(?R ?C))
4368 (setq tab-stop (cons tab-stop (following-char))))
4369 (setq tab-stop-list (cons tab-stop tab-stop-list))
4370 (skip-syntax-forward "^ ") ; skip following R, C, `;', etc.
4371 (skip-chars-forward " \t")
4372 )))
4373 (woman-delete-line 1) ; ignore any remaining arguments
4374 (setq tab-stop-list (reverse tab-stop-list))
4375 (woman2-format-paragraphs to))
4376
06b60517
JB
4377(defsubst woman-get-tab-stop (tab-stops)
4378 "If TAB-STOPS is a cons, return its car, else return TAB-STOPS."
4379 (if (consp tab-stops) (car tab-stops) tab-stops))
077decde
EZ
4380
4381(defun woman-tab-to-tab-stop ()
4382 "Insert spaces to next defined tab-stop column.
1edbbf8a
EZ
4383The variable `tab-stop-list' is a list whose elements are either left
4384tab stop columns or pairs (COLUMN . TYPE) where TYPE is R or C."
077decde
EZ
4385 ;; Based on tab-to-tab-stop in indent.el.
4386 ;; R & C tabs probably not quite right!
d355a0b7 4387 (delete-char -1)
077decde
EZ
4388 (let ((tabs tab-stop-list))
4389 (while (and tabs (>= (current-column)
4390 (woman-get-tab-stop (car tabs))))
4391 (setq tabs (cdr tabs)))
4392 (if tabs
4393 (let* ((tab (car tabs))
4394 (type (and (consp tab) (cdr tab)))
4395 eol n)
4396 (if type
4397 (setq tab (woman-get-tab-stop tab)
5ed619e0 4398 eol (line-end-position)
077decde
EZ
4399 n (save-excursion
4400 (search-forward "\t" eol t))
4401 n (- (if n (1- n) eol) (point))
4402 tab (- tab (if (eq type ?C) (/ n 2) n))) )
4403 (setq n (- tab (current-column)))
86970dbd 4404 (insert-char ?\s n))
081e8d65 4405 (insert ?\s))))
077decde
EZ
4406
4407(defun woman2-DT (to)
1edbbf8a
EZ
4408 ".DT -- Restore default tabs. Format paragraphs upto TO.
4409\(Breaks, but should not.)"
077decde
EZ
4410 ;; Currently just terminates special tab processing.
4411 (setq tab-stop-list nil)
4412 (woman-delete-line 1) ; ignore any arguments
4413 (woman2-format-paragraphs to))
4414
4415(defun woman2-fc (to)
1edbbf8a
EZ
4416 ".fc a b -- Set field delimiter a and pad character b.
4417Format paragraphs upto TO.
077decde
EZ
4418A VERY FIRST ATTEMPT to make fields at least readable!
4419Needs doing properly!"
4420 (if (eolp)
4421 (woman-delete-whole-line) ; ignore!
4422 (let ((delim (following-char))
081e8d65 4423 (pad ?\s) end) ; pad defaults to space
077decde
EZ
4424 (forward-char)
4425 (skip-chars-forward " \t")
4426 (or (eolp) (setq pad (following-char)))
4427 (woman-delete-whole-line)
4428 (save-excursion
4429 (if (re-search-forward "^[.'][ \t]*fc\\s " nil t)
4430 (setq end (match-beginning 0))))
4431 ;; A field is contained between a pair of field delimiter
4432 ;; characters and consists of sub-strings separated by padding
4433 ;; indicator characters:
4434 (setq delim (string delim ?[ ?^ delim ?] ?* delim))
4435 (save-excursion
4436 (while (re-search-forward delim end t)
4437 (goto-char (match-beginning 0))
4438 (delete-char 1)
4439 (insert woman-unpadded-space-char)
4440 (goto-char (match-end 0))
d355a0b7 4441 (delete-char -1)
077decde
EZ
4442 (insert-before-markers woman-unpadded-space-char)
4443 (subst-char-in-region
4444 (match-beginning 0) (match-end 0)
25c6f63e 4445 pad woman-unpadded-space-char t)))))
077decde
EZ
4446 (woman2-format-paragraphs to))
4447
4448\f
2d4c3c85
JB
4449;;; Preliminary table support (.TS/.TE)
4450
4451(defun woman2-TS (to)
4452 ".TS -- Start of table code for the tbl processor.
4453Format paragraphs upto TO."
2d4c3c85
JB
4454 (when woman-emulate-tbl
4455 ;; Assumes column separator is \t and intercolumn spacing is 3.
4456 ;; The first line may optionally be a list of options terminated by
4457 ;; a semicolon. Currently, just delete it:
4458 (if (looking-at ".*;[ \t]*$") (woman-delete-line 1)) ;
4459 ;; The following lines must specify the format of each line of the
4460 ;; table and end with a period. Currently, just delete them:
4461 (while (not (looking-at ".*\\.[ \t]*$")) (woman-delete-line 1))
4462 (woman-delete-line 1)
4463 ;; For each column, find its width and align it:
4464 (let ((start (point)) (col 1))
081e8d65
MV
4465 (WoMan-log "%s" (buffer-substring start (+ start 40)))
4466 ;; change T{ T} to tabs
4467 (while (search-forward "T{\n" to t)
4468 (replace-match "")
4469 (catch 'end
4470 (while (search-forward "\n" to t)
4471 (replace-match " ")
4472 (if (looking-at "T}")
4473 (progn
4474 (delete-char 2)
4475 (throw 'end t))))))
4476 (goto-char start)
4477 ;; strip space and headers
4478 (while (re-search-forward "^\\.TH\\|\\.sp" to t)
4479 (woman-delete-whole-line))
4480 (goto-char start)
2d4c3c85
JB
4481 (while (prog1 (search-forward "\t" to t) (goto-char start))
4482 ;; Find current column width:
4483 (while (< (point) to)
4484 (when (search-forward "\t" to t)
4485 (backward-char)
4486 (if (> (current-column) col) (setq col (current-column))))
4487 (forward-line))
4488 ;; Align current column:
4489 (goto-char start)
4490 (setq col (+ col 3)) ; intercolumn space
4491 (while (< (point) to)
4492 (when (search-forward "\t" to t)
4493 (delete-char -1)
081e8d65 4494 (insert-char ?\s (- col (current-column))))
2d4c3c85 4495 (forward-line))
081e8d65
MV
4496 (goto-char start))
4497 ;; find maximum width
4498 (let ((max-col 0))
4499 (while (search-forward "\n" to t)
4500 (backward-char)
4501 (if (> (current-column) max-col)
4502 (setq max-col (current-column)))
4503 (forward-char))
4504 (goto-char start)
4505 ;; break lines if they are too long
4506 (when (and (> max-col woman-fill-column)
4507 (> woman-fill-column col))
4508 (setq max-col woman-fill-column)
4509 (woman-break-table col to start)
4510 (goto-char start))
4511 (while (re-search-forward "^_$" to t)
4512 (replace-match (make-string max-col ?_)))
2d4c3c85
JB
4513 (goto-char start))))
4514 ;; Format table with no filling or adjusting (cf. woman2-nf):
4515 (setq woman-nofill t)
4516 (woman2-format-paragraphs to))
4517
4518(defalias 'woman2-TE 'woman2-fi)
4519 ;; ".TE -- End of table code for the tbl processor."
4520 ;; Turn filling and adjusting back on.
4521
081e8d65
MV
4522(defun woman-break-table (start-column to start)
4523 (while (< (point) to)
4524 (move-to-column woman-fill-column)
4525 (if (eolp)
4526 (forward-line)
4527 (if (and (search-backward " " start t)
4528 (> (current-column) start-column))
4529 (progn
4530 (insert-char ?\n 1)
4531 (insert-char ?\s (- start-column 5)))
4532 (forward-line)))))
2d4c3c85 4533\f
077decde
EZ
4534;;; WoMan message logging:
4535
1edbbf8a
EZ
4536;; The basis for this logging code was shamelessly pirated from bytecomp.el
4537;; by Jamie Zawinski <jwz@lucid.com> & Hallvard Furuseth <hbf@ulrik.uio.no>
077decde 4538
077decde
EZ
4539(defun WoMan-log-begin ()
4540 "Log the beginning of formatting in *WoMan-Log*."
4541 (let ((WoMan-current-buffer (buffer-name)))
86970dbd 4542 (with-current-buffer (get-buffer-create "*WoMan-Log*")
077decde
EZ
4543 (or (eq major-mode 'view-mode) (view-mode 1))
4544 (setq buffer-read-only nil)
4545 (goto-char (point-max))
4546 (insert "\n\^L\nFormatting "
4547 (if (stringp WoMan-current-file)
4548 (concat "file " WoMan-current-file)
4549 (concat "buffer " WoMan-current-buffer))
4550 " at " (current-time-string) "\n")
25c6f63e 4551 (setq WoMan-Log-header-point-max (point-max)))))
077decde
EZ
4552
4553(defun WoMan-log (format &rest args)
4554 "Log a message out of FORMAT control string and optional ARGS."
4555 (WoMan-log-1 (apply 'format format args)))
4556
4557(defun WoMan-warn (format &rest args)
4558 "Log a warning message out of FORMAT control string and optional ARGS."
4559 (setq format (apply 'format format args))
4560 (WoMan-log-1 (concat "** " format)))
4561
f7fe4837 4562;; request is not used dynamically by any callees.
077decde
EZ
4563(defun WoMan-warn-ignored (request ignored)
4564 "Log a warning message about ignored directive REQUEST.
4565IGNORED is a string appended to the log message."
4566 (let ((tail
4567 (buffer-substring (point)
5ed619e0 4568 (line-end-position))))
077decde 4569 (if (and (> (length tail) 0)
081e8d65 4570 (/= (string-to-char tail) ?\s))
077decde
EZ
4571 (setq tail (concat " " tail)))
4572 (WoMan-log-1
4573 (concat "** " request tail " request " ignored))))
4574
4575(defun WoMan-log-end (time)
4576 "Log the end of formatting in *WoMan-Log*.
4577TIME specifies the time it took to format the man page, to be printed
4578with the message."
b9444d97 4579 (WoMan-log-1 (format "Formatting time %g seconds." time) 'end))
077decde
EZ
4580
4581(defun WoMan-log-1 (string &optional end)
4582 "Log a message STRING in *WoMan-Log*.
1edbbf8a
EZ
4583If optional argument END is non-nil then make buffer read-only after
4584logging the message."
86970dbd 4585 (with-current-buffer (get-buffer-create "*WoMan-Log*")
e2a80284 4586 (setq buffer-read-only nil)
077decde
EZ
4587 (goto-char (point-max))
4588 (or end (insert " ")) (insert string "\n")
4589 (if end
4590 (setq buffer-read-only t)
4591 (if woman-show-log
4592 (select-window ; to return to
4593 (prog1 (selected-window) ; WoMan window
4594 (select-window (display-buffer (current-buffer)))
4595 (cond (WoMan-Log-header-point-max
4596 (goto-char WoMan-Log-header-point-max)
4597 (forward-line -1)
25c6f63e 4598 (recenter 0))))))))
077decde
EZ
4599 nil) ; for woman-file-readable-p etc.
4600
45be326a 4601;;; Bookmark Woman support.
e44fa724
KF
4602(declare-function bookmark-make-record-default
4603 "bookmark" (&optional no-file no-context posn))
398a825b
SM
4604(declare-function bookmark-prop-get "bookmark" (bookmark prop))
4605(declare-function bookmark-default-handler "bookmark" (bmk))
4606(declare-function bookmark-get-bookmark-record "bookmark" (bmk))
4607
4608;; FIXME: woman.el and man.el should be better integrated so, for
4609;; example, bookmarks of one can be used with the other.
45be326a
TV
4610
4611(defun woman-bookmark-make-record ()
4612 "Make a bookmark entry for a Woman buffer."
398a825b 4613 `(,(Man-default-bookmark-title)
e44fa724 4614 ,@(bookmark-make-record-default 'no-file)
ebb9641f 4615 (location . ,(concat "woman " woman-last-file-name))
398a825b
SM
4616 ;; Use the same form as man's bookmarks, as much as possible.
4617 (man-args . ,woman-last-file-name)
4618 (handler . woman-bookmark-jump)))
45be326a 4619
398a825b 4620;;;###autoload
45be326a
TV
4621(defun woman-bookmark-jump (bookmark)
4622 "Default bookmark handler for Woman buffers."
398a825b
SM
4623 (let* ((file (bookmark-prop-get bookmark 'man-args))
4624 ;; FIXME: we need woman-find-file-noselect, since
4625 ;; save-window-excursion can't protect us from the case where
4626 ;; woman-find-file creates a new frame.
45be326a
TV
4627 (buf (save-window-excursion
4628 (woman-find-file file) (current-buffer))))
4629 (bookmark-default-handler
4630 `("" (buffer . ,buf) . ,(bookmark-get-bookmark-record bookmark)))))
4631
077decde
EZ
4632(provide 'woman)
4633
8aa187e7
GM
4634\f
4635;; Local Variables:
4636;; coding: utf-8
4637;; End:
4638
077decde 4639;;; woman.el ends here