org-list.el 101 KB

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