org-e-man.el 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. ;; org-e-man.el --- Man Back-End For Org Export Engine
  2. ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Author: Luis R Anaya <papoanaya aroba hot mail punto com>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;;
  7. ;; This program is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; This program is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;
  19. ;; This library implements a Man back-end for Org generic exporter.
  20. ;;
  21. ;; To test it, run
  22. ;;
  23. ;; M-: (org-export-to-buffer 'e-man "*Test e-Man*") RET
  24. ;;
  25. ;; in an org-mode buffer then switch to the buffer to see the Man
  26. ;; export. See contrib/lisp/org-export.el for more details on how
  27. ;; this exporter works.
  28. ;;
  29. ;; It introduces one new buffer keywords:
  30. ;; "MAN_CLASS_OPTIONS".
  31. ;;; Code:
  32. (require 'org-export)
  33. (eval-when-compile (require 'cl))
  34. (defvar org-export-man-default-packages-alist)
  35. (defvar org-export-man-packages-alist)
  36. ;;; Define Back-End
  37. (org-export-define-backend e-man
  38. ((babel-call . org-e-man-babel-call)
  39. (bold . org-e-man-bold)
  40. (center-block . org-e-man-center-block)
  41. (clock . org-e-man-clock)
  42. (code . org-e-man-code)
  43. (comment . org-e-man-comment)
  44. (comment-block . org-e-man-comment-block)
  45. (drawer . org-e-man-drawer)
  46. (dynamic-block . org-e-man-dynamic-block)
  47. (entity . org-e-man-entity)
  48. (example-block . org-e-man-example-block)
  49. (export-block . org-e-man-export-block)
  50. (export-snippet . org-e-man-export-snippet)
  51. (fixed-width . org-e-man-fixed-width)
  52. (footnote-definition . org-e-man-footnote-definition)
  53. (footnote-reference . org-e-man-footnote-reference)
  54. (headline . org-e-man-headline)
  55. (horizontal-rule . org-e-man-horizontal-rule)
  56. (inline-babel-call . org-e-man-inline-babel-call)
  57. (inline-src-block . org-e-man-inline-src-block)
  58. (inlinetask . org-e-man-inlinetask)
  59. (italic . org-e-man-italic)
  60. (item . org-e-man-item)
  61. (keyword . org-e-man-keyword)
  62. (man-environment . org-e-man-man-environment)
  63. (man-fragment . org-e-man-man-fragment)
  64. (line-break . org-e-man-line-break)
  65. (link . org-e-man-link)
  66. (paragraph . org-e-man-paragraph)
  67. (plain-list . org-e-man-plain-list)
  68. (plain-text . org-e-man-plain-text)
  69. (planning . org-e-man-planning)
  70. (property-drawer . org-e-man-property-drawer)
  71. (quote-block . org-e-man-quote-block)
  72. (quote-section . org-e-man-quote-section)
  73. (radio-target . org-e-man-radio-target)
  74. (section . org-e-man-section)
  75. (special-block . org-e-man-special-block)
  76. (src-block . org-e-man-src-block)
  77. (statistics-cookie . org-e-man-statistics-cookie)
  78. (strike-through . org-e-man-strike-through)
  79. (subscript . org-e-man-subscript)
  80. (superscript . org-e-man-superscript)
  81. (table . org-e-man-table)
  82. (table-cell . org-e-man-table-cell)
  83. (table-row . org-e-man-table-row)
  84. (target . org-e-man-target)
  85. (template . org-e-man-template)
  86. (timestamp . org-e-man-timestamp)
  87. (underline . org-e-man-underline)
  88. (verbatim . org-e-man-verbatim)
  89. (verse-block . org-e-man-verse-block))
  90. :export-block "MAN"
  91. :menu-entry
  92. (?m "Export to MAN"
  93. ((?m "As MAN file" org-e-man-export-to-man)
  94. (?p "As PDF file" org-e-man-export-to-pdf)
  95. (?o "As PDF file and open"
  96. (lambda (s v b) (org-open-file (org-e-man-export-to-pdf s v b))))))
  97. :options-alist
  98. ((:date "DATE" nil nil t)
  99. (:man-class "MAN_CLASS" nil nil t)
  100. (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
  101. (:man-header-extra "MAN_HEADER" nil nil newline)))
  102. ;;; User Configurable Variables
  103. (defgroup org-export-e-man nil
  104. "Options for exporting Org mode files to Man."
  105. :tag "Org Export Man"
  106. :group 'org-export)
  107. ;;; Tables
  108. (defcustom org-e-man-tables-centered t
  109. "When non-nil, tables are exported in a center environment."
  110. :group 'org-export-e-man
  111. :type 'boolean)
  112. (defcustom org-e-man-tables-verbatim nil
  113. "When non-nil, tables are exported verbatim."
  114. :group 'org-export-e-man
  115. :type 'boolean)
  116. (defcustom org-e-man-table-scientific-notation "%sE%s"
  117. "Format string to display numbers in scientific notation.
  118. The format should have \"%s\" twice, for mantissa and exponent
  119. \(i.e. \"%s\\\\times10^{%s}\").
  120. When nil, no transformation is made."
  121. :group 'org-export-e-man
  122. :type '(choice
  123. (string :tag "Format string")
  124. (const :tag "No formatting")))
  125. ;;; Inlinetasks
  126. ;; Src blocks
  127. (defcustom org-e-man-source-highlight nil
  128. "Use GNU source highlight to embellish source blocks "
  129. :group 'org-export-e-man
  130. :type 'boolean)
  131. (defcustom org-e-man-source-highlight-langs
  132. '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
  133. (scheme "scheme")
  134. (c "c") (cc "cpp") (csharp "csharp") (d "d")
  135. (fortran "fortran") (cobol "cobol") (pascal "pascal")
  136. (ada "ada") (asm "asm")
  137. (perl "perl") (cperl "perl")
  138. (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
  139. (java "java") (javascript "javascript")
  140. (tex "latex")
  141. (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
  142. (ocaml "caml") (caml "caml")
  143. (sql "sql") (sqlite "sql")
  144. (html "html") (css "css") (xml "xml")
  145. (bat "bat") (bison "bison") (clipper "clipper")
  146. (ldap "ldap") (opa "opa")
  147. (php "php") (postscript "postscript") (prolog "prolog")
  148. (properties "properties") (makefile "makefile")
  149. (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
  150. "Alist mapping languages to their listing language counterpart.
  151. The key is a symbol, the major mode symbol without the \"-mode\".
  152. The value is the string that should be inserted as the language
  153. parameter for the listings package. If the mode name and the
  154. listings name are the same, the language does not need an entry
  155. in this list - but it does not hurt if it is present."
  156. :group 'org-export-e-man
  157. :type '(repeat
  158. (list
  159. (symbol :tag "Major mode ")
  160. (string :tag "Listings language"))))
  161. (defvar org-e-man-custom-lang-environments nil
  162. "Alist mapping languages to language-specific Man environments.
  163. It is used during export of src blocks by the listings and
  164. man packages. For example,
  165. \(setq org-e-man-custom-lang-environments
  166. '\(\(python \"pythoncode\"\)\)\)
  167. would have the effect that if org encounters begin_src python
  168. during man export."
  169. )
  170. ;;; Plain text
  171. (defcustom org-e-man-quotes
  172. '(("fr"
  173. ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
  174. ("\\(\\S-\\)\"" . "~»")
  175. ("\\(\\s-\\|(\\|^\\)'" . "'"))
  176. ("en"
  177. ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
  178. ("\\(\\S-\\)\"" . "''")
  179. ("\\(\\s-\\|(\\|^\\)'" . "`")))
  180. "Alist for quotes to use when converting english double-quotes.
  181. The CAR of each item in this alist is the language code.
  182. The CDR of each item in this alist is a list of three CONS:
  183. - the first CONS defines the opening quote;
  184. - the second CONS defines the closing quote;
  185. - the last CONS defines single quotes.
  186. For each item in a CONS, the first string is a regexp
  187. for allowed characters before/after the quote, the second
  188. string defines the replacement string for this quote."
  189. :group 'org-export-e-man
  190. :type '(list
  191. (cons :tag "Opening quote"
  192. (string :tag "Regexp for char before")
  193. (string :tag "Replacement quote "))
  194. (cons :tag "Closing quote"
  195. (string :tag "Regexp for char after ")
  196. (string :tag "Replacement quote "))
  197. (cons :tag "Single quote"
  198. (string :tag "Regexp for char before")
  199. (string :tag "Replacement quote "))))
  200. ;;; Compilation
  201. (defcustom org-e-man-pdf-process
  202. '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
  203. "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
  204. "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
  205. "Commands to process a Man file to a PDF file.
  206. This is a list of strings, each of them will be given to the
  207. shell as a command. %f in the command will be replaced by the
  208. full file name, %b by the file base name \(i.e. without
  209. extension) and %o by the base directory of the file.
  210. By default, Org uses 3 runs of to do the processing.
  211. Alternatively, this may be a Lisp function that does the
  212. processing. This function should accept the file name as
  213. its single argument."
  214. :group 'org-export-pdf
  215. :type '(choice
  216. (repeat :tag "Shell command sequence"
  217. (string :tag "Shell command"))
  218. (const :tag "2 runs of pdfgroff"
  219. ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
  220. "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
  221. (const :tag "3 runs of pdfgroff"
  222. ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
  223. "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
  224. "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
  225. (function)))
  226. (defcustom org-e-man-logfiles-extensions
  227. '("log" "out" "toc")
  228. "The list of file extensions to consider as Man logfiles."
  229. :group 'org-export-e-man
  230. :type '(repeat (string :tag "Extension")))
  231. (defcustom org-e-man-remove-logfiles t
  232. "Non-nil means remove the logfiles produced by PDF production.
  233. These are the .aux, .log, .out, and .toc files."
  234. :group 'org-export-e-man
  235. :type 'boolean)
  236. ;;; Internal Functions
  237. (defun org-e-man--caption/label-string (caption label info)
  238. "Return caption and label Man string for floats.
  239. CAPTION is a cons cell of secondary strings, the car being the
  240. standard caption and the cdr its short form. LABEL is a string
  241. representing the label. INFO is a plist holding contextual
  242. information.
  243. If there's no caption nor label, return the empty string.
  244. For non-floats, see `org-e-man--wrap-label'."
  245. (let ((label-str "" ))
  246. (cond
  247. ((and (not caption) (not label)) "")
  248. ((not caption) (format "\\fI%s\\fP" label))
  249. ;; Option caption format with short name.
  250. ((cdr caption)
  251. (format "\\fR%s\\fP - \\fI%s\\P - %s\n"
  252. (org-export-data (cdr caption) info)
  253. label-str
  254. (org-export-data (car caption) info)))
  255. ;; Standard caption format.
  256. (t (format "\\fR%s\\fP"
  257. (org-export-data (car caption) info))))))
  258. (defun org-e-man--quotation-marks (text info)
  259. "Export quotation marks depending on language conventions.
  260. TEXT is a string containing quotation marks to be replaced. INFO
  261. is a plist used as a communication channel."
  262. (mapc (lambda(l)
  263. (let ((start 0))
  264. (while (setq start (string-match (car l) text start))
  265. (let ((new-quote (concat (match-string 1 text) (cdr l))))
  266. (setq text (replace-match new-quote t t text))))))
  267. (cdr (or (assoc (plist-get info :language) org-e-man-quotes)
  268. ;; Falls back on English.
  269. (assoc "en" org-e-man-quotes)))) text)
  270. (defun org-e-man--wrap-label (element output)
  271. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  272. This function shouldn't be used for floats. See
  273. `org-e-man--caption/label-string'."
  274. (let ((label (org-element-property :name element)))
  275. (if (or (not output) (not label) (string= output "") (string= label ""))
  276. output
  277. (concat (format "%s\n.br\n" label) output))))
  278. ;;; Template
  279. (defun org-e-man-template (contents info)
  280. "Return complete document string after Man conversion.
  281. CONTENTS is the transcoded contents string. INFO is a plist
  282. holding export options."
  283. (let* ((title (org-export-data (plist-get info :title) info))
  284. (attr (read (format "(%s)"
  285. (mapconcat
  286. #'identity
  287. (list (plist-get info :man-class-options))
  288. " "))))
  289. (section-item (plist-get attr :section-id)))
  290. (concat
  291. (cond
  292. ((and title (stringp section-item))
  293. (format ".TH \"%s\" \"%s\" \n" title section-item))
  294. ((and (string= "" title) (stringp section-item))
  295. (format ".TH \"%s\" \"%s\" \n" " " section-item))
  296. (title
  297. (format ".TH \"%s\" \"1\" \n" title))
  298. (t
  299. ".TH \" \" \"1\" "))
  300. contents)))
  301. ;;; Transcode Functions
  302. ;;; Babel Call
  303. ;;
  304. ;; Babel Calls are ignored.
  305. ;;; Bold
  306. (defun org-e-man-bold (bold contents info)
  307. "Transcode BOLD from Org to Man.
  308. CONTENTS is the text with bold markup. INFO is a plist holding
  309. contextual information."
  310. (format "\\fB%s\\fP" contents))
  311. ;;; Center Block
  312. (defun org-e-man-center-block (center-block contents info)
  313. "Transcode a CENTER-BLOCK element from Org to Man.
  314. CONTENTS holds the contents of the center block. INFO is a plist
  315. holding contextual information."
  316. (org-e-man--wrap-label
  317. center-block
  318. (format ".ce %d\n.nf\n%s\n.fi"
  319. (- (length (split-string contents "\n")) 1 )
  320. contents)))
  321. ;;; Clock
  322. (defun org-e-man-clock (clock contents info)
  323. "Transcode a CLOCK element from Org to Man.
  324. CONTENTS is nil. INFO is a plist holding contextual
  325. information."
  326. "" )
  327. ;;; Code
  328. (defun org-e-man-code (code contents info)
  329. "Transcode a CODE object from Org to Man.
  330. CONTENTS is nil. INFO is a plist used as a communication
  331. channel."
  332. (format "\\fC%s\\fP" code))
  333. ;;; Comment
  334. ;;
  335. ;; Comments are ignored.
  336. ;;; Comment Block
  337. ;;
  338. ;; Comment Blocks are ignored.
  339. ;;; Drawer
  340. (defun org-e-man-drawer (drawer contents info)
  341. "Transcode a DRAWER element from Org to Man.
  342. DRAWER holds the drawer information
  343. CONTENTS holds the contents of the block.
  344. INFO is a plist holding contextual information. "
  345. contents)
  346. ;;; Dynamic Block
  347. (defun org-e-man-dynamic-block (dynamic-block contents info)
  348. "Transcode a DYNAMIC-BLOCK element from Org to Man.
  349. CONTENTS holds the contents of the block. INFO is a plist
  350. holding contextual information. See `org-export-data'."
  351. (org-e-man--wrap-label dynamic-block contents))
  352. ;;; Entity
  353. (defun org-e-man-entity (entity contents info)
  354. "Transcode an ENTITY object from Org to Man.
  355. CONTENTS are the definition itself. INFO is a plist holding
  356. contextual information."
  357. (let ((ent (org-element-property :utf8 entity))) ent))
  358. ;;; Example Block
  359. (defun org-e-man-example-block (example-block contents info)
  360. "Transcode an EXAMPLE-BLOCK element from Org to Man.
  361. CONTENTS is nil. INFO is a plist holding contextual
  362. information."
  363. (org-e-man--wrap-label
  364. example-block
  365. (format ".RS\n.nf\n%s\n.fi\n.RE"
  366. (org-export-format-code-default example-block info))))
  367. ;;; Export Block
  368. (defun org-e-man-export-block (export-block contents info)
  369. "Transcode a EXPORT-BLOCK element from Org to Man.
  370. CONTENTS is nil. INFO is a plist holding contextual information."
  371. (when (string= (org-element-property :type export-block) "MAN")
  372. (org-remove-indentation (org-element-property :value export-block))))
  373. ;;; Export Snippet
  374. (defun org-e-man-export-snippet (export-snippet contents info)
  375. "Transcode a EXPORT-SNIPPET object from Org to Man.
  376. CONTENTS is nil. INFO is a plist holding contextual information."
  377. (when (eq (org-export-snippet-backend export-snippet) 'e-man)
  378. (org-element-property :value export-snippet)))
  379. ;;; Fixed Width
  380. (defun org-e-man-fixed-width (fixed-width contents info)
  381. "Transcode a FIXED-WIDTH element from Org to Man.
  382. CONTENTS is nil. INFO is a plist holding contextual information."
  383. (org-e-man--wrap-label
  384. fixed-width
  385. (format "\\fC\n%s\\fP"
  386. (org-remove-indentation
  387. (org-element-property :value fixed-width)))))
  388. ;;; Footnote Definition
  389. ;;
  390. ;; Footnote Definitions are ignored.
  391. ;;; Footnote References
  392. ;;
  393. ;; Footnote References are Ignored
  394. ;;; Headline
  395. (defun org-e-man-headline (headline contents info)
  396. "Transcode an HEADLINE element from Org to Man.
  397. CONTENTS holds the contents of the headline. INFO is a plist
  398. holding contextual information."
  399. (let* ((level (org-export-get-relative-level headline info))
  400. (numberedp (org-export-numbered-headline-p headline info))
  401. ;; Section formatting will set two placeholders: one for the
  402. ;; title and the other for the contents.
  403. (section-fmt
  404. (case level
  405. (1 ".SH \"%s\"\n%s")
  406. (2 ".SS \"%s\"\n%s")
  407. (3 ".SS \"%s\"\n%s")
  408. (t nil)))
  409. (text (org-export-data (org-element-property :title headline) info)))
  410. (cond
  411. ;; Case 1: This is a footnote section: ignore it.
  412. ((org-element-property :footnote-section-p headline) nil)
  413. ;; Case 2. This is a deep sub-tree: export it as a list item.
  414. ;; Also export as items headlines for which no section
  415. ;; format has been found.
  416. ((or (not section-fmt) (org-export-low-level-p headline info))
  417. ;; Build the real contents of the sub-tree.
  418. (let ((low-level-body
  419. (concat
  420. ;; If the headline is the first sibling, start a list.
  421. (when (org-export-first-sibling-p headline info)
  422. (format "%s\n" ".RS"))
  423. ;; Itemize headline
  424. ".TP\n.ft I\n" text "\n.ft\n"
  425. contents ".RE")))
  426. ;; If headline is not the last sibling simply return
  427. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
  428. ;; blank line.
  429. (if (not (org-export-last-sibling-p headline info)) low-level-body
  430. (replace-regexp-in-string
  431. "[ \t\n]*\\'" ""
  432. low-level-body))))
  433. ;; Case 3. Standard headline. Export it as a section.
  434. (t (format section-fmt text contents )))))
  435. ;;; Horizontal Rule
  436. ;; Not supported
  437. ;;; Inline Babel Call
  438. ;;
  439. ;; Inline Babel Calls are ignored.
  440. ;;; Inline Src Block
  441. (defun org-e-man-inline-src-block (inline-src-block contents info)
  442. "Transcode an INLINE-SRC-BLOCK element from Org to Man.
  443. CONTENTS holds the contents of the item. INFO is a plist holding
  444. contextual information."
  445. (let* ((code (org-element-property :value inline-src-block)))
  446. (cond
  447. (org-e-man-source-highlight
  448. (let* ((tmpdir (if (featurep 'xemacs)
  449. temp-directory
  450. temporary-file-directory ))
  451. (in-file (make-temp-name
  452. (expand-file-name "srchilite" tmpdir)))
  453. (out-file (make-temp-name
  454. (expand-file-name "reshilite" tmpdir)))
  455. (org-lang (org-element-property :language inline-src-block))
  456. (lst-lang (cadr (assq (intern org-lang)
  457. org-e-man-source-highlight-langs)))
  458. (cmd (concat (expand-file-name "source-highlight")
  459. " -s " lst-lang
  460. " -f groff_man"
  461. " -i " in-file
  462. " -o " out-file )))
  463. (if lst-lang
  464. (let ((code-block "" ))
  465. (with-temp-file in-file (insert code))
  466. (shell-command cmd)
  467. (setq code-block (org-file-contents out-file))
  468. (delete-file in-file)
  469. (delete-file out-file)
  470. code-block)
  471. (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
  472. code))))
  473. ;; Do not use a special package: transcode it verbatim.
  474. (t
  475. (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
  476. "\\fP\n.fi\n.RE\n")))))
  477. ;;; Inlinetask
  478. ;;; Italic
  479. (defun org-e-man-italic (italic contents info)
  480. "Transcode ITALIC from Org to Man.
  481. CONTENTS is the text with italic markup. INFO is a plist holding
  482. contextual information."
  483. (format "\\fI%s\\fP" contents))
  484. ;;; Item
  485. (defun org-e-man-item (item contents info)
  486. "Transcode an ITEM element from Org to Man.
  487. CONTENTS holds the contents of the item. INFO is a plist holding
  488. contextual information."
  489. (let* ((bullet (org-element-property :bullet item))
  490. (type (org-element-property :type (org-element-property :parent item)))
  491. (checkbox (case (org-element-property :checkbox item)
  492. (on "\\o'\\(sq\\(mu'") ;;
  493. (off "\\(sq ") ;;
  494. (trans "\\o'\\(sq\\(mi'" ))) ;;
  495. (tag (let ((tag (org-element-property :tag item)))
  496. ;; Check-boxes must belong to the tag.
  497. (and tag (format "\\fB%s\\fP"
  498. (concat checkbox
  499. (org-export-data tag info)))))))
  500. (if (and (null tag )
  501. (null checkbox))
  502. (let* ((bullet (org-trim bullet))
  503. (marker (cond ((string= "-" bullet) "\\(em")
  504. ((string= "*" bullet) "\\(bu")
  505. ((eq type 'ordered)
  506. (format "%s " (org-trim bullet)))
  507. (t "\\(dg"))))
  508. (concat ".IP " marker " 4\n"
  509. (org-trim (or contents " " ))))
  510. ; else
  511. (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
  512. (org-trim (or contents " " ))))))
  513. ;;; Keyword
  514. (defun org-e-man-keyword (keyword contents info)
  515. "Transcode a KEYWORD element from Org to Man.
  516. CONTENTS is nil. INFO is a plist holding contextual information."
  517. (let ((key (org-element-property :key keyword))
  518. (value (org-element-property :value keyword)))
  519. (cond
  520. ((string= key "MAN") value)
  521. ((string= key "INDEX") nil)
  522. ;; Invisible targets.
  523. ((string= key "TARGET") nil)
  524. ((string= key "TOC" ) nil))))
  525. ;;; Man Environment
  526. (defun org-e-man-man-environment (man-environment contents info)
  527. "Transcode a MAN-ENVIRONMENT element from Org to Man.
  528. CONTENTS is nil. INFO is a plist holding contextual information."
  529. (let ((label (org-element-property :name man-environment))
  530. (value (org-remove-indentation
  531. (org-element-property :value man-environment))))
  532. (if (not (org-string-nw-p label)) value
  533. ;; Environment is labelled: label must be within the environment
  534. ;; (otherwise, a reference pointing to that element will count
  535. ;; the section instead).
  536. (with-temp-buffer
  537. (insert value)
  538. (goto-char (point-min))
  539. (forward-line)
  540. (insert (format "%s\n" label))
  541. (buffer-string)))))
  542. ;;; Man Fragment
  543. (defun org-e-man-man-fragment (man-fragment contents info)
  544. "Transcode a MAN-FRAGMENT object from Org to Man.
  545. CONTENTS is nil. INFO is a plist holding contextual information."
  546. (org-element-property :value man-fragment))
  547. ;;; Line Break
  548. (defun org-e-man-line-break (line-break contents info)
  549. "Transcode a LINE-BREAK object from Org to Man.
  550. CONTENTS is nil. INFO is a plist holding contextual information."
  551. ".br\n")
  552. ;;; Link
  553. (defun org-e-man-link (link desc info)
  554. "Transcode a LINK object from Org to Man.
  555. DESC is the description part of the link, or the empty string.
  556. INFO is a plist holding contextual information. See
  557. `org-export-data'."
  558. (let* ((type (org-element-property :type link))
  559. (raw-path (org-element-property :path link))
  560. ;; Ensure DESC really exists, or set it to nil.
  561. (desc (and (not (string= desc "")) desc))
  562. (path (cond
  563. ((member type '("http" "https" "ftp" "mailto"))
  564. (concat type ":" raw-path))
  565. ((string= type "file")
  566. (when (string-match "\\(.+\\)::.+" raw-path)
  567. (setq raw-path (match-string 1 raw-path)))
  568. (if (file-name-absolute-p raw-path)
  569. (concat "file://" (expand-file-name raw-path))
  570. (concat "file://" raw-path)))
  571. (t raw-path)))
  572. protocol)
  573. (cond
  574. ;; External link with a description part.
  575. ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
  576. ;; External link without a description part.
  577. (path (format "\\fI%s\\fP" path))
  578. ;; No path, only description. Try to do something useful.
  579. (t (format "\\fI%s\\fP" desc)))))
  580. ;;; Paragraph
  581. (defun org-e-man-paragraph (paragraph contents info)
  582. "Transcode a PARAGRAPH element from Org to Man.
  583. CONTENTS is the contents of the paragraph, as a string. INFO is
  584. the plist used as a communication channel."
  585. (let ((parent (plist-get (nth 1 paragraph) :parent)))
  586. (when parent
  587. (let ((parent-type (car parent))
  588. (fixed-paragraph ""))
  589. (cond ((and (eq parent-type 'item)
  590. (plist-get (nth 1 parent) :bullet ))
  591. (setq fixed-paragraph (concat "" contents)))
  592. ((eq parent-type 'section)
  593. (setq fixed-paragraph (concat ".PP\n" contents)))
  594. ((eq parent-type 'footnote-definition)
  595. (setq fixed-paragraph contents))
  596. (t (setq fixed-paragraph (concat "" contents))))
  597. fixed-paragraph ))))
  598. ;;; Plain List
  599. (defun org-e-man-plain-list (plain-list contents info)
  600. "Transcode a PLAIN-LIST element from Org to Man.
  601. CONTENTS is the contents of the list. INFO is a plist holding
  602. contextual information."
  603. contents)
  604. ;;; Plain Text
  605. (defun org-e-man-plain-text (text info)
  606. "Transcode a TEXT string from Org to Man.
  607. TEXT is the string to transcode. INFO is a plist holding
  608. contextual information."
  609. ;; Protect
  610. (setq text (replace-regexp-in-string
  611. "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
  612. "$\\" text nil t 1))
  613. ;; Handle quotation marks
  614. (setq text (org-e-man--quotation-marks text info))
  615. ;; Handle break preservation if required.
  616. (when (plist-get info :preserve-breaks)
  617. (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
  618. text)))
  619. ;; Return value.
  620. text)
  621. ;;; Planning
  622. ;;; Property Drawer
  623. ;;; Quote Block
  624. (defun org-e-man-quote-block (quote-block contents info)
  625. "Transcode a QUOTE-BLOCK element from Org to Man.
  626. CONTENTS holds the contents of the block. INFO is a plist
  627. holding contextual information."
  628. (org-e-man--wrap-label
  629. quote-block
  630. (format ".RS\n%s\n.RE" contents)))
  631. ;;; Quote Section
  632. (defun org-e-man-quote-section (quote-section contents info)
  633. "Transcode a QUOTE-SECTION element from Org to Man.
  634. CONTENTS is nil. INFO is a plist holding contextual information."
  635. (let ((value (org-remove-indentation
  636. (org-element-property :value quote-section))))
  637. (when value (format ".RS\\fI%s\\fP\n.RE\n" value))))
  638. ;;; Radio Target
  639. (defun org-e-man-radio-target (radio-target text info)
  640. "Transcode a RADIO-TARGET object from Org to Man.
  641. TEXT is the text of the target. INFO is a plist holding
  642. contextual information."
  643. text )
  644. ;;; Section
  645. (defun org-e-man-section (section contents info)
  646. "Transcode a SECTION element from Org to Man.
  647. CONTENTS holds the contents of the section. INFO is a plist
  648. holding contextual information."
  649. contents)
  650. ;;; Special Block
  651. (defun org-e-man-special-block (special-block contents info)
  652. "Transcode a SPECIAL-BLOCK element from Org to Man.
  653. CONTENTS holds the contents of the block. INFO is a plist
  654. holding contextual information."
  655. (let ((type (downcase (org-element-property :type special-block))))
  656. (org-e-man--wrap-label
  657. special-block
  658. (format "%s\n" contents))))
  659. ;;; Src Block
  660. (defun org-e-man-src-block (src-block contents info)
  661. "Transcode a SRC-BLOCK element from Org to Man.
  662. CONTENTS holds the contents of the item. INFO is a plist holding
  663. contextual information."
  664. (let* ((lang (org-element-property :language src-block))
  665. (caption (org-element-property :caption src-block))
  666. (label (org-element-property :name src-block))
  667. (code (org-element-property :value src-block))
  668. (custom-env (and lang
  669. (cadr (assq (intern lang)
  670. org-e-man-custom-lang-environments))))
  671. (num-start (case (org-element-property :number-lines src-block)
  672. (continued (org-export-get-loc src-block info))
  673. (new 0)))
  674. (retain-labels (org-element-property :retain-labels src-block)))
  675. (cond
  676. ;; Case 1. No source fontification.
  677. ((not org-e-man-source-highlight)
  678. (let ((caption-str (org-e-man--caption/label-string caption label info)))
  679. (concat
  680. (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
  681. (org-export-format-code-default src-block info)))))
  682. ( (and org-e-man-source-highlight)
  683. (let* ((tmpdir (if (featurep 'xemacs)
  684. temp-directory
  685. temporary-file-directory ))
  686. (in-file (make-temp-name
  687. (expand-file-name "srchilite" tmpdir)))
  688. (out-file (make-temp-name
  689. (expand-file-name "reshilite" tmpdir)))
  690. (org-lang (org-element-property :language src-block))
  691. (lst-lang (cadr (assq (intern org-lang)
  692. org-e-man-source-highlight-langs)))
  693. (cmd (concat "source-highlight"
  694. " -s " lst-lang
  695. " -f groff_man "
  696. " -i " in-file
  697. " -o " out-file)))
  698. (if lst-lang
  699. (let ((code-block "" ))
  700. (with-temp-file in-file (insert code))
  701. (shell-command cmd)
  702. (setq code-block (org-file-contents out-file))
  703. (delete-file in-file)
  704. (delete-file out-file)
  705. code-block)
  706. (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE"
  707. code)))))))
  708. ;;; Statistics Cookie
  709. (defun org-e-man-statistics-cookie (statistics-cookie contents info)
  710. "Transcode a STATISTICS-COOKIE object from Org to Man.
  711. CONTENTS is nil. INFO is a plist holding contextual information."
  712. (org-element-property :value statistics-cookie))
  713. ;;; Strike-Through
  714. (defun org-e-man-strike-through (strike-through contents info)
  715. "Transcode STRIKE-THROUGH from Org to Man.
  716. CONTENTS is the text with strike-through markup. INFO is a plist
  717. holding contextual information."
  718. (format "\\fI%s\\fP" contents))
  719. ;;; Subscript
  720. (defun org-e-man-subscript (subscript contents info)
  721. "Transcode a SUBSCRIPT object from Org to Man.
  722. CONTENTS is the contents of the object. INFO is a plist holding
  723. contextual information."
  724. (format "\\d\\s-2%s\\s+2\\u" contents))
  725. ;;; Superscript "^_%s$
  726. (defun org-e-man-superscript (superscript contents info)
  727. "Transcode a SUPERSCRIPT object from Org to Man.
  728. CONTENTS is the contents of the object. INFO is a plist holding
  729. contextual information."
  730. (format "\\u\\s-2%s\\s+2\\d" contents))
  731. ;;; Table
  732. ;;
  733. ;; `org-e-man-table' is the entry point for table transcoding. It
  734. ;; takes care of tables with a "verbatim" attribute. Otherwise, it
  735. ;; delegates the job to either `org-e-man-table--table.el-table' or
  736. ;; `org-e-man-table--org-table' functions, depending of the type of
  737. ;; the table.
  738. ;;
  739. ;; `org-e-man-table--align-string' is a subroutine used to build
  740. ;; alignment string for Org tables.
  741. (defun org-e-man-table (table contents info)
  742. "Transcode a TABLE element from Org to Man.
  743. CONTENTS is the contents of the table. INFO is a plist holding
  744. contextual information."
  745. (cond
  746. ;; Case 1: verbatim table.
  747. ((or org-e-man-tables-verbatim
  748. (let ((attr (read (format "(%s)"
  749. (mapconcat
  750. #'identity
  751. (org-element-property :attr_man table)
  752. " ")))))
  753. (and attr (plist-get attr :verbatim))))
  754. (format ".nf\n\\fC%s\\fP\n.fi"
  755. ;; Re-create table, without affiliated keywords.
  756. (org-trim
  757. (org-element-interpret-data
  758. `(table nil ,@(org-element-contents table))))))
  759. ;; Case 2: Standard table.
  760. (t (org-e-man-table--org-table table contents info))))
  761. (defun org-e-man-table--align-string (divider table info)
  762. "Return an appropriate Man alignment string.
  763. TABLE is the considered table. INFO is a plist used as
  764. a communication channel."
  765. (let (alignment)
  766. ;; Extract column groups and alignment from first (non-rule)
  767. ;; row.
  768. (org-element-map
  769. (org-element-map
  770. table 'table-row
  771. (lambda (row)
  772. (and (eq (org-element-property :type row) 'standard) row))
  773. info 'first-match)
  774. 'table-cell
  775. (lambda (cell)
  776. (let* ((borders (org-export-table-cell-borders cell info))
  777. (raw-width (org-export-table-cell-width cell info))
  778. (width-cm (when raw-width (/ raw-width 5)))
  779. (width (if raw-width (format "w(%dc)"
  780. (if (< width-cm 1) 1 width-cm)) "")))
  781. ;; Check left border for the first cell only.
  782. (when (and (memq 'left borders) (not alignment))
  783. (push "|" alignment))
  784. (push
  785. (case (org-export-table-cell-alignment cell info)
  786. (left (concat "l" width divider))
  787. (right (concat "r" width divider))
  788. (center (concat "c" width divider)))
  789. alignment)
  790. (when (memq 'right borders) (push "|" alignment))))
  791. info)
  792. (apply 'concat (reverse alignment))))
  793. (defun org-e-man-table--org-table (table contents info)
  794. "Return appropriate Man code for an Org table.
  795. TABLE is the table type element to transcode. CONTENTS is its
  796. contents, as a string. INFO is a plist used as a communication
  797. channel.
  798. This function assumes TABLE has `org' as its `:type' attribute."
  799. (let* ((label (org-element-property :name table))
  800. (caption (org-e-man--caption/label-string
  801. (org-element-property :caption table) label info))
  802. (attr (read
  803. (format "(%s)"
  804. (mapconcat
  805. #'identity
  806. (org-element-property :attr_man table)
  807. " "))))
  808. (divider (if (plist-get attr :divider)
  809. "|"
  810. " "))
  811. ;; Determine alignment string.
  812. (alignment (org-e-man-table--align-string divider table info))
  813. ;; Extract others display options.
  814. (lines (org-split-string contents "\n"))
  815. (attr-list
  816. (let ((result-list '()))
  817. (dolist (attr-item
  818. (list
  819. (if (plist-get attr :expand)
  820. "expand" nil)
  821. (case (plist-get attr :placement)
  822. ('center "center")
  823. ('left nil)
  824. (t
  825. (if org-e-man-tables-centered
  826. "center" "")))
  827. (case (plist-get attr :boxtype)
  828. ('box "box")
  829. ('doublebox "doublebox")
  830. ('allbox "allbox")
  831. ('none nil)
  832. (t "box"))))
  833. (if attr-item
  834. (add-to-list 'result-list attr-item)))
  835. result-list ))
  836. (title-line (plist-get attr :title-line))
  837. (disable-caption (plist-get attr :disable-caption))
  838. (long-cells (plist-get attr :long-cells))
  839. (table-format (concat
  840. (format "%s" (or (car attr-list) "" ))
  841. (or
  842. (let ((output-list '()))
  843. (when (cdr attr-list)
  844. (dolist (attr-item (cdr attr-list))
  845. (setq output-list (concat output-list (format ",%s" attr-item)))))
  846. output-list)
  847. "")))
  848. (first-line (when lines (org-split-string (car lines) "\t"))))
  849. ;; Prepare the final format string for the table.
  850. (cond
  851. ;; Others.
  852. (lines (concat ".TS\n " table-format ";\n"
  853. (format "%s.\n"
  854. (let ((final-line ""))
  855. (when title-line
  856. (dotimes (i (length first-line))
  857. (setq final-line (concat final-line "cb" divider))))
  858. (setq final-line (concat final-line "\n"))
  859. (if alignment
  860. (setq final-line (concat final-line alignment))
  861. (dotimes (i (length first-line))
  862. (setq final-line (concat final-line "c" divider))))
  863. final-line ))
  864. (format "%s.TE\n"
  865. (let ((final-line "")
  866. (long-line "")
  867. (lines (org-split-string contents "\n")))
  868. (dolist (line-item lines)
  869. (setq long-line "")
  870. (if long-cells
  871. (progn
  872. (if (string= line-item "_")
  873. (setq long-line (format "%s\n" line-item))
  874. ;; else string =
  875. (let ((cell-item-list (org-split-string line-item "\t")))
  876. (dolist (cell-item cell-item-list)
  877. (cond ((eq cell-item (car (last cell-item-list)))
  878. (setq long-line (concat long-line
  879. (format "T{\n%s\nT}\t\n" cell-item ))))
  880. (t
  881. (setq long-line (concat long-line
  882. (format "T{\n%s\nT}\t" cell-item ))))))
  883. long-line))
  884. ;; else long cells
  885. (setq final-line (concat final-line long-line )))
  886. (setq final-line (concat final-line line-item "\n"))))
  887. final-line))
  888. (if (not disable-caption)
  889. (format ".TB \"%s\""
  890. caption) ""))))))
  891. ;;; Table Cell
  892. (defun org-e-man-table-cell (table-cell contents info)
  893. "Transcode a TABLE-CELL element from Org to Man
  894. CONTENTS is the cell contents. INFO is a plist used as
  895. a communication channel."
  896. (concat (if (and contents
  897. org-e-man-table-scientific-notation
  898. (string-match orgtbl-exp-regexp contents))
  899. ;; Use appropriate format string for scientific
  900. ;; notation.
  901. (format org-e-man-table-scientific-notation
  902. (match-string 1 contents)
  903. (match-string 2 contents))
  904. contents )
  905. (when (org-export-get-next-element table-cell info) "\t")))
  906. ;;; Table Row
  907. (defun org-e-man-table-row (table-row contents info)
  908. "Transcode a TABLE-ROW element from Org to Man
  909. CONTENTS is the contents of the row. INFO is a plist used as
  910. a communication channel."
  911. ;; Rules are ignored since table separators are deduced from
  912. ;; borders of the current row.
  913. (when (eq (org-element-property :type table-row) 'standard)
  914. (let* ((attr (mapconcat 'identity
  915. (org-element-property
  916. :attr_man (org-export-get-parent table-row))
  917. " "))
  918. ;; TABLE-ROW's borders are extracted from its first cell.
  919. (borders
  920. (org-export-table-cell-borders
  921. (car (org-element-contents table-row)) info)))
  922. (concat
  923. ;; Mark horizontal lines
  924. (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
  925. contents
  926. (cond
  927. ;; When BOOKTABS are activated enforce bottom rule even when
  928. ;; no hline was specifically marked.
  929. ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
  930. ((memq 'below borders) "\n_"))))))
  931. ;;; Target
  932. (defun org-e-man-target (target contents info)
  933. "Transcode a TARGET object from Org to Man.
  934. CONTENTS is nil. INFO is a plist holding contextual
  935. information."
  936. (format "\\fI%s\\fP"
  937. (org-export-solidify-link-text (org-element-property :value target))))
  938. ;;; Timestamp
  939. (defun org-e-man-timestamp (timestamp contents info)
  940. "Transcode a TIMESTAMP object from Org to Man.
  941. CONTENTS is nil. INFO is a plist holding contextual
  942. information."
  943. "" )
  944. ;;; Underline
  945. (defun org-e-man-underline (underline contents info)
  946. "Transcode UNDERLINE from Org to Man.
  947. CONTENTS is the text with underline markup. INFO is a plist
  948. holding contextual information."
  949. (format "\\fI%s\\fP" contents))
  950. ;;; Verbatim
  951. (defun org-e-man-verbatim (verbatim contents info)
  952. "Transcode a VERBATIM object from Org to Man.
  953. CONTENTS is nil. INFO is a plist used as a communication
  954. channel."
  955. (format ".nf\n%s\n.fi" contents))
  956. ;;; Verse Block
  957. (defun org-e-man-verse-block (verse-block contents info)
  958. "Transcode a VERSE-BLOCK element from Org to Man.
  959. CONTENTS is verse block contents. INFO is a plist holding
  960. contextual information."
  961. (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
  962. ;;; Interactive functions
  963. (defun org-e-man-export-to-man
  964. (&optional subtreep visible-only body-only ext-plist pub-dir)
  965. "Export current buffer to a Man file.
  966. If narrowing is active in the current buffer, only export its
  967. narrowed part.
  968. If a region is active, export that region.
  969. When optional argument SUBTREEP is non-nil, export the sub-tree
  970. at point, extracting information from the headline properties
  971. first.
  972. When optional argument VISIBLE-ONLY is non-nil, don't export
  973. contents of hidden elements.
  974. When optional argument BODY-ONLY is non-nil, only the body
  975. without any markers.
  976. EXT-PLIST, when provided, is a property list with external
  977. parameters overriding Org default settings, but still inferior to
  978. file-local settings.
  979. When optional argument PUB-DIR is set, use it as the publishing
  980. directory.
  981. Return output file's name."
  982. (interactive)
  983. (let ((outfile (org-export-output-file-name ".man" subtreep pub-dir)))
  984. (org-export-to-file
  985. 'e-man outfile subtreep visible-only body-only ext-plist)))
  986. (defun org-e-man-export-to-pdf
  987. (&optional subtreep visible-only body-only ext-plist pub-dir)
  988. "Export current buffer to Groff then process through to PDF.
  989. If narrowing is active in the current buffer, only export its
  990. narrowed part.
  991. If a region is active, export that region.
  992. When optional argument SUBTREEP is non-nil, export the sub-tree
  993. at point, extracting information from the headline properties
  994. first.
  995. When optional argument VISIBLE-ONLY is non-nil, don't export
  996. contents of hidden elements.
  997. When optional argument BODY-ONLY is non-nil, only write between
  998. markers.
  999. EXT-PLIST, when provided, is a property list with external
  1000. parameters overriding Org default settings, but still inferior to
  1001. file-local settings.
  1002. When optional argument PUB-DIR is set, use it as the publishing
  1003. directory.
  1004. Return PDF file's name."
  1005. (interactive)
  1006. (org-e-man-compile
  1007. (org-e-man-export-to-man
  1008. subtreep visible-only body-only ext-plist pub-dir)))
  1009. (defun org-e-man-compile (grofffile)
  1010. "Compile a Groff file.
  1011. GROFFFILE is the name of the file being compiled. Processing is
  1012. done through the command specified in `org-e-man-pdf-process'.
  1013. Return PDF file name or an error if it couldn't be produced."
  1014. (let* ((wconfig (current-window-configuration))
  1015. (grofffile (file-truename grofffile))
  1016. (base (file-name-sans-extension grofffile))
  1017. errors)
  1018. (message (format "Processing Groff file %s ..." grofffile))
  1019. (unwind-protect
  1020. (progn
  1021. (cond
  1022. ;; A function is provided: Apply it.
  1023. ((functionp org-e-man-pdf-process)
  1024. (funcall org-e-man-pdf-process (shell-quote-argument grofffile)))
  1025. ;; A list is provided: Replace %b, %f and %o with appropriate
  1026. ;; values in each command before applying it. Output is
  1027. ;; redirected to "*Org PDF Groff Output*" buffer.
  1028. ((consp org-e-man-pdf-process)
  1029. (let* ((out-dir (or (file-name-directory grofffile) "./"))
  1030. (outbuf (get-buffer-create "*Org PDF Groff Output*")))
  1031. (mapc
  1032. (lambda (command)
  1033. (shell-command
  1034. (replace-regexp-in-string
  1035. "%b" (shell-quote-argument base)
  1036. (replace-regexp-in-string
  1037. "%f" (shell-quote-argument grofffile)
  1038. (replace-regexp-in-string
  1039. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  1040. outbuf))
  1041. org-e-man-pdf-process)
  1042. ;; Collect standard errors from output buffer.
  1043. (setq errors (org-e-man-collect-errors outbuf))))
  1044. (t (error "No valid command to process to PDF")))
  1045. (let ((pdffile (concat base ".pdf")))
  1046. ;; Check for process failure. Provide collected errors if
  1047. ;; possible.
  1048. (if (not (file-exists-p pdffile))
  1049. (error (concat (format "PDF file %s wasn't produced" pdffile)
  1050. (when errors (concat ": " errors))))
  1051. ;; Else remove log files, when specified, and signal end of
  1052. ;; process to user, along with any error encountered.
  1053. (when org-e-man-remove-logfiles
  1054. (dolist (ext org-e-man-logfiles-extensions)
  1055. (let ((file (concat base "." ext)))
  1056. (when (file-exists-p file) (delete-file file)))))
  1057. (message (concat "Process completed"
  1058. (if (not errors) "."
  1059. (concat " with errors: " errors)))))
  1060. ;; Return output file name.
  1061. pdffile))
  1062. (set-window-configuration wconfig))))
  1063. (defun org-e-man-collect-errors (buffer)
  1064. "Collect some kind of errors from \"groff\" output
  1065. BUFFER is the buffer containing output.
  1066. Return collected error types as a string, or nil if there was
  1067. none."
  1068. (with-current-buffer buffer
  1069. (save-excursion
  1070. (goto-char (point-max))
  1071. ;; Find final run
  1072. nil )))
  1073. (provide 'org-e-man)
  1074. ;;; org-e-man.el ends here