ox-man.el 37.6 KB
Newer Older
Rasmus's avatar
Rasmus committed
1
;; ox-man.el --- Man Back-End for Org Export Engine -*- lexical-binding: t; -*-
Bastien Guerry's avatar
Bastien Guerry committed
2

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 2011-2020 Free Software Foundation, Inc.
Bastien Guerry's avatar
Bastien Guerry committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
;;      Luis R Anaya <papoanaya aroba hot mail punto com>
;; Keywords: outlines, hypermedia, calendar, wp

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
22
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
Bastien Guerry's avatar
Bastien Guerry committed
23 24 25 26 27 28 29 30 31

;;; Commentary:
;;
;; This library implements a Man back-end for Org generic exporter.
;;
;; To test it, run
;;
;;   M-: (org-export-to-buffer 'man "*Test Man*") RET
;;
Rasmus's avatar
Rasmus committed
32 33
;; in an Org buffer then switch to the buffer to see the Man export.
;; See ox.el for more details on how this exporter works.
Bastien Guerry's avatar
Bastien Guerry committed
34 35 36 37 38 39
;;
;; It introduces one new buffer keywords:
;; "MAN_CLASS_OPTIONS".

;;; Code:

Rasmus's avatar
Rasmus committed
40
(require 'cl-lib)
Bastien Guerry's avatar
Bastien Guerry committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
(require 'ox)

(defvar org-export-man-default-packages-alist)
(defvar org-export-man-packages-alist)
(defvar orgtbl-exp-regexp)



;;; Define Back-End

(org-export-define-backend 'man
  '((babel-call . org-man-babel-call)
    (bold . org-man-bold)
    (center-block . org-man-center-block)
    (code . org-man-code)
    (drawer . org-man-drawer)
    (dynamic-block . org-man-dynamic-block)
    (entity . org-man-entity)
    (example-block . org-man-example-block)
    (export-block . org-man-export-block)
    (export-snippet . org-man-export-snippet)
    (fixed-width . org-man-fixed-width)
    (footnote-definition . org-man-footnote-definition)
    (footnote-reference . org-man-footnote-reference)
    (headline . org-man-headline)
    (horizontal-rule . org-man-horizontal-rule)
    (inline-babel-call . org-man-inline-babel-call)
    (inline-src-block . org-man-inline-src-block)
    (inlinetask . org-man-inlinetask)
    (italic . org-man-italic)
    (item . org-man-item)
    (keyword . org-man-keyword)
    (line-break . org-man-line-break)
    (link . org-man-link)
Rasmus's avatar
Rasmus committed
75
    (node-property . org-man-node-property)
Bastien Guerry's avatar
Bastien Guerry committed
76 77 78 79
    (paragraph . org-man-paragraph)
    (plain-list . org-man-plain-list)
    (plain-text . org-man-plain-text)
    (planning . org-man-planning)
Rasmus's avatar
Rasmus committed
80
    (property-drawer . org-man-property-drawer)
Bastien Guerry's avatar
Bastien Guerry committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    (quote-block . org-man-quote-block)
    (radio-target . org-man-radio-target)
    (section . org-man-section)
    (special-block . org-man-special-block)
    (src-block . org-man-src-block)
    (statistics-cookie . org-man-statistics-cookie)
    (strike-through . org-man-strike-through)
    (subscript . org-man-subscript)
    (superscript . org-man-superscript)
    (table . org-man-table)
    (table-cell . org-man-table-cell)
    (table-row . org-man-table-row)
    (target . org-man-target)
    (template . org-man-template)
    (timestamp . org-man-timestamp)
    (underline . org-man-underline)
    (verbatim . org-man-verbatim)
    (verse-block . org-man-verse-block))
  :menu-entry
Rasmus's avatar
Rasmus committed
100
  '(?M "Export to MAN"
Bastien Guerry's avatar
Bastien Guerry committed
101 102 103 104 105 106 107 108 109
       ((?m "As MAN file" org-man-export-to-man)
	(?p "As PDF file" org-man-export-to-pdf)
	(?o "As PDF file and open"
	    (lambda (a s v b)
	      (if a (org-man-export-to-pdf t s v b)
		(org-open-file (org-man-export-to-pdf nil s v b)))))))
  :options-alist
  '((:man-class "MAN_CLASS" nil nil t)
    (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
Rasmus's avatar
Rasmus committed
110 111 112 113 114 115 116
    (:man-header-extra "MAN_HEADER" nil nil newline)
    ;; Other variables.
    (:man-tables-centered nil nil org-man-tables-centered)
    (:man-tables-verbatim nil nil org-man-tables-verbatim)
    (:man-table-scientific-notation nil nil org-man-table-scientific-notation)
    (:man-source-highlight nil nil org-man-source-highlight)
    (:man-source-highlight-langs nil nil org-man-source-highlight-langs)))
Bastien Guerry's avatar
Bastien Guerry committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161



;;; User Configurable Variables

(defgroup org-export-man nil
  "Options for exporting Org mode files to Man."
  :tag "Org Export Man"
  :group 'org-export)

;;; Tables

(defcustom org-man-tables-centered t
  "When non-nil, tables are exported in a center environment."
  :group 'org-export-man
  :version "24.4"
  :package-version '(Org . "8.0")
  :type 'boolean)

(defcustom org-man-tables-verbatim nil
  "When non-nil, tables are exported verbatim."
  :group 'org-export-man
  :version "24.4"
  :package-version '(Org . "8.0")
  :type 'boolean)


(defcustom org-man-table-scientific-notation "%sE%s"
  "Format string to display numbers in scientific notation.
The format should have \"%s\" twice, for mantissa and exponent
\(i.e. \"%s\\\\times10^{%s}\").

When nil, no transformation is made."
  :group 'org-export-man
  :version "24.4"
  :package-version '(Org . "8.0")
  :type '(choice
          (string :tag "Format string")
          (const :tag "No formatting")))


;;; Inlinetasks
;; Src blocks

(defcustom org-man-source-highlight nil
162
  "Use GNU source highlight to embellish source blocks."
Bastien Guerry's avatar
Bastien Guerry committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  :group 'org-export-man
  :version "24.4"
  :package-version '(Org . "8.0")
  :type 'boolean)


(defcustom org-man-source-highlight-langs
  '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
    (scheme "scheme")
    (c "c") (cc "cpp") (csharp "csharp") (d "d")
    (fortran "fortran") (cobol "cobol") (pascal "pascal")
    (ada "ada") (asm "asm")
    (perl "perl") (cperl "perl")
    (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
    (java "java") (javascript "javascript")
    (tex "latex")
    (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
    (ocaml "caml") (caml "caml")
    (sql "sql") (sqlite "sql")
    (html "html") (css "css") (xml "xml")
    (bat "bat") (bison "bison") (clipper "clipper")
    (ldap "ldap") (opa "opa")
    (php "php") (postscript "postscript") (prolog "prolog")
    (properties "properties") (makefile "makefile")
    (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
  "Alist mapping languages to their listing language counterpart.
The key is a symbol, the major mode symbol without the \"-mode\".
The value is the string that should be inserted as the language
parameter for the listings package.  If the mode name and the
listings name are the same, the language does not need an entry
in this list - but it does not hurt if it is present."
  :group 'org-export-man
  :version "24.4"
  :package-version '(Org . "8.0")
  :type '(repeat
          (list
           (symbol :tag "Major mode       ")
           (string :tag "Listings language"))))


;;; Compilation

(defcustom org-man-pdf-process
  '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
    "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
    "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")

  "Commands to process a Man file to a PDF file.
Rasmus's avatar
Rasmus committed
211

Bastien Guerry's avatar
Bastien Guerry committed
212 213
This is a list of strings, each of them will be given to the
shell as a command.  %f in the command will be replaced by the
Rasmus's avatar
Rasmus committed
214 215 216 217
relative file name, %F by the absolute file name, %b by the file
base name (i.e. without directory and extension parts), %o by the
base directory of the file and %O by the absolute file name of
the output file.
Bastien Guerry's avatar
Bastien Guerry committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

By default, Org uses 3 runs of to do the processing.

Alternatively, this may be a Lisp function that does the
processing.  This function should accept the file name as
its single argument."
  :group 'org-export-pdf
  :group 'org-export-man
  :version "24.4"
  :package-version '(Org . "8.0")
  :type '(choice
          (repeat :tag "Shell command sequence"
                  (string :tag "Shell command"))
          (const :tag "2 runs of pdfgroff"
                 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
                  "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
          (const :tag "3 runs of pdfgroff"
                 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
                  "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
                  "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
          (function)))

(defcustom org-man-logfiles-extensions
  '("log" "out" "toc")
  "The list of file extensions to consider as Man logfiles."
  :group 'org-export-man
  :version "24.4"
  :package-version '(Org . "8.0")
  :type '(repeat (string :tag "Extension")))

(defcustom org-man-remove-logfiles t
  "Non-nil means remove the logfiles produced by PDF production.
These are the .aux, .log, .out, and .toc files."
  :group 'org-export-man
  :version "24.4"
  :package-version '(Org . "8.0")
  :type 'boolean)



;;; Internal Functions

(defun org-man--caption/label-string (element info)
  "Return caption and label Man string for ELEMENT.

INFO is a plist holding contextual information.  If there's no
caption nor label, return the empty string.

For non-floats, see `org-man--wrap-label'."
  (let ((label (org-element-property :label element))
	(main (org-export-get-caption element))
	(short (org-export-get-caption element t)))
    (cond ((and (not main) (not label)) "")
	  ((not main) (format "\\fI%s\\fP" label))
	  ;; Option caption format with short name.
	  (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
			 (org-export-data short info)
			 (org-export-data main info)))
	  ;; Standard caption format.
	  (t (format "\\fR%s\\fP" (org-export-data main info))))))

(defun org-man--wrap-label (element output)
  "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
This function shouldn't be used for floats.  See
`org-man--caption/label-string'."
  (let ((label (org-element-property :name element)))
    (if (or (not output) (not label) (string= output "") (string= label ""))
        output
      (concat (format "%s\n.br\n" label) output))))

Rasmus's avatar
Rasmus committed
288 289 290 291
(defun org-man--protect-text (text)
  "Protect minus and backslash characters in string TEXT."
  (replace-regexp-in-string "-" "\\-" text nil t))

Bastien Guerry's avatar
Bastien Guerry committed
292 293 294 295 296 297 298 299


;;; Template

(defun org-man-template (contents info)
  "Return complete document string after Man conversion.
CONTENTS is the transcoded contents string.  INFO is a plist
holding export options."
Rasmus's avatar
Rasmus committed
300 301
  (let* ((title (when (plist-get info :with-title)
		  (org-export-data (plist-get info :title) info)))
Bastien Guerry's avatar
Bastien Guerry committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
        (attr (read (format "(%s)"
                            (mapconcat
                             #'identity
                             (list (plist-get info :man-class-options))
                             " "))))
        (section-item (plist-get attr :section-id)))

    (concat

     (cond
      ((and title (stringp section-item))
       (format ".TH \"%s\" \"%s\" \n" title section-item))
      ((and (string= "" title) (stringp section-item))
       (format ".TH \"%s\" \"%s\" \n" " " section-item))
      (title
       (format ".TH \"%s\" \"1\" \n" title))
      (t
       ".TH \" \" \"1\" "))
     contents)))




;;; Transcode Functions

;;; Babel Call
;;
;; Babel Calls are ignored.


;;; Bold

Rasmus's avatar
Rasmus committed
334
(defun org-man-bold (_bold contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
335 336 337 338 339 340 341 342
  "Transcode BOLD from Org to Man.
CONTENTS is the text with bold markup.  INFO is a plist holding
contextual information."
  (format "\\fB%s\\fP" contents))


;;; Center Block

Rasmus's avatar
Rasmus committed
343
(defun org-man-center-block (center-block contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
344 345 346 347 348 349 350 351 352 353 354 355
  "Transcode a CENTER-BLOCK element from Org to Man.
CONTENTS holds the contents of the center block.  INFO is a plist
holding contextual information."
  (org-man--wrap-label
   center-block
   (format ".ce %d\n.nf\n%s\n.fi"
           (- (length (split-string contents "\n")) 1 )
           contents)))


;;; Code

Rasmus's avatar
Rasmus committed
356
(defun org-man-code (code _contents _info)
Rasmus's avatar
Rasmus committed
357 358 359
  "Transcode a CODE object from Org to Man."
  (format "\\fC%s\\fP"
	  (org-man--protect-text (org-element-property :value code))))
Bastien Guerry's avatar
Bastien Guerry committed
360 361 362 363


;;; Drawer

Rasmus's avatar
Rasmus committed
364
(defun org-man-drawer (_drawer contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
365 366 367 368 369 370 371 372 373
  "Transcode a DRAWER element from Org to Man.
   DRAWER holds the drawer information
   CONTENTS holds the contents of the block.
   INFO is a plist holding contextual information. "
  contents)


;;; Dynamic Block

Rasmus's avatar
Rasmus committed
374
(defun org-man-dynamic-block (dynamic-block contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
375 376 377 378 379 380 381 382
  "Transcode a DYNAMIC-BLOCK element from Org to Man.
CONTENTS holds the contents of the block.  INFO is a plist
holding contextual information.  See `org-export-data'."
  (org-man--wrap-label dynamic-block contents))


;;; Entity

Rasmus's avatar
Rasmus committed
383
(defun org-man-entity (entity _contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
384 385 386 387 388 389 390 391
  "Transcode an ENTITY object from Org to Man.
CONTENTS are the definition itself.  INFO is a plist holding
contextual information."
  (org-element-property :utf-8 entity))


;;; Example Block

Rasmus's avatar
Rasmus committed
392
(defun org-man-example-block (example-block _contents info)
Bastien Guerry's avatar
Bastien Guerry committed
393 394 395 396 397 398 399 400 401 402 403
  "Transcode an EXAMPLE-BLOCK element from Org to Man.
CONTENTS is nil.  INFO is a plist holding contextual
information."
  (org-man--wrap-label
   example-block
   (format ".RS\n.nf\n%s\n.fi\n.RE"
           (org-export-format-code-default example-block info))))


;;; Export Block

Rasmus's avatar
Rasmus committed
404
(defun org-man-export-block (export-block _contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
405 406 407 408 409 410 411 412
  "Transcode a EXPORT-BLOCK element from Org to Man.
CONTENTS is nil.  INFO is a plist holding contextual information."
  (when (string= (org-element-property :type export-block) "MAN")
    (org-remove-indentation (org-element-property :value export-block))))


;;; Export Snippet

Rasmus's avatar
Rasmus committed
413
(defun org-man-export-snippet (export-snippet _contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
414 415 416 417 418 419 420 421
  "Transcode a EXPORT-SNIPPET object from Org to Man.
CONTENTS is nil.  INFO is a plist holding contextual information."
  (when (eq (org-export-snippet-backend export-snippet) 'man)
    (org-element-property :value export-snippet)))


;;; Fixed Width

Rasmus's avatar
Rasmus committed
422
(defun org-man-fixed-width (fixed-width _contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
423 424 425 426
  "Transcode a FIXED-WIDTH element from Org to Man.
CONTENTS is nil.  INFO is a plist holding contextual information."
  (org-man--wrap-label
   fixed-width
Bastien's avatar
Bastien committed
427
   (format "\\fC\n%s\n\\fP"
Bastien Guerry's avatar
Bastien Guerry committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
           (org-remove-indentation
            (org-element-property :value fixed-width)))))


;;; Footnote Definition
;;
;; Footnote Definitions are ignored.

;;; Footnote References
;;
;; Footnote References are Ignored


;;; Headline

(defun org-man-headline (headline contents info)
  "Transcode a HEADLINE element from Org to Man.
CONTENTS holds the contents of the headline.  INFO is a plist
holding contextual information."
  (let* ((level (org-export-get-relative-level headline info))
Rasmus's avatar
Rasmus committed
448 449 450 451 452 453 454 455 456
	 ;; Section formatting will set two placeholders: one for the
	 ;; title and the other for the contents.
	 (section-fmt
	  (pcase level
	    (1 ".SH \"%s\"\n%s")
	    (2 ".SS \"%s\"\n%s")
	    (3 ".SS \"%s\"\n%s")
	    (_ nil)))
	 (text (org-export-data (org-element-property :title headline) info)))
Bastien Guerry's avatar
Bastien Guerry committed
457 458 459 460 461 462 463 464 465 466 467

    (cond
     ;; Case 1: This is a footnote section: ignore it.
     ((org-element-property :footnote-section-p headline) nil)

     ;; Case 2. This is a deep sub-tree: export it as a list item.
     ;;         Also export as items headlines for which no section
     ;;         format has been found.
     ((or (not section-fmt) (org-export-low-level-p headline info))
      ;; Build the real contents of the sub-tree.
      (let ((low-level-body
Rasmus's avatar
Rasmus committed
468 469 470 471 472 473 474 475 476 477 478 479 480 481
	     (concat
	      ;; If the headline is the first sibling, start a list.
	      (when (org-export-first-sibling-p headline info)
		(format "%s\n" ".RS"))
	      ;; Itemize headline
	      ".TP\n.ft I\n" text "\n.ft\n"
	      contents ".RE")))
	;; If headline is not the last sibling simply return
	;; LOW-LEVEL-BODY.  Otherwise, also close the list, before any
	;; blank line.
	(if (not (org-export-last-sibling-p headline info)) low-level-body
	  (replace-regexp-in-string
	   "[ \t\n]*\\'" ""
	   low-level-body))))
Bastien Guerry's avatar
Bastien Guerry committed
482 483 484 485 486 487 488 489 490 491 492 493 494

     ;; Case 3. Standard headline.  Export it as a section.
     (t (format section-fmt text contents )))))

;;; Horizontal Rule
;; Not supported

;;; Inline Babel Call
;;
;; Inline Babel Calls are ignored.

;;; Inline Src Block

Rasmus's avatar
Rasmus committed
495
(defun org-man-inline-src-block (inline-src-block _contents info)
Bastien Guerry's avatar
Bastien Guerry committed
496 497 498 499 500
  "Transcode an INLINE-SRC-BLOCK element from Org to Man.
CONTENTS holds the contents of the item.  INFO is a plist holding
contextual information."
  (let* ((code (org-element-property :value inline-src-block)))
    (cond
Rasmus's avatar
Rasmus committed
501 502
     ((plist-get info :man-source-highlight)
      (let* ((tmpdir temporary-file-directory)
Bastien Guerry's avatar
Bastien Guerry committed
503 504 505 506 507
             (in-file  (make-temp-name
                        (expand-file-name "srchilite" tmpdir)))
             (out-file (make-temp-name
                        (expand-file-name "reshilite" tmpdir)))
             (org-lang (org-element-property :language inline-src-block))
Rasmus's avatar
Rasmus committed
508 509 510
             (lst-lang
	      (cadr (assq (intern org-lang)
			  (plist-get info :man-source-highlight-langs))))
Bastien Guerry's avatar
Bastien Guerry committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

             (cmd (concat (expand-file-name "source-highlight")
                          " -s " lst-lang
                          " -f groff_man"
                          " -i " in-file
                          " -o " out-file )))

        (if lst-lang
            (let ((code-block "" ))
              (with-temp-file in-file (insert code))
              (shell-command cmd)
              (setq code-block  (org-file-contents out-file))
              (delete-file in-file)
              (delete-file out-file)
              code-block)
          (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
                  code))))

     ;; Do not use a special package: transcode it verbatim.
     (t
      (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
              "\\fP\n.fi\n.RE\n")))))


;;; Inlinetask
;;; Italic

Rasmus's avatar
Rasmus committed
538
(defun org-man-italic (_italic contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
  "Transcode ITALIC from Org to Man.
CONTENTS is the text with italic markup.  INFO is a plist holding
contextual information."
  (format "\\fI%s\\fP" contents))


;;; Item


(defun org-man-item (item contents info)
  "Transcode an ITEM element from Org to Man.
CONTENTS holds the contents of the item.  INFO is a plist holding
contextual information."
  (let* ((bullet (org-element-property :bullet item))
         (type (org-element-property :type (org-element-property :parent item)))
Rasmus's avatar
Rasmus committed
554 555 556 557
         (checkbox (pcase (org-element-property :checkbox item)
                     (`on "\\o'\\(sq\\(mu'")
                     (`off "\\(sq ")
                     (`trans "\\o'\\(sq\\(mi'")))
Bastien Guerry's avatar
Bastien Guerry committed
558 559 560 561 562 563 564

         (tag (let ((tag (org-element-property :tag item)))
                ;; Check-boxes must belong to the tag.
                (and tag (format "\\fB%s\\fP"
                                 (concat checkbox
                                         (org-export-data tag info)))))))

Rasmus's avatar
Rasmus committed
565 566 567 568 569 570 571 572 573
    (if (and (null tag) (null checkbox))
	(let* ((bullet (org-trim bullet))
	       (marker (cond  ((string= "-" bullet) "\\(em")
			      ((string= "*" bullet) "\\(bu")
			      ((eq type 'ordered)
			       (format "%s " (org-trim bullet)))
			      (t "\\(dg"))))
	  (concat ".IP " marker " 4\n"
		  (org-trim (or contents " " ))))
Bastien Guerry's avatar
Bastien Guerry committed
574 575 576 577 578 579
      (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
              (org-trim (or contents " " ))))))

;;; Keyword


Rasmus's avatar
Rasmus committed
580
(defun org-man-keyword (keyword _contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
581 582 583 584 585 586 587 588 589 590 591 592
  "Transcode a KEYWORD element from Org to Man.
CONTENTS is nil.  INFO is a plist holding contextual information."
  (let ((key (org-element-property :key keyword))
        (value (org-element-property :value keyword)))
    (cond
     ((string= key "MAN") value)
     ((string= key "INDEX") nil)
     ((string= key "TOC"   ) nil))))


;;; Line Break

Rasmus's avatar
Rasmus committed
593
(defun org-man-line-break (_line-break _contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
594 595
  "Transcode a LINE-BREAK object from Org to Man.
CONTENTS is nil.  INFO is a plist holding contextual information."
Rasmus's avatar
Rasmus committed
596
  "\n.br\n")
Bastien Guerry's avatar
Bastien Guerry committed
597 598 599 600 601


;;; Link


Rasmus's avatar
Rasmus committed
602
(defun org-man-link (link desc _info)
Bastien Guerry's avatar
Bastien Guerry committed
603 604 605 606 607 608 609 610 611 612 613 614
  "Transcode a LINK object from Org to Man.

DESC is the description part of the link, or the empty string.
INFO is a plist holding contextual information.  See
`org-export-data'."
  (let* ((type (org-element-property :type link))
         (raw-path (org-element-property :path link))
         ;; Ensure DESC really exists, or set it to nil.
         (desc (and (not (string= desc "")) desc))
         (path (cond
                ((member type '("http" "https" "ftp" "mailto"))
                 (concat type ":" raw-path))
Rasmus's avatar
Rasmus committed
615 616
                ((string= type "file") (org-export-file-uri raw-path))
                (t raw-path))))
Bastien Guerry's avatar
Bastien Guerry committed
617
    (cond
Rasmus's avatar
Rasmus committed
618 619
     ;; Link type is handled by a special function.
     ((org-export-custom-protocol-maybe link desc 'man))
Bastien Guerry's avatar
Bastien Guerry committed
620 621 622 623 624 625 626
     ;; External link with a description part.
     ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
     ;; External link without a description part.
     (path (format "\\fI%s\\fP" path))
     ;; No path, only description.  Try to do something useful.
     (t (format "\\fI%s\\fP" desc)))))

Rasmus's avatar
Rasmus committed
627 628 629 630 631 632 633 634 635 636
;;;; Node Property

(defun org-man-node-property (node-property _contents _info)
  "Transcode a NODE-PROPERTY element from Org to Man.
CONTENTS is nil.  INFO is a plist holding contextual
information."
  (format "%s:%s"
          (org-element-property :key node-property)
          (let ((value (org-element-property :value node-property)))
            (if value (concat " " value) ""))))
Bastien Guerry's avatar
Bastien Guerry committed
637 638 639

;;; Paragraph

Rasmus's avatar
Rasmus committed
640
(defun org-man-paragraph (paragraph contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
  "Transcode a PARAGRAPH element from Org to Man.
CONTENTS is the contents of the paragraph, as a string.  INFO is
the plist used as a communication channel."
  (let ((parent (plist-get (nth 1 paragraph) :parent)))
    (when parent
      (let ((parent-type (car parent))
            (fixed-paragraph ""))
        (cond ((and (eq parent-type 'item)
                    (plist-get (nth 1 parent) :bullet ))
               (setq fixed-paragraph (concat "" contents)))
              ((eq parent-type 'section)
               (setq fixed-paragraph (concat ".PP\n" contents)))
              ((eq parent-type 'footnote-definition)
               (setq fixed-paragraph contents))
              (t (setq fixed-paragraph (concat "" contents))))
        fixed-paragraph ))))


;;; Plain List

Rasmus's avatar
Rasmus committed
661
(defun org-man-plain-list (_plain-list contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
  "Transcode a PLAIN-LIST element from Org to Man.
CONTENTS is the contents of the list.  INFO is a plist holding
contextual information."
  contents)

;;; Plain Text

(defun org-man-plain-text (text info)
  "Transcode a TEXT string from Org to Man.
TEXT is the string to transcode.  INFO is a plist holding
contextual information."
  (let ((output text))
    ;; Protect various chars.
    (setq output (replace-regexp-in-string
		  "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
		  "$\\" output nil t 1))
    ;; Activate smart quotes.  Be sure to provide original TEXT string
    ;; since OUTPUT may have been modified.
    (when (plist-get info :with-smart-quotes)
      (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
    ;; Handle break preservation if required.
    (when (plist-get info :preserve-breaks)
      (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
					     output)))
    ;; Return value.
    output))



;;; Planning


;;; Property Drawer

Rasmus's avatar
Rasmus committed
696 697 698 699 700 701
(defun org-man-property-drawer (_property-drawer contents _info)
  "Transcode a PROPERTY-DRAWER element from Org to Man.
CONTENTS holds the contents of the drawer.  INFO is a plist
holding contextual information."
  (and (org-string-nw-p contents)
       (format ".RS\n.nf\n%s\n.fi\n.RE" contents)))
Bastien Guerry's avatar
Bastien Guerry committed
702 703 704

;;; Quote Block

Rasmus's avatar
Rasmus committed
705
(defun org-man-quote-block (quote-block contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
706 707 708 709 710 711 712 713 714 715
  "Transcode a QUOTE-BLOCK element from Org to Man.
CONTENTS holds the contents of the block.  INFO is a plist
holding contextual information."
  (org-man--wrap-label
   quote-block
   (format ".RS\n%s\n.RE" contents)))


;;; Radio Target

Rasmus's avatar
Rasmus committed
716
(defun org-man-radio-target (_radio-target text _info)
Bastien Guerry's avatar
Bastien Guerry committed
717 718 719
  "Transcode a RADIO-TARGET object from Org to Man.
TEXT is the text of the target.  INFO is a plist holding
contextual information."
Rasmus's avatar
Rasmus committed
720
  text)
Bastien Guerry's avatar
Bastien Guerry committed
721 722 723 724


;;; Section

Rasmus's avatar
Rasmus committed
725
(defun org-man-section (_section contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
726 727 728 729 730 731 732 733
  "Transcode a SECTION element from Org to Man.
CONTENTS holds the contents of the section.  INFO is a plist
holding contextual information."
  contents)


;;; Special Block

Rasmus's avatar
Rasmus committed
734
(defun org-man-special-block (special-block contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
735 736 737
  "Transcode a SPECIAL-BLOCK element from Org to Man.
CONTENTS holds the contents of the block.  INFO is a plist
holding contextual information."
Rasmus's avatar
Rasmus committed
738
  (org-man--wrap-label special-block (format "%s\n" contents)))
Bastien Guerry's avatar
Bastien Guerry committed
739 740 741 742


;;; Src Block

Rasmus's avatar
Rasmus committed
743
(defun org-man-src-block (src-block _contents info)
Bastien Guerry's avatar
Bastien Guerry committed
744 745 746
  "Transcode a SRC-BLOCK element from Org to Man.
CONTENTS holds the contents of the item.  INFO is a plist holding
contextual information."
Rasmus's avatar
Rasmus committed
747
  (if (not (plist-get info :man-source-highlight))
Bastien Guerry's avatar
Bastien Guerry committed
748
      (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
Rasmus's avatar
Rasmus committed
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
	      (org-export-format-code-default src-block info))
    (let* ((tmpdir temporary-file-directory)
	   (in-file  (make-temp-name (expand-file-name "srchilite" tmpdir)))
	   (out-file (make-temp-name (expand-file-name "reshilite" tmpdir)))
	   (code (org-element-property :value src-block))
	   (org-lang (org-element-property :language src-block))
	   (lst-lang
	    (cadr (assq (intern org-lang)
			(plist-get info :man-source-highlight-langs))))
	   (cmd (concat "source-highlight"
			" -s " lst-lang
			" -f groff_man "
			" -i " in-file
			" -o " out-file)))
      (if lst-lang
	  (let ((code-block ""))
	    (with-temp-file in-file (insert code))
	    (shell-command cmd)
	    (setq code-block  (org-file-contents out-file))
	    (delete-file in-file)
	    (delete-file out-file)
	    code-block)
	(format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code)))))
Bastien Guerry's avatar
Bastien Guerry committed
772 773 774 775


;;; Statistics Cookie

Rasmus's avatar
Rasmus committed
776
(defun org-man-statistics-cookie (statistics-cookie _contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
777 778 779 780 781 782 783
  "Transcode a STATISTICS-COOKIE object from Org to Man.
CONTENTS is nil.  INFO is a plist holding contextual information."
  (org-element-property :value statistics-cookie))


;;; Strike-Through

Rasmus's avatar
Rasmus committed
784
(defun org-man-strike-through (_strike-through contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
785 786 787 788 789 790 791
  "Transcode STRIKE-THROUGH from Org to Man.
CONTENTS is the text with strike-through markup.  INFO is a plist
holding contextual information."
  (format "\\fI%s\\fP" contents))

;;; Subscript

Rasmus's avatar
Rasmus committed
792
(defun org-man-subscript (_subscript contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
793 794 795 796 797 798 799
  "Transcode a SUBSCRIPT object from Org to Man.
CONTENTS is the contents of the object.  INFO is a plist holding
contextual information."
  (format  "\\d\\s-2%s\\s+2\\u" contents))

;;; Superscript "^_%s$

Rasmus's avatar
Rasmus committed
800
(defun org-man-superscript (_superscript contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
  "Transcode a SUPERSCRIPT object from Org to Man.
CONTENTS is the contents of the object.  INFO is a plist holding
contextual information."
  (format  "\\u\\s-2%s\\s+2\\d" contents))


;;; Table
;;
;; `org-man-table' is the entry point for table transcoding.  It
;; takes care of tables with a "verbatim" attribute.  Otherwise, it
;; delegates the job to either `org-man-table--table.el-table' or
;; `org-man-table--org-table' functions, depending of the type of
;; the table.
;;
;; `org-man-table--align-string' is a subroutine used to build
;; alignment string for Org tables.

(defun org-man-table (table contents info)
  "Transcode a TABLE element from Org to Man.
CONTENTS is the contents of the table.  INFO is a plist holding
contextual information."
  (cond
   ;; Case 1: verbatim table.
Rasmus's avatar
Rasmus committed
824
   ((or (plist-get info :man-tables-verbatim)
Bastien Guerry's avatar
Bastien Guerry committed
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
        (let ((attr (read (format "(%s)"
                 (mapconcat
                  #'identity
                  (org-element-property :attr_man table)
                  " ")))))

          (and attr (plist-get attr :verbatim))))

    (format ".nf\n\\fC%s\\fP\n.fi"
            ;; Re-create table, without affiliated keywords.
            (org-trim
             (org-element-interpret-data
              `(table nil ,@(org-element-contents table))))))
   ;; Case 2: Standard table.
   (t (org-man-table--org-table table contents info))))

(defun org-man-table--align-string (divider table info)
  "Return an appropriate Man alignment string.
TABLE is the considered table.  INFO is a plist used as
a communication channel."
  (let (alignment)
    ;; Extract column groups and alignment from first (non-rule) row.
    (org-element-map
	(org-element-map table 'table-row
	  (lambda (row)
	    (and (eq (org-element-property :type row) 'standard) row))
	  info 'first-match)
	'table-cell
      (lambda (cell)
	(let* ((borders (org-export-table-cell-borders cell info))
	       (raw-width (org-export-table-cell-width cell info))
	       (width-cm (when raw-width (/ raw-width 5)))
	       (width (if raw-width (format "w(%dc)"
					    (if (< width-cm 1) 1 width-cm)) "")))
	  ;; Check left border for the first cell only.
	  (when (and (memq 'left borders) (not alignment))
	    (push "|" alignment))
	  (push
Rasmus's avatar
Rasmus committed
863 864 865 866
	   (concat (pcase (org-export-table-cell-alignment cell info)
		     (`left "l") (`right "r") (`center "c"))
		   width
		   divider)
Bastien Guerry's avatar
Bastien Guerry committed
867 868 869
	   alignment)
	  (when (memq 'right borders) (push "|" alignment))))
      info)
Rasmus's avatar
Rasmus committed
870
    (apply #'concat (reverse alignment))))
Bastien Guerry's avatar
Bastien Guerry committed
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897

(defun org-man-table--org-table (table contents info)
  "Return appropriate Man code for an Org table.

TABLE is the table type element to transcode.  CONTENTS is its
contents, as a string.  INFO is a plist used as a communication
channel.

This function assumes TABLE has `org' as its `:type' attribute."
  (let* ((attr (org-export-read-attribute :attr_man table))
         (caption (and (not (plist-get attr :disable-caption))
		       (org-man--caption/label-string table info)))
         (divider (if (plist-get attr :divider) "|" " "))

         ;; Determine alignment string.
         (alignment (org-man-table--align-string divider table info))
         ;; Extract others display options.

         (lines (org-split-string contents "\n"))

         (attr-list
	  (delq nil
		(list
		 (and (plist-get attr :expand) "expand")
		 (let ((placement (plist-get attr :placement)))
		   (cond ((string= placement 'center) "center")
			 ((string= placement 'left) nil)
Rasmus's avatar
Rasmus committed
898 899
			 ((plist-get info :man-tables-centered) "center")
			 (t "")))
Bastien Guerry's avatar
Bastien Guerry committed
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
		 (or (plist-get attr :boxtype) "box"))))

         (title-line  (plist-get attr :title-line))
         (long-cells (plist-get attr :long-cells))

         (table-format (concat
                        (format "%s" (or (car attr-list) "" ))
                        (or
                         (let ((output-list '()))
                           (when (cdr attr-list)
                             (dolist (attr-item (cdr attr-list))
			       (setq output-list (concat output-list  (format ",%s" attr-item)))))
                           output-list)
                         "")))

	 (first-line (when lines (org-split-string (car lines) "\t"))))
    ;; Prepare the final format string for the table.


    (cond
     ;; Others.
     (lines (concat ".TS\n " table-format ";\n"

                    (format "%s.\n"
                            (let ((final-line ""))
                              (when title-line
Rasmus's avatar
Rasmus committed
926
                                (dotimes (_ (length first-line))
Bastien Guerry's avatar
Bastien Guerry committed
927 928 929 930 931 932
                                  (setq final-line (concat final-line "cb" divider))))

                              (setq final-line (concat final-line "\n"))

                              (if alignment
                                  (setq final-line (concat final-line alignment))
Rasmus's avatar
Rasmus committed
933
                                (dotimes (_ (length first-line))
Bastien Guerry's avatar
Bastien Guerry committed
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
                                  (setq final-line (concat final-line "c" divider))))
                              final-line ))

                    (format "%s.TE\n"
                            (let ((final-line "")
                                  (long-line "")
                                  (lines (org-split-string contents "\n")))

                              (dolist (line-item lines)
                                (setq long-line "")

                                (if long-cells
                                    (progn
                                      (if (string= line-item "_")
                                          (setq long-line (format "%s\n" line-item))
                                        ;; else string =
                                        (let ((cell-item-list (org-split-string line-item "\t")))
                                          (dolist (cell-item cell-item-list)

                                            (cond  ((eq cell-item (car (last cell-item-list)))
                                                    (setq long-line (concat long-line
                                                                            (format "T{\n%s\nT}\t\n"  cell-item ))))
                                                   (t
                                                    (setq long-line (concat long-line
                                                                            (format "T{\n%s\nT}\t"  cell-item ))))))
					  long-line))
				      ;; else long cells
				      (setq final-line (concat final-line long-line )))

                                  (setq final-line (concat final-line line-item "\n"))))
                              final-line))

                    (and caption (format ".TB \"%s\"" caption)))))))

;;; Table Cell

(defun org-man-table-cell (table-cell contents info)
  "Transcode a TABLE-CELL element from Org to Man
CONTENTS is the cell contents.  INFO is a plist used as
a communication channel."
Rasmus's avatar
Rasmus committed
974 975 976 977 978 979 980 981 982 983 984
  (concat
   (let ((scientific-format (plist-get info :man-table-scientific-notation)))
     (if (and contents
	      scientific-format
	      (string-match orgtbl-exp-regexp contents))
	 ;; Use appropriate format string for scientific notation.
	 (format scientific-format
		 (match-string 1 contents)
		 (match-string 2 contents))
       contents))
   (when (org-export-get-next-element table-cell info) "\t")))
Bastien Guerry's avatar
Bastien Guerry committed
985 986 987 988 989


;;; Table Row

(defun org-man-table-row (table-row contents info)
Rasmus's avatar
Rasmus committed
990
  "Transcode a TABLE-ROW element from Org to Man.
Bastien Guerry's avatar
Bastien Guerry committed
991 992
CONTENTS is the contents of the row.  INFO is a plist used as
a communication channel."
Rasmus's avatar
Rasmus committed
993 994
  ;; Rules are ignored since table separators are deduced from borders
  ;; of the current row.
Bastien Guerry's avatar
Bastien Guerry committed
995
  (when (eq (org-element-property :type table-row) 'standard)
Rasmus's avatar
Rasmus committed
996 997 998 999
    (let ((borders
	   ;; TABLE-ROW's borders are extracted from its first cell.
	   (org-export-table-cell-borders
	    (car (org-element-contents table-row)) info)))
Bastien Guerry's avatar
Bastien Guerry committed
1000
      (concat
Rasmus's avatar
Rasmus committed
1001
       (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
Bastien Guerry's avatar
Bastien Guerry committed
1002
       contents
Rasmus's avatar
Rasmus committed
1003 1004
       (cond ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
	     ((memq 'below borders) "\n_"))))))
Bastien Guerry's avatar
Bastien Guerry committed
1005 1006 1007 1008


;;; Target

Rasmus's avatar
Rasmus committed
1009
(defun org-man-target (target _contents info)
Bastien Guerry's avatar
Bastien Guerry committed
1010 1011 1012
  "Transcode a TARGET object from Org to Man.
CONTENTS is nil.  INFO is a plist holding contextual
information."
Rasmus's avatar
Rasmus committed
1013
  (format "\\fI%s\\fP" (org-export-get-reference target info)))
Bastien Guerry's avatar
Bastien Guerry committed
1014 1015 1016 1017


;;; Timestamp

Rasmus's avatar
Rasmus committed
1018
(defun org-man-timestamp (_timestamp _contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
1019
  "Transcode a TIMESTAMP object from Org to Man.
Paul Eggert's avatar
Paul Eggert committed
1020
CONTENTS is nil.  INFO is a plist holding contextual information."
Rasmus's avatar
Rasmus committed
1021
  "")
Bastien Guerry's avatar
Bastien Guerry committed
1022 1023 1024 1025


;;; Underline

Rasmus's avatar
Rasmus committed
1026
(defun org-man-underline (_underline contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
1027 1028 1029 1030 1031 1032 1033 1034
  "Transcode UNDERLINE from Org to Man.
CONTENTS is the text with underline markup.  INFO is a plist
holding contextual information."
  (format "\\fI%s\\fP" contents))


;;; Verbatim

Rasmus's avatar
Rasmus committed
1035 1036 1037 1038
(defun org-man-verbatim (verbatim _contents _info)
  "Transcode a VERBATIM object from Org to Man."
  (format "\\fI%s\\fP"
	  (org-man--protect-text (org-element-property :value verbatim))))
Bastien Guerry's avatar
Bastien Guerry committed
1039 1040 1041 1042


;;; Verse Block

Rasmus's avatar
Rasmus committed
1043
(defun org-man-verse-block (_verse-block contents _info)
Bastien Guerry's avatar
Bastien Guerry committed
1044
  "Transcode a VERSE-BLOCK element from Org to Man.
1045
CONTENTS is verse block contents.  INFO is a plist holding
Bastien Guerry's avatar
Bastien Guerry committed
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
contextual information."
  (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))



;;; Interactive functions

(defun org-man-export-to-man
  (&optional async subtreep visible-only body-only ext-plist)
  "Export current buffer to a Man file.

If narrowing is active in the current buffer, only export its
narrowed part.

If a region is active, export that region.

A non-nil optional argument ASYNC means the process should happen
asynchronously.  The resulting file should be accessible through
the `org-export-stack' interface.

When optional argument SUBTREEP is non-nil, export the sub-tree
at point, extracting information from the headline properties
first.

When optional argument VISIBLE-ONLY is non-nil, don't export
contents of hidden elements.

When optional argument BODY-ONLY is non-nil, only the body
without any markers.

EXT-PLIST, when provided, is a property list with external
parameters overriding Org default settings, but still inferior to
file-local settings.

Return output file's name."
  (interactive)
  (let ((outfile (org-export-output-file-name ".man" subtreep)))
    (org-export-to-file 'man outfile
      async subtreep visible-only body-only ext-plist)))

(defun org-man-export-to-pdf
  (&optional async subtreep visible-only body-only ext-plist)
  "Export current buffer to Groff then process through to PDF.

If narrowing is active in the current buffer, only export its
narrowed part.

If a region is active, export that region.

A non-nil optional argument ASYNC means the process should happen
asynchronously.  The resulting file should be accessible through
the `org-export-stack' interface.

When optional argument SUBTREEP is non-nil, export the sub-tree
at point, extracting information from the headline properties
first.

When optional argument VISIBLE-ONLY is non-nil, don't export
contents of hidden elements.

When optional argument BODY-ONLY is non-nil, only write between
markers.

EXT-PLIST, when provided, is a property list with external
parameters overriding Org default settings, but still inferior to
file-local settings.

Return PDF file's name."
  (interactive)
  (let ((outfile (org-export-output-file-name ".man" subtreep)))
    (org-export-to-file 'man outfile
      async subtreep visible-only body-only ext-plist
      (lambda (file) (org-latex-compile file)))))

(defun org-man-compile (file)
  "Compile a Groff file.

FILE is the name of the file being compiled.  Processing is done
through the command specified in `org-man-pdf-process'.

Return PDF file name or an error if it couldn't be produced."
Rasmus's avatar
Rasmus committed
1127 1128 1129 1130 1131 1132 1133 1134 1135
  (message "Processing Groff file %s..." file)
  (let ((output (org-compile-file file org-man-pdf-process "pdf")))
    (when org-man-remove-logfiles
      (let ((base (file-name-sans-extension output)))
	(dolist (ext org-man-logfiles-extensions)
	  (let ((file (concat base "." ext)))
	    (when (file-exists-p file) (delete-file file))))))
    (message "Process completed.")
    output))
Bastien Guerry's avatar
Bastien Guerry committed
1136 1137 1138 1139 1140 1141 1142 1143

(provide 'ox-man)

;; Local variables:
;; generated-autoload-file: "org-loaddefs.el"
;; End:

;;; ox-man.el ends here