org-list.el 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. ;;; org-list.el --- Plain lists for Org-mode
  2. ;;
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Bastien Guerry <bzg AT altern DOT org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 6.13a
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;; This file contains the code dealing with plain lists in Org-mode.
  27. ;;; Code:
  28. (require 'org-macs)
  29. (require 'org-compat)
  30. (defvar org-blank-before-new-entry)
  31. (defvar org-M-RET-may-split-line)
  32. (declare-function org-invisible-p "org" ())
  33. (declare-function org-on-heading-p "org" (&optional invisible-ok))
  34. (declare-function outline-next-heading "outline" ())
  35. (declare-function outline-back-to-heading "outline" (&optional invisible-ok))
  36. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  37. (declare-function org-back-over-empty-lines "org" ())
  38. (declare-function org-skip-whitespace "org" ())
  39. (declare-function org-trim "org" (s))
  40. (declare-function org-get-indentation "org" (&optional line))
  41. (defgroup org-plain-lists nil
  42. "Options concerning plain lists in Org-mode."
  43. :tag "Org Plain lists"
  44. :group 'org-structure)
  45. (defcustom org-cycle-include-plain-lists nil
  46. "Non-nil means, include plain lists into visibility cycling.
  47. This means that during cycling, plain list items will *temporarily* be
  48. interpreted as outline headlines with a level given by 1000+i where i is the
  49. indentation of the bullet. In all other operations, plain list items are
  50. not seen as headlines. For example, you cannot assign a TODO keyword to
  51. such an item."
  52. :group 'org-plain-lists
  53. :type 'boolean)
  54. (defcustom org-plain-list-ordered-item-terminator t
  55. "The character that makes a line with leading number an ordered list item.
  56. Valid values are ?. and ?\). To get both terminators, use t. While
  57. ?. may look nicer, it creates the danger that a line with leading
  58. number may be incorrectly interpreted as an item. ?\) therefore is
  59. the safe choice."
  60. :group 'org-plain-lists
  61. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  62. (const :tag "paren like in \"2)\"" ?\))
  63. (const :tab "both" t)))
  64. (defcustom org-list-two-spaces-after-bullet-regexp nil
  65. "A regular expression matching bullets that should have 2 spaces after them.
  66. When nil, no bullet will have two spaces after them.
  67. When a string, it will be used as a regular expression. When the bullet
  68. type of a list is changed, the new bullet type will be matched against this
  69. regexp. If it matches, there will be two spaces instead of one after
  70. the bullet in each item of he list."
  71. :group 'org-plain-list
  72. :type '(choice
  73. (const :tag "never" nil)
  74. (regexp)))
  75. (defcustom org-empty-line-terminates-plain-lists nil
  76. "Non-nil means, an empty line ends all plain list levels.
  77. When nil, empty lines are part of the preceeding item."
  78. :group 'org-plain-lists
  79. :type 'boolean)
  80. (defcustom org-auto-renumber-ordered-lists t
  81. "Non-nil means, automatically renumber ordered plain lists.
  82. Renumbering happens when the sequence have been changed with
  83. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  84. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  85. :group 'org-plain-lists
  86. :type 'boolean)
  87. (defcustom org-provide-checkbox-statistics t
  88. "Non-nil means, update checkbox statistics after insert and toggle.
  89. When this is set, checkbox statistics is updated each time you either insert
  90. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  91. with \\[org-ctrl-c-ctrl-c\\]."
  92. :group 'org-plain-lists
  93. :type 'boolean)
  94. (defcustom org-description-max-indent 20
  95. "Maximum indentation for the second line of a description list.
  96. When the indentation would be larger than this, it will become
  97. 5 characters instead."
  98. :group 'org-plain-lists
  99. :type 'integer)
  100. (defvar org-list-beginning-re
  101. "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) +\\(.*\\)$")
  102. (defcustom org-list-radio-list-templates
  103. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  104. % END RECEIVE ORGLST %n
  105. \\begin{comment}
  106. #+ORGLST: SEND %n org-list-to-latex
  107. | | |
  108. \\end{comment}\n")
  109. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  110. @c END RECEIVE ORGLST %n
  111. @ignore
  112. #+ORGLST: SEND %n org-list-to-texinfo
  113. | | |
  114. @end ignore\n")
  115. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  116. <!-- END RECEIVE ORGLST %n -->
  117. <!--
  118. #+ORGLST: SEND %n org-list-to-html
  119. | | |
  120. -->\n"))
  121. "Templates for radio lists in different major modes.
  122. All occurrences of %n in a template will be replaced with the name of the
  123. list, obtained by prompting the user."
  124. :group 'org-plain-lists
  125. :type '(repeat
  126. (list (symbol :tag "Major mode")
  127. (string :tag "Format"))))
  128. ;;;; Plain list items, including checkboxes
  129. ;;; Plain list items
  130. (defun org-at-item-p ()
  131. "Is point in a line starting a hand-formatted item?"
  132. (let ((llt org-plain-list-ordered-item-terminator))
  133. (save-excursion
  134. (goto-char (point-at-bol))
  135. (looking-at
  136. (cond
  137. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  138. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  139. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  140. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  141. (defun org-in-item-p ()
  142. "It the cursor inside a plain list item.
  143. Does not have to be the first line."
  144. (save-excursion
  145. (condition-case nil
  146. (progn
  147. (org-beginning-of-item)
  148. (org-at-item-p)
  149. t)
  150. (error nil))))
  151. (defun org-insert-item (&optional checkbox)
  152. "Insert a new item at the current level.
  153. Return t when things worked, nil when we are not in an item."
  154. (when (save-excursion
  155. (condition-case nil
  156. (progn
  157. (org-beginning-of-item)
  158. (org-at-item-p)
  159. (if (org-invisible-p) (error "Invisible item"))
  160. t)
  161. (error nil)))
  162. (let* ((bul (match-string 0))
  163. (descp (save-excursion (goto-char (match-beginning 0))
  164. (beginning-of-line 1)
  165. (save-match-data
  166. (and (looking-at "[ \t]*\\(.*?\\) ::")
  167. (match-string 1)))))
  168. (timerp (and descp
  169. (save-match-data
  170. (string-match "^[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+$"
  171. descp))))
  172. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  173. (match-end 0)))
  174. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  175. pos)
  176. (if descp (setq checkbox nil))
  177. (if timerp
  178. (progn (org-timer-item) t)
  179. (cond
  180. ((and (org-at-item-p) (<= (point) eow))
  181. ;; before the bullet
  182. (beginning-of-line 1)
  183. (open-line (if blank 2 1)))
  184. ((<= (point) eow)
  185. (beginning-of-line 1))
  186. (t
  187. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  188. (end-of-line 1)
  189. (delete-horizontal-space))
  190. (newline (if blank 2 1))))
  191. (insert bul
  192. (if checkbox "[ ]" "")
  193. (if descp (concat (if checkbox " " "")
  194. (read-string "Term: ") " :: ") ""))
  195. (just-one-space)
  196. (setq pos (point))
  197. (end-of-line 1)
  198. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  199. (org-maybe-renumber-ordered-list)
  200. (and checkbox (org-update-checkbox-count-maybe))
  201. t)))
  202. ;;; Checkboxes
  203. (defun org-at-item-checkbox-p ()
  204. "Is point at a line starting a plain-list item with a checklet?"
  205. (and (org-at-item-p)
  206. (save-excursion
  207. (goto-char (match-end 0))
  208. (skip-chars-forward " \t")
  209. (looking-at "\\[[- X]\\]"))))
  210. (defun org-toggle-checkbox (&optional arg)
  211. "Toggle the checkbox in the current line."
  212. (interactive "P")
  213. (catch 'exit
  214. (let (beg end status (firstnew 'unknown))
  215. (cond
  216. ((org-region-active-p)
  217. (setq beg (region-beginning) end (region-end)))
  218. ((org-on-heading-p)
  219. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  220. ((org-at-item-checkbox-p)
  221. (let ((pos (point)))
  222. (replace-match
  223. (cond (arg "[-]")
  224. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  225. (t "[ ]"))
  226. t t)
  227. (goto-char pos))
  228. (throw 'exit t))
  229. (t (error "Not at a checkbox or heading, and no active region")))
  230. (save-excursion
  231. (goto-char beg)
  232. (while (< (point) end)
  233. (when (org-at-item-checkbox-p)
  234. (setq status (equal (match-string 0) "[X]"))
  235. (when (eq firstnew 'unknown)
  236. (setq firstnew (not status)))
  237. (replace-match
  238. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  239. (beginning-of-line 2)))))
  240. (org-update-checkbox-count-maybe))
  241. (defun org-update-checkbox-count-maybe ()
  242. "Update checkbox statistics unless turned off by user."
  243. (when org-provide-checkbox-statistics
  244. (org-update-checkbox-count)))
  245. (defun org-update-checkbox-count (&optional all)
  246. "Update the checkbox statistics in the current section.
  247. This will find all statistic cookies like [57%] and [6/12] and update them
  248. with the current numbers. With optional prefix argument ALL, do this for
  249. the whole buffer."
  250. (interactive "P")
  251. (save-excursion
  252. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  253. (beg (condition-case nil
  254. (progn (org-back-to-heading) (point))
  255. (error (point-min))))
  256. (end (move-marker (make-marker)
  257. (progn (outline-next-heading) (point))))
  258. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  259. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  260. (re-find (concat re "\\|" re-box))
  261. beg-cookie end-cookie is-percent c-on c-off lim
  262. eline curr-ind next-ind continue-from startsearch
  263. (cstat 0)
  264. )
  265. (when all
  266. (goto-char (point-min))
  267. (outline-next-heading)
  268. (setq beg (point) end (point-max)))
  269. (goto-char end)
  270. ;; find each statistic cookie
  271. (while (re-search-backward re-find beg t)
  272. (setq beg-cookie (match-beginning 1)
  273. end-cookie (match-end 1)
  274. cstat (+ cstat (if end-cookie 1 0))
  275. startsearch (point-at-eol)
  276. continue-from (point-at-bol)
  277. is-percent (match-beginning 2)
  278. lim (cond
  279. ((org-on-heading-p) (outline-next-heading) (point))
  280. ((org-at-item-p) (org-end-of-item) (point))
  281. (t nil))
  282. c-on 0
  283. c-off 0)
  284. (when lim
  285. ;; find first checkbox for this cookie and gather
  286. ;; statistics from all that are at this indentation level
  287. (goto-char startsearch)
  288. (if (re-search-forward re-box lim t)
  289. (progn
  290. (org-beginning-of-item)
  291. (setq curr-ind (org-get-indentation))
  292. (setq next-ind curr-ind)
  293. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  294. (save-excursion (end-of-line) (setq eline (point)))
  295. (if (re-search-forward re-box eline t)
  296. (if (member (match-string 2) '("[ ]" "[-]"))
  297. (setq c-off (1+ c-off))
  298. (setq c-on (1+ c-on))
  299. )
  300. )
  301. (org-end-of-item)
  302. (setq next-ind (org-get-indentation))
  303. )))
  304. (goto-char continue-from)
  305. ;; update cookie
  306. (when end-cookie
  307. (delete-region beg-cookie end-cookie)
  308. (goto-char beg-cookie)
  309. (insert
  310. (if is-percent
  311. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  312. (format "[%d/%d]" c-on (+ c-on c-off)))))
  313. ;; update items checkbox if it has one
  314. (when (org-at-item-p)
  315. (org-beginning-of-item)
  316. (when (and (> (+ c-on c-off) 0)
  317. (re-search-forward re-box (point-at-eol) t))
  318. (setq beg-cookie (match-beginning 2)
  319. end-cookie (match-end 2))
  320. (delete-region beg-cookie end-cookie)
  321. (goto-char beg-cookie)
  322. (cond ((= c-off 0) (insert "[X]"))
  323. ((= c-on 0) (insert "[ ]"))
  324. (t (insert "[-]")))
  325. )))
  326. (goto-char continue-from))
  327. (when (interactive-p)
  328. (message "Checkbox satistics updated %s (%d places)"
  329. (if all "in entire file" "in current outline entry") cstat)))))
  330. (defun org-get-checkbox-statistics-face ()
  331. "Select the face for checkbox statistics.
  332. The face will be `org-done' when all relevant boxes are checked. Otherwise
  333. it will be `org-todo'."
  334. (if (match-end 1)
  335. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  336. (if (and (> (match-end 2) (match-beginning 2))
  337. (equal (match-string 2) (match-string 3)))
  338. 'org-done
  339. 'org-todo)))
  340. (defun org-beginning-of-item ()
  341. "Go to the beginning of the current hand-formatted item.
  342. If the cursor is not in an item, throw an error."
  343. (interactive)
  344. (let ((pos (point))
  345. (limit (save-excursion
  346. (condition-case nil
  347. (progn
  348. (org-back-to-heading)
  349. (beginning-of-line 2) (point))
  350. (error (point-min)))))
  351. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  352. ind ind1)
  353. (if (org-at-item-p)
  354. (beginning-of-line 1)
  355. (beginning-of-line 1)
  356. (skip-chars-forward " \t")
  357. (setq ind (current-column))
  358. (if (catch 'exit
  359. (while t
  360. (beginning-of-line 0)
  361. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  362. (if (looking-at "[ \t]*$")
  363. (setq ind1 ind-empty)
  364. (skip-chars-forward " \t")
  365. (setq ind1 (current-column)))
  366. (if (< ind1 ind)
  367. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  368. nil
  369. (goto-char pos)
  370. (error "Not in an item")))))
  371. (defun org-end-of-item ()
  372. "Go to the end of the current hand-formatted item.
  373. If the cursor is not in an item, throw an error."
  374. (interactive)
  375. (let* ((pos (point))
  376. ind1
  377. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  378. (limit (save-excursion (outline-next-heading) (point)))
  379. (ind (save-excursion
  380. (org-beginning-of-item)
  381. (skip-chars-forward " \t")
  382. (current-column)))
  383. (end (catch 'exit
  384. (while t
  385. (beginning-of-line 2)
  386. (if (eobp) (throw 'exit (point)))
  387. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  388. (if (looking-at "[ \t]*$")
  389. (setq ind1 ind-empty)
  390. (skip-chars-forward " \t")
  391. (setq ind1 (current-column)))
  392. (if (<= ind1 ind)
  393. (throw 'exit (point-at-bol)))))))
  394. (if end
  395. (goto-char end)
  396. (goto-char pos)
  397. (error "Not in an item"))))
  398. (defun org-next-item ()
  399. "Move to the beginning of the next item in the current plain list.
  400. Error if not at a plain list, or if this is the last item in the list."
  401. (interactive)
  402. (let (ind ind1 (pos (point)))
  403. (org-beginning-of-item)
  404. (setq ind (org-get-indentation))
  405. (org-end-of-item)
  406. (setq ind1 (org-get-indentation))
  407. (unless (and (org-at-item-p) (= ind ind1))
  408. (goto-char pos)
  409. (error "On last item"))))
  410. (defun org-previous-item ()
  411. "Move to the beginning of the previous item in the current plain list.
  412. Error if not at a plain list, or if this is the first item in the list."
  413. (interactive)
  414. (let (beg ind ind1 (pos (point)))
  415. (org-beginning-of-item)
  416. (setq beg (point))
  417. (setq ind (org-get-indentation))
  418. (goto-char beg)
  419. (catch 'exit
  420. (while t
  421. (beginning-of-line 0)
  422. (if (looking-at "[ \t]*$")
  423. nil
  424. (if (<= (setq ind1 (org-get-indentation)) ind)
  425. (throw 'exit t)))))
  426. (condition-case nil
  427. (if (or (not (org-at-item-p))
  428. (< ind1 (1- ind)))
  429. (error "")
  430. (org-beginning-of-item))
  431. (error (goto-char pos)
  432. (error "On first item")))))
  433. (defun org-first-list-item-p ()
  434. "Is this heading the item in a plain list?"
  435. (unless (org-at-item-p)
  436. (error "Not at a plain list item"))
  437. (org-beginning-of-item)
  438. (= (point) (save-excursion (org-beginning-of-item-list))))
  439. (defun org-move-item-down ()
  440. "Move the plain list item at point down, i.e. swap with following item.
  441. Subitems (items with larger indentation) are considered part of the item,
  442. so this really moves item trees."
  443. (interactive)
  444. (let ((col (current-column))
  445. (pos (point))
  446. beg beg0 end end0 ind ind1 txt ne-end ne-beg)
  447. (org-beginning-of-item)
  448. (setq beg0 (point))
  449. (save-excursion
  450. (setq ne-beg (org-back-over-empty-lines))
  451. (setq beg (point)))
  452. (goto-char beg0)
  453. (setq ind (org-get-indentation))
  454. (org-end-of-item)
  455. (setq end0 (point))
  456. (setq ind1 (org-get-indentation))
  457. (setq ne-end (org-back-over-empty-lines))
  458. (setq end (point))
  459. (goto-char beg0)
  460. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  461. ;; include less whitespace
  462. (save-excursion
  463. (goto-char beg)
  464. (forward-line (- ne-beg ne-end))
  465. (setq beg (point))))
  466. (goto-char end0)
  467. (if (and (org-at-item-p) (= ind ind1))
  468. (progn
  469. (org-end-of-item)
  470. (org-back-over-empty-lines)
  471. (setq txt (buffer-substring beg end))
  472. (save-excursion
  473. (delete-region beg end))
  474. (setq pos (point))
  475. (insert txt)
  476. (goto-char pos) (org-skip-whitespace)
  477. (org-maybe-renumber-ordered-list)
  478. (move-to-column col))
  479. (goto-char pos)
  480. (move-to-column col)
  481. (error "Cannot move this item further down"))))
  482. (defun org-move-item-up (arg)
  483. "Move the plain list item at point up, i.e. swap with previous item.
  484. Subitems (items with larger indentation) are considered part of the item,
  485. so this really moves item trees."
  486. (interactive "p")
  487. (let ((col (current-column)) (pos (point))
  488. beg beg0 end ind ind1 txt
  489. ne-beg ne-ins ins-end)
  490. (org-beginning-of-item)
  491. (setq beg0 (point))
  492. (setq ind (org-get-indentation))
  493. (save-excursion
  494. (setq ne-beg (org-back-over-empty-lines))
  495. (setq beg (point)))
  496. (goto-char beg0)
  497. (org-end-of-item)
  498. (org-back-over-empty-lines)
  499. (setq end (point))
  500. (goto-char beg0)
  501. (catch 'exit
  502. (while t
  503. (beginning-of-line 0)
  504. (if (looking-at "[ \t]*$")
  505. (if org-empty-line-terminates-plain-lists
  506. (progn
  507. (goto-char pos)
  508. (error "Cannot move this item further up"))
  509. nil)
  510. (if (<= (setq ind1 (org-get-indentation)) ind)
  511. (throw 'exit t)))))
  512. (condition-case nil
  513. (org-beginning-of-item)
  514. (error (goto-char beg0)
  515. (move-to-column col)
  516. (error "Cannot move this item further up")))
  517. (setq ind1 (org-get-indentation))
  518. (if (and (org-at-item-p) (= ind ind1))
  519. (progn
  520. (setq ne-ins (org-back-over-empty-lines))
  521. (setq txt (buffer-substring beg end))
  522. (save-excursion
  523. (delete-region beg end))
  524. (setq pos (point))
  525. (insert txt)
  526. (setq ins-end (point))
  527. (goto-char pos) (org-skip-whitespace)
  528. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  529. ;; Move whitespace back to beginning
  530. (save-excursion
  531. (goto-char ins-end)
  532. (let ((kill-whole-line t))
  533. (kill-line (- ne-ins ne-beg)) (point)))
  534. (insert (make-string (- ne-ins ne-beg) ?\n)))
  535. (org-maybe-renumber-ordered-list)
  536. (move-to-column col))
  537. (goto-char pos)
  538. (move-to-column col)
  539. (error "Cannot move this item further up"))))
  540. (defun org-maybe-renumber-ordered-list ()
  541. "Renumber the ordered list at point if setup allows it.
  542. This tests the user option `org-auto-renumber-ordered-lists' before
  543. doing the renumbering."
  544. (interactive)
  545. (when (and org-auto-renumber-ordered-lists
  546. (org-at-item-p))
  547. (if (match-beginning 3)
  548. (org-renumber-ordered-list 1)
  549. (org-fix-bullet-type))))
  550. (defun org-maybe-renumber-ordered-list-safe ()
  551. (condition-case nil
  552. (save-excursion
  553. (org-maybe-renumber-ordered-list))
  554. (error nil)))
  555. (defun org-cycle-list-bullet (&optional which)
  556. "Cycle through the different itemize/enumerate bullets.
  557. This cycle the entire list level through the sequence:
  558. `-' -> `+' -> `*' -> `1.' -> `1)'
  559. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  560. 0 meand `-', 1 means `+' etc."
  561. (interactive "P")
  562. (org-preserve-lc
  563. (org-beginning-of-item-list)
  564. (org-at-item-p)
  565. (beginning-of-line 1)
  566. (let ((current (match-string 0))
  567. (prevp (eq which 'previous))
  568. new old)
  569. (setq new (cond
  570. ((and (numberp which)
  571. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  572. ((string-match "-" current) (if prevp "1)" "+"))
  573. ((string-match "\\+" current)
  574. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  575. ((string-match "\\*" current) (if prevp "+" "1."))
  576. ((string-match "\\." current)
  577. (if prevp (if (looking-at "\\S-") "+" "*") "1)"))
  578. ((string-match ")" current) (if prevp "1." "-"))
  579. (t (error "This should not happen"))))
  580. (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
  581. (setq old (match-string 2))
  582. (replace-match (concat "\\1" new)))
  583. (org-shift-item-indentation (- (length new) (length old)))
  584. (org-fix-bullet-type)
  585. (org-maybe-renumber-ordered-list))))
  586. (defun org-get-string-indentation (s)
  587. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  588. (let ((n -1) (i 0) (w tab-width) c)
  589. (catch 'exit
  590. (while (< (setq n (1+ n)) (length s))
  591. (setq c (aref s n))
  592. (cond ((= c ?\ ) (setq i (1+ i)))
  593. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  594. (t (throw 'exit t)))))
  595. i))
  596. (defun org-renumber-ordered-list (arg)
  597. "Renumber an ordered plain list.
  598. Cursor needs to be in the first line of an item, the line that starts
  599. with something like \"1.\" or \"2)\"."
  600. (interactive "p")
  601. (unless (and (org-at-item-p)
  602. (match-beginning 3))
  603. (error "This is not an ordered list"))
  604. (let ((line (org-current-line))
  605. (col (current-column))
  606. (ind (org-get-string-indentation
  607. (buffer-substring (point-at-bol) (match-beginning 3))))
  608. ;; (term (substring (match-string 3) -1))
  609. ind1 (n (1- arg))
  610. fmt bobp old new)
  611. ;; find where this list begins
  612. (org-beginning-of-item-list)
  613. (setq bobp (bobp))
  614. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  615. (setq fmt (concat "%d" (match-string 1)))
  616. (beginning-of-line 0)
  617. ;; walk forward and replace these numbers
  618. (catch 'exit
  619. (while t
  620. (catch 'next
  621. (if bobp (setq bobp nil) (beginning-of-line 2))
  622. (if (eobp) (throw 'exit nil))
  623. (if (looking-at "[ \t]*$") (throw 'next nil))
  624. (skip-chars-forward " \t") (setq ind1 (current-column))
  625. (if (> ind1 ind) (throw 'next t))
  626. (if (< ind1 ind) (throw 'exit t))
  627. (if (not (org-at-item-p)) (throw 'exit nil))
  628. (setq old (match-string 2))
  629. (delete-region (match-beginning 2) (match-end 2))
  630. (goto-char (match-beginning 2))
  631. (insert (setq new (format fmt (setq n (1+ n)))))
  632. (org-shift-item-indentation (- (length new) (length old))))))
  633. (goto-line line)
  634. (org-move-to-column col)))
  635. (defun org-fix-bullet-type ()
  636. "Make sure all items in this list have the same bullet as the first item.
  637. Also, fix the indentation."
  638. (interactive)
  639. (unless (org-at-item-p) (error "This is not a list"))
  640. (let ((line (org-current-line))
  641. (col (current-column))
  642. (ind (current-indentation))
  643. ind1 bullet oldbullet)
  644. ;; find where this list begins
  645. (org-beginning-of-item-list)
  646. (beginning-of-line 1)
  647. ;; find out what the bullet type is
  648. (looking-at "[ \t]*\\(\\S-+\\)")
  649. (setq bullet (concat (match-string 1) " "))
  650. (if (and org-list-two-spaces-after-bullet-regexp
  651. (string-match org-list-two-spaces-after-bullet-regexp bullet))
  652. (setq bullet (concat bullet " ")))
  653. ;; walk forward and replace these numbers
  654. (beginning-of-line 0)
  655. (catch 'exit
  656. (while t
  657. (catch 'next
  658. (beginning-of-line 2)
  659. (if (eobp) (throw 'exit nil))
  660. (if (looking-at "[ \t]*$") (throw 'next nil))
  661. (skip-chars-forward " \t") (setq ind1 (current-column))
  662. (if (> ind1 ind) (throw 'next t))
  663. (if (< ind1 ind) (throw 'exit t))
  664. (if (not (org-at-item-p)) (throw 'exit nil))
  665. (skip-chars-forward " \t")
  666. (looking-at "\\S-+ *")
  667. (setq oldbullet (match-string 0))
  668. (replace-match bullet)
  669. (org-shift-item-indentation (- (length bullet) (length oldbullet))))))
  670. (goto-line line)
  671. (org-move-to-column col)
  672. (if (string-match "[0-9]" bullet)
  673. (org-renumber-ordered-list 1))))
  674. (defun org-shift-item-indentation (delta)
  675. "Shift the indentation in current item by DELTA."
  676. (save-excursion
  677. (let ((beg (point-at-bol))
  678. (end (progn (org-end-of-item) (point)))
  679. i)
  680. (goto-char end)
  681. (beginning-of-line 0)
  682. (while (> (point) beg)
  683. (when (looking-at "[ \t]*\\S-")
  684. ;; this is not an empty line
  685. (setq i (org-get-indentation))
  686. (if (and (> i 0) (> (setq i (+ i delta)) 0))
  687. (indent-line-to i)))
  688. (beginning-of-line 0)))))
  689. (defun org-beginning-of-item-list ()
  690. "Go to the beginning of the current item list.
  691. I.e. to the first item in this list."
  692. (interactive)
  693. (org-beginning-of-item)
  694. (let ((pos (point-at-bol))
  695. (ind (org-get-indentation))
  696. ind1)
  697. ;; find where this list begins
  698. (catch 'exit
  699. (while t
  700. (catch 'next
  701. (beginning-of-line 0)
  702. (if (looking-at "[ \t]*$")
  703. (throw (if (bobp) 'exit 'next) t))
  704. (skip-chars-forward " \t") (setq ind1 (current-column))
  705. (if (or (< ind1 ind)
  706. (and (= ind1 ind)
  707. (not (org-at-item-p)))
  708. (and (= (point-at-bol) (point-min))
  709. (setq pos (point-min))))
  710. (throw 'exit t)
  711. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  712. (goto-char pos)))
  713. (defun org-end-of-item-list ()
  714. "Go to the end of the current item list.
  715. I.e. to the text after the last item."
  716. (interactive)
  717. (org-beginning-of-item)
  718. (let ((pos (point-at-bol))
  719. (ind (org-get-indentation))
  720. ind1)
  721. ;; find where this list begins
  722. (catch 'exit
  723. (while t
  724. (catch 'next
  725. (beginning-of-line 2)
  726. (if (looking-at "[ \t]*$")
  727. (throw (if (eobp) 'exit 'next) t))
  728. (skip-chars-forward " \t") (setq ind1 (current-column))
  729. (if (or (< ind1 ind)
  730. (and (= ind1 ind)
  731. (not (org-at-item-p)))
  732. (eobp))
  733. (progn
  734. (setq pos (point-at-bol))
  735. (throw 'exit t))))))
  736. (goto-char pos)))
  737. (defvar org-last-indent-begin-marker (make-marker))
  738. (defvar org-last-indent-end-marker (make-marker))
  739. (defun org-outdent-item (arg)
  740. "Outdent a local list item."
  741. (interactive "p")
  742. (org-indent-item (- arg)))
  743. (defun org-indent-item (arg)
  744. "Indent a local list item."
  745. (interactive "p")
  746. (unless (org-at-item-p)
  747. (error "Not on an item"))
  748. (save-excursion
  749. (let (beg end ind ind1 tmp delta ind-down ind-up)
  750. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  751. (setq beg org-last-indent-begin-marker
  752. end org-last-indent-end-marker)
  753. (org-beginning-of-item)
  754. (setq beg (move-marker org-last-indent-begin-marker (point)))
  755. (org-end-of-item)
  756. (setq end (move-marker org-last-indent-end-marker (point))))
  757. (goto-char beg)
  758. (setq tmp (org-item-indent-positions)
  759. ind (car tmp)
  760. ind-down (nth 2 tmp)
  761. ind-up (nth 1 tmp)
  762. delta (if (> arg 0)
  763. (if ind-down (- ind-down ind) 2)
  764. (if ind-up (- ind-up ind) -2)))
  765. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  766. (while (< (point) end)
  767. (beginning-of-line 1)
  768. (skip-chars-forward " \t") (setq ind1 (current-column))
  769. (delete-region (point-at-bol) (point))
  770. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  771. (beginning-of-line 2))))
  772. (org-fix-bullet-type)
  773. (org-maybe-renumber-ordered-list-safe)
  774. (save-excursion
  775. (beginning-of-line 0)
  776. (condition-case nil (org-beginning-of-item) (error nil))
  777. (org-maybe-renumber-ordered-list-safe)))
  778. (defun org-item-indent-positions ()
  779. "Return indentation for plain list items.
  780. This returns a list with three values: The current indentation, the
  781. parent indentation and the indentation a child should habe.
  782. Assumes cursor in item line."
  783. (let* ((bolpos (point-at-bol))
  784. (ind (org-get-indentation))
  785. ind-down ind-up pos)
  786. (save-excursion
  787. (org-beginning-of-item-list)
  788. (skip-chars-backward "\n\r \t")
  789. (when (org-in-item-p)
  790. (org-beginning-of-item)
  791. (setq ind-up (org-get-indentation))))
  792. (setq pos (point))
  793. (save-excursion
  794. (cond
  795. ((and (condition-case nil (progn (org-previous-item) t)
  796. (error nil))
  797. (or (forward-char 1) t)
  798. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  799. (setq ind-down (org-get-indentation)))
  800. ((and (goto-char pos)
  801. (org-at-item-p))
  802. (goto-char (match-end 0))
  803. (skip-chars-forward " \t")
  804. (setq ind-down (current-column)))))
  805. (list ind ind-up ind-down)))
  806. ;;; Send and receive lists
  807. (defun org-list-parse-list (&optional delete)
  808. "Parse the list at point and maybe DELETE it.
  809. Return a list containing first level items as strings and
  810. sublevels as a list of strings."
  811. (let* ((item-beginning (org-list-item-beginning))
  812. (start (car item-beginning))
  813. (end (org-list-end (cdr item-beginning)))
  814. output itemsep ltype)
  815. (while (re-search-forward org-list-beginning-re end t)
  816. (goto-char (match-beginning 3))
  817. (save-match-data
  818. (cond ((string-match "[0-9]" (match-string 2))
  819. (setq itemsep "[0-9]+\\(?:\\.\\|)\\)"
  820. ltype 'ordered))
  821. ((string-match "^.*::" (match-string 0))
  822. (setq itemsep "[-+]" ltype 'descriptive))
  823. (t (setq itemsep "[-+]" ltype 'unordered))))
  824. (let* ((indent1 (match-string 1))
  825. (nextitem (save-excursion
  826. (save-match-data
  827. (or (and (re-search-forward
  828. (concat "^" indent1 itemsep " *?") end t)
  829. (match-beginning 0)) end))))
  830. (item (buffer-substring
  831. (point)
  832. (or (and (re-search-forward
  833. org-list-beginning-re end t)
  834. (goto-char (match-beginning 0)))
  835. (goto-char end))))
  836. (nextindent (match-string 1))
  837. (item (org-trim item))
  838. (item (if (string-match "^\\[.+\\]" item)
  839. (replace-match "\\\\texttt{\\&}"
  840. t nil item) item)))
  841. (push item output)
  842. (when (> (length nextindent)
  843. (length indent1))
  844. (narrow-to-region (point) nextitem)
  845. (push (org-list-parse-list) output)
  846. (widen))))
  847. (when delete (delete-region start end))
  848. (setq output (nreverse output))
  849. (push ltype output)))
  850. (defun org-list-item-beginning ()
  851. "Find the beginning of the list item.
  852. Return a cons which car is the beginning position of the item and
  853. cdr is the indentation string."
  854. (save-excursion
  855. (if (not (or (looking-at org-list-beginning-re)
  856. (re-search-backward
  857. org-list-beginning-re nil t)))
  858. (progn (goto-char (point-min)) (point))
  859. (cons (match-beginning 0) (match-string 1)))))
  860. (defun org-list-end (indent)
  861. "Return the position of the end of the list.
  862. INDENT is the indentation of the list."
  863. (save-excursion
  864. (catch 'exit
  865. (while (or (looking-at org-list-beginning-re)
  866. (looking-at (concat "^" indent "[ \t]+\\|^$")))
  867. (if (eq (point) (point-max))
  868. (throw 'exit (point-max)))
  869. (forward-line 1))) (point)))
  870. (defun org-list-insert-radio-list ()
  871. "Insert a radio list template appropriate for this major mode."
  872. (interactive)
  873. (let* ((e (assq major-mode org-list-radio-list-templates))
  874. (txt (nth 1 e))
  875. name pos)
  876. (unless e (error "No radio list setup defined for %s" major-mode))
  877. (setq name (read-string "List name: "))
  878. (while (string-match "%n" txt)
  879. (setq txt (replace-match name t t txt)))
  880. (or (bolp) (insert "\n"))
  881. (setq pos (point))
  882. (insert txt)
  883. (goto-char pos)))
  884. (defun org-list-send-list (&optional maybe)
  885. "Send a tranformed version of this list to the receiver position.
  886. With argument MAYBE, fail quietly if no transformation is defined for
  887. this list."
  888. (interactive)
  889. (catch 'exit
  890. (unless (org-at-item-p) (error "Not at a list"))
  891. (save-excursion
  892. (goto-char (car (org-list-item-beginning)))
  893. (beginning-of-line 0)
  894. (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  895. (if maybe
  896. (throw 'exit nil)
  897. (error "Don't know how to transform this list"))))
  898. (let* ((name (match-string 1))
  899. (item-beginning (org-list-item-beginning))
  900. (transform (intern (match-string 2)))
  901. (txt (buffer-substring-no-properties
  902. (car item-beginning)
  903. (org-list-end (cdr item-beginning))))
  904. (list (org-list-parse-list))
  905. beg)
  906. (unless (fboundp transform)
  907. (error "No such transformation function %s" transform))
  908. (setq txt (funcall transform list))
  909. ;; Find the insertion place
  910. (save-excursion
  911. (goto-char (point-min))
  912. (unless (re-search-forward
  913. (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
  914. (error "Don't know where to insert translated list"))
  915. (goto-char (match-beginning 0))
  916. (beginning-of-line 2)
  917. (setq beg (point))
  918. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  919. (error "Cannot find end of insertion region"))
  920. (beginning-of-line 1)
  921. (delete-region beg (point))
  922. (goto-char beg)
  923. (insert txt "\n"))
  924. (message "List converted and installed at receiver location"))))
  925. (defun org-list-to-generic (list params)
  926. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  927. Valid parameters PARAMS are
  928. :ustart String to start an unordered list
  929. :uend String to end an unordered list
  930. :ostart String to start an ordered list
  931. :oend String to end an ordered list
  932. :dstart String to start a descriptive list
  933. :dend String to end a descriptive list
  934. :dtstart String to start a descriptive term
  935. :dtend String to end a descriptive term
  936. :ddstart String to start a description
  937. :ddend String to end a description
  938. :splice When set to t, return only list body lines, don't wrap
  939. them into :[u/o]start and :[u/o]end. Default is nil.
  940. :istart String to start a list item
  941. :iend String to end a list item
  942. :isep String to separate items
  943. :lsep String to separate sublists"
  944. (interactive)
  945. (let* ((p params) sublist
  946. (splicep (plist-get p :splice))
  947. (ostart (plist-get p :ostart))
  948. (oend (plist-get p :oend))
  949. (ustart (plist-get p :ustart))
  950. (uend (plist-get p :uend))
  951. (dstart (plist-get p :dstart))
  952. (dend (plist-get p :dend))
  953. (dtstart (plist-get p :dtstart))
  954. (dtend (plist-get p :dtend))
  955. (ddstart (plist-get p :ddstart))
  956. (ddend (plist-get p :ddend))
  957. (istart (plist-get p :istart))
  958. (iend (plist-get p :iend))
  959. (isep (plist-get p :isep))
  960. (lsep (plist-get p :lsep)))
  961. (let ((wrapper
  962. (cond ((eq (car list) 'ordered)
  963. (concat ostart "\n%s" oend "\n"))
  964. ((eq (car list) 'unordered)
  965. (concat ustart "\n%s" uend "\n"))
  966. ((eq (car list) 'descriptive)
  967. (concat dstart "\n%s" dend "\n"))))
  968. rtn term defstart defend)
  969. (while (setq sublist (pop list))
  970. (cond ((symbolp sublist) nil)
  971. ((stringp sublist)
  972. (when (string-match "^\\(.*\\) ::" sublist)
  973. (setq term (org-trim (format (concat dtstart "%s" dtend)
  974. (match-string 1 sublist))))
  975. (setq sublist (substring sublist (1+ (length term)))))
  976. (setq rtn (concat rtn istart term ddstart
  977. sublist ddend iend isep)))
  978. (t (setq rtn (concat rtn ;; previous list
  979. lsep ;; list separator
  980. (org-list-to-generic sublist p)
  981. lsep ;; list separator
  982. )))))
  983. (format wrapper rtn))))
  984. (defun org-list-to-latex (list)
  985. "Convert LIST into a LaTeX list."
  986. (org-list-to-generic
  987. list '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  988. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  989. :dstart "\\begin{description}" :dend "\\end{description}"
  990. :dtstart "[" :dtend "]"
  991. :ddstart "" :ddend ""
  992. :istart "\\item " :iend ""
  993. :isep "\n" :lsep "\n")))
  994. (defun org-list-to-html (list)
  995. "Convert LIST into a HTML list."
  996. (org-list-to-generic
  997. list '(:splicep nil :ostart "<ol>" :oend "</ol>"
  998. :ustart "<ul>" :uend "</ul>"
  999. :dstart "<dl>" :dend "</dl>"
  1000. :dtstart "<dt>" :dtend "</dt>"
  1001. :ddstart "<dd>" :ddend "</dd>"
  1002. :istart "<li>" :iend "</li>"
  1003. :isep "\n" :lsep "\n")))
  1004. (defun org-list-to-texinfo (list)
  1005. "Convert LIST into a Texinfo list."
  1006. (org-list-to-generic
  1007. list '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  1008. :ustart "@enumerate" :uend "@end enumerate"
  1009. :dstart "@table" :dend "@end table"
  1010. :dtstart "@item " :dtend "\n"
  1011. :ddstart "" :ddend ""
  1012. :istart "@item\n" :iend ""
  1013. :isep "\n" :lsep "\n")))
  1014. (provide 'org-list)
  1015. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  1016. ;;; org-list.el ends here