PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/ide/bioperl-mode/site-lisp/pod.el

https://github.com/gjuggler/bioperl-live
Emacs Lisp | 224 lines | 180 code | 10 blank | 34 comment | 1 complexity | 161c7470429bd110e4c352247115e685 MD5 | raw file
  1. ;; $Id$
  2. ;;
  3. ;;
  4. ;; Emacs functions for simple Perl pod parsing
  5. ;; parse result format based on pod2text
  6. ;;
  7. ;; required in bioperl-mode.el
  8. ;;
  9. ;; Author: Mark A. Jensen
  10. ;; Email : maj -at- fortinbras -dot- us
  11. ;;
  12. ;; Part of The Documentation Project
  13. ;; http://www.bioperl.org/wiki/The_Documentation_Project
  14. ;;
  15. ;;
  16. ;; Copyright (C) 2009 Mark A. Jensen
  17. ;; This program is free software; you can redistribute it and/or
  18. ;; modify it under the terms of the GNU General Public License as
  19. ;; published by the Free Software Foundation; either version 3 of
  20. ;; the License, or (at your option) any later version.
  21. ;; This program is distributed in the hope that it will be
  22. ;; useful, but WITHOUT ANY WARRANTY; without even the implied
  23. ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  24. ;; PURPOSE. See the GNU General Public License for more details.
  25. ;; You should have received a copy of the GNU General Public
  26. ;; License along with this program; if not, write to the Free
  27. ;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  28. ;; Boston, MA 02110-1301 USA
  29. (defvar pod-keywords
  30. '( "pod" "head1" "head2" "head3" "head4" "over" "item" "back" "begin" "end" "for" "encoding" "cut" )
  31. "Perl pod keywords (sans '=') ")
  32. (defvar pod-format-codes
  33. '( "I" "B" "C" "L" "E" "F" "S" "X" "Z" )
  34. "Perl pod format codes (sans <>)" )
  35. (defun pod-parse-buffer (buf &optional alt-format)
  36. "Parse the pod in the BUF.
  37. Removes code and leaves pod. Does some simple formatting a la
  38. pod2text as setup for pod-mode. If ALT-FORMAT is true, headers
  39. are flanked by '='s as in pod2text -a. "
  40. (save-excursion
  41. (set-buffer buf)
  42. (let (
  43. (cur-state)
  44. (cur-content)
  45. (tmp-state)
  46. (tmp-content)
  47. (encoding-type)
  48. (parse-tree '(("Root")))
  49. (line)
  50. (header-level)
  51. (beg (goto-char (point-min)))
  52. (end)
  53. (tbeg) (tend) ;; text region
  54. )
  55. (goto-char (point-min))
  56. ;; get encoding if present
  57. (if (re-search-forward "^=encoding\\s +\\(.*?\\)\\s " (point-max) t)
  58. (setq encoding-type (match-string 1)))
  59. (goto-char (point-min))
  60. (while (not (eobp))
  61. (setq end (re-search-forward "^=\\([a-z0-9]+\\)\\(?:$\\|\\s *\\(.*?\\)\\)$" (point-max) 1))
  62. (if (not end)
  63. t ;; done
  64. (setq tmp-state (match-string 1))
  65. (setq tmp-content (match-string 2))
  66. (if (not cur-state)
  67. (progn
  68. (beginning-of-line)
  69. (pod-do-format "ignore" beg (point))
  70. (setq end beg)))
  71. (setq cur-state tmp-state)
  72. (setq cur-content tmp-content)
  73. (if (not cur-state)
  74. t ;; done
  75. ;; otherwise, in a pod region
  76. (if (not (member cur-state pod-keywords))
  77. (error (concat "'" cur-state "' not a pod keyword")))
  78. (cond
  79. ( (not cur-state)
  80. nil ;; ????
  81. )
  82. ( (string-equal cur-state "cut")
  83. (forward-line 0)
  84. (kill-line 2)
  85. (pod-do-format "text" beg (point))
  86. (setq cur-state nil) )
  87. ( (string-equal cur-state "pod")
  88. (forward-line 0)
  89. (kill-line 2)
  90. (pod-do-format "text" beg (point))
  91. )
  92. ( (string-match "head\\([1-4]\\)" cur-state)
  93. (setq head-level (string-to-number (match-string 1 cur-state)))
  94. (forward-line 0)
  95. (kill-line 2)
  96. (pod-do-format "text" beg (point))
  97. (if (not cur-content)
  98. nil
  99. (if (not alt-format)
  100. (progn
  101. (insert-char ? (* 2 (1- head-level)))
  102. (insert cur-content "\n"))
  103. (cond
  104. ((= head-level 1)
  105. (insert "==== " cur-content " ====\n"))
  106. ((= head-level 2)
  107. (insert "== " cur-content " ==\n"))
  108. ((= head-level 3)
  109. (insert "= " cur-content " =\n"))
  110. ((= head-level 4)
  111. (insert "- " cur-content " -\n"))))
  112. ))
  113. ( (string-equal cur-state "over")
  114. (let (
  115. (indent-level cur-content)
  116. (back (save-excursion
  117. (re-search-forward "^=back" (point-max) t)))
  118. )
  119. (unless back
  120. (error "=over has no matching =back"))
  121. (forward-line 0)
  122. (kill-line 2)
  123. (pod-do-format "text" beg (point))
  124. (setq beg (point))
  125. (while (re-search-forward "^=item\\s +\\(.*?\\)$" back t)
  126. (let ( (item (match-string 1) ) )
  127. (forward-line 0)
  128. (kill-line 2)
  129. (pod-do-format "text" beg (point))
  130. (if (not alt-format)
  131. (insert " * " item "\n")
  132. (insert ": " item "\n")))
  133. (setq beg (point)))
  134. (re-search-forward "^=back" (point-max))
  135. (forward-line 0)
  136. (kill-line 2)
  137. (pod-do-format "text" beg (point))
  138. ))
  139. ( (string-equal cur-state "begin")
  140. (let (
  141. (format cur-content)
  142. (end (save-excursion
  143. (re-search-forward (concat "^=end\\s +" format)
  144. (point-max) t)))
  145. (content-beg) (content-end)
  146. )
  147. (unless end
  148. (error (concat "=begin " format " has no matching end.")))
  149. (forward-line 0)
  150. (kill-line 2)
  151. (setq content-beg (point))
  152. (re-search-forward (concat "^=end\\s +" format))
  153. (forward-line 0)
  154. (kill-line 2)
  155. (setq content-end (point))
  156. (pod-do-format format content-beg content-end)
  157. ))
  158. ( (string-equal cur-state "for")
  159. (string-match "\\([a-z]+\\)\\s +\\(.*?\\)$" cur-content)
  160. (let (
  161. (format (match-string 1 cur-content))
  162. (content (match-string 2 cur-content))
  163. )
  164. (forward-line 0)
  165. (kill-line 2)
  166. (pod-do-format "text" beg (point))
  167. ))
  168. ( (string-equal cur-state "encoding")
  169. (let (
  170. (type cur-content)
  171. )
  172. (forward-line 0)
  173. (kill-line 2)
  174. (pod-do-format "text" beg (point))
  175. t))
  176. ))
  177. ;; movement here?
  178. (setq beg (point))
  179. (setq cur-content nil) ;; clear means 'moved off pod descr line'
  180. ))
  181. (pod-do-format (if cur-state "text" "ignore") beg (point-max))
  182. t)
  183. ))
  184. (defun pod-do-format (format beg end)
  185. "Handle pod =begin format ... =end format blocks.
  186. FORMAT is a format identifier (a string); BEG and END define the
  187. text region."
  188. ;; ignore for now
  189. (if (= beg end)
  190. nil
  191. (cond
  192. ((string-equal format "ignore")
  193. (delete-region beg end))
  194. ((string-equal format "text")
  195. ;; format ordinary and verbatim lines
  196. (save-excursion
  197. (goto-char beg)
  198. (forward-line 0)
  199. (while (and (< (point) end) (not (eobp)))
  200. (cond
  201. ((string-match "[[:blank:]]" (char-to-string (char-after)))
  202. (insert-char ? 4)
  203. (setq end (+ end 4)))
  204. ((string-match "[[:space:]]" (char-to-string (char-after)))
  205. t)
  206. (t
  207. (insert-char ? 4)
  208. (setq end (+ end 4))))
  209. (forward-line 1)
  210. )))
  211. (t
  212. ;; otherwise, remove (ignore)
  213. (delete-region beg end)) )))
  214. (provide 'pod)