Merge branch 'maint'
[org-mode.git] / contrib / lisp / org-e-groff.el
blob5fb0b0b881f2990db93dfc0a0004e9d3216e9e50
1 ;; org-e-groff.el --- Groff Back-End For Org Export Engine
3 ;; Copyright (C) 2011-2012 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/>.
22 ;;; Commentary:
24 ;; This library implements a Groff Memorandum Macro back-end for
25 ;; Org generic exporter.
27 ;; To test it, run
29 ;; M-: (org-export-to-buffer 'e-groff "*Test e-Groff*") RET
31 ;; in an org-mode buffer then switch to the buffer to see the Groff
32 ;; export. See contrib/lisp/org-export.el for more details on how
33 ;; this exporter works.
35 ;; It introduces two new buffer keywords: "GROFF_CLASS" and
36 ;; "GROFF_CLASS_OPTIONS".
38 ;;; Code:
40 (eval-when-compile (require 'cl))
41 (require 'org-export)
45 ;;; Define Back-End
47 (org-export-define-backend e-groff
48 ((bold . org-e-groff-bold)
49 (center-block . org-e-groff-center-block)
50 (clock . org-e-groff-clock)
51 (code . org-e-groff-code)
52 (drawer . org-e-groff-drawer)
53 (dynamic-block . org-e-groff-dynamic-block)
54 (entity . org-e-groff-entity)
55 (example-block . org-e-groff-example-block)
56 (export-block . org-e-groff-export-block)
57 (export-snippet . org-e-groff-export-snippet)
58 (fixed-width . org-e-groff-fixed-width)
59 (footnote-definition . org-e-groff-footnote-definition)
60 (footnote-reference . org-e-groff-footnote-reference)
61 (headline . org-e-groff-headline)
62 (horizontal-rule . org-e-groff-horizontal-rule)
63 (inline-src-block . org-e-groff-inline-src-block)
64 (inlinetask . org-e-groff-inlinetask)
65 (italic . org-e-groff-italic)
66 (item . org-e-groff-item)
67 (keyword . org-e-groff-keyword)
68 ;; (latex-environment . org-e-groff-latex-environment)
69 ;; (latex-fragment . org-e-groff-latex-fragment)
70 (line-break . org-e-groff-line-break)
71 (link . org-e-groff-link)
72 (paragraph . org-e-groff-paragraph)
73 (plain-list . org-e-groff-plain-list)
74 (plain-text . org-e-groff-plain-text)
75 (planning . org-e-groff-planning)
76 (property-drawer . org-e-groff-property-drawer)
77 (quote-block . org-e-groff-quote-block)
78 (quote-section . org-e-groff-quote-section)
79 (radio-target . org-e-groff-radio-target)
80 (section . org-e-groff-section)
81 (special-block . org-e-groff-special-block)
82 (src-block . org-e-groff-src-block)
83 (statistics-cookie . org-e-groff-statistics-cookie)
84 (strike-through . org-e-groff-strike-through)
85 (subscript . org-e-groff-subscript)
86 (superscript . org-e-groff-superscript)
87 (table . org-e-groff-table)
88 (table-cell . org-e-groff-table-cell)
89 (table-row . org-e-groff-table-row)
90 (target . org-e-groff-target)
91 (template . org-e-groff-template)
92 (timestamp . org-e-groff-timestamp)
93 (underline . org-e-groff-underline)
94 (verbatim . org-e-groff-verbatim)
95 (verse-block . org-e-groff-verse-block))
96 :export-block "GROFF"
97 :menu-entry
98 (?g "Export to GROFF"
99 ((?g "As GROFF file" org-e-groff-export-to-groff)
100 (?p "As PDF file" org-e-groff-export-to-pdf)
101 (?o "As PDF file and open"
102 (lambda (s v b) (org-open-file (org-e-groff-export-to-pdf s v b))))))
103 :options-alist
104 ((:date "DATE" nil org-e-groff-date-format t)
105 (:groff-class "GROFF_CLASS" nil org-e-groff-default-class t)
106 (:groff-class-options "GROFF_CLASS_OPTIONS" nil nil t)
107 (:groff-header-extra "GROFF_HEADER" nil nil newline)))
111 ;;; User Configurable Variables
113 (defgroup org-export-e-groff nil
114 "Options for exporting Org mode files to Groff."
115 :tag "Org Export Groff"
116 :group 'org-export)
118 ;;; Preamble
120 (defcustom org-e-groff-default-class "internal"
121 "The default Groff class."
122 :group 'org-export-e-groff
123 :type '(string :tag "Groff class"))
125 (defcustom org-e-groff-classes
126 '(("file" ".MT 1"
127 (:heading 'default :type "memo" :last-section "toc"))
128 ("internal" ".MT 0"
129 (:heading 'default :type "memo" :last-section "toc"))
130 ("programmer" ".MT 2"
131 (:heading 'default :type "memo" :last-section "toc"))
132 ("engineer" ".MT 3"
133 (:heading 'default :type "memo" :last-section "toc"))
134 ("external" ".MT 4"
135 (:heading 'default :type "memo" :last-section "toc"))
136 ("letter" ".MT 5"
137 (:heading 'default :type "memo" :last-section "sign"))
138 ("custom" ".so file"
139 (:heading custom-function :type "custom" :last-section "toc"))
140 ("dummy" ""
141 (:heading 'default :type "memo"))
142 ("ms" "ms"
143 (:heading 'default :type "cover" :last-section "toc"))
144 ("se_ms" "se_ms"
145 (:heading 'default :type "cover" :last-section "toc"))
146 ("block" "BL"
147 (:heading 'default :type "letter" :last-section "sign"))
148 ("semiblock" "SB"
149 (:heading 'default :type "letter" :last-section "sign"))
150 ("fullblock" "FB"
151 (:heading 'default :type "letter" :last-section "sign"))
152 ("simplified" "SP"
153 (:heading 'default :type "letter" :last-section "sign"))
154 ("none" "" (:heading 'default :type "custom")))
156 ;; none means, no Cover or Memorandum Type and no calls to AU, AT, ND and TL
157 ;; This is to facilitate the creation of custom pages.
159 ;; dummy means, no Cover or Memorandum Type but calls to AU, AT, ND and TL
160 ;; are made. This is to facilitate Abstract Insertion.
162 "This list describes the attributes for the documents being created.
163 It allows for the creation of new "
164 :group 'org-export-e-groff
165 :type '(repeat
166 (list (string :tag "Document Type")
167 (string :tag "Header")
168 (repeat :tag "Options" :inline t
169 (choice
170 (list :tag "Heading")
171 (function :tag "Hook computing sectioning"))))))
174 (defcustom org-e-groff-date-format
175 (format-time-string "%Y-%m-%d")
176 "Format string for .ND "
177 :group 'org-export-e-groff
178 :type 'boolean)
180 ;;; Headline
182 (defconst org-e-groff-special-tags
183 '("FROM" "TO" "ABSTRACT" "APPENDIX" "BODY" "NS"))
185 (defcustom org-e-groff-format-headline-function nil
186 "Function to format headline text.
188 This function will be called with 5 arguments:
189 TODO the todo keyword (string or nil).
190 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
191 PRIORITY the priority of the headline (integer or nil)
192 TEXT the main headline text (string).
193 TAGS the tags as a list of strings (list of strings or nil).
195 The function result will be used in the section format string.
197 As an example, one could set the variable to the following, in
198 order to reproduce the default set-up:
200 \(defun org-e-groff-format-headline (todo todo-type priority text tags)
201 \"Default format function for an headline.\"
202 \(concat (when todo
203 \(format \"\\fB%s\\fP \" todo))
204 \(when priority
205 \(format \"[\\#%c] \" priority))
206 text
207 \(when tags
208 \(format \" %s \"
209 \(mapconcat 'identity tags \":\"))))"
210 :group 'org-export-e-groff
211 :type 'function)
213 ;;; Timestamps
215 (defcustom org-e-groff-active-timestamp-format "\\fI%s\\fP"
216 "A printf format string to be applied to active timestamps."
217 :group 'org-export-e-groff
218 :type 'string)
220 (defcustom org-e-groff-inactive-timestamp-format "\\fI%s\\fP"
221 "A printf format string to be applied to inactive timestamps."
222 :group 'org-export-e-groff
223 :type 'string)
225 (defcustom org-e-groff-diary-timestamp-format "\\fI%s\\fP"
226 "A printf format string to be applied to diary timestamps."
227 :group 'org-export-e-groff
228 :type 'string)
230 ;;; Links
232 (defcustom org-e-groff-inline-image-rules
233 '(("file" . "\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'")
234 ("fuzzy" . "\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'"))
235 "Rules characterizing image files that can be inlined into Groff.
237 A rule consists in an association whose key is the type of link
238 to consider, and value is a regexp that will be matched against
239 link's path.
241 Note that, by default, the image extensions actually allowed
242 depend on the way the Groff file is processed. When used with
243 pdfgroff, pdf, jpg and png images are OK. When processing
244 through dvi to Postscript, only ps and eps are allowed. The
245 default we use here encompasses both."
246 :group 'org-export-e-groff
247 :type '(alist :key-type (string :tag "Type")
248 :value-type (regexp :tag "Path")))
250 (defcustom org-e-groff-link-with-unknown-path-format "\\fI%s\\fP"
251 "Format string for links with unknown path type."
252 :group 'org-export-groff
253 :type 'string)
255 ;;; Tables
257 (defcustom org-e-groff-tables-centered t
258 "When non-nil, tables are exported in a center environment."
259 :group 'org-export-e-groff
260 :type 'boolean)
262 (defcustom org-e-groff-tables-verbatim nil
263 "When non-nil, tables are exported verbatim."
264 :group 'org-export-e-groff
265 :type 'boolean)
267 (defcustom org-e-groff-table-scientific-notation "%sE%s"
268 "Format string to display numbers in scientific notation.
269 The format should have \"%s\" twice, for mantissa and exponent
270 \(i.e. \"%s\\\\times10^{%s}\").
272 When nil, no transformation is made."
273 :group 'org-export-e-groff
274 :type '(choice
275 (string :tag "Format string")
276 (const :tag "No formatting")))
278 ;;; Text markup
280 (defcustom org-e-groff-text-markup-alist
281 '((bold . "\\fB%s\\fP")
282 (code . "\\fC%s\\fP")
283 (italic . "\\fI%s\\fP")
284 (strike-through . "\\fC%s\\fP") ; Strike through and underline
285 (underline . "\\fI%s\\fP") ; need to be revised.
286 (verbatim . "protectedtexttt"))
287 "Alist of Groff expressions to convert text markup.
289 The key must be a symbol among `bold', `code', `italic',
290 `strike-through', `underline' and `verbatim'. The value is
291 a formatting string to wrap fontified text with it.
293 If no association can be found for a given markup, text will be
294 returned as-is."
295 :group 'org-export-e-groff
296 :type 'alist
297 :options '(bold code italic strike-through underline verbatim))
299 ;;; Drawers
301 (defcustom org-e-groff-format-drawer-function nil
302 "Function called to format a drawer in Groff code.
304 The function must accept two parameters:
305 NAME the drawer name, like \"LOGBOOK\"
306 CONTENTS the contents of the drawer.
308 The function should return the string to be exported.
310 For example, the variable could be set to the following function
311 in order to mimic default behaviour:
313 \(defun org-e-groff-format-drawer-default \(name contents\)
314 \"Format a drawer element for Groff export.\"
315 contents\)"
316 :group 'org-export-e-groff
317 :type 'function)
319 ;;; Inlinetasks
321 (defcustom org-e-groff-format-inlinetask-function nil
322 "Function called to format an inlinetask in Groff code.
324 The function must accept six parameters:
325 TODO the todo keyword, as a string
326 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
327 PRIORITY the inlinetask priority, as a string
328 NAME the inlinetask name, as a string.
329 TAGS the inlinetask tags, as a list of strings.
330 CONTENTS the contents of the inlinetask, as a string.
332 The function should return the string to be exported.
334 For example, the variable could be set to the following function
335 in order to mimic default behaviour:
337 \(defun org-e-groff-format-inlinetask \(todo type priority name tags contents\)
338 \"Format an inline task element for Groff export.\"
339 \(let ((full-title
340 \(concat
341 \(when todo
342 \(format \"\\fB%s\\fP \" todo))
343 \(when priority (format \"[\\#%c] \" priority))
344 title
345 \(when tags
346 \(format \":%s:\"
347 \(mapconcat 'identity tags \":\")))))
348 \(format (concat \".DS L\\n\"
349 \"%s\\n\\n\"
350 \"%s\"
351 \".DE\")
352 full-title contents))"
353 :group 'org-export-e-groff
354 :type 'function)
356 ;; Src blocks
358 (defcustom org-e-groff-source-highlight nil
359 "Use GNU source highlight to embellish source blocks "
360 :group 'org-export-e-groff
361 :type 'boolean)
363 (defcustom org-e-groff-source-highlight-langs
364 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
365 (scheme "scheme")
366 (c "c") (cc "cpp") (csharp "csharp") (d "d")
367 (fortran "fortran") (cobol "cobol") (pascal "pascal")
368 (ada "ada") (asm "asm")
369 (perl "perl") (cperl "perl")
370 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
371 (java "java") (javascript "javascript")
372 (tex "latex")
373 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
374 (ocaml "caml") (caml "caml")
375 (sql "sql") (sqlite "sql")
376 (html "html") (css "css") (xml "xml")
377 (bat "bat") (bison "bison") (clipper "clipper")
378 (ldap "ldap") (opa "opa")
379 (php "php") (postscript "postscript") (prolog "prolog")
380 (properties "properties") (makefile "makefile")
381 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
382 "Alist mapping languages to their listing language counterpart.
383 The key is a symbol, the major mode symbol without the \"-mode\".
384 The value is the string that should be inserted as the language
385 parameter for the listings package. If the mode name and the
386 listings name are the same, the language does not need an entry
387 in this list - but it does not hurt if it is present."
388 :group 'org-export-e-groff
389 :type '(repeat
390 (list
391 (symbol :tag "Major mode ")
392 (string :tag "Listings language"))))
394 (defcustom org-e-groff-source-highlight-options nil
395 "Association list of options for the groff listings package.
397 These options are supplied as a comma-separated list to the
398 \\lstset command. Each element of the association list should be
399 a list containing two strings: the name of the option, and the
400 value. For example,
402 (setq org-e-groff-source-highlight-options
403 '((\"basicstyle\" \"\\small\")
404 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
406 will typeset the code in a small size font with underlined, bold
407 black keywords.
409 Note that the same options will be applied to blocks of all
410 languages."
411 :group 'org-export-e-groff
412 :type '(repeat
413 (list
414 (string :tag "Listings option name ")
415 (string :tag "Listings option value"))))
417 (defvar org-e-groff-custom-lang-environments nil
418 "Alist mapping languages to language-specific Groff environments.
420 It is used during export of src blocks by the listings and
421 groff packages. For example,
423 \(setq org-e-groff-custom-lang-environments
424 '\(\(python \"pythoncode\"\)\)\)
426 would have the effect that if org encounters begin_src python
427 during groff export it will use pythoncode as the source-highlight
428 language.")
430 ;;; Plain text
432 (defcustom org-e-groff-quotes
433 '(("fr"
434 ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
435 ("\\(\\S-\\)\"" . "~»")
436 ("\\(\\s-\\|(\\|^\\)'" . "'"))
437 ("en"
438 ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
439 ("\\(\\S-\\)\"" . "''")
440 ("\\(\\s-\\|(\\|^\\)'" . "`")))
441 "Alist for quotes to use when converting english double-quotes.
443 The CAR of each item in this alist is the language code.
444 The CDR of each item in this alist is a list of three CONS:
445 - the first CONS defines the opening quote;
446 - the second CONS defines the closing quote;
447 - the last CONS defines single quotes.
449 For each item in a CONS, the first string is a regexp
450 for allowed characters before/after the quote, the second
451 string defines the replacement string for this quote."
452 :group 'org-export-e-groff
453 :type '(list
454 (cons :tag "Opening quote"
455 (string :tag "Regexp for char before")
456 (string :tag "Replacement quote "))
457 (cons :tag "Closing quote"
458 (string :tag "Regexp for char after ")
459 (string :tag "Replacement quote "))
460 (cons :tag "Single quote"
461 (string :tag "Regexp for char before")
462 (string :tag "Replacement quote "))))
464 (defcustom org-e-groff-special-char
465 '(("(c)" . "\\\\(co")
466 ("(tm)" . "\\\\(tm")
467 ("(rg)" . "\\\\(rg"))
468 "CONS list in which the value of the car
469 is replace on the value of the CDR. "
470 :group 'org-export-e-groff
471 :type '(list
472 (cons :tag "Character Subtitute"
473 (string :tag "Original Character Group")
474 (string :tag "Replacement Character"))))
476 ;;; Compilation
478 (defcustom org-e-groff-pdf-process
479 '("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
480 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
481 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf")
483 "Commands to process a Groff file to a PDF file.
484 This is a list of strings, each of them will be given to the
485 shell as a command. %f in the command will be replaced by the
486 full file name, %b by the file base name \(i.e. without
487 extension) and %o by the base directory of the file."
488 :group 'org-export-pdf
489 :type '(choice
490 (repeat :tag "Shell command sequence"
491 (string :tag "Shell command"))
492 (const :tag "2 runs of pdfgroff"
493 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
494 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
495 (const :tag "3 runs of pdfgroff"
496 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
497 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
498 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
499 (function)))
501 (defcustom org-e-groff-logfiles-extensions
502 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
503 "The list of file extensions to consider as Groff logfiles."
504 :group 'org-export-e-groff
505 :type '(repeat (string :tag "Extension")))
507 (defcustom org-e-groff-remove-logfiles t
508 "Non-nil means remove the logfiles produced by PDF production.
509 These are the .aux, .log, .out, and .toc files."
510 :group 'org-export-e-groff
511 :type 'boolean)
513 (defcustom org-e-groff-organization "Org User"
514 "Name of the organization used to populate the .AF command."
515 :group 'org-export-e-groff
516 :type 'string)
518 (defcustom org-e-groff-raster-to-ps nil
519 "Command used to convert raster to EPS. Nil for no conversion. Make sure that
520 `org-e-groff-inline-image-rules' is adjusted accordingly if not conversion is being
521 done. In this case, remove the entries for jpg and png in the file and fuzzy lists."
522 :group 'org-export-e-groff
523 :type '(choice
524 (repeat :tag "Shell Command Sequence" (string :tag "Shell Command"))
525 (const :tag "sam2p" "a=%s;b=%s;sam2p ${a} ${b} ;grep -v BeginData ${b} > b_${b};mv b_${b} ${b}" )
526 (const :tag "NetPNM" "a=%s;b=%s;pngtopnm ${a} | pnmtops -noturn > ${b}" )
527 (const :tag "None" nil)))
529 (defvar org-e-groff-registered-references nil)
530 (defvar org-e-groff-special-content nil)
534 ;;; Internal Functions
536 (defun org-e-groff--caption/label-string (element info)
537 "Return caption and label Groff string for ELEMENT.
539 INFO is a plist holding contextual information. If there's no
540 caption nor label, return the empty string.
542 For non-floats, see `org-e-groff--wrap-label'."
543 (let ((main (org-export-get-caption element))
544 (short (org-export-get-caption element t))
545 (label (org-element-property :name element)))
546 (cond ((and (not main) (not label)) "")
547 ((not main) (format "\\fI%s\\fP" label))
548 ;; Option caption format with short name.
549 (short (format "%s\n.br\n - %s\n"
550 (org-export-data short info)
551 (org-export-data main info)))
552 ;; Standard caption format.
553 (t (format "\\fR%s\\fP" (org-export-data main info))))))
555 (defun org-e-groff--quotation-marks (text info)
556 "Export quotation marks depending on language conventions.
557 TEXT is a string containing quotation marks to be replaced. INFO
558 is a plist used as a communication channel."
559 (mapc (lambda(l)
560 (let ((start 0))
561 (while (setq start (string-match (car l) text start))
562 (let ((new-quote (concat (match-string 1 text) (cdr l))))
563 (setq text (replace-match new-quote t t text))))))
564 (cdr (or (assoc (plist-get info :language) org-e-groff-quotes)
565 ;; Falls back on English.
566 (assoc "en" org-e-groff-quotes))))
567 text)
569 (defun org-e-groff--wrap-label (element output)
570 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
571 This function shouldn't be used for floats. See
572 `org-e-groff--caption/label-string'."
573 (let ((label (org-element-property :name element)))
574 (if (or (not output) (not label) (string= output "") (string= label ""))
575 output
576 (concat (format "%s\n.br\n" label) output))))
578 (defun org-e-groff--text-markup (text markup)
579 "Format TEXT depending on MARKUP text markup.
580 See `org-e-groff-text-markup-alist' for details."
581 (let ((fmt (cdr (assq markup org-e-groff-text-markup-alist))))
582 (cond
583 ;; No format string: Return raw text.
584 ((not fmt) text)
585 ((string= "protectedtexttt" fmt)
586 (let ((start 0)
587 (trans '(("\\" . "\\")))
588 (rtn "")
589 char)
590 (while (string-match "[\\{}$%&_#~^]" text)
591 (setq char (match-string 0 text))
592 (if (> (match-beginning 0) 0)
593 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
594 (setq text (substring text (1+ (match-beginning 0))))
595 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
596 rtn (concat rtn char)))
597 (setq text (concat rtn text))
598 (format "\\fC%s\\fP" text)))
599 ;; Else use format string.
600 (t (format fmt text)))))
603 (defun org-e-groff--get-tagged-content (tag info)
604 (cdr (assoc tag org-e-groff-special-content)))
606 (defun org-e-groff--mt-head (title contents attr info)
607 (concat
609 ;; 1. Insert Organization
610 (let ((firm-option (plist-get attr :firm)))
611 (cond
612 ((stringp firm-option)
613 (format ".AF \"%s\" \n" firm-option))
614 (t (format ".AF \"%s\" \n" (or org-e-groff-organization "")))))
616 ;; 2. Title
617 (let ((subtitle1 (plist-get attr :subtitle1))
618 (subtitle2 (plist-get attr :subtitle2)))
620 (cond
621 ((string= "" title)
622 (format ".TL \"%s\" \"%s\" \n%s\n"
623 (or subtitle1 "")
624 (or subtitle2 "") " "))
626 ((not (or subtitle1 subtitle2))
627 (format ".TL\n%s\n"
628 (or title "")))
630 (format ".TL \"%s\" \"%s \" \n%s\n"
631 (or subtitle1 "")
632 (or subtitle2 "") title))))
634 ;; 3. Author.
635 ;; In Groff, .AU *MUST* be placed after .TL
636 ;; If From, populate with data from From else
638 (let ((author (and (plist-get info :with-author)
639 (let ((auth (plist-get info :author)))
640 (and auth (org-export-data auth info)))))
641 (email (and (plist-get info :with-email)
642 (org-export-data (plist-get info :email) info)))
643 (from-data (org-e-groff--get-tagged-content "FROM" info))
645 (to-data (org-e-groff--get-tagged-content "TO" info)))
647 (cond
648 ((and author from-data)
649 (let ((au-line
650 (mapconcat
651 (lambda (from-line)
652 (format " \"%s\" " from-line))
653 (split-string
654 (setq from-data
655 (replace-regexp-in-string "\\.P\n" "" from-data)) "\n") "")))
657 (concat
658 (format ".AU \"%s\" " author) au-line "\n")))
660 ((and author email (not (string= "" email)))
661 (format ".AU \"%s\" \"%s\"\n" author email))
663 (author (format ".AU \"%s\"\n" author))
665 (t ".AU \"\" \n")))
668 ;; 4. Author Title, if present
669 (let ((at-item (plist-get attr :author-title)))
670 (if (and at-item (stringp at-item))
671 (format ".AT \"%s\" \n" at-item)
672 ""))
674 ;; 5. Date.
675 (let ((date (org-export-data (plist-get info :date) info)))
676 (and date (format ".ND \"%s\"\n" date)))
679 ;; If Abstract, then Populate Abstract
682 (let ((abstract-data (org-e-groff--get-tagged-content "ABSTRACT" info))
683 (to-data (org-e-groff--get-tagged-content "TO" info)))
684 (cond
685 (abstract-data
686 (format ".AS\n%s\n.AE\n" abstract-data))
687 (to-data
688 (format ".AS\n%s\n.AE\n" to-data))))))
690 (defun org-e-groff--letter-head (title contents attr info)
691 (let ((author (and (plist-get info :with-author)
692 (let ((auth (plist-get info :author)))
693 (and auth (org-export-data auth info)))))
694 (email (and (plist-get info :with-email)
695 (org-export-data (plist-get info :email) info)))
696 (from-data (org-e-groff--get-tagged-content "FROM" info))
697 (at-item (plist-get attr :author-title))
698 (to-data (org-e-groff--get-tagged-content "TO" info)))
701 ;; If FROM then get data from FROM
702 (if from-data
703 (setq from-data
704 (replace-regexp-in-string "\\.P\n" "" from-data))
705 (setq from-data ""))
707 (if to-data
708 (setq to-data
709 (replace-regexp-in-string "\\.P\n" "" to-data))
710 (setq from-data ""))
712 (concat
713 (cond
714 (from-data
715 (format ".WA \"%s\" \"%s\" \n%s\n.WE\n" author (or at-item "") from-data))
716 ((and author email (not (string= "" email)))
717 (format ".WA \"%s\"\n \"%s\"\n.WE\n" author email))
718 (author (format ".WA \"%s\"\n.WE\n" author))
719 (t ".WA \"\" \n.WE\n"))
721 ;; If TO then get data from TO
723 (when to-data
724 (format ".IA \n%s\n.IE\n" to-data)))))
727 ;;; Template
729 (defun org-e-groff-template (contents info)
730 "Return complete document string after Groff conversion.
731 CONTENTS is the transcoded contents string. INFO is a plist
732 holding export options."
733 (let* ((title (org-export-data (plist-get info :title) info))
734 (attr (read
735 (format "(%s)"
736 (mapconcat
737 #'identity
738 (list (plist-get info :groff-class-options))
739 " "))))
740 (class (plist-get info :groff-class))
741 (class-options (plist-get info :groff-class-options))
742 (classes (assoc class org-e-groff-classes))
743 (classes-options (car (last classes)))
744 (heading-option (plist-get classes-options :heading))
745 (type-option (plist-get classes-options :type))
746 (last-option (plist-get classes-options :last-section))
747 (hyphenate (plist-get attr :hyphenate))
748 (justify-right (plist-get attr :justify-right))
750 (document-class-string
751 (progn
752 (org-element-normalize-string
753 (let* ((header (nth 1 (assoc class org-e-groff-classes)))
754 (document-class-item (if (stringp header) header "")))
755 document-class-item)))))
758 (concat
759 (if justify-right
760 (case justify-right
761 ('yes ".SA 1 \n")
762 ('no ".SA 0 \n")
763 (t ""))
766 (if hyphenate
767 (case hyphenate
768 ('yes ".nr Hy 1 \n")
769 ('no ".nr Hy 0 \n")
770 (t ""))
773 (cond
774 ((string= type-option "custom") "")
776 ((and (stringp document-class-string)
777 (string= type-option "cover"))
779 (concat
780 (format ".COVER %s\n" document-class-string)
781 (org-e-groff--mt-head title contents attr info)
782 ".COVEND\n"))
784 ((string= type-option "memo")
785 (concat
786 (org-e-groff--mt-head title contents attr info)
787 document-class-string))
788 ((string= type-option "letter")
789 (concat
790 (org-e-groff--letter-head title contents attr info)
791 (let ((sa-item (plist-get attr :salutation))
792 (cn-item (plist-get attr :confidential))
793 (sj-item (plist-get attr :subject))
794 (rn-item (plist-get attr :reference))
795 (at-item (plist-get attr :attention)))
797 (concat
799 (if (stringp sa-item)
800 (format ".LO SA \"%s\" \n" sa-item)
801 ".LO SA\n")
803 (when cn-item
804 (if (stringp cn-item)
805 (format ".LO CN \"%s\"\n" cn-item)
806 ".LO CN\n"))
808 (when (and at-item (stringp at-item))
809 (format ".LO AT \"%s\" \n" at-item))
810 (when (and title rn-item)
811 (format ".LO RN \"%s\"\n" title))
813 (when (and sj-item (stringp sj-item))
814 (format ".LO SJ \"%s\" \n" sj-item))
817 ".LT " document-class-string "\n"))))
819 (t ""))
821 contents
823 (cond
824 ((string= last-option "toc")
825 ".TC")
826 ((string= last-option "sign")
827 (let ((fc-item (plist-get attr :closing)))
828 (concat (if (stringp fc-item)
829 (format ".FC \"%s\" \n" fc-item)
830 ".FC\n")
831 ".SG\n")))
832 (t ""))
834 (progn
835 (mapconcat
836 (lambda (item)
837 (when (string= (car item) "NS")
838 (replace-regexp-in-string
839 "\\.P\n" "" (cdr item))))
840 (reverse org-e-groff-special-content) "\n")))))
844 ;;; Transcode Functions
846 ;;; Babel Call
848 ;; Babel Calls are ignored.
851 ;;; Bold
853 (defun org-e-groff-bold (bold contents info)
854 "Transcode BOLD from Org to Groff.
855 CONTENTS is the text with bold markup. INFO is a plist holding
856 contextual information."
857 (org-e-groff--text-markup contents 'bold))
859 ;;; Center Block
861 (defun org-e-groff-center-block (center-block contents info)
862 "Transcode a CENTER-BLOCK element from Org to Groff.
863 CONTENTS holds the contents of the center block. INFO is a plist
864 holding contextual information."
865 (org-e-groff--wrap-label
866 center-block
867 (format ".DS C \n%s\n.DE" contents)))
869 ;;; Clock
871 (defun org-e-groff-clock (clock contents info)
872 "Transcode a CLOCK element from Org to Groff.
873 CONTENTS is nil. INFO is a plist holding contextual
874 information."
875 (concat
876 (format "\\fB%s\\fP " org-clock-string)
877 (format org-e-groff-inactive-timestamp-format
878 (concat (org-translate-time (org-element-property :value clock))
879 (let ((time (org-element-property :time clock)))
880 (and time (format " (%s)" time)))))))
882 ;;; Code
884 (defun org-e-groff-code (code contents info)
885 "Transcode a CODE object from Org to Groff.
886 CONTENTS is nil. INFO is a plist used as a communication
887 channel."
888 (org-e-groff--text-markup (org-element-property :value code) 'code))
890 ;;; Comments and Comment Blocks are ignored.
892 ;;; Drawer
894 (defun org-e-groff-drawer (drawer contents info)
895 "Transcode a DRAWER element from Org to Groff.
896 CONTENTS holds the contents of the block. INFO is a plist
897 holding contextual information."
898 (let* ((name (org-element-property :drawer-name drawer))
899 (output (if (functionp org-e-groff-format-drawer-function)
900 (funcall org-e-groff-format-drawer-function
901 name contents)
902 ;; If there's no user defined function: simply
903 ;; display contents of the drawer.
904 contents)))
905 (org-e-groff--wrap-label drawer output)))
907 ;;; Dynamic Block
909 (defun org-e-groff-dynamic-block (dynamic-block contents info)
910 "Transcode a DYNAMIC-BLOCK element from Org to Groff.
911 CONTENTS holds the contents of the block. INFO is a plist
912 holding contextual information. See `org-export-data'."
913 (org-e-groff--wrap-label dynamic-block contents))
915 ;;; Entity
917 (defun org-e-groff-entity (entity contents info)
918 "Transcode an ENTITY object from Org to Groff.
919 CONTENTS are the definition itself. INFO is a plist holding
920 contextual information."
921 (let ((ent (org-element-property :utf8 entity))) ent))
923 ;;; Example Block
925 (defun org-e-groff-example-block (example-block contents info)
926 "Transcode an EXAMPLE-BLOCK element from Org to Groff.
927 CONTENTS is nil. INFO is a plist holding contextual
928 information."
929 (org-e-groff--wrap-label
930 example-block
931 (format ".DS L\n%s\n.DE"
932 (org-export-format-code-default example-block info))))
934 ;;; Export Block
936 (defun org-e-groff-export-block (export-block contents info)
937 "Transcode a EXPORT-BLOCK element from Org to Groff.
938 CONTENTS is nil. INFO is a plist holding contextual information."
939 (when (string= (org-element-property :type export-block) "GROFF")
940 (org-remove-indentation (org-element-property :value export-block))))
942 ;;; Export Snippet
944 (defun org-e-groff-export-snippet (export-snippet contents info)
945 "Transcode a EXPORT-SNIPPET object from Org to Groff.
946 CONTENTS is nil. INFO is a plist holding contextual information."
947 (when (eq (org-export-snippet-backend export-snippet) 'e-groff)
948 (org-element-property :value export-snippet)))
950 ;;; Fixed Width
952 (defun org-e-groff-fixed-width (fixed-width contents info)
953 "Transcode a FIXED-WIDTH element from Org to Groff.
954 CONTENTS is nil. INFO is a plist holding contextual information."
955 (org-e-groff--wrap-label
956 fixed-width
957 (format "\\fC\n%s\\fP"
958 (org-remove-indentation
959 (org-element-property :value fixed-width)))))
961 ;;; Footnote Definition
963 ;; Footnote Definitions are ignored.
965 ;; Footnotes are handled automatically in GROFF. Although manual
966 ;; references can be added, not really required.
968 (defun org-e-groff-footnote-reference (footnote-reference contents info)
969 ;; Changing from info to footnote-reference
970 (let* ((raw (org-export-get-footnote-definition footnote-reference info))
971 (n (org-export-get-footnote-number footnote-reference info))
972 (data (org-trim (org-export-data raw info)))
973 (ref-id (plist-get (nth 1 footnote-reference) :label)))
974 ;; It is a reference
975 (if (string-match "fn:rl" ref-id)
976 (if (member ref-id org-e-groff-registered-references)
977 (format "\\*[%s]" ref-id)
978 (progn
979 (push ref-id org-e-groff-registered-references)
980 (format "\\*(Rf\n.RS \"%s\" \n%s\n.RF\n" ref-id data)))
981 ;; else it is a footnote
982 (format "\\u\\s-2%s\\d\\s+2\n.FS %s\n%s\n.FE\n" n n data))))
984 ;;; Headline
986 (defun org-e-groff-headline (headline contents info)
987 "Transcode an HEADLINE element from Org to Groff.
988 CONTENTS holds the contents of the headline. INFO is a plist
989 holding contextual information."
990 (let* ((class (plist-get info :groff-class))
991 (level (org-export-get-relative-level headline info))
992 (numberedp (org-export-numbered-headline-p headline info))
993 ;; Section formatting will set two placeholders: one for the
994 ;; title and the other for the contents.
995 (classes (assoc class org-e-groff-classes))
996 (classes-options (car (last classes)))
997 (heading-option (plist-get classes-options :heading))
998 (section-fmt
999 (progn
1000 (cond
1001 ((and (symbolp heading-option)
1002 (fboundp heading-option))
1003 (funcall heading-option level numberedp))
1004 ((> level 7) nil)
1005 (t (if numberedp
1006 (concat ".H " (number-to-string level) " \"%s\"\n%s")
1007 ".HU \"%s\"\n%s")))))
1008 ;; End of section-fmt
1009 (text (org-export-data (org-element-property :title headline) info))
1010 (todo
1011 (and (plist-get info :with-todo-keywords)
1012 (let ((todo (org-element-property :todo-keyword headline)))
1013 (and todo (org-export-data todo info)))))
1014 (todo-type (and todo (org-element-property :todo-type headline)))
1015 (tags (and (plist-get info :with-tags)
1016 (org-export-get-tags headline info)))
1017 (priority (and (plist-get info :with-priority)
1018 (org-element-property :priority headline)))
1019 ;; Create the headline text along with a no-tag version. The
1020 ;; latter is required to remove tags from table of contents.
1021 (full-text (if (functionp org-e-groff-format-headline-function)
1022 ;; User-defined formatting function.
1023 (funcall org-e-groff-format-headline-function
1024 todo todo-type priority text tags)
1025 ;; Default formatting.
1026 (concat
1027 (when todo
1028 (format "\\fB%s\\fP " todo))
1029 (when priority (format " [\\#%c] " priority))
1030 text
1031 (when tags
1032 (format " \\fC:%s:\\fP "
1033 (mapconcat 'identity tags ":"))))))
1034 (full-text-no-tag
1035 (if (functionp org-e-groff-format-headline-function)
1036 ;; User-defined formatting function.
1037 (funcall org-e-groff-format-headline-function
1038 todo todo-type priority text nil)
1039 ;; Default formatting.
1040 (concat
1041 (when todo (format "\\fB%s\\fP " todo))
1042 (when priority (format " [\\#%c] " priority))
1043 text)))
1044 ;; Associate some \label to the headline for internal links.
1045 ;; (headline-label
1046 ;; (format "\\label{sec-%s}\n"
1047 ;; (mapconcat 'number-to-string
1048 ;; (org-export-get-headline-number headline info)
1049 ;; "-")))
1050 (headline-label "")
1051 (pre-blanks
1052 (make-string (org-element-property :pre-blank headline) 10)))
1054 (cond
1055 ;; Case 1: Special Tag
1056 ((member (car tags) org-e-groff-special-tags)
1057 (cond
1058 ((string= (car tags) "BODY") contents)
1060 ((string= (car tags) "NS")
1061 (progn
1062 (push (cons (car tags)
1063 (format ".NS \"%s\" 1 \n%s"
1064 (car (org-element-property :title headline))
1065 (or contents " ")))
1066 org-e-groff-special-content) nil))
1069 (progn
1070 (push (cons (car tags) contents) org-e-groff-special-content)
1071 nil))))
1073 ;; Case 2: This is a footnote section: ignore it.
1074 ((org-element-property :footnote-section-p headline) nil)
1076 ;; Case 3: This is a deep sub-tree: export it as a list item.
1077 ;; Also export as items headlines for which no section
1078 ;; format has been found.
1079 ((or (not section-fmt) (org-export-low-level-p headline info))
1080 ;; Build the real contents of the sub-tree.
1081 (let ((low-level-body
1082 (concat
1083 ;; If the headline is the first sibling, start a list.
1084 (when (org-export-first-sibling-p headline info)
1085 (format "%s\n" (if numberedp ".AL 1\n" ".DL \n")))
1086 ;; Itemize headline
1087 ".LI\n" full-text "\n" headline-label pre-blanks contents)))
1088 ;; If headline is not the last sibling simply return
1089 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1090 ;; blank line.
1091 (if (not (org-export-last-sibling-p headline info)) low-level-body
1092 (replace-regexp-in-string
1093 "[ \t\n]*\\'"
1094 (concat "\n.LE")
1095 low-level-body))))
1097 ;; Case 4. Standard headline. Export it as a section.
1099 (format section-fmt full-text
1100 (concat headline-label pre-blanks contents))))))
1102 ;;; Horizontal Rule
1103 ;; Not supported
1105 ;;; Inline Babel Call
1107 ;; Inline Babel Calls are ignored.
1109 ;;; Inline Src Block
1111 (defun org-e-groff-inline-src-block (inline-src-block contents info)
1112 "Transcode an INLINE-SRC-BLOCK element from Org to Groff.
1113 CONTENTS holds the contents of the item. INFO is a plist holding
1114 contextual information."
1115 (let* ((code (org-element-property :value inline-src-block)))
1116 (cond
1117 (org-e-groff-source-highlight
1118 (let* ((tmpdir (if (featurep 'xemacs)
1119 temp-directory
1120 temporary-file-directory))
1121 (in-file (make-temp-name
1122 (expand-file-name "srchilite" tmpdir)))
1123 (out-file (make-temp-name
1124 (expand-file-name "reshilite" tmpdir)))
1125 (org-lang (org-element-property :language inline-src-block))
1126 (lst-lang (cadr (assq (intern org-lang)
1127 org-e-groff-source-highlight-langs)))
1129 (cmd (concat (expand-file-name "source-highlight")
1130 " -s " lst-lang
1131 " -f groff_mm_color "
1132 " -i " in-file
1133 " -o " out-file)))
1134 (if lst-lang
1135 (let ((code-block ""))
1136 (with-temp-file in-file (insert code))
1137 (shell-command cmd)
1138 (setq code-block (org-file-contents out-file))
1139 (delete-file in-file)
1140 (delete-file out-file)
1141 code-block)
1142 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1143 code))))
1145 ;; Do not use a special package: transcode it verbatim.
1147 (concat ".DS I\n" "\\fC" code "\\fP\n.DE\n")))))
1149 ;;; Inlinetask
1151 (defun org-e-groff-inlinetask (inlinetask contents info)
1152 "Transcode an INLINETASK element from Org to Groff.
1153 CONTENTS holds the contents of the block. INFO is a plist
1154 holding contextual information."
1155 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1156 (todo (and (plist-get info :with-todo-keywords)
1157 (let ((todo (org-element-property :todo-keyword inlinetask)))
1158 (and todo (org-export-data todo info)))))
1159 (todo-type (org-element-property :todo-type inlinetask))
1160 (tags (and (plist-get info :with-tags)
1161 (org-export-get-tags inlinetask info)))
1162 (priority (and (plist-get info :with-priority)
1163 (org-element-property :priority inlinetask))))
1164 ;; If `org-e-groff-format-inlinetask-function' is provided, call it
1165 ;; with appropriate arguments.
1166 (if (functionp org-e-groff-format-inlinetask-function)
1167 (funcall org-e-groff-format-inlinetask-function
1168 todo todo-type priority title tags contents)
1169 ;; Otherwise, use a default template.
1170 (org-e-groff--wrap-label
1171 inlinetask
1172 (let ((full-title
1173 (concat
1174 (when todo (format "\\fB%s\\fP " todo))
1175 (when priority (format " [\\#%c] " priority))
1176 title
1177 (when tags (format " \\fC:%s:\\fP "
1178 (mapconcat 'identity tags ":"))))))
1179 (format (concat "\n.DS I\n"
1180 "%s\n"
1181 ".sp"
1182 "%s\n"
1183 ".DE")
1184 full-title contents))))))
1186 ;;; Italic
1188 (defun org-e-groff-italic (italic contents info)
1189 "Transcode ITALIC from Org to Groff.
1190 CONTENTS is the text with italic markup. INFO is a plist holding
1191 contextual information."
1192 (org-e-groff--text-markup contents 'italic))
1194 ;;; Item
1196 (defun org-e-groff-item (item contents info)
1197 "Transcode an ITEM element from Org to Groff.
1198 CONTENTS holds the contents of the item. INFO is a plist holding
1199 contextual information."
1200 (let* ((bullet (org-element-property :bullet item))
1201 (type (org-element-property
1202 :type (org-element-property :parent item)))
1203 (checkbox (case (org-element-property :checkbox item)
1204 (on "\\o'\\(sq\\(mu'")
1205 (off "\\(sq")
1206 (trans "\\o'\\(sq\\(mi'")))
1207 (tag (let ((tag (org-element-property :tag item)))
1208 ;; Check-boxes must belong to the tag.
1209 (and tag (format "%s"
1210 (concat checkbox
1211 (org-export-data tag info)))))))
1213 (cond
1214 ((or checkbox tag)
1215 (concat ".LI ""\"" (or tag (concat "\\ " checkbox)) "\""
1216 "\n"
1217 (org-trim (or contents " "))))
1218 ((eq type 'ordered)
1219 (concat ".LI"
1220 "\n"
1221 (org-trim (or contents " "))))
1223 (let* ((bullet (org-trim bullet))
1224 (marker (cond ((string= "-" bullet) "\\(em")
1225 ((string= "*" bullet) "\\(bu")
1226 (t "\\(dg"))))
1227 (concat ".LI " marker "\n"
1228 (org-trim (or contents " "))))))))
1230 ;;; Keyword
1232 (defun org-e-groff-keyword (keyword contents info)
1233 "Transcode a KEYWORD element from Org to Groff.
1234 CONTENTS is nil. INFO is a plist holding contextual information."
1235 (let ((key (org-element-property :key keyword))
1236 (value (org-element-property :value keyword)))
1237 (cond
1238 ((string= key "GROFF") value)
1239 (t nil))))
1241 ;;; LaTeX Environment
1243 (defun org-e-groff-latex-environment (latex-environment contents info)
1244 "Transcode a LATEX-ENVIRONMENT element from Org to Groff.
1245 CONTENTS is nil. INFO is a plist holding contextual information."
1246 (let ((label (org-element-property :name latex-environment))
1247 (value (org-remove-indentation
1248 (org-element-property :value latex-environment))))
1249 (if (not (org-string-nw-p label)) value
1250 ;; Environment is labelled: label must be within the environment
1251 ;; (otherwise, a reference pointing to that element will count
1252 ;; the section instead).
1253 (with-temp-buffer
1254 (insert value)
1255 (goto-char (point-min))
1256 (forward-line)
1257 (insert (format "%s\n" label))
1258 (buffer-string)))))
1260 ;;; LaTeX Fragment
1262 (defun org-e-groff-latex-fragment (latex-fragment contents info)
1263 "Transcode a LATEX-FRAGMENT object from Org to Groff.
1264 CONTENTS is nil. INFO is a plist holding contextual information."
1265 (org-element-property :value latex-fragment))
1267 ;;; Line Break
1269 (defun org-e-groff-line-break (line-break contents info)
1270 "Transcode a LINE-BREAK object from Org to Groff.
1271 CONTENTS is nil. INFO is a plist holding contextual information."
1272 ".br\n")
1274 ;;; Link
1275 ;; Inline images just place a call to .PSPIC or .PS/.PE
1276 ;; and load the graph.
1278 (defun org-e-groff-link--inline-image (link info)
1279 "Return Groff code for an inline image.
1280 LINK is the link pointing to the inline image. INFO is a plist
1281 used as a communication channel."
1282 (let* ((parent (org-export-get-parent-element link))
1283 (path (let ((raw-path (org-element-property :path link)))
1284 (if (not (file-name-absolute-p raw-path)) raw-path
1285 (expand-file-name raw-path))))
1286 (attr (org-export-read-attribute :attr_groff link))
1287 (placement
1288 (case (plist-get attr :position)
1289 ('center "")
1290 ('left "-L")
1291 ('right "-R")
1292 (t "")))
1293 (width (or (plist-get attr :width) ""))
1294 (height (or (plist-get attr :height) ""))
1295 (caption (and (not (plist-get attr :disable-caption))
1296 (org-e-groff--caption/label-string parent info))))
1297 ;; Now clear ATTR from any special keyword and set a default value
1298 ;; if nothing is left. Return proper string.
1299 (concat
1300 (cond
1301 ((and org-e-groff-raster-to-ps
1302 (or (string-match ".\.png$" path)
1303 (string-match ".\.jpg$" path)))
1304 (let ((eps-path (concat path ".eps")))
1305 (shell-command (format org-e-groff-raster-to-ps path eps-path))
1306 (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1307 placement eps-path width height)))
1308 ((string-match ".\.pic$" path)
1309 (format "\n.PS\ncopy \"%s\"\n.PE" path))
1310 (t (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1311 placement path width height)))
1312 (and caption (format "\n.FG \"%s\"" caption)))))
1314 (defun org-e-groff-link (link desc info)
1315 "Transcode a LINK object from Org to Groff.
1317 DESC is the description part of the link, or the empty string.
1318 INFO is a plist holding contextual information. See
1319 `org-export-data'."
1321 (let* ((type (org-element-property :type link))
1322 (raw-path (org-element-property :path link))
1323 ;; Ensure DESC really exists, or set it to nil.
1324 (desc (and (not (string= desc "")) desc))
1325 (imagep (org-export-inline-image-p
1326 link org-e-groff-inline-image-rules))
1327 (path (cond
1328 ((member type '("http" "https" "ftp" "mailto"))
1329 (concat type ":" raw-path))
1330 ((string= type "file")
1331 (when (string-match "\\(.+\\)::.+" raw-path)
1332 (setq raw-path (match-string 1 raw-path)))
1333 (if (file-name-absolute-p raw-path)
1334 (concat "file://" (expand-file-name raw-path))
1335 (concat "file://" raw-path)))
1336 (t raw-path)))
1337 protocol)
1338 (cond
1339 ;; Image file.
1340 (imagep (org-e-groff-link--inline-image link info))
1341 ;; import groff files
1342 ((and (string= type "file")
1343 (string-match ".\.groff$" raw-path))
1344 (concat ".so " raw-path "\n"))
1345 ;; Radio link: transcode target's contents and use them as link's
1346 ;; description.
1347 ((string= type "radio")
1348 (let ((destination (org-export-resolve-radio-link link info)))
1349 (when destination
1350 (format "\\fI [%s] \\fP"
1351 (org-export-solidify-link-text path)))))
1353 ;; Links pointing to an headline: find destination and build
1354 ;; appropriate referencing command.
1355 ((member type '("custom-id" "fuzzy" "id"))
1356 (let ((destination (if (string= type "fuzzy")
1357 (org-export-resolve-fuzzy-link link info)
1358 (org-export-resolve-id-link link info))))
1359 (case (org-element-type destination)
1360 ;; Id link points to an external file.
1361 (plain-text
1362 (if desc (format "%s \\fBat\\fP \\fIfile://%s\\fP" desc destination)
1363 (format "\\fI file://%s \\fP" destination)))
1364 ;; Fuzzy link points nowhere.
1365 ('nil
1366 (format org-e-groff-link-with-unknown-path-format
1367 (or desc
1368 (org-export-data
1369 (org-element-property :raw-link link) info))))
1370 ;; Fuzzy link points to an invisible target.
1371 (keyword nil)
1372 ;; LINK points to an headline. If headlines are numbered
1373 ;; and the link has no description, display headline's
1374 ;; number. Otherwise, display description or headline's
1375 ;; title.
1376 (headline
1377 (let ((label ""))
1378 (if (and (plist-get info :section-numbers) (not desc))
1379 (format "\\fI%s\\fP" label)
1380 (format "\\fI%s\\fP"
1381 (or desc
1382 (org-export-data
1383 (org-element-property :title destination) info))))))
1384 ;; Fuzzy link points to a target. Do as above.
1385 (otherwise
1386 (let ((path (org-export-solidify-link-text path)))
1387 (if (not desc) (format "\\fI%s\\fP" path)
1388 (format "%s \\fBat\\fP \\fI%s\\fP" desc path)))))))
1389 ;; External link with a description part.
1390 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
1391 ;; External link without a description part.
1392 (path (format "\\fI%s\\fP" path))
1393 ;; No path, only description. Try to do something useful.
1394 (t (format org-e-groff-link-with-unknown-path-format desc)))))
1396 ;;; Paragraph
1398 (defun org-e-groff-paragraph (paragraph contents info)
1399 "Transcode a PARAGRAPH element from Org to Groff.
1400 CONTENTS is the contents of the paragraph, as a string. INFO is
1401 the plist used as a communication channel."
1402 (let ((parent (plist-get (nth 1 paragraph) :parent)))
1403 (when parent
1404 (let* ((parent-type (car parent))
1405 (fixed-paragraph "")
1406 (class (plist-get info :groff-class))
1407 (class-options (plist-get info :groff-class-options))
1408 (classes (assoc class org-e-groff-classes))
1409 (classes-options (car (last classes)))
1410 (paragraph-option (plist-get classes-options :paragraph)))
1411 (cond
1412 ((and (symbolp paragraph-option)
1413 (fboundp paragraph-option))
1414 (funcall paragraph-option parent-type parent contents))
1415 ((and (eq parent-type 'item)
1416 (plist-get (nth 1 parent) :bullet))
1417 (setq fixed-paragraph (concat "" contents)))
1418 ((eq parent-type 'section)
1419 (setq fixed-paragraph (concat ".P\n" contents)))
1420 ((eq parent-type 'footnote-definition)
1421 (setq fixed-paragraph (concat "" contents)))
1422 (t (setq fixed-paragraph (concat "" contents))))
1423 fixed-paragraph))))
1425 ;;; Plain List
1427 (defun org-e-groff-plain-list (plain-list contents info)
1428 "Transcode a PLAIN-LIST element from Org to Groff.
1429 CONTENTS is the contents of the list. INFO is a plist holding
1430 contextual information."
1431 (let* ((type (org-element-property :type plain-list))
1432 (attr (mapconcat #'identity
1433 (org-element-property :attr_groff plain-list)
1434 " "))
1435 (groff-type (cond
1436 ((eq type 'ordered) ".AL")
1437 ((eq type 'unordered) ".BL")
1438 ((eq type 'descriptive) ".VL 2.0i"))))
1439 (org-e-groff--wrap-label
1440 plain-list
1441 (format "%s\n%s\n.LE" groff-type contents))))
1443 ;;; Plain Text
1445 (defun org-e-groff-plain-text (text info)
1446 "Transcode a TEXT string from Org to Groff.
1447 TEXT is the string to transcode. INFO is a plist holding
1448 contextual information."
1449 ;; Protect
1450 (setq text (replace-regexp-in-string
1451 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1452 "$\\" text nil t 1))
1453 ;; Handle quotation marks
1454 (setq text (org-e-groff--quotation-marks text info))
1455 ;; Handle Special Characters
1456 (if org-e-groff-special-char
1457 (dolist (special-char-list org-e-groff-special-char)
1458 (setq text
1459 (replace-regexp-in-string (car special-char-list)
1460 (cdr special-char-list) text))))
1461 ;; Handle break preservation if required.
1462 (when (plist-get info :preserve-breaks)
1463 (setq text (replace-regexp-in-string
1464 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" text)))
1465 ;; Return value.
1466 text)
1468 ;;; Planning
1470 (defun org-e-groff-planning (planning contents info)
1471 "Transcode a PLANNING element from Org to Groff.
1472 CONTENTS is nil. INFO is a plist holding contextual
1473 information."
1474 (concat
1475 (mapconcat
1476 'identity
1477 (delq nil
1478 (list
1479 (let ((closed (org-element-property :closed planning)))
1480 (when closed
1481 (concat
1482 (format "\\fR %s \\fP" org-closed-string)
1483 (format org-e-groff-inactive-timestamp-format
1484 (org-translate-time closed)))))
1485 (let ((deadline (org-element-property :deadline planning)))
1486 (when deadline
1487 (concat
1488 (format "\\fB %s \\fP" org-deadline-string)
1489 (format org-e-groff-active-timestamp-format
1490 (org-translate-time deadline)))))
1491 (let ((scheduled (org-element-property :scheduled planning)))
1492 (when scheduled
1493 (concat
1494 (format "\\fR %s \\fP" org-scheduled-string)
1495 (format org-e-groff-active-timestamp-format
1496 (org-translate-time scheduled)))))))
1498 ""))
1500 ;;; Property Drawer
1502 (defun org-e-groff-property-drawer (property-drawer contents info)
1503 "Transcode a PROPERTY-DRAWER element from Org to Groff.
1504 CONTENTS is nil. INFO is a plist holding contextual
1505 information."
1506 ;; The property drawer isn't exported but we want separating blank
1507 ;; lines nonetheless.
1510 ;;; Quote Block
1512 (defun org-e-groff-quote-block (quote-block contents info)
1513 "Transcode a QUOTE-BLOCK element from Org to Groff.
1514 CONTENTS holds the contents of the block. INFO is a plist
1515 holding contextual information."
1516 (org-e-groff--wrap-label
1517 quote-block
1518 (format ".DS I\n.I\n%s\n.R\n.DE" contents)))
1520 ;;; Quote Section
1522 (defun org-e-groff-quote-section (quote-section contents info)
1523 "Transcode a QUOTE-SECTION element from Org to Groff.
1524 CONTENTS is nil. INFO is a plist holding contextual information."
1525 (let ((value (org-remove-indentation
1526 (org-element-property :value quote-section))))
1527 (when value (format ".DS L\n\\fI%s\\fP\n.DE\n" value))))
1529 ;;; Radio Target
1531 (defun org-e-groff-radio-target (radio-target text info)
1532 "Transcode a RADIO-TARGET object from Org to Groff.
1533 TEXT is the text of the target. INFO is a plist holding
1534 contextual information."
1535 (format "%s - %s"
1536 (org-export-solidify-link-text
1537 (org-element-property :value radio-target))
1538 text))
1540 ;;; Section
1542 (defun org-e-groff-section (section contents info)
1543 "Transcode a SECTION element from Org to Groff.
1544 CONTENTS holds the contents of the section. INFO is a plist
1545 holding contextual information."
1546 contents)
1548 ;;; Special Block
1550 (defun org-e-groff-special-block (special-block contents info)
1551 "Transcode a SPECIAL-BLOCK element from Org to Groff.
1552 CONTENTS holds the contents of the block. INFO is a plist
1553 holding contextual information."
1554 (let ((type (downcase (org-element-property :type special-block))))
1555 (org-e-groff--wrap-label
1556 special-block
1557 (format "%s\n" contents))))
1559 ;;; Src Block
1561 (defun org-e-groff-src-block (src-block contents info)
1562 "Transcode a SRC-BLOCK element from Org to Groff.
1563 CONTENTS holds the contents of the item. INFO is a plist holding
1564 contextual information."
1565 (let* ((lang (org-element-property :language src-block))
1566 (label (org-element-property :name src-block))
1567 (code (org-element-property :value src-block))
1568 (custom-env (and lang
1569 (cadr (assq (intern lang)
1570 org-e-groff-custom-lang-environments))))
1571 (num-start (case (org-element-property :number-lines src-block)
1572 (continued (org-export-get-loc src-block info))
1573 (new 0)))
1574 (retain-labels (org-element-property :retain-labels src-block))
1575 (caption (and (not (org-export-read-attribute
1576 :attr_groff src-block :disable-caption))
1577 (org-e-groff--caption/label-string src-block info))))
1579 (cond
1580 ;; Case 1. No source fontification.
1581 ((not org-e-groff-source-highlight)
1582 (concat
1583 (format ".DS I\n\\fC%s\\fP\n.DE\n"
1584 (org-export-format-code-default src-block info))
1585 (and caption (format ".EX \"%s\" " caption))))
1587 ;; Case 2. Source fontification.
1588 (org-e-groff-source-highlight
1589 (let* ((tmpdir (if (featurep 'xemacs)
1590 temp-directory
1591 temporary-file-directory))
1592 (in-file (make-temp-name
1593 (expand-file-name "srchilite" tmpdir)))
1594 (out-file (make-temp-name
1595 (expand-file-name "reshilite" tmpdir)))
1597 (org-lang (org-element-property :language src-block))
1598 (lst-lang (cadr (assq (intern org-lang)
1599 org-e-groff-source-highlight-langs)))
1601 (cmd (concat "source-highlight"
1602 " -s " lst-lang
1603 " -f groff_mm_color "
1604 " -i " in-file
1605 " -o " out-file)))
1607 (concat
1608 (if lst-lang
1609 (let ((code-block ""))
1610 (with-temp-file in-file (insert code))
1611 (shell-command cmd)
1612 (setq code-block (org-file-contents out-file))
1613 (delete-file in-file)
1614 (delete-file out-file)
1615 (format "%s\n" code-block))
1616 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1617 code))
1618 (and caption (format ".EX \"%s\" " caption))))))))
1621 ;;; Statistics Cookie
1623 (defun org-e-groff-statistics-cookie (statistics-cookie contents info)
1624 "Transcode a STATISTICS-COOKIE object from Org to Groff.
1625 CONTENTS is nil. INFO is a plist holding contextual information."
1626 (org-element-property :value statistics-cookie))
1629 ;;; Strike-Through
1631 (defun org-e-groff-strike-through (strike-through contents info)
1632 "Transcode STRIKE-THROUGH from Org to Groff.
1633 CONTENTS is the text with strike-through markup. INFO is a plist
1634 holding contextual information."
1635 (org-e-groff--text-markup contents 'strike-through))
1637 ;;; Subscript
1639 (defun org-e-groff-subscript (subscript contents info)
1640 "Transcode a SUBSCRIPT object from Org to Groff.
1641 CONTENTS is the contents of the object. INFO is a plist holding
1642 contextual information."
1643 (format "\\d\\s-2%s\\s+2\\u" contents))
1645 ;;; Superscript "^_%s$
1647 (defun org-e-groff-superscript (superscript contents info)
1648 "Transcode a SUPERSCRIPT object from Org to Groff.
1649 CONTENTS is the contents of the object. INFO is a plist holding
1650 contextual information."
1651 (format "\\u\\s-2%s\\s+2\\d" contents))
1654 ;;; Table
1656 ;; `org-e-groff-table' is the entry point for table transcoding. It
1657 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1658 ;; delegates the job to `org-e-groff-table--org-table' function,
1659 ;; depending of the type of the table.
1661 ;; `org-e-groff-table--align-string' is a subroutine used to build
1662 ;; alignment string for Org tables.
1664 (defun org-e-groff-table (table contents info)
1665 "Transcode a TABLE element from Org to Groff.
1666 CONTENTS is the contents of the table. INFO is a plist holding
1667 contextual information."
1668 (cond
1669 ;; Case 1: verbatim table.
1670 ((or org-e-groff-tables-verbatim
1671 (let ((attr (read (format "(%s)"
1672 (mapconcat
1673 #'identity
1674 (org-element-property :attr_groff table) " ")))))
1675 (and attr (plist-get attr :verbatim))))
1677 (format ".DS L\n\\fC%s\\fP\n.DE"
1678 ;; Re-create table, without affiliated keywords.
1679 (org-trim
1680 (org-element-interpret-data
1681 `(table nil ,@(org-element-contents table))))))
1683 ;; Case 2: Standard table.
1684 (t (org-e-groff-table--org-table table contents info))))
1686 (defun org-e-groff-table--align-string (divider table info)
1687 "Return an appropriate Groff alignment string.
1688 TABLE is the considered table. INFO is a plist used as
1689 a communication channel."
1690 (let (alignment)
1691 ;; Extract column groups and alignment from first (non-rule)
1692 ;; row.
1693 (org-element-map
1694 (org-element-map
1695 table 'table-row
1696 (lambda (row)
1697 (and (eq (org-element-property :type row) 'standard) row))
1698 info 'first-match)
1699 'table-cell
1700 (lambda (cell)
1701 (let* ((borders (org-export-table-cell-borders cell info))
1702 (raw-width (org-export-table-cell-width cell info))
1703 (width-cm (when raw-width (/ raw-width 5)))
1704 (width (if raw-width (format "w(%dc)"
1705 (if (< width-cm 1) 1 width-cm)) "")))
1706 ;; Check left border for the first cell only.
1707 ;; Alignment is nil on assignment
1709 (when (and (memq 'left borders) (not alignment))
1710 (push "|" alignment))
1711 (push
1712 (case (org-export-table-cell-alignment cell info)
1713 (left (concat "l" width divider))
1714 (right (concat "r" width divider))
1715 (center (concat "c" width divider)))
1716 alignment)
1717 (when (memq 'right borders) (push "|" alignment))))
1718 info)
1719 (apply 'concat (reverse alignment))))
1721 (defun org-e-groff-table--org-table (table contents info)
1722 "Return appropriate Groff code for an Org table.
1724 TABLE is the table type element to transcode. CONTENTS is its
1725 contents, as a string. INFO is a plist used as a communication
1726 channel.
1728 This function assumes TABLE has `org' as its `:type' attribute."
1729 (let* ((attr (org-export-read-attribute :attr_groff table))
1730 (label (org-element-property :name table))
1731 (caption (and (not (plist-get attr :disable-caption))
1732 (org-e-groff--caption/label-string table info)))
1733 (divider (if (plist-get attr :divider) "|" " "))
1735 ;; Determine alignment string.
1736 (alignment (org-e-groff-table--align-string divider table info))
1738 ;; Extract others display options.
1740 (lines (org-split-string contents "\n"))
1742 (attr-list
1743 (let (result-list)
1744 (dolist (attr-item
1745 (list
1746 (if (plist-get attr :expand)
1747 "expand" nil)
1749 (case (plist-get attr :placement)
1750 ('center "center")
1751 ('left nil)
1753 (if org-e-groff-tables-centered
1754 "center" "")))
1756 (case (plist-get attr :boxtype)
1757 ('box "box")
1758 ('doublebox "doublebox")
1759 ('allbox "allbox")
1760 ('none nil)
1761 (t "box"))))
1763 (if (not (null attr-item))
1764 (add-to-list 'result-list attr-item)))
1765 result-list))
1767 (title-line (plist-get attr :title-line))
1768 (long-cells (plist-get attr :long-cells))
1770 (table-format
1771 (concat
1772 (format "%s"
1773 (or (car attr-list) ""))
1775 (let (output-list)
1776 (when (cdr attr-list)
1777 (dolist (attr-item (cdr attr-list))
1778 (setq output-list (concat output-list
1779 (format ",%s" attr-item)))))
1780 output-list) "")))
1781 (first-line
1782 (when lines (org-split-string (car lines) "\t"))))
1783 ;; Prepare the final format string for the table.
1786 (cond
1787 ;; Others.
1788 (lines
1789 (concat ".TS\n " table-format ";\n"
1790 (format "%s.\n"
1791 (let ((final-line ""))
1792 (when title-line
1793 (dotimes (i (length first-line))
1794 (setq final-line (concat final-line "cb" divider))))
1796 (setq final-line (concat final-line "\n"))
1798 (if alignment
1799 (setq final-line (concat final-line alignment))
1800 (dotimes (i (length first-line))
1801 (setq final-line (concat final-line "c" divider))))
1802 final-line))
1804 (format "%s.TE\n"
1805 (let ((final-line "")
1806 (long-line "")
1807 (lines (org-split-string contents "\n")))
1809 (dolist (line-item lines)
1810 (setq long-line "")
1812 (if long-cells
1813 (progn
1814 (if (string= line-item "_")
1815 (setq long-line (format "%s\n" line-item))
1816 ;; else string =
1817 (let ((cell-item-list (org-split-string line-item "\t")))
1818 (dolist (cell-item cell-item-list)
1820 (cond ((eq cell-item (car (last cell-item-list)))
1821 (setq long-line (concat long-line
1822 (format "T{\n%s\nT}\t\n" cell-item))))
1824 (setq long-line (concat long-line
1825 (format "T{\n%s\nT}\t" cell-item))))))
1826 long-line))
1827 ;; else long cells
1828 (setq final-line (concat final-line long-line)))
1830 (setq final-line (concat final-line line-item "\n"))))
1831 final-line))
1833 (if caption (format ".TB \"%s\"" caption) ""))))))
1835 ;;; Table Cell
1837 (defun org-e-groff-table-cell (table-cell contents info)
1838 "Transcode a TABLE-CELL element from Org to Groff
1839 CONTENTS is the cell contents. INFO is a plist used as
1840 a communication channel."
1841 (progn
1842 (concat (if (and contents
1843 org-e-groff-table-scientific-notation
1844 (string-match orgtbl-exp-regexp contents))
1845 ;; Use appropriate format string for scientific
1846 ;; notation.
1847 (format org-e-groff-table-scientific-notation
1848 (match-string 1 contents)
1849 (match-string 2 contents))
1850 contents)
1851 (when (org-export-get-next-element table-cell info) "\t"))))
1854 ;;; Table Row
1856 (defun org-e-groff-table-row (table-row contents info)
1857 "Transcode a TABLE-ROW element from Org to Groff
1858 CONTENTS is the contents of the row. INFO is a plist used as
1859 a communication channel."
1860 ;; Rules are ignored since table separators are deduced from
1861 ;; borders of the current row.
1862 (when (eq (org-element-property :type table-row) 'standard)
1863 (let* ((attr (mapconcat 'identity
1864 (org-element-property
1865 :attr_groff (org-export-get-parent table-row))
1866 " "))
1867 ;; TABLE-ROW's borders are extracted from its first cell.
1868 (borders
1869 (org-export-table-cell-borders
1870 (car (org-element-contents table-row)) info)))
1871 (concat
1872 ;; Mark horizontal lines
1873 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1874 contents
1875 (cond
1876 ;; When BOOKTABS are activated enforce bottom rule even when
1877 ;; no hline was specifically marked.
1878 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1879 ((memq 'below borders) "\n_"))))))
1881 ;;; Target
1883 (defun org-e-groff-target (target contents info)
1884 "Transcode a TARGET object from Org to Groff.
1885 CONTENTS is nil. INFO is a plist holding contextual
1886 information."
1887 (format "\\fI%s\\fP"
1888 (org-export-solidify-link-text (org-element-property :value target))))
1890 ;;; Timestamp
1892 (defun org-e-groff-timestamp (timestamp contents info)
1893 "Transcode a TIMESTAMP object from Org to Groff.
1894 CONTENTS is nil. INFO is a plist holding contextual
1895 information."
1896 (let ((value (org-translate-time (org-element-property :value timestamp)))
1897 (type (org-element-property :type timestamp)))
1898 (cond ((memq type '(active active-range))
1899 (format org-e-groff-active-timestamp-format value))
1900 ((memq type '(inactive inactive-range))
1901 (format org-e-groff-inactive-timestamp-format value))
1902 (t (format org-e-groff-diary-timestamp-format value)))))
1904 ;;; Underline
1906 (defun org-e-groff-underline (underline contents info)
1907 "Transcode UNDERLINE from Org to Groff.
1908 CONTENTS is the text with underline markup. INFO is a plist
1909 holding contextual information."
1910 (org-e-groff--text-markup contents 'underline))
1912 ;;; Verbatim
1914 (defun org-e-groff-verbatim (verbatim contents info)
1915 "Transcode a VERBATIM object from Org to Groff.
1916 CONTENTS is nil. INFO is a plist used as a communication
1917 channel."
1918 (org-e-groff--text-markup (org-element-property :value verbatim) 'verbatim))
1920 ;;; Verse Block
1922 (defun org-e-groff-verse-block (verse-block contents info)
1923 "Transcode a VERSE-BLOCK element from Org to Groff.
1924 CONTENTS is verse block contents. INFO is a plist holding
1925 contextual information."
1926 (format ".DS C\n.ft HI\n%s\n.ft\n.DE" contents))
1929 ;;; Interactive functions
1931 (defun org-e-groff-export-to-groff
1932 (&optional subtreep visible-only body-only ext-plist pub-dir)
1933 "Export current buffer to a Groff file.
1935 If narrowing is active in the current buffer, only export its
1936 narrowed part.
1938 If a region is active, export that region.
1940 When optional argument SUBTREEP is non-nil, export the sub-tree
1941 at point, extracting information from the headline properties
1942 first.
1944 When optional argument VISIBLE-ONLY is non-nil, don't export
1945 contents of hidden elements.
1947 EXT-PLIST, when provided, is a property list with external
1948 parameters overriding Org default settings, but still inferior to
1949 file-local settings.
1951 When optional argument PUB-DIR is set, use it as the publishing
1952 directory.
1954 Return output file's name."
1955 (interactive)
1956 (setq org-e-groff-registered-references nil)
1957 (setq org-e-groff-special-content nil)
1958 (let ((outfile (org-export-output-file-name ".groff" subtreep pub-dir)))
1959 (org-export-to-file
1960 'e-groff outfile subtreep visible-only body-only ext-plist)))
1962 (defun org-e-groff-export-to-pdf
1963 (&optional subtreep visible-only body-only ext-plist pub-dir)
1964 "Export current buffer to Groff then process through to PDF.
1966 If narrowing is active in the current buffer, only export its
1967 narrowed part.
1969 If a region is active, export that region.
1971 When optional argument SUBTREEP is non-nil, export the sub-tree
1972 at point, extracting information from the headline properties
1973 first.
1975 When optional argument VISIBLE-ONLY is non-nil, don't export
1976 contents of hidden elements.
1978 EXT-PLIST, when provided, is a property list with external
1979 parameters overriding Org default settings, but still inferior to
1980 file-local settings.
1982 When optional argument PUB-DIR is set, use it as the publishing
1983 directory.
1985 Return PDF file's name."
1986 (interactive)
1987 (org-e-groff-compile
1988 (org-e-groff-export-to-groff
1989 subtreep visible-only body-only ext-plist pub-dir)))
1991 (defun org-e-groff-compile (grofffile)
1992 "Compile a Groff file.
1994 GROFFFILE is the name of the file being compiled. Processing is
1995 done through the command specified in `org-e-groff-pdf-process'.
1997 Return PDF file name or an error if it couldn't be produced."
1998 (let* ((wconfig (current-window-configuration))
1999 (grofffile (file-truename grofffile))
2000 (base (file-name-sans-extension grofffile))
2001 errors)
2002 (message (format "Processing Groff file %s ..." grofffile))
2003 (unwind-protect
2004 (progn
2005 (cond
2006 ;; A function is provided: Apply it.
2007 ((functionp org-e-groff-pdf-process)
2008 (funcall org-e-groff-pdf-process (shell-quote-argument grofffile)))
2009 ;; A list is provided: Replace %b, %f and %o with appropriate
2010 ;; values in each command before applying it. Output is
2011 ;; redirected to "*Org PDF Groff Output*" buffer.
2012 ((consp org-e-groff-pdf-process)
2013 (let* ((out-dir (or (file-name-directory grofffile) "./"))
2014 (outbuf (get-buffer-create "*Org PDF Groff Output*")))
2015 (mapc
2016 (lambda (command)
2017 (shell-command
2018 (replace-regexp-in-string
2019 "%b" (shell-quote-argument base)
2020 (replace-regexp-in-string
2021 "%f" (shell-quote-argument grofffile)
2022 (replace-regexp-in-string
2023 "%o" (shell-quote-argument out-dir) command t t)
2024 t t) t t)
2025 outbuf))
2026 org-e-groff-pdf-process)
2027 ;; Collect standard errors from output buffer.
2028 (setq errors (org-e-groff-collect-errors outbuf))))
2029 (t (error "No valid command to process to PDF")))
2030 (let ((pdffile (concat base ".pdf")))
2031 ;; Check for process failure. Provide collected errors if
2032 ;; possible.
2033 (if (not (file-exists-p pdffile))
2034 (error (concat (format "PDF file %s wasn't produced" pdffile)
2035 (when errors (concat ": " errors))))
2036 ;; Else remove log files, when specified, and signal end of
2037 ;; process to user, along with any error encountered.
2038 (when org-e-groff-remove-logfiles
2039 (dolist (ext org-e-groff-logfiles-extensions)
2040 (let ((file (concat base "." ext)))
2041 (when (file-exists-p file) (delete-file file)))))
2042 (message (concat "Process completed"
2043 (if (not errors) "."
2044 (concat " with errors: " errors)))))
2045 ;; Return output file name.
2046 pdffile))
2047 (set-window-configuration wconfig))))
2049 (defun org-e-groff-collect-errors (buffer)
2050 "Collect some kind of errors from \"groff\" output
2051 BUFFER is the buffer containing output.
2052 Return collected error types as a string, or nil if there was
2053 none."
2054 (with-current-buffer buffer
2055 (save-excursion
2056 (goto-char (point-max))
2057 ;; Find final run
2058 nil)))
2061 (provide 'org-e-groff)
2062 ;;; org-e-groff.el ends here