org-list.el 83 KB

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