org-agenda.el: New options to limit the number of displayed entries
[org-mode.git] / contrib / lisp / org-e-man.el
blob461ed6171e1e4887a384eb9cf903eb1daf8ec388
1 ;; org-e-man.el --- Man Back-End For Org Export Engine
3 ;; Copyright (C) 2011-2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Author: Luis R Anaya <papoanaya aroba hot mail punto com>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;;
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This library implements a Man back-end for Org generic exporter.
27 ;; To test it, run
29 ;; M-: (org-export-to-buffer 'e-man "*Test e-Man*") RET
31 ;; in an org-mode buffer then switch to the buffer to see the Man
32 ;; export. See contrib/lisp/org-export.el for more details on how
33 ;; this exporter works.
35 ;; It introduces one new buffer keywords:
36 ;; "MAN_CLASS_OPTIONS".
38 ;;; Code:
40 (require 'org-export)
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 e-man
53 ((babel-call . org-e-man-babel-call)
54 (bold . org-e-man-bold)
55 (center-block . org-e-man-center-block)
56 (clock . org-e-man-clock)
57 (code . org-e-man-code)
58 (comment . org-e-man-comment)
59 (comment-block . org-e-man-comment-block)
60 (drawer . org-e-man-drawer)
61 (dynamic-block . org-e-man-dynamic-block)
62 (entity . org-e-man-entity)
63 (example-block . org-e-man-example-block)
64 (export-block . org-e-man-export-block)
65 (export-snippet . org-e-man-export-snippet)
66 (fixed-width . org-e-man-fixed-width)
67 (footnote-definition . org-e-man-footnote-definition)
68 (footnote-reference . org-e-man-footnote-reference)
69 (headline . org-e-man-headline)
70 (horizontal-rule . org-e-man-horizontal-rule)
71 (inline-babel-call . org-e-man-inline-babel-call)
72 (inline-src-block . org-e-man-inline-src-block)
73 (inlinetask . org-e-man-inlinetask)
74 (italic . org-e-man-italic)
75 (item . org-e-man-item)
76 (keyword . org-e-man-keyword)
77 (man-environment . org-e-man-man-environment)
78 (man-fragment . org-e-man-man-fragment)
79 (line-break . org-e-man-line-break)
80 (link . org-e-man-link)
81 (paragraph . org-e-man-paragraph)
82 (plain-list . org-e-man-plain-list)
83 (plain-text . org-e-man-plain-text)
84 (planning . org-e-man-planning)
85 (property-drawer . org-e-man-property-drawer)
86 (quote-block . org-e-man-quote-block)
87 (quote-section . org-e-man-quote-section)
88 (radio-target . org-e-man-radio-target)
89 (section . org-e-man-section)
90 (special-block . org-e-man-special-block)
91 (src-block . org-e-man-src-block)
92 (statistics-cookie . org-e-man-statistics-cookie)
93 (strike-through . org-e-man-strike-through)
94 (subscript . org-e-man-subscript)
95 (superscript . org-e-man-superscript)
96 (table . org-e-man-table)
97 (table-cell . org-e-man-table-cell)
98 (table-row . org-e-man-table-row)
99 (target . org-e-man-target)
100 (template . org-e-man-template)
101 (timestamp . org-e-man-timestamp)
102 (underline . org-e-man-underline)
103 (verbatim . org-e-man-verbatim)
104 (verse-block . org-e-man-verse-block))
105 :export-block "MAN"
106 :menu-entry
107 (?m "Export to MAN"
108 ((?m "As MAN file" org-e-man-export-to-man)
109 (?p "As PDF file" org-e-man-export-to-pdf)
110 (?o "As PDF file and open"
111 (lambda (a s v b)
112 (if a (org-e-man-export-to-pdf t s v b)
113 (org-open-file (org-e-man-export-to-pdf nil s v b)))))))
114 :options-alist
115 ((:man-class "MAN_CLASS" nil nil t)
116 (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
117 (:man-header-extra "MAN_HEADER" nil nil newline)))
121 ;;; User Configurable Variables
123 (defgroup org-export-e-man nil
124 "Options for exporting Org mode files to Man."
125 :tag "Org Export Man"
126 :group 'org-export)
128 ;;; Tables
130 (defcustom org-e-man-tables-centered t
131 "When non-nil, tables are exported in a center environment."
132 :group 'org-export-e-man
133 :type 'boolean)
135 (defcustom org-e-man-tables-verbatim nil
136 "When non-nil, tables are exported verbatim."
137 :group 'org-export-e-man
138 :type 'boolean)
141 (defcustom org-e-man-table-scientific-notation "%sE%s"
142 "Format string to display numbers in scientific notation.
143 The format should have \"%s\" twice, for mantissa and exponent
144 \(i.e. \"%s\\\\times10^{%s}\").
146 When nil, no transformation is made."
147 :group 'org-export-e-man
148 :type '(choice
149 (string :tag "Format string")
150 (const :tag "No formatting")))
153 ;;; Inlinetasks
154 ;; Src blocks
156 (defcustom org-e-man-source-highlight nil
157 "Use GNU source highlight to embellish source blocks "
158 :group 'org-export-e-man
159 :type 'boolean)
162 (defcustom org-e-man-source-highlight-langs
163 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
164 (scheme "scheme")
165 (c "c") (cc "cpp") (csharp "csharp") (d "d")
166 (fortran "fortran") (cobol "cobol") (pascal "pascal")
167 (ada "ada") (asm "asm")
168 (perl "perl") (cperl "perl")
169 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
170 (java "java") (javascript "javascript")
171 (tex "latex")
172 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
173 (ocaml "caml") (caml "caml")
174 (sql "sql") (sqlite "sql")
175 (html "html") (css "css") (xml "xml")
176 (bat "bat") (bison "bison") (clipper "clipper")
177 (ldap "ldap") (opa "opa")
178 (php "php") (postscript "postscript") (prolog "prolog")
179 (properties "properties") (makefile "makefile")
180 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
181 "Alist mapping languages to their listing language counterpart.
182 The key is a symbol, the major mode symbol without the \"-mode\".
183 The value is the string that should be inserted as the language
184 parameter for the listings package. If the mode name and the
185 listings name are the same, the language does not need an entry
186 in this list - but it does not hurt if it is present."
187 :group 'org-export-e-man
188 :type '(repeat
189 (list
190 (symbol :tag "Major mode ")
191 (string :tag "Listings language"))))
195 (defvar org-e-man-custom-lang-environments nil
196 "Alist mapping languages to language-specific Man environments.
198 It is used during export of src blocks by the listings and
199 man packages. For example,
201 \(setq org-e-man-custom-lang-environments
202 '\(\(python \"pythoncode\"\)\)\)
204 would have the effect that if org encounters begin_src python
205 during man export."
209 ;;; Compilation
211 (defcustom org-e-man-pdf-process
212 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
213 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
214 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
216 "Commands to process a Man file to a PDF file.
217 This is a list of strings, each of them will be given to the
218 shell as a command. %f in the command will be replaced by the
219 full file name, %b by the file base name (i.e. without directory
220 and extension parts) and %o by the base directory of the file.
223 By default, Org uses 3 runs of to do the processing.
225 Alternatively, this may be a Lisp function that does the
226 processing. This function should accept the file name as
227 its single argument."
228 :group 'org-export-pdf
229 :type '(choice
230 (repeat :tag "Shell command sequence"
231 (string :tag "Shell command"))
232 (const :tag "2 runs of pdfgroff"
233 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
234 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
235 (const :tag "3 runs of pdfgroff"
236 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
237 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
238 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
239 (function)))
241 (defcustom org-e-man-logfiles-extensions
242 '("log" "out" "toc")
243 "The list of file extensions to consider as Man logfiles."
244 :group 'org-export-e-man
245 :type '(repeat (string :tag "Extension")))
247 (defcustom org-e-man-remove-logfiles t
248 "Non-nil means remove the logfiles produced by PDF production.
249 These are the .aux, .log, .out, and .toc files."
250 :group 'org-export-e-man
251 :type 'boolean)
255 ;;; Internal Functions
257 (defun org-e-man--caption/label-string (element info)
258 "Return caption and label Man string for ELEMENT.
260 INFO is a plist holding contextual information. If there's no
261 caption nor label, return the empty string.
263 For non-floats, see `org-e-man--wrap-label'."
264 (let ((label (org-element-property :label element))
265 (main (org-export-get-caption element))
266 (short (org-export-get-caption element t)))
267 (cond ((and (not main) (not label)) "")
268 ((not main) (format "\\fI%s\\fP" label))
269 ;; Option caption format with short name.
270 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
271 (org-export-data short info)
272 (org-export-data main info)))
273 ;; Standard caption format.
274 (t (format "\\fR%s\\fP" (org-export-data main info))))))
276 (defun org-e-man--wrap-label (element output)
277 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
278 This function shouldn't be used for floats. See
279 `org-e-man--caption/label-string'."
280 (let ((label (org-element-property :name element)))
281 (if (or (not output) (not label) (string= output "") (string= label ""))
282 output
283 (concat (format "%s\n.br\n" label) output))))
287 ;;; Template
289 (defun org-e-man-template (contents info)
290 "Return complete document string after Man conversion.
291 CONTENTS is the transcoded contents string. INFO is a plist
292 holding export options."
293 (let* ((title (org-export-data (plist-get info :title) info))
294 (attr (read (format "(%s)"
295 (mapconcat
296 #'identity
297 (list (plist-get info :man-class-options))
298 " "))))
299 (section-item (plist-get attr :section-id)))
301 (concat
303 (cond
304 ((and title (stringp section-item))
305 (format ".TH \"%s\" \"%s\" \n" title section-item))
306 ((and (string= "" title) (stringp section-item))
307 (format ".TH \"%s\" \"%s\" \n" " " section-item))
308 (title
309 (format ".TH \"%s\" \"1\" \n" title))
311 ".TH \" \" \"1\" "))
312 contents)))
317 ;;; Transcode Functions
319 ;;; Babel Call
321 ;; Babel Calls are ignored.
324 ;;; Bold
326 (defun org-e-man-bold (bold contents info)
327 "Transcode BOLD from Org to Man.
328 CONTENTS is the text with bold markup. INFO is a plist holding
329 contextual information."
330 (format "\\fB%s\\fP" contents))
333 ;;; Center Block
335 (defun org-e-man-center-block (center-block contents info)
336 "Transcode a CENTER-BLOCK element from Org to Man.
337 CONTENTS holds the contents of the center block. INFO is a plist
338 holding contextual information."
339 (org-e-man--wrap-label
340 center-block
341 (format ".ce %d\n.nf\n%s\n.fi"
342 (- (length (split-string contents "\n")) 1 )
343 contents)))
346 ;;; Clock
348 (defun org-e-man-clock (clock contents info)
349 "Transcode a CLOCK element from Org to Man.
350 CONTENTS is nil. INFO is a plist holding contextual
351 information."
352 "" )
355 ;;; Code
357 (defun org-e-man-code (code contents info)
358 "Transcode a CODE object from Org to Man.
359 CONTENTS is nil. INFO is a plist used as a communication
360 channel."
361 (format "\\fC%s\\fP" code))
364 ;;; Comment
366 ;; Comments are ignored.
369 ;;; Comment Block
371 ;; Comment Blocks are ignored.
374 ;;; Drawer
376 (defun org-e-man-drawer (drawer contents info)
377 "Transcode a DRAWER element from Org to Man.
378 DRAWER holds the drawer information
379 CONTENTS holds the contents of the block.
380 INFO is a plist holding contextual information. "
381 contents)
384 ;;; Dynamic Block
386 (defun org-e-man-dynamic-block (dynamic-block contents info)
387 "Transcode a DYNAMIC-BLOCK element from Org to Man.
388 CONTENTS holds the contents of the block. INFO is a plist
389 holding contextual information. See `org-export-data'."
390 (org-e-man--wrap-label dynamic-block contents))
393 ;;; Entity
395 (defun org-e-man-entity (entity contents info)
396 "Transcode an ENTITY object from Org to Man.
397 CONTENTS are the definition itself. INFO is a plist holding
398 contextual information."
399 (org-element-property :utf-8 entity))
402 ;;; Example Block
404 (defun org-e-man-example-block (example-block contents info)
405 "Transcode an EXAMPLE-BLOCK element from Org to Man.
406 CONTENTS is nil. INFO is a plist holding contextual
407 information."
408 (org-e-man--wrap-label
409 example-block
410 (format ".RS\n.nf\n%s\n.fi\n.RE"
411 (org-export-format-code-default example-block info))))
414 ;;; Export Block
416 (defun org-e-man-export-block (export-block contents info)
417 "Transcode a EXPORT-BLOCK element from Org to Man.
418 CONTENTS is nil. INFO is a plist holding contextual information."
419 (when (string= (org-element-property :type export-block) "MAN")
420 (org-remove-indentation (org-element-property :value export-block))))
423 ;;; Export Snippet
425 (defun org-e-man-export-snippet (export-snippet contents info)
426 "Transcode a EXPORT-SNIPPET object from Org to Man.
427 CONTENTS is nil. INFO is a plist holding contextual information."
428 (when (eq (org-export-snippet-backend export-snippet) 'e-man)
429 (org-element-property :value export-snippet)))
432 ;;; Fixed Width
434 (defun org-e-man-fixed-width (fixed-width contents info)
435 "Transcode a FIXED-WIDTH element from Org to Man.
436 CONTENTS is nil. INFO is a plist holding contextual information."
437 (org-e-man--wrap-label
438 fixed-width
439 (format "\\fC\n%s\\fP"
440 (org-remove-indentation
441 (org-element-property :value fixed-width)))))
444 ;;; Footnote Definition
446 ;; Footnote Definitions are ignored.
448 ;;; Footnote References
450 ;; Footnote References are Ignored
453 ;;; Headline
455 (defun org-e-man-headline (headline contents info)
456 "Transcode an HEADLINE element from Org to Man.
457 CONTENTS holds the contents of the headline. INFO is a plist
458 holding contextual information."
459 (let* ((level (org-export-get-relative-level headline info))
460 (numberedp (org-export-numbered-headline-p headline info))
461 ;; Section formatting will set two placeholders: one for the
462 ;; title and the other for the contents.
463 (section-fmt
464 (case level
465 (1 ".SH \"%s\"\n%s")
466 (2 ".SS \"%s\"\n%s")
467 (3 ".SS \"%s\"\n%s")
468 (t nil)))
469 (text (org-export-data (org-element-property :title headline) info)))
471 (cond
472 ;; Case 1: This is a footnote section: ignore it.
473 ((org-element-property :footnote-section-p headline) nil)
475 ;; Case 2. This is a deep sub-tree: export it as a list item.
476 ;; Also export as items headlines for which no section
477 ;; format has been found.
478 ((or (not section-fmt) (org-export-low-level-p headline info))
479 ;; Build the real contents of the sub-tree.
480 (let ((low-level-body
481 (concat
482 ;; If the headline is the first sibling, start a list.
483 (when (org-export-first-sibling-p headline info)
484 (format "%s\n" ".RS"))
485 ;; Itemize headline
486 ".TP\n.ft I\n" text "\n.ft\n"
487 contents ".RE")))
488 ;; If headline is not the last sibling simply return
489 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
490 ;; blank line.
491 (if (not (org-export-last-sibling-p headline info)) low-level-body
492 (replace-regexp-in-string
493 "[ \t\n]*\\'" ""
494 low-level-body))))
496 ;; Case 3. Standard headline. Export it as a section.
497 (t (format section-fmt text contents )))))
499 ;;; Horizontal Rule
500 ;; Not supported
502 ;;; Inline Babel Call
504 ;; Inline Babel Calls are ignored.
506 ;;; Inline Src Block
508 (defun org-e-man-inline-src-block (inline-src-block contents info)
509 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
510 CONTENTS holds the contents of the item. INFO is a plist holding
511 contextual information."
512 (let* ((code (org-element-property :value inline-src-block)))
513 (cond
514 (org-e-man-source-highlight
515 (let* ((tmpdir (if (featurep 'xemacs)
516 temp-directory
517 temporary-file-directory ))
518 (in-file (make-temp-name
519 (expand-file-name "srchilite" tmpdir)))
520 (out-file (make-temp-name
521 (expand-file-name "reshilite" tmpdir)))
522 (org-lang (org-element-property :language inline-src-block))
523 (lst-lang (cadr (assq (intern org-lang)
524 org-e-man-source-highlight-langs)))
526 (cmd (concat (expand-file-name "source-highlight")
527 " -s " lst-lang
528 " -f groff_man"
529 " -i " in-file
530 " -o " out-file )))
532 (if lst-lang
533 (let ((code-block "" ))
534 (with-temp-file in-file (insert code))
535 (shell-command cmd)
536 (setq code-block (org-file-contents out-file))
537 (delete-file in-file)
538 (delete-file out-file)
539 code-block)
540 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
541 code))))
543 ;; Do not use a special package: transcode it verbatim.
545 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
546 "\\fP\n.fi\n.RE\n")))))
549 ;;; Inlinetask
550 ;;; Italic
552 (defun org-e-man-italic (italic contents info)
553 "Transcode ITALIC from Org to Man.
554 CONTENTS is the text with italic markup. INFO is a plist holding
555 contextual information."
556 (format "\\fI%s\\fP" contents))
559 ;;; Item
562 (defun org-e-man-item (item contents info)
564 "Transcode an ITEM element from Org to Man.
565 CONTENTS holds the contents of the item. INFO is a plist holding
566 contextual information."
568 (let* ((bullet (org-element-property :bullet item))
569 (type (org-element-property :type (org-element-property :parent item)))
570 (checkbox (case (org-element-property :checkbox item)
571 (on "\\o'\\(sq\\(mu'") ;;
572 (off "\\(sq ") ;;
573 (trans "\\o'\\(sq\\(mi'" ))) ;;
575 (tag (let ((tag (org-element-property :tag item)))
576 ;; Check-boxes must belong to the tag.
577 (and tag (format "\\fB%s\\fP"
578 (concat checkbox
579 (org-export-data tag info)))))))
581 (if (and (null tag )
582 (null checkbox))
583 (let* ((bullet (org-trim bullet))
584 (marker (cond ((string= "-" bullet) "\\(em")
585 ((string= "*" bullet) "\\(bu")
586 ((eq type 'ordered)
587 (format "%s " (org-trim bullet)))
588 (t "\\(dg"))))
589 (concat ".IP " marker " 4\n"
590 (org-trim (or contents " " ))))
591 ; else
592 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
593 (org-trim (or contents " " ))))))
595 ;;; Keyword
598 (defun org-e-man-keyword (keyword contents info)
599 "Transcode a KEYWORD element from Org to Man.
600 CONTENTS is nil. INFO is a plist holding contextual information."
601 (let ((key (org-element-property :key keyword))
602 (value (org-element-property :value keyword)))
603 (cond
604 ((string= key "MAN") value)
605 ((string= key "INDEX") nil)
606 ;; Invisible targets.
607 ((string= key "TARGET") nil)
608 ((string= key "TOC" ) nil))))
611 ;;; Man Environment
613 (defun org-e-man-man-environment (man-environment contents info)
614 "Transcode a MAN-ENVIRONMENT element from Org to Man.
615 CONTENTS is nil. INFO is a plist holding contextual information."
616 (let ((label (org-element-property :name man-environment))
617 (value (org-remove-indentation
618 (org-element-property :value man-environment))))
619 (if (not (org-string-nw-p label)) value
620 ;; Environment is labelled: label must be within the environment
621 ;; (otherwise, a reference pointing to that element will count
622 ;; the section instead).
623 (with-temp-buffer
624 (insert value)
625 (goto-char (point-min))
626 (forward-line)
627 (insert (format "%s\n" label))
628 (buffer-string)))))
631 ;;; Man Fragment
633 (defun org-e-man-man-fragment (man-fragment contents info)
634 "Transcode a MAN-FRAGMENT object from Org to Man.
635 CONTENTS is nil. INFO is a plist holding contextual information."
636 (org-element-property :value man-fragment))
639 ;;; Line Break
641 (defun org-e-man-line-break (line-break contents info)
642 "Transcode a LINE-BREAK object from Org to Man.
643 CONTENTS is nil. INFO is a plist holding contextual information."
644 ".br\n")
647 ;;; Link
650 (defun org-e-man-link (link desc info)
651 "Transcode a LINK object from Org to Man.
653 DESC is the description part of the link, or the empty string.
654 INFO is a plist holding contextual information. See
655 `org-export-data'."
657 (let* ((type (org-element-property :type link))
658 (raw-path (org-element-property :path link))
659 ;; Ensure DESC really exists, or set it to nil.
660 (desc (and (not (string= desc "")) desc))
662 (path (cond
663 ((member type '("http" "https" "ftp" "mailto"))
664 (concat type ":" raw-path))
665 ((string= type "file")
666 (when (string-match "\\(.+\\)::.+" raw-path)
667 (setq raw-path (match-string 1 raw-path)))
668 (if (file-name-absolute-p raw-path)
669 (concat "file://" (expand-file-name raw-path))
670 (concat "file://" raw-path)))
671 (t raw-path)))
672 protocol)
673 (cond
674 ;; External link with a description part.
675 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
676 ;; External link without a description part.
677 (path (format "\\fI%s\\fP" path))
678 ;; No path, only description. Try to do something useful.
679 (t (format "\\fI%s\\fP" desc)))))
682 ;;; Paragraph
684 (defun org-e-man-paragraph (paragraph contents info)
685 "Transcode a PARAGRAPH element from Org to Man.
686 CONTENTS is the contents of the paragraph, as a string. INFO is
687 the plist used as a communication channel."
688 (let ((parent (plist-get (nth 1 paragraph) :parent)))
689 (when parent
690 (let ((parent-type (car parent))
691 (fixed-paragraph ""))
692 (cond ((and (eq parent-type 'item)
693 (plist-get (nth 1 parent) :bullet ))
694 (setq fixed-paragraph (concat "" contents)))
695 ((eq parent-type 'section)
696 (setq fixed-paragraph (concat ".PP\n" contents)))
697 ((eq parent-type 'footnote-definition)
698 (setq fixed-paragraph contents))
699 (t (setq fixed-paragraph (concat "" contents))))
700 fixed-paragraph ))))
703 ;;; Plain List
705 (defun org-e-man-plain-list (plain-list contents info)
706 "Transcode a PLAIN-LIST element from Org to Man.
707 CONTENTS is the contents of the list. INFO is a plist holding
708 contextual information."
709 contents)
711 ;;; Plain Text
713 (defun org-e-man-plain-text (text info)
714 "Transcode a TEXT string from Org to Man.
715 TEXT is the string to transcode. INFO is a plist holding
716 contextual information."
717 (let ((output text))
718 ;; Protect various chars.
719 (setq output (replace-regexp-in-string
720 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
721 "$\\" output nil t 1))
722 ;; Activate smart quotes. Be sure to provide original TEXT string
723 ;; since OUTPUT may have been modified.
724 (when (plist-get info :with-smart-quotes)
725 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
726 ;; Handle break preservation if required.
727 (when (plist-get info :preserve-breaks)
728 (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
729 output)))
730 ;; Return value.
731 output))
735 ;;; Planning
738 ;;; Property Drawer
741 ;;; Quote Block
743 (defun org-e-man-quote-block (quote-block contents info)
744 "Transcode a QUOTE-BLOCK element from Org to Man.
745 CONTENTS holds the contents of the block. INFO is a plist
746 holding contextual information."
747 (org-e-man--wrap-label
748 quote-block
749 (format ".RS\n%s\n.RE" contents)))
751 ;;; Quote Section
753 (defun org-e-man-quote-section (quote-section contents info)
754 "Transcode a QUOTE-SECTION element from Org to Man.
755 CONTENTS is nil. INFO is a plist holding contextual information."
756 (let ((value (org-remove-indentation
757 (org-element-property :value quote-section))))
758 (when value (format ".RS\\fI%s\\fP\n.RE\n" value))))
761 ;;; Radio Target
763 (defun org-e-man-radio-target (radio-target text info)
764 "Transcode a RADIO-TARGET object from Org to Man.
765 TEXT is the text of the target. INFO is a plist holding
766 contextual information."
767 text )
770 ;;; Section
772 (defun org-e-man-section (section contents info)
773 "Transcode a SECTION element from Org to Man.
774 CONTENTS holds the contents of the section. INFO is a plist
775 holding contextual information."
776 contents)
779 ;;; Special Block
781 (defun org-e-man-special-block (special-block contents info)
782 "Transcode a SPECIAL-BLOCK element from Org to Man.
783 CONTENTS holds the contents of the block. INFO is a plist
784 holding contextual information."
785 (let ((type (downcase (org-element-property :type special-block))))
786 (org-e-man--wrap-label
787 special-block
788 (format "%s\n" contents))))
791 ;;; Src Block
793 (defun org-e-man-src-block (src-block contents info)
794 "Transcode a SRC-BLOCK element from Org to Man.
795 CONTENTS holds the contents of the item. INFO is a plist holding
796 contextual information."
797 (let* ((lang (org-element-property :language src-block))
798 (code (org-element-property :value src-block))
799 (custom-env (and lang
800 (cadr (assq (intern lang)
801 org-e-man-custom-lang-environments))))
802 (num-start (case (org-element-property :number-lines src-block)
803 (continued (org-export-get-loc src-block info))
804 (new 0)))
805 (retain-labels (org-element-property :retain-labels src-block)))
806 (cond
807 ;; Case 1. No source fontification.
808 ((not org-e-man-source-highlight)
809 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
810 (org-export-format-code-default src-block info)))
811 (org-e-man-source-highlight
812 (let* ((tmpdir (if (featurep 'xemacs)
813 temp-directory
814 temporary-file-directory ))
816 (in-file (make-temp-name
817 (expand-file-name "srchilite" tmpdir)))
818 (out-file (make-temp-name
819 (expand-file-name "reshilite" tmpdir)))
821 (org-lang (org-element-property :language src-block))
822 (lst-lang (cadr (assq (intern org-lang)
823 org-e-man-source-highlight-langs)))
825 (cmd (concat "source-highlight"
826 " -s " lst-lang
827 " -f groff_man "
828 " -i " in-file
829 " -o " out-file)))
831 (if lst-lang
832 (let ((code-block ""))
833 (with-temp-file in-file (insert code))
834 (shell-command cmd)
835 (setq code-block (org-file-contents out-file))
836 (delete-file in-file)
837 (delete-file out-file)
838 code-block)
839 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code)))))))
842 ;;; Statistics Cookie
844 (defun org-e-man-statistics-cookie (statistics-cookie contents info)
845 "Transcode a STATISTICS-COOKIE object from Org to Man.
846 CONTENTS is nil. INFO is a plist holding contextual information."
847 (org-element-property :value statistics-cookie))
850 ;;; Strike-Through
852 (defun org-e-man-strike-through (strike-through contents info)
853 "Transcode STRIKE-THROUGH from Org to Man.
854 CONTENTS is the text with strike-through markup. INFO is a plist
855 holding contextual information."
856 (format "\\fI%s\\fP" contents))
858 ;;; Subscript
860 (defun org-e-man-subscript (subscript contents info)
861 "Transcode a SUBSCRIPT object from Org to Man.
862 CONTENTS is the contents of the object. INFO is a plist holding
863 contextual information."
864 (format "\\d\\s-2%s\\s+2\\u" contents))
866 ;;; Superscript "^_%s$
868 (defun org-e-man-superscript (superscript contents info)
869 "Transcode a SUPERSCRIPT object from Org to Man.
870 CONTENTS is the contents of the object. INFO is a plist holding
871 contextual information."
872 (format "\\u\\s-2%s\\s+2\\d" contents))
875 ;;; Table
877 ;; `org-e-man-table' is the entry point for table transcoding. It
878 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
879 ;; delegates the job to either `org-e-man-table--table.el-table' or
880 ;; `org-e-man-table--org-table' functions, depending of the type of
881 ;; the table.
883 ;; `org-e-man-table--align-string' is a subroutine used to build
884 ;; alignment string for Org tables.
886 (defun org-e-man-table (table contents info)
887 "Transcode a TABLE element from Org to Man.
888 CONTENTS is the contents of the table. INFO is a plist holding
889 contextual information."
890 (cond
891 ;; Case 1: verbatim table.
892 ((or org-e-man-tables-verbatim
893 (let ((attr (read (format "(%s)"
894 (mapconcat
895 #'identity
896 (org-element-property :attr_man table)
897 " ")))))
899 (and attr (plist-get attr :verbatim))))
901 (format ".nf\n\\fC%s\\fP\n.fi"
902 ;; Re-create table, without affiliated keywords.
903 (org-trim
904 (org-element-interpret-data
905 `(table nil ,@(org-element-contents table))))))
906 ;; Case 2: Standard table.
907 (t (org-e-man-table--org-table table contents info))))
909 (defun org-e-man-table--align-string (divider table info)
910 "Return an appropriate Man alignment string.
911 TABLE is the considered table. INFO is a plist used as
912 a communication channel."
913 (let (alignment)
914 ;; Extract column groups and alignment from first (non-rule)
915 ;; row.
916 (org-element-map
917 (org-element-map
918 table 'table-row
919 (lambda (row)
920 (and (eq (org-element-property :type row) 'standard) row))
921 info 'first-match)
922 'table-cell
923 (lambda (cell)
924 (let* ((borders (org-export-table-cell-borders cell info))
925 (raw-width (org-export-table-cell-width cell info))
926 (width-cm (when raw-width (/ raw-width 5)))
927 (width (if raw-width (format "w(%dc)"
928 (if (< width-cm 1) 1 width-cm)) "")))
929 ;; Check left border for the first cell only.
930 (when (and (memq 'left borders) (not alignment))
931 (push "|" alignment))
932 (push
933 (case (org-export-table-cell-alignment cell info)
934 (left (concat "l" width divider))
935 (right (concat "r" width divider))
936 (center (concat "c" width divider)))
937 alignment)
938 (when (memq 'right borders) (push "|" alignment))))
939 info)
940 (apply 'concat (reverse alignment))))
942 (defun org-e-man-table--org-table (table contents info)
943 "Return appropriate Man code for an Org table.
945 TABLE is the table type element to transcode. CONTENTS is its
946 contents, as a string. INFO is a plist used as a communication
947 channel.
949 This function assumes TABLE has `org' as its `:type' attribute."
950 (let* ((attr (org-export-read-attribute :attr_man table))
951 (label (org-element-property :name table))
952 (caption (and (not (plist-get attr :disable-caption))
953 (org-e-man--caption/label-string table info)))
954 (divider (if (plist-get attr :divider) "|" " "))
956 ;; Determine alignment string.
957 (alignment (org-e-man-table--align-string divider table info))
958 ;; Extract others display options.
960 (lines (org-split-string contents "\n"))
962 (attr-list
963 (let ((result-list '()))
964 (dolist (attr-item
965 (list
966 (if (plist-get attr :expand)
967 "expand" nil)
969 (case (plist-get attr :placement)
970 ('center "center")
971 ('left nil)
973 (if org-e-man-tables-centered
974 "center" "")))
976 (case (plist-get attr :boxtype)
977 ('box "box")
978 ('doublebox "doublebox")
979 ('allbox "allbox")
980 ('none nil)
981 (t "box"))))
983 (if attr-item
984 (add-to-list 'result-list attr-item)))
985 result-list ))
988 (title-line (plist-get attr :title-line))
989 (long-cells (plist-get attr :long-cells))
991 (table-format (concat
992 (format "%s" (or (car attr-list) "" ))
994 (let ((output-list '()))
995 (when (cdr attr-list)
996 (dolist (attr-item (cdr attr-list))
997 (setq output-list (concat output-list (format ",%s" attr-item)))))
998 output-list)
999 "")))
1001 (first-line (when lines (org-split-string (car lines) "\t"))))
1002 ;; Prepare the final format string for the table.
1005 (cond
1006 ;; Others.
1007 (lines (concat ".TS\n " table-format ";\n"
1009 (format "%s.\n"
1010 (let ((final-line ""))
1011 (when title-line
1012 (dotimes (i (length first-line))
1013 (setq final-line (concat final-line "cb" divider))))
1015 (setq final-line (concat final-line "\n"))
1017 (if alignment
1018 (setq final-line (concat final-line alignment))
1019 (dotimes (i (length first-line))
1020 (setq final-line (concat final-line "c" divider))))
1021 final-line ))
1023 (format "%s.TE\n"
1024 (let ((final-line "")
1025 (long-line "")
1026 (lines (org-split-string contents "\n")))
1028 (dolist (line-item lines)
1029 (setq long-line "")
1031 (if long-cells
1032 (progn
1033 (if (string= line-item "_")
1034 (setq long-line (format "%s\n" line-item))
1035 ;; else string =
1036 (let ((cell-item-list (org-split-string line-item "\t")))
1037 (dolist (cell-item cell-item-list)
1039 (cond ((eq cell-item (car (last cell-item-list)))
1040 (setq long-line (concat long-line
1041 (format "T{\n%s\nT}\t\n" cell-item ))))
1043 (setq long-line (concat long-line
1044 (format "T{\n%s\nT}\t" cell-item ))))))
1045 long-line))
1046 ;; else long cells
1047 (setq final-line (concat final-line long-line )))
1049 (setq final-line (concat final-line line-item "\n"))))
1050 final-line))
1052 (and caption (format ".TB \"%s\"" caption)))))))
1054 ;;; Table Cell
1056 (defun org-e-man-table-cell (table-cell contents info)
1057 "Transcode a TABLE-CELL element from Org to Man
1058 CONTENTS is the cell contents. INFO is a plist used as
1059 a communication channel."
1060 (concat (if (and contents
1061 org-e-man-table-scientific-notation
1062 (string-match orgtbl-exp-regexp contents))
1063 ;; Use appropriate format string for scientific
1064 ;; notation.
1065 (format org-e-man-table-scientific-notation
1066 (match-string 1 contents)
1067 (match-string 2 contents))
1068 contents )
1069 (when (org-export-get-next-element table-cell info) "\t")))
1072 ;;; Table Row
1074 (defun org-e-man-table-row (table-row contents info)
1075 "Transcode a TABLE-ROW element from Org to Man
1076 CONTENTS is the contents of the row. INFO is a plist used as
1077 a communication channel."
1078 ;; Rules are ignored since table separators are deduced from
1079 ;; borders of the current row.
1080 (when (eq (org-element-property :type table-row) 'standard)
1081 (let* ((attr (mapconcat 'identity
1082 (org-element-property
1083 :attr_man (org-export-get-parent table-row))
1084 " "))
1085 ;; TABLE-ROW's borders are extracted from its first cell.
1086 (borders
1087 (org-export-table-cell-borders
1088 (car (org-element-contents table-row)) info)))
1089 (concat
1090 ;; Mark horizontal lines
1091 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1092 contents
1094 (cond
1095 ;; When BOOKTABS are activated enforce bottom rule even when
1096 ;; no hline was specifically marked.
1097 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1098 ((memq 'below borders) "\n_"))))))
1101 ;;; Target
1103 (defun org-e-man-target (target contents info)
1104 "Transcode a TARGET object from Org to Man.
1105 CONTENTS is nil. INFO is a plist holding contextual
1106 information."
1107 (format "\\fI%s\\fP"
1108 (org-export-solidify-link-text (org-element-property :value target))))
1111 ;;; Timestamp
1113 (defun org-e-man-timestamp (timestamp contents info)
1114 "Transcode a TIMESTAMP object from Org to Man.
1115 CONTENTS is nil. INFO is a plist holding contextual
1116 information."
1117 "" )
1120 ;;; Underline
1122 (defun org-e-man-underline (underline contents info)
1123 "Transcode UNDERLINE from Org to Man.
1124 CONTENTS is the text with underline markup. INFO is a plist
1125 holding contextual information."
1126 (format "\\fI%s\\fP" contents))
1129 ;;; Verbatim
1131 (defun org-e-man-verbatim (verbatim contents info)
1132 "Transcode a VERBATIM object from Org to Man.
1133 CONTENTS is nil. INFO is a plist used as a communication
1134 channel."
1135 (format ".nf\n%s\n.fi" contents))
1138 ;;; Verse Block
1140 (defun org-e-man-verse-block (verse-block contents info)
1141 "Transcode a VERSE-BLOCK element from Org to Man.
1142 CONTENTS is verse block contents. INFO is a plist holding
1143 contextual information."
1144 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1148 ;;; Interactive functions
1150 (defun org-e-man-export-to-man
1151 (&optional async subtreep visible-only body-only ext-plist)
1152 "Export current buffer to a Man file.
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 the body
1171 without any 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 output file's name."
1178 (interactive)
1179 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1180 (if async
1181 (org-export-async-start
1182 (lambda (f) (org-export-add-to-stack f 'e-man))
1183 `(expand-file-name
1184 (org-export-to-file
1185 'e-man ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))
1186 (org-export-to-file
1187 'e-man outfile subtreep visible-only body-only ext-plist))))
1189 (defun org-e-man-export-to-pdf
1190 (&optional async subtreep visible-only body-only ext-plist)
1191 "Export current buffer to Groff then process through to PDF.
1193 If narrowing is active in the current buffer, only export its
1194 narrowed part.
1196 If a region is active, export that region.
1198 A non-nil optional argument ASYNC means the process should happen
1199 asynchronously. The resulting file should be accessible through
1200 the `org-export-stack' interface.
1202 When optional argument SUBTREEP is non-nil, export the sub-tree
1203 at point, extracting information from the headline properties
1204 first.
1206 When optional argument VISIBLE-ONLY is non-nil, don't export
1207 contents of hidden elements.
1209 When optional argument BODY-ONLY is non-nil, only write between
1210 markers.
1212 EXT-PLIST, when provided, is a property list with external
1213 parameters overriding Org default settings, but still inferior to
1214 file-local settings.
1216 Return PDF file's name."
1217 (interactive)
1218 (if async
1219 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1220 (org-export-async-start
1221 (lambda (f) (org-export-add-to-stack f 'e-man))
1222 `(expand-file-name
1223 (org-e-man-compile
1224 (org-export-to-file
1225 'e-man ,outfile ,subtreep ,visible-only ,body-only
1226 ',ext-plist)))))
1227 (org-e-man-compile
1228 (org-e-man-export-to-man nil subtreep visible-only body-only ext-plist))))
1230 (defun org-e-man-compile (file)
1231 "Compile a Groff file.
1233 FILE is the name of the file being compiled. Processing is done
1234 through the command specified in `org-e-man-pdf-process'.
1236 Return PDF file name or an error if it couldn't be produced."
1237 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1238 (full-name (file-truename file))
1239 (out-dir (file-name-directory file))
1240 ;; Make sure `default-directory' is set to FILE directory,
1241 ;; not to whatever value the current buffer may have.
1242 (default-directory (file-name-directory full-name))
1243 errors)
1244 (message (format "Processing Groff file %s ..." file))
1245 (save-window-excursion
1246 (cond
1247 ;; A function is provided: Apply it.
1248 ((functionp org-e-man-pdf-process)
1249 (funcall org-e-man-pdf-process (shell-quote-argument file)))
1250 ;; A list is provided: Replace %b, %f and %o with appropriate
1251 ;; values in each command before applying it. Output is
1252 ;; redirected to "*Org PDF Groff Output*" buffer.
1253 ((consp org-e-man-pdf-process)
1254 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1255 (mapc
1256 (lambda (command)
1257 (shell-command
1258 (replace-regexp-in-string
1259 "%b" (shell-quote-argument base-name)
1260 (replace-regexp-in-string
1261 "%f" (shell-quote-argument full-name)
1262 (replace-regexp-in-string
1263 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1264 outbuf))
1265 org-e-man-pdf-process)
1266 ;; Collect standard errors from output buffer.
1267 (setq errors (org-e-man-collect-errors outbuf))))
1268 (t (error "No valid command to process to PDF")))
1269 (let ((pdffile (concat out-dir base-name ".pdf")))
1270 ;; Check for process failure. Provide collected errors if
1271 ;; possible.
1272 (if (not (file-exists-p pdffile))
1273 (error (concat (format "PDF file %s wasn't produced" pdffile)
1274 (when errors (concat ": " errors))))
1275 ;; Else remove log files, when specified, and signal end of
1276 ;; process to user, along with any error encountered.
1277 (when org-e-man-remove-logfiles
1278 (dolist (ext org-e-man-logfiles-extensions)
1279 (let ((file (concat out-dir base-name "." ext)))
1280 (when (file-exists-p file) (delete-file file)))))
1281 (message (concat "Process completed"
1282 (if (not errors) "."
1283 (concat " with errors: " errors)))))
1284 ;; Return output file name.
1285 pdffile))))
1287 (defun org-e-man-collect-errors (buffer)
1288 "Collect some kind of errors from \"groff\" output
1289 BUFFER is the buffer containing output.
1290 Return collected error types as a string, or nil if there was
1291 none."
1292 (with-current-buffer buffer
1293 (save-excursion
1294 (goto-char (point-max))
1295 ;; Find final run
1296 nil )))
1299 (provide 'org-e-man)
1300 ;;; org-e-man.el ends here