org-list.el 87 KB

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