org-list.el 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699
  1. ;;; org-list.el --- Plain lists for Org-mode
  2. ;;
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Bastien Guerry <bzg AT altern DOT org>
  8. ;; Keywords: outlines, hypermedia, calendar, wp
  9. ;; Homepage: http://orgmode.org
  10. ;; Version: 7.01trans
  11. ;;
  12. ;; This file is part of GNU Emacs.
  13. ;;
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;; This file contains the code dealing with plain lists in Org-mode.
  28. ;;; Code:
  29. (eval-when-compile
  30. (require 'cl))
  31. (require 'org-macs)
  32. (require 'org-compat)
  33. (defvar org-blank-before-new-entry)
  34. (defvar org-M-RET-may-split-line)
  35. (defvar org-complex-heading-regexp)
  36. (defvar org-odd-levels-only)
  37. (declare-function org-invisible-p "org" ())
  38. (declare-function org-on-heading-p "org" (&optional invisible-ok))
  39. (declare-function outline-next-heading "outline" ())
  40. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  41. (declare-function org-back-over-empty-lines "org" ())
  42. (declare-function org-skip-whitespace "org" ())
  43. (declare-function org-trim "org" (s))
  44. (declare-function org-get-indentation "org" (&optional line))
  45. (declare-function org-timer-item "org-timer" (&optional arg))
  46. (declare-function org-combine-plists "org" (&rest plists))
  47. (declare-function org-entry-get "org"
  48. (pom property &optional inherit literal-nil))
  49. (declare-function org-narrow-to-subtree "org" ())
  50. (declare-function org-show-subtree "org" ())
  51. (defgroup org-plain-lists nil
  52. "Options concerning plain lists in Org-mode."
  53. :tag "Org Plain lists"
  54. :group 'org-structure)
  55. (defcustom org-cycle-include-plain-lists t
  56. "When t, make TAB cycle visibility on plain list items.
  57. Cycling plain lists works only when the cursor is on a plain list
  58. item. When the cursor is on an outline heading, plain lists are
  59. treated as text. This is the most stable way of handling this,
  60. which is why it is the default.
  61. When this is the symbol `integrate', then during cycling, plain
  62. list items will *temporarily* be interpreted as outline headlines
  63. with a level given by 1000+i where i is the indentation of the
  64. bullet. This setting can lead to strange effects when switching
  65. visibility to `children', because the first \"child\" in a
  66. subtree decides what children should be listed. If that first
  67. \"child\" is a plain list item with an implied large level
  68. number, all true children and grand children of the outline
  69. heading will be exposed in a children' view."
  70. :group 'org-plain-lists
  71. :type '(choice
  72. (const :tag "Never" nil)
  73. (const :tag "With cursor in plain list (recommended)" t)
  74. (const :tag "As children of outline headings" integrate)))
  75. (defcustom org-list-demote-modify-bullet nil
  76. "Default bullet type installed when demoting an item.
  77. This is an association list, for each bullet type, this alist will point
  78. to the bullet that should be used when this item is demoted.
  79. For example,
  80. (setq org-list-demote-modify-bullet
  81. '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
  82. will make
  83. + Movies
  84. + Silence of the Lambs
  85. + My Cousin Vinny
  86. + Books
  87. + The Hunt for Red October
  88. + The Road to Omaha
  89. into
  90. + Movies
  91. - Silence of the Lambs
  92. - My Cousin Vinny
  93. + Books
  94. - The Hunt for Red October
  95. - The Road to Omaha"
  96. :group 'org-plain-lists
  97. :type '(repeat
  98. (cons
  99. (choice :tag "If the current bullet is "
  100. (const "-")
  101. (const "+")
  102. (const "*")
  103. (const "1.")
  104. (const "1)"))
  105. (choice :tag "demotion will change it to"
  106. (const "-")
  107. (const "+")
  108. (const "*")
  109. (const "1.")
  110. (const "1)")))))
  111. (defcustom org-plain-list-ordered-item-terminator t
  112. "The character that makes a line with leading number an ordered list item.
  113. Valid values are ?. and ?\). To get both terminators, use t. While
  114. ?. may look nicer, it creates the danger that a line with leading
  115. number may be incorrectly interpreted as an item. ?\) therefore is
  116. the safe choice."
  117. :group 'org-plain-lists
  118. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  119. (const :tag "paren like in \"2)\"" ?\))
  120. (const :tab "both" t)))
  121. (defcustom org-list-two-spaces-after-bullet-regexp nil
  122. "A regular expression matching bullets that should have 2 spaces after them.
  123. When nil, no bullet will have two spaces after them.
  124. When a string, it will be used as a regular expression. When the
  125. bullet type of a list is changed, the new bullet type will be
  126. matched against this regexp. If it matches, there will be two
  127. spaces instead of one after the bullet in each item of the list."
  128. :group 'org-plain-lists
  129. :type '(choice
  130. (const :tag "never" nil)
  131. (regexp)))
  132. (defcustom org-empty-line-terminates-plain-lists nil
  133. "Non-nil means an empty line ends all plain list levels.
  134. Otherwise, look for `org-list-end-regexp'."
  135. :group 'org-plain-lists
  136. :type 'boolean)
  137. (defcustom org-list-end-regexp "^[ \t]*\n[ \t]*\n"
  138. "Regexp matching the end of all plain list levels.
  139. It must start with \"^\" and end with \"\\n\". It defaults to 2
  140. blank lines. `org-empty-line-terminates-plain-lists' has
  141. precedence over it."
  142. :group 'org-plain-lists
  143. :type 'string)
  144. (defcustom org-list-automatic-rules '((bullet . t)
  145. (checkbox . t)
  146. (indent . t)
  147. (insert . t)
  148. (renumber . t))
  149. "Non-nil means apply set of rules when acting on lists.
  150. By default, automatic actions are taken when using
  151. \\[org-shiftmetaup], \\[org-shiftmetadown], \\[org-meta-return],
  152. \\[org-metaright], \\[org-metaleft], \\[org-shiftmetaright],
  153. \\[org-shiftmetaleft], \\[org-ctrl-c-minus] or
  154. \\[org-insert-todo-heading]. You can disable individually these
  155. rules by setting sets to nil. Valid sets are:
  156. bullet when non-nil, cycling bullet do not allow lists at
  157. column 0 to have * as a bullet and descriptions lists
  158. to be numbered.
  159. checkbox when non-nil, checkbox statistics is updated each time
  160. you either insert a new checkbox or toggle a checkbox.
  161. indent when non-nil indenting or outdenting list top-item will
  162. move the whole list, indenting the first item of a
  163. sub-list will be forbidden and outdenting a list whose
  164. bullet is * to column 0 will change that bullet to -.
  165. insert when non-nil, trying to insert an item inside a block
  166. will insert it right before the block instead of
  167. throwing an error.
  168. renumber when non-nil, renumber ordered plain lists whenever it
  169. is modified. You can always use \\[org-ctrl-c-ctrl-c]
  170. to trigger renumbering."
  171. :group 'org-plain-lists
  172. :type '(alist :tag "Sets of rules"
  173. :key-type
  174. (choice
  175. (const :tag "Bullet" bullet)
  176. (const :tag "Checkbox" checkbox)
  177. (const :tag "Indent" indent)
  178. (const :tag "Insert" insert)
  179. (const :tag "Renumber" renumber))
  180. :value-type
  181. (boolean :tag "Activate" :value t)))
  182. (defcustom org-hierarchical-checkbox-statistics t
  183. "Non-nil means checkbox statistics counts only the state of direct children.
  184. When nil, all boxes below the cookie are counted.
  185. This can be set to nil on a per-node basis using a COOKIE_DATA property
  186. with the word \"recursive\" in the value."
  187. :group 'org-plain-lists
  188. :type 'boolean)
  189. (defcustom org-description-max-indent 20
  190. "Maximum indentation for the second line of a description list.
  191. When the indentation would be larger than this, it will become
  192. 5 characters instead."
  193. :group 'org-plain-lists
  194. :type 'integer)
  195. (defcustom org-list-radio-list-templates
  196. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  197. % END RECEIVE ORGLST %n
  198. \\begin{comment}
  199. #+ORGLST: SEND %n org-list-to-latex
  200. -
  201. \\end{comment}\n")
  202. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  203. @c END RECEIVE ORGLST %n
  204. @ignore
  205. #+ORGLST: SEND %n org-list-to-texinfo
  206. -
  207. @end ignore\n")
  208. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  209. <!-- END RECEIVE ORGLST %n -->
  210. <!--
  211. #+ORGLST: SEND %n org-list-to-html
  212. -
  213. -->\n"))
  214. "Templates for radio lists in different major modes.
  215. All occurrences of %n in a template will be replaced with the name of the
  216. list, obtained by prompting the user."
  217. :group 'org-plain-lists
  218. :type '(repeat
  219. (list (symbol :tag "Major mode")
  220. (string :tag "Format"))))
  221. ;;; Internal functions
  222. (defun org-list-end-re ()
  223. "Return the regex corresponding to the end of a list.
  224. It depends on `org-empty-line-terminates-plain-lists'."
  225. (if org-empty-line-terminates-plain-lists
  226. "^[ \t]*\n"
  227. org-list-end-regexp))
  228. (defun org-item-re (&optional general)
  229. "Return the correct regular expression for plain lists.
  230. If GENERAL is non-nil, return the general regexp independent of the value
  231. of `org-plain-list-ordered-item-terminator'."
  232. (cond
  233. ((or general (eq org-plain-list-ordered-item-terminator t))
  234. "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  235. ((= org-plain-list-ordered-item-terminator ?.)
  236. "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  237. ((= org-plain-list-ordered-item-terminator ?\))
  238. "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  239. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'"))))
  240. (defconst org-item-beginning-re (concat "^" (org-item-re))
  241. "Regexp matching the beginning of a plain list item.")
  242. (defun org-list-terminator-between (min max &optional firstp)
  243. "Find the position of a list ender between MIN and MAX, or nil.
  244. This function looks for `org-list-end-re' outside a block.
  245. If FIRSTP in non-nil, return the point at the beginning of the
  246. nearest valid terminator from min. Otherwise, return the point at
  247. the end of the nearest terminator from max."
  248. (save-excursion
  249. (let* ((start (if firstp min max))
  250. (end (if firstp max min))
  251. (search-fun (if firstp
  252. #'org-search-forward-unenclosed
  253. #'org-search-backward-unenclosed))
  254. (list-end-p (progn
  255. (goto-char start)
  256. (funcall search-fun (org-list-end-re) end t))))
  257. ;; Is there a valid list terminator somewhere ?
  258. (and list-end-p
  259. ;; we want to be on the first line of the list ender
  260. (match-beginning 0)))))
  261. (defun org-list-search-unenclosed-generic (search-fun regexp bound noerror count)
  262. "Search for REGEXP with SEARCH-FUN but don't stop inside blocks or at protected places."
  263. (let ((origin (point)))
  264. (cond
  265. ;; nothing found: return nil
  266. ((not (funcall search-fun regexp bound noerror count)) nil)
  267. ((or (save-match-data
  268. (org-in-regexps-block-p "^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
  269. '(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2))))
  270. (get-text-property (match-beginning 0) 'org-protected))
  271. ;; match is enclosed or protected: start again, searching one
  272. ;; occurrence away.
  273. (goto-char origin)
  274. (org-list-search-unenclosed-generic search-fun regexp bound noerror (1+ count)))
  275. ;; else return point.
  276. (t (point)))))
  277. (defun org-search-backward-unenclosed (regexp &optional bound noerror)
  278. "Like `re-search-backward' but don't stop inside blocks or at protected places."
  279. (org-list-search-unenclosed-generic #'re-search-backward regexp bound noerror 1))
  280. (defun org-search-forward-unenclosed (regexp &optional bound noerror)
  281. "Like `re-search-forward' but don't stop inside blocks or at protected places."
  282. (org-list-search-unenclosed-generic #'re-search-forward regexp bound noerror 1))
  283. (defun org-list-at-regexp-after-bullet-p (regexp)
  284. "Is point at a list item with REGEXP after bullet?"
  285. (and (org-at-item-p)
  286. (save-excursion
  287. (goto-char (match-end 0))
  288. (looking-at regexp))))
  289. (defun org-list-get-item-same-level (search-fun pos limit pre-move)
  290. "Return point at the beginning of next item at the same level.
  291. Search items using function SEARCH-FUN, from POS to LIMIT. It
  292. uses PRE-MOVE before search. Return nil if no item was found."
  293. (save-excursion
  294. (goto-char pos)
  295. (let ((ind (progn
  296. (org-beginning-of-item)
  297. (org-get-indentation)))
  298. (start (point-at-bol)))
  299. ;; We don't want to match the current line.
  300. (funcall pre-move)
  301. ;; Skip any sublist on the way
  302. (while (and (funcall search-fun org-item-beginning-re limit t)
  303. (> (org-get-indentation) ind)))
  304. (when (and (/= (point-at-bol) start) ; Have we moved ?
  305. (= (org-get-indentation) ind))
  306. (point-at-bol)))))
  307. (defun org-list-insert-item-generic (pos &optional checkbox after-bullet)
  308. "Insert a new list item at POS.
  309. If POS is before first character after bullet of the item, the
  310. new item will be created before the current one.
  311. Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
  312. after the bullet. Cursor will be after this text once the
  313. function ends."
  314. (goto-char pos)
  315. ;; Is point in a special block?
  316. (when (org-in-regexps-block-p
  317. "^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
  318. '(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2)))
  319. (if (not (cdr (assq 'insert org-list-automatic-rules)))
  320. ;; Rule in `org-list-automatic-rules' forbids insertion.
  321. (error "Cannot insert item inside a block.")
  322. ;; Else, move before it prior to add a new item.
  323. (end-of-line)
  324. (re-search-backward "^[ \t]*#\\+\\(begin\\|BEGIN\\)_" nil t)
  325. (end-of-line 0)))
  326. (let* ((true-pos (point))
  327. (bullet (and (org-beginning-of-item)
  328. (org-list-bullet-string (org-get-bullet))))
  329. (ind (org-get-indentation))
  330. (before-p (progn
  331. ;; Description item: text starts after colons.
  332. (or (org-at-item-description-p)
  333. ;; At a checkbox: text starts after it.
  334. (org-at-item-checkbox-p)
  335. ;; Otherwise, text starts after bullet.
  336. (org-at-item-p))
  337. (<= true-pos (match-end 0))))
  338. ;; Guess number of blank lines used to separate items.
  339. (blank-lines-nb
  340. (let ((insert-blank-p
  341. (cdr (assq 'plain-list-item org-blank-before-new-entry))))
  342. (cond
  343. ;; Trivial cases where there should be none.
  344. ((or org-empty-line-terminates-plain-lists
  345. (not insert-blank-p)) 0)
  346. ;; When `org-blank-before-new-entry' says so, it is 1.
  347. ((eq insert-blank-p t) 1)
  348. ;; plain-list-item is 'auto. Count blank lines separating
  349. ;; neighbours items in list.
  350. (t (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  351. (cond
  352. ;; Is there a next item?
  353. (next-p (goto-char next-p)
  354. (org-back-over-empty-lines))
  355. ;; Is there a previous item?
  356. ((not (org-list-first-item-p)) (org-back-over-empty-lines))
  357. ;; Item alone: count lines separating it from parent, if any
  358. ((/= (org-list-top-point) (point-at-bol))
  359. (org-back-over-empty-lines))
  360. ;; No parent: no blank line.
  361. (t 0)))))))
  362. (insert-fun
  363. (lambda (text)
  364. ;; insert bullet above item in order to avoid bothering
  365. ;; with possible blank lines ending last item.
  366. (org-beginning-of-item)
  367. (indent-to-column ind)
  368. (insert (concat bullet (when checkbox "[ ] ") after-bullet))
  369. ;; Stay between after-bullet and before text.
  370. (save-excursion
  371. (insert (concat text (make-string (1+ blank-lines-nb) ?\n))))
  372. (unless before-p (org-move-item-down))
  373. (when checkbox (org-update-checkbox-count-maybe)))))
  374. (goto-char true-pos)
  375. (cond
  376. (before-p (funcall insert-fun nil)
  377. ;; Not taking advantage of renumbering while moving
  378. ;; down. Need to call it directly.
  379. (org-maybe-renumber-ordered-list) t)
  380. ;; Can't split item: insert bullet at the end of item.
  381. ((not (org-get-alist-option org-M-RET-may-split-line 'item))
  382. (funcall insert-fun nil) t)
  383. ;; else, insert a new bullet along with everything from point
  384. ;; down to last non-blank line of item.
  385. (t
  386. (delete-horizontal-space)
  387. ;; Get pos again in case previous command modified line.
  388. (let* ((pos (point))
  389. (end-before-blank (org-end-of-item-before-blank))
  390. (after-text
  391. (when (< pos end-before-blank)
  392. (prog1
  393. (delete-and-extract-region pos end-before-blank)
  394. ;; delete any blank line at and before point.
  395. (beginning-of-line)
  396. (while (looking-at "^[ \t]*$")
  397. (delete-region (point-at-bol) (1+ (point-at-eol)))
  398. (beginning-of-line 0))))))
  399. (funcall insert-fun after-text) t)))))
  400. ;;; Predicates
  401. (defun org-in-item-p ()
  402. "Is the cursor inside a plain list ?"
  403. (unless (let ((outline-regexp org-outline-regexp)) (org-at-heading-p))
  404. (save-excursion
  405. (let* ((limit (save-excursion (outline-previous-heading)))
  406. ;; Move to eol so current line can be matched by `org-item-re'.
  407. (actual-pos (goto-char (point-at-eol)))
  408. (last-item-start (save-excursion
  409. (org-search-backward-unenclosed org-item-beginning-re limit t)))
  410. (list-ender (org-list-terminator-between last-item-start actual-pos)))
  411. ;; We are in a list when we are on an item line or when we can
  412. ;; find an item before point and there is no valid list ender
  413. ;; between it and the point.
  414. (and last-item-start
  415. (not list-ender))))))
  416. (defun org-list-first-item-p ()
  417. "Is this item the first item in a plain list?"
  418. (save-excursion
  419. (= (org-beginning-of-item) (org-beginning-of-item-list))))
  420. (defun org-at-item-p ()
  421. "Is point in a line starting a hand-formatted item?"
  422. (save-excursion
  423. (goto-char (point-at-bol))
  424. (looking-at org-item-beginning-re)))
  425. (defun org-at-item-bullet-p ()
  426. "Is point at the bullet of a plain list item?"
  427. (and (org-at-item-p)
  428. (not (member (char-after) '(?\ ?\t)))
  429. (< (point) (match-end 0))))
  430. (defun org-at-item-timer-p ()
  431. "Is point at a line starting a plain list item with a timer?"
  432. (org-list-at-regexp-after-bullet-p "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
  433. (defun org-at-item-description-p ()
  434. "Is point at a description list item?"
  435. (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
  436. (defun org-at-item-checkbox-p ()
  437. "Is point at a line starting a plain-list item with a checklet?"
  438. (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
  439. (defun org-item-has-child-p ()
  440. "Does the current item have subitems?"
  441. (save-excursion
  442. (org-beginning-of-item)
  443. (let ((ind (org-get-indentation)))
  444. (org-end-of-item-or-at-child)
  445. (and (org-at-item-p)
  446. (> (org-get-indentation) ind)))))
  447. (defun org-checkbox-blocked-p ()
  448. "Is the current checkbox blocked from for being checked now?
  449. A checkbox is blocked if all of the following conditions are fulfilled:
  450. 1. The checkbox is not checked already.
  451. 2. The current entry has the ORDERED property set.
  452. 3. There is an unchecked checkbox in this entry before the current line."
  453. (catch 'exit
  454. (save-match-data
  455. (save-excursion
  456. (unless (org-at-item-checkbox-p) (throw 'exit nil))
  457. (when (equal (match-string 1) "[X]")
  458. ;; the box is already checked!
  459. (throw 'exit nil))
  460. (let ((end (point-at-bol)))
  461. (condition-case nil (org-back-to-heading t)
  462. (error (throw 'exit nil)))
  463. (unless (org-entry-get nil "ORDERED") (throw 'exit nil))
  464. (when (org-search-forward-unenclosed "^[ \t]*[-+*0-9.)] \\[[- ]\\]" end t)
  465. (org-current-line)))))))
  466. ;;; Navigate
  467. (defun org-list-top-point ()
  468. "Return point at the top level item in a list, or nil if not in a list."
  469. (save-excursion
  470. (and (org-in-item-p)
  471. (let ((pos (point-at-eol))
  472. (bound (or (outline-previous-heading) (point-min))))
  473. ;; Is there some list above this one ? If so, go to its ending.
  474. ;; Otherwise, go back to the heading above or bob.
  475. (goto-char (or (org-list-terminator-between bound pos) bound))
  476. ;; From there, search down our list.
  477. (org-search-forward-unenclosed org-item-beginning-re pos t)
  478. (point-at-bol)))))
  479. (defun org-list-bottom-point ()
  480. "Return point just before list ending or nil if not in a list."
  481. (save-excursion
  482. (and (org-in-item-p)
  483. (let ((pos (org-beginning-of-item))
  484. (bound (or (and (let ((outline-regexp org-outline-regexp))
  485. ;; Use default regexp because folding
  486. ;; changes OUTLINE-REGEXP.
  487. (outline-next-heading))
  488. (skip-chars-backward " \t\r\n")
  489. (1+ (point-at-eol)))
  490. (point-max))))
  491. ;; The list ending is either first point matching
  492. ;; `org-list-end-re', point at first white-line before next
  493. ;; heading, or eob.
  494. (or (org-list-terminator-between pos bound t) bound)))))
  495. (defun org-beginning-of-item ()
  496. "Go to the beginning of the current hand-formatted item.
  497. If the cursor is not in an item, throw an error. Return point."
  498. (interactive)
  499. (if (not (org-in-item-p))
  500. (error "Not in an item")
  501. ;; Possibly match the current line.
  502. (end-of-line)
  503. (org-search-backward-unenclosed org-item-beginning-re nil t)
  504. (goto-char (point-at-bol))))
  505. (defun org-end-of-item ()
  506. "Go to the end of the current hand-formatted item.
  507. If the cursor is not in an item, throw an error."
  508. (interactive)
  509. (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  510. (cond ((not (org-in-item-p)) (error "Not in an item"))
  511. (next-p (goto-char next-p))
  512. (t (org-end-of-item-list)))))
  513. (defun org-end-of-item-or-at-child ()
  514. "Move to the end of the item text, stops before the first child if any."
  515. (let ((limit (org-list-bottom-point)))
  516. (end-of-line)
  517. (goto-char
  518. (if (org-search-forward-unenclosed org-item-beginning-re limit t)
  519. (point-at-bol)
  520. limit))))
  521. (defun org-end-of-item-before-blank ()
  522. "Return point at end of item, before any blank line.
  523. Point returned is at eol."
  524. (save-excursion
  525. (org-end-of-item)
  526. (skip-chars-backward " \r\t\n")
  527. (point-at-eol)))
  528. (defun org-get-next-item (pos limit)
  529. "Get the point of the next item at the same level as POS.
  530. Stop searching at LIMIT. Return nil if no item is found. This
  531. function does not move point."
  532. (org-list-get-item-same-level
  533. #'org-search-forward-unenclosed pos limit #'end-of-line))
  534. (defun org-get-previous-item (pos limit)
  535. "Get the point of the previous item at the same level as POS.
  536. Stop searching at LIMIT. Return nil if no item is found. This
  537. function does not move point."
  538. (org-list-get-item-same-level
  539. #'org-search-backward-unenclosed pos limit #'beginning-of-line))
  540. (defun org-next-item ()
  541. "Move to the beginning of the next item.
  542. Item is at the same level in the current plain list. Error if not
  543. in a plain list, or if this is the last item in the list."
  544. (interactive)
  545. (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  546. (if next-p
  547. (goto-char next-p)
  548. (error "On last item"))))
  549. (defun org-previous-item ()
  550. "Move to the beginning of the previous item.
  551. Item is at the same level in the current plain list. Error if not
  552. in a plain list, or if this is the first item in the list."
  553. (interactive)
  554. (let ((prev-p (org-get-previous-item (point) (org-list-top-point))))
  555. (if prev-p
  556. (goto-char prev-p)
  557. (error "On first item"))))
  558. (defun org-beginning-of-item-list ()
  559. "Go to the beginning item of the current list or sublist.
  560. Return point."
  561. (interactive)
  562. (org-beginning-of-item)
  563. (let ((limit (org-list-top-point))
  564. (move-up (lambda (pos bound)
  565. ;; prev-p: any item of same level before ?
  566. (let ((prev-p (org-get-previous-item pos bound)))
  567. ;; recurse until no more item of the same level
  568. ;; can be found.
  569. (if prev-p (funcall move-up prev-p bound) pos)))))
  570. ;; Go to the last item found and at bol in case we didn't move
  571. (goto-char (funcall move-up (point) limit))
  572. (goto-char (point-at-bol))))
  573. (defun org-end-of-item-list ()
  574. "Go to the end of the current list or sublist.
  575. Return point."
  576. (interactive)
  577. (org-beginning-of-item)
  578. (let ((limit (org-list-bottom-point))
  579. (ind (org-get-indentation))
  580. (get-last-item (lambda (pos bound)
  581. ;; next-p: any item of same level after ?
  582. (let ((next-p (org-get-next-item pos bound)))
  583. ;; recurse until no more item of the same level
  584. ;; can be found.
  585. (if next-p (funcall get-last-item next-p bound) pos)))))
  586. ;; Move to the last item of every list or sublist encountered, and
  587. ;; down to bol of a higher-level item, or limit.
  588. (while (and (/= (point) limit)
  589. (>= (org-get-indentation) ind))
  590. (goto-char (funcall get-last-item (point) limit))
  591. (end-of-line)
  592. (when (org-search-forward-unenclosed org-item-beginning-re limit 'move)
  593. (beginning-of-line)))
  594. (point)))
  595. ;;; Manipulate
  596. (defun org-list-exchange-items (beg-A beg-B)
  597. "Swap item starting at BEG-A with item starting at BEG-B.
  598. Blank lines at the end of items are left in place. Assumes
  599. BEG-A is lesser than BEG-B."
  600. (save-excursion
  601. (let* ((end-of-item-no-blank (lambda (pos)
  602. (goto-char pos)
  603. (goto-char (org-end-of-item-before-blank))))
  604. (end-A-no-blank (funcall end-of-item-no-blank beg-A))
  605. (end-B-no-blank (funcall end-of-item-no-blank beg-B))
  606. (body-A (buffer-substring beg-A end-A-no-blank))
  607. (body-B (buffer-substring beg-B end-B-no-blank))
  608. (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B)))
  609. (goto-char beg-A)
  610. (delete-region beg-A end-B-no-blank)
  611. (insert (concat body-B between-A-no-blank-and-B body-A)))))
  612. (defun org-move-item-down ()
  613. "Move the plain list item at point down, i.e. swap with following item.
  614. Subitems (items with larger indentation) are considered part of the item,
  615. so this really moves item trees."
  616. (interactive)
  617. (let ((pos (point))
  618. (col (current-column))
  619. (actual-item (org-beginning-of-item))
  620. (next-item (org-get-next-item (point) (save-excursion (org-end-of-item-list)))))
  621. (if (not next-item)
  622. (progn
  623. (goto-char pos)
  624. (error "Cannot move this item further down"))
  625. (org-list-exchange-items actual-item next-item)
  626. (org-maybe-renumber-ordered-list)
  627. (org-next-item)
  628. (move-to-column col))))
  629. (defun org-move-item-up ()
  630. "Move the plain list item at point up, i.e. swap with previous item.
  631. Subitems (items with larger indentation) are considered part of the item,
  632. so this really moves item trees."
  633. (interactive)
  634. (let ((pos (point))
  635. (col (current-column))
  636. (actual-item (org-beginning-of-item))
  637. (prev-item (org-get-previous-item (point) (save-excursion (org-beginning-of-item-list)))))
  638. (if (not prev-item)
  639. (progn
  640. (goto-char pos)
  641. (error "Cannot move this item further up"))
  642. (org-list-exchange-items prev-item actual-item)
  643. (org-maybe-renumber-ordered-list)
  644. (move-to-column col))))
  645. (defun org-insert-item (&optional checkbox)
  646. "Insert a new item at the current level.
  647. If cursor is before first character after bullet of the item, the
  648. new item will be created before the current one. Return t when
  649. things worked, nil when we are not in an item, or item is
  650. invisible."
  651. (unless (or (not (org-in-item-p))
  652. (org-invisible-p))
  653. (if (save-excursion
  654. (org-beginning-of-item)
  655. (org-at-item-timer-p))
  656. ;; Timer list: delegate to `org-timer-item'.
  657. (progn (org-timer-item) t)
  658. ;; if we're in a description list, ask for the new term.
  659. (let ((desc-text (when (save-excursion
  660. (and (org-beginning-of-item)
  661. (org-at-item-description-p)))
  662. (concat (read-string "Term: ") " :: "))))
  663. (org-list-insert-item-generic
  664. (point) (and checkbox (not desc-text)) desc-text)))))
  665. ;;; Indentation
  666. (defun org-get-string-indentation (s)
  667. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  668. (let ((n -1) (i 0) (w tab-width) c)
  669. (catch 'exit
  670. (while (< (setq n (1+ n)) (length s))
  671. (setq c (aref s n))
  672. (cond ((= c ?\ ) (setq i (1+ i)))
  673. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  674. (t (throw 'exit t)))))
  675. i))
  676. (defvar org-suppress-item-indentation) ; dynamically scoped parameter
  677. (defun org-shift-item-indentation (delta)
  678. "Shift the indentation in current item by DELTA."
  679. (unless (org-bound-and-true-p org-suppress-item-indentation)
  680. (save-excursion
  681. (let ((beg (point-at-bol))
  682. (end (org-end-of-item)))
  683. (beginning-of-line 0)
  684. (while (> (point) beg)
  685. (when (looking-at "[ \t]*\\S-")
  686. ;; this is not an empty line
  687. (let ((i (org-get-indentation)))
  688. (when (and (> i 0) (> (+ i delta) 0))
  689. (indent-line-to (+ i delta)))))
  690. (beginning-of-line 0))))))
  691. (defvar org-last-indent-begin-marker (make-marker))
  692. (defvar org-last-indent-end-marker (make-marker))
  693. (defun org-outdent-item (arg)
  694. "Outdent a local list item, but not its children."
  695. (interactive "p")
  696. (org-indent-item-tree (- arg) 'no-subtree))
  697. (defun org-indent-item (arg)
  698. "Indent a local list item, but not its children."
  699. (interactive "p")
  700. (org-indent-item-tree arg 'no-subtree))
  701. (defun org-outdent-item-tree (arg &optional no-subtree)
  702. "Outdent a local list item including its children.
  703. If NO-SUBTREE is set, only outdent the item itself, not its children."
  704. (interactive "p")
  705. (org-indent-item-tree (- arg) no-subtree))
  706. (defun org-indent-item-tree (arg &optional no-subtree)
  707. "Indent a local list item including its children.
  708. If NO-SUBTREE is set, only indent the item itself, not its
  709. children. Return t if sucessful."
  710. (interactive "p")
  711. (and (org-region-active-p) (org-cursor-to-region-beginning))
  712. (unless (org-at-item-p)
  713. (error "Not on an item"))
  714. (let ((line (org-current-line))
  715. (col (current-column))
  716. (pos (point))
  717. (origin-ind (save-excursion
  718. (goto-char (org-list-top-point))
  719. (org-get-indentation)))
  720. beg end ind ind1 ind-pos bullet delta ind-down ind-up)
  721. (setq end (and (org-region-active-p) (region-end)))
  722. ;; If moving a subtree, don't drag additional items on subsequent
  723. ;; moves.
  724. (if (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  725. (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
  726. (setq beg org-last-indent-begin-marker
  727. end org-last-indent-end-marker)
  728. (org-beginning-of-item)
  729. (setq beg (move-marker org-last-indent-begin-marker (point)))
  730. ;; Determine end point of indentation
  731. (if no-subtree (org-end-of-item-or-at-child) (org-end-of-item))
  732. (setq end (move-marker org-last-indent-end-marker (or end (point)))))
  733. ;; Get some information
  734. (goto-char beg)
  735. (setq ind-pos (org-item-indent-positions)
  736. bullet (cdr (car ind-pos))
  737. bul-up (cdr (nth 1 ind-pos))
  738. ind (caar ind-pos)
  739. ind-down (car (nth 2 ind-pos))
  740. ind-up (car (nth 1 ind-pos))
  741. delta (if (> arg 0)
  742. (if ind-down (- ind-down ind) 2)
  743. (if ind-up (- ind-up ind) -2)))
  744. ;; Make some checks before indenting.
  745. (cond
  746. ((< (+ delta ind) 0)
  747. (goto-char pos)
  748. (error "Cannot outdent beyond margin"))
  749. ;; Apply indent rules if activated.
  750. ((cdr (assq 'indent org-list-automatic-rules))
  751. (cond
  752. ;; 1. If at top-point move the whole list. Moreover, if
  753. ;; *-list is going to column 0, change bullet to "-".
  754. ((= (point-at-bol) (org-list-top-point))
  755. (when (and (= (+ delta ind) 0) (equal bullet "*"))
  756. (org-fix-bullet-type (setq bullet "-")))
  757. (setq end (set-marker org-last-indent-end-marker (org-list-bottom-point))))
  758. ;; 2. Do not indent before top-item.
  759. ((< (+ delta ind) origin-ind)
  760. (goto-char pos)
  761. (error "Cannot outdent beyond top level item"))
  762. ;; 3. Do not indent the first item of a list.
  763. ((and (org-list-first-item-p) (> delta 0))
  764. (goto-char pos)
  765. (error "Cannot indent the beginning of a sublist"))
  766. ;; 4. Do not outdent item that has children without moving.
  767. ;; In the case of a subtree, make sure the check applies to
  768. ;; its last item.
  769. ((and (< delta 0)
  770. (save-excursion (goto-char (1- end)) (org-item-has-child-p)))
  771. (goto-char pos)
  772. (error "Cannot outdent an item having children")))))
  773. ;; Replace bullet of current item with the bullet it is going to
  774. ;; have if we're outdenting. This is needed to prevent indentation
  775. ;; problems of subtrees when outdenting changes bullet size.
  776. (when (< delta 0)
  777. (let ((new-bul (org-list-bullet-string (or bul-up bullet))))
  778. (org-list-replace-bullet new-bul)))
  779. ;; Proceed to reindentation.
  780. (while (< (point) end)
  781. (beginning-of-line)
  782. (skip-chars-forward " \t") (setq ind1 (current-column))
  783. (delete-region (point-at-bol) (point))
  784. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  785. (beginning-of-line 2))
  786. ;; Get back to original position, shifted by delta
  787. (goto-line line)
  788. (move-to-column (max (+ delta col) 0))
  789. ;; Fix and reorder all lists and sublists from parent of the list
  790. ;; at point, or from list at point if it hasn't got any parent or
  791. ;; if we're outdenting.
  792. (save-excursion
  793. ;; Take care of parent list, if it makes sense.
  794. (org-beginning-of-item-list)
  795. (unless (or (< arg 0) (= (org-list-top-point) (point)))
  796. (beginning-of-line 0)
  797. (org-beginning-of-item)
  798. (org-fix-bullet-type)))
  799. ;; Take care of list at point. If demoting, look at
  800. ;; `org-list-demote-modify-bullet'.
  801. (org-fix-bullet-type
  802. (and (> arg 0)
  803. (cdr (assoc bullet org-list-demote-modify-bullet))))
  804. (save-excursion
  805. (when (org-item-has-child-p)
  806. ;; Take care of child, or of every sublist if we're moving a
  807. ;; subtree.
  808. (org-end-of-item-or-at-child)
  809. (if no-subtree
  810. (org-fix-bullet-type)
  811. (let ((fix-list (lambda (i)
  812. (when (org-list-first-item-p)
  813. (org-fix-bullet-type
  814. (and (> arg 0)
  815. (cdr (assoc (org-get-bullet) org-list-demote-modify-bullet)))))
  816. (when (org-item-has-child-p)
  817. (org-end-of-item-or-at-child)
  818. (org-apply-on-list fix-list nil)))))
  819. (org-apply-on-list fix-list nil))))))
  820. t)
  821. (defun org-item-indent-positions ()
  822. "Return indentation for plain list items.
  823. This returns a list with three values: The current indentation, the
  824. parent indentation and the indentation a child should have.
  825. Assumes cursor in item line."
  826. (let* ((bolpos (point-at-bol))
  827. (ind (org-get-indentation))
  828. (bullet (org-get-bullet))
  829. ind-down ind-up bullet-up bullet-down pos)
  830. (save-excursion
  831. (org-beginning-of-item-list)
  832. (skip-chars-backward "\n\r \t")
  833. (when (org-in-item-p)
  834. (org-beginning-of-item)
  835. (let ((prev-indent (org-get-indentation)))
  836. (when (< prev-indent ind)
  837. (setq ind-up prev-indent)
  838. (setq bullet-up (org-get-bullet))))))
  839. (setq pos (point))
  840. (save-excursion
  841. (cond
  842. ((and (ignore-errors (progn (org-previous-item) t))
  843. (or (end-of-line) t)
  844. (org-search-forward-unenclosed org-item-beginning-re bolpos t))
  845. (setq ind-down (org-get-indentation)
  846. bullet-down (org-get-bullet)))
  847. ((and (goto-char pos)
  848. (org-at-item-p))
  849. (goto-char (match-end 0))
  850. (skip-chars-forward " \t")
  851. (setq ind-down (current-column)
  852. bullet-down (org-get-bullet)))))
  853. (if (and bullet-down (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-down))
  854. (setq bullet-down (concat "1" (match-string 1 bullet-down))))
  855. (if (and bullet-up (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-up))
  856. (setq bullet-up (concat "1" (match-string 1 bullet-up))))
  857. (if (and bullet (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet))
  858. (setq bullet (concat "1" (match-string 1 bullet))))
  859. (list (cons ind bullet)
  860. (cons ind-up bullet-up)
  861. (cons ind-down bullet-down))))
  862. (defvar org-tab-ind-state)
  863. (defun org-cycle-item-indentation ()
  864. (let ((org-suppress-item-indentation t)
  865. (org-adapt-indentation nil))
  866. (when (and (or (org-at-item-description-p) (org-at-item-checkbox-p) (org-at-item-p))
  867. (>= (match-end 0) (save-excursion
  868. (org-end-of-item-or-at-child)
  869. (skip-chars-backward " \r\t\n")
  870. (point))))
  871. (setq this-command 'org-cycle-item-indentation)
  872. ;; When in the middle of the cycle, try to outdent first. If it
  873. ;; fails, and point is still at initial position, indent. Else,
  874. ;; go back to original position.
  875. (if (eq last-command 'org-cycle-item-indentation)
  876. (cond
  877. ((ignore-errors (org-indent-item -1)))
  878. ((and (= (org-get-indentation) (car org-tab-ind-state))
  879. (ignore-errors (org-indent-item 1))))
  880. (t (back-to-indentation)
  881. (indent-to-column (car org-tab-ind-state))
  882. (end-of-line)
  883. (org-fix-bullet-type (nth 1 org-tab-ind-state))
  884. ;; Break cycle
  885. (setq this-command 'identity)))
  886. ;; If a cycle is starting, remember indentation and bullet,
  887. ;; then try to indent. If it fails, try to outdent.
  888. (setq org-tab-ind-state
  889. (list (org-get-indentation) (org-get-bullet)))
  890. (cond
  891. ((ignore-errors (org-indent-item 1)))
  892. ((ignore-errors (org-indent-item -1)))
  893. (t (error "Cannot move item"))))
  894. t)))
  895. ;;; Bullets
  896. (defun org-get-bullet ()
  897. (and (org-at-item-p)
  898. (org-trim (match-string 1))))
  899. (defun org-list-bullet-string (bullet)
  900. "Concatenate BULLET with an appropriate number of whitespaces.
  901. It determines the number of whitespaces to append by looking at
  902. `org-list-two-spaces-after-bullet-regexp'."
  903. (concat
  904. bullet " "
  905. ;; Do we need to concat another white space ?
  906. (when (string-match org-list-two-spaces-after-bullet-regexp bullet) " ")))
  907. (defun org-list-replace-bullet (new-bullet)
  908. "Replace current item's bullet with NEW-BULLET.
  909. Assume point is at item. Indent body if needed."
  910. (save-excursion
  911. (let ((old (progn
  912. (looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
  913. (match-string 1))))
  914. (unless (equal new-bullet old)
  915. (replace-match new-bullet nil nil nil 1)
  916. ;; When bullet lengths are differents, move the whole
  917. ;; sublist accordingly
  918. (org-shift-item-indentation (- (length new-bullet) (length old)))))))
  919. (defun org-fix-bullet-type (&optional force-bullet)
  920. "Make sure all items in this list have the same bullet as the first item.
  921. Also, fix the indentation."
  922. (interactive)
  923. (unless (org-at-item-p) (error "This is not a list"))
  924. (org-preserve-lc
  925. (let* ((ini-bul (progn (org-beginning-of-item-list) (org-get-bullet)))
  926. (bullet (org-list-bullet-string (or force-bullet ini-bul)))
  927. (replace-bullet
  928. (lambda (result bullet)
  929. (org-list-replace-bullet bullet))))
  930. (org-apply-on-list replace-bullet nil bullet)
  931. (org-maybe-renumber-ordered-list))))
  932. (defun org-renumber-ordered-list (&optional arg)
  933. "Renumber an ordered plain list.
  934. Cursor needs to be in the first line of an item, the line that starts
  935. with something like \"1.\" or \"2)\". Start to count at ARG or 1."
  936. (interactive "p")
  937. (unless (and (org-at-item-p)
  938. (match-beginning 3))
  939. (error "This is not an ordered list"))
  940. (org-preserve-lc
  941. (let* ((offset (progn
  942. (org-beginning-of-item)
  943. (or (and (looking-at "[ \t]*\\[@start:\\([0-9]+\\)")
  944. (string-to-number (match-string 1)))
  945. arg
  946. 1)))
  947. (item-fmt (progn
  948. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  949. (concat "%d" (or (match-string 1) "."))))
  950. ;; Here is the function applied at each item of the list.
  951. (renumber-item (lambda (counter off fmt)
  952. (let* ((new (format fmt (+ counter off)))
  953. (old (progn
  954. (looking-at org-item-beginning-re)
  955. (match-string 2)))
  956. (begin (match-beginning 2))
  957. (end (match-end 2)))
  958. (delete-region begin end)
  959. (goto-char begin)
  960. (insert new)
  961. ;; In case item number went from 9. to 10.
  962. ;; or the other way.
  963. (org-shift-item-indentation (- (length new) (length old)))
  964. (1+ counter)))))
  965. (org-apply-on-list renumber-item 0 offset item-fmt))))
  966. (defun org-maybe-renumber-ordered-list ()
  967. "Renumber the ordered list at point if setup allows it.
  968. This tests the if 'renumber rule is set in
  969. `org-list-automatic-rules' before doing the renumbering.
  970. Do not throw error on failure."
  971. (interactive)
  972. (when (cdr (assq 'renumber org-list-automatic-rules))
  973. (ignore-errors (org-renumber-ordered-list))))
  974. (defun org-cycle-list-bullet (&optional which)
  975. "Cycle through the different itemize/enumerate bullets.
  976. This cycle the entire list level through the sequence:
  977. `-' -> `+' -> `*' -> `1.' -> `1)'
  978. If WHICH is a valid string, use that as the new bullet. If WHICH
  979. is an integer, 0 means `-', 1 means `+' etc. If WHICH is
  980. 'previous, cycle backwards."
  981. (interactive "P")
  982. (org-preserve-lc
  983. (let* ((bullet (progn (org-beginning-of-item-list)
  984. (org-get-bullet)))
  985. (current (cond
  986. ((string-match "\\." bullet) "1.")
  987. ((string-match ")" bullet) "1)")
  988. (t bullet)))
  989. (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules)))
  990. (bullet-list (append '("-" "+" )
  991. ;; *-bullets are not allowed at column 0
  992. (unless (and bullet-rule-p
  993. (looking-at "\\S-")) '("*"))
  994. ;; Description items cannot be numbered
  995. (unless (and bullet-rule-p
  996. (or (eq org-plain-list-ordered-item-terminator ?.)
  997. (org-at-item-description-p))) '("1)"))
  998. (unless (and bullet-rule-p
  999. (or (eq org-plain-list-ordered-item-terminator ?\))
  1000. (org-at-item-description-p))) '("1."))))
  1001. (len (length bullet-list))
  1002. (item-index (- len (length (member current bullet-list))))
  1003. (get-value (lambda (index) (nth (mod index len) bullet-list)))
  1004. (new (cond
  1005. ((member which bullet-list) which)
  1006. ((numberp which) (funcall get-value which))
  1007. ((eq 'previous which) (funcall get-value (1- item-index)))
  1008. (t (funcall get-value (1+ item-index))))))
  1009. (org-fix-bullet-type new))))
  1010. ;;; Checkboxes
  1011. (defun org-toggle-checkbox (&optional toggle-presence)
  1012. "Toggle the checkbox in the current line.
  1013. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
  1014. With double prefix, set checkbox to [-].
  1015. When there is an active region, toggle status or presence of the checkbox
  1016. in the first line, and make every item in the region have the same
  1017. status or presence, respectively.
  1018. If the cursor is in a headline, apply this to all checkbox items in the
  1019. text below the heading."
  1020. (interactive "P")
  1021. (catch 'exit
  1022. (let (beg end status first-present first-status blocked)
  1023. (cond
  1024. ((org-region-active-p)
  1025. (setq beg (region-beginning) end (region-end)))
  1026. ((org-on-heading-p)
  1027. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  1028. ((org-at-item-checkbox-p)
  1029. (save-excursion
  1030. (if (equal toggle-presence '(4))
  1031. (progn
  1032. (replace-match "" nil nil nil 1)
  1033. (goto-char (match-beginning 0))
  1034. (just-one-space))
  1035. (when (setq blocked (org-checkbox-blocked-p))
  1036. (error "Checkbox blocked because of unchecked box in line %d"
  1037. blocked))
  1038. (replace-match
  1039. (cond ((equal toggle-presence '(16)) "[-]")
  1040. ((member (match-string 1) '("[ ]" "[-]")) "[X]")
  1041. (t "[ ]"))
  1042. t t nil 1)))
  1043. (throw 'exit t))
  1044. ((org-at-item-p)
  1045. ;; add a checkbox if point is not at a description item
  1046. (save-excursion
  1047. (goto-char (match-end 0))
  1048. (if (org-at-item-description-p)
  1049. (error "Cannot add a checkbox in a description list")
  1050. (insert "[ ] ")))
  1051. (throw 'exit t))
  1052. (t (error "Not at a checkbox or heading, and no active region")))
  1053. (setq end (move-marker (make-marker) end))
  1054. (save-excursion
  1055. (goto-char beg)
  1056. (setq first-present (org-at-item-checkbox-p)
  1057. first-status
  1058. (save-excursion
  1059. (and (org-search-forward-unenclosed "[ \t]\\(\\[[ X]\\]\\)" end t)
  1060. (equal (match-string 0) "[X]"))))
  1061. (while (< (point) end)
  1062. (if toggle-presence
  1063. (cond
  1064. ((and first-present (org-at-item-checkbox-p))
  1065. (save-excursion
  1066. (replace-match "")
  1067. (goto-char (match-beginning 0))
  1068. (just-one-space)))
  1069. ((and (not first-present) (not (org-at-item-checkbox-p))
  1070. (org-at-item-p))
  1071. (save-excursion
  1072. (goto-char (match-end 0))
  1073. (insert "[ ] "))))
  1074. (when (org-at-item-checkbox-p)
  1075. (setq status (equal (match-string 1) "[X]"))
  1076. (replace-match
  1077. (if first-status "[ ]" "[X]") t t nil 1)))
  1078. (beginning-of-line 2)))))
  1079. (org-update-checkbox-count-maybe))
  1080. (defun org-reset-checkbox-state-subtree ()
  1081. "Reset all checkboxes in an entry subtree."
  1082. (interactive "*")
  1083. (save-restriction
  1084. (save-excursion
  1085. (org-narrow-to-subtree)
  1086. (org-show-subtree)
  1087. (goto-char (point-min))
  1088. (let ((end (point-max)))
  1089. (while (< (point) end)
  1090. (when (org-at-item-checkbox-p)
  1091. (replace-match "[ ]" t t nil 1))
  1092. (beginning-of-line 2))))
  1093. (org-update-checkbox-count-maybe)))
  1094. (defvar org-checkbox-statistics-hook nil
  1095. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  1096. This hook runs even if 'checkbox rules in
  1097. `org-list-automatic-rules' do not apply, so it can be used to
  1098. implement alternative ways of collecting statistics
  1099. information.")
  1100. (defun org-update-checkbox-count-maybe ()
  1101. "Update checkbox statistics unless turned off by user."
  1102. (when (cdr (assq 'checkbox org-list-automatic-rules))
  1103. (org-update-checkbox-count))
  1104. (run-hooks 'org-checkbox-statistics-hook))
  1105. (defun org-update-checkbox-count (&optional all)
  1106. "Update the checkbox statistics in the current section.
  1107. This will find all statistic cookies like [57%] and [6/12] and update them
  1108. with the current numbers. With optional prefix argument ALL, do this for
  1109. the whole buffer."
  1110. (interactive "P")
  1111. (save-excursion
  1112. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  1113. (beg (condition-case nil
  1114. (progn (org-back-to-heading) (point))
  1115. (error (point-min))))
  1116. (end (move-marker (make-marker)
  1117. (progn (outline-next-heading) (point))))
  1118. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  1119. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  1120. (re-find (concat re "\\|" re-box))
  1121. beg-cookie end-cookie is-percent c-on c-off lim new
  1122. eline curr-ind next-ind continue-from startsearch
  1123. (recursive
  1124. (or (not org-hierarchical-checkbox-statistics)
  1125. (string-match "\\<recursive\\>"
  1126. (or (ignore-errors
  1127. (org-entry-get nil "COOKIE_DATA"))
  1128. ""))))
  1129. (cstat 0)
  1130. )
  1131. (when all
  1132. (goto-char (point-min))
  1133. (outline-next-heading)
  1134. (setq beg (point) end (point-max)))
  1135. (goto-char end)
  1136. ;; find each statistics cookie
  1137. (while (and (org-search-backward-unenclosed re-find beg t)
  1138. (not (save-match-data
  1139. (and (org-on-heading-p)
  1140. (string-match "\\<todo\\>"
  1141. (downcase
  1142. (or (org-entry-get
  1143. nil "COOKIE_DATA")
  1144. "")))))))
  1145. (setq beg-cookie (match-beginning 1)
  1146. end-cookie (match-end 1)
  1147. cstat (+ cstat (if end-cookie 1 0))
  1148. startsearch (point-at-eol)
  1149. continue-from (match-beginning 0)
  1150. is-percent (match-beginning 2)
  1151. lim (cond
  1152. ((org-on-heading-p) (outline-next-heading) (point))
  1153. ((org-at-item-p) (org-end-of-item) (point))
  1154. (t nil))
  1155. c-on 0
  1156. c-off 0)
  1157. (when lim
  1158. ;; find first checkbox for this cookie and gather
  1159. ;; statistics from all that are at this indentation level
  1160. (goto-char startsearch)
  1161. (if (org-search-forward-unenclosed re-box lim t)
  1162. (progn
  1163. (org-beginning-of-item)
  1164. (setq curr-ind (org-get-indentation))
  1165. (setq next-ind curr-ind)
  1166. (while (and (bolp) (org-at-item-p)
  1167. (if recursive
  1168. (<= curr-ind next-ind)
  1169. (= curr-ind next-ind)))
  1170. (setq eline (point-at-eol))
  1171. (if (org-search-forward-unenclosed re-box eline t)
  1172. (if (member (match-string 2) '("[ ]" "[-]"))
  1173. (setq c-off (1+ c-off))
  1174. (setq c-on (1+ c-on))))
  1175. (if (not recursive)
  1176. ;; org-get-next-item goes through list-enders
  1177. ;; with proper limit.
  1178. (goto-char (or (org-get-next-item (point) lim) lim))
  1179. (end-of-line)
  1180. (when (org-search-forward-unenclosed org-item-beginning-re lim t)
  1181. (beginning-of-line)))
  1182. (setq next-ind (org-get-indentation)))))
  1183. (goto-char continue-from)
  1184. ;; update cookie
  1185. (when end-cookie
  1186. (setq new (if is-percent
  1187. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  1188. (format "[%d/%d]" c-on (+ c-on c-off))))
  1189. (goto-char beg-cookie)
  1190. (insert new)
  1191. (delete-region (point) (+ (point) (- end-cookie beg-cookie))))
  1192. ;; update items checkbox if it has one
  1193. (when (org-at-item-p)
  1194. (org-beginning-of-item)
  1195. (when (and (> (+ c-on c-off) 0)
  1196. (org-search-forward-unenclosed re-box (point-at-eol) t))
  1197. (setq beg-cookie (match-beginning 2)
  1198. end-cookie (match-end 2))
  1199. (delete-region beg-cookie end-cookie)
  1200. (goto-char beg-cookie)
  1201. (cond ((= c-off 0) (insert "[X]"))
  1202. ((= c-on 0) (insert "[ ]"))
  1203. (t (insert "[-]")))
  1204. )))
  1205. (goto-char continue-from))
  1206. (when (interactive-p)
  1207. (message "Checkbox statistics updated %s (%d places)"
  1208. (if all "in entire file" "in current outline entry") cstat)))))
  1209. (defun org-get-checkbox-statistics-face ()
  1210. "Select the face for checkbox statistics.
  1211. The face will be `org-done' when all relevant boxes are checked.
  1212. Otherwise it will be `org-todo'."
  1213. (if (match-end 1)
  1214. (if (equal (match-string 1) "100%")
  1215. 'org-checkbox-statistics-done
  1216. 'org-checkbox-statistics-todo)
  1217. (if (and (> (match-end 2) (match-beginning 2))
  1218. (equal (match-string 2) (match-string 3)))
  1219. 'org-checkbox-statistics-done
  1220. 'org-checkbox-statistics-todo)))
  1221. ;;; Misc Tools
  1222. (defun org-apply-on-list (function init-value &rest args)
  1223. "Call FUNCTION for each item of a the list under point.
  1224. FUNCTION must be called with at least one argument : a return
  1225. value that will contain the value returned by the function at
  1226. the previous item, plus ARGS extra arguments. INIT-VALUE will be
  1227. the value passed to the function at the first item of the list.
  1228. As an example, (org-apply-on-list (lambda (result) (1+ result)) 0)
  1229. will return the number of items in the current list.
  1230. Sublists of the list are skipped. Cursor is always at the
  1231. beginning of the item."
  1232. (save-excursion
  1233. (let ((end (copy-marker (save-excursion (org-end-of-item-list))))
  1234. (next-p (make-marker))
  1235. (move-down-action
  1236. (lambda (pos value &rest args)
  1237. (goto-char pos)
  1238. (set-marker next-p (org-get-next-item pos end))
  1239. (let ((return-value (apply function value args)))
  1240. (if (marker-position next-p)
  1241. (apply move-down-action next-p return-value args)
  1242. return-value)))))
  1243. (apply move-down-action (org-beginning-of-item-list) init-value args))))
  1244. (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
  1245. "Sort plain list items.
  1246. The cursor may be at any item of the list that should be sorted.
  1247. Sublists are not sorted. Checkboxes, if any, are ignored.
  1248. Sorting can be alphabetically, numerically, by date/time as given by
  1249. a time stamp, by a property or by priority.
  1250. The command prompts for the sorting type unless it has been given
  1251. to the function through the SORTING-TYPE argument, which needs to
  1252. be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
  1253. meaning of each character:
  1254. n Numerically, by converting the beginning of the item to a number.
  1255. a Alphabetically.
  1256. t By date/time, either the first active time stamp in the entry, if
  1257. any, or by the first inactive one. In a timer list, sorts the timers.
  1258. Only the first line of item is checked.
  1259. Capital letters will reverse the sort order.
  1260. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a
  1261. function to be called with point at the beginning of the record.
  1262. It must return either a string or a number that should serve as
  1263. the sorting key for that record.
  1264. Comparing entries ignores case by default. However, with an
  1265. optional argument WITH-CASE, the sorting considers case as well."
  1266. (interactive "P")
  1267. (let* ((case-func (if with-case 'identity 'downcase))
  1268. (start (org-beginning-of-item-list))
  1269. (end (save-excursion (org-end-of-item-list)))
  1270. (sorting-type
  1271. (progn
  1272. (message
  1273. "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
  1274. (read-char-exclusive)))
  1275. (getkey-func (and (= (downcase sorting-type) ?f)
  1276. (org-icompleting-read "Sort using function: "
  1277. obarray 'fboundp t nil nil)
  1278. (intern getkey-func))))
  1279. (message "Sorting items...")
  1280. (save-restriction
  1281. (narrow-to-region start end)
  1282. (let* ((dcst (downcase sorting-type))
  1283. (case-fold-search nil)
  1284. (now (current-time))
  1285. (sort-func (cond
  1286. ((= dcst ?a) 'string<)
  1287. ((= dcst ?f) compare-func)
  1288. ((= dcst ?t) '<)
  1289. (t nil)))
  1290. (begin-record (lambda ()
  1291. (skip-chars-forward " \r\t\n")
  1292. (beginning-of-line)))
  1293. (end-record (lambda ()
  1294. (goto-char (org-end-of-item-before-blank))))
  1295. (value-to-sort
  1296. (lambda ()
  1297. (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
  1298. (cond
  1299. ((= dcst ?n)
  1300. (string-to-number (buffer-substring (match-end 0)
  1301. (point-at-eol))))
  1302. ((= dcst ?a)
  1303. (buffer-substring (match-end 0) (point-at-eol)))
  1304. ((= dcst ?t)
  1305. (cond
  1306. ;; If it is a timer list, convert timer to seconds
  1307. ((org-at-item-timer-p)
  1308. (org-timer-hms-to-secs (match-string 1)))
  1309. ((or (org-search-forward-unenclosed org-ts-regexp
  1310. (point-at-eol) t)
  1311. (org-search-forward-unenclosed org-ts-regexp-both
  1312. (point-at-eol) t))
  1313. (org-time-string-to-seconds (match-string 0)))
  1314. (t (org-float-time now))))
  1315. ((= dcst ?f)
  1316. (if getkey-func
  1317. (let ((value (funcall getkey-func)))
  1318. (if (stringp value)
  1319. (funcall case-func value)
  1320. value))
  1321. (error "Invalid key function `%s'" getkey-func)))
  1322. (t (error "Invalid sorting type `%c'" sorting-type)))))))
  1323. (sort-subr (/= dcst sorting-type) begin-record end-record value-to-sort nil sort-func)
  1324. (org-maybe-renumber-ordered-list)
  1325. (run-hooks 'org-after-sorting-entries-or-items-hook)
  1326. (message "Sorting items...done")))))
  1327. ;;; Send and receive lists
  1328. (defun org-list-parse-list (&optional delete)
  1329. "Parse the list at point and maybe DELETE it.
  1330. Return a list containing first level items as strings and
  1331. sublevels as a list of strings."
  1332. (let* ((start (goto-char (org-list-top-point)))
  1333. (end (org-list-bottom-point))
  1334. output itemsep ltype)
  1335. (while (org-search-forward-unenclosed org-item-beginning-re end t)
  1336. (save-excursion
  1337. (beginning-of-line)
  1338. (setq ltype (cond ((looking-at-p "^[ \t]*[0-9]") 'ordered)
  1339. ((org-at-item-description-p) 'descriptive)
  1340. (t 'unordered))))
  1341. (let* ((indent1 (org-get-indentation))
  1342. (nextitem (or (org-get-next-item (point) end) end))
  1343. (item (org-trim (buffer-substring (point) (org-end-of-item-or-at-child))))
  1344. (nextindent (if (= (point) end) 0 (org-get-indentation)))
  1345. (item (if (string-match "^\\[\\([xX ]\\)\\]" item)
  1346. (replace-match (if (equal (match-string 1 item) " ")
  1347. "[CBOFF]"
  1348. "[CBON]")
  1349. t nil item)
  1350. item)))
  1351. (push item output)
  1352. (when (> nextindent indent1)
  1353. (save-restriction
  1354. (narrow-to-region (point) nextitem)
  1355. (push (org-list-parse-list) output)))))
  1356. (when delete
  1357. (delete-region start end)
  1358. (save-match-data
  1359. (when (looking-at (org-list-end-re))
  1360. (replace-match "\n"))))
  1361. (setq output (nreverse output))
  1362. (push ltype output)))
  1363. (defun org-list-make-subtree ()
  1364. "Convert the plain list at point into a subtree."
  1365. (interactive)
  1366. (if (not (org-in-item-p))
  1367. (error "Not in a list.")
  1368. (goto-char (org-list-top-point))
  1369. (let ((list (org-list-parse-list t)) nstars)
  1370. (save-excursion
  1371. (if (ignore-errors
  1372. (org-back-to-heading))
  1373. (progn (org-search-forward-unenclosed org-complex-heading-regexp nil t)
  1374. (setq nstars (length (match-string 1))))
  1375. (setq nstars 0)))
  1376. (org-list-make-subtrees list (1+ nstars)))))
  1377. (defun org-list-make-subtrees (list level)
  1378. "Convert LIST into subtrees starting at LEVEL."
  1379. (if (symbolp (car list))
  1380. (org-list-make-subtrees (cdr list) level)
  1381. (mapcar (lambda (item)
  1382. (if (stringp item)
  1383. (insert (make-string
  1384. (if org-odd-levels-only
  1385. (1- (* 2 level)) level) ?*) " " item "\n")
  1386. (org-list-make-subtrees item (1+ level))))
  1387. list)))
  1388. (defun org-list-insert-radio-list ()
  1389. "Insert a radio list template appropriate for this major mode."
  1390. (interactive)
  1391. (let* ((e (assq major-mode org-list-radio-list-templates))
  1392. (txt (nth 1 e))
  1393. name pos)
  1394. (unless e (error "No radio list setup defined for %s" major-mode))
  1395. (setq name (read-string "List name: "))
  1396. (while (string-match "%n" txt)
  1397. (setq txt (replace-match name t t txt)))
  1398. (or (bolp) (insert "\n"))
  1399. (setq pos (point))
  1400. (insert txt)
  1401. (goto-char pos)))
  1402. (defun org-list-send-list (&optional maybe)
  1403. "Send a transformed version of this list to the receiver position.
  1404. With argument MAYBE, fail quietly if no transformation is defined for
  1405. this list."
  1406. (interactive)
  1407. (catch 'exit
  1408. (unless (org-at-item-p) (error "Not at a list"))
  1409. (save-excursion
  1410. (re-search-backward "#\\+ORGLST" nil t)
  1411. (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
  1412. (if maybe
  1413. (throw 'exit nil)
  1414. (error "Don't know how to transform this list"))))
  1415. (let* ((name (match-string 1))
  1416. (transform (intern (match-string 2)))
  1417. (bottom-point
  1418. (save-excursion
  1419. (re-search-forward "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
  1420. (match-beginning 0)))
  1421. (top-point
  1422. (progn
  1423. (re-search-backward "#\\+ORGLST" nil t)
  1424. (re-search-forward org-item-beginning-re bottom-point t)
  1425. (match-beginning 0)))
  1426. (list (save-restriction
  1427. (narrow-to-region top-point bottom-point)
  1428. (org-list-parse-list)))
  1429. beg txt)
  1430. (unless (fboundp transform)
  1431. (error "No such transformation function %s" transform))
  1432. (let ((txt (funcall transform list)))
  1433. ;; Find the insertion place
  1434. (save-excursion
  1435. (goto-char (point-min))
  1436. (unless (re-search-forward
  1437. (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
  1438. (error "Don't know where to insert translated list"))
  1439. (goto-char (match-beginning 0))
  1440. (beginning-of-line 2)
  1441. (setq beg (point))
  1442. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  1443. (error "Cannot find end of insertion region"))
  1444. (delete-region beg (point-at-bol))
  1445. (goto-char beg)
  1446. (insert txt "\n")))
  1447. (message "List converted and installed at receiver location"))))
  1448. (defun org-list-to-generic (list params)
  1449. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  1450. Valid parameters PARAMS are
  1451. :ustart String to start an unordered list
  1452. :uend String to end an unordered list
  1453. :ostart String to start an ordered list
  1454. :oend String to end an ordered list
  1455. :dstart String to start a descriptive list
  1456. :dend String to end a descriptive list
  1457. :dtstart String to start a descriptive term
  1458. :dtend String to end a descriptive term
  1459. :ddstart String to start a description
  1460. :ddend String to end a description
  1461. :splice When set to t, return only list body lines, don't wrap
  1462. them into :[u/o]start and :[u/o]end. Default is nil.
  1463. :istart String to start a list item
  1464. :iend String to end a list item
  1465. :isep String to separate items
  1466. :lsep String to separate sublists
  1467. :cboff String to insert for an unchecked checkbox
  1468. :cbon String to insert for a checked checkbox"
  1469. (interactive)
  1470. (let* ((p params) sublist
  1471. (splicep (plist-get p :splice))
  1472. (ostart (plist-get p :ostart))
  1473. (oend (plist-get p :oend))
  1474. (ustart (plist-get p :ustart))
  1475. (uend (plist-get p :uend))
  1476. (dstart (plist-get p :dstart))
  1477. (dend (plist-get p :dend))
  1478. (dtstart (plist-get p :dtstart))
  1479. (dtend (plist-get p :dtend))
  1480. (ddstart (plist-get p :ddstart))
  1481. (ddend (plist-get p :ddend))
  1482. (istart (plist-get p :istart))
  1483. (iend (plist-get p :iend))
  1484. (isep (plist-get p :isep))
  1485. (lsep (plist-get p :lsep))
  1486. (cbon (plist-get p :cbon))
  1487. (cboff (plist-get p :cboff)))
  1488. (let ((wrapper
  1489. (cond ((eq (car list) 'ordered)
  1490. (concat ostart "\n%s" oend "\n"))
  1491. ((eq (car list) 'unordered)
  1492. (concat ustart "\n%s" uend "\n"))
  1493. ((eq (car list) 'descriptive)
  1494. (concat dstart "\n%s" dend "\n"))))
  1495. rtn term defstart defend)
  1496. (while (setq sublist (pop list))
  1497. (cond ((symbolp sublist) nil)
  1498. ((stringp sublist)
  1499. (when (string-match "^\\(\\S-+\\)[ \t]+::" sublist)
  1500. (setq term (org-trim (format (concat dtstart "%s" dtend)
  1501. (match-string 1 sublist))))
  1502. (setq sublist (concat ddstart
  1503. (org-trim (substring sublist (match-end 0)))
  1504. ddend)))
  1505. (if (string-match "\\[CBON\\]" sublist)
  1506. (setq sublist (replace-match cbon t t sublist)))
  1507. (if (string-match "\\[CBOFF\\]" sublist)
  1508. (setq sublist (replace-match cboff t t sublist)))
  1509. (if (string-match "\\[-\\]" sublist)
  1510. (setq sublist (replace-match "$\\boxminus$" t t sublist)))
  1511. (setq rtn (concat rtn istart term sublist iend isep)))
  1512. (t (setq rtn (concat rtn ;; previous list
  1513. lsep ;; list separator
  1514. (org-list-to-generic sublist p)
  1515. lsep ;; list separator
  1516. )))))
  1517. (format wrapper rtn))))
  1518. (defun org-list-to-latex (list &optional params)
  1519. "Convert LIST into a LaTeX list.
  1520. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1521. with overruling parameters for `org-list-to-generic'."
  1522. (org-list-to-generic
  1523. list
  1524. (org-combine-plists
  1525. '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  1526. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  1527. :dstart "\\begin{description}" :dend "\\end{description}"
  1528. :dtstart "[" :dtend "]"
  1529. :ddstart "" :ddend ""
  1530. :istart "\\item " :iend ""
  1531. :isep "\n" :lsep "\n"
  1532. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  1533. params)))
  1534. (defun org-list-to-html (list &optional params)
  1535. "Convert LIST into a HTML list.
  1536. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1537. with overruling parameters for `org-list-to-generic'."
  1538. (org-list-to-generic
  1539. list
  1540. (org-combine-plists
  1541. '(:splicep nil :ostart "<ol>" :oend "</ol>"
  1542. :ustart "<ul>" :uend "</ul>"
  1543. :dstart "<dl>" :dend "</dl>"
  1544. :dtstart "<dt>" :dtend "</dt>"
  1545. :ddstart "<dd>" :ddend "</dd>"
  1546. :istart "<li>" :iend "</li>"
  1547. :isep "\n" :lsep "\n"
  1548. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
  1549. params)))
  1550. (defun org-list-to-texinfo (list &optional params)
  1551. "Convert LIST into a Texinfo list.
  1552. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1553. with overruling parameters for `org-list-to-generic'."
  1554. (org-list-to-generic
  1555. list
  1556. (org-combine-plists
  1557. '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  1558. :ustart "@enumerate" :uend "@end enumerate"
  1559. :dstart "@table" :dend "@end table"
  1560. :dtstart "@item " :dtend "\n"
  1561. :ddstart "" :ddend ""
  1562. :istart "@item\n" :iend ""
  1563. :isep "\n" :lsep "\n"
  1564. :cbon "@code{[X]}" :cboff "@code{[ ]}")
  1565. params)))
  1566. (provide 'org-list)
  1567. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  1568. ;;; org-list.el ends here