org-list.el 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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.32trans
  11. ;;
  12. ;; This file is part of GNU Emacs.
  13. ;;
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;; This file contains the code dealing with plain lists in Org-mode.
  28. ;;; Code:
  29. (require 'org-macs)
  30. (require 'org-compat)
  31. (defvar org-blank-before-new-entry)
  32. (defvar org-M-RET-may-split-line)
  33. (defvar org-complex-heading-regexp)
  34. (defvar org-odd-levels-only)
  35. (declare-function org-invisible-p "org" ())
  36. (declare-function org-on-heading-p "org" (&optional invisible-ok))
  37. (declare-function outline-next-heading "outline" ())
  38. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  39. (declare-function org-back-over-empty-lines "org" ())
  40. (declare-function org-skip-whitespace "org" ())
  41. (declare-function org-trim "org" (s))
  42. (declare-function org-get-indentation "org" (&optional line))
  43. (declare-function org-timer-item "org-timer" (&optional arg))
  44. (declare-function org-combine-plists "org" (&rest plists))
  45. (declare-function org-entry-get "org" (pom property &optional inherit))
  46. (declare-function org-narrow-to-subtree "org" ())
  47. (declare-function org-show-subtree "org" ())
  48. (defgroup org-plain-lists nil
  49. "Options concerning plain lists in Org-mode."
  50. :tag "Org Plain lists"
  51. :group 'org-structure)
  52. (defcustom org-cycle-include-plain-lists t
  53. "When t, make TAB cycle visibility on plain list items.
  54. Cycling plain lists works only when the cursor is on a plain list
  55. item. When the cursor is on an outline heading, plain lists are
  56. treated as text. This is the most stable way of handling this,
  57. which is why it is the default.
  58. When this is the symbol `integrate', then during cycling, plain
  59. list items will *temporarily* be interpreted as outline headlines
  60. with a level given by 1000+i where i is the indentation of the
  61. bullet. This setting can lead to strange effects when switching
  62. visibility to `children', because the first \"child\" in a
  63. subtree decides what children should be listed. If that first
  64. \"child\" is a plain list item with an implied large level
  65. number, all true children and grand children of the outline
  66. heading will be exposed in a children' view."
  67. :group 'org-plain-lists
  68. :type '(choice
  69. (const :tag "Never" nil)
  70. (const :tag "With cursor in plain list (recommended)" t)
  71. (const :tag "As children of outline headings" integrate)))
  72. (defcustom org-list-demote-modify-bullet nil
  73. "Default bullet type installed when demoting an item.
  74. This is an association list, for each bullet type, this alist will point
  75. to the bulled that should be used when this item is demoted."
  76. :group 'org-plain-lists
  77. :type '(repeat
  78. (cons
  79. (choice :tag "If the current bullet is "
  80. (const "-")
  81. (const "+")
  82. (const "*")
  83. (const "1.")
  84. (const "1)"))
  85. (choice :tag "demotion will change it to"
  86. (const "-")
  87. (const "+")
  88. (const "*")
  89. (const "1.")
  90. (const "1)")))))
  91. (defcustom org-plain-list-ordered-item-terminator t
  92. "The character that makes a line with leading number an ordered list item.
  93. Valid values are ?. and ?\). To get both terminators, use t. While
  94. ?. may look nicer, it creates the danger that a line with leading
  95. number may be incorrectly interpreted as an item. ?\) therefore is
  96. the safe choice."
  97. :group 'org-plain-lists
  98. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  99. (const :tag "paren like in \"2)\"" ?\))
  100. (const :tab "both" t)))
  101. (defcustom org-list-two-spaces-after-bullet-regexp nil
  102. "A regular expression matching bullets that should have 2 spaces after them.
  103. When nil, no bullet will have two spaces after them.
  104. When a string, it will be used as a regular expression. When the bullet
  105. type of a list is changed, the new bullet type will be matched against this
  106. regexp. If it matches, there will be two spaces instead of one after
  107. the bullet in each item of he list."
  108. :group 'org-plain-list
  109. :type '(choice
  110. (const :tag "never" nil)
  111. (regexp)))
  112. (defcustom org-empty-line-terminates-plain-lists nil
  113. "Non-nil means, an empty line ends all plain list levels.
  114. When nil, empty lines are part of the preceding item."
  115. :group 'org-plain-lists
  116. :type 'boolean)
  117. (defcustom org-auto-renumber-ordered-lists t
  118. "Non-nil means, automatically renumber ordered plain lists.
  119. Renumbering happens when the sequence have been changed with
  120. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  121. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  122. :group 'org-plain-lists
  123. :type 'boolean)
  124. (defcustom org-provide-checkbox-statistics t
  125. "Non-nil means, update checkbox statistics after insert and toggle.
  126. When this is set, checkbox statistics is updated each time you
  127. either insert a new checkbox with \\[org-insert-todo-heading] or
  128. toggle a checkbox with \\[org-ctrl-c-ctrl-c]."
  129. :group 'org-plain-lists
  130. :type 'boolean)
  131. (defcustom org-hierarchical-checkbox-statistics t
  132. "Non-nil means, checkbox statistics counts only the state of direct children.
  133. When nil, all boxes below the cookie are counted.
  134. This can be set to nil on a per-node basis using a COCKIE_DATA property
  135. with the word \"recursive\" in the value."
  136. :group 'org-plain-lists
  137. :type 'boolean)
  138. (defcustom org-description-max-indent 20
  139. "Maximum indentation for the second line of a description list.
  140. When the indentation would be larger than this, it will become
  141. 5 characters instead."
  142. :group 'org-plain-lists
  143. :type 'integer)
  144. (defvar org-list-beginning-re
  145. "^\\([ \t]*\\)\\([-+]\\|[0-9]+[.)]\\) +\\(.*\\)$")
  146. (defcustom org-list-radio-list-templates
  147. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  148. % END RECEIVE ORGLST %n
  149. \\begin{comment}
  150. #+ORGLST: SEND %n org-list-to-latex
  151. | | |
  152. \\end{comment}\n")
  153. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  154. @c END RECEIVE ORGLST %n
  155. @ignore
  156. #+ORGLST: SEND %n org-list-to-texinfo
  157. | | |
  158. @end ignore\n")
  159. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  160. <!-- END RECEIVE ORGLST %n -->
  161. <!--
  162. #+ORGLST: SEND %n org-list-to-html
  163. | | |
  164. -->\n"))
  165. "Templates for radio lists in different major modes.
  166. All occurrences of %n in a template will be replaced with the name of the
  167. list, obtained by prompting the user."
  168. :group 'org-plain-lists
  169. :type '(repeat
  170. (list (symbol :tag "Major mode")
  171. (string :tag "Format"))))
  172. ;;;; Plain list items, including checkboxes
  173. ;;; Plain list items
  174. (defun org-at-item-p ()
  175. "Is point in a line starting a hand-formatted item?"
  176. (let ((llt org-plain-list-ordered-item-terminator))
  177. (save-excursion
  178. (goto-char (point-at-bol))
  179. (looking-at
  180. (cond
  181. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  182. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  183. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  184. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  185. (defun org-at-item-bullet-p ()
  186. "Is point at the bullet of a plain list item?"
  187. (and (org-at-item-p)
  188. (not (member (char-after) '(?\ ?\t)))
  189. (< (point) (match-end 0))))
  190. (defun org-in-item-p ()
  191. "It the cursor inside a plain list item.
  192. Does not have to be the first line."
  193. (save-excursion
  194. (condition-case nil
  195. (progn
  196. (org-beginning-of-item)
  197. (org-at-item-p)
  198. t)
  199. (error nil))))
  200. (defun org-insert-item (&optional checkbox)
  201. "Insert a new item at the current level.
  202. Return t when things worked, nil when we are not in an item."
  203. (when (save-excursion
  204. (condition-case nil
  205. (progn
  206. (org-beginning-of-item)
  207. (org-at-item-p)
  208. (if (org-invisible-p) (error "Invisible item"))
  209. t)
  210. (error nil)))
  211. (let* ((bul (match-string 0))
  212. (descp (save-excursion (goto-char (match-beginning 0))
  213. (beginning-of-line 1)
  214. (save-match-data
  215. (and (looking-at "[ \t]*\\(.*?\\) ::")
  216. (match-string 1)))))
  217. (empty-line-p (save-excursion
  218. (goto-char (match-beginning 0))
  219. (and (not (bobp))
  220. (or (beginning-of-line 0) t)
  221. (save-match-data
  222. (looking-at "[ \t]*$")))))
  223. (timerp (and descp
  224. (save-match-data
  225. (string-match "^[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+$"
  226. descp))))
  227. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  228. (match-end 0)))
  229. (blank-a (if org-empty-line-terminates-plain-lists
  230. nil
  231. (cdr (assq 'plain-list-item org-blank-before-new-entry))))
  232. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  233. pos)
  234. (if descp (setq checkbox nil))
  235. (if timerp
  236. (progn (org-timer-item) t)
  237. (cond
  238. ((and (org-at-item-p) (<= (point) eow))
  239. ;; before the bullet
  240. (beginning-of-line 1)
  241. (open-line (if blank 2 1)))
  242. ((<= (point) eow)
  243. (beginning-of-line 1))
  244. (t
  245. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  246. (end-of-line 1)
  247. (delete-horizontal-space))
  248. (newline (if blank 2 1))))
  249. (insert bul
  250. (if checkbox "[ ]" "")
  251. (if descp (concat (if checkbox " " "")
  252. (read-string "Term: ") " :: ") ""))
  253. (just-one-space)
  254. (setq pos (point))
  255. (end-of-line 1)
  256. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  257. (org-maybe-renumber-ordered-list)
  258. (and checkbox (org-update-checkbox-count-maybe))
  259. t)))
  260. ;;; Checkboxes
  261. (defun org-at-item-checkbox-p ()
  262. "Is point at a line starting a plain-list item with a checklet?"
  263. (and (org-at-item-p)
  264. (save-excursion
  265. (goto-char (match-end 0))
  266. (skip-chars-forward " \t")
  267. (looking-at "\\[[- X]\\]"))))
  268. (defun org-toggle-checkbox (&optional toggle-presence)
  269. "Toggle the checkbox in the current line.
  270. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
  271. With double prefix, set checkbox to [-].
  272. When there is an active region, toggle status or presence of the checkbox
  273. in the first line, and make every item in the region have the same
  274. status or presence, respectively.
  275. If the cursor is in a headline, apply this to all checkbox items in the
  276. text below the heading."
  277. (interactive "P")
  278. (catch 'exit
  279. (let (beg end status first-present first-status blocked)
  280. (cond
  281. ((org-region-active-p)
  282. (setq beg (region-beginning) end (region-end)))
  283. ((org-on-heading-p)
  284. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  285. ((org-at-item-checkbox-p)
  286. (save-excursion
  287. (if (equal toggle-presence '(4))
  288. (progn
  289. (replace-match "")
  290. (goto-char (match-beginning 0))
  291. (just-one-space))
  292. (when (setq blocked (org-checkbox-blocked-p))
  293. (error "Checkbox blocked because of unchecked box in line %d"
  294. blocked))
  295. (replace-match
  296. (cond ((equal toggle-presence '(16)) "[-]")
  297. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  298. (t "[ ]"))
  299. t t)))
  300. (throw 'exit t))
  301. ((org-at-item-p)
  302. ;; add a checkbox
  303. (save-excursion
  304. (goto-char (match-end 0))
  305. (insert "[ ] "))
  306. (throw 'exit t))
  307. (t (error "Not at a checkbox or heading, and no active region")))
  308. (setq end (move-marker (make-marker) end))
  309. (save-excursion
  310. (goto-char beg)
  311. (setq first-present (org-at-item-checkbox-p)
  312. first-status
  313. (save-excursion
  314. (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t)
  315. (equal (match-string 1) "[X]"))))
  316. (while (< (point) end)
  317. (if toggle-presence
  318. (cond
  319. ((and first-present (org-at-item-checkbox-p))
  320. (save-excursion
  321. (replace-match "")
  322. (goto-char (match-beginning 0))
  323. (just-one-space)))
  324. ((and (not first-present) (not (org-at-item-checkbox-p))
  325. (org-at-item-p))
  326. (save-excursion
  327. (goto-char (match-end 0))
  328. (insert "[ ] "))))
  329. (when (org-at-item-checkbox-p)
  330. (setq status (equal (match-string 0) "[X]"))
  331. (replace-match
  332. (if first-status "[ ]" "[X]") t t)))
  333. (beginning-of-line 2)))))
  334. (org-update-checkbox-count-maybe))
  335. (defun org-reset-checkbox-state-subtree ()
  336. "Reset all checkboxes in an entry subtree."
  337. (interactive "*")
  338. (save-restriction
  339. (save-excursion
  340. (org-narrow-to-subtree)
  341. (org-show-subtree)
  342. (goto-char (point-min))
  343. (let ((end (point-max)))
  344. (while (< (point) end)
  345. (when (org-at-item-checkbox-p)
  346. (replace-match "[ ]" t t))
  347. (beginning-of-line 2))))
  348. (org-update-checkbox-count-maybe)))
  349. (defun org-checkbox-blocked-p ()
  350. "Is the current checkbox blocked from for being checked now?
  351. A checkbox is blocked if all of the following conditions are fulfilled:
  352. 1. The checkbox is not checked already.
  353. 2. The current entry has the ORDERED property set.
  354. 3. There is an unchecked checkbox in this entry before the current line."
  355. (catch 'exit
  356. (save-match-data
  357. (save-excursion
  358. (unless (org-at-item-checkbox-p) (throw 'exit nil))
  359. (when (equal (match-string 0) "[X]")
  360. ;; the box is already checked!
  361. (throw 'exit nil))
  362. (let ((end (point-at-bol)))
  363. (condition-case nil (org-back-to-heading t)
  364. (error (throw 'exit nil)))
  365. (unless (org-entry-get nil "ORDERED") (throw 'exit nil))
  366. (if (re-search-forward "^[ \t]*[-+*0-9.)] \\[[- ]\\]" end t)
  367. (org-current-line)
  368. nil))))))
  369. (defvar org-checkbox-statistics-hook nil
  370. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  371. This hook runs even if `org-provide-checkbox-statistics' is nil, to it can
  372. be used to implement alternative ways of collecting statistics information.")
  373. (defun org-update-checkbox-count-maybe ()
  374. "Update checkbox statistics unless turned off by user."
  375. (when org-provide-checkbox-statistics
  376. (org-update-checkbox-count))
  377. (run-hooks 'org-checkbox-statistics-hook))
  378. (defun org-update-checkbox-count (&optional all)
  379. "Update the checkbox statistics in the current section.
  380. This will find all statistic cookies like [57%] and [6/12] and update them
  381. with the current numbers. With optional prefix argument ALL, do this for
  382. the whole buffer."
  383. (interactive "P")
  384. (save-excursion
  385. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  386. (beg (condition-case nil
  387. (progn (org-back-to-heading) (point))
  388. (error (point-min))))
  389. (end (move-marker (make-marker)
  390. (progn (outline-next-heading) (point))))
  391. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  392. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  393. (re-find (concat re "\\|" re-box))
  394. beg-cookie end-cookie is-percent c-on c-off lim new
  395. eline curr-ind next-ind continue-from startsearch
  396. (recursive
  397. (or (not org-hierarchical-checkbox-statistics)
  398. (string-match "\\<recursive\\>"
  399. (or (org-entry-get nil "COOKIE_DATA") ""))))
  400. (cstat 0)
  401. )
  402. (when all
  403. (goto-char (point-min))
  404. (outline-next-heading)
  405. (setq beg (point) end (point-max)))
  406. (goto-char end)
  407. ;; find each statistics cookie
  408. (while (and (re-search-backward re-find beg t)
  409. (not (save-match-data
  410. (and (org-on-heading-p)
  411. (string-match "\\<todo\\>"
  412. (downcase
  413. (or (org-entry-get
  414. nil "COOKIE_DATA")
  415. "")))))))
  416. (setq beg-cookie (match-beginning 1)
  417. end-cookie (match-end 1)
  418. cstat (+ cstat (if end-cookie 1 0))
  419. startsearch (point-at-eol)
  420. continue-from (match-beginning 0)
  421. is-percent (match-beginning 2)
  422. lim (cond
  423. ((org-on-heading-p) (outline-next-heading) (point))
  424. ((org-at-item-p) (org-end-of-item) (point))
  425. (t nil))
  426. c-on 0
  427. c-off 0)
  428. (when lim
  429. ;; find first checkbox for this cookie and gather
  430. ;; statistics from all that are at this indentation level
  431. (goto-char startsearch)
  432. (if (re-search-forward re-box lim t)
  433. (progn
  434. (org-beginning-of-item)
  435. (setq curr-ind (org-get-indentation))
  436. (setq next-ind curr-ind)
  437. (while (and (bolp) (org-at-item-p)
  438. (if recursive
  439. (<= curr-ind next-ind)
  440. (= curr-ind next-ind)))
  441. (save-excursion (end-of-line) (setq eline (point)))
  442. (if (re-search-forward re-box eline t)
  443. (if (member (match-string 2) '("[ ]" "[-]"))
  444. (setq c-off (1+ c-off))
  445. (setq c-on (1+ c-on))))
  446. (if (not recursive)
  447. (org-end-of-item)
  448. (end-of-line)
  449. (when (re-search-forward org-list-beginning-re lim t)
  450. (beginning-of-line)))
  451. (setq next-ind (org-get-indentation)))))
  452. (goto-char continue-from)
  453. ;; update cookie
  454. (when end-cookie
  455. (setq new (if is-percent
  456. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  457. (format "[%d/%d]" c-on (+ c-on c-off))))
  458. (goto-char beg-cookie)
  459. (insert new)
  460. (delete-region (point) (+ (point) (- end-cookie beg-cookie))))
  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 delta)
  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. (setq delta (- (length new) (length old)))
  784. (org-shift-item-indentation delta)
  785. (if (= (org-current-line) line) (setq col (+ col delta))))))
  786. (org-goto-line line)
  787. (org-move-to-column col)))
  788. (defun org-fix-bullet-type (&optional force-bullet)
  789. "Make sure all items in this list have the same bullet as the first item.
  790. Also, fix the indentation."
  791. (interactive)
  792. (unless (org-at-item-p) (error "This is not a list"))
  793. (let ((line (org-current-line))
  794. (col (current-column))
  795. (ind (current-indentation))
  796. ind1 bullet oldbullet)
  797. ;; find where this list begins
  798. (org-beginning-of-item-list)
  799. (beginning-of-line 1)
  800. ;; find out what the bullet type is
  801. (looking-at "[ \t]*\\(\\S-+\\)")
  802. (setq bullet (concat (or force-bullet (match-string 1)) " "))
  803. (if (and org-list-two-spaces-after-bullet-regexp
  804. (string-match org-list-two-spaces-after-bullet-regexp bullet))
  805. (setq bullet (concat bullet " ")))
  806. ;; walk forward and replace these numbers
  807. (beginning-of-line 0)
  808. (catch 'exit
  809. (while t
  810. (catch 'next
  811. (beginning-of-line 2)
  812. (if (eobp) (throw 'exit nil))
  813. (if (looking-at "[ \t]*$") (throw 'next nil))
  814. (skip-chars-forward " \t") (setq ind1 (current-column))
  815. (if (> ind1 ind) (throw 'next t))
  816. (if (< ind1 ind) (throw 'exit t))
  817. (if (not (org-at-item-p)) (throw 'exit nil))
  818. (skip-chars-forward " \t")
  819. (looking-at "\\S-+ *")
  820. (setq oldbullet (match-string 0))
  821. (unless (equal bullet oldbullet) (replace-match bullet))
  822. (org-shift-item-indentation (- (length bullet) (length oldbullet))))))
  823. (org-goto-line line)
  824. (org-move-to-column col)
  825. (if (string-match "[0-9]" bullet)
  826. (org-renumber-ordered-list 1))))
  827. (defun org-shift-item-indentation (delta)
  828. "Shift the indentation in current item by DELTA."
  829. (save-excursion
  830. (let ((beg (point-at-bol))
  831. (end (progn (org-end-of-item) (point)))
  832. i)
  833. (goto-char end)
  834. (beginning-of-line 0)
  835. (while (> (point) beg)
  836. (when (looking-at "[ \t]*\\S-")
  837. ;; this is not an empty line
  838. (setq i (org-get-indentation))
  839. (if (and (> i 0) (> (setq i (+ i delta)) 0))
  840. (indent-line-to i)))
  841. (beginning-of-line 0)))))
  842. (defun org-beginning-of-item-list ()
  843. "Go to the beginning of the current item list.
  844. I.e. to the first item in this list."
  845. (interactive)
  846. (org-beginning-of-item)
  847. (let ((pos (point-at-bol))
  848. (ind (org-get-indentation))
  849. ind1)
  850. ;; find where this list begins
  851. (catch 'exit
  852. (while t
  853. (catch 'next
  854. (beginning-of-line 0)
  855. (if (looking-at "[ \t]*$")
  856. (throw (if (bobp) 'exit 'next) t))
  857. (skip-chars-forward " \t") (setq ind1 (current-column))
  858. (if (or (< ind1 ind)
  859. (and (= ind1 ind)
  860. (not (org-at-item-p)))
  861. (and (= (point-at-bol) (point-min))
  862. (setq pos (point-min))))
  863. (throw 'exit t)
  864. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  865. (goto-char pos)))
  866. (defun org-end-of-item-list ()
  867. "Go to the end of the current item list.
  868. I.e. to the text after the last item."
  869. (interactive)
  870. (org-beginning-of-item)
  871. (let ((pos (point-at-bol))
  872. (ind (org-get-indentation))
  873. ind1)
  874. ;; find where this list begins
  875. (catch 'exit
  876. (while t
  877. (catch 'next
  878. (beginning-of-line 2)
  879. (if (looking-at "[ \t]*$")
  880. (if (eobp)
  881. (progn (setq pos (point)) (throw 'exit t))
  882. (throw 'next t)))
  883. (skip-chars-forward " \t") (setq ind1 (current-column))
  884. (if (or (< ind1 ind)
  885. (and (= ind1 ind)
  886. (not (org-at-item-p)))
  887. (eobp))
  888. (progn
  889. (setq pos (point-at-bol))
  890. (throw 'exit t))))))
  891. (goto-char pos)))
  892. (defvar org-last-indent-begin-marker (make-marker))
  893. (defvar org-last-indent-end-marker (make-marker))
  894. (defun org-outdent-item (arg)
  895. "Outdent a local list item."
  896. (interactive "p")
  897. (org-indent-item (- arg)))
  898. (defun org-indent-item (arg)
  899. "Indent a local list item."
  900. (interactive "p")
  901. (and (org-region-active-p) (org-cursor-to-region-beginning))
  902. (unless (org-at-item-p)
  903. (error "Not on an item"))
  904. (let (beg end ind ind1 ind-bul delta ind-down ind-up firstp)
  905. (setq firstp (org-first-list-item-p))
  906. (save-excursion
  907. (setq end (and (org-region-active-p) (region-end)))
  908. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  909. (setq beg org-last-indent-begin-marker
  910. end org-last-indent-end-marker)
  911. (org-beginning-of-item)
  912. (setq beg (move-marker org-last-indent-begin-marker (point)))
  913. (org-end-of-item)
  914. (setq end (move-marker org-last-indent-end-marker (or end (point)))))
  915. (goto-char beg)
  916. (setq ind-bul (org-item-indent-positions)
  917. ind (caar ind-bul)
  918. ind-down (car (nth 2 ind-bul))
  919. ind-up (car (nth 1 ind-bul))
  920. delta (if (> arg 0)
  921. (if ind-down (- ind-down ind) 2)
  922. (if ind-up (- ind-up ind) -2)))
  923. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  924. (while (< (point) end)
  925. (beginning-of-line 1)
  926. (skip-chars-forward " \t") (setq ind1 (current-column))
  927. (delete-region (point-at-bol) (point))
  928. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  929. (beginning-of-line 2)))
  930. (org-fix-bullet-type
  931. (and (> arg 0)
  932. (not firstp)
  933. (cdr (assoc (cdr (nth 0 ind-bul)) org-list-demote-modify-bullet))))
  934. (org-maybe-renumber-ordered-list-safe)
  935. (save-excursion
  936. (beginning-of-line 0)
  937. (condition-case nil (org-beginning-of-item) (error nil))
  938. (org-maybe-renumber-ordered-list-safe))))
  939. (defun org-item-indent-positions ()
  940. "Return indentation for plain list items.
  941. This returns a list with three values: The current indentation, the
  942. parent indentation and the indentation a child should have.
  943. Assumes cursor in item line."
  944. (let* ((bolpos (point-at-bol))
  945. (ind (org-get-indentation))
  946. (bullet (org-get-bullet))
  947. ind-down ind-up bullet-up bullet-down pos)
  948. (save-excursion
  949. (org-beginning-of-item-list)
  950. (skip-chars-backward "\n\r \t")
  951. (when (org-in-item-p)
  952. (org-beginning-of-item)
  953. (setq ind-up (org-get-indentation))
  954. (setq bullet-up (org-get-bullet))))
  955. (setq pos (point))
  956. (save-excursion
  957. (cond
  958. ((and (condition-case nil (progn (org-previous-item) t)
  959. (error nil))
  960. (or (forward-char 1) t)
  961. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  962. (setq ind-down (org-get-indentation)
  963. bullet-down (org-get-bullet)))
  964. ((and (goto-char pos)
  965. (org-at-item-p))
  966. (goto-char (match-end 0))
  967. (skip-chars-forward " \t")
  968. (setq ind-down (current-column)
  969. bullet-down (org-get-bullet)))))
  970. (if (and bullet-down (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-down))
  971. (setq bullet-down (concat "1" (match-string 1 bullet-down))))
  972. (if (and bullet-up (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-up))
  973. (setq bullet-up (concat "1" (match-string 1 bullet-up))))
  974. (if (and bullet (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet))
  975. (setq bullet (concat "1" (match-string 1 bullet))))
  976. (list (cons ind bullet)
  977. (cons ind-up bullet-up)
  978. (cons ind-down bullet-down))))
  979. (defun org-get-bullet ()
  980. (save-excursion
  981. (goto-char (point-at-bol))
  982. (and (looking-at
  983. "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\(\\*\\)\\)\\( \\|$\\)")
  984. (or (match-string 2) (match-string 4)))))
  985. ;;; Send and receive lists
  986. (defun org-list-parse-list (&optional delete)
  987. "Parse the list at point and maybe DELETE it.
  988. Return a list containing first level items as strings and
  989. sublevels as a list of strings."
  990. (let* ((item-beginning (org-list-item-beginning))
  991. (start (car item-beginning))
  992. (end (org-list-end (cdr item-beginning)))
  993. output itemsep ltype)
  994. (while (re-search-forward org-list-beginning-re end t)
  995. (goto-char (match-beginning 3))
  996. (save-match-data
  997. (cond ((string-match "[0-9]" (match-string 2))
  998. (setq itemsep "[0-9]+\\(?:\\.\\|)\\)"
  999. ltype 'ordered))
  1000. ((string-match "^.*::" (match-string 0))
  1001. (setq itemsep "[-+]" ltype 'descriptive))
  1002. (t (setq itemsep "[-+]" ltype 'unordered))))
  1003. (let* ((indent1 (match-string 1))
  1004. (nextitem (save-excursion
  1005. (save-match-data
  1006. (or (and (re-search-forward
  1007. (concat "^" indent1 itemsep " *?") end t)
  1008. (match-beginning 0)) end))))
  1009. (item (buffer-substring
  1010. (point)
  1011. (or (and (re-search-forward
  1012. org-list-beginning-re end t)
  1013. (goto-char (match-beginning 0)))
  1014. (goto-char end))))
  1015. (nextindent (match-string 1))
  1016. (item (org-trim item))
  1017. (item (if (string-match "^\\[\\([xX ]\\)\\]" item)
  1018. (replace-match (if (equal (match-string 1 item) " ")
  1019. "[CBOFF]"
  1020. "[CBON]")
  1021. t nil item)
  1022. item)))
  1023. (push item output)
  1024. (when (> (length nextindent)
  1025. (length indent1))
  1026. (narrow-to-region (point) nextitem)
  1027. (push (org-list-parse-list) output)
  1028. (widen))))
  1029. (when delete (delete-region start end))
  1030. (setq output (nreverse output))
  1031. (push ltype output)))
  1032. (defun org-list-item-beginning ()
  1033. "Find the beginning of the list item.
  1034. Return a cons which car is the beginning position of the item and
  1035. cdr is the indentation string."
  1036. (save-excursion
  1037. (if (not (or (looking-at org-list-beginning-re)
  1038. (re-search-backward
  1039. org-list-beginning-re nil t)))
  1040. (progn (goto-char (point-min)) (point))
  1041. (cons (match-beginning 0) (match-string 1)))))
  1042. (defun org-list-goto-true-beginning ()
  1043. "Go to the beginning of the list at point."
  1044. (beginning-of-line 1)
  1045. (while (looking-at org-list-beginning-re)
  1046. (beginning-of-line 0))
  1047. (progn
  1048. (re-search-forward org-list-beginning-re nil t)
  1049. (goto-char (match-beginning 0))))
  1050. (defun org-list-make-subtree ()
  1051. "Convert the plain list at point into a subtree."
  1052. (interactive)
  1053. (org-list-goto-true-beginning)
  1054. (let ((list (org-list-parse-list t)) nstars)
  1055. (save-excursion
  1056. (if (condition-case nil
  1057. (org-back-to-heading)
  1058. (error nil))
  1059. (progn (re-search-forward org-complex-heading-regexp nil t)
  1060. (setq nstars (length (match-string 1))))
  1061. (setq nstars 0)))
  1062. (org-list-make-subtrees list (1+ nstars))))
  1063. (defun org-list-make-subtrees (list level)
  1064. "Convert LIST into subtrees starting at LEVEL."
  1065. (if (symbolp (car list))
  1066. (org-list-make-subtrees (cdr list) level)
  1067. (mapcar (lambda (item)
  1068. (if (stringp item)
  1069. (insert (make-string
  1070. (if org-odd-levels-only
  1071. (1- (* 2 level)) level) ?*) " " item "\n")
  1072. (org-list-make-subtrees item (1+ level))))
  1073. list)))
  1074. (defun org-list-end (indent)
  1075. "Return the position of the end of the list.
  1076. INDENT is the indentation of the list, as a string."
  1077. (save-excursion
  1078. (catch 'exit
  1079. (while (or (looking-at org-list-beginning-re)
  1080. (looking-at (concat "^" indent "[ \t]+\\|^$"))
  1081. (> (or (get-text-property (point) 'original-indentation) -1)
  1082. (length indent)))
  1083. (if (eq (point) (point-max))
  1084. (throw 'exit (point-max)))
  1085. (forward-line 1)))
  1086. (point)))
  1087. (defun org-list-insert-radio-list ()
  1088. "Insert a radio list template appropriate for this major mode."
  1089. (interactive)
  1090. (let* ((e (assq major-mode org-list-radio-list-templates))
  1091. (txt (nth 1 e))
  1092. name pos)
  1093. (unless e (error "No radio list setup defined for %s" major-mode))
  1094. (setq name (read-string "List name: "))
  1095. (while (string-match "%n" txt)
  1096. (setq txt (replace-match name t t txt)))
  1097. (or (bolp) (insert "\n"))
  1098. (setq pos (point))
  1099. (insert txt)
  1100. (goto-char pos)))
  1101. (defun org-list-send-list (&optional maybe)
  1102. "Send a tranformed version of this list to the receiver position.
  1103. With argument MAYBE, fail quietly if no transformation is defined for
  1104. this list."
  1105. (interactive)
  1106. (catch 'exit
  1107. (unless (org-at-item-p) (error "Not at a list"))
  1108. (save-excursion
  1109. (org-list-goto-true-beginning)
  1110. (beginning-of-line 0)
  1111. (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  1112. (if maybe
  1113. (throw 'exit nil)
  1114. (error "Don't know how to transform this list"))))
  1115. (let* ((name (match-string 1))
  1116. (item-beginning (org-list-item-beginning))
  1117. (transform (intern (match-string 2)))
  1118. (txt (buffer-substring-no-properties
  1119. (car item-beginning)
  1120. (org-list-end (cdr item-beginning))))
  1121. (list (org-list-parse-list))
  1122. beg)
  1123. (unless (fboundp transform)
  1124. (error "No such transformation function %s" transform))
  1125. (setq txt (funcall transform list))
  1126. ;; Find the insertion place
  1127. (save-excursion
  1128. (goto-char (point-min))
  1129. (unless (re-search-forward
  1130. (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
  1131. (error "Don't know where to insert translated list"))
  1132. (goto-char (match-beginning 0))
  1133. (beginning-of-line 2)
  1134. (setq beg (point))
  1135. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  1136. (error "Cannot find end of insertion region"))
  1137. (beginning-of-line 1)
  1138. (delete-region beg (point))
  1139. (goto-char beg)
  1140. (insert txt "\n"))
  1141. (message "List converted and installed at receiver location"))))
  1142. (defun org-list-to-generic (list params)
  1143. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  1144. Valid parameters PARAMS are
  1145. :ustart String to start an unordered list
  1146. :uend String to end an unordered list
  1147. :ostart String to start an ordered list
  1148. :oend String to end an ordered list
  1149. :dstart String to start a descriptive list
  1150. :dend String to end a descriptive list
  1151. :dtstart String to start a descriptive term
  1152. :dtend String to end a descriptive term
  1153. :ddstart String to start a description
  1154. :ddend String to end a description
  1155. :splice When set to t, return only list body lines, don't wrap
  1156. them into :[u/o]start and :[u/o]end. Default is nil.
  1157. :istart String to start a list item
  1158. :iend String to end a list item
  1159. :isep String to separate items
  1160. :lsep String to separate sublists
  1161. :cboff String to insert for an unchecked checkbox
  1162. :cbon String to insert for a checked checkbox"
  1163. (interactive)
  1164. (let* ((p params) sublist
  1165. (splicep (plist-get p :splice))
  1166. (ostart (plist-get p :ostart))
  1167. (oend (plist-get p :oend))
  1168. (ustart (plist-get p :ustart))
  1169. (uend (plist-get p :uend))
  1170. (dstart (plist-get p :dstart))
  1171. (dend (plist-get p :dend))
  1172. (dtstart (plist-get p :dtstart))
  1173. (dtend (plist-get p :dtend))
  1174. (ddstart (plist-get p :ddstart))
  1175. (ddend (plist-get p :ddend))
  1176. (istart (plist-get p :istart))
  1177. (iend (plist-get p :iend))
  1178. (isep (plist-get p :isep))
  1179. (lsep (plist-get p :lsep))
  1180. (cbon (plist-get p :cbon))
  1181. (cboff (plist-get p :cboff)))
  1182. (let ((wrapper
  1183. (cond ((eq (car list) 'ordered)
  1184. (concat ostart "\n%s" oend "\n"))
  1185. ((eq (car list) 'unordered)
  1186. (concat ustart "\n%s" uend "\n"))
  1187. ((eq (car list) 'descriptive)
  1188. (concat dstart "\n%s" dend "\n"))))
  1189. rtn term defstart defend)
  1190. (while (setq sublist (pop list))
  1191. (cond ((symbolp sublist) nil)
  1192. ((stringp sublist)
  1193. (when (string-match "^\\(.*\\) ::" sublist)
  1194. (setq term (org-trim (format (concat dtstart "%s" dtend)
  1195. (match-string 1 sublist))))
  1196. (setq sublist (substring sublist (1+ (length term)))))
  1197. (if (string-match "\\[CBON\\]" sublist)
  1198. (setq sublist (replace-match cbon t t sublist)))
  1199. (if (string-match "\\[CBOFF\\]" sublist)
  1200. (setq sublist (replace-match cboff t t sublist)))
  1201. (setq rtn (concat rtn istart term ddstart
  1202. sublist ddend iend isep)))
  1203. (t (setq rtn (concat rtn ;; previous list
  1204. lsep ;; list separator
  1205. (org-list-to-generic sublist p)
  1206. lsep ;; list separator
  1207. )))))
  1208. (format wrapper rtn))))
  1209. (defun org-list-to-latex (list &optional params)
  1210. "Convert LIST into a LaTeX list.
  1211. LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
  1212. with overruling parameters for `org-list-to-generic'."
  1213. (org-list-to-generic
  1214. list
  1215. (org-combine-plists
  1216. '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  1217. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  1218. :dstart "\\begin{description}" :dend "\\end{description}"
  1219. :dtstart "[" :dtend "]"
  1220. :ddstart "" :ddend ""
  1221. :istart "\\item " :iend ""
  1222. :isep "\n" :lsep "\n"
  1223. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  1224. params)))
  1225. (defun org-list-to-html (list &optional params)
  1226. "Convert LIST into a HTML list.
  1227. LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
  1228. with overruling parameters for `org-list-to-generic'."
  1229. (org-list-to-generic
  1230. list
  1231. (org-combine-plists
  1232. '(:splicep nil :ostart "<ol>" :oend "</ol>"
  1233. :ustart "<ul>" :uend "</ul>"
  1234. :dstart "<dl>" :dend "</dl>"
  1235. :dtstart "<dt>" :dtend "</dt>"
  1236. :ddstart "<dd>" :ddend "</dd>"
  1237. :istart "<li>" :iend "</li>"
  1238. :isep "\n" :lsep "\n"
  1239. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
  1240. params)))
  1241. (defun org-list-to-texinfo (list &optional params)
  1242. "Convert LIST into a Texinfo list.
  1243. LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
  1244. with overruling parameters for `org-list-to-generic'."
  1245. (org-list-to-generic
  1246. list
  1247. (org-combine-plists
  1248. '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  1249. :ustart "@enumerate" :uend "@end enumerate"
  1250. :dstart "@table" :dend "@end table"
  1251. :dtstart "@item " :dtend "\n"
  1252. :ddstart "" :ddend ""
  1253. :istart "@item\n" :iend ""
  1254. :isep "\n" :lsep "\n"
  1255. :cbon "@code{[X]}" :cboff "@code{[ ]}")
  1256. params)))
  1257. (provide 'org-list)
  1258. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  1259. ;;; org-list.el ends here