org-list.el 43 KB

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