ob-eval: Do not use read-only mode, switch to compilation mode only once
[org-mode/org-tableheadings.git] / lisp / ox-man.el
blobc8c999857e302781bd88ee897a28d494af5c7dbd
1 ;; ox-man.el --- Man Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2014 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Luis R Anaya <papoanaya aroba hot mail punto com>
7 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This library implements a Man back-end for Org generic exporter.
28 ;; To test it, run
30 ;; M-: (org-export-to-buffer 'man "*Test Man*") RET
32 ;; in an org-mode buffer then switch to the buffer to see the Man
33 ;; export. See ox.el for more details on how this exporter works.
35 ;; It introduces one new buffer keywords:
36 ;; "MAN_CLASS_OPTIONS".
38 ;;; Code:
40 (require 'ox)
42 (eval-when-compile (require 'cl))
44 (defvar org-export-man-default-packages-alist)
45 (defvar org-export-man-packages-alist)
46 (defvar orgtbl-exp-regexp)
50 ;;; Define Back-End
52 (org-export-define-backend 'man
53 '((babel-call . org-man-babel-call)
54 (bold . org-man-bold)
55 (center-block . org-man-center-block)
56 (clock . org-man-clock)
57 (code . org-man-code)
58 (comment . (lambda (&rest args) ""))
59 (comment-block . (lambda (&rest args) ""))
60 (drawer . org-man-drawer)
61 (dynamic-block . org-man-dynamic-block)
62 (entity . org-man-entity)
63 (example-block . org-man-example-block)
64 (export-block . org-man-export-block)
65 (export-snippet . org-man-export-snippet)
66 (fixed-width . org-man-fixed-width)
67 (footnote-definition . org-man-footnote-definition)
68 (footnote-reference . org-man-footnote-reference)
69 (headline . org-man-headline)
70 (horizontal-rule . org-man-horizontal-rule)
71 (inline-babel-call . org-man-inline-babel-call)
72 (inline-src-block . org-man-inline-src-block)
73 (inlinetask . org-man-inlinetask)
74 (italic . org-man-italic)
75 (item . org-man-item)
76 (keyword . org-man-keyword)
77 (line-break . org-man-line-break)
78 (link . org-man-link)
79 (node-property . org-man-node-property)
80 (paragraph . org-man-paragraph)
81 (plain-list . org-man-plain-list)
82 (plain-text . org-man-plain-text)
83 (planning . org-man-planning)
84 (property-drawer . org-man-property-drawer)
85 (quote-block . org-man-quote-block)
86 (radio-target . org-man-radio-target)
87 (section . org-man-section)
88 (special-block . org-man-special-block)
89 (src-block . org-man-src-block)
90 (statistics-cookie . org-man-statistics-cookie)
91 (strike-through . org-man-strike-through)
92 (subscript . org-man-subscript)
93 (superscript . org-man-superscript)
94 (table . org-man-table)
95 (table-cell . org-man-table-cell)
96 (table-row . org-man-table-row)
97 (target . org-man-target)
98 (template . org-man-template)
99 (timestamp . org-man-timestamp)
100 (underline . org-man-underline)
101 (verbatim . org-man-verbatim)
102 (verse-block . org-man-verse-block))
103 :export-block "MAN"
104 :menu-entry
105 '(?m "Export to MAN"
106 ((?m "As MAN file" org-man-export-to-man)
107 (?p "As PDF file" org-man-export-to-pdf)
108 (?o "As PDF file and open"
109 (lambda (a s v b)
110 (if a (org-man-export-to-pdf t s v b)
111 (org-open-file (org-man-export-to-pdf nil s v b)))))))
112 :options-alist
113 '((:man-class "MAN_CLASS" nil nil t)
114 (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
115 (:man-header-extra "MAN_HEADER" nil nil newline)
116 ;; Other variables.
117 (:man-tables-centered nil nil org-man-tables-centered)
118 (:man-tables-verbatim nil nil org-man-tables-verbatim)
119 (:man-table-scientific-notation nil nil org-man-table-scientific-notation)
120 (:man-source-highlight nil nil org-man-source-highlight)
121 (:man-source-highlight-langs nil nil org-man-source-highlight-langs)))
125 ;;; User Configurable Variables
127 (defgroup org-export-man nil
128 "Options for exporting Org mode files to Man."
129 :tag "Org Export Man"
130 :group 'org-export)
132 ;;; Tables
134 (defcustom org-man-tables-centered t
135 "When non-nil, tables are exported in a center environment."
136 :group 'org-export-man
137 :version "24.4"
138 :package-version '(Org . "8.0")
139 :type 'boolean)
141 (defcustom org-man-tables-verbatim nil
142 "When non-nil, tables are exported verbatim."
143 :group 'org-export-man
144 :version "24.4"
145 :package-version '(Org . "8.0")
146 :type 'boolean)
149 (defcustom org-man-table-scientific-notation "%sE%s"
150 "Format string to display numbers in scientific notation.
151 The format should have \"%s\" twice, for mantissa and exponent
152 \(i.e. \"%s\\\\times10^{%s}\").
154 When nil, no transformation is made."
155 :group 'org-export-man
156 :version "24.4"
157 :package-version '(Org . "8.0")
158 :type '(choice
159 (string :tag "Format string")
160 (const :tag "No formatting")))
163 ;;; Inlinetasks
164 ;; Src blocks
166 (defcustom org-man-source-highlight nil
167 "Use GNU source highlight to embellish source blocks "
168 :group 'org-export-man
169 :version "24.4"
170 :package-version '(Org . "8.0")
171 :type 'boolean)
174 (defcustom org-man-source-highlight-langs
175 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
176 (scheme "scheme")
177 (c "c") (cc "cpp") (csharp "csharp") (d "d")
178 (fortran "fortran") (cobol "cobol") (pascal "pascal")
179 (ada "ada") (asm "asm")
180 (perl "perl") (cperl "perl")
181 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
182 (java "java") (javascript "javascript")
183 (tex "latex")
184 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
185 (ocaml "caml") (caml "caml")
186 (sql "sql") (sqlite "sql")
187 (html "html") (css "css") (xml "xml")
188 (bat "bat") (bison "bison") (clipper "clipper")
189 (ldap "ldap") (opa "opa")
190 (php "php") (postscript "postscript") (prolog "prolog")
191 (properties "properties") (makefile "makefile")
192 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
193 "Alist mapping languages to their listing language counterpart.
194 The key is a symbol, the major mode symbol without the \"-mode\".
195 The value is the string that should be inserted as the language
196 parameter for the listings package. If the mode name and the
197 listings name are the same, the language does not need an entry
198 in this list - but it does not hurt if it is present."
199 :group 'org-export-man
200 :version "24.4"
201 :package-version '(Org . "8.0")
202 :type '(repeat
203 (list
204 (symbol :tag "Major mode ")
205 (string :tag "Listings language"))))
209 (defvar org-man-custom-lang-environments nil
210 "Alist mapping languages to language-specific Man environments.
212 It is used during export of src blocks by the listings and
213 man packages. For example,
215 \(setq org-man-custom-lang-environments
216 '\(\(python \"pythoncode\"\)\)\)
218 would have the effect that if org encounters begin_src python
219 during man export."
223 ;;; Compilation
225 (defcustom org-man-pdf-process
226 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
227 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
228 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
230 "Commands to process a Man file to a PDF file.
231 This is a list of strings, each of them will be given to the
232 shell as a command. %f in the command will be replaced by the
233 full file name, %b by the file base name (i.e. without directory
234 and extension parts) and %o by the base directory of the file.
237 By default, Org uses 3 runs of to do the processing.
239 Alternatively, this may be a Lisp function that does the
240 processing. This function should accept the file name as
241 its single argument."
242 :group 'org-export-pdf
243 :group 'org-export-man
244 :version "24.4"
245 :package-version '(Org . "8.0")
246 :type '(choice
247 (repeat :tag "Shell command sequence"
248 (string :tag "Shell command"))
249 (const :tag "2 runs of pdfgroff"
250 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
251 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
252 (const :tag "3 runs of pdfgroff"
253 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
254 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
255 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
256 (function)))
258 (defcustom org-man-logfiles-extensions
259 '("log" "out" "toc")
260 "The list of file extensions to consider as Man logfiles."
261 :group 'org-export-man
262 :version "24.4"
263 :package-version '(Org . "8.0")
264 :type '(repeat (string :tag "Extension")))
266 (defcustom org-man-remove-logfiles t
267 "Non-nil means remove the logfiles produced by PDF production.
268 These are the .aux, .log, .out, and .toc files."
269 :group 'org-export-man
270 :version "24.4"
271 :package-version '(Org . "8.0")
272 :type 'boolean)
276 ;;; Internal Functions
278 (defun org-man--caption/label-string (element info)
279 "Return caption and label Man string for ELEMENT.
281 INFO is a plist holding contextual information. If there's no
282 caption nor label, return the empty string.
284 For non-floats, see `org-man--wrap-label'."
285 (let ((label (org-element-property :label element))
286 (main (org-export-get-caption element))
287 (short (org-export-get-caption element t)))
288 (cond ((and (not main) (not label)) "")
289 ((not main) (format "\\fI%s\\fP" label))
290 ;; Option caption format with short name.
291 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
292 (org-export-data short info)
293 (org-export-data main info)))
294 ;; Standard caption format.
295 (t (format "\\fR%s\\fP" (org-export-data main info))))))
297 (defun org-man--wrap-label (element output)
298 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
299 This function shouldn't be used for floats. See
300 `org-man--caption/label-string'."
301 (let ((label (org-element-property :name element)))
302 (if (or (not output) (not label) (string= output "") (string= label ""))
303 output
304 (concat (format "%s\n.br\n" label) output))))
308 ;;; Template
310 (defun org-man-template (contents info)
311 "Return complete document string after Man conversion.
312 CONTENTS is the transcoded contents string. INFO is a plist
313 holding export options."
314 (let* ((title (org-export-data (plist-get info :title) info))
315 (attr (read (format "(%s)"
316 (mapconcat
317 #'identity
318 (list (plist-get info :man-class-options))
319 " "))))
320 (section-item (plist-get attr :section-id)))
322 (concat
324 (cond
325 ((and title (stringp section-item))
326 (format ".TH \"%s\" \"%s\" \n" title section-item))
327 ((and (string= "" title) (stringp section-item))
328 (format ".TH \"%s\" \"%s\" \n" " " section-item))
329 (title
330 (format ".TH \"%s\" \"1\" \n" title))
332 ".TH \" \" \"1\" "))
333 contents)))
338 ;;; Transcode Functions
340 ;;; Babel Call
342 ;; Babel Calls are ignored.
345 ;;; Bold
347 (defun org-man-bold (bold contents info)
348 "Transcode BOLD from Org to Man.
349 CONTENTS is the text with bold markup. INFO is a plist holding
350 contextual information."
351 (format "\\fB%s\\fP" contents))
354 ;;; Center Block
356 (defun org-man-center-block (center-block contents info)
357 "Transcode a CENTER-BLOCK element from Org to Man.
358 CONTENTS holds the contents of the center block. INFO is a plist
359 holding contextual information."
360 (org-man--wrap-label
361 center-block
362 (format ".ce %d\n.nf\n%s\n.fi"
363 (- (length (split-string contents "\n")) 1 )
364 contents)))
367 ;;; Clock
369 (defun org-man-clock (clock contents info)
370 "Transcode a CLOCK element from Org to Man.
371 CONTENTS is nil. INFO is a plist holding contextual
372 information."
373 "" )
376 ;;; Code
378 (defun org-man-code (code contents info)
379 "Transcode a CODE object from Org to Man.
380 CONTENTS is nil. INFO is a plist used as a communication
381 channel."
382 (format "\\fC%s\\fP" code))
385 ;;; Comment
387 ;; Comments are ignored.
390 ;;; Comment Block
392 ;; Comment Blocks are ignored.
395 ;;; Drawer
397 (defun org-man-drawer (drawer contents info)
398 "Transcode a DRAWER element from Org to Man.
399 DRAWER holds the drawer information
400 CONTENTS holds the contents of the block.
401 INFO is a plist holding contextual information. "
402 contents)
405 ;;; Dynamic Block
407 (defun org-man-dynamic-block (dynamic-block contents info)
408 "Transcode a DYNAMIC-BLOCK element from Org to Man.
409 CONTENTS holds the contents of the block. INFO is a plist
410 holding contextual information. See `org-export-data'."
411 (org-man--wrap-label dynamic-block contents))
414 ;;; Entity
416 (defun org-man-entity (entity contents info)
417 "Transcode an ENTITY object from Org to Man.
418 CONTENTS are the definition itself. INFO is a plist holding
419 contextual information."
420 (org-element-property :utf-8 entity))
423 ;;; Example Block
425 (defun org-man-example-block (example-block contents info)
426 "Transcode an EXAMPLE-BLOCK element from Org to Man.
427 CONTENTS is nil. INFO is a plist holding contextual
428 information."
429 (org-man--wrap-label
430 example-block
431 (format ".RS\n.nf\n%s\n.fi\n.RE"
432 (org-export-format-code-default example-block info))))
435 ;;; Export Block
437 (defun org-man-export-block (export-block contents info)
438 "Transcode a EXPORT-BLOCK element from Org to Man.
439 CONTENTS is nil. INFO is a plist holding contextual information."
440 (when (string= (org-element-property :type export-block) "MAN")
441 (org-remove-indentation (org-element-property :value export-block))))
444 ;;; Export Snippet
446 (defun org-man-export-snippet (export-snippet contents info)
447 "Transcode a EXPORT-SNIPPET object from Org to Man.
448 CONTENTS is nil. INFO is a plist holding contextual information."
449 (when (eq (org-export-snippet-backend export-snippet) 'man)
450 (org-element-property :value export-snippet)))
453 ;;; Fixed Width
455 (defun org-man-fixed-width (fixed-width contents info)
456 "Transcode a FIXED-WIDTH element from Org to Man.
457 CONTENTS is nil. INFO is a plist holding contextual information."
458 (org-man--wrap-label
459 fixed-width
460 (format "\\fC\n%s\\fP"
461 (org-remove-indentation
462 (org-element-property :value fixed-width)))))
465 ;;; Footnote Definition
467 ;; Footnote Definitions are ignored.
469 ;;; Footnote References
471 ;; Footnote References are Ignored
474 ;;; Headline
476 (defun org-man-headline (headline contents info)
477 "Transcode a HEADLINE element from Org to Man.
478 CONTENTS holds the contents of the headline. INFO is a plist
479 holding contextual information."
480 (let* ((level (org-export-get-relative-level headline info))
481 (numberedp (org-export-numbered-headline-p headline info))
482 ;; Section formatting will set two placeholders: one for the
483 ;; title and the other for the contents.
484 (section-fmt
485 (case level
486 (1 ".SH \"%s\"\n%s")
487 (2 ".SS \"%s\"\n%s")
488 (3 ".SS \"%s\"\n%s")
489 (t nil)))
490 (text (org-export-data (org-element-property :title headline) info)))
492 (cond
493 ;; Case 1: This is a footnote section: ignore it.
494 ((org-element-property :footnote-section-p headline) nil)
496 ;; Case 2. This is a deep sub-tree: export it as a list item.
497 ;; Also export as items headlines for which no section
498 ;; format has been found.
499 ((or (not section-fmt) (org-export-low-level-p headline info))
500 ;; Build the real contents of the sub-tree.
501 (let ((low-level-body
502 (concat
503 ;; If the headline is the first sibling, start a list.
504 (when (org-export-first-sibling-p headline info)
505 (format "%s\n" ".RS"))
506 ;; Itemize headline
507 ".TP\n.ft I\n" text "\n.ft\n"
508 contents ".RE")))
509 ;; If headline is not the last sibling simply return
510 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
511 ;; blank line.
512 (if (not (org-export-last-sibling-p headline info)) low-level-body
513 (replace-regexp-in-string
514 "[ \t\n]*\\'" ""
515 low-level-body))))
517 ;; Case 3. Standard headline. Export it as a section.
518 (t (format section-fmt text contents )))))
520 ;;; Horizontal Rule
521 ;; Not supported
523 ;;; Inline Babel Call
525 ;; Inline Babel Calls are ignored.
527 ;;; Inline Src Block
529 (defun org-man-inline-src-block (inline-src-block contents info)
530 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
531 CONTENTS holds the contents of the item. INFO is a plist holding
532 contextual information."
533 (let* ((code (org-element-property :value inline-src-block)))
534 (cond
535 ((plist-get info :man-source-highlight)
536 (let* ((tmpdir (if (featurep 'xemacs)
537 temp-directory
538 temporary-file-directory ))
539 (in-file (make-temp-name
540 (expand-file-name "srchilite" tmpdir)))
541 (out-file (make-temp-name
542 (expand-file-name "reshilite" tmpdir)))
543 (org-lang (org-element-property :language inline-src-block))
544 (lst-lang
545 (cadr (assq (intern org-lang)
546 (plist-get info :man-source-highlight-langs))))
548 (cmd (concat (expand-file-name "source-highlight")
549 " -s " lst-lang
550 " -f groff_man"
551 " -i " in-file
552 " -o " out-file )))
554 (if lst-lang
555 (let ((code-block "" ))
556 (with-temp-file in-file (insert code))
557 (shell-command cmd)
558 (setq code-block (org-file-contents out-file))
559 (delete-file in-file)
560 (delete-file out-file)
561 code-block)
562 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
563 code))))
565 ;; Do not use a special package: transcode it verbatim.
567 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
568 "\\fP\n.fi\n.RE\n")))))
571 ;;; Inlinetask
572 ;;; Italic
574 (defun org-man-italic (italic contents info)
575 "Transcode ITALIC from Org to Man.
576 CONTENTS is the text with italic markup. INFO is a plist holding
577 contextual information."
578 (format "\\fI%s\\fP" contents))
581 ;;; Item
584 (defun org-man-item (item contents info)
586 "Transcode an ITEM element from Org to Man.
587 CONTENTS holds the contents of the item. INFO is a plist holding
588 contextual information."
590 (let* ((bullet (org-element-property :bullet item))
591 (type (org-element-property :type (org-element-property :parent item)))
592 (checkbox (case (org-element-property :checkbox item)
593 (on "\\o'\\(sq\\(mu'") ;;
594 (off "\\(sq ") ;;
595 (trans "\\o'\\(sq\\(mi'" ))) ;;
597 (tag (let ((tag (org-element-property :tag item)))
598 ;; Check-boxes must belong to the tag.
599 (and tag (format "\\fB%s\\fP"
600 (concat checkbox
601 (org-export-data tag info)))))))
603 (if (and (null tag )
604 (null checkbox))
605 (let* ((bullet (org-trim bullet))
606 (marker (cond ((string= "-" bullet) "\\(em")
607 ((string= "*" bullet) "\\(bu")
608 ((eq type 'ordered)
609 (format "%s " (org-trim bullet)))
610 (t "\\(dg"))))
611 (concat ".IP " marker " 4\n"
612 (org-trim (or contents " " ))))
613 ; else
614 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
615 (org-trim (or contents " " ))))))
617 ;;; Keyword
620 (defun org-man-keyword (keyword contents info)
621 "Transcode a KEYWORD element from Org to Man.
622 CONTENTS is nil. INFO is a plist holding contextual information."
623 (let ((key (org-element-property :key keyword))
624 (value (org-element-property :value keyword)))
625 (cond
626 ((string= key "MAN") value)
627 ((string= key "INDEX") nil)
628 ((string= key "TOC" ) nil))))
631 ;;; Line Break
633 (defun org-man-line-break (line-break contents info)
634 "Transcode a LINE-BREAK object from Org to Man.
635 CONTENTS is nil. INFO is a plist holding contextual information."
636 ".br\n")
639 ;;; Link
642 (defun org-man-link (link desc info)
643 "Transcode a LINK object from Org to Man.
645 DESC is the description part of the link, or the empty string.
646 INFO is a plist holding contextual information. See
647 `org-export-data'."
648 (let* ((type (org-element-property :type link))
649 (raw-path (org-element-property :path link))
650 ;; Ensure DESC really exists, or set it to nil.
651 (desc (and (not (string= desc "")) desc))
652 (path (cond
653 ((member type '("http" "https" "ftp" "mailto"))
654 (concat type ":" raw-path))
655 ((and (string= type "file") (file-name-absolute-p raw-path))
656 (concat "file:" raw-path))
657 (t raw-path)))
658 protocol)
659 (cond
660 ;; External link with a description part.
661 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
662 ;; External link without a description part.
663 (path (format "\\fI%s\\fP" path))
664 ;; No path, only description. Try to do something useful.
665 (t (format "\\fI%s\\fP" desc)))))
667 ;;;; Node Property
669 (defun org-man-node-property (node-property contents info)
670 "Transcode a NODE-PROPERTY element from Org to Man.
671 CONTENTS is nil. INFO is a plist holding contextual
672 information."
673 (format "%s:%s"
674 (org-element-property :key node-property)
675 (let ((value (org-element-property :value node-property)))
676 (if value (concat " " value) ""))))
678 ;;; Paragraph
680 (defun org-man-paragraph (paragraph contents info)
681 "Transcode a PARAGRAPH element from Org to Man.
682 CONTENTS is the contents of the paragraph, as a string. INFO is
683 the plist used as a communication channel."
684 (let ((parent (plist-get (nth 1 paragraph) :parent)))
685 (when parent
686 (let ((parent-type (car parent))
687 (fixed-paragraph ""))
688 (cond ((and (eq parent-type 'item)
689 (plist-get (nth 1 parent) :bullet ))
690 (setq fixed-paragraph (concat "" contents)))
691 ((eq parent-type 'section)
692 (setq fixed-paragraph (concat ".PP\n" contents)))
693 ((eq parent-type 'footnote-definition)
694 (setq fixed-paragraph contents))
695 (t (setq fixed-paragraph (concat "" contents))))
696 fixed-paragraph ))))
699 ;;; Plain List
701 (defun org-man-plain-list (plain-list contents info)
702 "Transcode a PLAIN-LIST element from Org to Man.
703 CONTENTS is the contents of the list. INFO is a plist holding
704 contextual information."
705 contents)
707 ;;; Plain Text
709 (defun org-man-plain-text (text info)
710 "Transcode a TEXT string from Org to Man.
711 TEXT is the string to transcode. INFO is a plist holding
712 contextual information."
713 (let ((output text))
714 ;; Protect various chars.
715 (setq output (replace-regexp-in-string
716 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
717 "$\\" output nil t 1))
718 ;; Activate smart quotes. Be sure to provide original TEXT string
719 ;; since OUTPUT may have been modified.
720 (when (plist-get info :with-smart-quotes)
721 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
722 ;; Handle break preservation if required.
723 (when (plist-get info :preserve-breaks)
724 (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
725 output)))
726 ;; Return value.
727 output))
731 ;;; Planning
734 ;;; Property Drawer
736 (defun org-man-property-drawer (property-drawer contents info)
737 "Transcode a PROPERTY-DRAWER element from Org to Man.
738 CONTENTS holds the contents of the drawer. INFO is a plist
739 holding contextual information."
740 (and (org-string-nw-p contents)
741 (format ".RS\n.nf\n%s\n.fi\n.RE" contents)))
743 ;;; Quote Block
745 (defun org-man-quote-block (quote-block contents info)
746 "Transcode a QUOTE-BLOCK element from Org to Man.
747 CONTENTS holds the contents of the block. INFO is a plist
748 holding contextual information."
749 (org-man--wrap-label
750 quote-block
751 (format ".RS\n%s\n.RE" contents)))
754 ;;; Radio Target
756 (defun org-man-radio-target (radio-target text info)
757 "Transcode a RADIO-TARGET object from Org to Man.
758 TEXT is the text of the target. INFO is a plist holding
759 contextual information."
760 text )
763 ;;; Section
765 (defun org-man-section (section contents info)
766 "Transcode a SECTION element from Org to Man.
767 CONTENTS holds the contents of the section. INFO is a plist
768 holding contextual information."
769 contents)
772 ;;; Special Block
774 (defun org-man-special-block (special-block contents info)
775 "Transcode a SPECIAL-BLOCK element from Org to Man.
776 CONTENTS holds the contents of the block. INFO is a plist
777 holding contextual information."
778 (let ((type (downcase (org-element-property :type special-block))))
779 (org-man--wrap-label
780 special-block
781 (format "%s\n" contents))))
784 ;;; Src Block
786 (defun org-man-src-block (src-block contents info)
787 "Transcode a SRC-BLOCK element from Org to Man.
788 CONTENTS holds the contents of the item. INFO is a plist holding
789 contextual information."
790 (let* ((lang (org-element-property :language src-block))
791 (code (org-element-property :value src-block))
792 (custom-env (and lang
793 (cadr (assq (intern lang)
794 org-man-custom-lang-environments))))
795 (num-start (case (org-element-property :number-lines src-block)
796 (continued (org-export-get-loc src-block info))
797 (new 0)))
798 (retain-labels (org-element-property :retain-labels src-block)))
799 (if (not (plist-get info :man-source-highlight))
800 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
801 (org-export-format-code-default src-block info))
802 (let* ((tmpdir (if (featurep 'xemacs) temp-directory
803 temporary-file-directory))
804 (in-file (make-temp-name (expand-file-name "srchilite" tmpdir)))
805 (out-file (make-temp-name (expand-file-name "reshilite" tmpdir)))
806 (org-lang (org-element-property :language src-block))
807 (lst-lang
808 (cadr (assq (intern org-lang)
809 (plist-get info :man-source-highlight-langs))))
810 (cmd (concat "source-highlight"
811 " -s " lst-lang
812 " -f groff_man "
813 " -i " in-file
814 " -o " out-file)))
815 (if lst-lang
816 (let ((code-block ""))
817 (with-temp-file in-file (insert code))
818 (shell-command cmd)
819 (setq code-block (org-file-contents out-file))
820 (delete-file in-file)
821 (delete-file out-file)
822 code-block)
823 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code))))))
826 ;;; Statistics Cookie
828 (defun org-man-statistics-cookie (statistics-cookie contents info)
829 "Transcode a STATISTICS-COOKIE object from Org to Man.
830 CONTENTS is nil. INFO is a plist holding contextual information."
831 (org-element-property :value statistics-cookie))
834 ;;; Strike-Through
836 (defun org-man-strike-through (strike-through contents info)
837 "Transcode STRIKE-THROUGH from Org to Man.
838 CONTENTS is the text with strike-through markup. INFO is a plist
839 holding contextual information."
840 (format "\\fI%s\\fP" contents))
842 ;;; Subscript
844 (defun org-man-subscript (subscript contents info)
845 "Transcode a SUBSCRIPT object from Org to Man.
846 CONTENTS is the contents of the object. INFO is a plist holding
847 contextual information."
848 (format "\\d\\s-2%s\\s+2\\u" contents))
850 ;;; Superscript "^_%s$
852 (defun org-man-superscript (superscript contents info)
853 "Transcode a SUPERSCRIPT object from Org to Man.
854 CONTENTS is the contents of the object. INFO is a plist holding
855 contextual information."
856 (format "\\u\\s-2%s\\s+2\\d" contents))
859 ;;; Table
861 ;; `org-man-table' is the entry point for table transcoding. It
862 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
863 ;; delegates the job to either `org-man-table--table.el-table' or
864 ;; `org-man-table--org-table' functions, depending of the type of
865 ;; the table.
867 ;; `org-man-table--align-string' is a subroutine used to build
868 ;; alignment string for Org tables.
870 (defun org-man-table (table contents info)
871 "Transcode a TABLE element from Org to Man.
872 CONTENTS is the contents of the table. INFO is a plist holding
873 contextual information."
874 (cond
875 ;; Case 1: verbatim table.
876 ((or (plist-get info :man-tables-verbatim)
877 (let ((attr (read (format "(%s)"
878 (mapconcat
879 #'identity
880 (org-element-property :attr_man table)
881 " ")))))
883 (and attr (plist-get attr :verbatim))))
885 (format ".nf\n\\fC%s\\fP\n.fi"
886 ;; Re-create table, without affiliated keywords.
887 (org-trim
888 (org-element-interpret-data
889 `(table nil ,@(org-element-contents table))))))
890 ;; Case 2: Standard table.
891 (t (org-man-table--org-table table contents info))))
893 (defun org-man-table--align-string (divider table info)
894 "Return an appropriate Man alignment string.
895 TABLE is the considered table. INFO is a plist used as
896 a communication channel."
897 (let (alignment)
898 ;; Extract column groups and alignment from first (non-rule) row.
899 (org-element-map
900 (org-element-map table 'table-row
901 (lambda (row)
902 (and (eq (org-element-property :type row) 'standard) row))
903 info 'first-match)
904 'table-cell
905 (lambda (cell)
906 (let* ((borders (org-export-table-cell-borders cell info))
907 (raw-width (org-export-table-cell-width cell info))
908 (width-cm (when raw-width (/ raw-width 5)))
909 (width (if raw-width (format "w(%dc)"
910 (if (< width-cm 1) 1 width-cm)) "")))
911 ;; Check left border for the first cell only.
912 (when (and (memq 'left borders) (not alignment))
913 (push "|" alignment))
914 (push
915 (case (org-export-table-cell-alignment cell info)
916 (left (concat "l" width divider))
917 (right (concat "r" width divider))
918 (center (concat "c" width divider)))
919 alignment)
920 (when (memq 'right borders) (push "|" alignment))))
921 info)
922 (apply 'concat (reverse alignment))))
924 (defun org-man-table--org-table (table contents info)
925 "Return appropriate Man code for an Org table.
927 TABLE is the table type element to transcode. CONTENTS is its
928 contents, as a string. INFO is a plist used as a communication
929 channel.
931 This function assumes TABLE has `org' as its `:type' attribute."
932 (let* ((attr (org-export-read-attribute :attr_man table))
933 (label (org-element-property :name table))
934 (caption (and (not (plist-get attr :disable-caption))
935 (org-man--caption/label-string table info)))
936 (divider (if (plist-get attr :divider) "|" " "))
938 ;; Determine alignment string.
939 (alignment (org-man-table--align-string divider table info))
940 ;; Extract others display options.
942 (lines (org-split-string contents "\n"))
944 (attr-list
945 (delq nil
946 (list
947 (and (plist-get attr :expand) "expand")
948 (let ((placement (plist-get attr :placement)))
949 (cond ((string= placement 'center) "center")
950 ((string= placement 'left) nil)
951 ((plist-get info :man-tables-centered) "center")
952 (t "")))
953 (or (plist-get attr :boxtype) "box"))))
955 (title-line (plist-get attr :title-line))
956 (long-cells (plist-get attr :long-cells))
958 (table-format (concat
959 (format "%s" (or (car attr-list) "" ))
961 (let ((output-list '()))
962 (when (cdr attr-list)
963 (dolist (attr-item (cdr attr-list))
964 (setq output-list (concat output-list (format ",%s" attr-item)))))
965 output-list)
966 "")))
968 (first-line (when lines (org-split-string (car lines) "\t"))))
969 ;; Prepare the final format string for the table.
972 (cond
973 ;; Others.
974 (lines (concat ".TS\n " table-format ";\n"
976 (format "%s.\n"
977 (let ((final-line ""))
978 (when title-line
979 (dotimes (i (length first-line))
980 (setq final-line (concat final-line "cb" divider))))
982 (setq final-line (concat final-line "\n"))
984 (if alignment
985 (setq final-line (concat final-line alignment))
986 (dotimes (i (length first-line))
987 (setq final-line (concat final-line "c" divider))))
988 final-line ))
990 (format "%s.TE\n"
991 (let ((final-line "")
992 (long-line "")
993 (lines (org-split-string contents "\n")))
995 (dolist (line-item lines)
996 (setq long-line "")
998 (if long-cells
999 (progn
1000 (if (string= line-item "_")
1001 (setq long-line (format "%s\n" line-item))
1002 ;; else string =
1003 (let ((cell-item-list (org-split-string line-item "\t")))
1004 (dolist (cell-item cell-item-list)
1006 (cond ((eq cell-item (car (last cell-item-list)))
1007 (setq long-line (concat long-line
1008 (format "T{\n%s\nT}\t\n" cell-item ))))
1010 (setq long-line (concat long-line
1011 (format "T{\n%s\nT}\t" cell-item ))))))
1012 long-line))
1013 ;; else long cells
1014 (setq final-line (concat final-line long-line )))
1016 (setq final-line (concat final-line line-item "\n"))))
1017 final-line))
1019 (and caption (format ".TB \"%s\"" caption)))))))
1021 ;;; Table Cell
1023 (defun org-man-table-cell (table-cell contents info)
1024 "Transcode a TABLE-CELL element from Org to Man
1025 CONTENTS is the cell contents. INFO is a plist used as
1026 a communication channel."
1027 (concat
1028 (let ((scientific-format (plist-get info :man-table-scientific-notation)))
1029 (if (and contents
1030 scientific-format
1031 (string-match orgtbl-exp-regexp contents))
1032 ;; Use appropriate format string for scientific notation.
1033 (format scientific-format
1034 (match-string 1 contents)
1035 (match-string 2 contents))
1036 contents))
1037 (when (org-export-get-next-element table-cell info) "\t")))
1040 ;;; Table Row
1042 (defun org-man-table-row (table-row contents info)
1043 "Transcode a TABLE-ROW element from Org to Man
1044 CONTENTS is the contents of the row. INFO is a plist used as
1045 a communication channel."
1046 ;; Rules are ignored since table separators are deduced from
1047 ;; borders of the current row.
1048 (when (eq (org-element-property :type table-row) 'standard)
1049 (let* ((attr (mapconcat 'identity
1050 (org-element-property
1051 :attr_man (org-export-get-parent table-row))
1052 " "))
1053 ;; TABLE-ROW's borders are extracted from its first cell.
1054 (borders
1055 (org-export-table-cell-borders
1056 (car (org-element-contents table-row)) info)))
1057 (concat
1058 ;; Mark horizontal lines
1059 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1060 contents
1062 (cond
1063 ;; When BOOKTABS are activated enforce bottom rule even when
1064 ;; no hline was specifically marked.
1065 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1066 ((memq 'below borders) "\n_"))))))
1069 ;;; Target
1071 (defun org-man-target (target contents info)
1072 "Transcode a TARGET object from Org to Man.
1073 CONTENTS is nil. INFO is a plist holding contextual
1074 information."
1075 (format "\\fI%s\\fP"
1076 (org-export-solidify-link-text (org-element-property :value target))))
1079 ;;; Timestamp
1081 (defun org-man-timestamp (timestamp contents info)
1082 "Transcode a TIMESTAMP object from Org to Man.
1083 CONTENTS is nil. INFO is a plist holding contextual
1084 information."
1085 "" )
1088 ;;; Underline
1090 (defun org-man-underline (underline contents info)
1091 "Transcode UNDERLINE from Org to Man.
1092 CONTENTS is the text with underline markup. INFO is a plist
1093 holding contextual information."
1094 (format "\\fI%s\\fP" contents))
1097 ;;; Verbatim
1099 (defun org-man-verbatim (verbatim contents info)
1100 "Transcode a VERBATIM object from Org to Man.
1101 CONTENTS is nil. INFO is a plist used as a communication
1102 channel."
1103 (format ".nf\n%s\n.fi" contents))
1106 ;;; Verse Block
1108 (defun org-man-verse-block (verse-block contents info)
1109 "Transcode a VERSE-BLOCK element from Org to Man.
1110 CONTENTS is verse block contents. INFO is a plist holding
1111 contextual information."
1112 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1116 ;;; Interactive functions
1118 (defun org-man-export-to-man
1119 (&optional async subtreep visible-only body-only ext-plist)
1120 "Export current buffer to a Man file.
1122 If narrowing is active in the current buffer, only export its
1123 narrowed part.
1125 If a region is active, export that region.
1127 A non-nil optional argument ASYNC means the process should happen
1128 asynchronously. The resulting file should be accessible through
1129 the `org-export-stack' interface.
1131 When optional argument SUBTREEP is non-nil, export the sub-tree
1132 at point, extracting information from the headline properties
1133 first.
1135 When optional argument VISIBLE-ONLY is non-nil, don't export
1136 contents of hidden elements.
1138 When optional argument BODY-ONLY is non-nil, only the body
1139 without any markers.
1141 EXT-PLIST, when provided, is a property list with external
1142 parameters overriding Org default settings, but still inferior to
1143 file-local settings.
1145 Return output file's name."
1146 (interactive)
1147 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1148 (org-export-to-file 'man outfile
1149 async subtreep visible-only body-only ext-plist)))
1151 (defun org-man-export-to-pdf
1152 (&optional async subtreep visible-only body-only ext-plist)
1153 "Export current buffer to Groff then process through to PDF.
1155 If narrowing is active in the current buffer, only export its
1156 narrowed part.
1158 If a region is active, export that region.
1160 A non-nil optional argument ASYNC means the process should happen
1161 asynchronously. The resulting file should be accessible through
1162 the `org-export-stack' interface.
1164 When optional argument SUBTREEP is non-nil, export the sub-tree
1165 at point, extracting information from the headline properties
1166 first.
1168 When optional argument VISIBLE-ONLY is non-nil, don't export
1169 contents of hidden elements.
1171 When optional argument BODY-ONLY is non-nil, only write between
1172 markers.
1174 EXT-PLIST, when provided, is a property list with external
1175 parameters overriding Org default settings, but still inferior to
1176 file-local settings.
1178 Return PDF file's name."
1179 (interactive)
1180 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1181 (org-export-to-file 'man outfile
1182 async subtreep visible-only body-only ext-plist
1183 (lambda (file) (org-latex-compile file)))))
1185 (defun org-man-compile (file)
1186 "Compile a Groff file.
1188 FILE is the name of the file being compiled. Processing is done
1189 through the command specified in `org-man-pdf-process'.
1191 Return PDF file name or an error if it couldn't be produced."
1192 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1193 (full-name (file-truename file))
1194 (out-dir (file-name-directory file))
1195 ;; Properly set working directory for compilation.
1196 (default-directory (if (file-name-absolute-p file)
1197 (file-name-directory full-name)
1198 default-directory))
1199 errors)
1200 (message (format "Processing Groff file %s..." file))
1201 (save-window-excursion
1202 (cond
1203 ;; A function is provided: Apply it.
1204 ((functionp org-man-pdf-process)
1205 (funcall org-man-pdf-process (shell-quote-argument file)))
1206 ;; A list is provided: Replace %b, %f and %o with appropriate
1207 ;; values in each command before applying it. Output is
1208 ;; redirected to "*Org PDF Groff Output*" buffer.
1209 ((consp org-man-pdf-process)
1210 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1211 (mapc
1212 (lambda (command)
1213 (shell-command
1214 (replace-regexp-in-string
1215 "%b" (shell-quote-argument base-name)
1216 (replace-regexp-in-string
1217 "%f" (shell-quote-argument full-name)
1218 (replace-regexp-in-string
1219 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1220 outbuf))
1221 org-man-pdf-process)
1222 ;; Collect standard errors from output buffer.
1223 (setq errors (org-man-collect-errors outbuf))))
1224 (t (error "No valid command to process to PDF")))
1225 (let ((pdffile (concat out-dir base-name ".pdf")))
1226 ;; Check for process failure. Provide collected errors if
1227 ;; possible.
1228 (if (not (file-exists-p pdffile))
1229 (error (concat (format "PDF file %s wasn't produced" pdffile)
1230 (when errors (concat ": " errors))))
1231 ;; Else remove log files, when specified, and signal end of
1232 ;; process to user, along with any error encountered.
1233 (when org-man-remove-logfiles
1234 (dolist (ext org-man-logfiles-extensions)
1235 (let ((file (concat out-dir base-name "." ext)))
1236 (when (file-exists-p file) (delete-file file)))))
1237 (message (concat "Process completed"
1238 (if (not errors) "."
1239 (concat " with errors: " errors)))))
1240 ;; Return output file name.
1241 pdffile))))
1243 (defun org-man-collect-errors (buffer)
1244 "Collect some kind of errors from \"groff\" output
1245 BUFFER is the buffer containing output.
1246 Return collected error types as a string, or nil if there was
1247 none."
1248 (with-current-buffer buffer
1249 (save-excursion
1250 (goto-char (point-max))
1251 ;; Find final run
1252 nil )))
1255 (provide 'ox-man)
1257 ;; Local variables:
1258 ;; generated-autoload-file: "org-loaddefs.el"
1259 ;; End:
1261 ;;; ox-man.el ends here