Backport commit 0c856a2 from Emacs master branch
[org-mode/org-tableheadings.git] / lisp / ox-man.el
blobf9d37ac3fbf8e280554c27e308f10f0ea83adfe8
1 ;; ox-man.el --- Man Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2015 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 (drawer . org-man-drawer)
59 (dynamic-block . org-man-dynamic-block)
60 (entity . org-man-entity)
61 (example-block . org-man-example-block)
62 (export-block . org-man-export-block)
63 (export-snippet . org-man-export-snippet)
64 (fixed-width . org-man-fixed-width)
65 (footnote-definition . org-man-footnote-definition)
66 (footnote-reference . org-man-footnote-reference)
67 (headline . org-man-headline)
68 (horizontal-rule . org-man-horizontal-rule)
69 (inline-babel-call . org-man-inline-babel-call)
70 (inline-src-block . org-man-inline-src-block)
71 (inlinetask . org-man-inlinetask)
72 (italic . org-man-italic)
73 (item . org-man-item)
74 (keyword . org-man-keyword)
75 (line-break . org-man-line-break)
76 (link . org-man-link)
77 (node-property . org-man-node-property)
78 (paragraph . org-man-paragraph)
79 (plain-list . org-man-plain-list)
80 (plain-text . org-man-plain-text)
81 (planning . org-man-planning)
82 (property-drawer . org-man-property-drawer)
83 (quote-block . org-man-quote-block)
84 (radio-target . org-man-radio-target)
85 (section . org-man-section)
86 (special-block . org-man-special-block)
87 (src-block . org-man-src-block)
88 (statistics-cookie . org-man-statistics-cookie)
89 (strike-through . org-man-strike-through)
90 (subscript . org-man-subscript)
91 (superscript . org-man-superscript)
92 (table . org-man-table)
93 (table-cell . org-man-table-cell)
94 (table-row . org-man-table-row)
95 (target . org-man-target)
96 (template . org-man-template)
97 (timestamp . org-man-timestamp)
98 (underline . org-man-underline)
99 (verbatim . org-man-verbatim)
100 (verse-block . org-man-verse-block))
101 :export-block "MAN"
102 :menu-entry
103 '(?M "Export to MAN"
104 ((?m "As MAN file" org-man-export-to-man)
105 (?p "As PDF file" org-man-export-to-pdf)
106 (?o "As PDF file and open"
107 (lambda (a s v b)
108 (if a (org-man-export-to-pdf t s v b)
109 (org-open-file (org-man-export-to-pdf nil s v b)))))))
110 :options-alist
111 '((:man-class "MAN_CLASS" nil nil t)
112 (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
113 (:man-header-extra "MAN_HEADER" nil nil newline)
114 ;; Other variables.
115 (:man-tables-centered nil nil org-man-tables-centered)
116 (:man-tables-verbatim nil nil org-man-tables-verbatim)
117 (:man-table-scientific-notation nil nil org-man-table-scientific-notation)
118 (:man-source-highlight nil nil org-man-source-highlight)
119 (:man-source-highlight-langs nil nil org-man-source-highlight-langs)))
123 ;;; User Configurable Variables
125 (defgroup org-export-man nil
126 "Options for exporting Org mode files to Man."
127 :tag "Org Export Man"
128 :group 'org-export)
130 ;;; Tables
132 (defcustom org-man-tables-centered t
133 "When non-nil, tables are exported in a center environment."
134 :group 'org-export-man
135 :version "24.4"
136 :package-version '(Org . "8.0")
137 :type 'boolean)
139 (defcustom org-man-tables-verbatim nil
140 "When non-nil, tables are exported verbatim."
141 :group 'org-export-man
142 :version "24.4"
143 :package-version '(Org . "8.0")
144 :type 'boolean)
147 (defcustom org-man-table-scientific-notation "%sE%s"
148 "Format string to display numbers in scientific notation.
149 The format should have \"%s\" twice, for mantissa and exponent
150 \(i.e. \"%s\\\\times10^{%s}\").
152 When nil, no transformation is made."
153 :group 'org-export-man
154 :version "24.4"
155 :package-version '(Org . "8.0")
156 :type '(choice
157 (string :tag "Format string")
158 (const :tag "No formatting")))
161 ;;; Inlinetasks
162 ;; Src blocks
164 (defcustom org-man-source-highlight nil
165 "Use GNU source highlight to embellish source blocks "
166 :group 'org-export-man
167 :version "24.4"
168 :package-version '(Org . "8.0")
169 :type 'boolean)
172 (defcustom org-man-source-highlight-langs
173 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
174 (scheme "scheme")
175 (c "c") (cc "cpp") (csharp "csharp") (d "d")
176 (fortran "fortran") (cobol "cobol") (pascal "pascal")
177 (ada "ada") (asm "asm")
178 (perl "perl") (cperl "perl")
179 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
180 (java "java") (javascript "javascript")
181 (tex "latex")
182 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
183 (ocaml "caml") (caml "caml")
184 (sql "sql") (sqlite "sql")
185 (html "html") (css "css") (xml "xml")
186 (bat "bat") (bison "bison") (clipper "clipper")
187 (ldap "ldap") (opa "opa")
188 (php "php") (postscript "postscript") (prolog "prolog")
189 (properties "properties") (makefile "makefile")
190 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
191 "Alist mapping languages to their listing language counterpart.
192 The key is a symbol, the major mode symbol without the \"-mode\".
193 The value is the string that should be inserted as the language
194 parameter for the listings package. If the mode name and the
195 listings name are the same, the language does not need an entry
196 in this list - but it does not hurt if it is present."
197 :group 'org-export-man
198 :version "24.4"
199 :package-version '(Org . "8.0")
200 :type '(repeat
201 (list
202 (symbol :tag "Major mode ")
203 (string :tag "Listings language"))))
207 (defvar org-man-custom-lang-environments nil
208 "Alist mapping languages to language-specific Man environments.
210 It is used during export of src blocks by the listings and
211 man packages. For example,
213 \(setq org-man-custom-lang-environments
214 '\(\(python \"pythoncode\"\)\)\)
216 would have the effect that if org encounters begin_src python
217 during man export."
221 ;;; Compilation
223 (defcustom org-man-pdf-process
224 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
225 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
226 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
228 "Commands to process a Man file to a PDF file.
229 This is a list of strings, each of them will be given to the
230 shell as a command. %f in the command will be replaced by the
231 full file name, %b by the file base name (i.e. without directory
232 and extension parts) and %o by the base directory of the file.
235 By default, Org uses 3 runs of to do the processing.
237 Alternatively, this may be a Lisp function that does the
238 processing. This function should accept the file name as
239 its single argument."
240 :group 'org-export-pdf
241 :group 'org-export-man
242 :version "24.4"
243 :package-version '(Org . "8.0")
244 :type '(choice
245 (repeat :tag "Shell command sequence"
246 (string :tag "Shell command"))
247 (const :tag "2 runs of pdfgroff"
248 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
249 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
250 (const :tag "3 runs of pdfgroff"
251 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
252 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
253 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
254 (function)))
256 (defcustom org-man-logfiles-extensions
257 '("log" "out" "toc")
258 "The list of file extensions to consider as Man logfiles."
259 :group 'org-export-man
260 :version "24.4"
261 :package-version '(Org . "8.0")
262 :type '(repeat (string :tag "Extension")))
264 (defcustom org-man-remove-logfiles t
265 "Non-nil means remove the logfiles produced by PDF production.
266 These are the .aux, .log, .out, and .toc files."
267 :group 'org-export-man
268 :version "24.4"
269 :package-version '(Org . "8.0")
270 :type 'boolean)
274 ;;; Internal Functions
276 (defun org-man--caption/label-string (element info)
277 "Return caption and label Man string for ELEMENT.
279 INFO is a plist holding contextual information. If there's no
280 caption nor label, return the empty string.
282 For non-floats, see `org-man--wrap-label'."
283 (let ((label (org-element-property :label element))
284 (main (org-export-get-caption element))
285 (short (org-export-get-caption element t)))
286 (cond ((and (not main) (not label)) "")
287 ((not main) (format "\\fI%s\\fP" label))
288 ;; Option caption format with short name.
289 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
290 (org-export-data short info)
291 (org-export-data main info)))
292 ;; Standard caption format.
293 (t (format "\\fR%s\\fP" (org-export-data main info))))))
295 (defun org-man--wrap-label (element output)
296 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
297 This function shouldn't be used for floats. See
298 `org-man--caption/label-string'."
299 (let ((label (org-element-property :name element)))
300 (if (or (not output) (not label) (string= output "") (string= label ""))
301 output
302 (concat (format "%s\n.br\n" label) output))))
306 ;;; Template
308 (defun org-man-template (contents info)
309 "Return complete document string after Man conversion.
310 CONTENTS is the transcoded contents string. INFO is a plist
311 holding export options."
312 (let* ((title (when (plist-get info :with-title)
313 (org-export-data (plist-get info :title) info)))
314 (attr (read (format "(%s)"
315 (mapconcat
316 #'identity
317 (list (plist-get info :man-class-options))
318 " "))))
319 (section-item (plist-get attr :section-id)))
321 (concat
323 (cond
324 ((and title (stringp section-item))
325 (format ".TH \"%s\" \"%s\" \n" title section-item))
326 ((and (string= "" title) (stringp section-item))
327 (format ".TH \"%s\" \"%s\" \n" " " section-item))
328 (title
329 (format ".TH \"%s\" \"1\" \n" title))
331 ".TH \" \" \"1\" "))
332 contents)))
337 ;;; Transcode Functions
339 ;;; Babel Call
341 ;; Babel Calls are ignored.
344 ;;; Bold
346 (defun org-man-bold (bold contents info)
347 "Transcode BOLD from Org to Man.
348 CONTENTS is the text with bold markup. INFO is a plist holding
349 contextual information."
350 (format "\\fB%s\\fP" contents))
353 ;;; Center Block
355 (defun org-man-center-block (center-block contents info)
356 "Transcode a CENTER-BLOCK element from Org to Man.
357 CONTENTS holds the contents of the center block. INFO is a plist
358 holding contextual information."
359 (org-man--wrap-label
360 center-block
361 (format ".ce %d\n.nf\n%s\n.fi"
362 (- (length (split-string contents "\n")) 1 )
363 contents)))
366 ;;; Clock
368 (defun org-man-clock (clock contents info)
369 "Transcode a CLOCK element from Org to Man.
370 CONTENTS is nil. INFO is a plist holding contextual
371 information."
372 "" )
375 ;;; Code
377 (defun org-man-code (code contents info)
378 "Transcode a CODE object from Org to Man.
379 CONTENTS is nil. INFO is a plist used as a communication
380 channel."
381 (format "\\fC%s\\fP" code))
384 ;;; Comment
386 ;; Comments are ignored.
389 ;;; Comment Block
391 ;; Comment Blocks are ignored.
394 ;;; Drawer
396 (defun org-man-drawer (drawer contents info)
397 "Transcode a DRAWER element from Org to Man.
398 DRAWER holds the drawer information
399 CONTENTS holds the contents of the block.
400 INFO is a plist holding contextual information. "
401 contents)
404 ;;; Dynamic Block
406 (defun org-man-dynamic-block (dynamic-block contents info)
407 "Transcode a DYNAMIC-BLOCK element from Org to Man.
408 CONTENTS holds the contents of the block. INFO is a plist
409 holding contextual information. See `org-export-data'."
410 (org-man--wrap-label dynamic-block contents))
413 ;;; Entity
415 (defun org-man-entity (entity contents info)
416 "Transcode an ENTITY object from Org to Man.
417 CONTENTS are the definition itself. INFO is a plist holding
418 contextual information."
419 (org-element-property :utf-8 entity))
422 ;;; Example Block
424 (defun org-man-example-block (example-block contents info)
425 "Transcode an EXAMPLE-BLOCK element from Org to Man.
426 CONTENTS is nil. INFO is a plist holding contextual
427 information."
428 (org-man--wrap-label
429 example-block
430 (format ".RS\n.nf\n%s\n.fi\n.RE"
431 (org-export-format-code-default example-block info))))
434 ;;; Export Block
436 (defun org-man-export-block (export-block contents info)
437 "Transcode a EXPORT-BLOCK element from Org to Man.
438 CONTENTS is nil. INFO is a plist holding contextual information."
439 (when (string= (org-element-property :type export-block) "MAN")
440 (org-remove-indentation (org-element-property :value export-block))))
443 ;;; Export Snippet
445 (defun org-man-export-snippet (export-snippet contents info)
446 "Transcode a EXPORT-SNIPPET object from Org to Man.
447 CONTENTS is nil. INFO is a plist holding contextual information."
448 (when (eq (org-export-snippet-backend export-snippet) 'man)
449 (org-element-property :value export-snippet)))
452 ;;; Fixed Width
454 (defun org-man-fixed-width (fixed-width contents info)
455 "Transcode a FIXED-WIDTH element from Org to Man.
456 CONTENTS is nil. INFO is a plist holding contextual information."
457 (org-man--wrap-label
458 fixed-width
459 (format "\\fC\n%s\\fP"
460 (org-remove-indentation
461 (org-element-property :value fixed-width)))))
464 ;;; Footnote Definition
466 ;; Footnote Definitions are ignored.
468 ;;; Footnote References
470 ;; Footnote References are Ignored
473 ;;; Headline
475 (defun org-man-headline (headline contents info)
476 "Transcode a HEADLINE element from Org to Man.
477 CONTENTS holds the contents of the headline. INFO is a plist
478 holding contextual information."
479 (let* ((level (org-export-get-relative-level headline info))
480 (numberedp (org-export-numbered-headline-p headline info))
481 ;; Section formatting will set two placeholders: one for the
482 ;; title and the other for the contents.
483 (section-fmt
484 (case level
485 (1 ".SH \"%s\"\n%s")
486 (2 ".SS \"%s\"\n%s")
487 (3 ".SS \"%s\"\n%s")
488 (t nil)))
489 (text (org-export-data (org-element-property :title headline) info)))
491 (cond
492 ;; Case 1: This is a footnote section: ignore it.
493 ((org-element-property :footnote-section-p headline) nil)
495 ;; Case 2. This is a deep sub-tree: export it as a list item.
496 ;; Also export as items headlines for which no section
497 ;; format has been found.
498 ((or (not section-fmt) (org-export-low-level-p headline info))
499 ;; Build the real contents of the sub-tree.
500 (let ((low-level-body
501 (concat
502 ;; If the headline is the first sibling, start a list.
503 (when (org-export-first-sibling-p headline info)
504 (format "%s\n" ".RS"))
505 ;; Itemize headline
506 ".TP\n.ft I\n" text "\n.ft\n"
507 contents ".RE")))
508 ;; If headline is not the last sibling simply return
509 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
510 ;; blank line.
511 (if (not (org-export-last-sibling-p headline info)) low-level-body
512 (replace-regexp-in-string
513 "[ \t\n]*\\'" ""
514 low-level-body))))
516 ;; Case 3. Standard headline. Export it as a section.
517 (t (format section-fmt text contents )))))
519 ;;; Horizontal Rule
520 ;; Not supported
522 ;;; Inline Babel Call
524 ;; Inline Babel Calls are ignored.
526 ;;; Inline Src Block
528 (defun org-man-inline-src-block (inline-src-block contents info)
529 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
530 CONTENTS holds the contents of the item. INFO is a plist holding
531 contextual information."
532 (let* ((code (org-element-property :value inline-src-block)))
533 (cond
534 ((plist-get info :man-source-highlight)
535 (let* ((tmpdir (if (featurep 'xemacs)
536 temp-directory
537 temporary-file-directory ))
538 (in-file (make-temp-name
539 (expand-file-name "srchilite" tmpdir)))
540 (out-file (make-temp-name
541 (expand-file-name "reshilite" tmpdir)))
542 (org-lang (org-element-property :language inline-src-block))
543 (lst-lang
544 (cadr (assq (intern org-lang)
545 (plist-get info :man-source-highlight-langs))))
547 (cmd (concat (expand-file-name "source-highlight")
548 " -s " lst-lang
549 " -f groff_man"
550 " -i " in-file
551 " -o " out-file )))
553 (if lst-lang
554 (let ((code-block "" ))
555 (with-temp-file in-file (insert code))
556 (shell-command cmd)
557 (setq code-block (org-file-contents out-file))
558 (delete-file in-file)
559 (delete-file out-file)
560 code-block)
561 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
562 code))))
564 ;; Do not use a special package: transcode it verbatim.
566 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
567 "\\fP\n.fi\n.RE\n")))))
570 ;;; Inlinetask
571 ;;; Italic
573 (defun org-man-italic (italic contents info)
574 "Transcode ITALIC from Org to Man.
575 CONTENTS is the text with italic markup. INFO is a plist holding
576 contextual information."
577 (format "\\fI%s\\fP" contents))
580 ;;; Item
583 (defun org-man-item (item contents info)
585 "Transcode an ITEM element from Org to Man.
586 CONTENTS holds the contents of the item. INFO is a plist holding
587 contextual information."
589 (let* ((bullet (org-element-property :bullet item))
590 (type (org-element-property :type (org-element-property :parent item)))
591 (checkbox (case (org-element-property :checkbox item)
592 (on "\\o'\\(sq\\(mu'") ;;
593 (off "\\(sq ") ;;
594 (trans "\\o'\\(sq\\(mi'" ))) ;;
596 (tag (let ((tag (org-element-property :tag item)))
597 ;; Check-boxes must belong to the tag.
598 (and tag (format "\\fB%s\\fP"
599 (concat checkbox
600 (org-export-data tag info)))))))
602 (if (and (null tag )
603 (null checkbox))
604 (let* ((bullet (org-trim bullet))
605 (marker (cond ((string= "-" bullet) "\\(em")
606 ((string= "*" bullet) "\\(bu")
607 ((eq type 'ordered)
608 (format "%s " (org-trim bullet)))
609 (t "\\(dg"))))
610 (concat ".IP " marker " 4\n"
611 (org-trim (or contents " " ))))
612 ; else
613 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
614 (org-trim (or contents " " ))))))
616 ;;; Keyword
619 (defun org-man-keyword (keyword contents info)
620 "Transcode a KEYWORD element from Org to Man.
621 CONTENTS is nil. INFO is a plist holding contextual information."
622 (let ((key (org-element-property :key keyword))
623 (value (org-element-property :value keyword)))
624 (cond
625 ((string= key "MAN") value)
626 ((string= key "INDEX") nil)
627 ((string= key "TOC" ) nil))))
630 ;;; Line Break
632 (defun org-man-line-break (line-break contents info)
633 "Transcode a LINE-BREAK object from Org to Man.
634 CONTENTS is nil. INFO is a plist holding contextual information."
635 ".br\n")
638 ;;; Link
641 (defun org-man-link (link desc info)
642 "Transcode a LINK object from Org to Man.
644 DESC is the description part of the link, or the empty string.
645 INFO is a plist holding contextual information. See
646 `org-export-data'."
647 (let* ((type (org-element-property :type link))
648 (raw-path (org-element-property :path link))
649 ;; Ensure DESC really exists, or set it to nil.
650 (desc (and (not (string= desc "")) desc))
651 (path (cond
652 ((member type '("http" "https" "ftp" "mailto"))
653 (concat type ":" raw-path))
654 ((string= type "file") (org-export-file-uri raw-path))
655 (t raw-path)))
656 protocol)
657 (cond
658 ;; Link type is handled by a special function.
659 ((org-export-custom-protocol-maybe link desc 'man))
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 (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" (org-export-get-reference target info)))
1078 ;;; Timestamp
1080 (defun org-man-timestamp (timestamp contents info)
1081 "Transcode a TIMESTAMP object from Org to Man.
1082 CONTENTS is nil. INFO is a plist holding contextual
1083 information."
1084 "" )
1087 ;;; Underline
1089 (defun org-man-underline (underline contents info)
1090 "Transcode UNDERLINE from Org to Man.
1091 CONTENTS is the text with underline markup. INFO is a plist
1092 holding contextual information."
1093 (format "\\fI%s\\fP" contents))
1096 ;;; Verbatim
1098 (defun org-man-verbatim (verbatim contents info)
1099 "Transcode a VERBATIM object from Org to Man.
1100 CONTENTS is nil. INFO is a plist used as a communication
1101 channel."
1102 (format ".nf\n%s\n.fi" contents))
1105 ;;; Verse Block
1107 (defun org-man-verse-block (verse-block contents info)
1108 "Transcode a VERSE-BLOCK element from Org to Man.
1109 CONTENTS is verse block contents. INFO is a plist holding
1110 contextual information."
1111 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1115 ;;; Interactive functions
1117 (defun org-man-export-to-man
1118 (&optional async subtreep visible-only body-only ext-plist)
1119 "Export current buffer to a Man file.
1121 If narrowing is active in the current buffer, only export its
1122 narrowed part.
1124 If a region is active, export that region.
1126 A non-nil optional argument ASYNC means the process should happen
1127 asynchronously. The resulting file should be accessible through
1128 the `org-export-stack' interface.
1130 When optional argument SUBTREEP is non-nil, export the sub-tree
1131 at point, extracting information from the headline properties
1132 first.
1134 When optional argument VISIBLE-ONLY is non-nil, don't export
1135 contents of hidden elements.
1137 When optional argument BODY-ONLY is non-nil, only the body
1138 without any markers.
1140 EXT-PLIST, when provided, is a property list with external
1141 parameters overriding Org default settings, but still inferior to
1142 file-local settings.
1144 Return output file's name."
1145 (interactive)
1146 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1147 (org-export-to-file 'man outfile
1148 async subtreep visible-only body-only ext-plist)))
1150 (defun org-man-export-to-pdf
1151 (&optional async subtreep visible-only body-only ext-plist)
1152 "Export current buffer to Groff then process through to PDF.
1154 If narrowing is active in the current buffer, only export its
1155 narrowed part.
1157 If a region is active, export that region.
1159 A non-nil optional argument ASYNC means the process should happen
1160 asynchronously. The resulting file should be accessible through
1161 the `org-export-stack' interface.
1163 When optional argument SUBTREEP is non-nil, export the sub-tree
1164 at point, extracting information from the headline properties
1165 first.
1167 When optional argument VISIBLE-ONLY is non-nil, don't export
1168 contents of hidden elements.
1170 When optional argument BODY-ONLY is non-nil, only write between
1171 markers.
1173 EXT-PLIST, when provided, is a property list with external
1174 parameters overriding Org default settings, but still inferior to
1175 file-local settings.
1177 Return PDF file's name."
1178 (interactive)
1179 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1180 (org-export-to-file 'man outfile
1181 async subtreep visible-only body-only ext-plist
1182 (lambda (file) (org-latex-compile file)))))
1184 (defun org-man-compile (file)
1185 "Compile a Groff file.
1187 FILE is the name of the file being compiled. Processing is done
1188 through the command specified in `org-man-pdf-process'.
1190 Return PDF file name or an error if it couldn't be produced."
1191 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1192 (full-name (file-truename file))
1193 (out-dir (file-name-directory file))
1194 ;; Properly set working directory for compilation.
1195 (default-directory (if (file-name-absolute-p file)
1196 (file-name-directory full-name)
1197 default-directory))
1198 errors)
1199 (message "Processing Groff file %s..." file)
1200 (save-window-excursion
1201 (cond
1202 ;; A function is provided: Apply it.
1203 ((functionp org-man-pdf-process)
1204 (funcall org-man-pdf-process (shell-quote-argument file)))
1205 ;; A list is provided: Replace %b, %f and %o with appropriate
1206 ;; values in each command before applying it. Output is
1207 ;; redirected to "*Org PDF Groff Output*" buffer.
1208 ((consp org-man-pdf-process)
1209 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1210 (mapc
1211 (lambda (command)
1212 (shell-command
1213 (replace-regexp-in-string
1214 "%b" (shell-quote-argument base-name)
1215 (replace-regexp-in-string
1216 "%f" (shell-quote-argument full-name)
1217 (replace-regexp-in-string
1218 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1219 outbuf))
1220 org-man-pdf-process)
1221 ;; Collect standard errors from output buffer.
1222 (setq errors (org-man-collect-errors outbuf))))
1223 (t (error "No valid command to process to PDF")))
1224 (let ((pdffile (concat out-dir base-name ".pdf")))
1225 ;; Check for process failure. Provide collected errors if
1226 ;; possible.
1227 (if (not (file-exists-p pdffile))
1228 (error (concat (format "PDF file %s wasn't produced" pdffile)
1229 (when errors (concat ": " errors))))
1230 ;; Else remove log files, when specified, and signal end of
1231 ;; process to user, along with any error encountered.
1232 (when org-man-remove-logfiles
1233 (dolist (ext org-man-logfiles-extensions)
1234 (let ((file (concat out-dir base-name "." ext)))
1235 (when (file-exists-p file) (delete-file file)))))
1236 (message (concat "Process completed"
1237 (if (not errors) "."
1238 (concat " with errors: " errors)))))
1239 ;; Return output file name.
1240 pdffile))))
1242 (defun org-man-collect-errors (buffer)
1243 "Collect some kind of errors from \"groff\" output
1244 BUFFER is the buffer containing output.
1245 Return collected error types as a string, or nil if there was
1246 none."
1247 (with-current-buffer buffer
1248 (save-excursion
1249 (goto-char (point-max))
1250 ;; Find final run
1251 nil )))
1254 (provide 'ox-man)
1256 ;; Local variables:
1257 ;; generated-autoload-file: "org-loaddefs.el"
1258 ;; End:
1260 ;;; ox-man.el ends here