org-list.el 61 KB

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