org-list.el 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. ;;; org-list.el --- Plain lists for Org-mode
  2. ;;
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Bastien Guerry <bzg AT altern DOT org>
  8. ;; Keywords: outlines, hypermedia, calendar, wp
  9. ;; Homepage: http://orgmode.org
  10. ;; Version: 6.28d
  11. ;;
  12. ;; This file is part of GNU Emacs.
  13. ;;
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;; This file contains the code dealing with plain lists in Org-mode.
  28. ;;; Code:
  29. (require 'org-macs)
  30. (require 'org-compat)
  31. (defvar org-blank-before-new-entry)
  32. (defvar org-M-RET-may-split-line)
  33. (declare-function org-invisible-p "org" ())
  34. (declare-function org-on-heading-p "org" (&optional invisible-ok))
  35. (declare-function outline-next-heading "outline" ())
  36. (declare-function outline-back-to-heading "outline" (&optional invisible-ok))
  37. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  38. (declare-function org-back-over-empty-lines "org" ())
  39. (declare-function org-skip-whitespace "org" ())
  40. (declare-function org-trim "org" (s))
  41. (declare-function org-get-indentation "org" (&optional line))
  42. (declare-function org-timer-item "org-timer" (&optional arg))
  43. (declare-function org-combine-plists "org" (&rest plists))
  44. (declare-function org-entry-get "org" (pom property &optional inherit))
  45. (declare-function org-narrow-to-subtree "org" ())
  46. (declare-function org-show-subtree "org" ())
  47. (defgroup org-plain-lists nil
  48. "Options concerning plain lists in Org-mode."
  49. :tag "Org Plain lists"
  50. :group 'org-structure)
  51. (defcustom org-cycle-include-plain-lists nil
  52. "Non-nil means, include plain lists into visibility cycling.
  53. This means that during cycling, plain list items will *temporarily* be
  54. interpreted as outline headlines with a level given by 1000+i where i is the
  55. indentation of the bullet. In all other operations, plain list items are
  56. not seen as headlines. For example, you cannot assign a TODO keyword to
  57. such an item."
  58. :group 'org-plain-lists
  59. :type 'boolean)
  60. (defcustom org-plain-list-ordered-item-terminator t
  61. "The character that makes a line with leading number an ordered list item.
  62. Valid values are ?. and ?\). To get both terminators, use t. While
  63. ?. may look nicer, it creates the danger that a line with leading
  64. number may be incorrectly interpreted as an item. ?\) therefore is
  65. the safe choice."
  66. :group 'org-plain-lists
  67. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  68. (const :tag "paren like in \"2)\"" ?\))
  69. (const :tab "both" t)))
  70. (defcustom org-list-two-spaces-after-bullet-regexp nil
  71. "A regular expression matching bullets that should have 2 spaces after them.
  72. When nil, no bullet will have two spaces after them.
  73. When a string, it will be used as a regular expression. When the bullet
  74. type of a list is changed, the new bullet type will be matched against this
  75. regexp. If it matches, there will be two spaces instead of one after
  76. the bullet in each item of he list."
  77. :group 'org-plain-list
  78. :type '(choice
  79. (const :tag "never" nil)
  80. (regexp)))
  81. (defcustom org-empty-line-terminates-plain-lists nil
  82. "Non-nil means, an empty line ends all plain list levels.
  83. When nil, empty lines are part of the preceding item."
  84. :group 'org-plain-lists
  85. :type 'boolean)
  86. (defcustom org-auto-renumber-ordered-lists t
  87. "Non-nil means, automatically renumber ordered plain lists.
  88. Renumbering happens when the sequence have been changed with
  89. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  90. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  91. :group 'org-plain-lists
  92. :type 'boolean)
  93. (defcustom org-provide-checkbox-statistics t
  94. "Non-nil means, update checkbox statistics after insert and toggle.
  95. When this is set, checkbox statistics is updated each time you either insert
  96. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  97. with \\[org-ctrl-c-ctrl-c\\]."
  98. :group 'org-plain-lists
  99. :type 'boolean)
  100. (defcustom org-hierarchical-checkbox-statistics t
  101. "Non-nil means, checkbox statistics counts only the state of direct children.
  102. When nil, all boxes below the cookie are counted."
  103. :group 'org-plain-lists
  104. :type 'boolean)
  105. (defcustom org-description-max-indent 20
  106. "Maximum indentation for the second line of a description list.
  107. When the indentation would be larger than this, it will become
  108. 5 characters instead."
  109. :group 'org-plain-lists
  110. :type 'integer)
  111. (defvar org-list-beginning-re
  112. "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) +\\(.*\\)$")
  113. (defcustom org-list-radio-list-templates
  114. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  115. % END RECEIVE ORGLST %n
  116. \\begin{comment}
  117. #+ORGLST: SEND %n org-list-to-latex
  118. | | |
  119. \\end{comment}\n")
  120. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  121. @c END RECEIVE ORGLST %n
  122. @ignore
  123. #+ORGLST: SEND %n org-list-to-texinfo
  124. | | |
  125. @end ignore\n")
  126. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  127. <!-- END RECEIVE ORGLST %n -->
  128. <!--
  129. #+ORGLST: SEND %n org-list-to-html
  130. | | |
  131. -->\n"))
  132. "Templates for radio lists in different major modes.
  133. All occurrences of %n in a template will be replaced with the name of the
  134. list, obtained by prompting the user."
  135. :group 'org-plain-lists
  136. :type '(repeat
  137. (list (symbol :tag "Major mode")
  138. (string :tag "Format"))))
  139. ;;;; Plain list items, including checkboxes
  140. ;;; Plain list items
  141. (defun org-at-item-p ()
  142. "Is point in a line starting a hand-formatted item?"
  143. (let ((llt org-plain-list-ordered-item-terminator))
  144. (save-excursion
  145. (goto-char (point-at-bol))
  146. (looking-at
  147. (cond
  148. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  149. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  150. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  151. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  152. (defun org-at-item-bullet-p ()
  153. "Is point at the bullet of a plain list item?"
  154. (and (org-at-item-p)
  155. (not (member (char-after) '(?\ ?\t)))
  156. (< (point) (match-end 0))))
  157. (defun org-in-item-p ()
  158. "It the cursor inside a plain list item.
  159. Does not have to be the first line."
  160. (save-excursion
  161. (condition-case nil
  162. (progn
  163. (org-beginning-of-item)
  164. (org-at-item-p)
  165. t)
  166. (error nil))))
  167. (defun org-insert-item (&optional checkbox)
  168. "Insert a new item at the current level.
  169. Return t when things worked, nil when we are not in an item."
  170. (when (save-excursion
  171. (condition-case nil
  172. (progn
  173. (org-beginning-of-item)
  174. (org-at-item-p)
  175. (if (org-invisible-p) (error "Invisible item"))
  176. t)
  177. (error nil)))
  178. (let* ((bul (match-string 0))
  179. (descp (save-excursion (goto-char (match-beginning 0))
  180. (beginning-of-line 1)
  181. (save-match-data
  182. (and (looking-at "[ \t]*\\(.*?\\) ::")
  183. (match-string 1)))))
  184. (empty-line-p (save-excursion
  185. (goto-char (match-beginning 0))
  186. (and (not (bobp))
  187. (or (beginning-of-line 0) t)
  188. (save-match-data
  189. (looking-at "[ \t]*$")))))
  190. (timerp (and descp
  191. (save-match-data
  192. (string-match "^[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+$"
  193. descp))))
  194. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  195. (match-end 0)))
  196. (blank-a (if org-empty-line-terminates-plain-lists
  197. nil
  198. (cdr (assq 'plain-list-item org-blank-before-new-entry))))
  199. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  200. pos)
  201. (if descp (setq checkbox nil))
  202. (if timerp
  203. (progn (org-timer-item) t)
  204. (cond
  205. ((and (org-at-item-p) (<= (point) eow))
  206. ;; before the bullet
  207. (beginning-of-line 1)
  208. (open-line (if blank 2 1)))
  209. ((<= (point) eow)
  210. (beginning-of-line 1))
  211. (t
  212. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  213. (end-of-line 1)
  214. (delete-horizontal-space))
  215. (newline (if blank 2 1))))
  216. (insert bul
  217. (if checkbox "[ ]" "")
  218. (if descp (concat (if checkbox " " "")
  219. (read-string "Term: ") " :: ") ""))
  220. (just-one-space)
  221. (setq pos (point))
  222. (end-of-line 1)
  223. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  224. (org-maybe-renumber-ordered-list)
  225. (and checkbox (org-update-checkbox-count-maybe))
  226. t)))
  227. ;;; Checkboxes
  228. (defun org-at-item-checkbox-p ()
  229. "Is point at a line starting a plain-list item with a checklet?"
  230. (and (org-at-item-p)
  231. (save-excursion
  232. (goto-char (match-end 0))
  233. (skip-chars-forward " \t")
  234. (looking-at "\\[[- X]\\]"))))
  235. (defun org-toggle-checkbox (&optional toggle-presence)
  236. "Toggle the checkbox in the current line.
  237. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
  238. With double prefix, set checkbox to [-].
  239. When there is an active region, toggle status or presence of the checkbox
  240. in the first line, and make every item in the region have the same
  241. status or presence, respectively.
  242. If the cursor is in a headline, apply this to all checkbox items in the
  243. text below the heading."
  244. (interactive "P")
  245. (catch 'exit
  246. (let (beg end status first-present first-status blocked)
  247. (cond
  248. ((org-region-active-p)
  249. (setq beg (region-beginning) end (region-end)))
  250. ((org-on-heading-p)
  251. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  252. ((org-at-item-checkbox-p)
  253. (save-excursion
  254. (if (equal toggle-presence '(4))
  255. (progn
  256. (replace-match "")
  257. (goto-char (match-beginning 0))
  258. (just-one-space))
  259. (when (setq blocked (org-checkbox-blocked-p))
  260. (error "Checkbox blocked because of unchecked box in line %d"
  261. blocked))
  262. (replace-match
  263. (cond ((equal toggle-presence '(16)) "[-]")
  264. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  265. (t "[ ]"))
  266. t t)))
  267. (throw 'exit t))
  268. ((org-at-item-p)
  269. ;; add a checkbox
  270. (save-excursion
  271. (goto-char (match-end 0))
  272. (insert "[ ] "))
  273. (throw 'exit t))
  274. (t (error "Not at a checkbox or heading, and no active region")))
  275. (setq end (move-marker (make-marker) end))
  276. (save-excursion
  277. (goto-char beg)
  278. (setq first-present (org-at-item-checkbox-p)
  279. first-status
  280. (save-excursion
  281. (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t)
  282. (equal (match-string 1) "[X]"))))
  283. (while (< (point) end)
  284. (if toggle-presence
  285. (cond
  286. ((and first-present (org-at-item-checkbox-p))
  287. (save-excursion
  288. (replace-match "")
  289. (goto-char (match-beginning 0))
  290. (just-one-space)))
  291. ((and (not first-present) (not (org-at-item-checkbox-p))
  292. (org-at-item-p))
  293. (save-excursion
  294. (goto-char (match-end 0))
  295. (insert "[ ] "))))
  296. (when (org-at-item-checkbox-p)
  297. (setq status (equal (match-string 0) "[X]"))
  298. (replace-match
  299. (if first-status "[ ]" "[X]") t t)))
  300. (beginning-of-line 2)))))
  301. (org-update-checkbox-count-maybe))
  302. (defun org-reset-checkbox-state-subtree ()
  303. "Reset all checkboxes in an entry subtree."
  304. (interactive "*")
  305. (save-restriction
  306. (save-excursion
  307. (org-narrow-to-subtree)
  308. (org-show-subtree)
  309. (goto-char (point-min))
  310. (let ((end (point-max)))
  311. (while (< (point) end)
  312. (when (org-at-item-checkbox-p)
  313. (replace-match "[ ]" t t))
  314. (beginning-of-line 2))))
  315. (org-update-checkbox-count-maybe)))
  316. (defun org-checkbox-blocked-p ()
  317. "Is the current checkbox blocked from for being checked now?
  318. A checkbox is blocked if all of the following conditions are fulfilled:
  319. 1. The checkbox is not checked already.
  320. 2. The current entry has the ORDERED property set.
  321. 3. There is an unchecked checkbox in this entry before the current line."
  322. (catch 'exit
  323. (save-match-data
  324. (save-excursion
  325. (unless (org-at-item-checkbox-p) (throw 'exit nil))
  326. (when (equal (match-string 0) "[X]")
  327. ;; the box is already checked!
  328. (throw 'exit nil))
  329. (let ((end (point-at-bol)))
  330. (condition-case nil (org-back-to-heading t)
  331. (error (throw 'exit nil)))
  332. (unless (org-entry-get nil "ORDERED") (throw 'exit nil))
  333. (if (re-search-forward "^[ \t]*[-+*0-9.)] \\[[- ]\\]" end t)
  334. (org-current-line)
  335. nil))))))
  336. (defvar org-checkbox-statistics-hook nil
  337. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  338. This hook runs even if `org-provide-checkbox-statistics' is nil, to it can
  339. be used to implement alternative ways of collecting statistics information.")
  340. (defun org-update-checkbox-count-maybe ()
  341. "Update checkbox statistics unless turned off by user."
  342. (when org-provide-checkbox-statistics
  343. (org-update-checkbox-count))
  344. (run-hooks 'org-checkbox-statistics-hook))
  345. (defun org-update-checkbox-count (&optional all)
  346. "Update the checkbox statistics in the current section.
  347. This will find all statistic cookies like [57%] and [6/12] and update them
  348. with the current numbers. With optional prefix argument ALL, do this for
  349. the whole buffer."
  350. (interactive "P")
  351. (save-excursion
  352. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  353. (beg (condition-case nil
  354. (progn (org-back-to-heading) (point))
  355. (error (point-min))))
  356. (end (move-marker (make-marker)
  357. (progn (outline-next-heading) (point))))
  358. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  359. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  360. (re-find (concat re "\\|" re-box))
  361. beg-cookie end-cookie is-percent c-on c-off lim
  362. eline curr-ind next-ind continue-from startsearch
  363. (recursive
  364. (or (not org-hierarchical-checkbox-statistics)
  365. (string-match "\\<recursive\\>"
  366. (or (org-entry-get nil "COOKIE_DATA") ""))))
  367. (cstat 0)
  368. )
  369. (when all
  370. (goto-char (point-min))
  371. (outline-next-heading)
  372. (setq beg (point) end (point-max)))
  373. (goto-char end)
  374. ;; find each statistics cookie
  375. (while (and (re-search-backward re-find beg t)
  376. (not (save-match-data
  377. (and (org-on-heading-p)
  378. (string-match "\\<todo\\>"
  379. (downcase
  380. (or (org-entry-get
  381. nil "COOKIE_DATA")
  382. "")))))))
  383. (setq beg-cookie (match-beginning 1)
  384. end-cookie (match-end 1)
  385. cstat (+ cstat (if end-cookie 1 0))
  386. startsearch (point-at-eol)
  387. continue-from (match-beginning 0)
  388. is-percent (match-beginning 2)
  389. lim (cond
  390. ((org-on-heading-p) (outline-next-heading) (point))
  391. ((org-at-item-p) (org-end-of-item) (point))
  392. (t nil))
  393. c-on 0
  394. c-off 0)
  395. (when lim
  396. ;; find first checkbox for this cookie and gather
  397. ;; statistics from all that are at this indentation level
  398. (goto-char startsearch)
  399. (if (re-search-forward re-box lim t)
  400. (progn
  401. (org-beginning-of-item)
  402. (setq curr-ind (org-get-indentation))
  403. (setq next-ind curr-ind)
  404. (while (and (bolp) (org-at-item-p)
  405. (if recursive
  406. (<= curr-ind next-ind)
  407. (= curr-ind next-ind)))
  408. (save-excursion (end-of-line) (setq eline (point)))
  409. (if (re-search-forward re-box eline t)
  410. (if (member (match-string 2) '("[ ]" "[-]"))
  411. (setq c-off (1+ c-off))
  412. (setq c-on (1+ c-on))
  413. )
  414. )
  415. (if (not recursive)
  416. (org-end-of-item)
  417. (end-of-line)
  418. (when (re-search-forward org-list-beginning-re lim t)
  419. (beginning-of-line)))
  420. (setq next-ind (org-get-indentation))
  421. )))
  422. (goto-char continue-from)
  423. ;; update cookie
  424. (when end-cookie
  425. (delete-region beg-cookie end-cookie)
  426. (goto-char beg-cookie)
  427. (insert
  428. (if is-percent
  429. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  430. (format "[%d/%d]" c-on (+ c-on c-off)))))
  431. ;; update items checkbox if it has one
  432. (when (org-at-item-p)
  433. (org-beginning-of-item)
  434. (when (and (> (+ c-on c-off) 0)
  435. (re-search-forward re-box (point-at-eol) t))
  436. (setq beg-cookie (match-beginning 2)
  437. end-cookie (match-end 2))
  438. (delete-region beg-cookie end-cookie)
  439. (goto-char beg-cookie)
  440. (cond ((= c-off 0) (insert "[X]"))
  441. ((= c-on 0) (insert "[ ]"))
  442. (t (insert "[-]")))
  443. )))
  444. (goto-char continue-from))
  445. (when (interactive-p)
  446. (message "Checkbox statistics updated %s (%d places)"
  447. (if all "in entire file" "in current outline entry") cstat)))))
  448. (defun org-get-checkbox-statistics-face ()
  449. "Select the face for checkbox statistics.
  450. The face will be `org-done' when all relevant boxes are checked. Otherwise
  451. it will be `org-todo'."
  452. (if (match-end 1)
  453. (if (equal (match-string 1) "100%")
  454. 'org-checkbox-statistics-done
  455. 'org-checkbox-statistics-todo)
  456. (if (and (> (match-end 2) (match-beginning 2))
  457. (equal (match-string 2) (match-string 3)))
  458. 'org-checkbox-statistics-done
  459. 'org-checkbox-statistics-todo)))
  460. (defun org-beginning-of-item ()
  461. "Go to the beginning of the current hand-formatted item.
  462. If the cursor is not in an item, throw an error."
  463. (interactive)
  464. (let ((pos (point))
  465. (limit (save-excursion
  466. (condition-case nil
  467. (progn
  468. (org-back-to-heading)
  469. (beginning-of-line 2) (point))
  470. (error (point-min)))))
  471. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  472. ind ind1)
  473. (if (org-at-item-p)
  474. (beginning-of-line 1)
  475. (beginning-of-line 1)
  476. (skip-chars-forward " \t")
  477. (setq ind (current-column))
  478. (if (catch 'exit
  479. (while t
  480. (beginning-of-line 0)
  481. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  482. (if (looking-at "[ \t]*$")
  483. (setq ind1 ind-empty)
  484. (skip-chars-forward " \t")
  485. (setq ind1 (current-column)))
  486. (if (< ind1 ind)
  487. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  488. nil
  489. (goto-char pos)
  490. (error "Not in an item")))))
  491. (defun org-end-of-item ()
  492. "Go to the end of the current hand-formatted item.
  493. If the cursor is not in an item, throw an error."
  494. (interactive)
  495. (let* ((pos (point))
  496. ind1
  497. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  498. (limit (save-excursion (outline-next-heading) (point)))
  499. (ind (save-excursion
  500. (org-beginning-of-item)
  501. (skip-chars-forward " \t")
  502. (current-column)))
  503. (end (catch 'exit
  504. (while t
  505. (beginning-of-line 2)
  506. (if (eobp) (throw 'exit (point)))
  507. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  508. (if (looking-at "[ \t]*$")
  509. (setq ind1 ind-empty)
  510. (skip-chars-forward " \t")
  511. (setq ind1 (current-column)))
  512. (if (<= ind1 ind)
  513. (throw 'exit (point-at-bol)))))))
  514. (if end
  515. (goto-char end)
  516. (goto-char pos)
  517. (error "Not in an item"))))
  518. (defun org-next-item ()
  519. "Move to the beginning of the next item in the current plain list.
  520. Error if not at a plain list, or if this is the last item in the list."
  521. (interactive)
  522. (let (ind ind1 (pos (point)))
  523. (org-beginning-of-item)
  524. (setq ind (org-get-indentation))
  525. (org-end-of-item)
  526. (setq ind1 (org-get-indentation))
  527. (unless (and (org-at-item-p) (= ind ind1))
  528. (goto-char pos)
  529. (error "On last item"))))
  530. (defun org-previous-item ()
  531. "Move to the beginning of the previous item in the current plain list.
  532. Error if not at a plain list, or if this is the first item in the list."
  533. (interactive)
  534. (let (beg ind ind1 (pos (point)))
  535. (org-beginning-of-item)
  536. (setq beg (point))
  537. (setq ind (org-get-indentation))
  538. (goto-char beg)
  539. (catch 'exit
  540. (while t
  541. (beginning-of-line 0)
  542. (if (looking-at "[ \t]*$")
  543. nil
  544. (if (<= (setq ind1 (org-get-indentation)) ind)
  545. (throw 'exit t)))))
  546. (condition-case nil
  547. (if (or (not (org-at-item-p))
  548. (< ind1 (1- ind)))
  549. (error "")
  550. (org-beginning-of-item))
  551. (error (goto-char pos)
  552. (error "On first item")))))
  553. (defun org-first-list-item-p ()
  554. "Is this heading the item in a plain list?"
  555. (unless (org-at-item-p)
  556. (error "Not at a plain list item"))
  557. (org-beginning-of-item)
  558. (= (point) (save-excursion (org-beginning-of-item-list))))
  559. (defun org-move-item-down ()
  560. "Move the plain list item at point down, i.e. swap with following item.
  561. Subitems (items with larger indentation) are considered part of the item,
  562. so this really moves item trees."
  563. (interactive)
  564. (let ((col (current-column))
  565. (pos (point))
  566. beg beg0 end end0 ind ind1 txt ne-end ne-beg)
  567. (org-beginning-of-item)
  568. (setq beg0 (point))
  569. (save-excursion
  570. (setq ne-beg (org-back-over-empty-lines))
  571. (setq beg (point)))
  572. (goto-char beg0)
  573. (setq ind (org-get-indentation))
  574. (org-end-of-item)
  575. (setq end0 (point))
  576. (setq ind1 (org-get-indentation))
  577. (setq ne-end (org-back-over-empty-lines))
  578. (setq end (point))
  579. (goto-char beg0)
  580. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  581. ;; include less whitespace
  582. (save-excursion
  583. (goto-char beg)
  584. (forward-line (- ne-beg ne-end))
  585. (setq beg (point))))
  586. (goto-char end0)
  587. (if (and (org-at-item-p) (= ind ind1))
  588. (progn
  589. (org-end-of-item)
  590. (org-back-over-empty-lines)
  591. (setq txt (buffer-substring beg end))
  592. (save-excursion
  593. (delete-region beg end))
  594. (setq pos (point))
  595. (insert txt)
  596. (goto-char pos) (org-skip-whitespace)
  597. (org-maybe-renumber-ordered-list)
  598. (move-to-column col))
  599. (goto-char pos)
  600. (move-to-column col)
  601. (error "Cannot move this item further down"))))
  602. (defun org-move-item-up (arg)
  603. "Move the plain list item at point up, i.e. swap with previous item.
  604. Subitems (items with larger indentation) are considered part of the item,
  605. so this really moves item trees."
  606. (interactive "p")
  607. (let ((col (current-column)) (pos (point))
  608. beg beg0 end ind ind1 txt
  609. ne-beg ne-ins ins-end)
  610. (org-beginning-of-item)
  611. (setq beg0 (point))
  612. (setq ind (org-get-indentation))
  613. (save-excursion
  614. (setq ne-beg (org-back-over-empty-lines))
  615. (setq beg (point)))
  616. (goto-char beg0)
  617. (org-end-of-item)
  618. (org-back-over-empty-lines)
  619. (setq end (point))
  620. (goto-char beg0)
  621. (catch 'exit
  622. (while t
  623. (beginning-of-line 0)
  624. (if (looking-at "[ \t]*$")
  625. (if org-empty-line-terminates-plain-lists
  626. (progn
  627. (goto-char pos)
  628. (error "Cannot move this item further up"))
  629. nil)
  630. (if (<= (setq ind1 (org-get-indentation)) ind)
  631. (throw 'exit t)))))
  632. (condition-case nil
  633. (org-beginning-of-item)
  634. (error (goto-char beg0)
  635. (move-to-column col)
  636. (error "Cannot move this item further up")))
  637. (setq ind1 (org-get-indentation))
  638. (if (and (org-at-item-p) (= ind ind1))
  639. (progn
  640. (setq ne-ins (org-back-over-empty-lines))
  641. (setq txt (buffer-substring beg end))
  642. (save-excursion
  643. (delete-region beg end))
  644. (setq pos (point))
  645. (insert txt)
  646. (setq ins-end (point))
  647. (goto-char pos) (org-skip-whitespace)
  648. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  649. ;; Move whitespace back to beginning
  650. (save-excursion
  651. (goto-char ins-end)
  652. (let ((kill-whole-line t))
  653. (kill-line (- ne-ins ne-beg)) (point)))
  654. (insert (make-string (- ne-ins ne-beg) ?\n)))
  655. (org-maybe-renumber-ordered-list)
  656. (move-to-column col))
  657. (goto-char pos)
  658. (move-to-column col)
  659. (error "Cannot move this item further up"))))
  660. (defun org-maybe-renumber-ordered-list ()
  661. "Renumber the ordered list at point if setup allows it.
  662. This tests the user option `org-auto-renumber-ordered-lists' before
  663. doing the renumbering."
  664. (interactive)
  665. (when (and org-auto-renumber-ordered-lists
  666. (org-at-item-p))
  667. (if (match-beginning 3)
  668. (org-renumber-ordered-list 1)
  669. (org-fix-bullet-type))))
  670. (defun org-maybe-renumber-ordered-list-safe ()
  671. (condition-case nil
  672. (save-excursion
  673. (org-maybe-renumber-ordered-list))
  674. (error nil)))
  675. (defun org-cycle-list-bullet (&optional which)
  676. "Cycle through the different itemize/enumerate bullets.
  677. This cycle the entire list level through the sequence:
  678. `-' -> `+' -> `*' -> `1.' -> `1)'
  679. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  680. 0 means `-', 1 means `+' etc."
  681. (interactive "P")
  682. (org-preserve-lc
  683. (org-beginning-of-item-list)
  684. (org-at-item-p)
  685. (beginning-of-line 1)
  686. (let ((current (match-string 0))
  687. (prevp (eq which 'previous))
  688. new old)
  689. (setq new (cond
  690. ((and (numberp which)
  691. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  692. ((string-match "-" current) (if prevp "1)" "+"))
  693. ((string-match "\\+" current)
  694. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  695. ((string-match "\\*" current) (if prevp "+" "1."))
  696. ((string-match "\\." current)
  697. (if prevp (if (looking-at "\\S-") "+" "*") "1)"))
  698. ((string-match ")" current) (if prevp "1." "-"))
  699. (t (error "This should not happen"))))
  700. (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
  701. (setq old (match-string 2))
  702. (replace-match (concat "\\1" new)))
  703. (org-shift-item-indentation (- (length new) (length old)))
  704. (org-fix-bullet-type)
  705. (org-maybe-renumber-ordered-list))))
  706. (defun org-get-string-indentation (s)
  707. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  708. (let ((n -1) (i 0) (w tab-width) c)
  709. (catch 'exit
  710. (while (< (setq n (1+ n)) (length s))
  711. (setq c (aref s n))
  712. (cond ((= c ?\ ) (setq i (1+ i)))
  713. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  714. (t (throw 'exit t)))))
  715. i))
  716. (defun org-renumber-ordered-list (arg)
  717. "Renumber an ordered plain list.
  718. Cursor needs to be in the first line of an item, the line that starts
  719. with something like \"1.\" or \"2)\"."
  720. (interactive "p")
  721. (unless (and (org-at-item-p)
  722. (match-beginning 3))
  723. (error "This is not an ordered list"))
  724. (let ((line (org-current-line))
  725. (col (current-column))
  726. (ind (org-get-string-indentation
  727. (buffer-substring (point-at-bol) (match-beginning 3))))
  728. ;; (term (substring (match-string 3) -1))
  729. ind1 (n (1- arg))
  730. fmt bobp old new)
  731. ;; find where this list begins
  732. (org-beginning-of-item-list)
  733. (setq bobp (bobp))
  734. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  735. (setq fmt (concat "%d" (or (match-string 1) ".")))
  736. (beginning-of-line 0)
  737. ;; walk forward and replace these numbers
  738. (catch 'exit
  739. (while t
  740. (catch 'next
  741. (if bobp (setq bobp nil) (beginning-of-line 2))
  742. (if (eobp) (throw 'exit nil))
  743. (if (looking-at "[ \t]*$") (throw 'next nil))
  744. (skip-chars-forward " \t") (setq ind1 (current-column))
  745. (if (> ind1 ind) (throw 'next t))
  746. (if (< ind1 ind) (throw 'exit t))
  747. (if (not (org-at-item-p)) (throw 'exit nil))
  748. (setq old (match-string 2))
  749. (delete-region (match-beginning 2) (match-end 2))
  750. (goto-char (match-beginning 2))
  751. (insert (setq new (format fmt (setq n (1+ n)))))
  752. (org-shift-item-indentation (- (length new) (length old))))))
  753. (goto-line line)
  754. (org-move-to-column col)))
  755. (defun org-fix-bullet-type ()
  756. "Make sure all items in this list have the same bullet as the first item.
  757. Also, fix the indentation."
  758. (interactive)
  759. (unless (org-at-item-p) (error "This is not a list"))
  760. (let ((line (org-current-line))
  761. (col (current-column))
  762. (ind (current-indentation))
  763. ind1 bullet oldbullet)
  764. ;; find where this list begins
  765. (org-beginning-of-item-list)
  766. (beginning-of-line 1)
  767. ;; find out what the bullet type is
  768. (looking-at "[ \t]*\\(\\S-+\\)")
  769. (setq bullet (concat (match-string 1) " "))
  770. (if (and org-list-two-spaces-after-bullet-regexp
  771. (string-match org-list-two-spaces-after-bullet-regexp bullet))
  772. (setq bullet (concat bullet " ")))
  773. ;; walk forward and replace these numbers
  774. (beginning-of-line 0)
  775. (catch 'exit
  776. (while t
  777. (catch 'next
  778. (beginning-of-line 2)
  779. (if (eobp) (throw 'exit nil))
  780. (if (looking-at "[ \t]*$") (throw 'next nil))
  781. (skip-chars-forward " \t") (setq ind1 (current-column))
  782. (if (> ind1 ind) (throw 'next t))
  783. (if (< ind1 ind) (throw 'exit t))
  784. (if (not (org-at-item-p)) (throw 'exit nil))
  785. (skip-chars-forward " \t")
  786. (looking-at "\\S-+ *")
  787. (setq oldbullet (match-string 0))
  788. (unless (equal bullet oldbullet) (replace-match bullet))
  789. (org-shift-item-indentation (- (length bullet) (length oldbullet))))))
  790. (goto-line line)
  791. (org-move-to-column col)
  792. (if (string-match "[0-9]" bullet)
  793. (org-renumber-ordered-list 1))))
  794. (defun org-shift-item-indentation (delta)
  795. "Shift the indentation in current item by DELTA."
  796. (save-excursion
  797. (let ((beg (point-at-bol))
  798. (end (progn (org-end-of-item) (point)))
  799. i)
  800. (goto-char end)
  801. (beginning-of-line 0)
  802. (while (> (point) beg)
  803. (when (looking-at "[ \t]*\\S-")
  804. ;; this is not an empty line
  805. (setq i (org-get-indentation))
  806. (if (and (> i 0) (> (setq i (+ i delta)) 0))
  807. (indent-line-to i)))
  808. (beginning-of-line 0)))))
  809. (defun org-beginning-of-item-list ()
  810. "Go to the beginning of the current item list.
  811. I.e. to the first item in this list."
  812. (interactive)
  813. (org-beginning-of-item)
  814. (let ((pos (point-at-bol))
  815. (ind (org-get-indentation))
  816. ind1)
  817. ;; find where this list begins
  818. (catch 'exit
  819. (while t
  820. (catch 'next
  821. (beginning-of-line 0)
  822. (if (looking-at "[ \t]*$")
  823. (throw (if (bobp) 'exit 'next) t))
  824. (skip-chars-forward " \t") (setq ind1 (current-column))
  825. (if (or (< ind1 ind)
  826. (and (= ind1 ind)
  827. (not (org-at-item-p)))
  828. (and (= (point-at-bol) (point-min))
  829. (setq pos (point-min))))
  830. (throw 'exit t)
  831. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  832. (goto-char pos)))
  833. (defun org-end-of-item-list ()
  834. "Go to the end of the current item list.
  835. I.e. to the text after the last item."
  836. (interactive)
  837. (org-beginning-of-item)
  838. (let ((pos (point-at-bol))
  839. (ind (org-get-indentation))
  840. ind1)
  841. ;; find where this list begins
  842. (catch 'exit
  843. (while t
  844. (catch 'next
  845. (beginning-of-line 2)
  846. (if (looking-at "[ \t]*$")
  847. (if (eobp)
  848. (progn (setq pos (point)) (throw 'exit t))
  849. (throw 'next t)))
  850. (skip-chars-forward " \t") (setq ind1 (current-column))
  851. (if (or (< ind1 ind)
  852. (and (= ind1 ind)
  853. (not (org-at-item-p)))
  854. (eobp))
  855. (progn
  856. (setq pos (point-at-bol))
  857. (throw 'exit t))))))
  858. (goto-char pos)))
  859. (defvar org-last-indent-begin-marker (make-marker))
  860. (defvar org-last-indent-end-marker (make-marker))
  861. (defun org-outdent-item (arg)
  862. "Outdent a local list item."
  863. (interactive "p")
  864. (org-indent-item (- arg)))
  865. (defun org-indent-item (arg)
  866. "Indent a local list item."
  867. (interactive "p")
  868. (and (org-region-active-p) (org-cursor-to-region-beginning))
  869. (unless (org-at-item-p)
  870. (error "Not on an item"))
  871. (save-excursion
  872. (let (beg end ind ind1 tmp delta ind-down ind-up)
  873. (setq end (and (org-region-active-p) (region-end)))
  874. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  875. (setq beg org-last-indent-begin-marker
  876. end org-last-indent-end-marker)
  877. (org-beginning-of-item)
  878. (setq beg (move-marker org-last-indent-begin-marker (point)))
  879. (org-end-of-item)
  880. (setq end (move-marker org-last-indent-end-marker (or end (point)))))
  881. (goto-char beg)
  882. (setq tmp (org-item-indent-positions)
  883. ind (car tmp)
  884. ind-down (nth 2 tmp)
  885. ind-up (nth 1 tmp)
  886. delta (if (> arg 0)
  887. (if ind-down (- ind-down ind) 2)
  888. (if ind-up (- ind-up ind) -2)))
  889. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  890. (while (< (point) end)
  891. (beginning-of-line 1)
  892. (skip-chars-forward " \t") (setq ind1 (current-column))
  893. (delete-region (point-at-bol) (point))
  894. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  895. (beginning-of-line 2))))
  896. (org-fix-bullet-type)
  897. (org-maybe-renumber-ordered-list-safe)
  898. (save-excursion
  899. (beginning-of-line 0)
  900. (condition-case nil (org-beginning-of-item) (error nil))
  901. (org-maybe-renumber-ordered-list-safe)))
  902. (defun org-item-indent-positions ()
  903. "Return indentation for plain list items.
  904. This returns a list with three values: The current indentation, the
  905. parent indentation and the indentation a child should have.
  906. Assumes cursor in item line."
  907. (let* ((bolpos (point-at-bol))
  908. (ind (org-get-indentation))
  909. ind-down ind-up pos)
  910. (save-excursion
  911. (org-beginning-of-item-list)
  912. (skip-chars-backward "\n\r \t")
  913. (when (org-in-item-p)
  914. (org-beginning-of-item)
  915. (setq ind-up (org-get-indentation))))
  916. (setq pos (point))
  917. (save-excursion
  918. (cond
  919. ((and (condition-case nil (progn (org-previous-item) t)
  920. (error nil))
  921. (or (forward-char 1) t)
  922. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  923. (setq ind-down (org-get-indentation)))
  924. ((and (goto-char pos)
  925. (org-at-item-p))
  926. (goto-char (match-end 0))
  927. (skip-chars-forward " \t")
  928. (setq ind-down (current-column)))))
  929. (list ind ind-up ind-down)))
  930. ;;; Send and receive lists
  931. (defun org-list-parse-list (&optional delete)
  932. "Parse the list at point and maybe DELETE it.
  933. Return a list containing first level items as strings and
  934. sublevels as a list of strings."
  935. (let* ((item-beginning (org-list-item-beginning))
  936. (start (car item-beginning))
  937. (end (org-list-end (cdr item-beginning)))
  938. output itemsep ltype)
  939. (while (re-search-forward org-list-beginning-re end t)
  940. (goto-char (match-beginning 3))
  941. (save-match-data
  942. (cond ((string-match "[0-9]" (match-string 2))
  943. (setq itemsep "[0-9]+\\(?:\\.\\|)\\)"
  944. ltype 'ordered))
  945. ((string-match "^.*::" (match-string 0))
  946. (setq itemsep "[-+]" ltype 'descriptive))
  947. (t (setq itemsep "[-+]" ltype 'unordered))))
  948. (let* ((indent1 (match-string 1))
  949. (nextitem (save-excursion
  950. (save-match-data
  951. (or (and (re-search-forward
  952. (concat "^" indent1 itemsep " *?") end t)
  953. (match-beginning 0)) end))))
  954. (item (buffer-substring
  955. (point)
  956. (or (and (re-search-forward
  957. org-list-beginning-re end t)
  958. (goto-char (match-beginning 0)))
  959. (goto-char end))))
  960. (nextindent (match-string 1))
  961. (item (org-trim item))
  962. (item (if (string-match "^\\[\\([xX ]\\)\\]" item)
  963. (replace-match (if (equal (match-string 1 item) " ")
  964. "[CBOFF]"
  965. "[CBON]")
  966. t nil item)
  967. item)))
  968. (push item output)
  969. (when (> (length nextindent)
  970. (length indent1))
  971. (narrow-to-region (point) nextitem)
  972. (push (org-list-parse-list) output)
  973. (widen))))
  974. (when delete (delete-region start end))
  975. (setq output (nreverse output))
  976. (push ltype output)))
  977. (defun org-list-item-beginning ()
  978. "Find the beginning of the list item.
  979. Return a cons which car is the beginning position of the item and
  980. cdr is the indentation string."
  981. (save-excursion
  982. (if (not (or (looking-at org-list-beginning-re)
  983. (re-search-backward
  984. org-list-beginning-re nil t)))
  985. (progn (goto-char (point-min)) (point))
  986. (cons (match-beginning 0) (match-string 1)))))
  987. (defun org-list-end (indent)
  988. "Return the position of the end of the list.
  989. INDENT is the indentation of the list, as a string."
  990. (save-excursion
  991. (catch 'exit
  992. (while (or (looking-at org-list-beginning-re)
  993. (looking-at (concat "^" indent "[ \t]+\\|^$"))
  994. (> (or (get-text-property (point) 'original-indentation) -1)
  995. (length indent)))
  996. (if (eq (point) (point-max))
  997. (throw 'exit (point-max)))
  998. (forward-line 1)))
  999. (point)))
  1000. (defun org-list-insert-radio-list ()
  1001. "Insert a radio list template appropriate for this major mode."
  1002. (interactive)
  1003. (let* ((e (assq major-mode org-list-radio-list-templates))
  1004. (txt (nth 1 e))
  1005. name pos)
  1006. (unless e (error "No radio list setup defined for %s" major-mode))
  1007. (setq name (read-string "List name: "))
  1008. (while (string-match "%n" txt)
  1009. (setq txt (replace-match name t t txt)))
  1010. (or (bolp) (insert "\n"))
  1011. (setq pos (point))
  1012. (insert txt)
  1013. (goto-char pos)))
  1014. (defun org-list-send-list (&optional maybe)
  1015. "Send a tranformed version of this list to the receiver position.
  1016. With argument MAYBE, fail quietly if no transformation is defined for
  1017. this list."
  1018. (interactive)
  1019. (catch 'exit
  1020. (unless (org-at-item-p) (error "Not at a list"))
  1021. (save-excursion
  1022. (goto-char (car (org-list-item-beginning)))
  1023. (beginning-of-line 0)
  1024. (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  1025. (if maybe
  1026. (throw 'exit nil)
  1027. (error "Don't know how to transform this list"))))
  1028. (let* ((name (match-string 1))
  1029. (item-beginning (org-list-item-beginning))
  1030. (transform (intern (match-string 2)))
  1031. (txt (buffer-substring-no-properties
  1032. (car item-beginning)
  1033. (org-list-end (cdr item-beginning))))
  1034. (list (org-list-parse-list))
  1035. beg)
  1036. (unless (fboundp transform)
  1037. (error "No such transformation function %s" transform))
  1038. (setq txt (funcall transform list))
  1039. ;; Find the insertion place
  1040. (save-excursion
  1041. (goto-char (point-min))
  1042. (unless (re-search-forward
  1043. (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
  1044. (error "Don't know where to insert translated list"))
  1045. (goto-char (match-beginning 0))
  1046. (beginning-of-line 2)
  1047. (setq beg (point))
  1048. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  1049. (error "Cannot find end of insertion region"))
  1050. (beginning-of-line 1)
  1051. (delete-region beg (point))
  1052. (goto-char beg)
  1053. (insert txt "\n"))
  1054. (message "List converted and installed at receiver location"))))
  1055. (defun org-list-to-generic (list params)
  1056. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  1057. Valid parameters PARAMS are
  1058. :ustart String to start an unordered list
  1059. :uend String to end an unordered list
  1060. :ostart String to start an ordered list
  1061. :oend String to end an ordered list
  1062. :dstart String to start a descriptive list
  1063. :dend String to end a descriptive list
  1064. :dtstart String to start a descriptive term
  1065. :dtend String to end a descriptive term
  1066. :ddstart String to start a description
  1067. :ddend String to end a description
  1068. :splice When set to t, return only list body lines, don't wrap
  1069. them into :[u/o]start and :[u/o]end. Default is nil.
  1070. :istart String to start a list item
  1071. :iend String to end a list item
  1072. :isep String to separate items
  1073. :lsep String to separate sublists
  1074. :cboff String to insert for an unchecked checkbox
  1075. :cbon String to insert for a checked checkbox"
  1076. (interactive)
  1077. (let* ((p params) sublist
  1078. (splicep (plist-get p :splice))
  1079. (ostart (plist-get p :ostart))
  1080. (oend (plist-get p :oend))
  1081. (ustart (plist-get p :ustart))
  1082. (uend (plist-get p :uend))
  1083. (dstart (plist-get p :dstart))
  1084. (dend (plist-get p :dend))
  1085. (dtstart (plist-get p :dtstart))
  1086. (dtend (plist-get p :dtend))
  1087. (ddstart (plist-get p :ddstart))
  1088. (ddend (plist-get p :ddend))
  1089. (istart (plist-get p :istart))
  1090. (iend (plist-get p :iend))
  1091. (isep (plist-get p :isep))
  1092. (lsep (plist-get p :lsep))
  1093. (cbon (plist-get p :cbon))
  1094. (cboff (plist-get p :cboff)))
  1095. (let ((wrapper
  1096. (cond ((eq (car list) 'ordered)
  1097. (concat ostart "\n%s" oend "\n"))
  1098. ((eq (car list) 'unordered)
  1099. (concat ustart "\n%s" uend "\n"))
  1100. ((eq (car list) 'descriptive)
  1101. (concat dstart "\n%s" dend "\n"))))
  1102. rtn term defstart defend)
  1103. (while (setq sublist (pop list))
  1104. (cond ((symbolp sublist) nil)
  1105. ((stringp sublist)
  1106. (when (string-match "^\\(.*\\) ::" sublist)
  1107. (setq term (org-trim (format (concat dtstart "%s" dtend)
  1108. (match-string 1 sublist))))
  1109. (setq sublist (substring sublist (1+ (length term)))))
  1110. (if (string-match "\\[CBON\\]" sublist)
  1111. (setq sublist (replace-match cbon t t sublist)))
  1112. (if (string-match "\\[CBOFF\\]" sublist)
  1113. (setq sublist (replace-match cboff t t sublist)))
  1114. (setq rtn (concat rtn istart term ddstart
  1115. sublist ddend iend isep)))
  1116. (t (setq rtn (concat rtn ;; previous list
  1117. lsep ;; list separator
  1118. (org-list-to-generic sublist p)
  1119. lsep ;; list separator
  1120. )))))
  1121. (format wrapper rtn))))
  1122. (defun org-list-to-latex (list &optional params)
  1123. "Convert LIST into a LaTeX list.
  1124. LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
  1125. with overruling parameters for `org-list-to-generic'."
  1126. (org-list-to-generic
  1127. list
  1128. (org-combine-plists
  1129. '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  1130. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  1131. :dstart "\\begin{description}" :dend "\\end{description}"
  1132. :dtstart "[" :dtend "]"
  1133. :ddstart "" :ddend ""
  1134. :istart "\\item " :iend ""
  1135. :isep "\n" :lsep "\n"
  1136. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  1137. params)))
  1138. (defun org-list-to-html (list &optional params)
  1139. "Convert LIST into a HTML list.
  1140. LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
  1141. with overruling parameters for `org-list-to-generic'."
  1142. (org-list-to-generic
  1143. list
  1144. (org-combine-plists
  1145. '(:splicep nil :ostart "<ol>" :oend "</ol>"
  1146. :ustart "<ul>" :uend "</ul>"
  1147. :dstart "<dl>" :dend "</dl>"
  1148. :dtstart "<dt>" :dtend "</dt>"
  1149. :ddstart "<dd>" :ddend "</dd>"
  1150. :istart "<li>" :iend "</li>"
  1151. :isep "\n" :lsep "\n"
  1152. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
  1153. params)))
  1154. (defun org-list-to-texinfo (list &optional params)
  1155. "Convert LIST into a Texinfo list.
  1156. LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
  1157. with overruling parameters for `org-list-to-generic'."
  1158. (org-list-to-generic
  1159. list
  1160. (org-combine-plists
  1161. '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  1162. :ustart "@enumerate" :uend "@end enumerate"
  1163. :dstart "@table" :dend "@end table"
  1164. :dtstart "@item " :dtend "\n"
  1165. :ddstart "" :ddend ""
  1166. :istart "@item\n" :iend ""
  1167. :isep "\n" :lsep "\n"
  1168. :cbon "@code{[X]}" :cboff "@code{[ ]}")
  1169. params)))
  1170. (provide 'org-list)
  1171. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  1172. ;;; org-list.el ends here