org-list.el 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323
  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. (defvar org-outline-regexp)
  38. (defvar org-ts-regexp)
  39. (defvar org-ts-regexp-both)
  40. (declare-function org-invisible-p "org" ())
  41. (declare-function org-on-heading-p "org" (&optional invisible-ok))
  42. (declare-function outline-next-heading "outline" ())
  43. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  44. (declare-function org-back-over-empty-lines "org" ())
  45. (declare-function org-trim "org" (s))
  46. (declare-function org-get-indentation "org" (&optional line))
  47. (declare-function org-timer-item "org-timer" (&optional arg))
  48. (declare-function org-timer-hms-to-secs "org-timer" (hms))
  49. (declare-function org-combine-plists "org" (&rest plists))
  50. (declare-function org-entry-get "org"
  51. (pom property &optional inherit literal-nil))
  52. (declare-function org-narrow-to-subtree "org" ())
  53. (declare-function org-show-subtree "org" ())
  54. (declare-function org-in-regexps-block-p "org"
  55. (start-re end-re &optional bound))
  56. (declare-function org-level-increment "org" ())
  57. (declare-function org-at-heading-p "org" (&optional ignored))
  58. (declare-function outline-previous-heading "outline" ())
  59. (declare-function org-icompleting-read "org" (&rest args))
  60. (declare-function org-time-string-to-seconds "org" (s))
  61. (defgroup org-plain-lists nil
  62. "Options concerning plain lists in Org-mode."
  63. :tag "Org Plain lists"
  64. :group 'org-structure)
  65. (defcustom org-cycle-include-plain-lists t
  66. "When t, make TAB cycle visibility on plain list items.
  67. Cycling plain lists works only when the cursor is on a plain list
  68. item. When the cursor is on an outline heading, plain lists are
  69. treated as text. This is the most stable way of handling this,
  70. which is why it is the default.
  71. When this is the symbol `integrate', then during cycling, plain
  72. list items will *temporarily* be interpreted as outline headlines
  73. with a level given by 1000+i where i is the indentation of the
  74. bullet. This setting can lead to strange effects when switching
  75. visibility to `children', because the first \"child\" in a
  76. subtree decides what children should be listed. If that first
  77. \"child\" is a plain list item with an implied large level
  78. number, all true children and grand children of the outline
  79. heading will be exposed in a children' view."
  80. :group 'org-plain-lists
  81. :type '(choice
  82. (const :tag "Never" nil)
  83. (const :tag "With cursor in plain list (recommended)" t)
  84. (const :tag "As children of outline headings" integrate)))
  85. (defcustom org-list-demote-modify-bullet nil
  86. "Default bullet type installed when demoting an item.
  87. This is an association list, for each bullet type, this alist will point
  88. to the bullet that should be used when this item is demoted.
  89. For example,
  90. (setq org-list-demote-modify-bullet
  91. '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
  92. will make
  93. + Movies
  94. + Silence of the Lambs
  95. + My Cousin Vinny
  96. + Books
  97. + The Hunt for Red October
  98. + The Road to Omaha
  99. into
  100. + Movies
  101. - Silence of the Lambs
  102. - My Cousin Vinny
  103. + Books
  104. - The Hunt for Red October
  105. - The Road to Omaha"
  106. :group 'org-plain-lists
  107. :type '(repeat
  108. (cons
  109. (choice :tag "If the current bullet is "
  110. (const "-")
  111. (const "+")
  112. (const "*")
  113. (const "1.")
  114. (const "1)"))
  115. (choice :tag "demotion will change it to"
  116. (const "-")
  117. (const "+")
  118. (const "*")
  119. (const "1.")
  120. (const "1)")))))
  121. (defcustom org-plain-list-ordered-item-terminator t
  122. "The character that makes a line with leading number an ordered list item.
  123. Valid values are ?. and ?\). To get both terminators, use t. While
  124. ?. may look nicer, it creates the danger that a line with leading
  125. number may be incorrectly interpreted as an item. ?\) therefore is
  126. the safe choice."
  127. :group 'org-plain-lists
  128. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  129. (const :tag "paren like in \"2)\"" ?\))
  130. (const :tab "both" t)))
  131. (defcustom org-list-two-spaces-after-bullet-regexp nil
  132. "A regular expression matching bullets that should have 2 spaces after them.
  133. When nil, no bullet will have two spaces after them.
  134. When a string, it will be used as a regular expression. When the
  135. bullet type of a list is changed, the new bullet type will be
  136. matched against this regexp. If it matches, there will be two
  137. spaces instead of one after the bullet in each item of the list."
  138. :group 'org-plain-lists
  139. :type '(choice
  140. (const :tag "never" nil)
  141. (regexp)))
  142. (defcustom org-list-ending-method 'both
  143. "Determine where plain lists should end.
  144. Valid values are: `regexp', `indent' or `both'.
  145. When set to `regexp', Org will look into two variables,
  146. `org-empty-line-terminates-plain-lists' and the more general
  147. `org-list-end-regexp', to determine what will end lists. This is
  148. the fastest method.
  149. When set to `indent', a list will end whenever a line following
  150. an item, but not starting one, is less or equally indented than
  151. it.
  152. When set to `both', each of the preceding methods is applied to
  153. determine lists endings. This is the default method."
  154. :group 'org-plain-lists
  155. :type '(choice
  156. (const :tag "With a regexp defining ending" regexp)
  157. (const :tag "With indentation of regular (no bullet) text" indent)
  158. (const :tag "With both methods" both)))
  159. (defcustom org-empty-line-terminates-plain-lists nil
  160. "Non-nil means an empty line ends all plain list levels.
  161. This variable only makes sense if `org-list-ending-method' is set
  162. to `regexp' or `both'. This is then equivalent to set
  163. `org-list-end-regexp' to \"^[ \\t]*$\"."
  164. :group 'org-plain-lists
  165. :type 'boolean)
  166. (defcustom org-list-end-regexp "^[ \t]*\n[ \t]*\n"
  167. "Regexp matching the end of all plain list levels.
  168. It must start with \"^\" and end with \"\\n\". It defaults to 2
  169. blank lines. `org-empty-line-terminates-plain-lists' has
  170. precedence over it."
  171. :group 'org-plain-lists
  172. :type 'string)
  173. (defcustom org-list-automatic-rules '((bullet . t)
  174. (checkbox . t)
  175. (indent . t)
  176. (insert . t))
  177. "Non-nil means apply set of rules when acting on lists.
  178. By default, automatic actions are taken when using
  179. \\[org-shiftmetaup], \\[org-shiftmetadown], \\[org-meta-return],
  180. \\[org-metaright], \\[org-metaleft], \\[org-shiftmetaright],
  181. \\[org-shiftmetaleft], \\[org-ctrl-c-minus],
  182. \\[org-toggle-checkbox] or \\[org-insert-todo-heading]. You can
  183. disable individually these rules by setting them to nil. Valid
  184. rules are:
  185. bullet when non-nil, cycling bullet do not allow lists at
  186. column 0 to have * as a bullet and descriptions lists
  187. to be numbered.
  188. checkbox when non-nil, checkbox statistics is updated each time
  189. you either insert a new checkbox or toggle a checkbox.
  190. It also prevents from inserting a checkbox in a
  191. description item.
  192. indent when non-nil, indenting or outdenting list top-item
  193. with its subtree will move the whole list and
  194. outdenting a list whose bullet is * to column 0 will
  195. change that bullet to -
  196. insert when non-nil, trying to insert an item inside a block
  197. will insert it right before the block instead of
  198. throwing an error."
  199. :group 'org-plain-lists
  200. :type '(alist :tag "Sets of rules"
  201. :key-type
  202. (choice
  203. (const :tag "Bullet" bullet)
  204. (const :tag "Checkbox" checkbox)
  205. (const :tag "Indent" indent)
  206. (const :tag "Insert" insert))
  207. :value-type
  208. (boolean :tag "Activate" :value t)))
  209. (defcustom org-hierarchical-checkbox-statistics t
  210. "Non-nil means checkbox statistics counts only the state of direct children.
  211. When nil, all boxes below the cookie are counted.
  212. This can be set to nil on a per-node basis using a COOKIE_DATA property
  213. with the word \"recursive\" in the value."
  214. :group 'org-plain-lists
  215. :type 'boolean)
  216. (defcustom org-description-max-indent 20
  217. "Maximum indentation for the second line of a description list.
  218. When the indentation would be larger than this, it will become
  219. 5 characters instead."
  220. :group 'org-plain-lists
  221. :type 'integer)
  222. (defcustom org-list-radio-list-templates
  223. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  224. % END RECEIVE ORGLST %n
  225. \\begin{comment}
  226. #+ORGLST: SEND %n org-list-to-latex
  227. -
  228. \\end{comment}\n")
  229. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  230. @c END RECEIVE ORGLST %n
  231. @ignore
  232. #+ORGLST: SEND %n org-list-to-texinfo
  233. -
  234. @end ignore\n")
  235. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  236. <!-- END RECEIVE ORGLST %n -->
  237. <!--
  238. #+ORGLST: SEND %n org-list-to-html
  239. -
  240. -->\n"))
  241. "Templates for radio lists in different major modes.
  242. All occurrences of %n in a template will be replaced with the name of the
  243. list, obtained by prompting the user."
  244. :group 'org-plain-lists
  245. :type '(repeat
  246. (list (symbol :tag "Major mode")
  247. (string :tag "Format"))))
  248. ;;; Internal functions
  249. (defun org-list-end-re ()
  250. "Return the regex corresponding to the end of a list.
  251. It depends on `org-empty-line-terminates-plain-lists'."
  252. (if org-empty-line-terminates-plain-lists
  253. "^[ \t]*\n"
  254. org-list-end-regexp))
  255. (defun org-item-re (&optional general)
  256. "Return the correct regular expression for plain lists.
  257. If GENERAL is non-nil, return the general regexp independent of the value
  258. of `org-plain-list-ordered-item-terminator'."
  259. (cond
  260. ((or general (eq org-plain-list-ordered-item-terminator t))
  261. "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  262. ((= org-plain-list-ordered-item-terminator ?.)
  263. "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  264. ((= org-plain-list-ordered-item-terminator ?\))
  265. "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  266. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'"))))
  267. (defconst org-item-beginning-re (concat "^" (org-item-re))
  268. "Regexp matching the beginning of a plain list item.")
  269. (defun org-list-ending-between (min max &optional firstp)
  270. "Find the position of a list ending between MIN and MAX, or nil.
  271. This function looks for `org-list-end-re' outside a block.
  272. If FIRSTP in non-nil, return the point at the beginning of the
  273. nearest valid terminator from MIN. Otherwise, return the point at
  274. the end of the nearest terminator from MAX."
  275. (save-excursion
  276. (let* ((start (if firstp min max))
  277. (end (if firstp max min))
  278. (search-fun (if firstp
  279. #'org-search-forward-unenclosed
  280. #'org-search-backward-unenclosed))
  281. (list-end-p (progn
  282. (goto-char start)
  283. (funcall search-fun (org-list-end-re) end t))))
  284. ;; Is there a valid list ending somewhere ?
  285. (and list-end-p
  286. ;; we want to be on the first line of the list ender
  287. (match-beginning 0)))))
  288. (defun org-list-maybe-skip-block (search limit)
  289. "Return non-nil value if point is in a block, skipping it on the way.
  290. It looks for the boundary of the block in SEARCH direction,
  291. stopping at LIMIT."
  292. (save-match-data
  293. (let ((case-fold-search t)
  294. (boundary (if (eq search 're-search-forward) 3 5)))
  295. (when (save-excursion
  296. (and (funcall search "^[ \t]*#\\+\\(begin\\|end\\)_" limit t)
  297. (= (length (match-string 1)) boundary)))
  298. ;; We're in a block: get out of it
  299. (goto-char (match-beginning 0))))))
  300. (defun org-list-search-unenclosed-generic (search re bound noerr)
  301. "Search a string outside blocks and protected places.
  302. Arguments SEARCH, RE, BOUND and NOERR are similar to those in
  303. `search-forward', `search-backward', `re-search-forward' and
  304. `re-search-backward'."
  305. (catch 'exit
  306. (let ((origin (point)))
  307. (while t
  308. ;; 1. No match: return to origin or bound, depending on NOERR.
  309. (unless (funcall search re bound noerr)
  310. (throw 'exit (and (goto-char (if (booleanp noerr) origin bound))
  311. nil)))
  312. ;; 2. Match not in block or protected: return point. Else
  313. ;; skip the block and carry on.
  314. (unless (or (get-text-property (match-beginning 0) 'org-protected)
  315. (org-list-maybe-skip-block search bound))
  316. (throw 'exit (point)))))))
  317. (defun org-search-backward-unenclosed (regexp &optional bound noerror)
  318. "Like `re-search-backward' but don't stop inside blocks or protected places.
  319. Arguments REGEXP, BOUND and NOERROR are similar to those used in
  320. `re-search-backward'."
  321. (org-list-search-unenclosed-generic
  322. #'re-search-backward regexp (or bound (point-min)) noerror))
  323. (defun org-search-forward-unenclosed (regexp &optional bound noerror)
  324. "Like `re-search-forward' but don't stop inside blocks or protected places.
  325. Arguments REGEXP, BOUND and NOERROR are similar to those used in
  326. `re-search-forward'."
  327. (org-list-search-unenclosed-generic
  328. #'re-search-forward regexp (or bound (point-max)) noerror))
  329. (defun org-list-in-item-p-with-indent (limit)
  330. "Is the cursor inside a plain list?
  331. Plain lists are considered ending when a non-blank line is less
  332. indented than the previous item within LIMIT."
  333. (save-excursion
  334. (beginning-of-line)
  335. (cond
  336. ;; do not start searching inside a block...
  337. ((org-list-maybe-skip-block #'re-search-backward limit))
  338. ;; ... or at a blank line
  339. ((looking-at "^[ \t]*$")
  340. (skip-chars-backward " \r\t\n")
  341. (beginning-of-line)))
  342. (beginning-of-line)
  343. (or (org-at-item-p)
  344. (let* ((case-fold-search t)
  345. (ind-ref (org-get-indentation))
  346. ;; Ensure there is at least an item above
  347. (up-item-p (save-excursion
  348. (org-search-backward-unenclosed
  349. org-item-beginning-re limit t))))
  350. (and up-item-p
  351. (catch 'exit
  352. (while t
  353. (cond
  354. ((org-at-item-p)
  355. (throw 'exit (< (org-get-indentation) ind-ref)))
  356. ((looking-at "^[ \t]*$")
  357. (skip-chars-backward " \r\t\n")
  358. (beginning-of-line))
  359. ((looking-at "^[ \t]*#\\+end_")
  360. (re-search-backward "^[ \t]*#\\+begin_"))
  361. (t
  362. (setq ind-ref (min (org-get-indentation) ind-ref))
  363. (forward-line -1))))))))))
  364. (defun org-list-in-item-p-with-regexp (limit)
  365. "Is the cursor inside a plain list?
  366. Plain lists end when `org-list-end-regexp' is matched, or at a
  367. blank line if `org-empty-line-terminates-plain-lists' is true.
  368. Argument LIMIT specifies the upper-bound of the search."
  369. (save-excursion
  370. (let* ((actual-pos (goto-char (point-at-eol)))
  371. ;; Moved to eol so current line can be matched by
  372. ;; `org-item-re'.
  373. (last-item-start (save-excursion
  374. (org-search-backward-unenclosed
  375. org-item-beginning-re limit t)))
  376. (list-ender (org-list-ending-between
  377. last-item-start actual-pos)))
  378. ;; We are in a list when we are on an item line or when we can
  379. ;; find an item before point and there is no valid list ender
  380. ;; between it and the point.
  381. (and last-item-start (not list-ender)))))
  382. (defun org-list-top-point-with-regexp (limit)
  383. "Return point at the top level item in a list.
  384. Argument LIMIT specifies the upper-bound of the search.
  385. List ending is determined by regexp. See
  386. `org-list-ending-method'. for more information."
  387. (save-excursion
  388. (let ((pos (point-at-eol)))
  389. ;; Is there some list above this one ? If so, go to its ending.
  390. ;; Otherwise, go back to the heading above or bob.
  391. (goto-char (or (org-list-ending-between limit pos) limit))
  392. ;; From there, search down our list.
  393. (org-search-forward-unenclosed org-item-beginning-re pos t)
  394. (point-at-bol))))
  395. (defun org-list-bottom-point-with-regexp (limit)
  396. "Return point just before list ending.
  397. Argument LIMIT specifies the lower-bound of the search.
  398. List ending is determined by regexp. See
  399. `org-list-ending-method'. for more information."
  400. (save-excursion
  401. (let ((pos (org-get-item-beginning)))
  402. ;; The list ending is either first point matching
  403. ;; `org-list-end-re', point at first white-line before next
  404. ;; heading, or eob.
  405. (or (org-list-ending-between (min pos limit) limit t) limit))))
  406. (defun org-list-top-point-with-indent (limit)
  407. "Return point at the top level in a list.
  408. Argument LIMIT specifies the upper-bound of the search.
  409. List ending is determined by indentation of text. See
  410. `org-list-ending-method'. for more information."
  411. (save-excursion
  412. (let ((case-fold-search t))
  413. (let ((item-ref (goto-char (org-get-item-beginning)))
  414. (ind-ref 10000))
  415. (forward-line -1)
  416. (catch 'exit
  417. (while t
  418. (let ((ind (org-get-indentation)))
  419. (cond
  420. ((looking-at "^[ \t]*:END:")
  421. (throw 'exit item-ref))
  422. ((<= (point) limit)
  423. (throw 'exit
  424. (if (and (org-at-item-p) (< ind ind-ref))
  425. (point-at-bol)
  426. item-ref)))
  427. ((looking-at "^[ \t]*$")
  428. (skip-chars-backward " \r\t\n")
  429. (beginning-of-line))
  430. ((looking-at "^[ \t]*#\\+end_")
  431. (re-search-backward "^[ \t]*#\\+begin_"))
  432. ((not (org-at-item-p))
  433. (setq ind-ref (min ind ind-ref))
  434. (forward-line -1))
  435. ((>= ind ind-ref)
  436. (throw 'exit item-ref))
  437. (t
  438. (setq item-ref (point-at-bol) ind-ref 10000)
  439. (forward-line -1))))))))))
  440. (defun org-list-bottom-point-with-indent (limit)
  441. "Return point just before list ending or nil if not in a list.
  442. Argument LIMIT specifies the lower-bound of the search.
  443. List ending is determined by the indentation of text. See
  444. `org-list-ending-method' for more information."
  445. (save-excursion
  446. (let ((ind-ref (progn
  447. (goto-char (org-get-item-beginning))
  448. (org-get-indentation)))
  449. (case-fold-search t))
  450. ;; do not start inside a block
  451. (org-list-maybe-skip-block #'re-search-forward limit)
  452. (beginning-of-line)
  453. (catch 'exit
  454. (skip-chars-forward " \t")
  455. (while t
  456. (let ((ind (org-get-indentation)))
  457. (cond
  458. ((or (>= (point) limit)
  459. (looking-at ":END:"))
  460. (throw 'exit (progn
  461. ;; Ensure bottom is just after a
  462. ;; non-blank line.
  463. (skip-chars-backward " \r\t\n")
  464. (min (point-max) (1+ (point-at-eol))))))
  465. ((= (point) (point-at-eol))
  466. (skip-chars-forward " \r\t\n")
  467. (beginning-of-line))
  468. ((org-at-item-p)
  469. (setq ind-ref (min ind ind-ref))
  470. (forward-line 1))
  471. ((<= ind ind-ref)
  472. (throw 'exit (point-at-bol)))
  473. ((looking-at "#\\+begin_")
  474. (re-search-forward "[ \t]*#\\+end_")
  475. (forward-line 1))
  476. (t (forward-line 1)))))))))
  477. (defun org-list-at-regexp-after-bullet-p (regexp)
  478. "Is point at a list item with REGEXP after bullet?"
  479. (and (org-at-item-p)
  480. (save-excursion
  481. (goto-char (match-end 0))
  482. ;; Ignore counter if any
  483. (when (looking-at "\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?")
  484. (goto-char (match-end 0)))
  485. (looking-at regexp))))
  486. (defun org-list-get-item-same-level (search-fun pos limit pre-move)
  487. "Return point at the beginning of next item at the same level.
  488. Search items using function SEARCH-FUN, from POS to LIMIT. It
  489. uses PRE-MOVE before search. Return nil if no item was found."
  490. (save-excursion
  491. (goto-char pos)
  492. (let* ((start (org-get-item-beginning))
  493. (ind (progn (goto-char start) (org-get-indentation))))
  494. ;; We don't want to match the current line.
  495. (funcall pre-move)
  496. ;; Skip any sublist on the way
  497. (while (and (funcall search-fun org-item-beginning-re limit t)
  498. (> (org-get-indentation) ind)))
  499. (when (and (/= (point-at-bol) start) ; Have we moved ?
  500. (= (org-get-indentation) ind))
  501. (point-at-bol)))))
  502. (defun org-list-separating-blank-lines-number (pos top bottom)
  503. "Return number of blank lines that should separate items in list.
  504. POS is the position of point to be considered.
  505. TOP and BOTTOM are respectively position of list beginning and
  506. list ending.
  507. Assume point is at item's beginning. If the item is alone, apply
  508. some heuristics to guess the result."
  509. (save-excursion
  510. (let ((insert-blank-p
  511. (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  512. usr-blank)
  513. (cond
  514. ;; Trivial cases where there should be none.
  515. ((or (and (not (eq org-list-ending-method 'indent))
  516. org-empty-line-terminates-plain-lists)
  517. (not insert-blank-p)) 0)
  518. ;; When `org-blank-before-new-entry' says so, it is 1.
  519. ((eq insert-blank-p t) 1)
  520. ;; plain-list-item is 'auto. Count blank lines separating
  521. ;; neighbours items in list.
  522. (t (let ((next-p (org-get-next-item (point) bottom)))
  523. (cond
  524. ;; Is there a next item?
  525. (next-p (goto-char next-p)
  526. (org-back-over-empty-lines))
  527. ;; Is there a previous item?
  528. ((org-get-previous-item (point) top)
  529. (org-back-over-empty-lines))
  530. ;; User inserted blank lines, trust him
  531. ((and (> pos (org-end-of-item-before-blank bottom))
  532. (> (save-excursion
  533. (goto-char pos)
  534. (skip-chars-backward " \t")
  535. (setq usr-blank (org-back-over-empty-lines))) 0))
  536. usr-blank)
  537. ;; Are there blank lines inside the item ?
  538. ((save-excursion
  539. (org-search-forward-unenclosed
  540. "^[ \t]*$" (org-end-of-item-before-blank bottom) t)) 1)
  541. ;; No parent: no blank line.
  542. (t 0))))))))
  543. (defun org-list-insert-item-generic (pos &optional checkbox after-bullet)
  544. "Insert a new list item at POS.
  545. If POS is before first character after bullet of the item, the
  546. new item will be created before the current one.
  547. Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
  548. after the bullet. Cursor will be after this text once the
  549. function ends."
  550. (goto-char pos)
  551. ;; Is point in a special block?
  552. (when (org-in-regexps-block-p
  553. "^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
  554. '(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2)))
  555. (if (not (cdr (assq 'insert org-list-automatic-rules)))
  556. ;; Rule in `org-list-automatic-rules' forbids insertion.
  557. (error "Cannot insert item inside a block")
  558. ;; Else, move before it prior to add a new item.
  559. (end-of-line)
  560. (re-search-backward "^[ \t]*#\\+\\(begin\\|BEGIN\\)_" nil t)
  561. (end-of-line 0)))
  562. (let* ((true-pos (point))
  563. (top (org-list-top-point))
  564. (bottom (copy-marker (org-list-bottom-point)))
  565. (bullet (and (goto-char (org-get-item-beginning))
  566. (org-list-bullet-string (org-get-bullet))))
  567. (ind (org-get-indentation))
  568. (before-p (progn
  569. ;; Description item: text starts after colons.
  570. (or (org-at-item-description-p)
  571. ;; At a checkbox: text starts after it.
  572. (org-at-item-checkbox-p)
  573. ;; Otherwise, text starts after bullet.
  574. (org-at-item-p))
  575. (<= true-pos (match-end 0))))
  576. (blank-lines-nb (org-list-separating-blank-lines-number
  577. true-pos top bottom))
  578. (insert-fun
  579. (lambda (text)
  580. ;; insert bullet above item in order to avoid bothering
  581. ;; with possible blank lines ending last item.
  582. (goto-char (org-get-item-beginning))
  583. (indent-to-column ind)
  584. (insert (concat bullet (when checkbox "[ ] ") after-bullet))
  585. ;; Stay between after-bullet and before text.
  586. (save-excursion
  587. (insert (concat text (make-string (1+ blank-lines-nb) ?\n))))
  588. (unless before-p
  589. ;; store bottom: exchanging items doesn't change list
  590. ;; bottom point but will modify marker anyway
  591. (setq bottom (marker-position bottom))
  592. (let ((col (current-column)))
  593. (org-list-exchange-items
  594. (org-get-item-beginning) (org-get-next-item (point) bottom)
  595. bottom)
  596. ;; recompute next-item: last sexp modified list
  597. (goto-char (org-get-next-item (point) bottom))
  598. (org-move-to-column col)))
  599. ;; checkbox update might modify bottom point, so use a
  600. ;; marker here
  601. (setq bottom (copy-marker bottom))
  602. (when checkbox (org-update-checkbox-count-maybe))
  603. (org-list-repair nil top bottom))))
  604. (goto-char true-pos)
  605. (cond
  606. (before-p (funcall insert-fun nil) t)
  607. ;; Can't split item: insert bullet at the end of item.
  608. ((not (org-get-alist-option org-M-RET-may-split-line 'item))
  609. (funcall insert-fun nil) t)
  610. ;; else, insert a new bullet along with everything from point
  611. ;; down to last non-blank line of item.
  612. (t
  613. (delete-horizontal-space)
  614. ;; Get pos again in case previous command modified line.
  615. (let* ((pos (point))
  616. (end-before-blank (org-end-of-item-before-blank bottom))
  617. (after-text
  618. (when (< pos end-before-blank)
  619. (prog1
  620. (delete-and-extract-region pos end-before-blank)
  621. ;; delete any blank line at and before point.
  622. (beginning-of-line)
  623. (while (looking-at "^[ \t]*$")
  624. (delete-region (point-at-bol) (1+ (point-at-eol)))
  625. (beginning-of-line 0))))))
  626. (funcall insert-fun after-text) t)))))
  627. (defvar org-last-indent-begin-marker (make-marker))
  628. (defvar org-last-indent-end-marker (make-marker))
  629. (defun org-list-indent-item-generic (arg no-subtree top bottom)
  630. "Indent a local list item including its children.
  631. When number ARG is a negative, item will be outdented, otherwise
  632. it will be indented.
  633. If a region is active, all items inside will be moved.
  634. If NO-SUBTREE is non-nil, only indent the item itself, not its
  635. children.
  636. TOP and BOTTOM are respectively position at item beginning and at
  637. item ending.
  638. Return t if successful."
  639. (let* ((regionp (org-region-active-p))
  640. (rbeg (and regionp (region-beginning)))
  641. (rend (and regionp (region-end))))
  642. (cond
  643. ((and regionp
  644. (goto-char rbeg)
  645. (not (org-search-forward-unenclosed org-item-beginning-re rend t)))
  646. (error "No item in region"))
  647. ((not (org-at-item-p))
  648. (error "Not on an item"))
  649. (t
  650. ;; Are we going to move the whole list?
  651. (let* ((specialp (and (cdr (assq 'indent org-list-automatic-rules))
  652. (not no-subtree)
  653. (= top (point-at-bol)))))
  654. ;; Determine begin and end points of zone to indent. If moving
  655. ;; more than one item, ensure we keep them on subsequent moves.
  656. (unless (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  657. (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
  658. (if regionp
  659. (progn
  660. (set-marker org-last-indent-begin-marker rbeg)
  661. (set-marker org-last-indent-end-marker rend))
  662. (set-marker org-last-indent-begin-marker (point-at-bol))
  663. (set-marker org-last-indent-end-marker
  664. (save-excursion
  665. (cond
  666. (specialp bottom)
  667. (no-subtree (org-end-of-item-or-at-child bottom))
  668. (t (org-get-end-of-item bottom)))))))
  669. ;; Get everything ready
  670. (let* ((beg (marker-position org-last-indent-begin-marker))
  671. (end (marker-position org-last-indent-end-marker))
  672. (struct (org-list-struct
  673. beg end top (if specialp end bottom) (< arg 0)))
  674. (origins (org-list-struct-origins struct))
  675. (beg-item (assq beg struct)))
  676. (cond
  677. ;; Special case: moving top-item with indent rule
  678. (specialp
  679. (let* ((level-skip (org-level-increment))
  680. (offset (if (< arg 0) (- level-skip) level-skip))
  681. (top-ind (nth 1 beg-item)))
  682. (if (< (+ top-ind offset) 0)
  683. (error "Cannot outdent beyond margin")
  684. ;; Change bullet if necessary
  685. (when (and (= (+ top-ind offset) 0)
  686. (string-match "*" (nth 2 beg-item)))
  687. (setcdr beg-item (list (nth 1 beg-item)
  688. (org-list-bullet-string "-"))))
  689. ;; Shift ancestor
  690. (let ((anc (car struct)))
  691. (setcdr anc (list (+ (nth 1 anc) offset) "" nil)))
  692. (org-list-struct-fix-struct struct origins)
  693. (org-list-struct-apply-struct struct end))))
  694. ;; Forbidden move
  695. ((and (< arg 0)
  696. (or (and no-subtree
  697. (not regionp)
  698. (org-list-struct-get-child beg-item struct))
  699. (let ((last-item (save-excursion
  700. (goto-char end)
  701. (skip-chars-backward " \r\t\n")
  702. (goto-char (org-get-item-beginning))
  703. (org-list-struct-assoc-at-point))))
  704. (org-list-struct-get-child last-item struct))))
  705. (error "Cannot outdent an item without its children"))
  706. ;; Normal shifting
  707. (t
  708. (let* ((shifted-ori (if (< arg 0)
  709. (org-list-struct-outdent beg end origins)
  710. (org-list-struct-indent beg end origins struct))))
  711. (org-list-struct-fix-struct struct shifted-ori)
  712. (org-list-struct-apply-struct struct bottom))))))))))
  713. ;;; Predicates
  714. (defun org-in-item-p ()
  715. "Is the cursor inside a plain list?
  716. This checks `org-list-ending-method'."
  717. (unless (let ((outline-regexp org-outline-regexp)) (org-at-heading-p))
  718. (let* ((prev-head (save-excursion (outline-previous-heading)))
  719. (bound (if prev-head
  720. (or (save-excursion
  721. (let ((case-fold-search t))
  722. (re-search-backward "^[ \t]*:END:" prev-head t)))
  723. prev-head)
  724. (point-min))))
  725. (cond
  726. ((eq org-list-ending-method 'regexp)
  727. (org-list-in-item-p-with-regexp bound))
  728. ((eq org-list-ending-method 'indent)
  729. (org-list-in-item-p-with-indent bound))
  730. (t (and (org-list-in-item-p-with-regexp bound)
  731. (org-list-in-item-p-with-indent bound)))))))
  732. (defun org-list-first-item-p (top)
  733. "Is this item the first item in a plain list?
  734. Assume point is at an item.
  735. TOP is the position of list's top-item."
  736. (save-excursion
  737. (beginning-of-line)
  738. (let ((ind (org-get-indentation)))
  739. (or (not (org-search-backward-unenclosed org-item-beginning-re top t))
  740. (< (org-get-indentation) ind)))))
  741. (defun org-at-item-p ()
  742. "Is point in a line starting a hand-formatted item?"
  743. (save-excursion
  744. (beginning-of-line) (looking-at org-item-beginning-re)))
  745. (defun org-at-item-bullet-p ()
  746. "Is point at the bullet of a plain list item?"
  747. (and (org-at-item-p)
  748. (not (member (char-after) '(?\ ?\t)))
  749. (< (point) (match-end 0))))
  750. (defun org-at-item-timer-p ()
  751. "Is point at a line starting a plain list item with a timer?"
  752. (org-list-at-regexp-after-bullet-p
  753. "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
  754. (defun org-at-item-description-p ()
  755. "Is point at a description list item?"
  756. (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
  757. (defun org-at-item-checkbox-p ()
  758. "Is point at a line starting a plain-list item with a checklet?"
  759. (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
  760. (defun org-checkbox-blocked-p ()
  761. "Is the current checkbox blocked from for being checked now?
  762. A checkbox is blocked if all of the following conditions are fulfilled:
  763. 1. The checkbox is not checked already.
  764. 2. The current entry has the ORDERED property set.
  765. 3. There is an unchecked checkbox in this entry before the current line."
  766. (catch 'exit
  767. (save-match-data
  768. (save-excursion
  769. (unless (org-at-item-checkbox-p) (throw 'exit nil))
  770. (when (equal (match-string 1) "[X]")
  771. ;; the box is already checked!
  772. (throw 'exit nil))
  773. (let ((end (point-at-bol)))
  774. (condition-case nil (org-back-to-heading t)
  775. (error (throw 'exit nil)))
  776. (unless (org-entry-get nil "ORDERED") (throw 'exit nil))
  777. (when (org-search-forward-unenclosed
  778. "^[ \t]*[-+*0-9.)]+[ \t]+\\(\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\[[- ]\\]" end t)
  779. (org-current-line)))))))
  780. ;;; Navigate
  781. ;; Every interactive navigation function is derived from a
  782. ;; non-interactive one, which doesn't move point, assumes point is
  783. ;; already in a list and doesn't compute list boundaries.
  784. ;; If you plan to use more than one org-list function is some code,
  785. ;; you should therefore first compute list boundaries, and then make
  786. ;; use of non-interactive forms.
  787. (defun org-list-top-point ()
  788. "Return point at the top level in a list.
  789. Assume point is in a list."
  790. (let* ((prev-head (save-excursion (outline-previous-heading)))
  791. (bound (if prev-head
  792. (or (save-excursion
  793. (let ((case-fold-search t))
  794. (re-search-backward "^[ \t]*:END:" prev-head t)))
  795. prev-head)
  796. (point-min))))
  797. (cond
  798. ((eq org-list-ending-method 'regexp)
  799. (org-list-top-point-with-regexp bound))
  800. ((eq org-list-ending-method 'indent)
  801. (org-list-top-point-with-indent bound))
  802. (t (let ((top-re (org-list-top-point-with-regexp bound)))
  803. (org-list-top-point-with-indent (or top-re bound)))))))
  804. (defun org-list-bottom-point ()
  805. "Return point just before list ending.
  806. Assume point is in a list."
  807. (let* ((next-head (save-excursion
  808. (and (let ((outline-regexp org-outline-regexp))
  809. ;; Use default regexp because folding
  810. ;; changes OUTLINE-REGEXP.
  811. (outline-next-heading)))))
  812. (limit (or (save-excursion
  813. (and (re-search-forward "^[ \t]*:END:" next-head t)
  814. (point-at-bol)))
  815. next-head
  816. (point-max))))
  817. (cond
  818. ((eq org-list-ending-method 'regexp)
  819. (org-list-bottom-point-with-regexp limit))
  820. ((eq org-list-ending-method 'indent)
  821. (org-list-bottom-point-with-indent limit))
  822. (t (let ((bottom-re (org-list-bottom-point-with-regexp limit)))
  823. (org-list-bottom-point-with-indent (or bottom-re limit)))))))
  824. (defun org-get-item-beginning ()
  825. "Return position of current item beginning."
  826. (save-excursion
  827. ;; possibly match current line
  828. (end-of-line)
  829. (org-search-backward-unenclosed org-item-beginning-re nil t)
  830. (point-at-bol)))
  831. (defun org-beginning-of-item ()
  832. "Go to the beginning of the current hand-formatted item.
  833. If the cursor is not in an item, throw an error."
  834. (interactive)
  835. (if (org-in-item-p)
  836. (goto-char (org-get-item-beginning))
  837. (error "Not in an item")))
  838. (defun org-get-beginning-of-list (top)
  839. "Return position of the first item of the current list or sublist.
  840. TOP is the position at list beginning."
  841. (save-excursion
  842. (let (prev-p)
  843. (while (setq prev-p (org-get-previous-item (point) top))
  844. (goto-char prev-p))
  845. (point-at-bol))))
  846. (defun org-beginning-of-item-list ()
  847. "Go to the beginning item of the current list or sublist.
  848. Return an error if not in a list."
  849. (interactive)
  850. (if (org-in-item-p)
  851. (goto-char (org-get-beginning-of-list (org-list-top-point)))
  852. (error "Not in an item")))
  853. (defun org-get-end-of-list (bottom)
  854. "Return position at the end of the current list or sublist.
  855. BOTTOM is the position at list ending."
  856. (save-excursion
  857. (goto-char (org-get-item-beginning))
  858. (let ((ind (org-get-indentation)))
  859. (while (and (/= (point) bottom)
  860. (>= (org-get-indentation) ind))
  861. (org-search-forward-unenclosed org-item-beginning-re bottom 'move))
  862. (if (= (point) bottom) bottom (point-at-bol)))))
  863. (defun org-end-of-item-list ()
  864. "Go to the end of the current list or sublist.
  865. If the cursor in not in an item, throw an error."
  866. (interactive)
  867. (if (org-in-item-p)
  868. (goto-char (org-get-end-of-list (org-list-bottom-point)))
  869. (error "Not in an item")))
  870. (defun org-get-end-of-item (bottom)
  871. "Return position at the end of the current item.
  872. BOTTOM is the position at list ending."
  873. (let* ((next-p (org-get-next-item (point) bottom)))
  874. (or next-p (org-get-end-of-list bottom))))
  875. (defun org-end-of-item ()
  876. "Go to the end of the current hand-formatted item.
  877. If the cursor is not in an item, throw an error."
  878. (interactive)
  879. (if (org-in-item-p)
  880. (goto-char (org-get-end-of-item (org-list-bottom-point)))
  881. (error "Not in an item")))
  882. (defun org-end-of-item-or-at-child (bottom)
  883. "Move to the end of the item, stops before the first child if any.
  884. BOTTOM is the position at list ending."
  885. (end-of-line)
  886. (goto-char
  887. (if (org-search-forward-unenclosed org-item-beginning-re bottom t)
  888. (point-at-bol)
  889. (org-get-end-of-item bottom))))
  890. (defun org-end-of-item-before-blank (bottom)
  891. "Return point at end of item, before any blank line.
  892. Point returned is at eol.
  893. BOTTOM is the position at list ending."
  894. (save-excursion
  895. (goto-char (org-get-end-of-item bottom))
  896. (skip-chars-backward " \r\t\n")
  897. (point-at-eol)))
  898. (defun org-get-previous-item (pos limit)
  899. "Return point of the previous item at the same level as POS.
  900. Stop searching at LIMIT. Return nil if no item is found."
  901. (org-list-get-item-same-level
  902. #'org-search-backward-unenclosed pos limit #'beginning-of-line))
  903. (defun org-previous-item ()
  904. "Move to the beginning of the previous item.
  905. Item is at the same level in the current plain list. Error if not
  906. in a plain list, or if this is the first item in the list."
  907. (interactive)
  908. (if (not (org-in-item-p))
  909. (error "Not in an item")
  910. (let ((prev-p (org-get-previous-item (point) (org-list-top-point))))
  911. (if prev-p (goto-char prev-p) (error "On first item")))))
  912. (defun org-get-next-item (pos limit)
  913. "Return point of the next item at the same level as POS.
  914. Stop searching at LIMIT. Return nil if no item is found."
  915. (org-list-get-item-same-level
  916. #'org-search-forward-unenclosed pos limit #'end-of-line))
  917. (defun org-next-item ()
  918. "Move to the beginning of the next item.
  919. Item is at the same level in the current plain list. Error if not
  920. in a plain list, or if this is the last item in the list."
  921. (interactive)
  922. (if (not (org-in-item-p))
  923. (error "Not in an item")
  924. (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  925. (if next-p (goto-char next-p) (error "On last item")))))
  926. ;;; Manipulate
  927. (defun org-list-exchange-items (beg-A beg-B bottom)
  928. "Swap item starting at BEG-A with item starting at BEG-B.
  929. Blank lines at the end of items are left in place. Assume BEG-A
  930. is lesser than BEG-B.
  931. BOTTOM is the position at list ending."
  932. (save-excursion
  933. (let* ((end-of-item-no-blank
  934. (lambda (pos)
  935. (goto-char pos)
  936. (goto-char (org-end-of-item-before-blank bottom))))
  937. (end-A-no-blank (funcall end-of-item-no-blank beg-A))
  938. (end-B-no-blank (funcall end-of-item-no-blank beg-B))
  939. (body-A (buffer-substring beg-A end-A-no-blank))
  940. (body-B (buffer-substring beg-B end-B-no-blank))
  941. (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B)))
  942. (goto-char beg-A)
  943. (delete-region beg-A end-B-no-blank)
  944. (insert (concat body-B between-A-no-blank-and-B body-A)))))
  945. (defun org-move-item-down ()
  946. "Move the plain list item at point down, i.e. swap with following item.
  947. Subitems (items with larger indentation) are considered part of the item,
  948. so this really moves item trees."
  949. (interactive)
  950. (if (not (org-at-item-p))
  951. (error "Not at an item")
  952. (let* ((pos (point))
  953. (col (current-column))
  954. (bottom (org-list-bottom-point))
  955. (actual-item (goto-char (org-get-item-beginning)))
  956. (next-item (org-get-next-item (point) bottom)))
  957. (if (not next-item)
  958. (progn
  959. (goto-char pos)
  960. (error "Cannot move this item further down"))
  961. (org-list-exchange-items actual-item next-item bottom)
  962. (org-list-repair nil nil bottom)
  963. (goto-char (org-get-next-item (point) bottom))
  964. (move-to-column col)))))
  965. (defun org-move-item-up ()
  966. "Move the plain list item at point up, i.e. swap with previous item.
  967. Subitems (items with larger indentation) are considered part of the item,
  968. so this really moves item trees."
  969. (interactive)
  970. (if (not (org-at-item-p))
  971. (error "Not at an item")
  972. (let* ((pos (point))
  973. (col (current-column))
  974. (top (org-list-top-point))
  975. (bottom (org-list-bottom-point))
  976. (actual-item (goto-char (org-get-item-beginning)))
  977. (prev-item (org-get-previous-item (point) top)))
  978. (if (not prev-item)
  979. (progn
  980. (goto-char pos)
  981. (error "Cannot move this item further up"))
  982. (org-list-exchange-items prev-item actual-item bottom)
  983. (org-list-repair nil top bottom)
  984. (move-to-column col)))))
  985. (defun org-insert-item (&optional checkbox)
  986. "Insert a new item at the current level.
  987. If cursor is before first character after bullet of the item, the
  988. new item will be created before the current one.
  989. If CHECKBOX is non-nil, add a checkbox next to the bullet.
  990. Return t when things worked, nil when we are not in an item, or
  991. item is invisible."
  992. (unless (or (not (org-in-item-p))
  993. (save-excursion
  994. (goto-char (org-get-item-beginning))
  995. (org-invisible-p)))
  996. (if (save-excursion
  997. (goto-char (org-get-item-beginning))
  998. (org-at-item-timer-p))
  999. ;; Timer list: delegate to `org-timer-item'.
  1000. (progn (org-timer-item) t)
  1001. ;; if we're in a description list, ask for the new term.
  1002. (let ((desc-text (when (save-excursion
  1003. (and (goto-char (org-get-item-beginning))
  1004. (org-at-item-description-p)))
  1005. (concat (read-string "Term: ") " :: "))))
  1006. ;; Don't insert a checkbox if checkbox rule is applied and it
  1007. ;; is a description item.
  1008. (org-list-insert-item-generic
  1009. (point) (and checkbox
  1010. (or (not desc-text)
  1011. (not (cdr (assq 'checkbox org-list-automatic-rules)))))
  1012. desc-text)))))
  1013. ;;; Structures
  1014. ;; The idea behind structures is to avoid moving back and forth in the
  1015. ;; buffer on costly operations like indenting or fixing bullets.
  1016. ;; It achieves this by taking a snapshot of an interesting part of the
  1017. ;; list, in the shape of an alist, using `org-list-struct'.
  1018. ;; It then proceeds to changes directly on the alist, with the help of
  1019. ;; and `org-list-struct-origins'. When those are done,
  1020. ;; `org-list-struct-apply-struct' applies the changes to the buffer.
  1021. (defun org-list-struct-assoc-at-point ()
  1022. "Return the structure association at point.
  1023. It is a cons-cell whose key is point and values are indentation,
  1024. bullet string and bullet counter, if any."
  1025. (save-excursion
  1026. (beginning-of-line)
  1027. (list (point-at-bol)
  1028. (org-get-indentation)
  1029. (progn
  1030. (looking-at "^[ \t]*\\([-+*0-9.)]+[ \t]+\\)")
  1031. (match-string 1))
  1032. (progn
  1033. (goto-char (match-end 0))
  1034. (and (looking-at "\\[@\\(?:start:\\)?\\([0-9]+\\)\\]")
  1035. (match-string 1))))))
  1036. (defun org-list-struct (begin end top bottom &optional outdent)
  1037. "Return the structure containing the list between BEGIN and END.
  1038. A structure is an alist where key is point of item and values
  1039. are, in that order, indentation, bullet string and value of
  1040. counter, if any. A structure contains every list and sublist that
  1041. has items between BEGIN and END along with their common ancestor.
  1042. If no such ancestor can be found, the function will add a virtual
  1043. ancestor at position 0.
  1044. TOP and BOTTOM are respectively the position of list beginning
  1045. and list ending.
  1046. If OUTDENT is non-nil, it will also grab all of the parent list
  1047. and the grand-parent. Setting OUTDENT to t is mandatory when next
  1048. change is an outdent."
  1049. (save-excursion
  1050. (let* (struct
  1051. (extend
  1052. (lambda (struct)
  1053. (let* ((ind-min (apply 'min (mapcar 'cadr struct)))
  1054. (begin (caar struct))
  1055. (end (caar (last struct)))
  1056. pre-list post-list)
  1057. (goto-char begin)
  1058. ;; Find beginning of most outdented list (min list)
  1059. (while (and (org-search-backward-unenclosed
  1060. org-item-beginning-re top t)
  1061. (>= (org-get-indentation) ind-min))
  1062. (setq pre-list (cons (org-list-struct-assoc-at-point)
  1063. pre-list)))
  1064. ;; Now get the parent. If none, add a virtual ancestor
  1065. (if (< (org-get-indentation) ind-min)
  1066. (setq pre-list (cons (org-list-struct-assoc-at-point)
  1067. pre-list))
  1068. (setq pre-list (cons (list 0 (org-get-indentation) "" nil)
  1069. pre-list)))
  1070. ;; Find end of min list
  1071. (goto-char end)
  1072. (end-of-line)
  1073. (while (and (org-search-forward-unenclosed
  1074. org-item-beginning-re bottom 'move)
  1075. (>= (org-get-indentation) ind-min))
  1076. (setq post-list (cons (org-list-struct-assoc-at-point)
  1077. post-list)))
  1078. ;; Is list is malformed? If some items are less
  1079. ;; indented that top-item, add them anyhow.
  1080. (when (and (= (caar pre-list) 0) (< (point) bottom))
  1081. (beginning-of-line)
  1082. (while (org-search-forward-unenclosed
  1083. org-item-beginning-re bottom t)
  1084. (setq post-list (cons (org-list-struct-assoc-at-point)
  1085. post-list))))
  1086. (append pre-list struct (reverse post-list))))))
  1087. ;; Here we start: first get the core zone...
  1088. (goto-char end)
  1089. (while (org-search-backward-unenclosed org-item-beginning-re begin t)
  1090. (setq struct (cons (org-list-struct-assoc-at-point) struct)))
  1091. ;; ... then, extend it to make it a structure...
  1092. (let ((extended (funcall extend struct)))
  1093. ;; ... twice when OUTDENT is non-nil and struct still can be
  1094. ;; extended
  1095. (if (and outdent (> (caar extended) 0))
  1096. (funcall extend extended)
  1097. extended)))))
  1098. (defun org-list-struct-origins (struct)
  1099. "Return an alist where key is item's position and value parent's.
  1100. STRUCT is the list's structure looked up."
  1101. (let* ((struct-rev (reverse struct))
  1102. (acc (list (cons (nth 1 (car struct)) 0)))
  1103. (prev-item (lambda (item)
  1104. (car (nth 1 (member (assq item struct) struct-rev)))))
  1105. (get-origins
  1106. (lambda (item)
  1107. (let* ((item-pos (car item))
  1108. (ind (nth 1 item))
  1109. (prev-ind (caar acc)))
  1110. (cond
  1111. ;; List closing.
  1112. ((> prev-ind ind)
  1113. (let ((current-origin (or (member (assq ind acc) acc)
  1114. ;; needed if top-point is
  1115. ;; not the most outdented
  1116. (last acc))))
  1117. (setq acc current-origin)
  1118. (cons item-pos (cdar acc))))
  1119. ;; New list
  1120. ((< prev-ind ind)
  1121. (let ((origin (funcall prev-item item-pos)))
  1122. (setq acc (cons (cons ind origin) acc))
  1123. (cons item-pos origin)))
  1124. ;; Current list going on
  1125. (t (cons item-pos (cdar acc))))))))
  1126. (cons '(0 . 0) (mapcar get-origins (cdr struct)))))
  1127. (defun org-list-struct-get-parent (item struct origins)
  1128. "Return parent association of ITEM in STRUCT or nil.
  1129. ORIGINS is the alist of parents. See `org-list-struct-origins'."
  1130. (let* ((parent-pos (cdr (assq (car item) origins))))
  1131. (when (> parent-pos 0) (assq parent-pos struct))))
  1132. (defun org-list-struct-get-child (item struct)
  1133. "Return child association of ITEM in STRUCT or nil."
  1134. (let ((ind (nth 1 item))
  1135. (next-item (cadr (member item struct))))
  1136. (when (and next-item (> (nth 1 next-item) ind)) next-item)))
  1137. (defun org-list-struct-fix-bul (struct origins)
  1138. "Verify and correct bullets for every association in STRUCT.
  1139. ORIGINS is the alist of parents. See `org-list-struct-origins'.
  1140. This function modifies STRUCT."
  1141. (let* (acc
  1142. (init-bul (lambda (item)
  1143. (let ((counter (nth 3 item))
  1144. (bullet (org-list-bullet-string (nth 2 item))))
  1145. (cond
  1146. ((and (string-match "[0-9]+" bullet) counter)
  1147. (replace-match counter nil nil bullet))
  1148. ((string-match "[0-9]+" bullet)
  1149. (replace-match "1" nil nil bullet))
  1150. (t bullet)))))
  1151. (set-bul (lambda (item bullet)
  1152. (setcdr item (list (nth 1 item) bullet (nth 3 item)))))
  1153. (get-bul (lambda (item bullet)
  1154. (let* ((counter (nth 3 item)))
  1155. (if (and counter (string-match "[0-9]+" bullet))
  1156. (replace-match counter nil nil bullet)
  1157. bullet))))
  1158. (fix-bul
  1159. (lambda (item) struct
  1160. (let* ((parent (cdr (assq (car item) origins)))
  1161. (orig-ref (assq parent acc)))
  1162. (if orig-ref
  1163. ;; Continuing previous list
  1164. (let* ((prev-bul (cdr orig-ref))
  1165. (new-bul (funcall get-bul item prev-bul)))
  1166. (setcdr orig-ref (org-list-inc-bullet-maybe new-bul))
  1167. (funcall set-bul item new-bul))
  1168. ;; A new list is starting
  1169. (let ((new-bul (funcall init-bul item)))
  1170. (funcall set-bul item new-bul)
  1171. (setq acc (cons (cons parent
  1172. (org-list-inc-bullet-maybe new-bul))
  1173. acc))))))))
  1174. (mapc fix-bul (cdr struct))))
  1175. (defun org-list-struct-fix-ind (struct origins)
  1176. "Verify and correct indentation for every association in STRUCT.
  1177. ORIGINS is the alist of parents. See `org-list-struct-origins'.
  1178. This function modifies STRUCT."
  1179. (let* ((headless (cdr struct))
  1180. (ancestor (car struct))
  1181. (top-ind (+ (nth 1 ancestor) (length (nth 2 ancestor))))
  1182. (new-ind
  1183. (lambda (item)
  1184. (let* ((parent (org-list-struct-get-parent item headless origins)))
  1185. (if parent
  1186. ;; Indent like parent + length of parent's bullet
  1187. (setcdr item (cons (+ (length (nth 2 parent)) (nth 1 parent))
  1188. (cddr item)))
  1189. ;; If no parent, indent like top-point
  1190. (setcdr item (cons top-ind (cddr item))))))))
  1191. (mapc new-ind headless)))
  1192. (defun org-list-struct-fix-struct (struct origins)
  1193. "Return STRUCT with correct bullets and indentation.
  1194. ORIGINS is the alist of parents. See `org-list-struct-origins'.
  1195. Only elements of STRUCT that have changed are returned."
  1196. (let ((old (copy-alist struct)))
  1197. (org-list-struct-fix-bul struct origins)
  1198. (org-list-struct-fix-ind struct origins)
  1199. (delq nil (mapcar (lambda (e) (when (not (equal (pop old) e)) e)) struct))))
  1200. (defun org-list-struct-outdent (start end origins)
  1201. "Outdent items in a structure.
  1202. Items are indented when their key is between START, included, and
  1203. END, excluded.
  1204. ORIGINS is the alist of parents. See `org-list-struct-origins'.
  1205. STRUCT is the concerned structure."
  1206. (let* (acc
  1207. (out (lambda (cell)
  1208. (let* ((item (car cell))
  1209. (parent (cdr cell)))
  1210. (cond
  1211. ;; Item not yet in zone: keep association
  1212. ((< item start) cell)
  1213. ;; Item out of zone: follow associations in acc
  1214. ((>= item end)
  1215. (let ((convert (assq parent acc)))
  1216. (if convert (cons item (cdr convert)) cell)))
  1217. ;; Item has no parent: error
  1218. ((<= parent 0)
  1219. (error "Cannot outdent top-level items"))
  1220. ;; Parent is outdented: keep association
  1221. ((>= parent start)
  1222. (setq acc (cons (cons parent item) acc)) cell)
  1223. (t
  1224. ;; Parent isn't outdented: reparent to grand-parent
  1225. (let ((grand-parent (cdr (assq parent origins))))
  1226. (setq acc (cons (cons parent item) acc))
  1227. (cons item grand-parent))))))))
  1228. (mapcar out origins)))
  1229. (defun org-list-struct-indent (start end origins struct)
  1230. "Indent items in a structure.
  1231. Items are indented when their key is between START, included, and
  1232. END, excluded.
  1233. ORIGINS is the alist of parents. See `org-list-struct-origins'.
  1234. STRUCT is the concerned structure. It may be modified if
  1235. `org-list-demote-modify-bullet' matches bullets between START and
  1236. END."
  1237. (let* (acc
  1238. (orig-rev (reverse origins))
  1239. (get-prev-item
  1240. (lambda (cell parent)
  1241. (car (rassq parent (cdr (memq cell orig-rev))))))
  1242. (set-assoc
  1243. (lambda (cell)
  1244. (setq acc (cons cell acc)) cell))
  1245. (change-bullet-maybe
  1246. (lambda (item)
  1247. (let* ((full-item (assq item struct))
  1248. (item-bul (org-trim (nth 2 full-item)))
  1249. (new-bul-p (cdr (assoc item-bul org-list-demote-modify-bullet))))
  1250. (when new-bul-p
  1251. ;; new bullet is stored without space to ensure item
  1252. ;; will be modified
  1253. (setcdr full-item
  1254. (list (nth 1 full-item)
  1255. new-bul-p
  1256. (nth 3 full-item)))))))
  1257. (ind
  1258. (lambda (cell)
  1259. (let* ((item (car cell))
  1260. (parent (cdr cell)))
  1261. (cond
  1262. ;; Item not yet in zone: keep association
  1263. ((< item start) cell)
  1264. ((>= item end)
  1265. ;; Item out of zone: follow associations in acc
  1266. (let ((convert (assq parent acc)))
  1267. (if convert (cons item (cdr convert)) cell)))
  1268. (t
  1269. ;; Item is in zone...
  1270. (let ((prev (funcall get-prev-item cell parent)))
  1271. ;; Check if bullet needs to be changed
  1272. (funcall change-bullet-maybe item)
  1273. (cond
  1274. ;; First item indented but not parent: error
  1275. ((and (or (not prev) (= prev 0)) (< parent start))
  1276. (error "Cannot indent the first item of a list"))
  1277. ;; First item and parent indented: keep same parent
  1278. ((or (not prev) (= prev 0))
  1279. (funcall set-assoc cell))
  1280. ;; Previous item not indented: reparent to it
  1281. ((< prev start)
  1282. (funcall set-assoc (cons item prev)))
  1283. ;; Previous item indented: reparent like it
  1284. (t
  1285. (funcall set-assoc (cons item
  1286. (cdr (assq prev acc)))))))))))))
  1287. (mapcar ind origins)))
  1288. (defun org-list-struct-apply-struct (struct bottom)
  1289. "Apply modifications to list so it mirrors STRUCT.
  1290. BOTTOM is position at list ending.
  1291. Initial position is restored after the changes."
  1292. (let* ((pos (copy-marker (point)))
  1293. (ancestor (caar struct))
  1294. (modify
  1295. (lambda (item)
  1296. (goto-char (car item))
  1297. (let* ((new-ind (nth 1 item))
  1298. (new-bul (org-list-bullet-string (nth 2 item)))
  1299. (old-ind (org-get-indentation))
  1300. (old-bul (progn
  1301. (looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
  1302. (match-string 1)))
  1303. (old-body-ind (+ (length old-bul) old-ind))
  1304. (new-body-ind (+ (length new-bul) new-ind)))
  1305. ;; 1. Shift item's body
  1306. (unless (= old-body-ind new-body-ind)
  1307. (org-shift-item-indentation
  1308. (- new-body-ind old-body-ind) bottom))
  1309. ;; 2. Replace bullet
  1310. (unless (equal new-bul old-bul)
  1311. (save-excursion
  1312. (looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
  1313. (replace-match new-bul nil nil nil 1)))
  1314. ;; 3. Indent item to appropriate column
  1315. (unless (= new-ind old-ind)
  1316. (delete-region (point-at-bol)
  1317. (progn
  1318. (skip-chars-forward " \t")
  1319. (point)))
  1320. (indent-to new-ind)))))
  1321. ;; Remove ancestor if it is left.
  1322. (struct-to-apply (if (or (not ancestor) (= 0 ancestor))
  1323. (cdr struct)
  1324. struct)))
  1325. ;; Apply changes from bottom to top
  1326. (mapc modify (nreverse struct-to-apply))
  1327. (goto-char pos)))
  1328. ;;; Indentation
  1329. (defun org-get-string-indentation (s)
  1330. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  1331. (let ((n -1) (i 0) (w tab-width) c)
  1332. (catch 'exit
  1333. (while (< (setq n (1+ n)) (length s))
  1334. (setq c (aref s n))
  1335. (cond ((= c ?\ ) (setq i (1+ i)))
  1336. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  1337. (t (throw 'exit t)))))
  1338. i))
  1339. (defun org-shift-item-indentation (delta bottom)
  1340. "Shift the indentation in current item by DELTA.
  1341. Sub-items are not moved.
  1342. BOTTOM is position at list ending."
  1343. (save-excursion
  1344. (let ((beg (point-at-bol))
  1345. (end (org-end-of-item-or-at-child bottom)))
  1346. (beginning-of-line (unless (eolp) 0))
  1347. (while (> (point) beg)
  1348. (when (looking-at "[ \t]*\\S-")
  1349. ;; this is not an empty line
  1350. (let ((i (org-get-indentation)))
  1351. (when (and (> i 0) (> (+ i delta) 0))
  1352. (indent-line-to (+ i delta)))))
  1353. (beginning-of-line 0)))))
  1354. (defun org-outdent-item ()
  1355. "Outdent a local list item, but not its children.
  1356. If a region is active, all items inside will be moved."
  1357. (interactive)
  1358. (org-list-indent-item-generic
  1359. -1 t (org-list-top-point) (org-list-bottom-point)))
  1360. (defun org-indent-item ()
  1361. "Indent a local list item, but not its children.
  1362. If a region is active, all items inside will be moved."
  1363. (interactive)
  1364. (org-list-indent-item-generic
  1365. 1 t (org-list-top-point) (org-list-bottom-point)))
  1366. (defun org-outdent-item-tree ()
  1367. "Outdent a local list item including its children.
  1368. If a region is active, all items inside will be moved."
  1369. (interactive)
  1370. (org-list-indent-item-generic
  1371. -1 nil (org-list-top-point) (org-list-bottom-point)))
  1372. (defun org-indent-item-tree ()
  1373. "Indent a local list item including its children.
  1374. If a region is active, all items inside will be moved."
  1375. (interactive)
  1376. (org-list-indent-item-generic
  1377. 1 nil (org-list-top-point) (org-list-bottom-point)))
  1378. (defvar org-tab-ind-state)
  1379. (defun org-cycle-item-indentation ()
  1380. "Cycle levels of indentation of an empty item.
  1381. The first run indent the item, if applicable. Subsequents runs
  1382. outdent it at meaningful levels in the list. When done, item is
  1383. put back at its original position with its original bullet.
  1384. Return t at each successful move."
  1385. (let ((org-adapt-indentation nil)
  1386. (ind (org-get-indentation))
  1387. (bottom (and (org-at-item-p) (org-list-bottom-point))))
  1388. (when (and (or (org-at-item-description-p)
  1389. (org-at-item-checkbox-p)
  1390. (org-at-item-p))
  1391. ;; Check that item is really empty
  1392. (>= (match-end 0) (save-excursion
  1393. (org-end-of-item-or-at-child bottom)
  1394. (skip-chars-backward " \r\t\n")
  1395. (point))))
  1396. (setq this-command 'org-cycle-item-indentation)
  1397. (let ((top (org-list-top-point)))
  1398. ;; When in the middle of the cycle, try to outdent first. If it
  1399. ;; fails, and point is still at initial position, indent. Else,
  1400. ;; go back to original position.
  1401. (if (eq last-command 'org-cycle-item-indentation)
  1402. (cond
  1403. ((ignore-errors (org-list-indent-item-generic -1 t top bottom)))
  1404. ((and (= (org-get-indentation) (car org-tab-ind-state))
  1405. (ignore-errors
  1406. (org-list-indent-item-generic 1 t top bottom))))
  1407. (t (back-to-indentation)
  1408. (indent-to-column (car org-tab-ind-state))
  1409. (end-of-line)
  1410. (org-list-repair (cdr org-tab-ind-state))
  1411. ;; Break cycle
  1412. (setq this-command 'identity)))
  1413. ;; If a cycle is starting, remember indentation and bullet,
  1414. ;; then try to indent. If it fails, try to outdent.
  1415. (setq org-tab-ind-state (cons ind (org-get-bullet)))
  1416. (cond
  1417. ((ignore-errors (org-list-indent-item-generic 1 t top bottom)))
  1418. ((ignore-errors (org-list-indent-item-generic -1 t top bottom)))
  1419. (t (error "Cannot move item")))))
  1420. t)))
  1421. ;;; Bullets
  1422. (defun org-get-bullet ()
  1423. "Return the bullet of the item at point.
  1424. Assume cursor is at an item."
  1425. (save-excursion
  1426. (beginning-of-line)
  1427. (and (looking-at "[ \t]*\\(\\S-+\\)") (match-string 1))))
  1428. (defun org-list-bullet-string (bullet)
  1429. "Return BULLET with the correct number of whitespaces.
  1430. It determines the number of whitespaces to append by looking at
  1431. `org-list-two-spaces-after-bullet-regexp'."
  1432. (save-match-data
  1433. (string-match "\\S-+\\([ \t]*\\)" bullet)
  1434. (replace-match
  1435. (save-match-data
  1436. (concat
  1437. " "
  1438. ;; Do we need to concat another white space ?
  1439. (when (and org-list-two-spaces-after-bullet-regexp
  1440. (string-match org-list-two-spaces-after-bullet-regexp bullet))
  1441. " ")))
  1442. nil nil bullet 1)))
  1443. (defun org-list-inc-bullet-maybe (bullet)
  1444. "Increment BULLET if applicable."
  1445. (if (string-match "[0-9]+" bullet)
  1446. (replace-match
  1447. (number-to-string (1+ (string-to-number (match-string 0 bullet))))
  1448. nil nil bullet)
  1449. bullet))
  1450. (defun org-list-repair (&optional force-bullet top bottom)
  1451. "Make sure all items are correctly indented, with the right bullet.
  1452. This function scans the list at point, along with any sublist.
  1453. If FORCE-BULLET is a string, ensure all items in list share this
  1454. bullet, or a logical successor in the case of an ordered list.
  1455. When non-nil, TOP and BOTTOM specify respectively position of
  1456. list beginning and list ending.
  1457. Item's body is not indented, only shifted with the bullet."
  1458. (interactive)
  1459. (unless (org-at-item-p) (error "This is not a list"))
  1460. (let* ((bottom (or bottom (org-list-bottom-point)))
  1461. (struct (org-list-struct
  1462. (point-at-bol) (point-at-eol)
  1463. (or top (org-list-top-point)) bottom))
  1464. (origins (org-list-struct-origins struct))
  1465. fixed-struct)
  1466. (if (stringp force-bullet)
  1467. (let ((begin (nth 1 struct)))
  1468. (setcdr begin (list (nth 1 begin)
  1469. (org-list-bullet-string force-bullet)
  1470. (nth 3 begin)))
  1471. (setq fixed-struct
  1472. (cons begin (org-list-struct-fix-struct struct origins))))
  1473. (setq fixed-struct (org-list-struct-fix-struct struct origins)))
  1474. (org-list-struct-apply-struct fixed-struct bottom)))
  1475. (defun org-cycle-list-bullet (&optional which)
  1476. "Cycle through the different itemize/enumerate bullets.
  1477. This cycle the entire list level through the sequence:
  1478. `-' -> `+' -> `*' -> `1.' -> `1)'
  1479. If WHICH is a valid string, use that as the new bullet. If WHICH
  1480. is an integer, 0 means `-', 1 means `+' etc. If WHICH is
  1481. 'previous, cycle backwards."
  1482. (interactive "P")
  1483. (let* ((top (org-list-top-point))
  1484. (bullet (save-excursion
  1485. (goto-char (org-get-beginning-of-list top))
  1486. (org-get-bullet)))
  1487. (current (cond
  1488. ((string-match "\\." bullet) "1.")
  1489. ((string-match ")" bullet) "1)")
  1490. (t bullet)))
  1491. (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules)))
  1492. (bullet-list (append '("-" "+" )
  1493. ;; *-bullets are not allowed at column 0
  1494. (unless (and bullet-rule-p
  1495. (looking-at "\\S-")) '("*"))
  1496. ;; Description items cannot be numbered
  1497. (unless (and bullet-rule-p
  1498. (or (eq org-plain-list-ordered-item-terminator ?.)
  1499. (org-at-item-description-p))) '("1)"))
  1500. (unless (and bullet-rule-p
  1501. (or (eq org-plain-list-ordered-item-terminator ?\))
  1502. (org-at-item-description-p))) '("1."))))
  1503. (len (length bullet-list))
  1504. (item-index (- len (length (member current bullet-list))))
  1505. (get-value (lambda (index) (nth (mod index len) bullet-list)))
  1506. (new (cond
  1507. ((member which bullet-list) which)
  1508. ((numberp which) (funcall get-value which))
  1509. ((eq 'previous which) (funcall get-value (1- item-index)))
  1510. (t (funcall get-value (1+ item-index))))))
  1511. (org-list-repair new top)))
  1512. ;;; Checkboxes
  1513. (defun org-toggle-checkbox (&optional toggle-presence)
  1514. "Toggle the checkbox in the current line.
  1515. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
  1516. double prefix, set checkbox to [-].
  1517. When there is an active region, toggle status or presence of the
  1518. first checkbox there, and make every item inside have the
  1519. same status or presence, respectively.
  1520. If the cursor is in a headline, apply this to all checkbox items
  1521. in the text below the heading, taking as reference the first item
  1522. in subtree, ignoring drawers."
  1523. (interactive "P")
  1524. ;; Bounds is a list of type (beg end single-p) where single-p is t
  1525. ;; when `org-toggle-checkbox' is applied to a single item. Only
  1526. ;; toggles on single items will return errors.
  1527. (let* ((bounds
  1528. (cond
  1529. ((org-region-active-p)
  1530. (let ((rbeg (region-beginning))
  1531. (rend (region-end)))
  1532. (save-excursion
  1533. (goto-char rbeg)
  1534. (if (org-search-forward-unenclosed org-item-beginning-re rend 'move)
  1535. (list (point-at-bol) rend nil)
  1536. (error "No item in region")))))
  1537. ((org-on-heading-p)
  1538. ;; In this case, reference line is the first item in
  1539. ;; subtree outside drawers
  1540. (let ((pos (point))
  1541. (limit (save-excursion (outline-next-heading) (point))))
  1542. (save-excursion
  1543. (goto-char limit)
  1544. (org-search-backward-unenclosed ":END:" pos 'move)
  1545. (org-search-forward-unenclosed
  1546. org-item-beginning-re limit 'move)
  1547. (list (point) limit nil))))
  1548. ((org-at-item-p)
  1549. (list (point-at-bol) (point-at-eol) t))
  1550. (t (error "Not at an item or heading, and no active region"))))
  1551. (beg (car bounds))
  1552. ;; marker is needed because deleting or inserting checkboxes
  1553. ;; will change bottom point
  1554. (end (copy-marker (nth 1 bounds)))
  1555. (single-p (nth 2 bounds))
  1556. (ref-presence (save-excursion
  1557. (goto-char beg)
  1558. (org-at-item-checkbox-p)))
  1559. (ref-status (equal (match-string 1) "[X]"))
  1560. (act-on-item
  1561. (lambda (ref-pres ref-stat)
  1562. (if (equal toggle-presence '(4))
  1563. (cond
  1564. ((and ref-pres (org-at-item-checkbox-p))
  1565. (replace-match ""))
  1566. ((and (not ref-pres)
  1567. (not (org-at-item-checkbox-p))
  1568. (org-at-item-p))
  1569. (goto-char (match-end 0))
  1570. ;; Ignore counter, if any
  1571. (when (looking-at "\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?")
  1572. (goto-char (match-end 0)))
  1573. (let ((desc-p (and (org-at-item-description-p)
  1574. (cdr (assq 'checkbox org-list-automatic-rules)))))
  1575. (cond
  1576. ((and single-p desc-p)
  1577. (error "Cannot add a checkbox in a description list"))
  1578. ((not desc-p) (insert "[ ] "))))))
  1579. (let ((blocked (org-checkbox-blocked-p)))
  1580. (cond
  1581. ((and blocked single-p)
  1582. (error "Checkbox blocked because of unchecked box in line %d" blocked))
  1583. (blocked nil)
  1584. ((org-at-item-checkbox-p)
  1585. (replace-match
  1586. (cond ((equal toggle-presence '(16)) "[-]")
  1587. (ref-stat "[ ]")
  1588. (t "[X]"))
  1589. t t nil 1))))))))
  1590. (save-excursion
  1591. (goto-char beg)
  1592. (while (< (point) end)
  1593. (funcall act-on-item ref-presence ref-status)
  1594. (org-search-forward-unenclosed org-item-beginning-re end 'move)))
  1595. (org-update-checkbox-count-maybe)))
  1596. (defun org-reset-checkbox-state-subtree ()
  1597. "Reset all checkboxes in an entry subtree."
  1598. (interactive "*")
  1599. (save-restriction
  1600. (save-excursion
  1601. (org-narrow-to-subtree)
  1602. (org-show-subtree)
  1603. (goto-char (point-min))
  1604. (let ((end (point-max)))
  1605. (while (< (point) end)
  1606. (when (org-at-item-checkbox-p)
  1607. (replace-match "[ ]" t t nil 1))
  1608. (beginning-of-line 2))))
  1609. (org-update-checkbox-count-maybe)))
  1610. (defvar org-checkbox-statistics-hook nil
  1611. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  1612. This hook runs even if checkbox rule in
  1613. `org-list-automatic-rules' does not apply, so it can be used to
  1614. implement alternative ways of collecting statistics
  1615. information.")
  1616. (defun org-update-checkbox-count-maybe ()
  1617. "Update checkbox statistics unless turned off by user."
  1618. (when (cdr (assq 'checkbox org-list-automatic-rules))
  1619. (org-update-checkbox-count))
  1620. (run-hooks 'org-checkbox-statistics-hook))
  1621. (defun org-update-checkbox-count (&optional all)
  1622. "Update the checkbox statistics in the current section.
  1623. This will find all statistic cookies like [57%] and [6/12] and update them
  1624. with the current numbers. With optional prefix argument ALL, do this for
  1625. the whole buffer."
  1626. (interactive "P")
  1627. (save-excursion
  1628. (let ((cstat 0))
  1629. (catch 'exit
  1630. (while t
  1631. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  1632. (beg (condition-case nil
  1633. (progn (org-back-to-heading) (point))
  1634. (error (point-min))))
  1635. (end (copy-marker (save-excursion
  1636. (outline-next-heading) (point))))
  1637. (re-cookie "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  1638. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
  1639. beg-cookie end-cookie is-percent c-on c-off lim new
  1640. curr-ind next-ind continue-from startsearch list-beg list-end
  1641. (recursive
  1642. (or (not org-hierarchical-checkbox-statistics)
  1643. (string-match "\\<recursive\\>"
  1644. (or (ignore-errors
  1645. (org-entry-get nil "COOKIE_DATA"))
  1646. "")))))
  1647. (goto-char end)
  1648. ;; find each statistics cookie
  1649. (while (and (org-search-backward-unenclosed re-cookie beg 'move)
  1650. (not (save-match-data
  1651. (and (org-on-heading-p)
  1652. (string-match "\\<todo\\>"
  1653. (downcase
  1654. (or (org-entry-get
  1655. nil "COOKIE_DATA")
  1656. "")))))))
  1657. (setq beg-cookie (match-beginning 1)
  1658. end-cookie (match-end 1)
  1659. cstat (+ cstat (if end-cookie 1 0))
  1660. startsearch (point-at-eol)
  1661. continue-from (match-beginning 0)
  1662. is-percent (match-beginning 2)
  1663. lim (cond
  1664. ((org-on-heading-p) (outline-next-heading) (point))
  1665. ;; Ensure many cookies in the same list won't imply
  1666. ;; computing list boundaries as many times.
  1667. ((org-at-item-p)
  1668. (unless (and list-beg (>= (point) list-beg))
  1669. (setq list-beg (org-list-top-point)
  1670. list-end (copy-marker
  1671. (org-list-bottom-point))))
  1672. (org-get-end-of-item list-end))
  1673. (t nil))
  1674. c-on 0
  1675. c-off 0)
  1676. (when lim
  1677. ;; find first checkbox for this cookie and gather
  1678. ;; statistics from all that are at this indentation level
  1679. (goto-char startsearch)
  1680. (if (org-search-forward-unenclosed re-box lim t)
  1681. (progn
  1682. (beginning-of-line)
  1683. (setq curr-ind (org-get-indentation))
  1684. (setq next-ind curr-ind)
  1685. (while (and (bolp) (org-at-item-p)
  1686. (if recursive
  1687. (<= curr-ind next-ind)
  1688. (= curr-ind next-ind)))
  1689. (when (org-at-item-checkbox-p)
  1690. (if (member (match-string 1) '("[ ]" "[-]"))
  1691. (setq c-off (1+ c-off))
  1692. (setq c-on (1+ c-on))))
  1693. (if (not recursive)
  1694. ;; org-get-next-item goes through list-enders
  1695. ;; with proper limit.
  1696. (goto-char (or (org-get-next-item (point) lim) lim))
  1697. (end-of-line)
  1698. (when (org-search-forward-unenclosed
  1699. org-item-beginning-re lim t)
  1700. (beginning-of-line)))
  1701. (setq next-ind (org-get-indentation)))))
  1702. (goto-char continue-from)
  1703. ;; update cookie
  1704. (when end-cookie
  1705. (setq new (if is-percent
  1706. (format "[%d%%]" (/ (* 100 c-on)
  1707. (max 1 (+ c-on c-off))))
  1708. (format "[%d/%d]" c-on (+ c-on c-off))))
  1709. (goto-char beg-cookie)
  1710. (insert new)
  1711. (delete-region (point) (+ (point) (- end-cookie beg-cookie))))
  1712. ;; update items checkbox if it has one
  1713. (when (and (org-at-item-checkbox-p)
  1714. (> (+ c-on c-off) 0))
  1715. (setq beg-cookie (match-beginning 1)
  1716. end-cookie (match-end 1))
  1717. (delete-region beg-cookie end-cookie)
  1718. (goto-char beg-cookie)
  1719. (cond ((= c-off 0) (insert "[X]"))
  1720. ((= c-on 0) (insert "[ ]"))
  1721. (t (insert "[-]")))))
  1722. (goto-char continue-from)))
  1723. (unless (and all (outline-next-heading)) (throw 'exit nil))))
  1724. (when (interactive-p)
  1725. (message "Checkbox statistics updated %s (%d places)"
  1726. (if all "in entire file" "in current outline entry") cstat)))))
  1727. (defun org-get-checkbox-statistics-face ()
  1728. "Select the face for checkbox statistics.
  1729. The face will be `org-done' when all relevant boxes are checked.
  1730. Otherwise it will be `org-todo'."
  1731. (if (match-end 1)
  1732. (if (equal (match-string 1) "100%")
  1733. 'org-checkbox-statistics-done
  1734. 'org-checkbox-statistics-todo)
  1735. (if (and (> (match-end 2) (match-beginning 2))
  1736. (equal (match-string 2) (match-string 3)))
  1737. 'org-checkbox-statistics-done
  1738. 'org-checkbox-statistics-todo)))
  1739. ;;; Misc Tools
  1740. (defun org-apply-on-list (function init-value &rest args)
  1741. "Call FUNCTION for each item of a the list under point.
  1742. FUNCTION must be called with at least one argument: INIT-VALUE,
  1743. that will contain the value returned by the function at the
  1744. previous item, plus ARGS extra arguments.
  1745. As an example, (org-apply-on-list (lambda (result) (1+ result)) 0)
  1746. will return the number of items in the current list.
  1747. Sublists of the list are skipped. Cursor is always at the
  1748. beginning of the item."
  1749. (let* ((pos (copy-marker (point)))
  1750. (end (copy-marker (org-list-bottom-point)))
  1751. (next-p (copy-marker (org-get-beginning-of-list (org-list-top-point))))
  1752. (value init-value))
  1753. (while (< next-p end)
  1754. (goto-char next-p)
  1755. (set-marker next-p (or (org-get-next-item (point) end) end))
  1756. (setq value (apply function value args)))
  1757. (goto-char pos)
  1758. value))
  1759. (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
  1760. "Sort plain list items.
  1761. The cursor may be at any item of the list that should be sorted.
  1762. Sublists are not sorted. Checkboxes, if any, are ignored.
  1763. Sorting can be alphabetically, numerically, by date/time as given by
  1764. a time stamp, by a property or by priority.
  1765. Comparing entries ignores case by default. However, with an
  1766. optional argument WITH-CASE, the sorting considers case as well.
  1767. The command prompts for the sorting type unless it has been given
  1768. to the function through the SORTING-TYPE argument, which needs to
  1769. be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
  1770. meaning of each character:
  1771. n Numerically, by converting the beginning of the item to a number.
  1772. a Alphabetically. Only the first line of item is checked.
  1773. t By date/time, either the first active time stamp in the entry, if
  1774. any, or by the first inactive one. In a timer list, sort the timers.
  1775. Capital letters will reverse the sort order.
  1776. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a
  1777. function to be called with point at the beginning of the record.
  1778. It must return either a string or a number that should serve as
  1779. the sorting key for that record. It will then use COMPARE-FUNC to
  1780. compare entries."
  1781. (interactive "P")
  1782. (let* ((case-func (if with-case 'identity 'downcase))
  1783. (top (org-list-top-point))
  1784. (bottom (org-list-bottom-point))
  1785. (start (org-get-beginning-of-list top))
  1786. (end (org-get-end-of-list bottom))
  1787. (sorting-type
  1788. (progn
  1789. (message
  1790. "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
  1791. (read-char-exclusive)))
  1792. (getkey-func (and (= (downcase sorting-type) ?f)
  1793. (org-icompleting-read "Sort using function: "
  1794. obarray 'fboundp t nil nil)
  1795. (intern getkey-func))))
  1796. (message "Sorting items...")
  1797. (save-restriction
  1798. (narrow-to-region start end)
  1799. (let* ((dcst (downcase sorting-type))
  1800. (case-fold-search nil)
  1801. (now (current-time))
  1802. (sort-func (cond
  1803. ((= dcst ?a) 'string<)
  1804. ((= dcst ?f) compare-func)
  1805. ((= dcst ?t) '<)
  1806. (t nil)))
  1807. (begin-record (lambda ()
  1808. (skip-chars-forward " \r\t\n")
  1809. (beginning-of-line)))
  1810. (end-record (lambda ()
  1811. (goto-char (org-end-of-item-before-blank end))))
  1812. (value-to-sort
  1813. (lambda ()
  1814. (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
  1815. (cond
  1816. ((= dcst ?n)
  1817. (string-to-number (buffer-substring (match-end 0)
  1818. (point-at-eol))))
  1819. ((= dcst ?a)
  1820. (buffer-substring (match-end 0) (point-at-eol)))
  1821. ((= dcst ?t)
  1822. (cond
  1823. ;; If it is a timer list, convert timer to seconds
  1824. ((org-at-item-timer-p)
  1825. (org-timer-hms-to-secs (match-string 1)))
  1826. ((or (org-search-forward-unenclosed org-ts-regexp
  1827. (point-at-eol) t)
  1828. (org-search-forward-unenclosed org-ts-regexp-both
  1829. (point-at-eol) t))
  1830. (org-time-string-to-seconds (match-string 0)))
  1831. (t (org-float-time now))))
  1832. ((= dcst ?f)
  1833. (if getkey-func
  1834. (let ((value (funcall getkey-func)))
  1835. (if (stringp value)
  1836. (funcall case-func value)
  1837. value))
  1838. (error "Invalid key function `%s'" getkey-func)))
  1839. (t (error "Invalid sorting type `%c'" sorting-type)))))))
  1840. (sort-subr (/= dcst sorting-type)
  1841. begin-record
  1842. end-record
  1843. value-to-sort
  1844. nil
  1845. sort-func)
  1846. (org-list-repair nil top bottom)
  1847. (run-hooks 'org-after-sorting-entries-or-items-hook)
  1848. (message "Sorting items...done")))))
  1849. ;;; Send and receive lists
  1850. (defun org-list-parse-list (&optional delete)
  1851. "Parse the list at point and maybe DELETE it.
  1852. Return a list containing first level items as strings and
  1853. sublevels as a list of strings."
  1854. (let* ((start (goto-char (org-list-top-point)))
  1855. (end (org-list-bottom-point))
  1856. output itemsep ltype)
  1857. (while (org-search-forward-unenclosed org-item-beginning-re end t)
  1858. (save-excursion
  1859. (beginning-of-line)
  1860. (setq ltype (cond ((looking-at-p "^[ \t]*[0-9]") 'ordered)
  1861. ((org-at-item-description-p) 'descriptive)
  1862. (t 'unordered))))
  1863. (let* ((indent1 (org-get-indentation))
  1864. (nextitem (or (org-get-next-item (point) end) end))
  1865. (item (org-trim (buffer-substring (point)
  1866. (org-end-of-item-or-at-child end))))
  1867. (nextindent (if (= (point) end) 0 (org-get-indentation)))
  1868. (item (if (string-match
  1869. "^\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\[\\([xX ]\\)\\]"
  1870. item)
  1871. (replace-match (if (equal (match-string 1 item) " ")
  1872. "CBOFF"
  1873. "CBON")
  1874. t nil item 1)
  1875. item)))
  1876. (push item output)
  1877. (when (> nextindent indent1)
  1878. (save-restriction
  1879. (narrow-to-region (point) nextitem)
  1880. (push (org-list-parse-list) output)))))
  1881. (when delete
  1882. (delete-region start end)
  1883. (save-match-data
  1884. (when (and (not (eq org-list-ending-method 'indent))
  1885. (looking-at (org-list-end-re)))
  1886. (replace-match "\n"))))
  1887. (setq output (nreverse output))
  1888. (push ltype output)))
  1889. (defun org-list-make-subtree ()
  1890. "Convert the plain list at point into a subtree."
  1891. (interactive)
  1892. (if (not (org-in-item-p))
  1893. (error "Not in a list")
  1894. (let ((list (org-list-parse-list t)) nstars)
  1895. (save-excursion
  1896. (if (ignore-errors
  1897. (org-back-to-heading))
  1898. (progn (looking-at org-complex-heading-regexp)
  1899. (setq nstars (length (match-string 1))))
  1900. (setq nstars 0)))
  1901. (org-list-make-subtrees list (1+ nstars)))))
  1902. (defun org-list-make-subtrees (list level)
  1903. "Convert LIST into subtrees starting at LEVEL."
  1904. (if (symbolp (car list))
  1905. (org-list-make-subtrees (cdr list) level)
  1906. (mapcar (lambda (item)
  1907. (if (stringp item)
  1908. (insert (make-string
  1909. (if org-odd-levels-only
  1910. (1- (* 2 level)) level) ?*) " " item "\n")
  1911. (org-list-make-subtrees item (1+ level))))
  1912. list)))
  1913. (defun org-list-insert-radio-list ()
  1914. "Insert a radio list template appropriate for this major mode."
  1915. (interactive)
  1916. (let* ((e (assq major-mode org-list-radio-list-templates))
  1917. (txt (nth 1 e))
  1918. name pos)
  1919. (unless e (error "No radio list setup defined for %s" major-mode))
  1920. (setq name (read-string "List name: "))
  1921. (while (string-match "%n" txt)
  1922. (setq txt (replace-match name t t txt)))
  1923. (or (bolp) (insert "\n"))
  1924. (setq pos (point))
  1925. (insert txt)
  1926. (goto-char pos)))
  1927. (defun org-list-send-list (&optional maybe)
  1928. "Send a transformed version of this list to the receiver position.
  1929. With argument MAYBE, fail quietly if no transformation is defined for
  1930. this list."
  1931. (interactive)
  1932. (catch 'exit
  1933. (unless (org-at-item-p) (error "Not at a list item"))
  1934. (save-excursion
  1935. (re-search-backward "#\\+ORGLST" nil t)
  1936. (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
  1937. (if maybe
  1938. (throw 'exit nil)
  1939. (error "Don't know how to transform this list"))))
  1940. (let* ((name (match-string 1))
  1941. (transform (intern (match-string 2)))
  1942. (bottom-point
  1943. (save-excursion
  1944. (re-search-forward
  1945. "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
  1946. (match-beginning 0)))
  1947. (top-point
  1948. (progn
  1949. (re-search-backward "#\\+ORGLST" nil t)
  1950. (re-search-forward org-item-beginning-re bottom-point t)
  1951. (match-beginning 0)))
  1952. (list (save-restriction
  1953. (narrow-to-region top-point bottom-point)
  1954. (org-list-parse-list)))
  1955. beg txt)
  1956. (unless (fboundp transform)
  1957. (error "No such transformation function %s" transform))
  1958. (let ((txt (funcall transform list)))
  1959. ;; Find the insertion place
  1960. (save-excursion
  1961. (goto-char (point-min))
  1962. (unless (re-search-forward
  1963. (concat "BEGIN RECEIVE ORGLST +"
  1964. name
  1965. "\\([ \t]\\|$\\)") nil t)
  1966. (error "Don't know where to insert translated list"))
  1967. (goto-char (match-beginning 0))
  1968. (beginning-of-line 2)
  1969. (setq beg (point))
  1970. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  1971. (error "Cannot find end of insertion region"))
  1972. (delete-region beg (point-at-bol))
  1973. (goto-char beg)
  1974. (insert txt "\n")))
  1975. (message "List converted and installed at receiver location"))))
  1976. (defun org-list-to-generic (list params)
  1977. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  1978. Valid parameters PARAMS are
  1979. :ustart String to start an unordered list
  1980. :uend String to end an unordered list
  1981. :ostart String to start an ordered list
  1982. :oend String to end an ordered list
  1983. :dstart String to start a descriptive list
  1984. :dend String to end a descriptive list
  1985. :dtstart String to start a descriptive term
  1986. :dtend String to end a descriptive term
  1987. :ddstart String to start a description
  1988. :ddend String to end a description
  1989. :splice When set to t, return only list body lines, don't wrap
  1990. them into :[u/o]start and :[u/o]end. Default is nil.
  1991. :istart String to start a list item
  1992. :iend String to end a list item
  1993. :isep String to separate items
  1994. :lsep String to separate sublists
  1995. :cboff String to insert for an unchecked checkbox
  1996. :cbon String to insert for a checked checkbox"
  1997. (interactive)
  1998. (let* ((p params) sublist
  1999. (splicep (plist-get p :splice))
  2000. (ostart (plist-get p :ostart))
  2001. (oend (plist-get p :oend))
  2002. (ustart (plist-get p :ustart))
  2003. (uend (plist-get p :uend))
  2004. (dstart (plist-get p :dstart))
  2005. (dend (plist-get p :dend))
  2006. (dtstart (plist-get p :dtstart))
  2007. (dtend (plist-get p :dtend))
  2008. (ddstart (plist-get p :ddstart))
  2009. (ddend (plist-get p :ddend))
  2010. (istart (plist-get p :istart))
  2011. (iend (plist-get p :iend))
  2012. (isep (plist-get p :isep))
  2013. (lsep (plist-get p :lsep))
  2014. (cbon (plist-get p :cbon))
  2015. (cboff (plist-get p :cboff)))
  2016. (let ((wrapper
  2017. (cond ((eq (car list) 'ordered)
  2018. (concat ostart "\n%s" oend "\n"))
  2019. ((eq (car list) 'unordered)
  2020. (concat ustart "\n%s" uend "\n"))
  2021. ((eq (car list) 'descriptive)
  2022. (concat dstart "\n%s" dend "\n"))))
  2023. rtn term defstart defend)
  2024. (while (setq sublist (pop list))
  2025. (cond ((symbolp sublist) nil)
  2026. ((stringp sublist)
  2027. (when (string-match "^\\(\\S-+\\)[ \t]+::" sublist)
  2028. (setq term (org-trim (format (concat dtstart "%s" dtend)
  2029. (match-string 1 sublist))))
  2030. (setq sublist (concat ddstart
  2031. (org-trim (substring sublist
  2032. (match-end 0)))
  2033. ddend)))
  2034. (if (string-match "\\[CBON\\]" sublist)
  2035. (setq sublist (replace-match cbon t t sublist)))
  2036. (if (string-match "\\[CBOFF\\]" sublist)
  2037. (setq sublist (replace-match cboff t t sublist)))
  2038. (if (string-match "\\[-\\]" sublist)
  2039. (setq sublist (replace-match "$\\boxminus$" t t sublist)))
  2040. (setq rtn (concat rtn istart term sublist iend isep)))
  2041. (t (setq rtn (concat rtn ;; previous list
  2042. lsep ;; list separator
  2043. (org-list-to-generic sublist p)
  2044. lsep ;; list separator
  2045. )))))
  2046. (format wrapper rtn))))
  2047. (defun org-list-to-latex (list &optional params)
  2048. "Convert LIST into a LaTeX list.
  2049. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2050. with overruling parameters for `org-list-to-generic'."
  2051. (org-list-to-generic
  2052. list
  2053. (org-combine-plists
  2054. '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  2055. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  2056. :dstart "\\begin{description}" :dend "\\end{description}"
  2057. :dtstart "[" :dtend "]"
  2058. :ddstart "" :ddend ""
  2059. :istart "\\item " :iend ""
  2060. :isep "\n" :lsep "\n"
  2061. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  2062. params)))
  2063. (defun org-list-to-html (list &optional params)
  2064. "Convert LIST into a HTML list.
  2065. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2066. with overruling parameters for `org-list-to-generic'."
  2067. (org-list-to-generic
  2068. list
  2069. (org-combine-plists
  2070. '(:splicep nil :ostart "<ol>" :oend "</ol>"
  2071. :ustart "<ul>" :uend "</ul>"
  2072. :dstart "<dl>" :dend "</dl>"
  2073. :dtstart "<dt>" :dtend "</dt>"
  2074. :ddstart "<dd>" :ddend "</dd>"
  2075. :istart "<li>" :iend "</li>"
  2076. :isep "\n" :lsep "\n"
  2077. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
  2078. params)))
  2079. (defun org-list-to-texinfo (list &optional params)
  2080. "Convert LIST into a Texinfo list.
  2081. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2082. with overruling parameters for `org-list-to-generic'."
  2083. (org-list-to-generic
  2084. list
  2085. (org-combine-plists
  2086. '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  2087. :ustart "@enumerate" :uend "@end enumerate"
  2088. :dstart "@table" :dend "@end table"
  2089. :dtstart "@item " :dtend "\n"
  2090. :ddstart "" :ddend ""
  2091. :istart "@item\n" :iend ""
  2092. :isep "\n" :lsep "\n"
  2093. :cbon "@code{[X]}" :cboff "@code{[ ]}")
  2094. params)))
  2095. (provide 'org-list)
  2096. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  2097. ;;; org-list.el ends here