Merge branch 'maint'
[org-mode.git] / contrib / lisp / ox-groff.el
blob1a15eca4d6fb101e7a020d976d133a728d54e702
1 ;;; ox-groff.el --- Groff Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2014 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
9 ;; This file is not part of GNU Emacs.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This library implements a Groff Memorandum Macro back-end for Org
26 ;; generic exporter.
28 ;; To test it, run
30 ;; M-: (org-export-to-buffer 'groff "*Test Groff*") RET
32 ;; in an org-mode buffer then switch to the buffer to see the Groff
33 ;; export. See ox.el for more details on how 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 'ox)
43 (defvar orgtbl-exp-regexp)
46 ;;; Define Back-End
48 (org-export-define-backend 'groff
49 '((bold . org-groff-bold)
50 (center-block . org-groff-center-block)
51 (clock . org-groff-clock)
52 (code . org-groff-code)
53 (comment . (lambda (&rest args) ""))
54 (comment-block . (lambda (&rest args) ""))
55 (drawer . org-groff-drawer)
56 (dynamic-block . org-groff-dynamic-block)
57 (entity . org-groff-entity)
58 (example-block . org-groff-example-block)
59 (export-snippet . org-groff-export-snippet)
60 (fixed-width . org-groff-fixed-width)
61 (footnote-definition . org-groff-footnote-definition)
62 (footnote-reference . org-groff-footnote-reference)
63 (headline . org-groff-headline)
64 (horizontal-rule . org-groff-horizontal-rule)
65 (inline-src-block . org-groff-inline-src-block)
66 (inlinetask . org-groff-inlinetask)
67 (italic . org-groff-italic)
68 (item . org-groff-item)
69 (keyword . org-groff-keyword)
70 (line-break . org-groff-line-break)
71 (link . org-groff-link)
72 (node-property . org-groff-node-property)
73 (paragraph . org-groff-paragraph)
74 (plain-list . org-groff-plain-list)
75 (plain-text . org-groff-plain-text)
76 (planning . org-groff-planning)
77 (property-drawer . org-groff-property-drawer)
78 (quote-block . org-groff-quote-block)
79 (radio-target . org-groff-radio-target)
80 (section . org-groff-section)
81 (special-block . org-groff-special-block)
82 (src-block . org-groff-src-block)
83 (statistics-cookie . org-groff-statistics-cookie)
84 (strike-through . org-groff-strike-through)
85 (subscript . org-groff-subscript)
86 (superscript . org-groff-superscript)
87 (table . org-groff-table)
88 (table-cell . org-groff-table-cell)
89 (table-row . org-groff-table-row)
90 (target . org-groff-target)
91 (template . org-groff-template)
92 (timestamp . org-groff-timestamp)
93 (underline . org-groff-underline)
94 (verbatim . org-groff-verbatim)
95 (verse-block . org-groff-verse-block))
96 :export-block "GROFF"
97 :menu-entry
98 '(?g "Export to GROFF"
99 ((?g "As GROFF file" org-groff-export-to-groff)
100 (?p "As PDF file" org-groff-export-to-pdf)
101 (?o "As PDF file and open"
102 (lambda (a s v b)
103 (if a (org-groff-export-to-pdf t s v b)
104 (org-open-file (org-groff-export-to-pdf nil s v b)))))))
105 :options-alist
106 '((:groff-class "GROFF_CLASS" nil org-groff-default-class t)
107 (:groff-class-options "GROFF_CLASS_OPTIONS" nil nil t)
108 (:groff-header-extra "GROFF_HEADER" nil nil newline)))
112 ;;; User Configurable Variables
114 (defgroup org-export-groff nil
115 "Options for exporting Org mode files to Groff."
116 :tag "Org Export Groff"
117 :group 'org-export)
119 ;;; Preamble
121 (defcustom org-groff-default-class "internal"
122 "The default Groff class."
123 :group 'org-export-groff
124 :type '(string :tag "Groff class"))
126 (defcustom org-groff-classes
127 '(("file" ".MT 1"
128 (:heading 'default :type "memo" :last-section "toc"))
129 ("internal" ".MT 0"
130 (:heading 'default :type "memo" :last-section "toc"))
131 ("programmer" ".MT 2"
132 (:heading 'default :type "memo" :last-section "toc"))
133 ("engineer" ".MT 3"
134 (:heading 'default :type "memo" :last-section "toc"))
135 ("external" ".MT 4"
136 (:heading 'default :type "memo" :last-section "toc"))
137 ("letter" ".MT 5"
138 (:heading 'default :type "memo" :last-section "sign"))
139 ("custom" ".so file"
140 (:heading custom-function :type "custom" :last-section "toc"))
141 ("dummy" ""
142 (:heading 'default :type "memo"))
143 ("ms" "ms"
144 (:heading 'default :type "cover" :last-section "toc"))
145 ("se_ms" "se_ms"
146 (:heading 'default :type "cover" :last-section "toc"))
147 ("block" "BL"
148 (:heading 'default :type "letter" :last-section "sign"))
149 ("semiblock" "SB"
150 (:heading 'default :type "letter" :last-section "sign"))
151 ("fullblock" "FB"
152 (:heading 'default :type "letter" :last-section "sign"))
153 ("simplified" "SP"
154 (:heading 'default :type "letter" :last-section "sign"))
155 ("none" "" (:heading 'default :type "custom")))
157 ;; none means, no Cover or Memorandum Type and no calls to AU, AT, ND and TL
158 ;; This is to facilitate the creation of custom pages.
160 ;; dummy means, no Cover or Memorandum Type but calls to AU, AT, ND and TL
161 ;; are made. This is to facilitate Abstract Insertion.
163 "This list describes the attributes for the documents being created.
164 It allows for the creation of new "
165 :group 'org-export-groff
166 :type '(repeat
167 (list (string :tag "Document Type")
168 (string :tag "Header")
169 (repeat :tag "Options" :inline t
170 (choice
171 (list :tag "Heading")
172 (function :tag "Hook computing sectioning"))))))
174 ;;; Headline
176 (defconst org-groff-special-tags
177 '("FROM" "TO" "ABSTRACT" "APPENDIX" "BODY" "NS"))
179 (defcustom org-groff-format-headline-function nil
180 "Function to format headline text.
182 This function will be called with 5 arguments:
183 TODO the todo keyword (string or nil).
184 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
185 PRIORITY the priority of the headline (integer or nil)
186 TEXT the main headline text (string).
187 TAGS the tags as a list of strings (list of strings or nil).
189 The function result will be used in the section format string.
191 As an example, one could set the variable to the following, in
192 order to reproduce the default set-up:
194 \(defun org-groff-format-headline (todo todo-type priority text tags)
195 \"Default format function for a headline.\"
196 \(concat (when todo
197 \(format \"\\fB%s\\fP \" todo))
198 \(when priority
199 \(format \"[\\#%c] \" priority))
200 text
201 \(when tags
202 \(format \" %s \"
203 \(mapconcat 'identity tags \":\"))))"
204 :group 'org-export-groff
205 :type 'function)
207 ;;; Timestamps
209 (defcustom org-groff-active-timestamp-format "\\fI%s\\fP"
210 "A printf format string to be applied to active timestamps."
211 :group 'org-export-groff
212 :type 'string)
214 (defcustom org-groff-inactive-timestamp-format "\\fI%s\\fP"
215 "A printf format string to be applied to inactive timestamps."
216 :group 'org-export-groff
217 :type 'string)
219 (defcustom org-groff-diary-timestamp-format "\\fI%s\\fP"
220 "A printf format string to be applied to diary timestamps."
221 :group 'org-export-groff
222 :type 'string)
224 ;;; Links
226 (defcustom org-groff-inline-image-rules
227 '(("file" . "\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'")
228 ("fuzzy" . "\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'"))
229 "Rules characterizing image files that can be inlined into Groff.
231 A rule consists in an association whose key is the type of link
232 to consider, and value is a regexp that will be matched against
233 link's path.
235 Note that, by default, the image extensions actually allowed
236 depend on the way the Groff file is processed. When used with
237 pdfgroff, pdf, jpg and png images are OK. When processing
238 through dvi to Postscript, only ps and eps are allowed. The
239 default we use here encompasses both."
240 :group 'org-export-groff
241 :type '(alist :key-type (string :tag "Type")
242 :value-type (regexp :tag "Path")))
244 (defcustom org-groff-link-with-unknown-path-format "\\fI%s\\fP"
245 "Format string for links with unknown path type."
246 :group 'org-export-groff
247 :type 'string)
249 ;;; Tables
251 (defcustom org-groff-tables-centered t
252 "When non-nil, tables are exported in a center environment."
253 :group 'org-export-groff
254 :type 'boolean)
256 (defcustom org-groff-tables-verbatim nil
257 "When non-nil, tables are exported verbatim."
258 :group 'org-export-groff
259 :type 'boolean)
261 (defcustom org-groff-table-scientific-notation "%sE%s"
262 "Format string to display numbers in scientific notation.
263 The format should have \"%s\" twice, for mantissa and exponent
264 \(i.e. \"%s\\\\times10^{%s}\").
266 When nil, no transformation is made."
267 :group 'org-export-groff
268 :type '(choice
269 (string :tag "Format string")
270 (const :tag "No formatting")))
272 ;;; Text markup
274 (defcustom org-groff-text-markup-alist
275 '((bold . "\\fB%s\\fP")
276 (code . "\\fC%s\\fP")
277 (italic . "\\fI%s\\fP")
278 (strike-through . "\\fC%s\\fP") ; Strike through and underline
279 (underline . "\\fI%s\\fP") ; need to be revised.
280 (verbatim . "protectedtexttt"))
281 "Alist of Groff expressions to convert text markup.
283 The key must be a symbol among `bold', `code', `italic',
284 `strike-through', `underline' and `verbatim'. The value is
285 a formatting string to wrap fontified text with it.
287 If no association can be found for a given markup, text will be
288 returned as-is."
289 :group 'org-export-groff
290 :type 'alist
291 :options '(bold code italic strike-through underline verbatim))
293 ;;; Drawers
295 (defcustom org-groff-format-drawer-function nil
296 "Function called to format a drawer in Groff code.
298 The function must accept two parameters:
299 NAME the drawer name, like \"LOGBOOK\"
300 CONTENTS the contents of the drawer.
302 The function should return the string to be exported.
304 For example, the variable could be set to the following function
305 in order to mimic default behaviour:
307 \(defun org-groff-format-drawer-default \(name contents\)
308 \"Format a drawer element for Groff export.\"
309 contents\)"
310 :group 'org-export-groff
311 :type 'function)
313 ;;; Inlinetasks
315 (defcustom org-groff-format-inlinetask-function nil
316 "Function called to format an inlinetask in Groff code.
318 The function must accept six parameters:
319 TODO the todo keyword, as a string
320 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
321 PRIORITY the inlinetask priority, as a string
322 NAME the inlinetask name, as a string.
323 TAGS the inlinetask tags, as a list of strings.
324 CONTENTS the contents of the inlinetask, as a string.
326 The function should return the string to be exported.
328 For example, the variable could be set to the following function
329 in order to mimic default behaviour:
331 \(defun org-groff-format-inlinetask \(todo type priority name tags contents\)
332 \"Format an inline task element for Groff export.\"
333 \(let ((full-title
334 \(concat
335 \(when todo
336 \(format \"\\fB%s\\fP \" todo))
337 \(when priority (format \"[\\#%c] \" priority))
338 title
339 \(when tags
340 \(format \":%s:\"
341 \(mapconcat 'identity tags \":\")))))
342 \(format (concat \".DS L\\n\"
343 \"%s\\n\\n\"
344 \"%s\"
345 \".DE\")
346 full-title contents))"
347 :group 'org-export-groff
348 :type 'function)
350 ;; Src blocks
352 (defcustom org-groff-source-highlight nil
353 "Use GNU source highlight to embellish source blocks "
354 :group 'org-export-groff
355 :type 'boolean)
357 (defcustom org-groff-source-highlight-langs
358 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
359 (scheme "scheme")
360 (c "c") (cc "cpp") (csharp "csharp") (d "d")
361 (fortran "fortran") (cobol "cobol") (pascal "pascal")
362 (ada "ada") (asm "asm")
363 (perl "perl") (cperl "perl")
364 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
365 (java "java") (javascript "javascript")
366 (tex "latex")
367 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
368 (ocaml "caml") (caml "caml")
369 (sql "sql") (sqlite "sql")
370 (html "html") (css "css") (xml "xml")
371 (bat "bat") (bison "bison") (clipper "clipper")
372 (ldap "ldap") (opa "opa")
373 (php "php") (postscript "postscript") (prolog "prolog")
374 (properties "properties") (makefile "makefile")
375 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
376 "Alist mapping languages to their listing language counterpart.
377 The key is a symbol, the major mode symbol without the \"-mode\".
378 The value is the string that should be inserted as the language
379 parameter for the listings package. If the mode name and the
380 listings name are the same, the language does not need an entry
381 in this list - but it does not hurt if it is present."
382 :group 'org-export-groff
383 :type '(repeat
384 (list
385 (symbol :tag "Major mode ")
386 (string :tag "Listings language"))))
388 (defcustom org-groff-source-highlight-options nil
389 "Association list of options for the groff listings package.
391 These options are supplied as a comma-separated list to the
392 \\lstset command. Each element of the association list should be
393 a list containing two strings: the name of the option, and the
394 value. For example,
396 (setq org-groff-source-highlight-options
397 '((\"basicstyle\" \"\\small\")
398 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
400 will typeset the code in a small size font with underlined, bold
401 black keywords.
403 Note that the same options will be applied to blocks of all
404 languages."
405 :group 'org-export-groff
406 :type '(repeat
407 (list
408 (string :tag "Listings option name ")
409 (string :tag "Listings option value"))))
411 (defvar org-groff-custom-lang-environments nil
412 "Alist mapping languages to language-specific Groff environments.
414 It is used during export of src blocks by the listings and
415 groff packages. For example,
417 \(setq org-groff-custom-lang-environments
418 '\(\(python \"pythoncode\"\)\)\)
420 would have the effect that if org encounters begin_src python
421 during groff export it will use pythoncode as the source-highlight
422 language.")
424 ;;; Plain text
426 (defcustom org-groff-special-char
427 '(("(c)" . "\\\\(co")
428 ("(tm)" . "\\\\(tm")
429 ("(rg)" . "\\\\(rg"))
430 "CONS list in which the value of the car
431 is replace on the value of the CDR. "
432 :group 'org-export-groff
433 :type '(list
434 (cons :tag "Character Subtitute"
435 (string :tag "Original Character Group")
436 (string :tag "Replacement Character"))))
438 ;;; Compilation
440 (defcustom org-groff-pdf-process
441 '("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
442 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
443 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf")
445 "Commands to process a Groff file to a PDF file.
446 This is a list of strings, each of them will be given to the
447 shell as a command. %f in the command will be replaced by the
448 full file name, %b by the file base name \(i.e. without
449 extension) and %o by the base directory of the file."
450 :group 'org-export-pdf
451 :type '(choice
452 (repeat :tag "Shell command sequence"
453 (string :tag "Shell command"))
454 (const :tag "2 runs of pdfgroff"
455 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
456 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
457 (const :tag "3 runs of pdfgroff"
458 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
459 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
460 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
461 (function)))
463 (defcustom org-groff-logfiles-extensions
464 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
465 "The list of file extensions to consider as Groff logfiles."
466 :group 'org-export-groff
467 :type '(repeat (string :tag "Extension")))
469 (defcustom org-groff-remove-logfiles t
470 "Non-nil means remove the logfiles produced by PDF production.
471 These are the .aux, .log, .out, and .toc files."
472 :group 'org-export-groff
473 :type 'boolean)
475 (defcustom org-groff-organization "Org User"
476 "Name of the organization used to populate the .AF command."
477 :group 'org-export-groff
478 :type 'string)
480 (defcustom org-groff-raster-to-ps nil
481 "Command used to convert raster to EPS. Nil for no conversion. Make sure that
482 `org-groff-inline-image-rules' is adjusted accordingly if not conversion is being
483 done. In this case, remove the entries for jpg and png in the file and fuzzy lists."
484 :group 'org-export-groff
485 :type '(choice
486 (repeat :tag "Shell Command Sequence" (string :tag "Shell Command"))
487 (const :tag "sam2p" "a=%s;b=%s;sam2p ${a} ${b} ;grep -v BeginData ${b} > b_${b};mv b_${b} ${b}" )
488 (const :tag "NetPNM" "a=%s;b=%s;pngtopnm ${a} | pnmtops -noturn > ${b}" )
489 (const :tag "None" nil)))
491 (defvar org-groff-registered-references nil)
492 (defvar org-groff-special-content nil)
496 ;;; Internal Functions
498 (defun org-groff--caption/label-string (element info)
499 "Return caption and label Groff string for ELEMENT.
501 INFO is a plist holding contextual information. If there's no
502 caption nor label, return the empty string.
504 For non-floats, see `org-groff--wrap-label'."
505 (let ((main (org-export-get-caption element))
506 (short (org-export-get-caption element t))
507 (label (org-element-property :name element)))
508 (cond ((and (not main) (not label)) "")
509 ((not main) (format "\\fI%s\\fP" label))
510 ;; Option caption format with short name.
511 (short (format "%s\n.br\n - %s\n"
512 (org-export-data short info)
513 (org-export-data main info)))
514 ;; Standard caption format.
515 (t (format "\\fR%s\\fP" (org-export-data main info))))))
517 (defun org-groff--wrap-label (element output)
518 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
519 This function shouldn't be used for floats. See
520 `org-groff--caption/label-string'."
521 (let ((label (org-element-property :name element)))
522 (if (or (not output) (not label) (string= output "") (string= label ""))
523 output
524 (concat (format "%s\n.br\n" label) output))))
526 (defun org-groff--text-markup (text markup)
527 "Format TEXT depending on MARKUP text markup.
528 See `org-groff-text-markup-alist' for details."
529 (let ((fmt (cdr (assq markup org-groff-text-markup-alist))))
530 (cond
531 ;; No format string: Return raw text.
532 ((not fmt) text)
533 ((string= "protectedtexttt" fmt)
534 (let ((start 0)
535 (trans '(("\\" . "\\")))
536 (rtn "")
537 char)
538 (while (string-match "[\\{}$%&_#~^]" text)
539 (setq char (match-string 0 text))
540 (if (> (match-beginning 0) 0)
541 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
542 (setq text (substring text (1+ (match-beginning 0))))
543 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
544 rtn (concat rtn char)))
545 (setq text (concat rtn text))
546 (format "\\fC%s\\fP" text)))
547 ;; Else use format string.
548 (t (format fmt text)))))
551 (defun org-groff--get-tagged-content (tag info)
552 (cdr (assoc tag org-groff-special-content)))
554 (defun org-groff--mt-head (title contents attr info)
555 (concat
557 ;; 1. Insert Organization
558 (let ((firm-option (plist-get attr :firm)))
559 (cond
560 ((stringp firm-option)
561 (format ".AF \"%s\" \n" firm-option))
562 (t (format ".AF \"%s\" \n" (or org-groff-organization "")))))
564 ;; 2. Title
565 (let ((subtitle1 (plist-get attr :subtitle1))
566 (subtitle2 (plist-get attr :subtitle2)))
568 (cond
569 ((string= "" title)
570 (format ".TL \"%s\" \"%s\" \n%s\n"
571 (or subtitle1 "")
572 (or subtitle2 "") " "))
574 ((not (or subtitle1 subtitle2))
575 (format ".TL\n%s\n"
576 (or title "")))
578 (format ".TL \"%s\" \"%s \" \n%s\n"
579 (or subtitle1 "")
580 (or subtitle2 "") title))))
582 ;; 3. Author.
583 ;; In Groff, .AU *MUST* be placed after .TL
584 ;; If From, populate with data from From else
586 (let ((author (and (plist-get info :with-author)
587 (let ((auth (plist-get info :author)))
588 (and auth (org-export-data auth info)))))
589 (email (and (plist-get info :with-email)
590 (org-export-data (plist-get info :email) info)))
591 (from-data (org-groff--get-tagged-content "FROM" info))
593 (to-data (org-groff--get-tagged-content "TO" info)))
595 (cond
596 ((and author from-data)
597 (let ((au-line
598 (mapconcat
599 (lambda (from-line)
600 (format " \"%s\" " from-line))
601 (split-string
602 (setq from-data
603 (replace-regexp-in-string "\\.P\n" "" from-data)) "\n") "")))
605 (concat
606 (format ".AU \"%s\" " author) au-line "\n")))
608 ((and author email (not (string= "" email)))
609 (format ".AU \"%s\" \"%s\"\n" author email))
611 (author (format ".AU \"%s\"\n" author))
613 (t ".AU \"\" \n")))
616 ;; 4. Author Title, if present
617 (let ((at-item (plist-get attr :author-title)))
618 (if (and at-item (stringp at-item))
619 (format ".AT \"%s\" \n" at-item)
620 ""))
622 ;; 5. Date.
623 (when (plist-get info :with-date)
624 (let ((date (org-export-data (org-export-get-date info) info)))
625 (and (org-string-nw-p date) (format ".ND \"%s\"\n" date))))
628 ;; If Abstract, then Populate Abstract
631 (let ((abstract-data (org-groff--get-tagged-content "ABSTRACT" info))
632 (to-data (org-groff--get-tagged-content "TO" info)))
633 (cond
634 (abstract-data
635 (format ".AS\n%s\n.AE\n" abstract-data))
636 (to-data
637 (format ".AS\n%s\n.AE\n" to-data))))))
639 (defun org-groff--letter-head (title contents attr info)
640 (let ((author (and (plist-get info :with-author)
641 (let ((auth (plist-get info :author)))
642 (and auth (org-export-data auth info)))))
643 (email (and (plist-get info :with-email)
644 (org-export-data (plist-get info :email) info)))
645 (from-data (org-groff--get-tagged-content "FROM" info))
646 (at-item (plist-get attr :author-title))
647 (to-data (org-groff--get-tagged-content "TO" info)))
650 ;; If FROM then get data from FROM
651 (if from-data
652 (setq from-data
653 (replace-regexp-in-string "\\.P\n" "" from-data))
654 (setq from-data ""))
656 (if to-data
657 (setq to-data
658 (replace-regexp-in-string "\\.P\n" "" to-data))
659 (setq from-data ""))
661 (concat
662 (cond
663 (from-data
664 (format ".WA \"%s\" \"%s\" \n%s\n.WE\n" author (or at-item "") from-data))
665 ((and author email (not (string= "" email)))
666 (format ".WA \"%s\"\n \"%s\"\n.WE\n" author email))
667 (author (format ".WA \"%s\"\n.WE\n" author))
668 (t ".WA \"\" \n.WE\n"))
670 ;; If TO then get data from TO
672 (when to-data
673 (format ".IA \n%s\n.IE\n" to-data)))))
676 ;;; Template
678 (defun org-groff-template (contents info)
679 "Return complete document string after Groff conversion.
680 CONTENTS is the transcoded contents string. INFO is a plist
681 holding export options."
682 (let* ((title (org-export-data (plist-get info :title) info))
683 (attr (read
684 (format "(%s)"
685 (mapconcat
686 #'identity
687 (list (plist-get info :groff-class-options))
688 " "))))
689 (class (plist-get info :groff-class))
690 (class-options (plist-get info :groff-class-options))
691 (classes (assoc class org-groff-classes))
692 (classes-options (car (last classes)))
693 (heading-option (plist-get classes-options :heading))
694 (type-option (plist-get classes-options :type))
695 (last-option (plist-get classes-options :last-section))
696 (hyphenate (plist-get attr :hyphenate))
697 (justify-right (plist-get attr :justify-right))
699 (document-class-string
700 (progn
701 (org-element-normalize-string
702 (let* ((header (nth 1 (assoc class org-groff-classes)))
703 (document-class-item (if (stringp header) header "")))
704 document-class-item)))))
707 (concat
708 (if justify-right
709 (case justify-right
710 ('yes ".SA 1 \n")
711 ('no ".SA 0 \n")
712 (t ""))
715 (if hyphenate
716 (case hyphenate
717 ('yes ".nr Hy 1 \n")
718 ('no ".nr Hy 0 \n")
719 (t ""))
722 (cond
723 ((string= type-option "custom") "")
725 ((and (stringp document-class-string)
726 (string= type-option "cover"))
728 (concat
729 (format ".COVER %s\n" document-class-string)
730 (org-groff--mt-head title contents attr info)
731 ".COVEND\n"))
733 ((string= type-option "memo")
734 (concat
735 (org-groff--mt-head title contents attr info)
736 document-class-string))
737 ((string= type-option "letter")
738 (concat
739 (org-groff--letter-head title contents attr info)
740 (let ((sa-item (plist-get attr :salutation))
741 (cn-item (plist-get attr :confidential))
742 (sj-item (plist-get attr :subject))
743 (rn-item (plist-get attr :reference))
744 (at-item (plist-get attr :attention)))
746 (concat
748 (if (stringp sa-item)
749 (format ".LO SA \"%s\" \n" sa-item)
750 ".LO SA\n")
752 (when cn-item
753 (if (stringp cn-item)
754 (format ".LO CN \"%s\"\n" cn-item)
755 ".LO CN\n"))
757 (when (and at-item (stringp at-item))
758 (format ".LO AT \"%s\" \n" at-item))
759 (when (and title rn-item)
760 (format ".LO RN \"%s\"\n" title))
762 (when (and sj-item (stringp sj-item))
763 (format ".LO SJ \"%s\" \n" sj-item))
766 ".LT " document-class-string "\n"))))
768 (t ""))
770 contents
772 (cond
773 ((string= last-option "toc")
774 ".TC")
775 ((string= last-option "sign")
776 (let ((fc-item (plist-get attr :closing)))
777 (concat (if (stringp fc-item)
778 (format ".FC \"%s\" \n" fc-item)
779 ".FC\n")
780 ".SG\n")))
781 (t ""))
783 (progn
784 (mapconcat
785 (lambda (item)
786 (when (string= (car item) "NS")
787 (replace-regexp-in-string
788 "\\.P\n" "" (cdr item))))
789 (reverse org-groff-special-content) "\n")))))
793 ;;; Transcode Functions
795 ;;; Babel Call
797 ;; Babel Calls are ignored.
800 ;;; Bold
802 (defun org-groff-bold (bold contents info)
803 "Transcode BOLD from Org to Groff.
804 CONTENTS is the text with bold markup. INFO is a plist holding
805 contextual information."
806 (org-groff--text-markup contents 'bold))
808 ;;; Center Block
810 (defun org-groff-center-block (center-block contents info)
811 "Transcode a CENTER-BLOCK element from Org to Groff.
812 CONTENTS holds the contents of the center block. INFO is a plist
813 holding contextual information."
814 (org-groff--wrap-label
815 center-block
816 (format ".DS C \n%s\n.DE" contents)))
818 ;;; Clock
820 (defun org-groff-clock (clock contents info)
821 "Transcode a CLOCK element from Org to Groff.
822 CONTENTS is nil. INFO is a plist holding contextual
823 information."
824 (concat
825 (format "\\fB%s\\fP " org-clock-string)
826 (format org-groff-inactive-timestamp-format
827 (concat (org-translate-time
828 (org-element-property :raw-value
829 (org-element-property :value clock)))
830 (let ((time (org-element-property :duration clock)))
831 (and time (format " (%s)" time)))))))
833 ;;; Code
835 (defun org-groff-code (code contents info)
836 "Transcode a CODE object from Org to Groff.
837 CONTENTS is nil. INFO is a plist used as a communication
838 channel."
839 (org-groff--text-markup (org-element-property :value code) 'code))
841 ;;; Comments and Comment Blocks are ignored.
843 ;;; Drawer
845 (defun org-groff-drawer (drawer contents info)
846 "Transcode a DRAWER element from Org to Groff.
847 CONTENTS holds the contents of the block. INFO is a plist
848 holding contextual information."
849 (let* ((name (org-element-property :drawer-name drawer))
850 (output (if (functionp org-groff-format-drawer-function)
851 (funcall org-groff-format-drawer-function
852 name contents)
853 ;; If there's no user defined function: simply
854 ;; display contents of the drawer.
855 contents)))
856 (org-groff--wrap-label drawer output)))
858 ;;; Dynamic Block
860 (defun org-groff-dynamic-block (dynamic-block contents info)
861 "Transcode a DYNAMIC-BLOCK element from Org to Groff.
862 CONTENTS holds the contents of the block. INFO is a plist
863 holding contextual information. See `org-export-data'."
864 (org-groff--wrap-label dynamic-block contents))
866 ;;; Entity
868 (defun org-groff-entity (entity contents info)
869 "Transcode an ENTITY object from Org to Groff.
870 CONTENTS are the definition itself. INFO is a plist holding
871 contextual information."
872 (org-element-property :utf-8 entity))
874 ;;; Example Block
876 (defun org-groff-example-block (example-block contents info)
877 "Transcode an EXAMPLE-BLOCK element from Org to Groff.
878 CONTENTS is nil. INFO is a plist holding contextual
879 information."
880 (org-groff--wrap-label
881 example-block
882 (format ".DS L\n%s\n.DE"
883 (org-export-format-code-default example-block info))))
885 ;;; Export Snippet
887 (defun org-groff-export-snippet (export-snippet contents info)
888 "Transcode a EXPORT-SNIPPET object from Org to Groff.
889 CONTENTS is nil. INFO is a plist holding contextual information."
890 (when (eq (org-export-snippet-backend export-snippet) 'groff)
891 (org-element-property :value export-snippet)))
893 ;;; Fixed Width
895 (defun org-groff-fixed-width (fixed-width contents info)
896 "Transcode a FIXED-WIDTH element from Org to Groff.
897 CONTENTS is nil. INFO is a plist holding contextual information."
898 (org-groff--wrap-label
899 fixed-width
900 (format "\\fC\n%s\\fP"
901 (org-remove-indentation
902 (org-element-property :value fixed-width)))))
904 ;;; Footnote Definition
906 ;; Footnote Definitions are ignored.
908 ;; Footnotes are handled automatically in GROFF. Although manual
909 ;; references can be added, not really required.
911 (defun org-groff-footnote-reference (footnote-reference contents info)
912 ;; Changing from info to footnote-reference
913 (let* ((raw (org-export-get-footnote-definition footnote-reference info))
914 (n (org-export-get-footnote-number footnote-reference info))
915 (data (org-trim (org-export-data raw info)))
916 (ref-id (plist-get (nth 1 footnote-reference) :label)))
917 ;; It is a reference
918 (if (string-match "fn:rl" ref-id)
919 (if (member ref-id org-groff-registered-references)
920 (format "\\*[%s]" ref-id)
921 (progn
922 (push ref-id org-groff-registered-references)
923 (format "\\*(Rf\n.RS \"%s\" \n%s\n.RF\n" ref-id data)))
924 ;; else it is a footnote
925 (format "\\u\\s-2%s\\d\\s+2\n.FS %s\n%s\n.FE\n" n n data))))
927 ;;; Headline
929 (defun org-groff-headline (headline contents info)
930 "Transcode a HEADLINE element from Org to Groff.
931 CONTENTS holds the contents of the headline. INFO is a plist
932 holding contextual information."
933 (let* ((class (plist-get info :groff-class))
934 (level (org-export-get-relative-level headline info))
935 (numberedp (org-export-numbered-headline-p headline info))
936 ;; Section formatting will set two placeholders: one for the
937 ;; title and the other for the contents.
938 (classes (assoc class org-groff-classes))
939 (classes-options (car (last classes)))
940 (heading-option (plist-get classes-options :heading))
941 (section-fmt
942 (progn
943 (cond
944 ((and (symbolp heading-option)
945 (fboundp heading-option))
946 (funcall heading-option level numberedp))
947 ((> level 7) nil)
948 (t (if numberedp
949 (concat ".H " (number-to-string level) " \"%s\"\n%s")
950 ".HU \"%s\"\n%s")))))
951 ;; End of section-fmt
952 (text (org-export-data (org-element-property :title headline) info))
953 (todo
954 (and (plist-get info :with-todo-keywords)
955 (let ((todo (org-element-property :todo-keyword headline)))
956 (and todo (org-export-data todo info)))))
957 (todo-type (and todo (org-element-property :todo-type headline)))
958 (tags (and (plist-get info :with-tags)
959 (org-export-get-tags headline info)))
960 (priority (and (plist-get info :with-priority)
961 (org-element-property :priority headline)))
962 ;; Create the headline text along with a no-tag version. The
963 ;; latter is required to remove tags from table of contents.
964 (full-text (if (functionp org-groff-format-headline-function)
965 ;; User-defined formatting function.
966 (funcall org-groff-format-headline-function
967 todo todo-type priority text tags)
968 ;; Default formatting.
969 (concat
970 (when todo
971 (format "\\fB%s\\fP " todo))
972 (when priority (format " [\\#%c] " priority))
973 text
974 (when tags
975 (format " \\fC:%s:\\fP "
976 (mapconcat 'identity tags ":"))))))
977 (full-text-no-tag
978 (if (functionp org-groff-format-headline-function)
979 ;; User-defined formatting function.
980 (funcall org-groff-format-headline-function
981 todo todo-type priority text nil)
982 ;; Default formatting.
983 (concat
984 (when todo (format "\\fB%s\\fP " todo))
985 (when priority (format " [\\#%c] " priority))
986 text)))
987 ;; Associate some \label to the headline for internal links.
988 ;; (headline-label
989 ;; (format "\\label{sec-%s}\n"
990 ;; (mapconcat 'number-to-string
991 ;; (org-export-get-headline-number headline info)
992 ;; "-")))
993 (headline-label "")
994 (pre-blanks
995 (make-string (org-element-property :pre-blank headline) 10)))
997 (cond
998 ;; Case 1: Special Tag
999 ((member (car tags) org-groff-special-tags)
1000 (cond
1001 ((string= (car tags) "BODY") contents)
1003 ((string= (car tags) "NS")
1004 (progn
1005 (push (cons (car tags)
1006 (format ".NS \"%s\" 1 \n%s"
1007 (car (org-element-property :title headline))
1008 (or contents " ")))
1009 org-groff-special-content) nil))
1012 (progn
1013 (push (cons (car tags) contents) org-groff-special-content)
1014 nil))))
1016 ;; Case 2: This is a footnote section: ignore it.
1017 ((org-element-property :footnote-section-p headline) nil)
1019 ;; Case 3: This is a deep sub-tree: export it as a list item.
1020 ;; Also export as items headlines for which no section
1021 ;; format has been found.
1022 ((or (not section-fmt) (org-export-low-level-p headline info))
1023 ;; Build the real contents of the sub-tree.
1024 (let ((low-level-body
1025 (concat
1026 ;; If the headline is the first sibling, start a list.
1027 (when (org-export-first-sibling-p headline info)
1028 (format "%s\n" (if numberedp ".AL 1\n" ".DL \n")))
1029 ;; Itemize headline
1030 ".LI\n" full-text "\n" headline-label pre-blanks contents)))
1031 ;; If headline is not the last sibling simply return
1032 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1033 ;; blank line.
1034 (if (not (org-export-last-sibling-p headline info)) low-level-body
1035 (replace-regexp-in-string
1036 "[ \t\n]*\\'"
1037 (concat "\n.LE")
1038 low-level-body))))
1040 ;; Case 4. Standard headline. Export it as a section.
1042 (format section-fmt full-text
1043 (concat headline-label pre-blanks contents))))))
1045 ;;; Horizontal Rule
1046 ;; Not supported
1048 ;;; Inline Babel Call
1050 ;; Inline Babel Calls are ignored.
1052 ;;; Inline Src Block
1054 (defun org-groff-inline-src-block (inline-src-block contents info)
1055 "Transcode an INLINE-SRC-BLOCK element from Org to Groff.
1056 CONTENTS holds the contents of the item. INFO is a plist holding
1057 contextual information."
1058 (let* ((code (org-element-property :value inline-src-block)))
1059 (cond
1060 (org-groff-source-highlight
1061 (let* ((tmpdir (if (featurep 'xemacs)
1062 temp-directory
1063 temporary-file-directory))
1064 (in-file (make-temp-name
1065 (expand-file-name "srchilite" tmpdir)))
1066 (out-file (make-temp-name
1067 (expand-file-name "reshilite" tmpdir)))
1068 (org-lang (org-element-property :language inline-src-block))
1069 (lst-lang (cadr (assq (intern org-lang)
1070 org-groff-source-highlight-langs)))
1072 (cmd (concat (expand-file-name "source-highlight")
1073 " -s " lst-lang
1074 " -f groff_mm_color "
1075 " -i " in-file
1076 " -o " out-file)))
1077 (if lst-lang
1078 (let ((code-block ""))
1079 (with-temp-file in-file (insert code))
1080 (shell-command cmd)
1081 (setq code-block (org-file-contents out-file))
1082 (delete-file in-file)
1083 (delete-file out-file)
1084 code-block)
1085 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1086 code))))
1088 ;; Do not use a special package: transcode it verbatim.
1090 (concat ".DS I\n" "\\fC" code "\\fP\n.DE\n")))))
1092 ;;; Inlinetask
1094 (defun org-groff-inlinetask (inlinetask contents info)
1095 "Transcode an INLINETASK element from Org to Groff.
1096 CONTENTS holds the contents of the block. INFO is a plist
1097 holding contextual information."
1098 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1099 (todo (and (plist-get info :with-todo-keywords)
1100 (let ((todo (org-element-property :todo-keyword inlinetask)))
1101 (and todo (org-export-data todo info)))))
1102 (todo-type (org-element-property :todo-type inlinetask))
1103 (tags (and (plist-get info :with-tags)
1104 (org-export-get-tags inlinetask info)))
1105 (priority (and (plist-get info :with-priority)
1106 (org-element-property :priority inlinetask))))
1107 ;; If `org-groff-format-inlinetask-function' is provided, call it
1108 ;; with appropriate arguments.
1109 (if (functionp org-groff-format-inlinetask-function)
1110 (funcall org-groff-format-inlinetask-function
1111 todo todo-type priority title tags contents)
1112 ;; Otherwise, use a default template.
1113 (org-groff--wrap-label
1114 inlinetask
1115 (let ((full-title
1116 (concat
1117 (when todo (format "\\fB%s\\fP " todo))
1118 (when priority (format " [\\#%c] " priority))
1119 title
1120 (when tags (format " \\fC:%s:\\fP "
1121 (mapconcat 'identity tags ":"))))))
1122 (format (concat "\n.DS I\n"
1123 "%s\n"
1124 ".sp"
1125 "%s\n"
1126 ".DE")
1127 full-title contents))))))
1129 ;;; Italic
1131 (defun org-groff-italic (italic contents info)
1132 "Transcode ITALIC from Org to Groff.
1133 CONTENTS is the text with italic markup. INFO is a plist holding
1134 contextual information."
1135 (org-groff--text-markup contents 'italic))
1137 ;;; Item
1139 (defun org-groff-item (item contents info)
1140 "Transcode an ITEM element from Org to Groff.
1141 CONTENTS holds the contents of the item. INFO is a plist holding
1142 contextual information."
1143 (let* ((bullet (org-element-property :bullet item))
1144 (type (org-element-property
1145 :type (org-element-property :parent item)))
1146 (checkbox (case (org-element-property :checkbox item)
1147 (on "\\o'\\(sq\\(mu'")
1148 (off "\\(sq")
1149 (trans "\\o'\\(sq\\(mi'")))
1150 (tag (let ((tag (org-element-property :tag item)))
1151 ;; Check-boxes must belong to the tag.
1152 (and tag (format "%s"
1153 (concat checkbox
1154 (org-export-data tag info)))))))
1156 (cond
1157 ((or checkbox tag)
1158 (concat ".LI ""\"" (or tag (concat "\\ " checkbox)) "\""
1159 "\n"
1160 (org-trim (or contents " "))))
1161 ((eq type 'ordered)
1162 (concat ".LI"
1163 "\n"
1164 (org-trim (or contents " "))))
1166 (let* ((bullet (org-trim bullet))
1167 (marker (cond ((string= "-" bullet) "\\(em")
1168 ((string= "*" bullet) "\\(bu")
1169 (t "\\(dg"))))
1170 (concat ".LI " marker "\n"
1171 (org-trim (or contents " "))))))))
1173 ;;; Keyword
1175 (defun org-groff-keyword (keyword contents info)
1176 "Transcode a KEYWORD element from Org to Groff.
1177 CONTENTS is nil. INFO is a plist holding contextual information."
1178 (let ((key (org-element-property :key keyword))
1179 (value (org-element-property :value keyword)))
1180 (cond
1181 ((string= key "GROFF") value)
1182 (t nil))))
1184 ;;; Line Break
1186 (defun org-groff-line-break (line-break contents info)
1187 "Transcode a LINE-BREAK object from Org to Groff.
1188 CONTENTS is nil. INFO is a plist holding contextual information."
1189 ".br\n")
1191 ;;; Link
1192 ;; Inline images just place a call to .PSPIC or .PS/.PE
1193 ;; and load the graph.
1195 (defun org-groff-link--inline-image (link info)
1196 "Return Groff code for an inline image.
1197 LINK is the link pointing to the inline image. INFO is a plist
1198 used as a communication channel."
1199 (let* ((parent (org-export-get-parent-element link))
1200 (path (let ((raw-path (org-element-property :path link)))
1201 (if (not (file-name-absolute-p raw-path)) raw-path
1202 (expand-file-name raw-path))))
1203 (attr (org-export-read-attribute :attr_groff link))
1204 (placement
1205 (let ((pos (plist-get attr :position)))
1206 (cond ((string= pos 'center) "")
1207 ((string= pos 'left) "-L")
1208 ((string= pos 'right) "-R")
1209 (t ""))))
1210 (width (or (plist-get attr :width) ""))
1211 (height (or (plist-get attr :height) ""))
1212 (caption (and (not (plist-get attr :disable-caption))
1213 (org-groff--caption/label-string parent info))))
1214 ;; Now clear ATTR from any special keyword and set a default value
1215 ;; if nothing is left. Return proper string.
1216 (concat
1217 (cond
1218 ((and org-groff-raster-to-ps
1219 (or (string-match ".\.png$" path)
1220 (string-match ".\.jpg$" path)))
1221 (let ((eps-path (concat path ".eps")))
1222 (shell-command (format org-groff-raster-to-ps path eps-path))
1223 (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1224 placement eps-path width height)))
1225 ((string-match ".\.pic$" path)
1226 (format "\n.PS\ncopy \"%s\"\n.PE" path))
1227 (t (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1228 placement path width height)))
1229 (and caption (format "\n.FG \"%s\"" caption)))))
1231 (defun org-groff-link (link desc info)
1232 "Transcode a LINK object from Org to Groff.
1234 DESC is the description part of the link, or the empty string.
1235 INFO is a plist holding contextual information. See
1236 `org-export-data'."
1238 (let* ((type (org-element-property :type link))
1239 (raw-path (org-element-property :path link))
1240 ;; Ensure DESC really exists, or set it to nil.
1241 (desc (and (not (string= desc "")) desc))
1242 (imagep (org-export-inline-image-p
1243 link org-groff-inline-image-rules))
1244 (path (cond
1245 ((member type '("http" "https" "ftp" "mailto"))
1246 (concat type ":" raw-path))
1247 ((and (string= type "file") (file-name-absolute-p raw-path))
1248 (concat "file://" raw-path))
1249 (t raw-path)))
1250 protocol)
1251 (cond
1252 ;; Image file.
1253 (imagep (org-groff-link--inline-image link info))
1254 ;; import groff files
1255 ((and (string= type "file")
1256 (string-match ".\.groff$" raw-path))
1257 (concat ".so " raw-path "\n"))
1258 ;; Radio link: transcode target's contents and use them as link's
1259 ;; description.
1260 ((string= type "radio")
1261 (let ((destination (org-export-resolve-radio-link link info)))
1262 (when destination
1263 (format "\\fI [%s] \\fP"
1264 (org-export-solidify-link-text
1265 (org-element-property :value destination))))))
1267 ;; Links pointing to a headline: find destination and build
1268 ;; appropriate referencing command.
1269 ((member type '("custom-id" "fuzzy" "id"))
1270 (let ((destination (if (string= type "fuzzy")
1271 (org-export-resolve-fuzzy-link link info)
1272 (org-export-resolve-id-link link info))))
1273 (case (org-element-type destination)
1274 ;; Id link points to an external file.
1275 (plain-text
1276 (if desc (format "%s \\fBat\\fP \\fIfile://%s\\fP" desc destination)
1277 (format "\\fI file://%s \\fP" destination)))
1278 ;; Fuzzy link points nowhere.
1279 ('nil
1280 (format org-groff-link-with-unknown-path-format
1281 (or desc
1282 (org-export-data
1283 (org-element-property :raw-link link) info))))
1284 ;; LINK points to a headline. If headlines are numbered and
1285 ;; the link has no description, display headline's number.
1286 ;; Otherwise, display description or headline's title.
1287 (headline
1288 (let ((label ""))
1289 (if (and (plist-get info :section-numbers) (not desc))
1290 (format "\\fI%s\\fP" label)
1291 (format "\\fI%s\\fP"
1292 (or desc
1293 (org-export-data
1294 (org-element-property :title destination) info))))))
1295 ;; Fuzzy link points to a target. Do as above.
1296 (otherwise
1297 (let ((path (org-export-solidify-link-text path)))
1298 (if (not desc) (format "\\fI%s\\fP" path)
1299 (format "%s \\fBat\\fP \\fI%s\\fP" desc path)))))))
1300 ;; External link with a description part.
1301 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
1302 ;; External link without a description part.
1303 (path (format "\\fI%s\\fP" path))
1304 ;; No path, only description. Try to do something useful.
1305 (t (format org-groff-link-with-unknown-path-format desc)))))
1307 ;;; Node Property
1309 (defun org-groff-node-property (node-property contents info)
1310 "Transcode a NODE-PROPERTY element from Org to Groff.
1311 CONTENTS is nil. INFO is a plist holding contextual
1312 information."
1313 (format "%s:%s"
1314 (org-element-property :key node-property)
1315 (let ((value (org-element-property :value node-property)))
1316 (if value (concat " " value) ""))))
1318 ;;; Paragraph
1320 (defun org-groff-paragraph (paragraph contents info)
1321 "Transcode a PARAGRAPH element from Org to Groff.
1322 CONTENTS is the contents of the paragraph, as a string. INFO is
1323 the plist used as a communication channel."
1324 (let ((parent (plist-get (nth 1 paragraph) :parent)))
1325 (when parent
1326 (let* ((parent-type (car parent))
1327 (fixed-paragraph "")
1328 (class (plist-get info :groff-class))
1329 (class-options (plist-get info :groff-class-options))
1330 (classes (assoc class org-groff-classes))
1331 (classes-options (car (last classes)))
1332 (paragraph-option (plist-get classes-options :paragraph)))
1333 (cond
1334 ((and (symbolp paragraph-option)
1335 (fboundp paragraph-option))
1336 (funcall paragraph-option parent-type parent contents))
1337 ((and (eq parent-type 'item)
1338 (plist-get (nth 1 parent) :bullet))
1339 (setq fixed-paragraph (concat "" contents)))
1340 ((eq parent-type 'section)
1341 (setq fixed-paragraph (concat ".P\n" contents)))
1342 ((eq parent-type 'footnote-definition)
1343 (setq fixed-paragraph (concat "" contents)))
1344 (t (setq fixed-paragraph (concat "" contents))))
1345 fixed-paragraph))))
1347 ;;; Plain List
1349 (defun org-groff-plain-list (plain-list contents info)
1350 "Transcode a PLAIN-LIST element from Org to Groff.
1351 CONTENTS is the contents of the list. INFO is a plist holding
1352 contextual information."
1353 (let* ((type (org-element-property :type plain-list))
1354 (attr (mapconcat #'identity
1355 (org-element-property :attr_groff plain-list)
1356 " "))
1357 (groff-type (cond
1358 ((eq type 'ordered) ".AL")
1359 ((eq type 'unordered) ".BL")
1360 ((eq type 'descriptive) ".VL 2.0i"))))
1361 (org-groff--wrap-label
1362 plain-list
1363 (format "%s\n%s\n.LE" groff-type contents))))
1365 ;;; Plain Text
1367 (defun org-groff-plain-text (text info)
1368 "Transcode a TEXT string from Org to Groff.
1369 TEXT is the string to transcode. INFO is a plist holding
1370 contextual information."
1371 (let ((output text))
1372 ;; Protect various characters.
1373 (setq output (replace-regexp-in-string
1374 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1375 "$\\" output nil t 1))
1376 ;; Activate smart quotes. Be sure to provide original TEXT string
1377 ;; since OUTPUT may have been modified.
1378 (when (plist-get info :with-smart-quotes)
1379 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
1380 ;; Handle Special Characters
1381 (if org-groff-special-char
1382 (dolist (special-char-list org-groff-special-char)
1383 (setq output
1384 (replace-regexp-in-string (car special-char-list)
1385 (cdr special-char-list) output))))
1386 ;; Handle break preservation if required.
1387 (when (plist-get info :preserve-breaks)
1388 (setq output (replace-regexp-in-string
1389 "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n" output)))
1390 ;; Return value.
1391 output))
1393 ;;; Planning
1395 (defun org-groff-planning (planning contents info)
1396 "Transcode a PLANNING element from Org to Groff.
1397 CONTENTS is nil. INFO is a plist holding contextual
1398 information."
1399 (concat
1400 (mapconcat
1401 'identity
1402 (delq nil
1403 (list
1404 (let ((closed (org-element-property :closed planning)))
1405 (when closed
1406 (concat
1407 (format "\\fR %s \\fP" org-closed-string)
1408 (format org-groff-inactive-timestamp-format
1409 (org-translate-time
1410 (org-element-property :raw-value closed))))))
1411 (let ((deadline (org-element-property :deadline planning)))
1412 (when deadline
1413 (concat
1414 (format "\\fB %s \\fP" org-deadline-string)
1415 (format org-groff-active-timestamp-format
1416 (org-translate-time
1417 (org-element-property :raw-value deadline))))))
1418 (let ((scheduled (org-element-property :scheduled planning)))
1419 (when scheduled
1420 (concat
1421 (format "\\fR %s \\fP" org-scheduled-string)
1422 (format org-groff-active-timestamp-format
1423 (org-translate-time
1424 (org-element-property :raw-value scheduled))))))))
1426 ""))
1428 ;;;; Property Drawer
1430 (defun org-groff-property-drawer (property-drawer contents info)
1431 "Transcode a PROPERTY-DRAWER element from Org to Groff.
1432 CONTENTS holds the contents of the drawer. INFO is a plist
1433 holding contextual information."
1434 (and (org-string-nw-p contents)
1435 (format "\\fC\n%s\\fP" contents)))
1437 ;;; Quote Block
1439 (defun org-groff-quote-block (quote-block contents info)
1440 "Transcode a QUOTE-BLOCK element from Org to Groff.
1441 CONTENTS holds the contents of the block. INFO is a plist
1442 holding contextual information."
1443 (org-groff--wrap-label
1444 quote-block
1445 (format ".DS I\n.I\n%s\n.R\n.DE" contents)))
1447 ;;; Radio Target
1449 (defun org-groff-radio-target (radio-target text info)
1450 "Transcode a RADIO-TARGET object from Org to Groff.
1451 TEXT is the text of the target. INFO is a plist holding
1452 contextual information."
1453 (format "%s - %s"
1454 (org-export-solidify-link-text
1455 (org-element-property :value radio-target))
1456 text))
1458 ;;; Section
1460 (defun org-groff-section (section contents info)
1461 "Transcode a SECTION element from Org to Groff.
1462 CONTENTS holds the contents of the section. INFO is a plist
1463 holding contextual information."
1464 contents)
1466 ;;; Special Block
1468 (defun org-groff-special-block (special-block contents info)
1469 "Transcode a SPECIAL-BLOCK element from Org to Groff.
1470 CONTENTS holds the contents of the block. INFO is a plist
1471 holding contextual information."
1472 (if (org-export-raw-special-block-p special-block info)
1473 (org-remove-indentation (org-element-property :raw-value special-block))
1474 (let ((type (downcase (org-element-property :type special-block))))
1475 (org-groff--wrap-label special-block (format "%s\n" contents)))))
1477 ;;; Src Block
1479 (defun org-groff-src-block (src-block contents info)
1480 "Transcode a SRC-BLOCK element from Org to Groff.
1481 CONTENTS holds the contents of the item. INFO is a plist holding
1482 contextual information."
1483 (let* ((lang (org-element-property :language src-block))
1484 (label (org-element-property :name src-block))
1485 (code (org-element-property :value src-block))
1486 (custom-env (and lang
1487 (cadr (assq (intern lang)
1488 org-groff-custom-lang-environments))))
1489 (num-start (case (org-element-property :number-lines src-block)
1490 (continued (org-export-get-loc src-block info))
1491 (new 0)))
1492 (retain-labels (org-element-property :retain-labels src-block))
1493 (caption (and (not (org-export-read-attribute
1494 :attr_groff src-block :disable-caption))
1495 (org-groff--caption/label-string src-block info))))
1497 (cond
1498 ;; Case 1. No source fontification.
1499 ((not org-groff-source-highlight)
1500 (concat
1501 (format ".DS I\n\\fC%s\\fP\n.DE\n"
1502 (org-export-format-code-default src-block info))
1503 (and caption (format ".EX \"%s\" " caption))))
1505 ;; Case 2. Source fontification.
1506 (org-groff-source-highlight
1507 (let* ((tmpdir (if (featurep 'xemacs)
1508 temp-directory
1509 temporary-file-directory))
1510 (in-file (make-temp-name
1511 (expand-file-name "srchilite" tmpdir)))
1512 (out-file (make-temp-name
1513 (expand-file-name "reshilite" tmpdir)))
1515 (org-lang (org-element-property :language src-block))
1516 (lst-lang (cadr (assq (intern org-lang)
1517 org-groff-source-highlight-langs)))
1519 (cmd (concat "source-highlight"
1520 " -s " lst-lang
1521 " -f groff_mm_color "
1522 " -i " in-file
1523 " -o " out-file)))
1525 (concat
1526 (if lst-lang
1527 (let ((code-block ""))
1528 (with-temp-file in-file (insert code))
1529 (shell-command cmd)
1530 (setq code-block (org-file-contents out-file))
1531 (delete-file in-file)
1532 (delete-file out-file)
1533 (format "%s\n" code-block))
1534 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1535 code))
1536 (and caption (format ".EX \"%s\" " caption))))))))
1539 ;;; Statistics Cookie
1541 (defun org-groff-statistics-cookie (statistics-cookie contents info)
1542 "Transcode a STATISTICS-COOKIE object from Org to Groff.
1543 CONTENTS is nil. INFO is a plist holding contextual information."
1544 (org-element-property :value statistics-cookie))
1547 ;;; Strike-Through
1549 (defun org-groff-strike-through (strike-through contents info)
1550 "Transcode STRIKE-THROUGH from Org to Groff.
1551 CONTENTS is the text with strike-through markup. INFO is a plist
1552 holding contextual information."
1553 (org-groff--text-markup contents 'strike-through))
1555 ;;; Subscript
1557 (defun org-groff-subscript (subscript contents info)
1558 "Transcode a SUBSCRIPT object from Org to Groff.
1559 CONTENTS is the contents of the object. INFO is a plist holding
1560 contextual information."
1561 (format "\\d\\s-2%s\\s+2\\u" contents))
1563 ;;; Superscript "^_%s$
1565 (defun org-groff-superscript (superscript contents info)
1566 "Transcode a SUPERSCRIPT object from Org to Groff.
1567 CONTENTS is the contents of the object. INFO is a plist holding
1568 contextual information."
1569 (format "\\u\\s-2%s\\s+2\\d" contents))
1572 ;;; Table
1574 ;; `org-groff-table' is the entry point for table transcoding. It
1575 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1576 ;; delegates the job to `org-groff-table--org-table' function,
1577 ;; depending of the type of the table.
1579 ;; `org-groff-table--align-string' is a subroutine used to build
1580 ;; alignment string for Org tables.
1582 (defun org-groff-table (table contents info)
1583 "Transcode a TABLE element from Org to Groff.
1584 CONTENTS is the contents of the table. INFO is a plist holding
1585 contextual information."
1586 (cond
1587 ;; Case 1: verbatim table.
1588 ((or org-groff-tables-verbatim
1589 (let ((attr (read (format "(%s)"
1590 (mapconcat
1591 #'identity
1592 (org-element-property :attr_groff table) " ")))))
1593 (and attr (plist-get attr :verbatim))))
1595 (format ".DS L\n\\fC%s\\fP\n.DE"
1596 ;; Re-create table, without affiliated keywords.
1597 (org-trim
1598 (org-element-interpret-data
1599 `(table nil ,@(org-element-contents table))))))
1601 ;; Case 2: Standard table.
1602 (t (org-groff-table--org-table table contents info))))
1604 (defun org-groff-table--align-string (divider table info)
1605 "Return an appropriate Groff alignment string.
1606 TABLE is the considered table. INFO is a plist used as
1607 a communication channel."
1608 (let (alignment)
1609 ;; Extract column groups and alignment from first (non-rule) row.
1610 (org-element-map
1611 (org-element-map table 'table-row
1612 (lambda (row)
1613 (and (eq (org-element-property :type row) 'standard) row))
1614 info 'first-match)
1615 'table-cell
1616 (lambda (cell)
1617 (let* ((borders (org-export-table-cell-borders cell info))
1618 (raw-width (org-export-table-cell-width cell info))
1619 (width-cm (when raw-width (/ raw-width 5)))
1620 (width (if raw-width (format "w(%dc)"
1621 (if (< width-cm 1) 1 width-cm)) "")))
1622 ;; Check left border for the first cell only.
1623 ;; Alignment is nil on assignment
1625 (when (and (memq 'left borders) (not alignment))
1626 (push "|" alignment))
1627 (push
1628 (case (org-export-table-cell-alignment cell info)
1629 (left (concat "l" width divider))
1630 (right (concat "r" width divider))
1631 (center (concat "c" width divider)))
1632 alignment)
1633 (when (memq 'right borders) (push "|" alignment))))
1634 info)
1635 (apply 'concat (reverse alignment))))
1637 (defun org-groff-table--org-table (table contents info)
1638 "Return appropriate Groff code for an Org table.
1640 TABLE is the table type element to transcode. CONTENTS is its
1641 contents, as a string. INFO is a plist used as a communication
1642 channel.
1644 This function assumes TABLE has `org' as its `:type' attribute."
1645 (let* ((attr (org-export-read-attribute :attr_groff table))
1646 (label (org-element-property :name table))
1647 (caption (and (not (plist-get attr :disable-caption))
1648 (org-groff--caption/label-string table info)))
1649 (divider (if (plist-get attr :divider) "|" " "))
1651 ;; Determine alignment string.
1652 (alignment (org-groff-table--align-string divider table info))
1654 ;; Extract others display options.
1656 (lines (org-split-string contents "\n"))
1658 (attr-list
1659 (delq nil
1660 (list (and (plist-get attr :expand) "expand")
1661 (let ((placement (plist-get attr :placement)))
1662 (cond ((string= placement 'center) "center")
1663 ((string= placement 'left) nil)
1664 (t (if org-groff-tables-centered "center" ""))))
1665 (or (plist-get attr :boxtype) "box"))))
1667 (title-line (plist-get attr :title-line))
1668 (long-cells (plist-get attr :long-cells))
1670 (table-format
1671 (concat
1672 (or (car attr-list) "")
1674 (let (output-list)
1675 (when (cdr attr-list)
1676 (dolist (attr-item (cdr attr-list))
1677 (setq output-list (concat output-list
1678 (format ",%s" attr-item)))))
1679 output-list) "")))
1680 (first-line
1681 (when lines (org-split-string (car lines) "\t"))))
1682 ;; Prepare the final format string for the table.
1685 (cond
1686 ;; Others.
1687 (lines
1688 (concat ".TS\n " table-format ";\n"
1689 (format "%s.\n"
1690 (let ((final-line ""))
1691 (when title-line
1692 (dotimes (i (length first-line))
1693 (setq final-line (concat final-line "cb" divider))))
1695 (setq final-line (concat final-line "\n"))
1697 (if alignment
1698 (setq final-line (concat final-line alignment))
1699 (dotimes (i (length first-line))
1700 (setq final-line (concat final-line "c" divider))))
1701 final-line))
1703 (format "%s.TE\n"
1704 (let ((final-line "")
1705 (long-line "")
1706 (lines (org-split-string contents "\n")))
1708 (dolist (line-item lines)
1709 (setq long-line "")
1711 (if long-cells
1712 (progn
1713 (if (string= line-item "_")
1714 (setq long-line (format "%s\n" line-item))
1715 ;; else string =
1716 (let ((cell-item-list (org-split-string line-item "\t")))
1717 (dolist (cell-item cell-item-list)
1719 (cond ((eq cell-item (car (last cell-item-list)))
1720 (setq long-line (concat long-line
1721 (format "T{\n%s\nT}\t\n" cell-item))))
1723 (setq long-line (concat long-line
1724 (format "T{\n%s\nT}\t" cell-item))))))
1725 long-line))
1726 ;; else long cells
1727 (setq final-line (concat final-line long-line)))
1729 (setq final-line (concat final-line line-item "\n"))))
1730 final-line))
1732 (if caption (format ".TB \"%s\"" caption) ""))))))
1734 ;;; Table Cell
1736 (defun org-groff-table-cell (table-cell contents info)
1737 "Transcode a TABLE-CELL element from Org to Groff
1738 CONTENTS is the cell contents. INFO is a plist used as
1739 a communication channel."
1740 (progn
1741 (concat (if (and contents
1742 org-groff-table-scientific-notation
1743 (string-match orgtbl-exp-regexp contents))
1744 ;; Use appropriate format string for scientific
1745 ;; notation.
1746 (format org-groff-table-scientific-notation
1747 (match-string 1 contents)
1748 (match-string 2 contents))
1749 contents)
1750 (when (org-export-get-next-element table-cell info) "\t"))))
1753 ;;; Table Row
1755 (defun org-groff-table-row (table-row contents info)
1756 "Transcode a TABLE-ROW element from Org to Groff
1757 CONTENTS is the contents of the row. INFO is a plist used as
1758 a communication channel."
1759 ;; Rules are ignored since table separators are deduced from
1760 ;; borders of the current row.
1761 (when (eq (org-element-property :type table-row) 'standard)
1762 (let* ((attr (mapconcat 'identity
1763 (org-element-property
1764 :attr_groff (org-export-get-parent table-row))
1765 " "))
1766 ;; TABLE-ROW's borders are extracted from its first cell.
1767 (borders
1768 (org-export-table-cell-borders
1769 (car (org-element-contents table-row)) info)))
1770 (concat
1771 ;; Mark horizontal lines
1772 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1773 contents
1774 (cond
1775 ;; When BOOKTABS are activated enforce bottom rule even when
1776 ;; no hline was specifically marked.
1777 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1778 ((memq 'below borders) "\n_"))))))
1780 ;;; Target
1782 (defun org-groff-target (target contents info)
1783 "Transcode a TARGET object from Org to Groff.
1784 CONTENTS is nil. INFO is a plist holding contextual
1785 information."
1786 (format "\\fI%s\\fP"
1787 (org-export-solidify-link-text (org-element-property :value target))))
1789 ;;; Timestamp
1791 (defun org-groff-timestamp (timestamp contents info)
1792 "Transcode a TIMESTAMP object from Org to Groff.
1793 CONTENTS is nil. INFO is a plist holding contextual
1794 information."
1795 (let ((value (org-groff-plain-text
1796 (org-timestamp-translate timestamp) info)))
1797 (case (org-element-property :type timestamp)
1798 ((active active-range)
1799 (format org-groff-active-timestamp-format value))
1800 ((inactive inactive-range)
1801 (format org-groff-inactive-timestamp-format value))
1802 (t (format org-groff-diary-timestamp-format value)))))
1804 ;;; Underline
1806 (defun org-groff-underline (underline contents info)
1807 "Transcode UNDERLINE from Org to Groff.
1808 CONTENTS is the text with underline markup. INFO is a plist
1809 holding contextual information."
1810 (org-groff--text-markup contents 'underline))
1812 ;;; Verbatim
1814 (defun org-groff-verbatim (verbatim contents info)
1815 "Transcode a VERBATIM object from Org to Groff.
1816 CONTENTS is nil. INFO is a plist used as a communication
1817 channel."
1818 (org-groff--text-markup (org-element-property :value verbatim) 'verbatim))
1820 ;;; Verse Block
1822 (defun org-groff-verse-block (verse-block contents info)
1823 "Transcode a VERSE-BLOCK element from Org to Groff.
1824 CONTENTS is verse block contents. INFO is a plist holding
1825 contextual information."
1826 (format ".DS C\n.ft HI\n%s\n.ft\n.DE" contents))
1829 ;;; Interactive functions
1831 (defun org-groff-export-to-groff
1832 (&optional async subtreep visible-only body-only ext-plist)
1833 "Export current buffer to a Groff file.
1835 If narrowing is active in the current buffer, only export its
1836 narrowed part.
1838 If a region is active, export that region.
1840 A non-nil optional argument ASYNC means the process should happen
1841 asynchronously. The resulting file should be accessible through
1842 the `org-export-stack' interface.
1844 When optional argument SUBTREEP is non-nil, export the sub-tree
1845 at point, extracting information from the headline properties
1846 first.
1848 When optional argument VISIBLE-ONLY is non-nil, don't export
1849 contents of hidden elements.
1851 EXT-PLIST, when provided, is a property list with external
1852 parameters overriding Org default settings, but still inferior to
1853 file-local settings.
1855 Return output file's name."
1856 (interactive)
1857 (let ((outfile (org-export-output-file-name ".groff" subtreep))
1858 (org-groff-registered-references nil)
1859 (org-groff-special-content nil))
1860 (org-export-to-file 'groff outfile
1861 async subtreep visible-only body-only ext-plist)))
1863 (defun org-groff-export-to-pdf
1864 (&optional async subtreep visible-only body-only ext-plist)
1865 "Export current buffer to Groff then process through to PDF.
1867 If narrowing is active in the current buffer, only export its
1868 narrowed part.
1870 If a region is active, export that region.
1872 A non-nil optional argument ASYNC means the process should happen
1873 asynchronously. The resulting file should be accessible through
1874 the `org-export-stack' interface.
1876 When optional argument SUBTREEP is non-nil, export the sub-tree
1877 at point, extracting information from the headline properties
1878 first.
1880 When optional argument VISIBLE-ONLY is non-nil, don't export
1881 contents of hidden elements.
1883 EXT-PLIST, when provided, is a property list with external
1884 parameters overriding Org default settings, but still inferior to
1885 file-local settings.
1887 Return PDF file's name."
1888 (interactive)
1889 (let ((outfile (org-export-output-file-name ".groff" subtreep)))
1890 (org-export-to-file 'groff outfile
1891 async subtreep visible-only body-only ext-plist
1892 (lambda (file) (org-groff-compile file)))))
1894 (defun org-groff-compile (file)
1895 "Compile a Groff file.
1897 FILE is the name of the file being compiled. Processing is done
1898 through the command specified in `org-groff-pdf-process'.
1900 Return PDF file name or an error if it couldn't be produced."
1901 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1902 (full-name (file-truename file))
1903 (out-dir (file-name-directory file))
1904 ;; Properly set working directory for compilation.
1905 (default-directory (if (file-name-absolute-p file)
1906 (file-name-directory full-name)
1907 default-directory))
1908 errors)
1909 (message (format "Processing Groff file %s ..." file))
1910 (save-window-excursion
1911 (cond
1912 ;; A function is provided: Apply it.
1913 ((functionp org-groff-pdf-process)
1914 (funcall org-groff-pdf-process (shell-quote-argument file)))
1915 ;; A list is provided: Replace %b, %f and %o with appropriate
1916 ;; values in each command before applying it. Output is
1917 ;; redirected to "*Org PDF Groff Output*" buffer.
1918 ((consp org-groff-pdf-process)
1919 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1920 (mapc
1921 (lambda (command)
1922 (shell-command
1923 (replace-regexp-in-string
1924 "%b" (shell-quote-argument base-name)
1925 (replace-regexp-in-string
1926 "%f" (shell-quote-argument full-name)
1927 (replace-regexp-in-string
1928 "%o" (shell-quote-argument out-dir) command t t)
1929 t t) t t)
1930 outbuf))
1931 org-groff-pdf-process)
1932 ;; Collect standard errors from output buffer.
1933 (setq errors (org-groff-collect-errors outbuf))))
1934 (t (error "No valid command to process to PDF")))
1935 (let ((pdffile (concat out-dir base-name ".pdf")))
1936 ;; Check for process failure. Provide collected errors if
1937 ;; possible.
1938 (if (not (file-exists-p pdffile))
1939 (error (concat (format "PDF file %s wasn't produced" pdffile)
1940 (when errors (concat ": " errors))))
1941 ;; Else remove log files, when specified, and signal end of
1942 ;; process to user, along with any error encountered.
1943 (when org-groff-remove-logfiles
1944 (dolist (ext org-groff-logfiles-extensions)
1945 (let ((file (concat out-dir base-name "." ext)))
1946 (when (file-exists-p file) (delete-file file)))))
1947 (message (concat "Process completed"
1948 (if (not errors) "."
1949 (concat " with errors: " errors)))))
1950 ;; Return output file name.
1951 pdffile))))
1953 (defun org-groff-collect-errors (buffer)
1954 "Collect some kind of errors from \"groff\" output
1955 BUFFER is the buffer containing output.
1956 Return collected error types as a string, or nil if there was
1957 none."
1958 (with-current-buffer buffer
1959 (save-excursion
1960 (goto-char (point-max))
1961 ;; Find final run
1962 nil)))
1965 (provide 'ox-groff)
1966 ;;; ox-groff.el ends here