org-list.el 48 KB

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