org-list.el 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814
  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.4
  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. ;; The fundamental idea behind lists work is to use structures. A
  29. ;; structure is a snapshot of the list, in the shape of data tree (see
  30. ;; `org-list-struct').
  31. ;; Once the list structure is stored, it is possible to make changes
  32. ;; directly on it or get useful information on the list, with helper
  33. ;; functions `org-list-struct-parent-alist' and
  34. ;; `org-list-struct-prev-alist', and using accessors provided in the
  35. ;; file.
  36. ;; Structure is repaired with `org-list-struct-fix-struct'. Then
  37. ;; changes are applied to buffer with `org-list-struct-apply-struct'.
  38. ;; So any function working on plain lists should follow this template:
  39. ;; 1. Verify point is in a list and grab item beginning (with the same
  40. ;; function `org-in-item-p') ;
  41. ;; 2. Get list structure ;
  42. ;; 3. Compute one, or both, helper functions depending on required
  43. ;; accessors ;
  44. ;; 4. Proceed with the modifications ;
  45. ;; 5. Then fix the structure one last time and apply it on buffer.
  46. ;; It is usally a bad idea to use directly an interactive function
  47. ;; inside a function, as those read the whole list structure another
  48. ;; time.
  49. ;;; Code:
  50. (eval-when-compile
  51. (require 'cl))
  52. (require 'org-macs)
  53. (require 'org-compat)
  54. (defvar org-blank-before-new-entry)
  55. (defvar org-M-RET-may-split-line)
  56. (defvar org-complex-heading-regexp)
  57. (defvar org-odd-levels-only)
  58. (defvar org-outline-regexp)
  59. (defvar org-ts-regexp)
  60. (defvar org-ts-regexp-both)
  61. (declare-function org-at-heading-p "org" (&optional ignored))
  62. (declare-function org-back-over-empty-lines "org" ())
  63. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  64. (declare-function org-combine-plists "org" (&rest plists))
  65. (declare-function org-entry-get "org"
  66. (pom property &optional inherit literal-nil))
  67. (declare-function org-get-indentation "org" (&optional line))
  68. (declare-function org-icompleting-read "org" (&rest args))
  69. (declare-function org-in-regexps-block-p "org"
  70. (start-re end-re &optional bound))
  71. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  72. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  73. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  74. (declare-function org-invisible-p "org" ())
  75. (declare-function org-level-increment "org" ())
  76. (declare-function org-narrow-to-subtree "org" ())
  77. (declare-function org-on-heading-p "org" (&optional invisible-ok))
  78. (declare-function org-remove-if "org" (predicate seq))
  79. (declare-function org-show-subtree "org" ())
  80. (declare-function org-time-string-to-seconds "org" (s))
  81. (declare-function org-timer-hms-to-secs "org-timer" (hms))
  82. (declare-function org-timer-item "org-timer" (&optional arg))
  83. (declare-function org-trim "org" (s))
  84. (declare-function org-uniquify "org" (list))
  85. (declare-function outline-next-heading "outline" ())
  86. (declare-function outline-previous-heading "outline" ())
  87. (defgroup org-plain-lists nil
  88. "Options concerning plain lists in Org-mode."
  89. :tag "Org Plain lists"
  90. :group 'org-structure)
  91. (defcustom org-cycle-include-plain-lists t
  92. "When t, make TAB cycle visibility on plain list items.
  93. Cycling plain lists works only when the cursor is on a plain list
  94. item. When the cursor is on an outline heading, plain lists are
  95. treated as text. This is the most stable way of handling this,
  96. which is why it is the default.
  97. When this is the symbol `integrate', then during cycling, plain
  98. list items will *temporarily* be interpreted as outline headlines
  99. with a level given by 1000+i where i is the indentation of the
  100. bullet. This setting can lead to strange effects when switching
  101. visibility to `children', because the first \"child\" in a
  102. subtree decides what children should be listed. If that first
  103. \"child\" is a plain list item with an implied large level
  104. number, all true children and grand children of the outline
  105. heading will be exposed in a children' view."
  106. :group 'org-plain-lists
  107. :type '(choice
  108. (const :tag "Never" nil)
  109. (const :tag "With cursor in plain list (recommended)" t)
  110. (const :tag "As children of outline headings" integrate)))
  111. (defcustom org-list-demote-modify-bullet nil
  112. "Default bullet type installed when demoting an item.
  113. This is an association list, for each bullet type, this alist will point
  114. to the bullet that should be used when this item is demoted.
  115. For example,
  116. (setq org-list-demote-modify-bullet
  117. '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
  118. will make
  119. + Movies
  120. + Silence of the Lambs
  121. + My Cousin Vinny
  122. + Books
  123. + The Hunt for Red October
  124. + The Road to Omaha
  125. into
  126. + Movies
  127. - Silence of the Lambs
  128. - My Cousin Vinny
  129. + Books
  130. - The Hunt for Red October
  131. - The Road to Omaha"
  132. :group 'org-plain-lists
  133. :type '(repeat
  134. (cons
  135. (choice :tag "If the current bullet is "
  136. (const "-")
  137. (const "+")
  138. (const "*")
  139. (const "1.")
  140. (const "1)"))
  141. (choice :tag "demotion will change it to"
  142. (const "-")
  143. (const "+")
  144. (const "*")
  145. (const "1.")
  146. (const "1)")))))
  147. (defcustom org-plain-list-ordered-item-terminator t
  148. "The character that makes a line with leading number an ordered list item.
  149. Valid values are ?. and ?\). To get both terminators, use t. While
  150. ?. may look nicer, it creates the danger that a line with leading
  151. number may be incorrectly interpreted as an item. ?\) therefore is
  152. the safe choice."
  153. :group 'org-plain-lists
  154. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  155. (const :tag "paren like in \"2)\"" ?\))
  156. (const :tab "both" t)))
  157. (defcustom org-alphabetical-lists nil
  158. "Non-nil means single character alphabetical bullets are allowed.
  159. Both uppercase and lowercase are handled. Lists with more than 26
  160. items will fallback to standard numbering."
  161. :group 'org-plain-lists
  162. :type 'boolean)
  163. (defcustom org-list-two-spaces-after-bullet-regexp nil
  164. "A regular expression matching bullets that should have 2 spaces after them.
  165. When nil, no bullet will have two spaces after them.
  166. When a string, it will be used as a regular expression. When the
  167. bullet type of a list is changed, the new bullet type will be
  168. matched against this regexp. If it matches, there will be two
  169. spaces instead of one after the bullet in each item of the list."
  170. :group 'org-plain-lists
  171. :type '(choice
  172. (const :tag "never" nil)
  173. (regexp)))
  174. (defcustom org-list-ending-method 'both
  175. "Determine where plain lists should end.
  176. Valid values are: `regexp', `indent' or `both'.
  177. When set to `regexp', Org will look into two variables,
  178. `org-empty-line-terminates-plain-lists' and the more general
  179. `org-list-end-regexp', to determine what will end lists.
  180. When set to `indent', a list will end whenever a line following
  181. an item, but not starting one, is less or equally indented than
  182. the first item of the list.
  183. When set to `both', each of the preceding methods is applied to
  184. determine lists endings. This is the default method."
  185. :group 'org-plain-lists
  186. :type '(choice
  187. (const :tag "With a regexp defining ending" regexp)
  188. (const :tag "With indentation of regular (no bullet) text" indent)
  189. (const :tag "With both methods" both)))
  190. (defcustom org-empty-line-terminates-plain-lists nil
  191. "Non-nil means an empty line ends all plain list levels.
  192. This variable only makes sense if `org-list-ending-method' is set
  193. to `regexp' or `both'. This is then equivalent to set
  194. `org-list-end-regexp' to \"^[ \\t]*$\"."
  195. :group 'org-plain-lists
  196. :type 'boolean)
  197. (defcustom org-list-end-regexp "^[ \t]*\n[ \t]*\n"
  198. "Regexp matching the end of all plain list levels.
  199. It must start with \"^\" and end with \"\\n\". It defaults to 2
  200. blank lines. `org-empty-line-terminates-plain-lists' has
  201. precedence over it."
  202. :group 'org-plain-lists
  203. :type 'string)
  204. (defcustom org-list-automatic-rules '((bullet . t)
  205. (checkbox . t)
  206. (indent . t))
  207. "Non-nil means apply set of rules when acting on lists.
  208. By default, automatic actions are taken when using
  209. \\[org-meta-return], \\[org-metaright], \\[org-metaleft],
  210. \\[org-shiftmetaright], \\[org-shiftmetaleft],
  211. \\[org-ctrl-c-minus], \\[org-toggle-checkbox] or
  212. \\[org-insert-todo-heading]. You can disable individually these
  213. rules by setting them to nil. Valid rules are:
  214. bullet when non-nil, cycling bullet do not allow lists at
  215. column 0 to have * as a bullet and descriptions lists
  216. to be numbered.
  217. checkbox when non-nil, checkbox statistics is updated each time
  218. you either insert a new checkbox or toggle a checkbox.
  219. It also prevents from inserting a checkbox in a
  220. description item.
  221. indent when non-nil, indenting or outdenting list top-item
  222. with its subtree will move the whole list and
  223. outdenting a list whose bullet is * to column 0 will
  224. change that bullet to \"-\"."
  225. :group 'org-plain-lists
  226. :type '(alist :tag "Sets of rules"
  227. :key-type
  228. (choice
  229. (const :tag "Bullet" bullet)
  230. (const :tag "Checkbox" checkbox)
  231. (const :tag "Indent" indent)
  232. (const :tag "Insert" insert))
  233. :value-type
  234. (boolean :tag "Activate" :value t)))
  235. (defcustom org-hierarchical-checkbox-statistics t
  236. "Non-nil means checkbox statistics counts only the state of direct children.
  237. When nil, all boxes below the cookie are counted.
  238. This can be set to nil on a per-node basis using a COOKIE_DATA property
  239. with the word \"recursive\" in the value."
  240. :group 'org-plain-lists
  241. :type 'boolean)
  242. (defcustom org-description-max-indent 20
  243. "Maximum indentation for the second line of a description list.
  244. When the indentation would be larger than this, it will become
  245. 5 characters instead."
  246. :group 'org-plain-lists
  247. :type 'integer)
  248. (defcustom org-list-radio-list-templates
  249. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  250. % END RECEIVE ORGLST %n
  251. \\begin{comment}
  252. #+ORGLST: SEND %n org-list-to-latex
  253. -
  254. \\end{comment}\n")
  255. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  256. @c END RECEIVE ORGLST %n
  257. @ignore
  258. #+ORGLST: SEND %n org-list-to-texinfo
  259. -
  260. @end ignore\n")
  261. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  262. <!-- END RECEIVE ORGLST %n -->
  263. <!--
  264. #+ORGLST: SEND %n org-list-to-html
  265. -
  266. -->\n"))
  267. "Templates for radio lists in different major modes.
  268. All occurrences of %n in a template will be replaced with the name of the
  269. list, obtained by prompting the user."
  270. :group 'org-plain-lists
  271. :type '(repeat
  272. (list (symbol :tag "Major mode")
  273. (string :tag "Format"))))
  274. (defvar org-list-forbidden-blocks '("example" "verse" "src")
  275. "Names of blocks where lists are not allowed.
  276. Names must be in lower case.")
  277. (defvar org-list-export-context '(block inlinetask)
  278. "Context types where lists will be interpreted during export.
  279. Valid types are `drawer', `inlinetask' and `block'. More
  280. specifically, type `block' is determined by the variable
  281. `org-list-forbidden-blocks'.")
  282. ;;; Internal functions
  283. (defconst org-list-end-re (if org-empty-line-terminates-plain-lists
  284. "^[ \t]*\n"
  285. org-list-end-regexp)
  286. "Regex corresponding to the end of a list.
  287. It depends on `org-empty-line-terminates-plain-lists'.")
  288. (defun org-item-re ()
  289. "Return the correct regular expression for plain lists."
  290. (let ((term (cond
  291. ((eq org-plain-list-ordered-item-terminator t) "[.)]")
  292. ((= org-plain-list-ordered-item-terminator ?\)) ")")
  293. ((= org-plain-list-ordered-item-terminator ?.) "\\.")
  294. (t "[.)]")))
  295. (alpha (if org-alphabetical-lists "\\|[A-Za-z]" "")))
  296. (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha "\\)" term
  297. "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
  298. (defun org-item-beginning-re ()
  299. "Regexp matching the beginning of a plain list item."
  300. (concat "^" (org-item-re)))
  301. (defconst org-list-full-item-re
  302. (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\\)"
  303. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\]\\)?"
  304. "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
  305. "\\(?:\\(.*\\)[ \t]+::[ \t]+\\)?")
  306. "Matches a list item and puts everything into groups:
  307. group 1: bullet
  308. group 2: counter
  309. group 3: checkbox
  310. group 4: description tag")
  311. (defun org-list-context ()
  312. "Determine context, and its boundaries, around point.
  313. Context will be an alist like (MIN MAX CONTEXT) where MIN and MAX
  314. are boundaries and CONTEXT is a symbol among `drawer', `block',
  315. `invalid', `inlinetask' and nil.
  316. Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
  317. (save-match-data
  318. (save-excursion
  319. (beginning-of-line)
  320. (let* ((outline-regexp (org-get-limited-outline-regexp))
  321. ;; can't use org-drawers-regexp as this function might be
  322. ;; called in buffers not in Org mode
  323. (drawers-re (concat "^[ \t]*:\\("
  324. (mapconcat 'regexp-quote org-drawers "\\|")
  325. "\\):[ \t]*$"))
  326. (case-fold-search t)
  327. ;; compute position of surrounding headings. this is the
  328. ;; default context.
  329. (heading
  330. (save-excursion
  331. (list
  332. (or (and (org-at-heading-p) (point-at-bol))
  333. (outline-previous-heading)
  334. (point-min))
  335. (or (outline-next-heading)
  336. (point-max))
  337. nil)))
  338. (prev-head (car heading))
  339. (next-head (nth 1 heading))
  340. ;; Are we strictly inside a drawer?
  341. (drawerp
  342. (when (and (org-in-regexps-block-p
  343. drawers-re "^[ \t]*:END:" prev-head)
  344. (save-excursion
  345. (beginning-of-line)
  346. (and (not (looking-at drawers-re))
  347. (not (looking-at "^[ \t]*:END:")))))
  348. (save-excursion
  349. (list
  350. (progn
  351. (re-search-backward drawers-re prev-head t)
  352. (1+ (point-at-eol)))
  353. (if (re-search-forward "^[ \t]*:END:" next-head t)
  354. (1- (point-at-bol))
  355. next-head)
  356. 'drawer))))
  357. ;; Are we strictly in a block, and of which type?
  358. (blockp
  359. (save-excursion
  360. (when (and (org-in-regexps-block-p
  361. "^[ \t]*#\\+begin_" "^[ \t]*#\\+end_" prev-head)
  362. (save-excursion
  363. (beginning-of-line)
  364. (not (looking-at
  365. "^[ \t]*#\\+\\(begin\\|end\\)_"))))
  366. (list
  367. (progn
  368. (re-search-backward
  369. "^[ \t]*#\\+begin_\\(\\S-+\\)" prev-head t)
  370. (1+ (point-at-eol)))
  371. (save-match-data
  372. (if (re-search-forward "^[ \t]*#\\+end_" next-head t)
  373. (1- (point-at-bol))
  374. next-head))
  375. (if (member (downcase (match-string 1))
  376. org-list-forbidden-blocks)
  377. 'invalid
  378. 'block)))))
  379. ;; Are we in an inlinetask?
  380. (inlinetaskp
  381. (when (and (featurep 'org-inlinetask)
  382. (org-inlinetask-in-task-p)
  383. (not (looking-at "^\\*+")))
  384. (save-excursion
  385. (list
  386. (progn (org-inlinetask-goto-beginning)
  387. (1+ (point-at-eol)))
  388. (progn
  389. (org-inlinetask-goto-end)
  390. (forward-line -1)
  391. (1- (point-at-bol)))
  392. 'inlinetask))))
  393. ;; list actual candidates
  394. (context-list
  395. (delq nil (list heading drawerp blockp inlinetaskp))))
  396. ;; Return the closest context around
  397. (assq (apply 'max (mapcar 'car context-list)) context-list)))))
  398. (defun org-list-search-generic (search re bound noerr)
  399. "Search a string in valid contexts for lists.
  400. Arguments SEARCH, RE, BOUND and NOERR are similar to those in
  401. `re-search-forward'."
  402. (catch 'exit
  403. (let ((origin (point)))
  404. (while t
  405. ;; 1. No match: return to origin or bound, depending on NOERR.
  406. (unless (funcall search re bound noerr)
  407. (throw 'exit (and (goto-char (if (memq noerr '(t nil)) origin bound))
  408. nil)))
  409. ;; 2. Match in an `invalid' context: continue searching. Else,
  410. ;; return point.
  411. (unless (eq (org-list-context) 'invalid) (throw 'exit (point)))))))
  412. (defun org-list-search-backward (regexp &optional bound noerror)
  413. "Like `re-search-backward' but stop only where lists are recognized.
  414. Arguments REGEXP, BOUND and NOERROR are similar to those used in
  415. `re-search-backward'."
  416. (org-list-search-generic #'re-search-backward
  417. regexp (or bound (point-min)) noerror))
  418. (defun org-list-search-forward (regexp &optional bound noerror)
  419. "Like `re-search-forward' but stop only where lists are recognized.
  420. Arguments REGEXP, BOUND and NOERROR are similar to those used in
  421. `re-search-forward'."
  422. (org-list-search-generic #'re-search-forward
  423. regexp (or bound (point-max)) noerror))
  424. (defun org-list-at-regexp-after-bullet-p (regexp)
  425. "Is point at a list item with REGEXP after bullet?"
  426. (and (org-at-item-p)
  427. (save-excursion
  428. (goto-char (match-end 0))
  429. ;; Ignore counter if any
  430. (when (looking-at "\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?")
  431. (goto-char (match-end 0)))
  432. (looking-at regexp))))
  433. (defun org-list-separating-blank-lines-number (pos struct prevs)
  434. "Return number of blank lines that should separate items in list.
  435. POS is the position at item beginning to be considered. STRUCT is
  436. the list structure. PREVS is the alist of previous items. See
  437. `org-list-struct-prev-alist'.
  438. Assume point is at item's beginning. If the item is alone, apply
  439. some heuristics to guess the result."
  440. (save-excursion
  441. (let ((insert-blank-p
  442. (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  443. usr-blank)
  444. (cond
  445. ;; Trivial cases where there should be none.
  446. ((or (and (not (eq org-list-ending-method 'indent))
  447. org-empty-line-terminates-plain-lists)
  448. (not insert-blank-p)) 0)
  449. ;; When `org-blank-before-new-entry' says so, it is 1.
  450. ((eq insert-blank-p t) 1)
  451. ;; plain-list-item is 'auto. Count blank lines separating
  452. ;; neighbours items in list.
  453. (t (let ((next-p (org-list-get-next-item (point) struct prevs)))
  454. (cond
  455. ;; Is there a next item?
  456. (next-p (goto-char next-p)
  457. (org-back-over-empty-lines))
  458. ;; Is there a previous item?
  459. ((org-list-get-prev-item (point) struct prevs)
  460. (org-back-over-empty-lines))
  461. ;; User inserted blank lines, trust him
  462. ((and (> pos (org-list-get-item-end-before-blank pos struct))
  463. (> (save-excursion
  464. (goto-char pos)
  465. (skip-chars-backward " \t")
  466. (setq usr-blank (org-back-over-empty-lines))) 0))
  467. usr-blank)
  468. ;; Are there blank lines inside the item ?
  469. ((save-excursion
  470. (org-list-search-forward
  471. "^[ \t]*$" (org-list-get-item-end-before-blank pos struct) t))
  472. 1)
  473. ;; No parent: no blank line.
  474. (t 0))))))))
  475. (defun org-list-insert-item-generic (pos &optional checkbox after-bullet)
  476. "Insert a new list item at POS.
  477. If POS is before first character after bullet of the item, the
  478. new item will be created before the current one.
  479. Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
  480. after the bullet. Cursor will be after this text once the
  481. function ends."
  482. (let ((case-fold-search t))
  483. ;; 1. Get information about list: structure, usual helper
  484. ;; functions, position of point with regards to item start
  485. ;; (BEFOREP), blank lines number separating items (BLANK-NB),
  486. ;; position of split (POS) if we're allowed to (SPLIT-LINE-P).
  487. (let* ((pos (point))
  488. (item (goto-char (org-list-get-item-begin)))
  489. (struct (org-list-struct))
  490. (prevs (org-list-struct-prev-alist struct))
  491. (item-end (org-list-get-item-end item struct))
  492. (item-end-no-blank (org-list-get-item-end-before-blank item struct))
  493. (beforep (and (looking-at org-list-full-item-re)
  494. (<= pos (match-end 0))))
  495. (split-line-p (org-get-alist-option org-M-RET-may-split-line 'item))
  496. (blank-nb (org-list-separating-blank-lines-number
  497. item struct prevs))
  498. ;; 2. Build the new item to be created. Concatenate same
  499. ;; bullet as item, checkbox, text AFTER-BULLET if
  500. ;; provided, and text cut from point to end of item
  501. ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
  502. ;; BEFOREP and SPLIT-LINE-P. The difference of size
  503. ;; between what was cut and what was inserted in buffer
  504. ;; is stored in SIZE-OFFSET.
  505. (ind (org-list-get-ind item struct))
  506. (bullet (org-list-bullet-string (org-list-get-bullet item struct)))
  507. (box (when checkbox "[ ]"))
  508. (text-cut
  509. (and (not beforep) split-line-p
  510. (progn
  511. (goto-char pos)
  512. (skip-chars-backward " \r\t\n")
  513. (setq pos (point))
  514. (delete-and-extract-region pos item-end-no-blank))))
  515. (body (concat bullet (when box (concat box " ")) after-bullet
  516. (or (and text-cut
  517. (if (string-match "\\`[ \t]+" text-cut)
  518. (replace-match "" t t text-cut)
  519. text-cut))
  520. "")))
  521. (item-sep (make-string (1+ blank-nb) ?\n))
  522. (item-size (+ ind (length body) (length item-sep)))
  523. (size-offset (- item-size (length text-cut))))
  524. ;; 4. Insert effectively item into buffer
  525. (goto-char item)
  526. (org-indent-to-column ind)
  527. (insert body)
  528. (insert item-sep)
  529. ;; 5. Add new item to STRUCT.
  530. (mapc (lambda (e)
  531. (let ((p (car e))
  532. (end (nth 6 e)))
  533. (cond
  534. ;; Before inserted item, positions don't change but
  535. ;; an item ending after insertion has its end shifted
  536. ;; by SIZE-OFFSET.
  537. ((< p item)
  538. (when (> end item) (setcar (nthcdr 6 e) (+ end size-offset))))
  539. ;; Trivial cases where current item isn't split in
  540. ;; two. Just shift every item after new one by
  541. ;; ITEM-SIZE.
  542. ((or beforep (not split-line-p))
  543. (setcar e (+ p item-size))
  544. (setcar (nthcdr 6 e) (+ end item-size)))
  545. ;; Item is split in two: elements before POS are just
  546. ;; shifted by ITEM-SIZE. In the case item would end
  547. ;; after split POS, ending is only shifted by
  548. ;; SIZE-OFFSET.
  549. ((< p pos)
  550. (setcar e (+ p item-size))
  551. (if (< end pos)
  552. (setcar (nthcdr 6 e) (+ end item-size))
  553. (setcar (nthcdr 6 e) (+ end size-offset))))
  554. ;; Elements after POS are moved into new item. Length
  555. ;; of ITEM-SEP has to be removed as ITEM-SEP
  556. ;; doesn't appear in buffer yet.
  557. ((< p item-end)
  558. (setcar e (+ p size-offset (- item pos (length item-sep))))
  559. (if (= end item-end)
  560. (setcar (nthcdr 6 e) (+ item item-size))
  561. (setcar (nthcdr 6 e)
  562. (+ end size-offset
  563. (- item pos (length item-sep))))))
  564. ;; Elements at ITEM-END or after are only shifted by
  565. ;; SIZE-OFFSET.
  566. (t (setcar e (+ p size-offset))
  567. (setcar (nthcdr 6 e) (+ end size-offset))))))
  568. struct)
  569. (setq struct (sort
  570. (cons (list item ind bullet nil box nil (+ item item-size))
  571. struct)
  572. (lambda (e1 e2) (< (car e1) (car e2)))))
  573. ;; 6. If not BEFOREP, new item must appear after ITEM, so
  574. ;; exchange ITEM with the next item in list. Position cursor
  575. ;; after bullet, counter, checkbox, and label.
  576. (if beforep
  577. (goto-char item)
  578. (setq struct (org-list-exchange-items item (+ item item-size) struct))
  579. (goto-char (org-list-get-next-item
  580. item struct (org-list-struct-prev-alist struct))))
  581. (org-list-struct-fix-struct struct (org-list-struct-parent-alist struct))
  582. (when checkbox (org-update-checkbox-count-maybe))
  583. (looking-at org-list-full-item-re)
  584. (goto-char (match-end 0))
  585. t)))
  586. (defvar org-last-indent-begin-marker (make-marker))
  587. (defvar org-last-indent-end-marker (make-marker))
  588. (defun org-list-indent-item-generic (arg no-subtree struct)
  589. "Indent a local list item including its children.
  590. When number ARG is a negative, item will be outdented, otherwise
  591. it will be indented.
  592. If a region is active, all items inside will be moved.
  593. If NO-SUBTREE is non-nil, only indent the item itself, not its
  594. children.
  595. STRUCT is the list structure. Return t if successful."
  596. (save-excursion
  597. (beginning-of-line)
  598. (let* ((regionp (org-region-active-p))
  599. (rbeg (and regionp (region-beginning)))
  600. (rend (and regionp (region-end)))
  601. (top (org-list-get-top-point struct))
  602. (parents (org-list-struct-parent-alist struct))
  603. (prevs (org-list-struct-prev-alist struct))
  604. ;; Are we going to move the whole list?
  605. (specialp
  606. (and (= top (point))
  607. (cdr (assq 'indent org-list-automatic-rules))
  608. (if no-subtree
  609. (error
  610. "First item of list cannot move without its subtree")
  611. t))))
  612. ;; Determine begin and end points of zone to indent. If moving
  613. ;; more than one item, save them for subsequent moves.
  614. (unless (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  615. (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
  616. (if regionp
  617. (progn
  618. (set-marker org-last-indent-begin-marker rbeg)
  619. (set-marker org-last-indent-end-marker rend))
  620. (set-marker org-last-indent-begin-marker (point))
  621. (set-marker org-last-indent-end-marker
  622. (cond
  623. (specialp (org-list-get-bottom-point struct))
  624. (no-subtree (1+ (point)))
  625. (t (org-list-get-item-end (point) struct))))))
  626. (let* ((beg (marker-position org-last-indent-begin-marker))
  627. (end (marker-position org-last-indent-end-marker)))
  628. (cond
  629. ;; Special case: moving top-item with indent rule
  630. (specialp
  631. (let* ((level-skip (org-level-increment))
  632. (offset (if (< arg 0) (- level-skip) level-skip))
  633. (top-ind (org-list-get-ind beg struct))
  634. (old-struct (mapcar (lambda (e) (copy-alist e)) struct)))
  635. (if (< (+ top-ind offset) 0)
  636. (error "Cannot outdent beyond margin")
  637. ;; Change bullet if necessary
  638. (when (and (= (+ top-ind offset) 0)
  639. (string-match "*"
  640. (org-list-get-bullet beg struct)))
  641. (org-list-set-bullet beg struct
  642. (org-list-bullet-string "-")))
  643. ;; Shift every item by OFFSET and fix bullets. Then
  644. ;; apply changes to buffer.
  645. (mapc (lambda (e)
  646. (let ((ind (org-list-get-ind (car e) struct)))
  647. (org-list-set-ind (car e) struct (+ ind offset))))
  648. struct)
  649. (org-list-struct-fix-bul struct prevs)
  650. (org-list-struct-apply-struct struct old-struct))))
  651. ;; Forbidden move:
  652. ((and (< arg 0)
  653. ;; If only one item is moved, it mustn't have a child
  654. (or (and no-subtree
  655. (not regionp)
  656. (org-list-has-child-p beg struct))
  657. ;; If a subtree or region is moved, the last item
  658. ;; of the subtree mustn't have a child
  659. (let ((last-item (caar
  660. (reverse
  661. (org-remove-if
  662. (lambda (e) (>= (car e) end))
  663. struct)))))
  664. (org-list-has-child-p last-item struct))))
  665. (error "Cannot outdent an item without its children"))
  666. ;; Normal shifting
  667. (t
  668. (let* ((new-parents
  669. (if (< arg 0)
  670. (org-list-struct-outdent beg end struct parents)
  671. (org-list-struct-indent beg end struct parents prevs))))
  672. (org-list-struct-fix-struct struct new-parents))
  673. (org-update-checkbox-count-maybe))))))
  674. t)
  675. ;;; Predicates
  676. (defun org-in-item-p ()
  677. "Return item beginning position when in a plain list, nil otherwise.
  678. This checks `org-list-ending-method'."
  679. (save-excursion
  680. (beginning-of-line)
  681. (unless (or (let ((outline-regexp org-outline-regexp)) (org-at-heading-p))
  682. (and (not (eq org-list-ending-method 'indent))
  683. (looking-at org-list-end-re)
  684. (progn (forward-line -1) (looking-at org-list-end-re))))
  685. (or (and (org-at-item-p) (point-at-bol))
  686. (let* ((case-fold-search t)
  687. (context (org-list-context))
  688. (lim-up (car context))
  689. (inlinetask-re (and (featurep 'org-inlinetask)
  690. (org-inlinetask-outline-regexp)))
  691. (ind-ref (if (looking-at "^[ \t]*$")
  692. 10000
  693. (org-get-indentation))))
  694. (catch 'exit
  695. (while t
  696. (let ((ind (org-get-indentation)))
  697. (cond
  698. ((<= (point) lim-up)
  699. (throw 'exit (and (org-at-item-p) (< ind ind-ref) (point))))
  700. ((and (not (eq org-list-ending-method 'indent))
  701. (looking-at org-list-end-re))
  702. (throw 'exit nil))
  703. ;; Skip blocks, drawers, inline-tasks, blank lines
  704. ((looking-at "^[ \t]*#\\+end_")
  705. (re-search-backward "^[ \t]*#\\+begin_" nil t))
  706. ((looking-at "^[ \t]*:END:")
  707. (re-search-backward org-drawer-regexp nil t)
  708. (beginning-of-line))
  709. ((and inlinetask-re (looking-at inlinetask-re))
  710. (org-inlinetask-goto-beginning)
  711. (forward-line -1))
  712. ((looking-at "^[ \t]*$")
  713. (forward-line -1))
  714. ((< ind ind-ref)
  715. (if (org-at-item-p)
  716. (throw 'exit (point))
  717. (setq ind-ref ind)
  718. (forward-line -1)))
  719. (t (if (and (eq org-list-ending-method 'regexp)
  720. (org-at-item-p))
  721. (throw 'exit (point))
  722. (forward-line -1))))))))))))
  723. (defun org-at-item-p ()
  724. "Is point in a line starting a hand-formatted item?"
  725. (save-excursion (beginning-of-line) (looking-at (org-item-beginning-re))))
  726. (defun org-at-item-bullet-p ()
  727. "Is point at the bullet of a plain list item?"
  728. (and (org-at-item-p)
  729. (not (member (char-after) '(?\ ?\t)))
  730. (< (point) (match-end 0))))
  731. (defun org-at-item-timer-p ()
  732. "Is point at a line starting a plain list item with a timer?"
  733. (org-list-at-regexp-after-bullet-p
  734. "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
  735. (defun org-at-item-description-p ()
  736. "Is point at a description list item?"
  737. (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
  738. (defun org-at-item-checkbox-p ()
  739. "Is point at a line starting a plain-list item with a checklet?"
  740. (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
  741. ;;; Navigate
  742. (defalias 'org-list-get-item-begin 'org-in-item-p)
  743. (defun org-beginning-of-item ()
  744. "Go to the beginning of the current hand-formatted item.
  745. If the cursor is not in an item, throw an error."
  746. (interactive)
  747. (let ((begin (org-in-item-p)))
  748. (if begin (goto-char begin) (error "Not in an item"))))
  749. (defun org-beginning-of-item-list ()
  750. "Go to the beginning item of the current list or sublist.
  751. Return an error if not in a list."
  752. (interactive)
  753. (let ((begin (org-in-item-p)))
  754. (if (not begin)
  755. (error "Not in an item")
  756. (goto-char begin)
  757. (let* ((struct (org-list-struct))
  758. (prevs (org-list-struct-prev-alist struct)))
  759. (goto-char (org-list-get-list-begin begin struct prevs))))))
  760. (defun org-end-of-item-list ()
  761. "Go to the end of the current list or sublist.
  762. If the cursor in not in an item, throw an error."
  763. (interactive)
  764. (let ((begin (org-in-item-p)))
  765. (if (not begin)
  766. (error "Not in an item")
  767. (goto-char begin)
  768. (let* ((struct (org-list-struct))
  769. (prevs (org-list-struct-prev-alist struct)))
  770. (goto-char (org-list-get-list-end begin struct prevs))))))
  771. (defun org-end-of-item ()
  772. "Go to the end of the current hand-formatted item.
  773. If the cursor is not in an item, throw an error."
  774. (interactive)
  775. (let ((begin (org-in-item-p)))
  776. (if (not begin)
  777. (error "Not in an item")
  778. (goto-char begin)
  779. (let ((struct (org-list-struct)))
  780. (goto-char (org-list-get-item-end begin struct))))))
  781. (defun org-previous-item ()
  782. "Move to the beginning of the previous item.
  783. Item is at the same level in the current plain list. Error if not
  784. in a plain list, or if this is the first item in the list."
  785. (interactive)
  786. (let ((begin (org-in-item-p)))
  787. (if (not begin)
  788. (error "Not in an item")
  789. (goto-char begin)
  790. (let* ((struct (org-list-struct))
  791. (prevs (org-list-struct-prev-alist struct))
  792. (prevp (org-list-get-prev-item begin struct prevs)))
  793. (if prevp (goto-char prevp) (error "On first item"))))))
  794. (defun org-next-item ()
  795. "Move to the beginning of the next item.
  796. Item is at the same level in the current plain list. Error if not
  797. in a plain list, or if this is the last item in the list."
  798. (interactive)
  799. (let ((begin (org-in-item-p)))
  800. (if (not begin)
  801. (error "Not in an item")
  802. (goto-char begin)
  803. (let* ((struct (org-list-struct))
  804. (prevs (org-list-struct-prev-alist struct))
  805. (prevp (org-list-get-next-item begin struct prevs)))
  806. (if prevp (goto-char prevp) (error "On last item"))))))
  807. ;;; Manipulate
  808. (defun org-list-exchange-items (beg-A beg-B struct)
  809. "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
  810. Blank lines at the end of items are left in place. Return the new
  811. structure after the changes.
  812. Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
  813. to the same sub-list.
  814. This function modifies STRUCT."
  815. (save-excursion
  816. (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct))
  817. (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct))
  818. (end-A (org-list-get-item-end beg-A struct))
  819. (end-B (org-list-get-item-end beg-B struct))
  820. (size-A (- end-A-no-blank beg-A))
  821. (size-B (- end-B-no-blank beg-B))
  822. (body-A (buffer-substring beg-A end-A-no-blank))
  823. (body-B (buffer-substring beg-B end-B-no-blank))
  824. (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B))
  825. (sub-A (cons beg-A (org-list-get-subtree beg-A struct)))
  826. (sub-B (cons beg-B (org-list-get-subtree beg-B struct))))
  827. ;; 1. Move effectively items in buffer.
  828. (goto-char beg-A)
  829. (delete-region beg-A end-B-no-blank)
  830. (insert (concat body-B between-A-no-blank-and-B body-A))
  831. ;; 2. Now modify struct. No need to re-read the list, the
  832. ;; transformation is just a shift of positions. Some special
  833. ;; attention is required for items ending at END-A and END-B
  834. ;; as empty spaces are not moved there. In others words, item
  835. ;; BEG-A will end with whitespaces that were at the end of
  836. ;; BEG-B and the same applies to BEG-B.
  837. (mapc (lambda (e)
  838. (let ((pos (car e)))
  839. (cond
  840. ((< pos beg-A))
  841. ((memq pos sub-A)
  842. (let ((end-e (nth 6 e)))
  843. (setcar e (+ pos (- end-B-no-blank end-A-no-blank)))
  844. (setcar (nthcdr 6 e)
  845. (+ end-e (- end-B-no-blank end-A-no-blank)))
  846. (when (= end-e end-A) (setcar (nthcdr 6 e) end-B))))
  847. ((memq pos sub-B)
  848. (let ((end-e (nth 6 e)))
  849. (setcar e (- (+ pos beg-A) beg-B))
  850. (setcar (nthcdr 6 e) (+ end-e (- beg-A beg-B)))
  851. (when (= end-e end-B)
  852. (setcar (nthcdr 6 e)
  853. (+ beg-A size-B (- end-A end-A-no-blank))))))
  854. ((< pos beg-B)
  855. (let ((end-e (nth 6 e)))
  856. (setcar e (+ pos (- size-B size-A)))
  857. (setcar (nthcdr 6 e) (+ end-e (- size-B size-A))))))))
  858. struct)
  859. (sort struct (lambda (e1 e2) (< (car e1) (car e2)))))))
  860. (defun org-move-item-down ()
  861. "Move the plain list item at point down, i.e. swap with following item.
  862. Subitems (items with larger indentation) are considered part of the item,
  863. so this really moves item trees."
  864. (interactive)
  865. (unless (org-at-item-p) (error "Not at an item"))
  866. (let* ((pos (point))
  867. (col (current-column))
  868. (actual-item (point-at-bol))
  869. (struct (org-list-struct))
  870. (prevs (org-list-struct-prev-alist struct))
  871. (next-item (org-list-get-next-item (point-at-bol) struct prevs)))
  872. (if (not next-item)
  873. (progn
  874. (goto-char pos)
  875. (error "Cannot move this item further down"))
  876. (setq struct
  877. (org-list-exchange-items actual-item next-item struct))
  878. ;; Use a short variation of `org-list-struct-fix-struct' as
  879. ;; there's no need to go through all the steps.
  880. (let ((old-struct (mapcar (lambda (e) (copy-alist e)) struct))
  881. (prevs (org-list-struct-prev-alist struct))
  882. (parents (org-list-struct-parent-alist struct)))
  883. (org-list-struct-fix-bul struct prevs)
  884. (org-list-struct-fix-ind struct parents)
  885. (org-list-struct-apply-struct struct old-struct)
  886. (goto-char (org-list-get-next-item (point-at-bol) struct prevs)))
  887. (org-move-to-column col))))
  888. (defun org-move-item-up ()
  889. "Move the plain list item at point up, i.e. swap with previous item.
  890. Subitems (items with larger indentation) are considered part of the item,
  891. so this really moves item trees."
  892. (interactive)
  893. (unless (org-at-item-p) (error "Not at an item"))
  894. (let* ((pos (point))
  895. (col (current-column))
  896. (actual-item (point-at-bol))
  897. (struct (org-list-struct))
  898. (prevs (org-list-struct-prev-alist struct))
  899. (prev-item (org-list-get-prev-item (point-at-bol) struct prevs)))
  900. (if (not prev-item)
  901. (progn
  902. (goto-char pos)
  903. (error "Cannot move this item further up"))
  904. (setq struct
  905. (org-list-exchange-items prev-item actual-item struct))
  906. ;; Use a short variation of `org-list-struct-fix-struct' as
  907. ;; there's no need to go through all the steps.
  908. (let ((old-struct (mapcar (lambda (e) (copy-alist e)) struct))
  909. (prevs (org-list-struct-prev-alist struct))
  910. (parents (org-list-struct-parent-alist struct)))
  911. (org-list-struct-fix-bul struct prevs)
  912. (org-list-struct-fix-ind struct parents)
  913. (org-list-struct-apply-struct struct old-struct))
  914. (org-move-to-column col))))
  915. (defun org-insert-item (&optional checkbox)
  916. "Insert a new item at the current level.
  917. If cursor is before first character after bullet of the item, the
  918. new item will be created before the current one.
  919. If CHECKBOX is non-nil, add a checkbox next to the bullet.
  920. Return t when things worked, nil when we are not in an item, or
  921. item is invisible."
  922. (let ((itemp (org-in-item-p)))
  923. (unless (or (not itemp)
  924. (save-excursion
  925. (goto-char itemp)
  926. (org-invisible-p)))
  927. (if (save-excursion
  928. (goto-char itemp)
  929. (org-at-item-timer-p))
  930. ;; Timer list: delegate to `org-timer-item'.
  931. (progn (org-timer-item) t)
  932. ;; if we're in a description list, ask for the new term.
  933. (let ((desc-text (when (save-excursion
  934. (and (goto-char itemp)
  935. (org-at-item-description-p)))
  936. (concat (read-string "Term: ") " :: "))))
  937. ;; Don't insert a checkbox if checkbox rule is applied and it
  938. ;; is a description item.
  939. (org-list-insert-item-generic
  940. (point) (and checkbox
  941. (or (not desc-text)
  942. (not (cdr (assq 'checkbox org-list-automatic-rules)))))
  943. desc-text))))))
  944. ;;; Structures
  945. (defun org-list-struct ()
  946. "Return structure of list at point.
  947. A list structure is an alist where keys is point at item, and
  948. values are:
  949. 1. indentation,
  950. 2. bullet with trailing whitespace,
  951. 3. bullet counter, if any,
  952. 4. checkbox, if any,
  953. 5. position at item end,
  954. 6. description tag, if any.
  955. Thus the following list, where numbers in parens are
  956. point-at-bol:
  957. - [X] first item (1)
  958. 1. sub-item 1 (18)
  959. 5. [@5] sub-item 2 (34)
  960. some other text belonging to first item (55)
  961. - last item (97)
  962. + tag :: description (109)
  963. (131)
  964. will get the following structure:
  965. \(\(1 0 \"- \" nil [X] nil 97)
  966. \(18 2 \"1. \" nil nil nil 34\)
  967. \(34 2 \"5. \" \"5\" nil nil 55\)
  968. \(97 0 \"- \" nil nil nil 131\)
  969. \(109 2 \"+ \" nil nil \"tag\" 131\)
  970. Assume point is at an item."
  971. (save-excursion
  972. (beginning-of-line)
  973. (let* ((case-fold-search t)
  974. (context (org-list-context))
  975. (lim-up (car context))
  976. (lim-down (nth 1 context))
  977. (text-min-ind 10000)
  978. (drawers-re (concat "^[ \t]*:\\("
  979. (mapconcat 'regexp-quote org-drawers "\\|")
  980. "\\):[ \t]*$"))
  981. (inlinetask-re (and (featurep 'org-inlinetask)
  982. (org-inlinetask-outline-regexp)))
  983. (beg-cell (cons (point) (org-get-indentation)))
  984. ind itm-lst itm-lst-2 end-lst end-lst-2 struct
  985. (assoc-at-point
  986. ;; Return an association whose key is point and values are
  987. ;; indentation, bullet string, bullet counter, and
  988. ;; checkbox.
  989. (function
  990. (lambda (ind)
  991. (looking-at org-list-full-item-re)
  992. (list (point)
  993. ind
  994. (match-string-no-properties 1) ; bullet
  995. (match-string-no-properties 2) ; counter
  996. (match-string-no-properties 3) ; checkbox
  997. (match-string-no-properties 4))))) ; description tag
  998. (end-before-blank
  999. ;; Ensure list ends at the first blank line.
  1000. (function
  1001. (lambda ()
  1002. (skip-chars-backward " \r\t\n")
  1003. (min (1+ (point-at-eol)) lim-down)))))
  1004. ;; 1. Read list from starting item to its beginning, and save
  1005. ;; top item position and indentation in BEG-CELL. Also store
  1006. ;; ending position of items in END-LST.
  1007. (save-excursion
  1008. (catch 'exit
  1009. (while t
  1010. (let ((ind (+ (or (get-text-property (point) 'original-indentation) 0)
  1011. (org-get-indentation))))
  1012. (cond
  1013. ((<= (point) lim-up)
  1014. ;; At upward limit: if we ended at an item, store it,
  1015. ;; else dimiss useless data recorded above BEG-CELL.
  1016. ;; Jump to part 2.
  1017. (throw 'exit
  1018. (setq itm-lst
  1019. (if (not (org-at-item-p))
  1020. (memq (assq (car beg-cell) itm-lst) itm-lst)
  1021. (setq beg-cell (cons (point) ind))
  1022. (cons (funcall assoc-at-point ind) itm-lst)))))
  1023. ((and (not (eq org-list-ending-method 'indent))
  1024. (looking-at org-list-end-re))
  1025. ;; Looking at a list ending regexp. Dismiss useless
  1026. ;; data recorded above BEG-CELL. Jump to part 2.
  1027. (throw 'exit
  1028. (setq itm-lst
  1029. (memq (assq (car beg-cell) itm-lst) itm-lst))))
  1030. ;; Skip blocks, drawers, inline tasks, blank lines
  1031. ;; along the way.
  1032. ((looking-at "^[ \t]*#\\+end_")
  1033. (re-search-backward "^[ \t]*#\\+begin_" nil t))
  1034. ((looking-at "^[ \t]*:END:")
  1035. (re-search-backward drawers-re nil t)
  1036. (beginning-of-line))
  1037. ((and inlinetask-re (looking-at inlinetask-re))
  1038. (org-inlinetask-goto-beginning)
  1039. (forward-line -1))
  1040. ((looking-at "^[ \t]*$")
  1041. (forward-line -1))
  1042. ((org-at-item-p)
  1043. ;; Point is at an item. Add data to ITM-LST. It may
  1044. ;; also end a previous item: save it in END-LST. If
  1045. ;; ind is less or equal than BEG-CELL and there is no
  1046. ;; end at this ind or lesser, this item becomes the
  1047. ;; new BEG-CELL.
  1048. (push (funcall assoc-at-point ind) itm-lst)
  1049. (push (cons ind (point-at-bol)) end-lst)
  1050. (when (or (and (eq org-list-ending-method 'regexp)
  1051. (<= ind (cdr beg-cell)))
  1052. (< ind text-min-ind))
  1053. (setq beg-cell (cons (point-at-bol) ind)))
  1054. (forward-line -1))
  1055. (t
  1056. ;; Point is not at an item. Unless ending method is
  1057. ;; `regexp', interpret line's indentation:
  1058. ;;
  1059. ;; - text at column 0 is necessarily out of any list.
  1060. ;; Dismiss data recorded above BEG-CELL. Jump to
  1061. ;; part 2.
  1062. ;;
  1063. ;; - any other case, it can possibly be an ending
  1064. ;; position for an item above. Save it and proceed.
  1065. (cond
  1066. ((eq org-list-ending-method 'regexp))
  1067. ((= ind 0)
  1068. (throw 'exit
  1069. (setq itm-lst
  1070. (memq (assq (car beg-cell) itm-lst) itm-lst))))
  1071. (t
  1072. (when (< ind text-min-ind) (setq text-min-ind ind))
  1073. (push (cons ind (point-at-bol)) end-lst)))
  1074. (forward-line -1)))))))
  1075. ;; 2. Read list from starting point to its end, that is until we
  1076. ;; get out of context, or a non-item line is less or equally
  1077. ;; indented that BEG-CELL's cdr. Also store ending position
  1078. ;; of items in END-LST-2.
  1079. (catch 'exit
  1080. (while t
  1081. (let ((ind (+ (or (get-text-property (point) 'original-indentation) 0)
  1082. (org-get-indentation))))
  1083. (cond
  1084. ((>= (point) lim-down)
  1085. ;; At downward limit: this is de facto the end of the
  1086. ;; list. Save point as an ending position, and jump to
  1087. ;; part 3.
  1088. (throw 'exit
  1089. (push (cons 0 (funcall end-before-blank)) end-lst-2)))
  1090. ((and (not (eq org-list-ending-method 'indent))
  1091. (looking-at org-list-end-re))
  1092. ;; Looking at a list ending regexp. Save point as an
  1093. ;; ending position and jump to part 3.
  1094. (throw 'exit (push (cons 0 (point-at-bol)) end-lst-2)))
  1095. ;; Skip blocks, drawers, inline tasks and blank lines
  1096. ;; along the way
  1097. ((looking-at "^[ \t]*#\\+begin_")
  1098. (re-search-forward "^[ \t]*#\\+end_")
  1099. (forward-line 1))
  1100. ((looking-at drawers-re)
  1101. (re-search-forward "^[ \t]*:END:" nil t)
  1102. (forward-line 1))
  1103. ((and inlinetask-re (looking-at inlinetask-re))
  1104. (org-inlinetask-goto-end))
  1105. ((looking-at "^[ \t]*$")
  1106. (forward-line 1))
  1107. ((org-at-item-p)
  1108. ;; Point is at an item. Add data to ITM-LST-2. It may also
  1109. ;; end a previous item, so save it in END-LST-2.
  1110. (push (funcall assoc-at-point ind) itm-lst-2)
  1111. (push (cons ind (point-at-bol)) end-lst-2)
  1112. (forward-line 1))
  1113. (t
  1114. ;; Point is not at an item. If ending method is not
  1115. ;; `regexp', two situations are of interest:
  1116. ;;
  1117. ;; - ind is lesser or equal than BEG-CELL's. The list is
  1118. ;; over. Store point as an ending position and jump to
  1119. ;; part 3.
  1120. ;;
  1121. ;; - ind is lesser or equal than previous item's. This
  1122. ;; is an ending position. Store it and proceed.
  1123. (cond
  1124. ((eq org-list-ending-method 'regexp))
  1125. ((<= ind (cdr beg-cell))
  1126. (push (cons ind (funcall end-before-blank)) end-lst-2)
  1127. (throw 'exit nil))
  1128. ((<= ind (nth 1 (car itm-lst-2)))
  1129. (push (cons ind (point-at-bol)) end-lst-2)))
  1130. (forward-line 1))))))
  1131. (setq struct (append itm-lst (cdr (nreverse itm-lst-2))))
  1132. (setq end-lst (append end-lst (cdr (nreverse end-lst-2))))
  1133. ;; 3. Correct ill-formed lists by making sure top item has the
  1134. ;; least indentation of the list
  1135. (let ((min-ind (nth 1 (car struct))))
  1136. (mapc (lambda (item)
  1137. (let ((ind (nth 1 item)))
  1138. (when (< ind min-ind) (setcar (cdr item) min-ind))))
  1139. struct))
  1140. ;; 4. Associate each item to its end pos.
  1141. (org-list-struct-assoc-end struct end-lst)
  1142. ;; 5. Return STRUCT
  1143. struct)))
  1144. (defun org-list-struct-assoc-end (struct end-list)
  1145. "Associate proper ending point to items in STRUCT.
  1146. END-LIST is a pseudo-alist where car is indentation and cdr is
  1147. ending position.
  1148. This function modifies STRUCT."
  1149. (let ((endings end-list))
  1150. (mapc
  1151. (lambda (elt)
  1152. (let ((pos (car elt))
  1153. (ind (nth 1 elt)))
  1154. ;; remove end candidates behind current item
  1155. (while (or (<= (cdar endings) pos))
  1156. (pop endings))
  1157. ;; add end position to item assoc
  1158. (let ((old-end (nthcdr 6 elt))
  1159. (new-end (assoc-default ind endings '<=)))
  1160. (if old-end
  1161. (setcar old-end new-end)
  1162. (setcdr elt (append (cdr elt) (list new-end)))))))
  1163. struct)))
  1164. (defun org-list-struct-prev-alist (struct)
  1165. "Return alist between item and previous item in STRUCT."
  1166. (let ((item-end-alist (mapcar (lambda (e) (cons (car e) (nth 6 e)))
  1167. struct)))
  1168. (mapcar (lambda (e)
  1169. (let ((prev (car (rassq (car e) item-end-alist))))
  1170. (cons (car e) prev)))
  1171. struct)))
  1172. (defun org-list-struct-parent-alist (struct)
  1173. "Return alist between item and parent in STRUCT."
  1174. (let ((ind-to-ori (list (list (nth 1 (car struct)))))
  1175. (prev-pos (list (caar struct))))
  1176. (cons prev-pos
  1177. (mapcar (lambda (item)
  1178. (let ((pos (car item))
  1179. (ind (nth 1 item))
  1180. (prev-ind (caar ind-to-ori)))
  1181. (push pos prev-pos)
  1182. (cond
  1183. ((> prev-ind ind)
  1184. (setq ind-to-ori
  1185. (member (assq ind ind-to-ori) ind-to-ori))
  1186. (cons pos (cdar ind-to-ori)))
  1187. ((< prev-ind ind)
  1188. (let ((origin (nth 1 prev-pos)))
  1189. (push (cons ind origin) ind-to-ori)
  1190. (cons pos origin)))
  1191. (t (cons pos (cdar ind-to-ori))))))
  1192. (cdr struct)))))
  1193. (defun org-list-get-parent (item struct parents)
  1194. "Return parent of ITEM in STRUCT, or nil.
  1195. PARENTS is the alist of items' parent. See
  1196. `org-list-struct-parent-alist'."
  1197. (let ((parents (or parents (org-list-struct-parent-alist struct))))
  1198. (cdr (assq item parents))))
  1199. (defun org-list-has-child-p (item struct)
  1200. "Return a non-nil value if ITEM in STRUCT has a child.
  1201. Value returned is the position of the first child of ITEM."
  1202. (let ((ind (org-list-get-ind item struct))
  1203. (child-maybe (car (nth 1 (member (assq item struct) struct)))))
  1204. (when (and child-maybe
  1205. (< ind (org-list-get-ind child-maybe struct)))
  1206. child-maybe)))
  1207. (defun org-list-get-next-item (item struct prevs)
  1208. "Return next item in same sub-list as ITEM in STRUCT, or nil.
  1209. PREVS is the alist of previous items. See
  1210. `org-list-struct-prev-alist'."
  1211. (car (rassq item prevs)))
  1212. (defun org-list-get-prev-item (item struct prevs)
  1213. "Return previous item in same sub-list as ITEM in STRUCT, or nil.
  1214. PREVS is the alist of previous items. See
  1215. `org-list-struct-prev-alist'."
  1216. (cdr (assq item prevs)))
  1217. (defun org-list-get-subtree (item struct)
  1218. "Return all items with ITEM as a common ancestor or nil.
  1219. STRUCT is the list structure considered."
  1220. (let* ((item-end (org-list-get-item-end item struct))
  1221. (sub-struct (cdr (member (assq item struct) struct)))
  1222. subtree)
  1223. (catch 'exit
  1224. (mapc (lambda (e)
  1225. (let ((pos (car e)))
  1226. (if (< pos item-end) (push pos subtree) (throw 'exit nil))))
  1227. sub-struct))
  1228. (nreverse subtree)))
  1229. (defun org-list-get-all-items (item struct prevs)
  1230. "List of items in the same sub-list as ITEM in STRUCT.
  1231. PREVS, when provided, is the alist of previous items. See
  1232. `org-list-struct-prev-alist'."
  1233. (let ((prev-item item)
  1234. (next-item item)
  1235. before-item after-item)
  1236. (while (setq prev-item (org-list-get-prev-item prev-item struct prevs))
  1237. (push prev-item before-item))
  1238. (while (setq next-item (org-list-get-next-item next-item struct prevs))
  1239. (push next-item after-item))
  1240. (append before-item (list item) (nreverse after-item))))
  1241. (defun org-list-get-children (item struct parents)
  1242. "List all children of ITEM in STRUCT, or nil.
  1243. PARENTS is the alist of items' parent. See
  1244. `org-list-struct-parent-alist'."
  1245. (let (all)
  1246. (while (setq child (car (rassq item parents)))
  1247. (setq parents (cdr (member (assq child parents) parents)))
  1248. (push child all))
  1249. (nreverse all)))
  1250. (defun org-list-get-top-point (struct)
  1251. "Return point at beginning of list.
  1252. STRUCT is the structure of the list."
  1253. (caar struct))
  1254. (defun org-list-get-bottom-point (struct)
  1255. "Return point at bottom of list.
  1256. STRUCT is the structure of the list."
  1257. (apply 'max
  1258. (mapcar (lambda (e) (org-list-get-item-end (car e) struct)) struct)))
  1259. (defun org-list-get-list-begin (item struct prevs)
  1260. "Return point at beginning of sub-list ITEM belongs.
  1261. STRUCT is the structure of the list. PREVS is the alist of
  1262. previous items. See `org-list-struct-prev-alist'."
  1263. (let ((first-item item) prev-item)
  1264. (while (setq prev-item (org-list-get-prev-item first-item struct prevs))
  1265. (setq first-item prev-item))
  1266. first-item))
  1267. (defalias 'org-list-get-first-item 'org-list-get-list-begin)
  1268. (defun org-list-get-last-item (item struct prevs)
  1269. "Return point at last item of sub-list ITEM belongs.
  1270. STRUCT is the structure of the list. PREVS is the alist of
  1271. previous items. See `org-list-struct-prev-alist'."
  1272. (let ((last-item item) next-item)
  1273. (while (setq next-item (org-list-get-next-item last-item struct prevs))
  1274. (setq last-item next-item))
  1275. last-item))
  1276. (defun org-list-get-list-end (item struct prevs)
  1277. "Return point at end of sub-list ITEM belongs.
  1278. STRUCT is the structure of the list. PREVS is the alist of
  1279. previous items. See `org-list-struct-prev-alist'."
  1280. (org-list-get-item-end (org-list-get-last-item item struct prevs) struct))
  1281. (defun org-list-get-list-type (item struct prevs)
  1282. "Return the type of the list containing ITEM as a symbol.
  1283. STRUCT is the structure of the list, as returned by
  1284. `org-list-struct'. PREVS is the alist of previous items. See
  1285. `org-list-struct-prev-alist'.
  1286. Possible types are `descriptive', `ordered' and `unordered'. The
  1287. type is determined by the first item of the list."
  1288. (let ((first (org-list-get-list-begin item struct prevs)))
  1289. (cond
  1290. ((org-list-get-tag first struct) 'descriptive)
  1291. ((string-match "[[:alnum:]]" (org-list-get-bullet first struct)) 'ordered)
  1292. (t 'unordered))))
  1293. (defun org-list-get-nth (n key struct)
  1294. "Return the Nth value of KEY in STRUCT."
  1295. (nth n (assq key struct)))
  1296. (defun org-list-set-nth (n key struct new)
  1297. "Set the Nth value of KEY in STRUCT to NEW.
  1298. \nThis function modifies STRUCT."
  1299. (setcar (nthcdr n (assq key struct)) new))
  1300. (defun org-list-get-ind (item struct)
  1301. "Return indentation of ITEM in STRUCT."
  1302. (org-list-get-nth 1 item struct))
  1303. (defun org-list-set-ind (item struct ind)
  1304. "Set indentation of ITEM in STRUCT to IND.
  1305. \nThis function modifies STRUCT."
  1306. (org-list-set-nth 1 item struct ind))
  1307. (defun org-list-get-bullet (item struct)
  1308. "Return bullet of ITEM in STRUCT."
  1309. (org-list-get-nth 2 item struct))
  1310. (defun org-list-set-bullet (item struct bullet)
  1311. "Set bullet of ITEM in STRUCT to BULLET.
  1312. \nThis function modifies STRUCT."
  1313. (org-list-set-nth 2 item struct bullet))
  1314. (defun org-list-get-counter (item struct)
  1315. "Return counter of ITEM in STRUCT."
  1316. (org-list-get-nth 3 item struct))
  1317. (defun org-list-get-checkbox (item struct)
  1318. "Return checkbox of ITEM in STRUCT or nil."
  1319. (org-list-get-nth 4 item struct))
  1320. (defun org-list-set-checkbox (item struct checkbox)
  1321. "Set checkbox of ITEM in STRUCT to CHECKBOX.
  1322. \nThis function modifies STRUCT."
  1323. (org-list-set-nth 4 item struct checkbox))
  1324. (defun org-list-get-tag (item struct)
  1325. "Return end position of ITEM in STRUCT."
  1326. (org-list-get-nth 5 item struct))
  1327. (defun org-list-get-item-end (item struct)
  1328. "Return end position of ITEM in STRUCT."
  1329. (org-list-get-nth 6 item struct))
  1330. (defun org-list-get-item-end-before-blank (item struct)
  1331. "Return point at end of ITEM in STRUCT, before any blank line.
  1332. Point returned is at end of line."
  1333. (save-excursion
  1334. (goto-char (org-list-get-item-end item struct))
  1335. (skip-chars-backward " \r\t\n")
  1336. (point-at-eol)))
  1337. (defun org-list-struct-fix-bul (struct prevs)
  1338. "Verify and correct bullets for every association in STRUCT.
  1339. PREVS is the alist of previous items. See
  1340. `org-list-struct-prev-alist'.
  1341. This function modifies STRUCT."
  1342. (let ((case-fold-search nil)
  1343. (fix-bul
  1344. (function
  1345. ;; Set bullet of ITEM in STRUCT, depending on the type of
  1346. ;; first item of the list, the previous bullet and counter
  1347. ;; if any.
  1348. (lambda (item)
  1349. (let* ((prev (org-list-get-prev-item item struct prevs))
  1350. (prev-bul (and prev (org-list-get-bullet prev struct)))
  1351. (counter (org-list-get-counter item struct))
  1352. (bullet (org-list-get-bullet item struct)))
  1353. (org-list-set-bullet
  1354. item struct
  1355. (org-list-bullet-string
  1356. (cond
  1357. ;; Alpha counter in alpha list: use counter.
  1358. ((and prev counter
  1359. (string-match "[a-zA-Z]" counter)
  1360. (string-match "[a-zA-Z]" prev-bul))
  1361. ;; Use cond to be sure `string-match' is used in
  1362. ;; both cases.
  1363. (let ((real-count
  1364. (cond
  1365. ((string-match "[a-z]" prev-bul) (downcase counter))
  1366. ((string-match "[A-Z]" prev-bul) (upcase counter)))))
  1367. (replace-match real-count nil nil prev-bul)))
  1368. ;; Num counter in a num list: use counter.
  1369. ((and prev counter
  1370. (string-match "[0-9]+" counter)
  1371. (string-match "[0-9]+" prev-bul))
  1372. (replace-match counter nil nil prev-bul))
  1373. ;; No counter: increase, if needed, previous bullet.
  1374. (prev
  1375. (org-list-inc-bullet-maybe (org-list-get-bullet prev struct)))
  1376. ;; Alpha counter at first item: use counter.
  1377. ((and counter (org-list-use-alpha-bul-p item struct prevs)
  1378. (string-match "[A-Za-z]" counter)
  1379. (string-match "[A-Za-z]" bullet))
  1380. (let ((real-count
  1381. (cond
  1382. ((string-match "[a-z]" bullet) (downcase counter))
  1383. ((string-match "[A-Z]" bullet) (upcase counter)))))
  1384. (replace-match real-count nil nil bullet)))
  1385. ;; Num counter at first item: use counter.
  1386. ((and counter
  1387. (string-match "[0-9]+" counter)
  1388. (string-match "[0-9]+" bullet))
  1389. (replace-match counter nil nil bullet))
  1390. ;; First bullet is alpha uppercase: use "A".
  1391. ((and (org-list-use-alpha-bul-p item struct prevs)
  1392. (string-match "[A-Z]" bullet))
  1393. (replace-match "A" nil nil bullet))
  1394. ;; First bullet is alpha lowercase: use "a".
  1395. ((and (org-list-use-alpha-bul-p item struct prevs)
  1396. (string-match "[a-z]" bullet))
  1397. (replace-match "a" nil nil bullet))
  1398. ;; First bullet is num: use "1".
  1399. ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet)
  1400. (replace-match "1" nil nil bullet))
  1401. ;; Not an ordered list: keep bullet.
  1402. (t bullet)))))))))
  1403. (mapc fix-bul (mapcar 'car struct))))
  1404. (defun org-list-struct-fix-ind (struct parents &optional bullet-size)
  1405. "Verify and correct indentation for every association in STRUCT.
  1406. PARENTS is the alist of items' parents. See
  1407. `org-list-struct-parent-alist'.
  1408. If numeric optional argument BULLET-SIZE is set, assume all
  1409. bullets in list have this length to determine new indentation.
  1410. This function modifies STRUCT."
  1411. (let* ((ancestor (org-list-get-top-point struct))
  1412. (top-ind (org-list-get-ind ancestor struct))
  1413. (new-ind
  1414. (lambda (item)
  1415. (let ((parent (org-list-get-parent item struct parents)))
  1416. (if parent
  1417. ;; Indent like parent + length of parent's bullet
  1418. (org-list-set-ind
  1419. item struct (+ (or bullet-size
  1420. (length
  1421. (org-list-get-bullet parent struct)))
  1422. (org-list-get-ind parent struct)))
  1423. ;; If no parent, indent like top-point
  1424. (org-list-set-ind item struct top-ind))))))
  1425. (mapc new-ind (mapcar 'car (cdr struct)))))
  1426. (defun org-list-struct-fix-box (struct parents prevs &optional ordered)
  1427. "Verify and correct checkboxes for every association in STRUCT.
  1428. PARENTS is the alist of items' parents. See
  1429. `org-list-struct-parent-alist'. PREVS is the alist of previous
  1430. items. See `org-list-struct-prev-alist.
  1431. If ORDERED is non-nil, a checkbox can only be checked when every
  1432. checkbox before it is checked too. If there was an attempt to
  1433. break this rule, the function will return the blocking item. In
  1434. all others cases, the return value will be nil.
  1435. This function modifies STRUCT."
  1436. (let ((all-items (mapcar 'car struct))
  1437. (set-parent-box
  1438. (function
  1439. (lambda (item)
  1440. (let* ((box-list
  1441. (mapcar (lambda (child)
  1442. (org-list-get-checkbox child struct))
  1443. (org-list-get-children item struct parents))))
  1444. (org-list-set-checkbox
  1445. item struct
  1446. (cond
  1447. ((and (member "[ ]" box-list) (member "[X]" box-list)) "[-]")
  1448. ((member "[-]" box-list) "[-]")
  1449. ((member "[X]" box-list) "[X]")
  1450. ((member "[ ]" box-list) "[ ]")
  1451. ;; parent has no boxed child: leave box as-is
  1452. (t (org-list-get-checkbox item struct))))))))
  1453. parent-list)
  1454. ;; 1. List all parents with a checkbox
  1455. (mapc
  1456. (lambda (e)
  1457. (let* ((parent (org-list-get-parent e struct parents))
  1458. (parent-box-p (org-list-get-checkbox parent struct)))
  1459. (when (and parent-box-p (not (memq parent parent-list)))
  1460. (push parent parent-list))))
  1461. all-items)
  1462. ;; 2. Sort those parents by decreasing indentation
  1463. (setq parent-list (sort parent-list
  1464. (lambda (e1 e2)
  1465. (> (org-list-get-ind e1 struct)
  1466. (org-list-get-ind e2 struct)))))
  1467. ;; 3. For each parent, get all children's checkboxes to determine
  1468. ;; and set its checkbox accordingly
  1469. (mapc set-parent-box parent-list)
  1470. ;; 4. If ORDERED is set, see if we need to uncheck some boxes
  1471. (when ordered
  1472. (let* ((box-list
  1473. (mapcar (lambda (e) (org-list-get-checkbox e struct)) all-items))
  1474. (after-unchecked (member "[ ]" box-list)))
  1475. ;; there are boxes checked after an unchecked one: fix that
  1476. (when (member "[X]" after-unchecked)
  1477. (let ((index (- (length struct) (length after-unchecked))))
  1478. (mapc (lambda (e) (org-list-set-checkbox e struct "[ ]"))
  1479. (nthcdr index all-items))
  1480. ;; Verify once again the structure, without ORDERED
  1481. (org-list-struct-fix-box struct parents prevs nil)
  1482. ;; return blocking item
  1483. (nth index all-items)))))))
  1484. (defun org-list-struct-fix-struct (struct parents)
  1485. "Return STRUCT with correct bullets and indentation.
  1486. PARENTS is the alist of items' parents. See
  1487. `org-list-struct-parent-alist'."
  1488. ;; Order of functions matters here: checkboxes and endings need
  1489. ;; correct indentation to be set, and indentation needs correct
  1490. ;; bullets.
  1491. ;;
  1492. ;; 0. Save a copy of structure before modifications
  1493. (let ((old-struct (mapcar (lambda (e) (copy-alist e)) struct)))
  1494. ;; 1. Set a temporary, but coherent with PARENTS, indentation in
  1495. ;; order to get items endings and bullets properly
  1496. (org-list-struct-fix-ind struct parents 2)
  1497. ;; 2. Get pseudo-alist of ending positions and sort it by position.
  1498. ;; Then associate them to the structure.
  1499. (let (end-list acc-end)
  1500. (mapc (lambda (e)
  1501. (let* ((pos (car e))
  1502. (ind-pos (org-list-get-ind pos struct))
  1503. (end-pos (org-list-get-item-end pos struct)))
  1504. (unless (assq end-pos struct)
  1505. ;; to determine real ind of an ending position that is
  1506. ;; not at an item, we have to find the item it belongs
  1507. ;; to: it is the last item (ITEM-UP), whose ending is
  1508. ;; further than the position we're interested in.
  1509. (let ((item-up (assoc-default end-pos acc-end '>)))
  1510. (push (cons
  1511. ;; else part is for the bottom point
  1512. (if item-up (+ (org-list-get-ind item-up struct) 2) 0)
  1513. end-pos)
  1514. end-list)))
  1515. (push (cons ind-pos pos) end-list)
  1516. (push (cons end-pos pos) acc-end)))
  1517. struct)
  1518. (setq end-list (sort end-list (lambda (e1 e2) (< (cdr e1) (cdr e2)))))
  1519. (org-list-struct-assoc-end struct end-list))
  1520. ;; 3. Get bullets right
  1521. (let ((prevs (org-list-struct-prev-alist struct)))
  1522. (org-list-struct-fix-bul struct prevs)
  1523. ;; 4. Now get real indentation
  1524. (org-list-struct-fix-ind struct parents)
  1525. ;; 5. Eventually fix checkboxes
  1526. (org-list-struct-fix-box struct parents prevs))
  1527. ;; 6. Apply structure modifications to buffer
  1528. (org-list-struct-apply-struct struct old-struct)))
  1529. (defun org-list-struct-outdent (start end struct parents)
  1530. "Outdent items between START and END in structure STRUCT.
  1531. PARENTS is the alist of items' parents. See
  1532. `org-list-struct-parent-alist'.
  1533. START is included, END excluded."
  1534. (let* (acc
  1535. (out (lambda (cell)
  1536. (let* ((item (car cell))
  1537. (parent (cdr cell)))
  1538. (cond
  1539. ;; Item not yet in zone: keep association
  1540. ((< item start) cell)
  1541. ;; Item out of zone: follow associations in acc
  1542. ((>= item end)
  1543. (let ((convert (and parent (assq parent acc))))
  1544. (if convert (cons item (cdr convert)) cell)))
  1545. ;; Item has no parent: error
  1546. ((not parent)
  1547. (error "Cannot outdent top-level items"))
  1548. ;; Parent is outdented: keep association
  1549. ((>= parent start)
  1550. (push (cons parent item) acc) cell)
  1551. (t
  1552. ;; Parent isn't outdented: reparent to grand-parent
  1553. (let ((grand-parent (org-list-get-parent
  1554. parent struct parents)))
  1555. (push (cons parent item) acc)
  1556. (cons item grand-parent))))))))
  1557. (mapcar out parents)))
  1558. (defun org-list-struct-indent (start end struct parents prevs)
  1559. "Indent items between START and END in structure STRUCT.
  1560. PARENTS is the alist of parents. See
  1561. `org-list-struct-parent-alist'. PREVS is the alist of previous
  1562. items. See `org-list-struct-prev-alist'.
  1563. START is included and END excluded.
  1564. STRUCT may be modified if `org-list-demote-modify-bullet' matches
  1565. bullets between START and END."
  1566. (let* (acc
  1567. (set-assoc (lambda (cell) (push cell acc) cell))
  1568. (change-bullet-maybe
  1569. (function
  1570. (lambda (item)
  1571. (let* ((bul (org-trim (org-list-get-bullet item struct)))
  1572. (new-bul-p (cdr (assoc bul org-list-demote-modify-bullet))))
  1573. (when new-bul-p (org-list-set-bullet item struct new-bul-p))))))
  1574. (ind
  1575. (lambda (cell)
  1576. (let* ((item (car cell))
  1577. (parent (cdr cell)))
  1578. (cond
  1579. ;; Item not yet in zone: keep association
  1580. ((< item start) cell)
  1581. ((>= item end)
  1582. ;; Item out of zone: follow associations in acc
  1583. (let ((convert (assq parent acc)))
  1584. (if convert (cons item (cdr convert)) cell)))
  1585. (t
  1586. ;; Item is in zone...
  1587. (let ((prev (org-list-get-prev-item item struct prevs)))
  1588. ;; Check if bullet needs to be changed
  1589. (funcall change-bullet-maybe item)
  1590. (cond
  1591. ;; First item indented but not parent: error
  1592. ((and (not prev) (< parent start))
  1593. (error "Cannot indent the first item of a list"))
  1594. ;; First item and parent indented: keep same parent
  1595. ((not prev) (funcall set-assoc cell))
  1596. ;; Previous item not indented: reparent to it
  1597. ((< prev start) (funcall set-assoc (cons item prev)))
  1598. ;; Previous item indented: reparent like it
  1599. (t
  1600. (funcall set-assoc
  1601. (cons item (cdr (assq prev acc)))))))))))))
  1602. (mapcar ind parents)))
  1603. (defun org-list-struct-apply-struct (struct old-struct)
  1604. "Apply modifications to list so it mirrors STRUCT.
  1605. OLD-STRUCT is the structure before any modifications. Thus, the
  1606. function is smart enough to modify only parts of buffer which
  1607. have changed.
  1608. Initial position of cursor is restored after the changes."
  1609. (let* ((pos (copy-marker (point)))
  1610. (shift-body-ind
  1611. (function
  1612. ;; Shift the indentation between END and BEG by DELTA.
  1613. ;; Start from the line before END.
  1614. (lambda (end beg delta)
  1615. (unless (= delta 0)
  1616. (goto-char end)
  1617. (forward-line -1)
  1618. (while (or (> (point) beg)
  1619. (and (= (point) beg) (not (org-at-item-p))))
  1620. (when (org-looking-at-p "^[ \t]*\\S-")
  1621. (let ((i (org-get-indentation)))
  1622. (org-indent-line-to (+ i delta))))
  1623. (forward-line -1))))))
  1624. (modify-item
  1625. (function
  1626. ;; Replace item first line elements with new elements from
  1627. ;; STRUCT, if appropriate.
  1628. (lambda (item)
  1629. (goto-char item)
  1630. (let* ((new-ind (org-list-get-ind item struct))
  1631. (old-ind (org-list-get-ind item old-struct))
  1632. (new-bul (org-list-bullet-string
  1633. (org-list-get-bullet item struct)))
  1634. (old-bul (org-list-get-bullet item old-struct))
  1635. (new-box (org-list-get-checkbox item struct)))
  1636. (looking-at org-list-full-item-re)
  1637. ;; a. Replace bullet
  1638. (unless (equal old-bul new-bul)
  1639. (replace-match new-bul nil nil nil 1))
  1640. ;; b. Replace checkbox
  1641. (cond
  1642. ((and new-box
  1643. (save-match-data (org-at-item-description-p))
  1644. (cdr (assq 'checkbox org-list-automatic-rules)))
  1645. (message "Cannot add a checkbox to a description list item"))
  1646. ((equal (match-string 3) new-box))
  1647. ((and (match-string 3) new-box)
  1648. (replace-match new-box nil nil nil 3))
  1649. ((match-string 3)
  1650. (goto-char (or (match-end 2) (match-end 1)))
  1651. (looking-at "\\[[ X-]\\][ \t]+")
  1652. (replace-match ""))
  1653. (t (goto-char (or (match-end 2) (match-end 1)))
  1654. (insert (concat new-box " "))))
  1655. ;; c. Indent item to appropriate column
  1656. (unless (= new-ind old-ind)
  1657. (delete-region (goto-char (point-at-bol))
  1658. (progn (skip-chars-forward " \t") (point)))
  1659. (indent-to new-ind)))))))
  1660. ;; 1. First get list of items and position endings. We maintain
  1661. ;; two alists: ITM-SHIFT, determining indentation shift needed
  1662. ;; at item, and END-POS, a pseudo-alist where key is ending
  1663. ;; position and value point
  1664. (let (end-list acc-end itm-shift all-ends sliced-struct)
  1665. (mapc (lambda (e)
  1666. (let* ((pos (car e))
  1667. (ind-pos (org-list-get-ind pos struct))
  1668. (ind-old (org-list-get-ind pos old-struct))
  1669. (bul-pos (org-list-get-bullet pos struct))
  1670. (bul-old (org-list-get-bullet pos old-struct))
  1671. (ind-shift (- (+ ind-pos (length bul-pos))
  1672. (+ ind-old (length bul-old))))
  1673. (end-pos (org-list-get-item-end pos old-struct)))
  1674. (push (cons pos ind-shift) itm-shift)
  1675. (unless (assq end-pos old-struct)
  1676. ;; To determine real ind of an ending position that is
  1677. ;; not at an item, we have to find the item it belongs
  1678. ;; to: it is the last item (ITEM-UP), whose ending is
  1679. ;; further than the position we're interested in.
  1680. (let ((item-up (assoc-default end-pos acc-end '>)))
  1681. (push (cons end-pos item-up) end-list)))
  1682. (push (cons end-pos pos) acc-end)))
  1683. old-struct)
  1684. ;; 2. Slice the items into parts that should be shifted by the
  1685. ;; same amount of indentation. The slices are returned in
  1686. ;; reverse order so changes modifying buffer do not change
  1687. ;; positions they refer to.
  1688. (setq all-ends (sort (append (mapcar 'car itm-shift)
  1689. (org-uniquify (mapcar 'car end-list)))
  1690. '<))
  1691. (while (cdr all-ends)
  1692. (let* ((up (pop all-ends))
  1693. (down (car all-ends))
  1694. (ind (if (assq up struct)
  1695. (cdr (assq up itm-shift))
  1696. (cdr (assq (cdr (assq up end-list)) itm-shift)))))
  1697. (push (list down up ind) sliced-struct)))
  1698. ;; 3. Modify each slice in buffer, from end to beginning, with a
  1699. ;; special action when beginning is at item start.
  1700. (mapc (lambda (e)
  1701. (apply shift-body-ind e)
  1702. (let ((beg (nth 1 e)))
  1703. (when (assq beg struct)
  1704. (funcall modify-item beg))))
  1705. sliced-struct))
  1706. ;; 4. Go back to initial position
  1707. (goto-char pos)))
  1708. ;;; Indentation
  1709. (defun org-outdent-item ()
  1710. "Outdent a local list item, but not its children.
  1711. If a region is active, all items inside will be moved."
  1712. (interactive)
  1713. (if (org-at-item-p)
  1714. (let ((struct (org-list-struct)))
  1715. (org-list-indent-item-generic -1 t struct))
  1716. (error "Not at an item")))
  1717. (defun org-indent-item ()
  1718. "Indent a local list item, but not its children.
  1719. If a region is active, all items inside will be moved."
  1720. (interactive)
  1721. (if (org-at-item-p)
  1722. (let ((struct (org-list-struct)))
  1723. (org-list-indent-item-generic 1 t struct))
  1724. (error "Not at an item")))
  1725. (defun org-outdent-item-tree ()
  1726. "Outdent a local list item including its children.
  1727. If a region is active, all items inside will be moved."
  1728. (interactive)
  1729. (let ((regionp (org-region-active-p)))
  1730. (cond
  1731. ((or (org-at-item-p)
  1732. (and (org-region-active-p)
  1733. (goto-char (region-beginning))
  1734. (org-at-item-p)))
  1735. (let ((struct (org-list-struct)))
  1736. (org-list-indent-item-generic -1 nil struct)))
  1737. (regionp (error "Region not starting at an item"))
  1738. (t (error "Not at an item")))))
  1739. (defun org-indent-item-tree ()
  1740. "Indent a local list item including its children.
  1741. If a region is active, all items inside will be moved."
  1742. (interactive)
  1743. (let ((regionp (org-region-active-p)))
  1744. (cond
  1745. ((or (org-at-item-p)
  1746. (and (org-region-active-p)
  1747. (goto-char (region-beginning))
  1748. (org-at-item-p)))
  1749. (let ((struct (org-list-struct)))
  1750. (org-list-indent-item-generic 1 nil struct)))
  1751. (regionp (error "Region not starting at an item"))
  1752. (t (error "Not at an item")))))
  1753. (defvar org-tab-ind-state)
  1754. (defun org-cycle-item-indentation ()
  1755. "Cycle levels of indentation of an empty item.
  1756. The first run indents the item, if applicable. Subsequents runs
  1757. outdent it at meaningful levels in the list. When done, item is
  1758. put back at its original position with its original bullet.
  1759. Return t at each successful move."
  1760. (when (org-at-item-p)
  1761. (let* ((org-adapt-indentation nil)
  1762. (struct (org-list-struct))
  1763. (ind (org-list-get-ind (point-at-bol) struct)))
  1764. ;; Check that item is really empty
  1765. (when (and (save-excursion
  1766. (beginning-of-line)
  1767. (looking-at org-list-full-item-re))
  1768. (>= (match-end 0) (save-excursion
  1769. (goto-char (org-list-get-item-end
  1770. (point-at-bol) struct))
  1771. (skip-chars-backward " \r\t\n")
  1772. (point))))
  1773. (setq this-command 'org-cycle-item-indentation)
  1774. ;; When in the middle of the cycle, try to outdent first. If it
  1775. ;; fails, and point is still at initial position, indent. Else,
  1776. ;; go back to original position.
  1777. (if (eq last-command 'org-cycle-item-indentation)
  1778. (cond
  1779. ((ignore-errors (org-list-indent-item-generic -1 t struct)))
  1780. ((and (= ind (car org-tab-ind-state))
  1781. (ignore-errors (org-list-indent-item-generic 1 t struct))))
  1782. (t (back-to-indentation)
  1783. (org-indent-to-column (car org-tab-ind-state))
  1784. (looking-at "\\S-+")
  1785. (replace-match (cdr org-tab-ind-state))
  1786. (end-of-line)
  1787. ;; Break cycle
  1788. (setq this-command 'identity)))
  1789. ;; If a cycle is starting, remember indentation and bullet,
  1790. ;; then try to indent. If it fails, try to outdent.
  1791. (setq org-tab-ind-state (cons ind (org-get-bullet)))
  1792. (cond
  1793. ((ignore-errors (org-list-indent-item-generic 1 t struct)))
  1794. ((ignore-errors (org-list-indent-item-generic -1 t struct)))
  1795. (t (error "Cannot move item"))))
  1796. t))))
  1797. ;;; Bullets
  1798. (defun org-get-bullet ()
  1799. "Return the bullet of the item at point.
  1800. Assume cursor is at an item."
  1801. (save-excursion
  1802. (beginning-of-line)
  1803. (and (looking-at "[ \t]*\\(\\S-+\\)") (match-string 1))))
  1804. (defun org-list-bullet-string (bullet)
  1805. "Return BULLET with the correct number of whitespaces.
  1806. It determines the number of whitespaces to append by looking at
  1807. `org-list-two-spaces-after-bullet-regexp'."
  1808. (save-match-data
  1809. (string-match "\\S-+\\([ \t]*\\)" bullet)
  1810. (replace-match
  1811. (save-match-data
  1812. (concat
  1813. " "
  1814. ;; Do we need to concat another white space ?
  1815. (when (and org-list-two-spaces-after-bullet-regexp
  1816. (string-match org-list-two-spaces-after-bullet-regexp bullet))
  1817. " ")))
  1818. nil nil bullet 1)))
  1819. (defun org-list-use-alpha-bul-p (first struct prevs)
  1820. "Can list starting at FIRST use alphabetical bullets?
  1821. STRUCT is list structure. See `org-list-struct'. PREVS is the
  1822. alist of previous items. See `org-list-struct-prev-alist'."
  1823. (and org-alphabetical-lists
  1824. (catch 'exit
  1825. (let ((item first) (ascii 64) (case-fold-search nil))
  1826. ;; Pretend that bullets are uppercase and checked if
  1827. ;; alphabet is sufficient, taking counters into account.
  1828. (while item
  1829. (let ((bul (org-list-get-bullet item struct))
  1830. (count (org-list-get-counter item struct)))
  1831. ;; Virtually determine current bullet
  1832. (if (and count (string-match "[a-zA-Z]" count))
  1833. ;; Counters are not case-sensitive.
  1834. (setq ascii (string-to-char (upcase count)))
  1835. (setq ascii (1+ ascii)))
  1836. ;; Test if bullet would be over z or Z.
  1837. (if (> ascii 90)
  1838. (throw 'exit nil)
  1839. (setq item (org-list-get-next-item item struct prevs)))))
  1840. ;; All items checked. All good.
  1841. t))))
  1842. (defun org-list-inc-bullet-maybe (bullet)
  1843. "Increment BULLET if applicable."
  1844. (let ((case-fold-search nil))
  1845. (cond
  1846. ;; Num bullet: increment it.
  1847. ((string-match "[0-9]+" bullet)
  1848. (replace-match
  1849. (number-to-string (1+ (string-to-number (match-string 0 bullet))))
  1850. nil nil bullet))
  1851. ;; Alpha bullet: increment it.
  1852. ((string-match "[A-Za-z]" bullet)
  1853. (replace-match
  1854. (char-to-string (1+ (string-to-char (match-string 0 bullet))))
  1855. nil nil bullet))
  1856. ;; Unordered bullet: leave it.
  1857. (t bullet))))
  1858. (defun org-list-repair ()
  1859. "Make sure all items are correctly indented, with the right bullet.
  1860. This function scans the list at point, along with any sublist."
  1861. (interactive)
  1862. (unless (org-at-item-p) (error "This is not a list"))
  1863. (let* ((struct (org-list-struct))
  1864. (parents (org-list-struct-parent-alist struct)))
  1865. (org-list-struct-fix-struct struct parents)))
  1866. (defun org-cycle-list-bullet (&optional which)
  1867. "Cycle through the different itemize/enumerate bullets.
  1868. This cycle the entire list level through the sequence:
  1869. `-' -> `+' -> `*' -> `1.' -> `1)'
  1870. If WHICH is a valid string, use that as the new bullet. If WHICH
  1871. is an integer, 0 means `-', 1 means `+' etc. If WHICH is
  1872. `previous', cycle backwards."
  1873. (interactive "P")
  1874. (unless (org-at-item-p) (error "This is not a list"))
  1875. (save-excursion
  1876. (beginning-of-line)
  1877. (let* ((struct (org-list-struct))
  1878. (parents (org-list-struct-parent-alist struct))
  1879. (prevs (org-list-struct-prev-alist struct))
  1880. (list-beg (org-list-get-first-item (point) struct prevs))
  1881. (bullet (org-list-get-bullet list-beg struct))
  1882. (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules)))
  1883. (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs))
  1884. (case-fold-search nil)
  1885. (current (cond
  1886. ((string-match "[a-z]\\." bullet) "a.")
  1887. ((string-match "[a-z])" bullet) "a)")
  1888. ((string-match "[A-Z]\\." bullet) "A.")
  1889. ((string-match "[A-Z])" bullet) "A)")
  1890. ((string-match "\\." bullet) "1.")
  1891. ((string-match ")" bullet) "1)")
  1892. (t (org-trim bullet))))
  1893. ;; Compute list of possible bullets, depending on context
  1894. (bullet-list
  1895. (append '("-" "+" )
  1896. ;; *-bullets are not allowed at column 0
  1897. (unless (and bullet-rule-p
  1898. (looking-at "\\S-")) '("*"))
  1899. ;; Description items cannot be numbered
  1900. (unless (or (eq org-plain-list-ordered-item-terminator ?\))
  1901. (and bullet-rule-p (org-at-item-description-p)))
  1902. '("1."))
  1903. (unless (or (eq org-plain-list-ordered-item-terminator ?.)
  1904. (and bullet-rule-p (org-at-item-description-p)))
  1905. '("1)"))
  1906. (unless (or (not alpha-p)
  1907. (eq org-plain-list-ordered-item-terminator ?\))
  1908. (and bullet-rule-p (org-at-item-description-p)))
  1909. '("a." "A."))
  1910. (unless (or (not alpha-p)
  1911. (eq org-plain-list-ordered-item-terminator ?.)
  1912. (and bullet-rule-p (org-at-item-description-p)))
  1913. '("a)" "A)"))))
  1914. (len (length bullet-list))
  1915. (item-index (- len (length (member current bullet-list))))
  1916. (get-value (lambda (index) (nth (mod index len) bullet-list)))
  1917. (new (cond
  1918. ((member which bullet-list) which)
  1919. ((numberp which) (funcall get-value which))
  1920. ((eq 'previous which) (funcall get-value (1- item-index)))
  1921. (t (funcall get-value (1+ item-index))))))
  1922. ;; Use a short variation of `org-list-struct-fix-struct' as
  1923. ;; there's no need to go through all the steps.
  1924. (let ((old-struct (mapcar (lambda (e) (copy-alist e)) struct)))
  1925. (org-list-set-bullet list-beg struct (org-list-bullet-string new))
  1926. (org-list-struct-fix-bul struct prevs)
  1927. (org-list-struct-fix-ind struct parents)
  1928. (org-list-struct-apply-struct struct old-struct)))))
  1929. ;;; Checkboxes
  1930. (defun org-toggle-checkbox (&optional toggle-presence)
  1931. "Toggle the checkbox in the current line.
  1932. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
  1933. double prefix, set checkbox to [-].
  1934. When there is an active region, toggle status or presence of the
  1935. first checkbox there, and make every item inside have the
  1936. same status or presence, respectively.
  1937. If the cursor is in a headline, apply this to all checkbox items
  1938. in the text below the heading, taking as reference the first item
  1939. in subtree, ignoring drawers."
  1940. (interactive "P")
  1941. (save-excursion
  1942. (let* (singlep
  1943. block-item
  1944. lim-up
  1945. lim-down
  1946. (orderedp (ignore-errors (org-entry-get nil "ORDERED")))
  1947. (bounds
  1948. ;; In a region, start at first item in region
  1949. (cond
  1950. ((org-region-active-p)
  1951. (let ((limit (region-end)))
  1952. (goto-char (region-beginning))
  1953. (if (org-list-search-forward (org-item-beginning-re) limit t)
  1954. (setq lim-up (point-at-bol))
  1955. (error "No item in region"))
  1956. (setq lim-down (copy-marker limit))))
  1957. ((org-on-heading-p)
  1958. ;; On an heading, start at first item after drawers
  1959. (let ((limit (save-excursion (outline-next-heading) (point))))
  1960. (forward-line 1)
  1961. (when (looking-at org-drawer-regexp)
  1962. (re-search-forward "^[ \t]*:END:" limit nil))
  1963. (if (org-list-search-forward (org-item-beginning-re) limit t)
  1964. (setq lim-up (point-at-bol))
  1965. (error "No item in subtree"))
  1966. (setq lim-down (copy-marker limit))))
  1967. ;; Just one item: set singlep flag
  1968. ((org-at-item-p)
  1969. (setq singlep t)
  1970. (setq lim-up (point-at-bol)
  1971. lim-down (point-at-eol)))
  1972. (t (error "Not at an item or heading, and no active region"))))
  1973. ;; determine the checkbox going to be applied to all items
  1974. ;; within bounds
  1975. (ref-checkbox
  1976. (progn
  1977. (goto-char lim-up)
  1978. (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
  1979. (cond
  1980. ((equal toggle-presence '(16)) "[-]")
  1981. ((equal toggle-presence '(4))
  1982. (unless cbox "[ ]"))
  1983. ((equal "[ ]" cbox) "[X]")
  1984. (t "[ ]"))))))
  1985. ;; When an item is found within bounds, grab the full list at
  1986. ;; point structure, then: 1. set checkbox of all its items
  1987. ;; within bounds to ref-checkbox; 2. fix checkboxes of the whole
  1988. ;; list; 3. move point after the list.
  1989. (goto-char lim-up)
  1990. (while (and (< (point) lim-down)
  1991. (org-list-search-forward (org-item-beginning-re)
  1992. lim-down 'move))
  1993. (let* ((struct (org-list-struct))
  1994. (struct-copy (mapcar (lambda (e) (copy-alist e)) struct))
  1995. (parents (org-list-struct-parent-alist struct))
  1996. (prevs (org-list-struct-prev-alist struct))
  1997. (bottom (copy-marker (org-list-get-bottom-point struct)))
  1998. (items-to-toggle (org-remove-if
  1999. (lambda (e) (or (< e lim-up) (> e lim-down)))
  2000. (mapcar 'car (cdr struct)))))
  2001. (mapc (lambda (e) (org-list-set-checkbox
  2002. e struct
  2003. ;; if there is no box at item, leave as-is
  2004. ;; unless function was called with C-u prefix
  2005. (let ((cur-box (org-list-get-checkbox e struct)))
  2006. (if (or cur-box (equal toggle-presence '(4)))
  2007. ref-checkbox
  2008. cur-box))))
  2009. items-to-toggle)
  2010. (setq block-item (org-list-struct-fix-box
  2011. struct parents prevs orderedp))
  2012. ;; Report some problems due to ORDERED status of subtree. If
  2013. ;; only one box was being checked, throw an error, else,
  2014. ;; only signal problems.
  2015. (cond
  2016. ((and singlep block-item (> lim-up block-item))
  2017. (error
  2018. "Checkbox blocked because of unchecked box at line %d"
  2019. (org-current-line block-item)))
  2020. (block-item
  2021. (message
  2022. "Checkboxes were removed due to unchecked box at line %d"
  2023. (org-current-line block-item))))
  2024. (goto-char bottom)
  2025. (org-list-struct-apply-struct struct struct-copy))))
  2026. (org-update-checkbox-count-maybe)))
  2027. (defun org-reset-checkbox-state-subtree ()
  2028. "Reset all checkboxes in an entry subtree."
  2029. (interactive "*")
  2030. (save-restriction
  2031. (save-excursion
  2032. (org-narrow-to-subtree)
  2033. (org-show-subtree)
  2034. (goto-char (point-min))
  2035. (let ((end (point-max)))
  2036. (while (< (point) end)
  2037. (when (org-at-item-checkbox-p)
  2038. (replace-match "[ ]" t t nil 1))
  2039. (beginning-of-line 2))))
  2040. (org-update-checkbox-count-maybe)))
  2041. (defvar org-checkbox-statistics-hook nil
  2042. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  2043. This hook runs even if checkbox rule in
  2044. `org-list-automatic-rules' does not apply, so it can be used to
  2045. implement alternative ways of collecting statistics
  2046. information.")
  2047. (defun org-update-checkbox-count-maybe ()
  2048. "Update checkbox statistics unless turned off by user."
  2049. (when (cdr (assq 'checkbox org-list-automatic-rules))
  2050. (org-update-checkbox-count))
  2051. (run-hooks 'org-checkbox-statistics-hook))
  2052. (defun org-update-checkbox-count (&optional all)
  2053. "Update the checkbox statistics in the current section.
  2054. This will find all statistic cookies like [57%] and [6/12] and
  2055. update them with the current numbers.
  2056. With optional prefix argument ALL, do this for the whole buffer."
  2057. (interactive "P")
  2058. (save-excursion
  2059. (let ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  2060. (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
  2061. (recursivep
  2062. (or (not org-hierarchical-checkbox-statistics)
  2063. (string-match "\\<recursive\\>"
  2064. (or (ignore-errors
  2065. (org-entry-get nil "COOKIE_DATA"))
  2066. ""))))
  2067. (bounds (if all
  2068. (cons (point-min) (point-max))
  2069. (cons (or (ignore-errors (org-back-to-heading) (point))
  2070. (point-min))
  2071. (save-excursion (outline-next-heading) (point)))))
  2072. (count-boxes
  2073. (function
  2074. ;; Return number of checked boxes and boxes of all types
  2075. ;; in all structures in STRUCTS. If RECURSIVEP is non-nil,
  2076. ;; also count boxes in sub-lists. If ITEM is nil, count
  2077. ;; across the whole structure, else count only across
  2078. ;; subtree whose ancestor is ITEM.
  2079. (lambda (item structs recursivep)
  2080. (let ((c-on 0) (c-all 0))
  2081. (mapc
  2082. (lambda (s)
  2083. (let* ((pre (org-list-struct-prev-alist s))
  2084. (par (org-list-struct-parent-alist s))
  2085. (items
  2086. (cond
  2087. ((and recursivep item) (org-list-get-subtree item s))
  2088. (recursivep (mapcar 'car s))
  2089. (item (org-list-get-children item s par))
  2090. (t (org-list-get-all-items
  2091. (org-list-get-top-point s) s pre))))
  2092. (cookies (delq nil (mapcar
  2093. (lambda (e)
  2094. (org-list-get-checkbox e s))
  2095. items))))
  2096. (setq c-all (+ (length cookies) c-all)
  2097. c-on (+ (org-count "[X]" cookies) c-on))))
  2098. structs)
  2099. (cons c-on c-all)))))
  2100. (backup-end 1)
  2101. cookies-list structs-bak box-num)
  2102. (goto-char (car bounds))
  2103. ;; 1. Build an alist for each cookie found within BOUNDS. The
  2104. ;; key will be position at beginning of cookie and values
  2105. ;; ending position, format of cookie, and a cell whose car is
  2106. ;; number of checked boxes to report, and cdr total number of
  2107. ;; boxes.
  2108. (while (re-search-forward cookie-re (cdr bounds) t)
  2109. (catch 'skip
  2110. (save-excursion
  2111. (push
  2112. (list
  2113. (match-beginning 1) ; cookie start
  2114. (match-end 1) ; cookie end
  2115. (match-string 2) ; percent?
  2116. (cond ; boxes count
  2117. ;; Cookie is at an heading, but specifically for todo,
  2118. ;; not for checkboxes: skip it.
  2119. ((and (org-on-heading-p)
  2120. (string-match "\\<todo\\>"
  2121. (downcase
  2122. (or (org-entry-get nil "COOKIE_DATA") ""))))
  2123. (throw 'skip nil))
  2124. ;; Cookie is at an heading, but all lists before next
  2125. ;; heading already have been read. Use data collected
  2126. ;; in STRUCTS-BAK. This should only happen when heading
  2127. ;; has more than one cookie on it.
  2128. ((and (org-on-heading-p)
  2129. (<= (save-excursion (outline-next-heading) (point))
  2130. backup-end))
  2131. (funcall count-boxes nil structs-bak recursivep))
  2132. ;; Cookie is at a fresh heading. Grab structure of
  2133. ;; every list containing a checkbox between point and
  2134. ;; next headline, and save them in STRUCTS-BAK.
  2135. ((org-on-heading-p)
  2136. (setq backup-end (save-excursion
  2137. (outline-next-heading) (point)))
  2138. (while (org-list-search-forward box-re backup-end 'move)
  2139. (let* ((struct (org-list-struct))
  2140. (bottom (org-list-get-bottom-point struct)))
  2141. (push struct structs-bak)
  2142. (goto-char bottom)))
  2143. (funcall count-boxes nil structs-bak recursivep))
  2144. ;; Cookie is at an item, and we already have list
  2145. ;; structure stored in STRUCTS-BAK.
  2146. ((and (org-at-item-p)
  2147. (< (point-at-bol) backup-end))
  2148. (funcall count-boxes (point-at-bol) structs-bak recursivep))
  2149. ;; Cookie is at an item, but we need to compute list
  2150. ;; structure.
  2151. ((org-at-item-p)
  2152. (let ((struct (org-list-struct)))
  2153. (setq backup-end (org-list-get-bottom-point struct)
  2154. structs-bak (list struct)))
  2155. (funcall count-boxes (point-at-bol) structs-bak recursivep))
  2156. ;; Else, cookie found is at a wrong place. Skip it.
  2157. (t (throw 'skip nil))))
  2158. cookies-list))))
  2159. ;; 2. Apply alist to buffer, in reverse order so positions stay
  2160. ;; unchanged after cookie modifications.
  2161. (mapc (lambda (cookie)
  2162. (let* ((beg (car cookie))
  2163. (end (nth 1 cookie))
  2164. (percentp (nth 2 cookie))
  2165. (checked (car (nth 3 cookie)))
  2166. (total (cdr (nth 3 cookie)))
  2167. (new (if percentp
  2168. (format "[%d%%]" (/ (* 100 checked)
  2169. (max 1 total)))
  2170. (format "[%d/%d]" checked total))))
  2171. (goto-char beg)
  2172. (insert new)
  2173. (delete-region (point) (+ (point) (- end beg)))))
  2174. cookies-list))))
  2175. (defun org-get-checkbox-statistics-face ()
  2176. "Select the face for checkbox statistics.
  2177. The face will be `org-done' when all relevant boxes are checked.
  2178. Otherwise it will be `org-todo'."
  2179. (if (match-end 1)
  2180. (if (equal (match-string 1) "100%")
  2181. 'org-checkbox-statistics-done
  2182. 'org-checkbox-statistics-todo)
  2183. (if (and (> (match-end 2) (match-beginning 2))
  2184. (equal (match-string 2) (match-string 3)))
  2185. 'org-checkbox-statistics-done
  2186. 'org-checkbox-statistics-todo)))
  2187. ;;; Misc Tools
  2188. (defun org-apply-on-list (function init-value &rest args)
  2189. "Call FUNCTION on each item of the list at point.
  2190. FUNCTION must be called with at least one argument: INIT-VALUE,
  2191. that will contain the value returned by the function at the
  2192. previous item, plus ARGS extra arguments.
  2193. FUNCTION is applied on items in reverse order.
  2194. As an example, (org-apply-on-list (lambda (result) (1+ result)) 0)
  2195. will return the number of items in the current list.
  2196. Sublists of the list are skipped. Cursor is always at the
  2197. beginning of the item."
  2198. (let* ((struct (org-list-struct))
  2199. (prevs (org-list-struct-prev-alist struct))
  2200. (item (copy-marker (point-at-bol)))
  2201. (all (org-list-get-all-items (marker-position item) struct prevs))
  2202. (value init-value))
  2203. (mapc (lambda (e)
  2204. (goto-char e)
  2205. (setq value (apply function value args)))
  2206. (nreverse all))
  2207. (goto-char item)
  2208. value))
  2209. (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
  2210. "Sort plain list items.
  2211. The cursor may be at any item of the list that should be sorted.
  2212. Sublists are not sorted. Checkboxes, if any, are ignored.
  2213. Sorting can be alphabetically, numerically, by date/time as given by
  2214. a time stamp, by a property or by priority.
  2215. Comparing entries ignores case by default. However, with an
  2216. optional argument WITH-CASE, the sorting considers case as well.
  2217. The command prompts for the sorting type unless it has been given
  2218. to the function through the SORTING-TYPE argument, which needs to
  2219. be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
  2220. meaning of each character:
  2221. n Numerically, by converting the beginning of the item to a number.
  2222. a Alphabetically. Only the first line of item is checked.
  2223. t By date/time, either the first active time stamp in the entry, if
  2224. any, or by the first inactive one. In a timer list, sort the timers.
  2225. Capital letters will reverse the sort order.
  2226. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a
  2227. function to be called with point at the beginning of the record.
  2228. It must return either a string or a number that should serve as
  2229. the sorting key for that record. It will then use COMPARE-FUNC to
  2230. compare entries."
  2231. (interactive "P")
  2232. (let* ((case-func (if with-case 'identity 'downcase))
  2233. (struct (org-list-struct))
  2234. (prevs (org-list-struct-prev-alist struct))
  2235. (start (org-list-get-list-begin (point-at-bol) struct prevs))
  2236. (end (org-list-get-list-end (point-at-bol) struct prevs))
  2237. (sorting-type
  2238. (progn
  2239. (message
  2240. "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
  2241. (read-char-exclusive)))
  2242. (getkey-func (and (= (downcase sorting-type) ?f)
  2243. (org-icompleting-read "Sort using function: "
  2244. obarray 'fboundp t nil nil)
  2245. (intern getkey-func))))
  2246. (message "Sorting items...")
  2247. (save-restriction
  2248. (narrow-to-region start end)
  2249. (goto-char (point-min))
  2250. (let* ((dcst (downcase sorting-type))
  2251. (case-fold-search nil)
  2252. (now (current-time))
  2253. (sort-func (cond
  2254. ((= dcst ?a) 'string<)
  2255. ((= dcst ?f) compare-func)
  2256. ((= dcst ?t) '<)
  2257. (t nil)))
  2258. (next-record (lambda ()
  2259. (skip-chars-forward " \r\t\n")
  2260. (beginning-of-line)))
  2261. (end-record (lambda ()
  2262. (goto-char (org-list-get-item-end-before-blank
  2263. (point) struct))))
  2264. (value-to-sort
  2265. (lambda ()
  2266. (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
  2267. (cond
  2268. ((= dcst ?n)
  2269. (string-to-number (buffer-substring (match-end 0)
  2270. (point-at-eol))))
  2271. ((= dcst ?a)
  2272. (buffer-substring (match-end 0) (point-at-eol)))
  2273. ((= dcst ?t)
  2274. (cond
  2275. ;; If it is a timer list, convert timer to seconds
  2276. ((org-at-item-timer-p)
  2277. (org-timer-hms-to-secs (match-string 1)))
  2278. ((or (re-search-forward org-ts-regexp (point-at-eol) t)
  2279. (re-search-forward org-ts-regexp-both
  2280. (point-at-eol) t))
  2281. (org-time-string-to-seconds (match-string 0)))
  2282. (t (org-float-time now))))
  2283. ((= dcst ?f)
  2284. (if getkey-func
  2285. (let ((value (funcall getkey-func)))
  2286. (if (stringp value)
  2287. (funcall case-func value)
  2288. value))
  2289. (error "Invalid key function `%s'" getkey-func)))
  2290. (t (error "Invalid sorting type `%c'" sorting-type)))))))
  2291. (sort-subr (/= dcst sorting-type)
  2292. next-record
  2293. end-record
  2294. value-to-sort
  2295. nil
  2296. sort-func)
  2297. ;; Read and fix list again, as `sort-subr' probably destroyed
  2298. ;; its structure.
  2299. (org-list-repair)
  2300. (run-hooks 'org-after-sorting-entries-or-items-hook)
  2301. (message "Sorting items...done")))))
  2302. ;;; Send and receive lists
  2303. (defun org-list-parse-list (&optional delete)
  2304. "Parse the list at point and maybe DELETE it.
  2305. Return a list whose car is a symbol of list type, among
  2306. `ordered', `unordered' and `descriptive'. Then, each item is a
  2307. list whose elements are strings and other sub-lists. Inside
  2308. strings, checkboxes are replaced by \"[CBON]\" and \"[CBOFF]\".
  2309. For example, the following list:
  2310. 1. first item
  2311. + sub-item one
  2312. + [X] sub-item two
  2313. more text in first item
  2314. 2. last item
  2315. will be parsed as:
  2316. \(ordered \(\"first item\"
  2317. \(unordered \(\"sub-item one\"\) \(\"[CBON] sub-item two\"\)\)
  2318. \"more text in first item\"\)
  2319. \(\"last item\"\)\)
  2320. Point is left at list end."
  2321. (let* ((struct (org-list-struct))
  2322. (prevs (org-list-struct-prev-alist struct))
  2323. (parents (org-list-struct-parent-alist struct))
  2324. (top (org-list-get-top-point struct))
  2325. (bottom (org-list-get-bottom-point struct))
  2326. out
  2327. (parse-sublist
  2328. (function
  2329. ;; return a list whose car is list type and cdr a list of
  2330. ;; items' body.
  2331. (lambda (e)
  2332. (cons (org-list-get-list-type (car e) struct prevs)
  2333. (mapcar parse-item e)))))
  2334. (parse-item
  2335. (function
  2336. ;; return a list containing text and any sublist inside
  2337. ;; item.
  2338. (lambda (e)
  2339. (let ((start (save-excursion
  2340. (goto-char e)
  2341. (looking-at (org-item-beginning-re))
  2342. (match-end 0)))
  2343. (childp (org-list-has-child-p e struct))
  2344. (end (org-list-get-item-end e struct)))
  2345. (if childp
  2346. (let* ((children (org-list-get-children e struct parents))
  2347. (body (list (funcall get-text start childp t))))
  2348. (while children
  2349. (let* ((first (car children))
  2350. (sub (org-list-get-all-items first struct prevs))
  2351. (last-c (car (last sub)))
  2352. (last-end (org-list-get-item-end last-c struct)))
  2353. (push (funcall parse-sublist sub) body)
  2354. (setq children (cdr (member last-c children)))
  2355. (unless (= (or (car children) end) last-end)
  2356. (push (funcall get-text last-end (or (car children) end) nil)
  2357. body))))
  2358. (nreverse body))
  2359. (list (funcall get-text start end t)))))))
  2360. (get-text
  2361. (function
  2362. ;; return text between BEG and END, trimmed, with
  2363. ;; checkboxes replaced if BOX is true.
  2364. (lambda (beg end box)
  2365. (let ((text (org-trim (buffer-substring beg end))))
  2366. (if (and box
  2367. (string-match
  2368. "^\\(?:\\[@\\(?:start:\\)?\\(?:[0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\[\\([xX ]\\)\\]"
  2369. text))
  2370. (replace-match
  2371. (if (equal (match-string 1 text) " ") "CBOFF" "CBON")
  2372. t nil text 1)
  2373. text))))))
  2374. ;; store output, take care of cursor position and deletion of
  2375. ;; list, then return output.
  2376. (setq out (funcall parse-sublist (org-list-get-all-items top struct prevs)))
  2377. (goto-char top)
  2378. (when delete
  2379. (delete-region top bottom)
  2380. (when (and (not (eq org-list-ending-method 'indent))
  2381. (looking-at org-list-end-re))
  2382. (replace-match "\n")))
  2383. out))
  2384. (defun org-list-make-subtree ()
  2385. "Convert the plain list at point into a subtree."
  2386. (interactive)
  2387. (if (not (ignore-errors (goto-char (org-in-item-p))))
  2388. (error "Not in a list")
  2389. (let ((list (org-list-parse-list t)) nstars)
  2390. (save-excursion
  2391. (if (ignore-errors (org-back-to-heading))
  2392. (progn (looking-at org-complex-heading-regexp)
  2393. (setq nstars (length (match-string 1))))
  2394. (setq nstars 0)))
  2395. (org-list-make-subtrees list (1+ nstars)))))
  2396. (defun org-list-make-subtrees (list level)
  2397. "Convert LIST into subtrees starting at LEVEL."
  2398. (if (symbolp (car list))
  2399. (org-list-make-subtrees (cdr list) level)
  2400. (mapcar (lambda (item)
  2401. (if (stringp item)
  2402. (insert (make-string
  2403. (if org-odd-levels-only
  2404. (1- (* 2 level)) level) ?*) " " item "\n")
  2405. (org-list-make-subtrees item (1+ level))))
  2406. list)))
  2407. (defun org-list-insert-radio-list ()
  2408. "Insert a radio list template appropriate for this major mode."
  2409. (interactive)
  2410. (let* ((e (assq major-mode org-list-radio-list-templates))
  2411. (txt (nth 1 e))
  2412. name pos)
  2413. (unless e (error "No radio list setup defined for %s" major-mode))
  2414. (setq name (read-string "List name: "))
  2415. (while (string-match "%n" txt)
  2416. (setq txt (replace-match name t t txt)))
  2417. (or (bolp) (insert "\n"))
  2418. (setq pos (point))
  2419. (insert txt)
  2420. (goto-char pos)))
  2421. (defun org-list-send-list (&optional maybe)
  2422. "Send a transformed version of this list to the receiver position.
  2423. With argument MAYBE, fail quietly if no transformation is defined for
  2424. this list."
  2425. (interactive)
  2426. (catch 'exit
  2427. (unless (org-at-item-p) (error "Not at a list item"))
  2428. (save-excursion
  2429. (re-search-backward "#\\+ORGLST" nil t)
  2430. (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
  2431. (if maybe
  2432. (throw 'exit nil)
  2433. (error "Don't know how to transform this list"))))
  2434. (let* ((name (match-string 1))
  2435. (transform (intern (match-string 2)))
  2436. (bottom-point
  2437. (save-excursion
  2438. (re-search-forward
  2439. "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
  2440. (match-beginning 0)))
  2441. (top-point
  2442. (progn
  2443. (re-search-backward "#\\+ORGLST" nil t)
  2444. (re-search-forward (org-item-beginning-re) bottom-point t)
  2445. (match-beginning 0)))
  2446. (list (save-restriction
  2447. (narrow-to-region top-point bottom-point)
  2448. (org-list-parse-list)))
  2449. beg txt)
  2450. (unless (fboundp transform)
  2451. (error "No such transformation function %s" transform))
  2452. (let ((txt (funcall transform list)))
  2453. ;; Find the insertion place
  2454. (save-excursion
  2455. (goto-char (point-min))
  2456. (unless (re-search-forward
  2457. (concat "BEGIN RECEIVE ORGLST +"
  2458. name
  2459. "\\([ \t]\\|$\\)") nil t)
  2460. (error "Don't know where to insert translated list"))
  2461. (goto-char (match-beginning 0))
  2462. (beginning-of-line 2)
  2463. (setq beg (point))
  2464. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  2465. (error "Cannot find end of insertion region"))
  2466. (delete-region beg (point-at-bol))
  2467. (goto-char beg)
  2468. (insert txt "\n")))
  2469. (message "List converted and installed at receiver location"))))
  2470. (defun org-list-to-generic (list params)
  2471. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  2472. Valid parameters PARAMS are
  2473. :ustart String to start an unordered list
  2474. :uend String to end an unordered list
  2475. :ostart String to start an ordered list
  2476. :oend String to end an ordered list
  2477. :dstart String to start a descriptive list
  2478. :dend String to end a descriptive list
  2479. :dtstart String to start a descriptive term
  2480. :dtend String to end a descriptive term
  2481. :ddstart String to start a description
  2482. :ddend String to end a description
  2483. :splice When set to t, return only list body lines, don't wrap
  2484. them into :[u/o]start and :[u/o]end. Default is nil.
  2485. :istart String to start a list item
  2486. :iend String to end a list item
  2487. :isep String to separate items
  2488. :lsep String to separate sublists
  2489. :cboff String to insert for an unchecked checkbox
  2490. :cbon String to insert for a checked checkbox"
  2491. (interactive)
  2492. (let* ((p params) sublist
  2493. (splicep (plist-get p :splice))
  2494. (ostart (plist-get p :ostart))
  2495. (oend (plist-get p :oend))
  2496. (ustart (plist-get p :ustart))
  2497. (uend (plist-get p :uend))
  2498. (dstart (plist-get p :dstart))
  2499. (dend (plist-get p :dend))
  2500. (dtstart (plist-get p :dtstart))
  2501. (dtend (plist-get p :dtend))
  2502. (ddstart (plist-get p :ddstart))
  2503. (ddend (plist-get p :ddend))
  2504. (istart (plist-get p :istart))
  2505. (iend (plist-get p :iend))
  2506. (isep (plist-get p :isep))
  2507. (lsep (plist-get p :lsep))
  2508. (cbon (plist-get p :cbon))
  2509. (cboff (plist-get p :cboff))
  2510. (export-item
  2511. (function
  2512. ;; Export an item ITEM of type TYPE. First string in item
  2513. ;; is treated in a special way as it can bring extra
  2514. ;; information that needs to be processed.
  2515. (lambda (item type)
  2516. (let ((fmt (if (eq type 'descriptive)
  2517. (concat (org-trim istart) "%s" ddend iend isep)
  2518. (concat istart "%s" iend isep)))
  2519. (first (car item)))
  2520. ;; Replace checkbox if any is found.
  2521. (cond
  2522. ((string-match "\\[CBON\\]" first)
  2523. (setq first (replace-match cbon t t first)))
  2524. ((string-match "\\[CBOFF\\]" first)
  2525. (setq first (replace-match cboff t t first)))
  2526. ((string-match "\\[-\\]" first)
  2527. (setq first (replace-match "$\\boxminus$" t t first))))
  2528. ;; Insert descriptive term if TYPE is `descriptive'.
  2529. (when (and (eq type 'descriptive)
  2530. (string-match "^\\(.*\\)[ \t]+::" first))
  2531. (setq first (concat
  2532. dtstart (org-trim (match-string 1 first)) dtend
  2533. ddstart (org-trim (substring first (match-end 0))))))
  2534. (setcar item first)
  2535. (format fmt (mapconcat
  2536. (lambda (e)
  2537. (if (stringp e) e (funcall export-sublist e)))
  2538. item isep))))))
  2539. (export-sublist
  2540. (function
  2541. ;; Export sublist SUB
  2542. (lambda (sub)
  2543. (let* ((type (car sub))
  2544. (items (cdr sub))
  2545. (fmt (cond
  2546. (splicep "%s")
  2547. ((eq type 'ordered)
  2548. (concat ostart "\n%s" oend))
  2549. ((eq type 'descriptive)
  2550. (concat dstart "\n%s" dend))
  2551. (t (concat ustart "\n%s" uend)))))
  2552. (format fmt (mapconcat
  2553. (lambda (e) (funcall export-item e type))
  2554. items lsep)))))))
  2555. (concat (funcall export-sublist list) "\n")))
  2556. (defun org-list-to-latex (list &optional params)
  2557. "Convert LIST into a LaTeX list.
  2558. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2559. with overruling parameters for `org-list-to-generic'."
  2560. (org-list-to-generic
  2561. list
  2562. (org-combine-plists
  2563. '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  2564. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  2565. :dstart "\\begin{description}" :dend "\\end{description}"
  2566. :dtstart "[" :dtend "] "
  2567. :ddstart "" :ddend ""
  2568. :istart "\\item " :iend ""
  2569. :isep "\n" :lsep "\n"
  2570. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  2571. params)))
  2572. (defun org-list-to-html (list &optional params)
  2573. "Convert LIST into a HTML list.
  2574. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2575. with overruling parameters for `org-list-to-generic'."
  2576. (org-list-to-generic
  2577. list
  2578. (org-combine-plists
  2579. '(:splicep nil :ostart "<ol>" :oend "</ol>"
  2580. :ustart "<ul>" :uend "</ul>"
  2581. :dstart "<dl>" :dend "</dl>"
  2582. :dtstart "<dt>" :dtend "</dt>"
  2583. :ddstart "<dd>" :ddend "</dd>"
  2584. :istart "<li>" :iend "</li>"
  2585. :isep "\n" :lsep "\n"
  2586. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
  2587. params)))
  2588. (defun org-list-to-texinfo (list &optional params)
  2589. "Convert LIST into a Texinfo list.
  2590. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2591. with overruling parameters for `org-list-to-generic'."
  2592. (org-list-to-generic
  2593. list
  2594. (org-combine-plists
  2595. '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  2596. :ustart "@enumerate" :uend "@end enumerate"
  2597. :dstart "@table @asis" :dend "@end table"
  2598. :dtstart " " :dtend "\n"
  2599. :ddstart "" :ddend ""
  2600. :istart "@item\n" :iend ""
  2601. :isep "\n" :lsep "\n"
  2602. :cbon "@code{[X]}" :cboff "@code{[ ]}")
  2603. params)))
  2604. (provide 'org-list)
  2605. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  2606. ;;; org-list.el ends here