org-list.el 45 KB

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