org-list.el 62 KB

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