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
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/>.
24 ;; This library implements a Groff Memorandum Macro back-end for
25 ;; Org generic exporter.
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".
40 (eval-when-compile (require 'cl
))
43 (defvar orgtbl-exp-regexp
)
48 (org-export-define-backend e-groff
49 ((bold . org-e-groff-bold
)
50 (center-block . org-e-groff-center-block
)
51 (clock . org-e-groff-clock
)
52 (code . org-e-groff-code
)
53 (drawer . org-e-groff-drawer
)
54 (dynamic-block . org-e-groff-dynamic-block
)
55 (entity . org-e-groff-entity
)
56 (example-block . org-e-groff-example-block
)
57 (export-block . org-e-groff-export-block
)
58 (export-snippet . org-e-groff-export-snippet
)
59 (fixed-width . org-e-groff-fixed-width
)
60 (footnote-definition . org-e-groff-footnote-definition
)
61 (footnote-reference . org-e-groff-footnote-reference
)
62 (headline . org-e-groff-headline
)
63 (horizontal-rule . org-e-groff-horizontal-rule
)
64 (inline-src-block . org-e-groff-inline-src-block
)
65 (inlinetask . org-e-groff-inlinetask
)
66 (italic . org-e-groff-italic
)
67 (item . org-e-groff-item
)
68 (keyword . org-e-groff-keyword
)
69 ;; (latex-environment . org-e-groff-latex-environment)
70 ;; (latex-fragment . org-e-groff-latex-fragment)
71 (line-break . org-e-groff-line-break
)
72 (link . org-e-groff-link
)
73 (paragraph . org-e-groff-paragraph
)
74 (plain-list . org-e-groff-plain-list
)
75 (plain-text . org-e-groff-plain-text
)
76 (planning . org-e-groff-planning
)
77 (property-drawer . org-e-groff-property-drawer
)
78 (quote-block . org-e-groff-quote-block
)
79 (quote-section . org-e-groff-quote-section
)
80 (radio-target . org-e-groff-radio-target
)
81 (section . org-e-groff-section
)
82 (special-block . org-e-groff-special-block
)
83 (src-block . org-e-groff-src-block
)
84 (statistics-cookie . org-e-groff-statistics-cookie
)
85 (strike-through . org-e-groff-strike-through
)
86 (subscript . org-e-groff-subscript
)
87 (superscript . org-e-groff-superscript
)
88 (table . org-e-groff-table
)
89 (table-cell . org-e-groff-table-cell
)
90 (table-row . org-e-groff-table-row
)
91 (target . org-e-groff-target
)
92 (template . org-e-groff-template
)
93 (timestamp . org-e-groff-timestamp
)
94 (underline . org-e-groff-underline
)
95 (verbatim . org-e-groff-verbatim
)
96 (verse-block . org-e-groff-verse-block
))
100 ((?g
"As GROFF file" org-e-groff-export-to-groff
)
101 (?p
"As PDF file" org-e-groff-export-to-pdf
)
102 (?o
"As PDF file and open"
103 (lambda (s v b
) (org-open-file (org-e-groff-export-to-pdf s v b
))))))
105 ((:date
"DATE" nil org-e-groff-date-format t
)
106 (:groff-class
"GROFF_CLASS" nil org-e-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-e-groff nil
115 "Options for exporting Org mode files to Groff."
116 :tag
"Org Export Groff"
121 (defcustom org-e-groff-default-class
"internal"
122 "The default Groff class."
123 :group
'org-export-e-groff
124 :type
'(string :tag
"Groff class"))
126 (defcustom org-e-groff-classes
128 (:heading
'default
:type
"memo" :last-section
"toc"))
130 (:heading
'default
:type
"memo" :last-section
"toc"))
131 ("programmer" ".MT 2"
132 (:heading
'default
:type
"memo" :last-section
"toc"))
134 (:heading
'default
:type
"memo" :last-section
"toc"))
136 (:heading
'default
:type
"memo" :last-section
"toc"))
138 (:heading
'default
:type
"memo" :last-section
"sign"))
140 (:heading custom-function
:type
"custom" :last-section
"toc"))
142 (:heading
'default
:type
"memo"))
144 (:heading
'default
:type
"cover" :last-section
"toc"))
146 (:heading
'default
:type
"cover" :last-section
"toc"))
148 (:heading
'default
:type
"letter" :last-section
"sign"))
150 (:heading
'default
:type
"letter" :last-section
"sign"))
152 (:heading
'default
:type
"letter" :last-section
"sign"))
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-e-groff
167 (list (string :tag
"Document Type")
168 (string :tag
"Header")
169 (repeat :tag
"Options" :inline t
171 (list :tag
"Heading")
172 (function :tag
"Hook computing sectioning"))))))
175 (defcustom org-e-groff-date-format
176 (format-time-string "%Y-%m-%d")
177 "Format string for .ND "
178 :group
'org-export-e-groff
183 (defconst org-e-groff-special-tags
184 '("FROM" "TO" "ABSTRACT" "APPENDIX" "BODY" "NS"))
186 (defcustom org-e-groff-format-headline-function nil
187 "Function to format headline text.
189 This function will be called with 5 arguments:
190 TODO the todo keyword (string or nil).
191 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
192 PRIORITY the priority of the headline (integer or nil)
193 TEXT the main headline text (string).
194 TAGS the tags as a list of strings (list of strings or nil).
196 The function result will be used in the section format string.
198 As an example, one could set the variable to the following, in
199 order to reproduce the default set-up:
201 \(defun org-e-groff-format-headline (todo todo-type priority text tags)
202 \"Default format function for an headline.\"
204 \(format \"\\fB%s\\fP \" todo))
206 \(format \"[\\#%c] \" priority))
210 \(mapconcat 'identity tags \":\"))))"
211 :group
'org-export-e-groff
216 (defcustom org-e-groff-active-timestamp-format
"\\fI%s\\fP"
217 "A printf format string to be applied to active timestamps."
218 :group
'org-export-e-groff
221 (defcustom org-e-groff-inactive-timestamp-format
"\\fI%s\\fP"
222 "A printf format string to be applied to inactive timestamps."
223 :group
'org-export-e-groff
226 (defcustom org-e-groff-diary-timestamp-format
"\\fI%s\\fP"
227 "A printf format string to be applied to diary timestamps."
228 :group
'org-export-e-groff
233 (defcustom org-e-groff-inline-image-rules
234 '(("file" .
"\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'")
235 ("fuzzy" .
"\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'"))
236 "Rules characterizing image files that can be inlined into Groff.
238 A rule consists in an association whose key is the type of link
239 to consider, and value is a regexp that will be matched against
242 Note that, by default, the image extensions actually allowed
243 depend on the way the Groff file is processed. When used with
244 pdfgroff, pdf, jpg and png images are OK. When processing
245 through dvi to Postscript, only ps and eps are allowed. The
246 default we use here encompasses both."
247 :group
'org-export-e-groff
248 :type
'(alist :key-type
(string :tag
"Type")
249 :value-type
(regexp :tag
"Path")))
251 (defcustom org-e-groff-link-with-unknown-path-format
"\\fI%s\\fP"
252 "Format string for links with unknown path type."
253 :group
'org-export-groff
258 (defcustom org-e-groff-tables-centered t
259 "When non-nil, tables are exported in a center environment."
260 :group
'org-export-e-groff
263 (defcustom org-e-groff-tables-verbatim nil
264 "When non-nil, tables are exported verbatim."
265 :group
'org-export-e-groff
268 (defcustom org-e-groff-table-scientific-notation
"%sE%s"
269 "Format string to display numbers in scientific notation.
270 The format should have \"%s\" twice, for mantissa and exponent
271 \(i.e. \"%s\\\\times10^{%s}\").
273 When nil, no transformation is made."
274 :group
'org-export-e-groff
276 (string :tag
"Format string")
277 (const :tag
"No formatting")))
281 (defcustom org-e-groff-text-markup-alist
282 '((bold .
"\\fB%s\\fP")
283 (code .
"\\fC%s\\fP")
284 (italic .
"\\fI%s\\fP")
285 (strike-through .
"\\fC%s\\fP") ; Strike through and underline
286 (underline .
"\\fI%s\\fP") ; need to be revised.
287 (verbatim .
"protectedtexttt"))
288 "Alist of Groff expressions to convert text markup.
290 The key must be a symbol among `bold', `code', `italic',
291 `strike-through', `underline' and `verbatim'. The value is
292 a formatting string to wrap fontified text with it.
294 If no association can be found for a given markup, text will be
296 :group
'org-export-e-groff
298 :options
'(bold code italic strike-through underline verbatim
))
302 (defcustom org-e-groff-format-drawer-function nil
303 "Function called to format a drawer in Groff code.
305 The function must accept two parameters:
306 NAME the drawer name, like \"LOGBOOK\"
307 CONTENTS the contents of the drawer.
309 The function should return the string to be exported.
311 For example, the variable could be set to the following function
312 in order to mimic default behaviour:
314 \(defun org-e-groff-format-drawer-default \(name contents\)
315 \"Format a drawer element for Groff export.\"
317 :group
'org-export-e-groff
322 (defcustom org-e-groff-format-inlinetask-function nil
323 "Function called to format an inlinetask in Groff code.
325 The function must accept six parameters:
326 TODO the todo keyword, as a string
327 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
328 PRIORITY the inlinetask priority, as a string
329 NAME the inlinetask name, as a string.
330 TAGS the inlinetask tags, as a list of strings.
331 CONTENTS the contents of the inlinetask, as a string.
333 The function should return the string to be exported.
335 For example, the variable could be set to the following function
336 in order to mimic default behaviour:
338 \(defun org-e-groff-format-inlinetask \(todo type priority name tags contents\)
339 \"Format an inline task element for Groff export.\"
343 \(format \"\\fB%s\\fP \" todo))
344 \(when priority (format \"[\\#%c] \" priority))
348 \(mapconcat 'identity tags \":\")))))
349 \(format (concat \".DS L\\n\"
353 full-title contents))"
354 :group
'org-export-e-groff
359 (defcustom org-e-groff-source-highlight nil
360 "Use GNU source highlight to embellish source blocks "
361 :group
'org-export-e-groff
364 (defcustom org-e-groff-source-highlight-langs
365 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
367 (c "c") (cc "cpp") (csharp "csharp") (d "d")
368 (fortran "fortran") (cobol "cobol") (pascal "pascal")
369 (ada "ada") (asm "asm")
370 (perl "perl") (cperl "perl")
371 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
372 (java "java") (javascript "javascript")
374 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
375 (ocaml "caml") (caml "caml")
376 (sql "sql") (sqlite "sql")
377 (html "html") (css "css") (xml "xml")
378 (bat "bat") (bison "bison") (clipper "clipper")
379 (ldap "ldap") (opa "opa")
380 (php "php") (postscript "postscript") (prolog "prolog")
381 (properties "properties") (makefile "makefile")
382 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
383 "Alist mapping languages to their listing language counterpart.
384 The key is a symbol, the major mode symbol without the \"-mode\".
385 The value is the string that should be inserted as the language
386 parameter for the listings package. If the mode name and the
387 listings name are the same, the language does not need an entry
388 in this list - but it does not hurt if it is present."
389 :group
'org-export-e-groff
392 (symbol :tag
"Major mode ")
393 (string :tag
"Listings language"))))
395 (defcustom org-e-groff-source-highlight-options nil
396 "Association list of options for the groff listings package.
398 These options are supplied as a comma-separated list to the
399 \\lstset command. Each element of the association list should be
400 a list containing two strings: the name of the option, and the
403 (setq org-e-groff-source-highlight-options
404 '((\"basicstyle\" \"\\small\")
405 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
407 will typeset the code in a small size font with underlined, bold
410 Note that the same options will be applied to blocks of all
412 :group
'org-export-e-groff
415 (string :tag
"Listings option name ")
416 (string :tag
"Listings option value"))))
418 (defvar org-e-groff-custom-lang-environments nil
419 "Alist mapping languages to language-specific Groff environments.
421 It is used during export of src blocks by the listings and
422 groff packages. For example,
424 \(setq org-e-groff-custom-lang-environments
425 '\(\(python \"pythoncode\"\)\)\)
427 would have the effect that if org encounters begin_src python
428 during groff export it will use pythoncode as the source-highlight
433 (defcustom org-e-groff-quotes
435 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"«~")
436 ("\\(\\S-\\)\"" .
"~»")
437 ("\\(\\s-\\|(\\|^\\)'" .
"'"))
439 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"``")
440 ("\\(\\S-\\)\"" .
"''")
441 ("\\(\\s-\\|(\\|^\\)'" .
"`")))
442 "Alist for quotes to use when converting english double-quotes.
444 The CAR of each item in this alist is the language code.
445 The CDR of each item in this alist is a list of three CONS:
446 - the first CONS defines the opening quote;
447 - the second CONS defines the closing quote;
448 - the last CONS defines single quotes.
450 For each item in a CONS, the first string is a regexp
451 for allowed characters before/after the quote, the second
452 string defines the replacement string for this quote."
453 :group
'org-export-e-groff
455 (cons :tag
"Opening quote"
456 (string :tag
"Regexp for char before")
457 (string :tag
"Replacement quote "))
458 (cons :tag
"Closing quote"
459 (string :tag
"Regexp for char after ")
460 (string :tag
"Replacement quote "))
461 (cons :tag
"Single quote"
462 (string :tag
"Regexp for char before")
463 (string :tag
"Replacement quote "))))
465 (defcustom org-e-groff-special-char
466 '(("(c)" .
"\\\\(co")
468 ("(rg)" .
"\\\\(rg"))
469 "CONS list in which the value of the car
470 is replace on the value of the CDR. "
471 :group
'org-export-e-groff
473 (cons :tag
"Character Subtitute"
474 (string :tag
"Original Character Group")
475 (string :tag
"Replacement Character"))))
479 (defcustom org-e-groff-pdf-process
480 '("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
481 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
482 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf")
484 "Commands to process a Groff file to a PDF file.
485 This is a list of strings, each of them will be given to the
486 shell as a command. %f in the command will be replaced by the
487 full file name, %b by the file base name \(i.e. without
488 extension) and %o by the base directory of the file."
489 :group
'org-export-pdf
491 (repeat :tag
"Shell command sequence"
492 (string :tag
"Shell command"))
493 (const :tag
"2 runs of pdfgroff"
494 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
495 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
496 (const :tag
"3 runs of pdfgroff"
497 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
498 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
499 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
502 (defcustom org-e-groff-logfiles-extensions
503 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
504 "The list of file extensions to consider as Groff logfiles."
505 :group
'org-export-e-groff
506 :type
'(repeat (string :tag
"Extension")))
508 (defcustom org-e-groff-remove-logfiles t
509 "Non-nil means remove the logfiles produced by PDF production.
510 These are the .aux, .log, .out, and .toc files."
511 :group
'org-export-e-groff
514 (defcustom org-e-groff-organization
"Org User"
515 "Name of the organization used to populate the .AF command."
516 :group
'org-export-e-groff
519 (defcustom org-e-groff-raster-to-ps nil
520 "Command used to convert raster to EPS. Nil for no conversion. Make sure that
521 `org-e-groff-inline-image-rules' is adjusted accordingly if not conversion is being
522 done. In this case, remove the entries for jpg and png in the file and fuzzy lists."
523 :group
'org-export-e-groff
525 (repeat :tag
"Shell Command Sequence" (string :tag
"Shell Command"))
526 (const :tag
"sam2p" "a=%s;b=%s;sam2p ${a} ${b} ;grep -v BeginData ${b} > b_${b};mv b_${b} ${b}" )
527 (const :tag
"NetPNM" "a=%s;b=%s;pngtopnm ${a} | pnmtops -noturn > ${b}" )
528 (const :tag
"None" nil
)))
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
(element info
)
538 "Return caption and label Groff string for ELEMENT.
540 INFO is a plist holding contextual information. If there's no
541 caption nor label, return the empty string.
543 For non-floats, see `org-e-groff--wrap-label'."
544 (let ((main (org-export-get-caption element
))
545 (short (org-export-get-caption element t
))
546 (label (org-element-property :name element
)))
547 (cond ((and (not main
) (not label
)) "")
548 ((not main
) (format "\\fI%s\\fP" label
))
549 ;; Option caption format with short name.
550 (short (format "%s\n.br\n - %s\n"
551 (org-export-data short info
)
552 (org-export-data main info
)))
553 ;; Standard caption format.
554 (t (format "\\fR%s\\fP" (org-export-data main info
))))))
556 (defun org-e-groff--quotation-marks (text info
)
557 "Export quotation marks depending on language conventions.
558 TEXT is a string containing quotation marks to be replaced. INFO
559 is a plist used as a communication channel."
562 (while (setq start
(string-match (car l
) text start
))
563 (let ((new-quote (concat (match-string 1 text
) (cdr l
))))
564 (setq text
(replace-match new-quote t t text
))))))
565 (cdr (or (assoc (plist-get info
:language
) org-e-groff-quotes
)
566 ;; Falls back on English.
567 (assoc "en" org-e-groff-quotes
))))
570 (defun org-e-groff--wrap-label (element output
)
571 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
572 This function shouldn't be used for floats. See
573 `org-e-groff--caption/label-string'."
574 (let ((label (org-element-property :name element
)))
575 (if (or (not output
) (not label
) (string= output
"") (string= label
""))
577 (concat (format "%s\n.br\n" label
) output
))))
579 (defun org-e-groff--text-markup (text markup
)
580 "Format TEXT depending on MARKUP text markup.
581 See `org-e-groff-text-markup-alist' for details."
582 (let ((fmt (cdr (assq markup org-e-groff-text-markup-alist
))))
584 ;; No format string: Return raw text.
586 ((string= "protectedtexttt" fmt
)
588 (trans '(("\\" .
"\\")))
591 (while (string-match "[\\{}$%&_#~^]" text
)
592 (setq char
(match-string 0 text
))
593 (if (> (match-beginning 0) 0)
594 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
595 (setq text
(substring text
(1+ (match-beginning 0))))
596 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
597 rtn
(concat rtn char
)))
598 (setq text
(concat rtn text
))
599 (format "\\fC%s\\fP" text
)))
600 ;; Else use format string.
601 (t (format fmt text
)))))
604 (defun org-e-groff--get-tagged-content (tag info
)
605 (cdr (assoc tag org-e-groff-special-content
)))
607 (defun org-e-groff--mt-head (title contents attr info
)
610 ;; 1. Insert Organization
611 (let ((firm-option (plist-get attr
:firm
)))
613 ((stringp firm-option
)
614 (format ".AF \"%s\" \n" firm-option
))
615 (t (format ".AF \"%s\" \n" (or org-e-groff-organization
"")))))
618 (let ((subtitle1 (plist-get attr
:subtitle1
))
619 (subtitle2 (plist-get attr
:subtitle2
)))
623 (format ".TL \"%s\" \"%s\" \n%s\n"
625 (or subtitle2
"") " "))
627 ((not (or subtitle1 subtitle2
))
631 (format ".TL \"%s\" \"%s \" \n%s\n"
633 (or subtitle2
"") title
))))
636 ;; In Groff, .AU *MUST* be placed after .TL
637 ;; If From, populate with data from From else
639 (let ((author (and (plist-get info
:with-author
)
640 (let ((auth (plist-get info
:author
)))
641 (and auth
(org-export-data auth info
)))))
642 (email (and (plist-get info
:with-email
)
643 (org-export-data (plist-get info
:email
) info
)))
644 (from-data (org-e-groff--get-tagged-content "FROM" info
))
646 (to-data (org-e-groff--get-tagged-content "TO" info
)))
649 ((and author from-data
)
653 (format " \"%s\" " from-line
))
656 (replace-regexp-in-string "\\.P\n" "" from-data
)) "\n") "")))
659 (format ".AU \"%s\" " author
) au-line
"\n")))
661 ((and author email
(not (string= "" email
)))
662 (format ".AU \"%s\" \"%s\"\n" author email
))
664 (author (format ".AU \"%s\"\n" author
))
669 ;; 4. Author Title, if present
670 (let ((at-item (plist-get attr
:author-title
)))
671 (if (and at-item
(stringp at-item
))
672 (format ".AT \"%s\" \n" at-item
)
676 (let ((date (org-export-data (plist-get info
:date
) info
)))
677 (and date
(format ".ND \"%s\"\n" date
)))
680 ;; If Abstract, then Populate Abstract
683 (let ((abstract-data (org-e-groff--get-tagged-content "ABSTRACT" info
))
684 (to-data (org-e-groff--get-tagged-content "TO" info
)))
687 (format ".AS\n%s\n.AE\n" abstract-data
))
689 (format ".AS\n%s\n.AE\n" to-data
))))))
691 (defun org-e-groff--letter-head (title contents attr info
)
692 (let ((author (and (plist-get info
:with-author
)
693 (let ((auth (plist-get info
:author
)))
694 (and auth
(org-export-data auth info
)))))
695 (email (and (plist-get info
:with-email
)
696 (org-export-data (plist-get info
:email
) info
)))
697 (from-data (org-e-groff--get-tagged-content "FROM" info
))
698 (at-item (plist-get attr
:author-title
))
699 (to-data (org-e-groff--get-tagged-content "TO" info
)))
702 ;; If FROM then get data from FROM
705 (replace-regexp-in-string "\\.P\n" "" from-data
))
710 (replace-regexp-in-string "\\.P\n" "" to-data
))
716 (format ".WA \"%s\" \"%s\" \n%s\n.WE\n" author
(or at-item
"") from-data
))
717 ((and author email
(not (string= "" email
)))
718 (format ".WA \"%s\"\n \"%s\"\n.WE\n" author email
))
719 (author (format ".WA \"%s\"\n.WE\n" author
))
720 (t ".WA \"\" \n.WE\n"))
722 ;; If TO then get data from TO
725 (format ".IA \n%s\n.IE\n" to-data
)))))
730 (defun org-e-groff-template (contents info
)
731 "Return complete document string after Groff conversion.
732 CONTENTS is the transcoded contents string. INFO is a plist
733 holding export options."
734 (let* ((title (org-export-data (plist-get info
:title
) info
))
739 (list (plist-get info
:groff-class-options
))
741 (class (plist-get info
:groff-class
))
742 (class-options (plist-get info
:groff-class-options
))
743 (classes (assoc class org-e-groff-classes
))
744 (classes-options (car (last classes
)))
745 (heading-option (plist-get classes-options
:heading
))
746 (type-option (plist-get classes-options
:type
))
747 (last-option (plist-get classes-options
:last-section
))
748 (hyphenate (plist-get attr
:hyphenate
))
749 (justify-right (plist-get attr
:justify-right
))
751 (document-class-string
753 (org-element-normalize-string
754 (let* ((header (nth 1 (assoc class org-e-groff-classes
)))
755 (document-class-item (if (stringp header
) header
"")))
756 document-class-item
)))))
775 ((string= type-option
"custom") "")
777 ((and (stringp document-class-string
)
778 (string= type-option
"cover"))
781 (format ".COVER %s\n" document-class-string
)
782 (org-e-groff--mt-head title contents attr info
)
785 ((string= type-option
"memo")
787 (org-e-groff--mt-head title contents attr info
)
788 document-class-string
))
789 ((string= type-option
"letter")
791 (org-e-groff--letter-head title contents attr info
)
792 (let ((sa-item (plist-get attr
:salutation
))
793 (cn-item (plist-get attr
:confidential
))
794 (sj-item (plist-get attr
:subject
))
795 (rn-item (plist-get attr
:reference
))
796 (at-item (plist-get attr
:attention
)))
800 (if (stringp sa-item
)
801 (format ".LO SA \"%s\" \n" sa-item
)
805 (if (stringp cn-item
)
806 (format ".LO CN \"%s\"\n" cn-item
)
809 (when (and at-item
(stringp at-item
))
810 (format ".LO AT \"%s\" \n" at-item
))
811 (when (and title rn-item
)
812 (format ".LO RN \"%s\"\n" title
))
814 (when (and sj-item
(stringp sj-item
))
815 (format ".LO SJ \"%s\" \n" sj-item
))
818 ".LT " document-class-string
"\n"))))
825 ((string= last-option
"toc")
827 ((string= last-option
"sign")
828 (let ((fc-item (plist-get attr
:closing
)))
829 (concat (if (stringp fc-item
)
830 (format ".FC \"%s\" \n" fc-item
)
838 (when (string= (car item
) "NS")
839 (replace-regexp-in-string
840 "\\.P\n" "" (cdr item
))))
841 (reverse org-e-groff-special-content
) "\n")))))
845 ;;; Transcode Functions
849 ;; Babel Calls are ignored.
854 (defun org-e-groff-bold (bold contents info
)
855 "Transcode BOLD from Org to Groff.
856 CONTENTS is the text with bold markup. INFO is a plist holding
857 contextual information."
858 (org-e-groff--text-markup contents
'bold
))
862 (defun org-e-groff-center-block (center-block contents info
)
863 "Transcode a CENTER-BLOCK element from Org to Groff.
864 CONTENTS holds the contents of the center block. INFO is a plist
865 holding contextual information."
866 (org-e-groff--wrap-label
868 (format ".DS C \n%s\n.DE" contents
)))
872 (defun org-e-groff-clock (clock contents info
)
873 "Transcode a CLOCK element from Org to Groff.
874 CONTENTS is nil. INFO is a plist holding contextual
877 (format "\\fB%s\\fP " org-clock-string
)
878 (format org-e-groff-inactive-timestamp-format
879 (concat (org-translate-time (org-element-property :value clock
))
880 (let ((time (org-element-property :time clock
)))
881 (and time
(format " (%s)" time
)))))))
885 (defun org-e-groff-code (code contents info
)
886 "Transcode a CODE object from Org to Groff.
887 CONTENTS is nil. INFO is a plist used as a communication
889 (org-e-groff--text-markup (org-element-property :value code
) 'code
))
891 ;;; Comments and Comment Blocks are ignored.
895 (defun org-e-groff-drawer (drawer contents info
)
896 "Transcode a DRAWER element from Org to Groff.
897 CONTENTS holds the contents of the block. INFO is a plist
898 holding contextual information."
899 (let* ((name (org-element-property :drawer-name drawer
))
900 (output (if (functionp org-e-groff-format-drawer-function
)
901 (funcall org-e-groff-format-drawer-function
903 ;; If there's no user defined function: simply
904 ;; display contents of the drawer.
906 (org-e-groff--wrap-label drawer output
)))
910 (defun org-e-groff-dynamic-block (dynamic-block contents info
)
911 "Transcode a DYNAMIC-BLOCK element from Org to Groff.
912 CONTENTS holds the contents of the block. INFO is a plist
913 holding contextual information. See `org-export-data'."
914 (org-e-groff--wrap-label dynamic-block contents
))
918 (defun org-e-groff-entity (entity contents info
)
919 "Transcode an ENTITY object from Org to Groff.
920 CONTENTS are the definition itself. INFO is a plist holding
921 contextual information."
922 (let ((ent (org-element-property :utf8 entity
))) ent
))
926 (defun org-e-groff-example-block (example-block contents info
)
927 "Transcode an EXAMPLE-BLOCK element from Org to Groff.
928 CONTENTS is nil. INFO is a plist holding contextual
930 (org-e-groff--wrap-label
932 (format ".DS L\n%s\n.DE"
933 (org-export-format-code-default example-block info
))))
937 (defun org-e-groff-export-block (export-block contents info
)
938 "Transcode a EXPORT-BLOCK element from Org to Groff.
939 CONTENTS is nil. INFO is a plist holding contextual information."
940 (when (string= (org-element-property :type export-block
) "GROFF")
941 (org-remove-indentation (org-element-property :value export-block
))))
945 (defun org-e-groff-export-snippet (export-snippet contents info
)
946 "Transcode a EXPORT-SNIPPET object from Org to Groff.
947 CONTENTS is nil. INFO is a plist holding contextual information."
948 (when (eq (org-export-snippet-backend export-snippet
) 'e-groff
)
949 (org-element-property :value export-snippet
)))
953 (defun org-e-groff-fixed-width (fixed-width contents info
)
954 "Transcode a FIXED-WIDTH element from Org to Groff.
955 CONTENTS is nil. INFO is a plist holding contextual information."
956 (org-e-groff--wrap-label
958 (format "\\fC\n%s\\fP"
959 (org-remove-indentation
960 (org-element-property :value fixed-width
)))))
962 ;;; Footnote Definition
964 ;; Footnote Definitions are ignored.
966 ;; Footnotes are handled automatically in GROFF. Although manual
967 ;; references can be added, not really required.
969 (defun org-e-groff-footnote-reference (footnote-reference contents info
)
970 ;; Changing from info to footnote-reference
971 (let* ((raw (org-export-get-footnote-definition footnote-reference info
))
972 (n (org-export-get-footnote-number footnote-reference info
))
973 (data (org-trim (org-export-data raw info
)))
974 (ref-id (plist-get (nth 1 footnote-reference
) :label
)))
976 (if (string-match "fn:rl" ref-id
)
977 (if (member ref-id org-e-groff-registered-references
)
978 (format "\\*[%s]" ref-id
)
980 (push ref-id org-e-groff-registered-references
)
981 (format "\\*(Rf\n.RS \"%s\" \n%s\n.RF\n" ref-id data
)))
982 ;; else it is a footnote
983 (format "\\u\\s-2%s\\d\\s+2\n.FS %s\n%s\n.FE\n" n n data
))))
987 (defun org-e-groff-headline (headline contents info
)
988 "Transcode an HEADLINE element from Org to Groff.
989 CONTENTS holds the contents of the headline. INFO is a plist
990 holding contextual information."
991 (let* ((class (plist-get info
:groff-class
))
992 (level (org-export-get-relative-level headline info
))
993 (numberedp (org-export-numbered-headline-p headline info
))
994 ;; Section formatting will set two placeholders: one for the
995 ;; title and the other for the contents.
996 (classes (assoc class org-e-groff-classes
))
997 (classes-options (car (last classes
)))
998 (heading-option (plist-get classes-options
:heading
))
1002 ((and (symbolp heading-option
)
1003 (fboundp heading-option
))
1004 (funcall heading-option level numberedp
))
1007 (concat ".H " (number-to-string level
) " \"%s\"\n%s")
1008 ".HU \"%s\"\n%s")))))
1009 ;; End of section-fmt
1010 (text (org-export-data (org-element-property :title headline
) info
))
1012 (and (plist-get info
:with-todo-keywords
)
1013 (let ((todo (org-element-property :todo-keyword headline
)))
1014 (and todo
(org-export-data todo info
)))))
1015 (todo-type (and todo
(org-element-property :todo-type headline
)))
1016 (tags (and (plist-get info
:with-tags
)
1017 (org-export-get-tags headline info
)))
1018 (priority (and (plist-get info
:with-priority
)
1019 (org-element-property :priority headline
)))
1020 ;; Create the headline text along with a no-tag version. The
1021 ;; latter is required to remove tags from table of contents.
1022 (full-text (if (functionp org-e-groff-format-headline-function
)
1023 ;; User-defined formatting function.
1024 (funcall org-e-groff-format-headline-function
1025 todo todo-type priority text tags
)
1026 ;; Default formatting.
1029 (format "\\fB%s\\fP " todo
))
1030 (when priority
(format " [\\#%c] " priority
))
1033 (format " \\fC:%s:\\fP "
1034 (mapconcat 'identity tags
":"))))))
1036 (if (functionp org-e-groff-format-headline-function
)
1037 ;; User-defined formatting function.
1038 (funcall org-e-groff-format-headline-function
1039 todo todo-type priority text nil
)
1040 ;; Default formatting.
1042 (when todo
(format "\\fB%s\\fP " todo
))
1043 (when priority
(format " [\\#%c] " priority
))
1045 ;; Associate some \label to the headline for internal links.
1047 ;; (format "\\label{sec-%s}\n"
1048 ;; (mapconcat 'number-to-string
1049 ;; (org-export-get-headline-number headline info)
1053 (make-string (org-element-property :pre-blank headline
) 10)))
1056 ;; Case 1: Special Tag
1057 ((member (car tags
) org-e-groff-special-tags
)
1059 ((string= (car tags
) "BODY") contents
)
1061 ((string= (car tags
) "NS")
1063 (push (cons (car tags
)
1064 (format ".NS \"%s\" 1 \n%s"
1065 (car (org-element-property :title headline
))
1067 org-e-groff-special-content
) nil
))
1071 (push (cons (car tags
) contents
) org-e-groff-special-content
)
1074 ;; Case 2: This is a footnote section: ignore it.
1075 ((org-element-property :footnote-section-p headline
) nil
)
1077 ;; Case 3: This is a deep sub-tree: export it as a list item.
1078 ;; Also export as items headlines for which no section
1079 ;; format has been found.
1080 ((or (not section-fmt
) (org-export-low-level-p headline info
))
1081 ;; Build the real contents of the sub-tree.
1082 (let ((low-level-body
1084 ;; If the headline is the first sibling, start a list.
1085 (when (org-export-first-sibling-p headline info
)
1086 (format "%s\n" (if numberedp
".AL 1\n" ".DL \n")))
1088 ".LI\n" full-text
"\n" headline-label pre-blanks contents
)))
1089 ;; If headline is not the last sibling simply return
1090 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1092 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1093 (replace-regexp-in-string
1098 ;; Case 4. Standard headline. Export it as a section.
1100 (format section-fmt full-text
1101 (concat headline-label pre-blanks contents
))))))
1106 ;;; Inline Babel Call
1108 ;; Inline Babel Calls are ignored.
1110 ;;; Inline Src Block
1112 (defun org-e-groff-inline-src-block (inline-src-block contents info
)
1113 "Transcode an INLINE-SRC-BLOCK element from Org to Groff.
1114 CONTENTS holds the contents of the item. INFO is a plist holding
1115 contextual information."
1116 (let* ((code (org-element-property :value inline-src-block
)))
1118 (org-e-groff-source-highlight
1119 (let* ((tmpdir (if (featurep 'xemacs
)
1121 temporary-file-directory
))
1122 (in-file (make-temp-name
1123 (expand-file-name "srchilite" tmpdir
)))
1124 (out-file (make-temp-name
1125 (expand-file-name "reshilite" tmpdir
)))
1126 (org-lang (org-element-property :language inline-src-block
))
1127 (lst-lang (cadr (assq (intern org-lang
)
1128 org-e-groff-source-highlight-langs
)))
1130 (cmd (concat (expand-file-name "source-highlight")
1132 " -f groff_mm_color "
1136 (let ((code-block ""))
1137 (with-temp-file in-file
(insert code
))
1139 (setq code-block
(org-file-contents out-file
))
1140 (delete-file in-file
)
1141 (delete-file out-file
)
1143 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1146 ;; Do not use a special package: transcode it verbatim.
1148 (concat ".DS I\n" "\\fC" code
"\\fP\n.DE\n")))))
1152 (defun org-e-groff-inlinetask (inlinetask contents info
)
1153 "Transcode an INLINETASK element from Org to Groff.
1154 CONTENTS holds the contents of the block. INFO is a plist
1155 holding contextual information."
1156 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1157 (todo (and (plist-get info
:with-todo-keywords
)
1158 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1159 (and todo
(org-export-data todo info
)))))
1160 (todo-type (org-element-property :todo-type inlinetask
))
1161 (tags (and (plist-get info
:with-tags
)
1162 (org-export-get-tags inlinetask info
)))
1163 (priority (and (plist-get info
:with-priority
)
1164 (org-element-property :priority inlinetask
))))
1165 ;; If `org-e-groff-format-inlinetask-function' is provided, call it
1166 ;; with appropriate arguments.
1167 (if (functionp org-e-groff-format-inlinetask-function
)
1168 (funcall org-e-groff-format-inlinetask-function
1169 todo todo-type priority title tags contents
)
1170 ;; Otherwise, use a default template.
1171 (org-e-groff--wrap-label
1175 (when todo
(format "\\fB%s\\fP " todo
))
1176 (when priority
(format " [\\#%c] " priority
))
1178 (when tags
(format " \\fC:%s:\\fP "
1179 (mapconcat 'identity tags
":"))))))
1180 (format (concat "\n.DS I\n"
1185 full-title contents
))))))
1189 (defun org-e-groff-italic (italic contents info
)
1190 "Transcode ITALIC from Org to Groff.
1191 CONTENTS is the text with italic markup. INFO is a plist holding
1192 contextual information."
1193 (org-e-groff--text-markup contents
'italic
))
1197 (defun org-e-groff-item (item contents info
)
1198 "Transcode an ITEM element from Org to Groff.
1199 CONTENTS holds the contents of the item. INFO is a plist holding
1200 contextual information."
1201 (let* ((bullet (org-element-property :bullet item
))
1202 (type (org-element-property
1203 :type
(org-element-property :parent item
)))
1204 (checkbox (case (org-element-property :checkbox item
)
1205 (on "\\o'\\(sq\\(mu'")
1207 (trans "\\o'\\(sq\\(mi'")))
1208 (tag (let ((tag (org-element-property :tag item
)))
1209 ;; Check-boxes must belong to the tag.
1210 (and tag
(format "%s"
1212 (org-export-data tag info
)))))))
1216 (concat ".LI ""\"" (or tag
(concat "\\ " checkbox
)) "\""
1218 (org-trim (or contents
" "))))
1222 (org-trim (or contents
" "))))
1224 (let* ((bullet (org-trim bullet
))
1225 (marker (cond ((string= "-" bullet
) "\\(em")
1226 ((string= "*" bullet
) "\\(bu")
1228 (concat ".LI " marker
"\n"
1229 (org-trim (or contents
" "))))))))
1233 (defun org-e-groff-keyword (keyword contents info
)
1234 "Transcode a KEYWORD element from Org to Groff.
1235 CONTENTS is nil. INFO is a plist holding contextual information."
1236 (let ((key (org-element-property :key keyword
))
1237 (value (org-element-property :value keyword
)))
1239 ((string= key
"GROFF") value
)
1242 ;;; LaTeX Environment
1244 (defun org-e-groff-latex-environment (latex-environment contents info
)
1245 "Transcode a LATEX-ENVIRONMENT element from Org to Groff.
1246 CONTENTS is nil. INFO is a plist holding contextual information."
1247 (let ((label (org-element-property :name latex-environment
))
1248 (value (org-remove-indentation
1249 (org-element-property :value latex-environment
))))
1250 (if (not (org-string-nw-p label
)) value
1251 ;; Environment is labelled: label must be within the environment
1252 ;; (otherwise, a reference pointing to that element will count
1253 ;; the section instead).
1256 (goto-char (point-min))
1258 (insert (format "%s\n" label
))
1263 (defun org-e-groff-latex-fragment (latex-fragment contents info
)
1264 "Transcode a LATEX-FRAGMENT object from Org to Groff.
1265 CONTENTS is nil. INFO is a plist holding contextual information."
1266 (org-element-property :value latex-fragment
))
1270 (defun org-e-groff-line-break (line-break contents info
)
1271 "Transcode a LINE-BREAK object from Org to Groff.
1272 CONTENTS is nil. INFO is a plist holding contextual information."
1276 ;; Inline images just place a call to .PSPIC or .PS/.PE
1277 ;; and load the graph.
1279 (defun org-e-groff-link--inline-image (link info
)
1280 "Return Groff code for an inline image.
1281 LINK is the link pointing to the inline image. INFO is a plist
1282 used as a communication channel."
1283 (let* ((parent (org-export-get-parent-element link
))
1284 (path (let ((raw-path (org-element-property :path link
)))
1285 (if (not (file-name-absolute-p raw-path
)) raw-path
1286 (expand-file-name raw-path
))))
1287 (attr (org-export-read-attribute :attr_groff link
))
1289 (case (plist-get attr
:position
)
1294 (width (or (plist-get attr
:width
) ""))
1295 (height (or (plist-get attr
:height
) ""))
1296 (caption (and (not (plist-get attr
:disable-caption
))
1297 (org-e-groff--caption/label-string parent info
))))
1298 ;; Now clear ATTR from any special keyword and set a default value
1299 ;; if nothing is left. Return proper string.
1302 ((and org-e-groff-raster-to-ps
1303 (or (string-match ".\.png$" path
)
1304 (string-match ".\.jpg$" path
)))
1305 (let ((eps-path (concat path
".eps")))
1306 (shell-command (format org-e-groff-raster-to-ps path eps-path
))
1307 (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1308 placement eps-path width height
)))
1309 ((string-match ".\.pic$" path
)
1310 (format "\n.PS\ncopy \"%s\"\n.PE" path
))
1311 (t (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1312 placement path width height
)))
1313 (and caption
(format "\n.FG \"%s\"" caption
)))))
1315 (defun org-e-groff-link (link desc info
)
1316 "Transcode a LINK object from Org to Groff.
1318 DESC is the description part of the link, or the empty string.
1319 INFO is a plist holding contextual information. See
1322 (let* ((type (org-element-property :type link
))
1323 (raw-path (org-element-property :path link
))
1324 ;; Ensure DESC really exists, or set it to nil.
1325 (desc (and (not (string= desc
"")) desc
))
1326 (imagep (org-export-inline-image-p
1327 link org-e-groff-inline-image-rules
))
1329 ((member type
'("http" "https" "ftp" "mailto"))
1330 (concat type
":" raw-path
))
1331 ((string= type
"file")
1332 (when (string-match "\\(.+\\)::.+" raw-path
)
1333 (setq raw-path
(match-string 1 raw-path
)))
1334 (if (file-name-absolute-p raw-path
)
1335 (concat "file://" (expand-file-name raw-path
))
1336 (concat "file://" raw-path
)))
1341 (imagep (org-e-groff-link--inline-image link info
))
1342 ;; import groff files
1343 ((and (string= type
"file")
1344 (string-match ".\.groff$" raw-path
))
1345 (concat ".so " raw-path
"\n"))
1346 ;; Radio link: transcode target's contents and use them as link's
1348 ((string= type
"radio")
1349 (let ((destination (org-export-resolve-radio-link link info
)))
1351 (format "\\fI [%s] \\fP"
1352 (org-export-solidify-link-text path
)))))
1354 ;; Links pointing to an headline: find destination and build
1355 ;; appropriate referencing command.
1356 ((member type
'("custom-id" "fuzzy" "id"))
1357 (let ((destination (if (string= type
"fuzzy")
1358 (org-export-resolve-fuzzy-link link info
)
1359 (org-export-resolve-id-link link info
))))
1360 (case (org-element-type destination
)
1361 ;; Id link points to an external file.
1363 (if desc
(format "%s \\fBat\\fP \\fIfile://%s\\fP" desc destination
)
1364 (format "\\fI file://%s \\fP" destination
)))
1365 ;; Fuzzy link points nowhere.
1367 (format org-e-groff-link-with-unknown-path-format
1370 (org-element-property :raw-link link
) info
))))
1371 ;; Fuzzy link points to an invisible target.
1373 ;; LINK points to an headline. If headlines are numbered
1374 ;; and the link has no description, display headline's
1375 ;; number. Otherwise, display description or headline's
1379 (if (and (plist-get info
:section-numbers
) (not desc
))
1380 (format "\\fI%s\\fP" label
)
1381 (format "\\fI%s\\fP"
1384 (org-element-property :title destination
) info
))))))
1385 ;; Fuzzy link points to a target. Do as above.
1387 (let ((path (org-export-solidify-link-text path
)))
1388 (if (not desc
) (format "\\fI%s\\fP" path
)
1389 (format "%s \\fBat\\fP \\fI%s\\fP" desc path
)))))))
1390 ;; External link with a description part.
1391 ((and path desc
) (format "%s \\fBat\\fP \\fI%s\\fP" path desc
))
1392 ;; External link without a description part.
1393 (path (format "\\fI%s\\fP" path
))
1394 ;; No path, only description. Try to do something useful.
1395 (t (format org-e-groff-link-with-unknown-path-format desc
)))))
1399 (defun org-e-groff-paragraph (paragraph contents info
)
1400 "Transcode a PARAGRAPH element from Org to Groff.
1401 CONTENTS is the contents of the paragraph, as a string. INFO is
1402 the plist used as a communication channel."
1403 (let ((parent (plist-get (nth 1 paragraph
) :parent
)))
1405 (let* ((parent-type (car parent
))
1406 (fixed-paragraph "")
1407 (class (plist-get info
:groff-class
))
1408 (class-options (plist-get info
:groff-class-options
))
1409 (classes (assoc class org-e-groff-classes
))
1410 (classes-options (car (last classes
)))
1411 (paragraph-option (plist-get classes-options
:paragraph
)))
1413 ((and (symbolp paragraph-option
)
1414 (fboundp paragraph-option
))
1415 (funcall paragraph-option parent-type parent contents
))
1416 ((and (eq parent-type
'item
)
1417 (plist-get (nth 1 parent
) :bullet
))
1418 (setq fixed-paragraph
(concat "" contents
)))
1419 ((eq parent-type
'section
)
1420 (setq fixed-paragraph
(concat ".P\n" contents
)))
1421 ((eq parent-type
'footnote-definition
)
1422 (setq fixed-paragraph
(concat "" contents
)))
1423 (t (setq fixed-paragraph
(concat "" contents
))))
1428 (defun org-e-groff-plain-list (plain-list contents info
)
1429 "Transcode a PLAIN-LIST element from Org to Groff.
1430 CONTENTS is the contents of the list. INFO is a plist holding
1431 contextual information."
1432 (let* ((type (org-element-property :type plain-list
))
1433 (attr (mapconcat #'identity
1434 (org-element-property :attr_groff plain-list
)
1437 ((eq type
'ordered
) ".AL")
1438 ((eq type
'unordered
) ".BL")
1439 ((eq type
'descriptive
) ".VL 2.0i"))))
1440 (org-e-groff--wrap-label
1442 (format "%s\n%s\n.LE" groff-type contents
))))
1446 (defun org-e-groff-plain-text (text info
)
1447 "Transcode a TEXT string from Org to Groff.
1448 TEXT is the string to transcode. INFO is a plist holding
1449 contextual information."
1451 (setq text
(replace-regexp-in-string
1452 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1453 "$\\" text nil t
1))
1454 ;; Handle quotation marks
1455 (setq text
(org-e-groff--quotation-marks text info
))
1456 ;; Handle Special Characters
1457 (if org-e-groff-special-char
1458 (dolist (special-char-list org-e-groff-special-char
)
1460 (replace-regexp-in-string (car special-char-list
)
1461 (cdr special-char-list
) text
))))
1462 ;; Handle break preservation if required.
1463 (when (plist-get info
:preserve-breaks
)
1464 (setq text
(replace-regexp-in-string
1465 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" text
)))
1471 (defun org-e-groff-planning (planning contents info
)
1472 "Transcode a PLANNING element from Org to Groff.
1473 CONTENTS is nil. INFO is a plist holding contextual
1480 (let ((closed (org-element-property :closed planning
)))
1483 (format "\\fR %s \\fP" org-closed-string
)
1484 (format org-e-groff-inactive-timestamp-format
1485 (org-translate-time closed
)))))
1486 (let ((deadline (org-element-property :deadline planning
)))
1489 (format "\\fB %s \\fP" org-deadline-string
)
1490 (format org-e-groff-active-timestamp-format
1491 (org-translate-time deadline
)))))
1492 (let ((scheduled (org-element-property :scheduled planning
)))
1495 (format "\\fR %s \\fP" org-scheduled-string
)
1496 (format org-e-groff-active-timestamp-format
1497 (org-translate-time scheduled
)))))))
1503 (defun org-e-groff-property-drawer (property-drawer contents info
)
1504 "Transcode a PROPERTY-DRAWER element from Org to Groff.
1505 CONTENTS is nil. INFO is a plist holding contextual
1507 ;; The property drawer isn't exported but we want separating blank
1508 ;; lines nonetheless.
1513 (defun org-e-groff-quote-block (quote-block contents info
)
1514 "Transcode a QUOTE-BLOCK element from Org to Groff.
1515 CONTENTS holds the contents of the block. INFO is a plist
1516 holding contextual information."
1517 (org-e-groff--wrap-label
1519 (format ".DS I\n.I\n%s\n.R\n.DE" contents
)))
1523 (defun org-e-groff-quote-section (quote-section contents info
)
1524 "Transcode a QUOTE-SECTION element from Org to Groff.
1525 CONTENTS is nil. INFO is a plist holding contextual information."
1526 (let ((value (org-remove-indentation
1527 (org-element-property :value quote-section
))))
1528 (when value
(format ".DS L\n\\fI%s\\fP\n.DE\n" value
))))
1532 (defun org-e-groff-radio-target (radio-target text info
)
1533 "Transcode a RADIO-TARGET object from Org to Groff.
1534 TEXT is the text of the target. INFO is a plist holding
1535 contextual information."
1537 (org-export-solidify-link-text
1538 (org-element-property :value radio-target
))
1543 (defun org-e-groff-section (section contents info
)
1544 "Transcode a SECTION element from Org to Groff.
1545 CONTENTS holds the contents of the section. INFO is a plist
1546 holding contextual information."
1551 (defun org-e-groff-special-block (special-block contents info
)
1552 "Transcode a SPECIAL-BLOCK element from Org to Groff.
1553 CONTENTS holds the contents of the block. INFO is a plist
1554 holding contextual information."
1555 (let ((type (downcase (org-element-property :type special-block
))))
1556 (org-e-groff--wrap-label
1558 (format "%s\n" contents
))))
1562 (defun org-e-groff-src-block (src-block contents info
)
1563 "Transcode a SRC-BLOCK element from Org to Groff.
1564 CONTENTS holds the contents of the item. INFO is a plist holding
1565 contextual information."
1566 (let* ((lang (org-element-property :language src-block
))
1567 (label (org-element-property :name src-block
))
1568 (code (org-element-property :value src-block
))
1569 (custom-env (and lang
1570 (cadr (assq (intern lang
)
1571 org-e-groff-custom-lang-environments
))))
1572 (num-start (case (org-element-property :number-lines src-block
)
1573 (continued (org-export-get-loc src-block info
))
1575 (retain-labels (org-element-property :retain-labels src-block
))
1576 (caption (and (not (org-export-read-attribute
1577 :attr_groff src-block
:disable-caption
))
1578 (org-e-groff--caption/label-string src-block info
))))
1581 ;; Case 1. No source fontification.
1582 ((not org-e-groff-source-highlight
)
1584 (format ".DS I\n\\fC%s\\fP\n.DE\n"
1585 (org-export-format-code-default src-block info
))
1586 (and caption
(format ".EX \"%s\" " caption
))))
1588 ;; Case 2. Source fontification.
1589 (org-e-groff-source-highlight
1590 (let* ((tmpdir (if (featurep 'xemacs
)
1592 temporary-file-directory
))
1593 (in-file (make-temp-name
1594 (expand-file-name "srchilite" tmpdir
)))
1595 (out-file (make-temp-name
1596 (expand-file-name "reshilite" tmpdir
)))
1598 (org-lang (org-element-property :language src-block
))
1599 (lst-lang (cadr (assq (intern org-lang
)
1600 org-e-groff-source-highlight-langs
)))
1602 (cmd (concat "source-highlight"
1604 " -f groff_mm_color "
1610 (let ((code-block ""))
1611 (with-temp-file in-file
(insert code
))
1613 (setq code-block
(org-file-contents out-file
))
1614 (delete-file in-file
)
1615 (delete-file out-file
)
1616 (format "%s\n" code-block
))
1617 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1619 (and caption
(format ".EX \"%s\" " caption
))))))))
1622 ;;; Statistics Cookie
1624 (defun org-e-groff-statistics-cookie (statistics-cookie contents info
)
1625 "Transcode a STATISTICS-COOKIE object from Org to Groff.
1626 CONTENTS is nil. INFO is a plist holding contextual information."
1627 (org-element-property :value statistics-cookie
))
1632 (defun org-e-groff-strike-through (strike-through contents info
)
1633 "Transcode STRIKE-THROUGH from Org to Groff.
1634 CONTENTS is the text with strike-through markup. INFO is a plist
1635 holding contextual information."
1636 (org-e-groff--text-markup contents
'strike-through
))
1640 (defun org-e-groff-subscript (subscript contents info
)
1641 "Transcode a SUBSCRIPT object from Org to Groff.
1642 CONTENTS is the contents of the object. INFO is a plist holding
1643 contextual information."
1644 (format "\\d\\s-2%s\\s+2\\u" contents
))
1646 ;;; Superscript "^_%s$
1648 (defun org-e-groff-superscript (superscript contents info
)
1649 "Transcode a SUPERSCRIPT object from Org to Groff.
1650 CONTENTS is the contents of the object. INFO is a plist holding
1651 contextual information."
1652 (format "\\u\\s-2%s\\s+2\\d" contents
))
1657 ;; `org-e-groff-table' is the entry point for table transcoding. It
1658 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1659 ;; delegates the job to `org-e-groff-table--org-table' function,
1660 ;; depending of the type of the table.
1662 ;; `org-e-groff-table--align-string' is a subroutine used to build
1663 ;; alignment string for Org tables.
1665 (defun org-e-groff-table (table contents info
)
1666 "Transcode a TABLE element from Org to Groff.
1667 CONTENTS is the contents of the table. INFO is a plist holding
1668 contextual information."
1670 ;; Case 1: verbatim table.
1671 ((or org-e-groff-tables-verbatim
1672 (let ((attr (read (format "(%s)"
1675 (org-element-property :attr_groff table
) " ")))))
1676 (and attr
(plist-get attr
:verbatim
))))
1678 (format ".DS L\n\\fC%s\\fP\n.DE"
1679 ;; Re-create table, without affiliated keywords.
1681 (org-element-interpret-data
1682 `(table nil
,@(org-element-contents table
))))))
1684 ;; Case 2: Standard table.
1685 (t (org-e-groff-table--org-table table contents info
))))
1687 (defun org-e-groff-table--align-string (divider table info
)
1688 "Return an appropriate Groff alignment string.
1689 TABLE is the considered table. INFO is a plist used as
1690 a communication channel."
1692 ;; Extract column groups and alignment from first (non-rule)
1698 (and (eq (org-element-property :type row
) 'standard
) row
))
1702 (let* ((borders (org-export-table-cell-borders cell info
))
1703 (raw-width (org-export-table-cell-width cell info
))
1704 (width-cm (when raw-width
(/ raw-width
5)))
1705 (width (if raw-width
(format "w(%dc)"
1706 (if (< width-cm
1) 1 width-cm
)) "")))
1707 ;; Check left border for the first cell only.
1708 ;; Alignment is nil on assignment
1710 (when (and (memq 'left borders
) (not alignment
))
1711 (push "|" alignment
))
1713 (case (org-export-table-cell-alignment cell info
)
1714 (left (concat "l" width divider
))
1715 (right (concat "r" width divider
))
1716 (center (concat "c" width divider
)))
1718 (when (memq 'right borders
) (push "|" alignment
))))
1720 (apply 'concat
(reverse alignment
))))
1722 (defun org-e-groff-table--org-table (table contents info
)
1723 "Return appropriate Groff code for an Org table.
1725 TABLE is the table type element to transcode. CONTENTS is its
1726 contents, as a string. INFO is a plist used as a communication
1729 This function assumes TABLE has `org' as its `:type' attribute."
1730 (let* ((attr (org-export-read-attribute :attr_groff table
))
1731 (label (org-element-property :name table
))
1732 (caption (and (not (plist-get attr
:disable-caption
))
1733 (org-e-groff--caption/label-string table info
)))
1734 (divider (if (plist-get attr
:divider
) "|" " "))
1736 ;; Determine alignment string.
1737 (alignment (org-e-groff-table--align-string divider table info
))
1739 ;; Extract others display options.
1741 (lines (org-split-string contents
"\n"))
1747 (if (plist-get attr
:expand
)
1750 (case (plist-get attr
:placement
)
1754 (if org-e-groff-tables-centered
1757 (case (plist-get attr
:boxtype
)
1759 ('doublebox
"doublebox")
1764 (if (not (null attr-item
))
1765 (add-to-list 'result-list attr-item
)))
1768 (title-line (plist-get attr
:title-line
))
1769 (long-cells (plist-get attr
:long-cells
))
1774 (or (car attr-list
) ""))
1777 (when (cdr attr-list
)
1778 (dolist (attr-item (cdr attr-list
))
1779 (setq output-list
(concat output-list
1780 (format ",%s" attr-item
)))))
1783 (when lines
(org-split-string (car lines
) "\t"))))
1784 ;; Prepare the final format string for the table.
1790 (concat ".TS\n " table-format
";\n"
1792 (let ((final-line ""))
1794 (dotimes (i (length first-line
))
1795 (setq final-line
(concat final-line
"cb" divider
))))
1797 (setq final-line
(concat final-line
"\n"))
1800 (setq final-line
(concat final-line alignment
))
1801 (dotimes (i (length first-line
))
1802 (setq final-line
(concat final-line
"c" divider
))))
1806 (let ((final-line "")
1808 (lines (org-split-string contents
"\n")))
1810 (dolist (line-item lines
)
1815 (if (string= line-item
"_")
1816 (setq long-line
(format "%s\n" line-item
))
1818 (let ((cell-item-list (org-split-string line-item
"\t")))
1819 (dolist (cell-item cell-item-list
)
1821 (cond ((eq cell-item
(car (last cell-item-list
)))
1822 (setq long-line
(concat long-line
1823 (format "T{\n%s\nT}\t\n" cell-item
))))
1825 (setq long-line
(concat long-line
1826 (format "T{\n%s\nT}\t" cell-item
))))))
1829 (setq final-line
(concat final-line long-line
)))
1831 (setq final-line
(concat final-line line-item
"\n"))))
1834 (if caption
(format ".TB \"%s\"" caption
) ""))))))
1838 (defun org-e-groff-table-cell (table-cell contents info
)
1839 "Transcode a TABLE-CELL element from Org to Groff
1840 CONTENTS is the cell contents. INFO is a plist used as
1841 a communication channel."
1843 (concat (if (and contents
1844 org-e-groff-table-scientific-notation
1845 (string-match orgtbl-exp-regexp contents
))
1846 ;; Use appropriate format string for scientific
1848 (format org-e-groff-table-scientific-notation
1849 (match-string 1 contents
)
1850 (match-string 2 contents
))
1852 (when (org-export-get-next-element table-cell info
) "\t"))))
1857 (defun org-e-groff-table-row (table-row contents info
)
1858 "Transcode a TABLE-ROW element from Org to Groff
1859 CONTENTS is the contents of the row. INFO is a plist used as
1860 a communication channel."
1861 ;; Rules are ignored since table separators are deduced from
1862 ;; borders of the current row.
1863 (when (eq (org-element-property :type table-row
) 'standard
)
1864 (let* ((attr (mapconcat 'identity
1865 (org-element-property
1866 :attr_groff
(org-export-get-parent table-row
))
1868 ;; TABLE-ROW's borders are extracted from its first cell.
1870 (org-export-table-cell-borders
1871 (car (org-element-contents table-row
)) info
)))
1873 ;; Mark horizontal lines
1874 (cond ((and (memq 'top borders
) (memq 'above borders
)) "_\n"))
1877 ;; When BOOKTABS are activated enforce bottom rule even when
1878 ;; no hline was specifically marked.
1879 ((and (memq 'bottom borders
) (memq 'below borders
)) "\n_")
1880 ((memq 'below borders
) "\n_"))))))
1884 (defun org-e-groff-target (target contents info
)
1885 "Transcode a TARGET object from Org to Groff.
1886 CONTENTS is nil. INFO is a plist holding contextual
1888 (format "\\fI%s\\fP"
1889 (org-export-solidify-link-text (org-element-property :value target
))))
1893 (defun org-e-groff-timestamp (timestamp contents info
)
1894 "Transcode a TIMESTAMP object from Org to Groff.
1895 CONTENTS is nil. INFO is a plist holding contextual
1897 (let ((value (org-translate-time (org-element-property :value timestamp
)))
1898 (type (org-element-property :type timestamp
)))
1899 (cond ((memq type
'(active active-range
))
1900 (format org-e-groff-active-timestamp-format value
))
1901 ((memq type
'(inactive inactive-range
))
1902 (format org-e-groff-inactive-timestamp-format value
))
1903 (t (format org-e-groff-diary-timestamp-format value
)))))
1907 (defun org-e-groff-underline (underline contents info
)
1908 "Transcode UNDERLINE from Org to Groff.
1909 CONTENTS is the text with underline markup. INFO is a plist
1910 holding contextual information."
1911 (org-e-groff--text-markup contents
'underline
))
1915 (defun org-e-groff-verbatim (verbatim contents info
)
1916 "Transcode a VERBATIM object from Org to Groff.
1917 CONTENTS is nil. INFO is a plist used as a communication
1919 (org-e-groff--text-markup (org-element-property :value verbatim
) 'verbatim
))
1923 (defun org-e-groff-verse-block (verse-block contents info
)
1924 "Transcode a VERSE-BLOCK element from Org to Groff.
1925 CONTENTS is verse block contents. INFO is a plist holding
1926 contextual information."
1927 (format ".DS C\n.ft HI\n%s\n.ft\n.DE" contents
))
1930 ;;; Interactive functions
1932 (defun org-e-groff-export-to-groff
1933 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1934 "Export current buffer to a Groff file.
1936 If narrowing is active in the current buffer, only export its
1939 If a region is active, export that region.
1941 When optional argument SUBTREEP is non-nil, export the sub-tree
1942 at point, extracting information from the headline properties
1945 When optional argument VISIBLE-ONLY is non-nil, don't export
1946 contents of hidden elements.
1948 EXT-PLIST, when provided, is a property list with external
1949 parameters overriding Org default settings, but still inferior to
1950 file-local settings.
1952 When optional argument PUB-DIR is set, use it as the publishing
1955 Return output file's name."
1957 (setq org-e-groff-registered-references nil
)
1958 (setq org-e-groff-special-content nil
)
1959 (let ((outfile (org-export-output-file-name ".groff" subtreep pub-dir
)))
1961 'e-groff outfile subtreep visible-only body-only ext-plist
)))
1963 (defun org-e-groff-export-to-pdf
1964 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1965 "Export current buffer to Groff then process through to PDF.
1967 If narrowing is active in the current buffer, only export its
1970 If a region is active, export that region.
1972 When optional argument SUBTREEP is non-nil, export the sub-tree
1973 at point, extracting information from the headline properties
1976 When optional argument VISIBLE-ONLY is non-nil, don't export
1977 contents of hidden elements.
1979 EXT-PLIST, when provided, is a property list with external
1980 parameters overriding Org default settings, but still inferior to
1981 file-local settings.
1983 When optional argument PUB-DIR is set, use it as the publishing
1986 Return PDF file's name."
1988 (org-e-groff-compile
1989 (org-e-groff-export-to-groff
1990 subtreep visible-only body-only ext-plist pub-dir
)))
1992 (defun org-e-groff-compile (grofffile)
1993 "Compile a Groff file.
1995 GROFFFILE is the name of the file being compiled. Processing is
1996 done through the command specified in `org-e-groff-pdf-process'.
1998 Return PDF file name or an error if it couldn't be produced."
1999 (let* ((wconfig (current-window-configuration))
2000 (grofffile (file-truename grofffile
))
2001 (base (file-name-sans-extension grofffile
))
2003 (message (format "Processing Groff file %s ..." grofffile
))
2007 ;; A function is provided: Apply it.
2008 ((functionp org-e-groff-pdf-process
)
2009 (funcall org-e-groff-pdf-process
(shell-quote-argument grofffile
)))
2010 ;; A list is provided: Replace %b, %f and %o with appropriate
2011 ;; values in each command before applying it. Output is
2012 ;; redirected to "*Org PDF Groff Output*" buffer.
2013 ((consp org-e-groff-pdf-process
)
2014 (let* ((out-dir (or (file-name-directory grofffile
) "./"))
2015 (outbuf (get-buffer-create "*Org PDF Groff Output*")))
2019 (replace-regexp-in-string
2020 "%b" (shell-quote-argument base
)
2021 (replace-regexp-in-string
2022 "%f" (shell-quote-argument grofffile
)
2023 (replace-regexp-in-string
2024 "%o" (shell-quote-argument out-dir
) command t t
)
2027 org-e-groff-pdf-process
)
2028 ;; Collect standard errors from output buffer.
2029 (setq errors
(org-e-groff-collect-errors outbuf
))))
2030 (t (error "No valid command to process to PDF")))
2031 (let ((pdffile (concat base
".pdf")))
2032 ;; Check for process failure. Provide collected errors if
2034 (if (not (file-exists-p pdffile
))
2035 (error (concat (format "PDF file %s wasn't produced" pdffile
)
2036 (when errors
(concat ": " errors
))))
2037 ;; Else remove log files, when specified, and signal end of
2038 ;; process to user, along with any error encountered.
2039 (when org-e-groff-remove-logfiles
2040 (dolist (ext org-e-groff-logfiles-extensions
)
2041 (let ((file (concat base
"." ext
)))
2042 (when (file-exists-p file
) (delete-file file
)))))
2043 (message (concat "Process completed"
2044 (if (not errors
) "."
2045 (concat " with errors: " errors
)))))
2046 ;; Return output file name.
2048 (set-window-configuration wconfig
))))
2050 (defun org-e-groff-collect-errors (buffer)
2051 "Collect some kind of errors from \"groff\" output
2052 BUFFER is the buffer containing output.
2053 Return collected error types as a string, or nil if there was
2055 (with-current-buffer buffer
2057 (goto-char (point-max))
2062 (provide 'org-e-groff
)
2063 ;;; org-e-groff.el ends here