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