org-list.el 48 KB

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