org-list.el 46 KB

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