allout-widgets.el 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  1. ;; allout-widgets.el --- Visually highlight allout outline structure.
  2. ;; Copyright (C) 2005-2017 Free Software Foundation, Inc.
  3. ;; Author: Ken Manheimer <ken dot manheimer at gmail...>
  4. ;; Maintainer: Ken Manheimer <ken dot manheimer at gmail...>
  5. ;; Version: 1.0
  6. ;; Created: Dec 2005
  7. ;; Keywords: outlines
  8. ;; Website: http://myriadicity.net/software-and-systems/craft/emacs-allout
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; This is an allout outline-mode add-on that highlights outline structure
  22. ;; with graphical widgets.
  23. ;;
  24. ;; To activate, customize `allout-widgets-auto-activation'. You can also
  25. ;; invoke allout-widgets-mode in a particular allout buffer. When
  26. ;; auto-enabled, you can inhibit widget operation in particular allout
  27. ;; buffers by setting the variable `allout-widgets-mode-inhibit' non-nil in
  28. ;; that file's buffer. Use emacs *file local variables* to generally
  29. ;; inhibit for a file.
  30. ;;
  31. ;; See the `allout-widgets-mode' docstring for more details.
  32. ;;
  33. ;; Info about allout and allout-widgets development are available at
  34. ;; http://myriadicity.net/Sundry/EmacsAllout
  35. ;;
  36. ;; The graphics include:
  37. ;;
  38. ;; - icons for item bullets, varying to distinguish whether the item either
  39. ;; lacks any subitems, the subitems are currently collapsed within the
  40. ;; item, or the item is currently expanded.
  41. ;;
  42. ;; - guide lines connecting item bullet-icons with those of their subitems.
  43. ;;
  44. ;; - cue area between the bullet-icon and the start of the body headline,
  45. ;; for item numbering, encryption indicator, and distinctive bullets.
  46. ;;
  47. ;; The bullet-icon and guide line graphics provide keybindings and mouse
  48. ;; bindings for easy outline navigation and exposure control, extending
  49. ;; outline hot-spot navigation (see `allout-mode' docstring for details).
  50. ;;
  51. ;; Developers note: Our use of emacs widgets is unconventional. We
  52. ;; decorate existing text rather than substituting for it, to
  53. ;; piggy-back on existing allout operation. This employs the C-coded
  54. ;; efficiencies of widget-apply, widget-get, and widget-put, along
  55. ;; with the basic object-oriented organization of widget-create, to
  56. ;; systematically couple overlays, graphics, and other features with
  57. ;; allout-governed text.
  58. ;;; Code:
  59. ;;;_ : General Environment
  60. (require 'allout)
  61. (require 'widget)
  62. (require 'wid-edit)
  63. (eval-when-compile
  64. (progn
  65. (require 'overlay)
  66. (require 'cl)
  67. ))
  68. ;;;_ : internal variables needed before user-customization variables
  69. ;;; In order to enable activation of allout-widgets-mode via customization,
  70. ;;; allout-widgets-auto-activation uses a setting function. That function
  71. ;;; is invoked when the customization variable definition is evaluated,
  72. ;;; during file load, so the involved code must reside above that
  73. ;;; definition in the file.
  74. ;;;_ = allout-widgets-mode
  75. (defvar allout-widgets-mode nil
  76. "Allout mode enhanced with graphical widgets.")
  77. (make-variable-buffer-local 'allout-widgets-mode)
  78. ;;;_ : USER CUSTOMIZATION VARIABLES and incidental functions:
  79. ;;;_ > defgroup allout-widgets
  80. (defgroup allout-widgets nil
  81. "Allout extension that highlights outline structure graphically.
  82. Customize `allout-widgets-auto-activation' to activate allout-widgets
  83. with allout-mode."
  84. :group 'allout)
  85. ;;;_ > defgroup allout-widgets-developer
  86. (defgroup allout-widgets-developer nil
  87. "Settings for development of allout widgets extension."
  88. :group 'allout-widgets)
  89. ;;;_ ; some functions a bit early, for allout-auto-activation dependency:
  90. ;;;_ > allout-widgets-mode-enable
  91. (defun allout-widgets-mode-enable ()
  92. "Enable allout-widgets-mode in allout-mode buffers.
  93. See `allout-widgets-mode-inhibit' for per-file/per-buffer
  94. inhibition of allout-widgets-mode."
  95. (add-hook 'allout-mode-off-hook 'allout-widgets-mode-off)
  96. (add-hook 'allout-mode-on-hook 'allout-widgets-mode-on)
  97. t)
  98. ;;;_ > allout-widgets-mode-disable
  99. (defun allout-widgets-mode-disable ()
  100. "Disable allout-widgets-mode in allout-mode buffers.
  101. See `allout-widgets-mode-inhibit' for per-file/per-buffer
  102. inhibition of allout-widgets-mode."
  103. (remove-hook 'allout-mode-off-hook 'allout-widgets-mode-off)
  104. (remove-hook 'allout-mode-on-hook 'allout-widgets-mode-on)
  105. t)
  106. ;;;_ > allout-widgets-setup (varname value)
  107. ;;;###autoload
  108. (defun allout-widgets-setup (varname value)
  109. "Commission or decommission allout-widgets-mode along with allout-mode.
  110. Meant to be used by customization of `allout-widgets-auto-activation'."
  111. (set-default varname value)
  112. (if allout-widgets-auto-activation
  113. (allout-widgets-mode-enable)
  114. (allout-widgets-mode-disable)))
  115. ;;;_ = allout-widgets-auto-activation
  116. ;;;###autoload
  117. (defcustom allout-widgets-auto-activation nil
  118. "Activate to enable allout icon graphics wherever allout mode is active.
  119. Also enable `allout-auto-activation' for this to take effect upon
  120. visiting an outline.
  121. When this is set you can disable allout widgets in select files
  122. by setting `allout-widgets-mode-inhibit'
  123. Instead of setting `allout-widgets-auto-activation' you can
  124. explicitly invoke `allout-widgets-mode' in allout buffers where
  125. you want allout widgets operation.
  126. See `allout-widgets-mode' for allout widgets mode features."
  127. :version "24.1"
  128. :type 'boolean
  129. :group 'allout-widgets
  130. :set 'allout-widgets-setup
  131. )
  132. ;; ;;;_ = allout-widgets-allow-unruly-edits
  133. ;; (defcustom allout-widgets-allow-unruly-edits nil
  134. ;; "Control whether manual edits are restricted to maintain outline integrity.
  135. ;; When nil, manual edits must either be within an item's body or encompass
  136. ;; one or more items completely - eg, killing topics as entities, rather than
  137. ;; deleting from the middle of one to the middle of another.
  138. ;; If you only occasionally need to make unrestricted change, you can set this
  139. ;; variable in the specific buffer using set-variable, or just deactivate
  140. ;; `allout-mode' temporarily. You can customize this to always allow unruly
  141. ;; edits, but you will be able to create outlines that are unnavigable in
  142. ;; principle, and not just for allout's navigation and exposure mechanisms."
  143. ;; :type 'boolean
  144. ;; :group allout-widgets)
  145. ;; (make-variable-buffer-local 'allout-widgets-allow-unruly-edits)
  146. ;;;_ = allout-widgets-auto-activation - below, for eval-order dependencies
  147. ;;;_ = allout-widgets-icons-dark-subdir
  148. (defcustom allout-widgets-icons-dark-subdir "icons/allout-widgets/dark-bg/"
  149. "Directory on `image-load-path' holding allout icons for dark backgrounds."
  150. :version "24.1"
  151. :type 'string
  152. :group 'allout-widgets)
  153. ;;;_ = allout-widgets-icons-light-subdir
  154. (defcustom allout-widgets-icons-light-subdir "icons/allout-widgets/light-bg/"
  155. "Directory on `image-load-path' holding allout icons for light backgrounds."
  156. :version "24.1"
  157. :type 'string
  158. :group 'allout-widgets)
  159. ;;;_ = allout-widgets-icon-types
  160. (defcustom allout-widgets-icon-types '(xpm png)
  161. "File extensions for the icon graphic format types, in order of preference."
  162. :version "24.1"
  163. :type '(repeat symbol)
  164. :group 'allout-widgets)
  165. ;;;_ . Decoration format
  166. ;;;_ = allout-widgets-theme-dark-background
  167. (defcustom allout-widgets-theme-dark-background "allout-dark-bg"
  168. "Identify the outline's icon theme to use with a dark background."
  169. :version "24.1"
  170. :type '(string)
  171. :group 'allout-widgets)
  172. ;;;_ = allout-widgets-theme-light-background
  173. (defcustom allout-widgets-theme-light-background "allout-light-bg"
  174. "Identify the outline's icon theme to use with a light background."
  175. :version "24.1"
  176. :type '(string)
  177. :group 'allout-widgets)
  178. ;;;_ = allout-widgets-item-image-properties-emacs
  179. (defcustom allout-widgets-item-image-properties-emacs
  180. '(:ascent center :mask (heuristic t))
  181. "Default properties item widget images in mainline Emacs."
  182. :version "24.1"
  183. :type 'plist
  184. :group 'allout-widgets)
  185. ;;;_ = allout-widgets-item-image-properties-xemacs
  186. (defcustom allout-widgets-item-image-properties-xemacs
  187. nil
  188. "Default properties item widget images in XEmacs."
  189. :version "24.1"
  190. :type 'plist
  191. :group 'allout-widgets)
  192. ;;;_ . Developer
  193. ;;;_ = allout-widgets-run-unit-tests-on-load
  194. (defcustom allout-widgets-run-unit-tests-on-load nil
  195. "When non-nil, unit tests will be run at end of loading allout-widgets.
  196. Generally, allout widgets code developers are the only ones who'll want to
  197. set this.
  198. \(If set, this makes it an even better practice to exercise changes by
  199. doing byte-compilation with a repeat count, so the file is loaded after
  200. compilation.)
  201. See `allout-widgets-run-unit-tests' to see what's run."
  202. :version "24.1"
  203. :type 'boolean
  204. :group 'allout-widgets-developer)
  205. ;;;_ = allout-widgets-time-decoration-activity
  206. (defcustom allout-widgets-time-decoration-activity nil
  207. "Retain timing info of the last cooperative redecoration.
  208. The details are retained as the value of
  209. `allout-widgets-last-decoration-timing'.
  210. Generally, allout widgets code developers are the only ones who'll want to
  211. set this."
  212. :version "24.1"
  213. :type 'boolean
  214. :group 'allout-widgets-developer)
  215. ;;;_ = allout-widgets-hook-error-post-time 0
  216. (defcustom allout-widgets-hook-error-post-time 0
  217. "Amount of time to sit showing hook error messages.
  218. 0 is minimal, or nil to not post to the message area.
  219. This is for debugging purposes."
  220. :version "24.1"
  221. :type 'integer
  222. :group 'allout-widgets-developer)
  223. ;;;_ = allout-widgets-maintain-tally nil
  224. (defcustom allout-widgets-maintain-tally nil
  225. "If non-nil, maintain a collection of widgets, `allout-widgets-tally'.
  226. This is for debugging purposes.
  227. The tally shows the total number of item widgets in the current
  228. buffer, and tracking increases as new widgets are added and
  229. decreases as obsolete widgets are garbage collected."
  230. :version "24.1"
  231. :type 'boolean
  232. :group 'allout-widgets-developer)
  233. (defvar allout-widgets-tally nil
  234. "Hash-table of existing allout widgets, for debugging.
  235. Table is maintained only if `allout-widgets-maintain-tally' is non-nil.
  236. The table contents will be out of sync if any widgets are created
  237. or deleted while this variable is nil.")
  238. (make-variable-buffer-local 'allout-widgets-tally)
  239. (defvar allout-widgets-mode-inhibit) ; defined below
  240. ;;;_ > allout-widgets-tally-string
  241. (defun allout-widgets-tally-string ()
  242. "Return a string giving the number of tracked widgets, or empty string if not tracking.
  243. The string is formed for appending to the allout-mode mode-line lighter.
  244. An empty string is also returned if tracking is inhibited or
  245. widgets are locally inhibited.
  246. The number varies according to the evanescence of objects on a
  247. hash table with weak keys, so tracking of widget erasures is often delayed."
  248. (when (and allout-widgets-maintain-tally
  249. (not allout-widgets-mode-inhibit)
  250. allout-widgets-tally)
  251. (format ":%s" (hash-table-count allout-widgets-tally))))
  252. ;;;_ = allout-widgets-track-decoration nil
  253. (defcustom allout-widgets-track-decoration nil
  254. "If non-nil, show cursor position of each item decoration.
  255. This is for debugging purposes, and generally set at need in a
  256. buffer rather than as a prevailing configuration (but it's handy
  257. to publicize it by making it a customization variable)."
  258. :version "24.1"
  259. :type 'boolean
  260. :group 'allout-widgets-developer)
  261. (make-variable-buffer-local 'allout-widgets-track-decoration)
  262. ;;;_ : Mode context - variables, hookup, and hooks
  263. ;;;_ . internal mode variables
  264. ;;;_ , Mode activation and environment
  265. ;;;_ = allout-widgets-version
  266. (defvar allout-widgets-version "1.0"
  267. "Version of currently loaded allout-widgets extension.")
  268. ;;;_ > allout-widgets-version
  269. (defun allout-widgets-version (&optional here)
  270. "Return string describing the loaded outline version."
  271. (interactive "P")
  272. (let ((msg (concat "Allout Outline Widgets Extension v "
  273. allout-widgets-version)))
  274. (if here (insert msg))
  275. (message "%s" msg)
  276. msg))
  277. ;;;_ = allout-widgets-mode-inhibit
  278. (defvar allout-widgets-mode-inhibit nil
  279. "Inhibit `allout-widgets-mode' from activating widgets.
  280. This also inhibits automatic adjustment of widgets to track allout outline
  281. changes.
  282. You can use this as a file local variable setting to disable
  283. allout widgets enhancements in selected buffers while generally
  284. enabling widgets by customizing `allout-widgets-auto-activation'.
  285. In addition, you can invoked `allout-widgets-mode' allout-mode
  286. buffers where this is set to enable and disable widget
  287. enhancements, directly.")
  288. ;;;###autoload
  289. (put 'allout-widgets-mode-inhibit 'safe-local-variable
  290. (if (fboundp 'booleanp) 'booleanp (lambda (x) (member x '(t nil)))))
  291. (make-variable-buffer-local 'allout-widgets-mode-inhibit)
  292. ;;;_ = allout-inhibit-body-modification-hook
  293. (defvar allout-inhibit-body-modification-hook nil
  294. "Override de-escaping of text-prefixes in item bodies during specific changes.
  295. This is used by `allout-buffer-modification-handler' to signal such changes
  296. to `allout-body-modification-handler', and is always reset by
  297. `allout-post-command-business'.")
  298. (make-variable-buffer-local 'allout-inhibit-body-modification-hook)
  299. ;;;_ = allout-widgets-icons-cache
  300. (defvar allout-widgets-icons-cache nil
  301. "Cache allout icon images, as an association list.
  302. `allout-fetch-icon-image' uses this cache transparently, keying
  303. images with lists containing the name of the icon directory (as
  304. found on the `load-path') and the icon name.
  305. Set this variable to nil to empty the cache, and have it replenish from the
  306. filesystem.")
  307. ;;;_ = allout-widgets-unset-inhibit-read-only
  308. (defvar allout-widgets-unset-inhibit-read-only nil
  309. "Tell `allout-widgets-post-command-business' to unset `inhibit-read-only'.
  310. Used by `allout-graphics-modification-handler'")
  311. ;;;_ = allout-widgets-reenable-before-change-handler
  312. (defvar allout-widgets-reenable-before-change-handler nil
  313. "Tell `allout-widgets-post-command-business' to reequip the handler.
  314. Necessary because the handler sometimes deliberately raises an
  315. error, causing it to be disabled.")
  316. ;;;_ , State for hooks
  317. ;;;_ = allout-unresolved-body-mod-workroster
  318. (defvar allout-unresolved-body-mod-workroster (make-hash-table :size 16)
  319. "List of body-overlays that did before-change business but not after-change.
  320. See `allout-post-command-business' and `allout-body-modification-handler'.")
  321. ;;;_ = allout-structure-unruly-deletion-message
  322. (defvar allout-structure-unruly-deletion-message
  323. "Unruly edit prevented --
  324. To change the bullet character: \\[allout-rebullet-current-heading]
  325. To promote this item: \\[allout-shift-out]
  326. To demote it: \\[allout-shift-in]
  327. To delete it and offspring: \\[allout-kill-topic]
  328. See \\[describe-mode] for many more options."
  329. "Informative message presented on improper editing of outline structure.
  330. The structure includes the guides lines, bullet, and bullet cue.")
  331. ;;;_ = allout-widgets-changes-record
  332. (defvar allout-widgets-changes-record nil
  333. "Record outline changes for processing by post-command hook.
  334. Entries on the list are lists whose first element is a symbol indicating
  335. the change type and subsequent elements are data specific to that change
  336. type. For example:
  337. (exposure ALLOUT-EXPOSURE-FROM ALLOUT-EXPOSURE-TO ALLOUT-EXPOSURE-FLAG)
  338. The changes are recorded in reverse order, with new values pushed
  339. onto the front.")
  340. (make-variable-buffer-local 'allout-widgets-changes-record)
  341. ;;;_ = allout-widgets-undo-exposure-record
  342. (defvar allout-widgets-undo-exposure-record nil
  343. "Record outline undo traces for processing by post-command hook.
  344. The changes are recorded in reverse order, with new values pushed
  345. onto the front.")
  346. (make-variable-buffer-local 'allout-widgets-undo-exposure-record)
  347. ;;;_ = allout-widgets-last-hook-error
  348. (defvar allout-widgets-last-hook-error nil
  349. "String holding last error string, for debugging purposes.")
  350. ;;;_ = allout-widgets-adjust-message-length-threshold 100
  351. (defvar allout-widgets-adjust-message-length-threshold 100
  352. "Display \"Adjusting widgets\" message above this number of pending changes."
  353. )
  354. ;;;_ = allout-widgets-adjust-message-size-threshold 10000
  355. (defvar allout-widgets-adjust-message-size-threshold 10000
  356. "Display \"Adjusting widgets\" message above this size of pending changes."
  357. )
  358. ;;;_ = allout-doing-exposure-undo-processor nil
  359. (defvar allout-undo-exposure-in-progress nil
  360. "Maintained true during `allout-widgets-exposure-undo-processor'")
  361. ;;;_ , Widget-specific outline text format
  362. ;;;_ = allout-escaped-prefix-regexp
  363. (defvar allout-escaped-prefix-regexp ""
  364. "Regular expression for body text that would look like an item prefix if
  365. not altered with an escape sequence.")
  366. (make-variable-buffer-local 'allout-escaped-prefix-regexp)
  367. ;;;_ , Widget element formatting
  368. ;;;_ = allout-item-icon-keymap
  369. (defvar allout-item-icon-keymap
  370. (let ((km (make-sparse-keymap)))
  371. (dolist (digit '("0" "1" "2" "3"
  372. "4" "5" "6" "7" "8" "9"))
  373. (define-key km digit 'digit-argument))
  374. (define-key km "-" 'negative-argument)
  375. ;; (define-key km [(return)] 'allout-tree-expand-command)
  376. ;; (define-key km [(meta return)] 'allout-toggle-torso-command)
  377. ;; (define-key km [(down-mouse-1)] 'allout-item-button-click)
  378. ;; (define-key km [(down-mouse-2)] 'allout-toggle-torso-event-command)
  379. ;; Override underlying mouse-1 and mouse-2 bindings in icon territory:
  380. (define-key km [(mouse-1)] (lambda () (interactive) nil))
  381. (define-key km [(mouse-2)] (lambda () (interactive) nil))
  382. ;; Catchall, handles actual keybindings, dynamically doing keymap lookups:
  383. (define-key km [t] 'allout-item-icon-key-handler)
  384. km)
  385. "General tree-node key bindings.")
  386. ;;;_ = allout-item-body-keymap
  387. (defvar allout-item-body-keymap
  388. (let ((km (make-sparse-keymap))
  389. (local-map (current-local-map)))
  390. ;; (define-key km [(control return)] 'allout-tree-expand-command)
  391. ;; (define-key km [(meta return)] 'allout-toggle-torso-command)
  392. ;; XXX We need to reset this per buffer's mode; we do so in
  393. ;; allout-widgets-mode.
  394. (if local-map
  395. (set-keymap-parent km local-map))
  396. km)
  397. "General key bindings for the text content of outline items.")
  398. (make-variable-buffer-local 'allout-item-body-keymap)
  399. ;;;_ = allout-body-span-category
  400. (defvar allout-body-span-category nil
  401. "Symbol carrying allout body-text overlay properties.")
  402. ;;;_ = allout-cue-span-keymap
  403. (defvar allout-cue-span-keymap
  404. (let ((km (make-sparse-keymap)))
  405. (set-keymap-parent km allout-item-icon-keymap)
  406. km)
  407. "Keymap used in the item cue area - the space between the icon and headline.")
  408. ;;;_ = allout-escapes-category
  409. (defvar allout-escapes-category nil
  410. "Symbol for category of text property used to hide escapes of prefix-like
  411. text in allout item bodies.")
  412. ;;;_ = allout-guides-category
  413. (defvar allout-guides-category nil
  414. "Symbol carrying allout icon-guides overlay properties.")
  415. ;;;_ = allout-guides-span-category
  416. (defvar allout-guides-span-category nil
  417. "Symbol carrying allout icon and guide lines overlay properties.")
  418. ;;;_ = allout-icon-span-category
  419. (defvar allout-icon-span-category nil
  420. "Symbol carrying allout icon and guide lines overlay properties.")
  421. ;;;_ = allout-cue-span-category
  422. (defvar allout-cue-span-category nil
  423. "Symbol carrying common properties of the space following the outline icon.
  424. \(That space is used to convey selected cues indicating body qualities,
  425. including things like:
  426. - encryption `~'
  427. - numbering `#'
  428. - indirect reference `@'
  429. - distinctive bullets - see `allout-distinctive-bullets-string'.)")
  430. ;;;_ = allout-span-to-category
  431. (defvar allout-span-to-category
  432. '((:guides-span . allout-guides-span-category)
  433. (:cue-span . allout-cue-span-category)
  434. (:icon-span . allout-icon-span-category)
  435. (:body-span . allout-body-span-category))
  436. "Association list mapping span identifier to category identifier.")
  437. ;;;_ = allout-trailing-category
  438. (defvar allout-trailing-category nil
  439. "Symbol carrying common properties of an overlay's trailing newline.")
  440. ;;;_ , Developer
  441. (defvar allout-widgets-last-decoration-timing nil
  442. "Timing details for the last cooperative decoration action.
  443. This is maintained when `allout-widgets-time-decoration-activity' is set.
  444. The value is a list containing two elements:
  445. - the elapsed time as a number of seconds
  446. - the list of changes processed, a la `allout-widgets-changes-record'.
  447. When active, the value is revised each time automatic decoration activity
  448. happens in the buffer.")
  449. (make-variable-buffer-local 'allout-widgets-last-decoration-timing)
  450. ;;;_ . mode hookup
  451. ;;;_ > define-minor-mode allout-widgets-mode (arg)
  452. ;;;###autoload
  453. (define-minor-mode allout-widgets-mode
  454. "Toggle Allout Widgets mode.
  455. With a prefix argument ARG, enable Allout Widgets mode if ARG is
  456. positive, and disable it otherwise. If called from Lisp, enable
  457. the mode if ARG is omitted or nil.
  458. Allout Widgets mode is an extension of Allout mode that provides
  459. graphical decoration of outline structure. It is meant to
  460. operate along with `allout-mode', via `allout-mode-hook'.
  461. The graphics include:
  462. - guide lines connecting item bullet-icons with those of their subitems.
  463. - icons for item bullets, varying to indicate whether or not the item
  464. has subitems, and if so, whether or not the item is expanded.
  465. - cue area between the bullet-icon and the start of the body headline,
  466. for item numbering, encryption indicator, and distinctive bullets.
  467. The bullet-icon and guide line graphics provide keybindings and mouse
  468. bindings for easy outline navigation and exposure control, extending
  469. outline hot-spot navigation (see `allout-mode')."
  470. :lighter nil
  471. :keymap nil
  472. ;; define-minor-mode handles any provided argument according to emacs
  473. ;; minor-mode conventions - '(elisp) Minor Mode Conventions' - and sets
  474. ;; allout-widgets-mode accordingly *before* running the body code, so we
  475. ;; cue on that.
  476. (if allout-widgets-mode
  477. ;; Activating:
  478. (progn
  479. (allout-add-resumptions
  480. ;; XXX user may need say in line-truncation/hscrolling - an option
  481. ;; that abstracts mode.
  482. ;; truncate text lines to keep guide lines intact:
  483. '(truncate-lines t)
  484. ;; and enable autoscrolling to ease view of text
  485. '(auto-hscroll-mode t)
  486. '(line-move-ignore-fields t)
  487. '(widget-push-button-prefix "")
  488. '(widget-push-button-suffix "")
  489. ;; allout-escaped-prefix-regexp depends on allout-regexp:
  490. (list 'allout-escaped-prefix-regexp (concat "\\(\\\\\\)"
  491. "\\(" allout-regexp "\\)")))
  492. (allout-add-resumptions
  493. (list 'allout-widgets-tally allout-widgets-tally)
  494. (list 'allout-widgets-escapes-sanitization-regexp-pair
  495. (list (concat "\\(\n\\|\\`\\)"
  496. allout-escaped-prefix-regexp
  497. )
  498. ;; Include everything but the escape symbol.
  499. "\\1\\3"))
  500. )
  501. (add-hook 'after-change-functions 'allout-widgets-after-change-handler
  502. nil t)
  503. (allout-setup-text-properties)
  504. (add-to-invisibility-spec '(allout-torso . t))
  505. (add-to-invisibility-spec 'allout-escapes)
  506. (if (current-local-map)
  507. (set-keymap-parent allout-item-body-keymap (current-local-map)))
  508. (add-hook 'allout-exposure-change-functions
  509. 'allout-widgets-exposure-change-recorder nil 'local)
  510. (add-hook 'allout-structure-added-functions
  511. 'allout-widgets-additions-recorder nil 'local)
  512. (add-hook 'allout-structure-deleted-functions
  513. 'allout-widgets-deletions-recorder nil 'local)
  514. (add-hook 'allout-structure-shifted-functions
  515. 'allout-widgets-shifts-recorder nil 'local)
  516. (add-hook 'allout-after-copy-or-kill-hook
  517. 'allout-widgets-after-copy-or-kill-function nil 'local)
  518. (add-hook 'allout-post-undo-hook
  519. 'allout-widgets-after-undo-function nil 'local)
  520. (add-hook 'before-change-functions 'allout-widgets-before-change-handler
  521. nil 'local)
  522. (add-hook 'post-command-hook 'allout-widgets-post-command-business
  523. nil 'local)
  524. (add-hook 'pre-command-hook 'allout-widgets-pre-command-business
  525. nil 'local)
  526. ;; init the widgets tally for debugging:
  527. (if (not allout-widgets-tally)
  528. (setq allout-widgets-tally (make-hash-table
  529. :test 'eq :weakness 'key)))
  530. ;; add tally count display on minor-mode-alist just after
  531. ;; allout-mode entry.
  532. ;; (we use ternary condition form to keep condition simple for deletion.)
  533. (let* ((mode-line-entry '(allout-widgets-mode-inhibit ""
  534. (:eval (allout-widgets-tally-string))))
  535. (associated (assoc (car mode-line-entry) minor-mode-alist))
  536. ;; need location for it only if not already present:
  537. (after (and (not associated)
  538. (memq (assq 'allout-mode minor-mode-alist) minor-mode-alist))))
  539. (if after
  540. (rplacd after (cons mode-line-entry (cdr after)))))
  541. (allout-widgets-prepopulate-buffer)
  542. t)
  543. ;; Deactivating:
  544. (let ((inhibit-read-only t)
  545. (was-modified (buffer-modified-p)))
  546. (allout-widgets-undecorate-region (point-min)(point-max))
  547. (remove-from-invisibility-spec '(allout-torso . t))
  548. (remove-from-invisibility-spec 'allout-escapes)
  549. (remove-hook 'after-change-functions
  550. 'allout-widgets-after-change-handler 'local)
  551. (remove-hook 'allout-exposure-change-functions
  552. 'allout-widgets-exposure-change-recorder 'local)
  553. (remove-hook 'allout-structure-added-functions
  554. 'allout-widgets-additions-recorder 'local)
  555. (remove-hook 'allout-structure-deleted-functions
  556. 'allout-widgets-deletions-recorder 'local)
  557. (remove-hook 'allout-structure-shifted-functions
  558. 'allout-widgets-shifts-recorder 'local)
  559. (remove-hook 'allout-after-copy-or-kill-hook
  560. 'allout-widgets-after-copy-or-kill-function 'local)
  561. (remove-hook 'before-change-functions
  562. 'allout-widgets-before-change-handler 'local)
  563. (remove-hook 'post-command-hook
  564. 'allout-widgets-post-command-business 'local)
  565. (remove-hook 'pre-command-hook
  566. 'allout-widgets-pre-command-business 'local)
  567. (assq-delete-all 'allout-widgets-mode-inhibit minor-mode-alist)
  568. (set-buffer-modified-p was-modified))))
  569. ;;;_ > allout-widgets-mode-off
  570. (defun allout-widgets-mode-off ()
  571. "Explicitly disable `allout-widgets-mode'."
  572. (allout-widgets-mode -1))
  573. ;;;_ > allout-widgets-mode-off
  574. (defun allout-widgets-mode-on ()
  575. "Explicitly enable `allout-widgets-mode'."
  576. (allout-widgets-mode 1))
  577. ;;;_ > allout-setup-text-properties ()
  578. (defun allout-setup-text-properties ()
  579. "Configure category and literal text properties."
  580. ;; XXX body - before-change, entry, keymap
  581. (setplist 'allout-guides-span-category nil)
  582. (put 'allout-guides-span-category
  583. 'modification-hooks '(allout-graphics-modification-handler))
  584. (put 'allout-guides-span-category 'local-map allout-item-icon-keymap)
  585. (put 'allout-guides-span-category 'mouse-face widget-button-face)
  586. (put 'allout-guides-span-category 'field 'structure)
  587. ;; (put 'allout-guides-span-category 'face 'widget-button)
  588. (setplist 'allout-icon-span-category
  589. (allout-widgets-copy-list (symbol-plist
  590. 'allout-guides-span-category)))
  591. (put 'allout-icon-span-category 'field 'structure)
  592. ;; XXX for body text we're instead going to use the buffer-wide
  593. ;; resources, like before/after-change-functions hooks and the
  594. ;; buffer's key map. that way we won't have to do painful provisions
  595. ;; to fixup things after edits, catch outlier interstitial
  596. ;; characters, like newline and empty lines after hidden subitems,
  597. ;; etc.
  598. (setplist 'allout-body-span-category nil)
  599. (put 'allout-body-span-category 'evaporate t)
  600. (put 'allout-body-span-category 'local-map allout-item-body-keymap)
  601. ;;(put 'allout-body-span-category
  602. ;; 'modification-hooks '(allout-body-modification-handler))
  603. ;;(put 'allout-body-span-category 'field 'body)
  604. (setplist 'allout-cue-span-category nil)
  605. (put 'allout-cue-span-category 'evaporate t)
  606. (put 'allout-cue-span-category
  607. 'modification-hooks '(allout-body-modification-handler))
  608. (put 'allout-cue-span-category 'local-map allout-cue-span-keymap)
  609. (put 'allout-cue-span-category 'mouse-face widget-button-face)
  610. (put 'allout-cue-span-category 'pointer 'arrow)
  611. (put 'allout-cue-span-category 'field 'structure)
  612. (setplist 'allout-trailing-category nil)
  613. (put 'allout-trailing-category 'evaporate t)
  614. (put 'allout-trailing-category 'local-map allout-item-body-keymap)
  615. (setplist 'allout-escapes-category nil)
  616. (put 'allout-escapes-category 'invisible 'allout-escapes)
  617. (put 'allout-escapes-category 'evaporate t))
  618. ;;;_ > allout-widgets-prepopulate-buffer ()
  619. (defun allout-widgets-prepopulate-buffer ()
  620. "Step over the current buffers exposed items to do initial widgetizing."
  621. (if (not allout-widgets-mode-inhibit)
  622. (save-excursion
  623. (goto-char (point-min))
  624. (while (allout-next-visible-heading 1)
  625. (when (not (widget-at (point)))
  626. (allout-get-or-create-item-widget))))))
  627. ;;;_ . settings context
  628. ;;;_ = allout-container-item
  629. (defvar allout-container-item-widget nil
  630. "A widget for the current outline's overarching container as an item.
  631. The item has settings (of the file/connection) and maybe a body, but no
  632. icon/bullet.")
  633. (make-variable-buffer-local 'allout-container-item-widget)
  634. ;;;_ . Hooks and hook helpers
  635. ;;;_ , major command-loop business:
  636. ;;;_ > allout-widgets-pre-command-business (&optional recursing)
  637. (defun allout-widgets-pre-command-business (&optional _recursing)
  638. "Handle actions pending before `allout-mode' activity."
  639. )
  640. ;;;_ > allout-widgets-post-command-business (&optional recursing)
  641. (defun allout-widgets-post-command-business (&optional _recursing)
  642. "Handle actions pending after any `allout-mode' commands.
  643. Optional RECURSING is for internal use, to limit recursion."
  644. ;; - check changed text for nesting discontinuities and escape anything
  645. ;; that's: (1) asterisks at bol or (2) excessively nested.
  646. (condition-case nil
  647. (when (and (boundp 'allout-mode) allout-mode)
  648. (if allout-widgets-unset-inhibit-read-only
  649. (setq inhibit-read-only nil
  650. allout-widgets-unset-inhibit-read-only nil))
  651. (when allout-widgets-reenable-before-change-handler
  652. (add-hook 'before-change-functions
  653. 'allout-widgets-before-change-handler
  654. nil 'local)
  655. (setq allout-widgets-reenable-before-change-handler nil))
  656. (when (or allout-widgets-undo-exposure-record
  657. allout-widgets-changes-record)
  658. (let* ((debug-on-signal t)
  659. (debug-on-error t)
  660. ;; inhibit recording new undo records when processing
  661. ;; effects of undo-exposure:
  662. (debugger 'allout-widgets-hook-error-handler)
  663. (adjusting-message " Adjusting widgets...")
  664. (replaced-message (allout-widgets-adjusting-message
  665. adjusting-message))
  666. (start-time (current-time)))
  667. (if allout-widgets-undo-exposure-record
  668. ;; inhibit undo recording iff undoing exposure stuff.
  669. ;; XXX we might need to inhibit per respective
  670. ;; change-record, rather than assuming that some undo
  671. ;; activity during a command is all undo activity.
  672. (let ((buffer-undo-list t))
  673. (allout-widgets-exposure-undo-processor)
  674. (allout-widgets-changes-dispatcher))
  675. (allout-widgets-exposure-undo-processor)
  676. (allout-widgets-changes-dispatcher))
  677. (if allout-widgets-time-decoration-activity
  678. (setq allout-widgets-last-decoration-timing
  679. (list (allout-elapsed-time-seconds (current-time)
  680. start-time)
  681. allout-widgets-changes-record)))
  682. (setq allout-widgets-changes-record nil)
  683. (if replaced-message
  684. (if (stringp replaced-message)
  685. (message replaced-message)
  686. (message "")))))
  687. ;; alas, decorated intermediate matches are not easily undecorated
  688. ;; when they're automatically rehidden by isearch, so we're
  689. ;; dropping this nicety.
  690. ;; ;; Detect undecorated items, eg during isearch into previously
  691. ;; ;; unexposed topics, and decorate "economically". Some
  692. ;; ;; undecorated stuff is often exposed, to reduce lag, but the
  693. ;; ;; item containing the cursor is decorated. We constrain
  694. ;; ;; recursion to avoid being trapped by unexpectedly undecoratable
  695. ;; ;; items.
  696. ;; (when (and (not recursing)
  697. ;; (not (allout-current-decorated-p))
  698. ;; (or (not (equal (allout-depth) 0))
  699. ;; (not allout-container-item-widget)))
  700. ;; (let ((buffer-undo-list t))
  701. ;; (allout-widgets-exposure-change-recorder
  702. ;; allout-recent-prefix-beginning allout-recent-prefix-end nil)
  703. ;; (allout-widgets-post-command-business 'recursing)))
  704. ;; Detect and rectify fouled outline structure - decorated item
  705. ;; not at beginning of line.
  706. (let ((this-widget (or (widget-at (point))
  707. ;; XXX we really should be checking across
  708. ;; edited span, not just point and point+1
  709. (and (not (eq (point) (point-max)))
  710. (widget-at (1+ (point))))))
  711. inserted-at)
  712. (save-excursion
  713. (if (and this-widget
  714. (goto-char (widget-get this-widget :from))
  715. (not (bolp)))
  716. (if (not
  717. (condition-case nil
  718. (yes-or-no-p
  719. (concat "Misplaced item won't be recognizable "
  720. " as part of outline - rectify? "))
  721. (quit nil)))
  722. (progn
  723. (if (allout-hidden-p (max (1- (point)) 1))
  724. (save-excursion
  725. (goto-char (max (1- (point)) 1))
  726. (allout-show-to-offshoot)))
  727. (allout-widgets-undecorate-item this-widget))
  728. ;; expose any hidden intervening items, so resulting
  729. ;; position is clear:
  730. (setq inserted-at (point))
  731. (allout-unprotected (insert-before-markers "\n"))
  732. (forward-char -1)
  733. ;; ensure the inserted newline is visible:
  734. (allout-flag-region inserted-at (1+ inserted-at) nil)
  735. (allout-widgets-post-command-business 'recursing)
  736. (message (concat "outline structure corrected - item"
  737. " moved to beginning of new line"))
  738. ;; preserve cursor position in some cases:
  739. (if (and inserted-at
  740. (> (point) inserted-at))
  741. (forward-char -1)))))))
  742. (error
  743. ;; zero work list so we don't get stuck futilely retrying.
  744. ;; error recording done by allout-widgets-hook-error-handler.
  745. (setq allout-widgets-changes-record nil))))
  746. ;;;_ , major change handlers:
  747. ;;;_ > allout-widgets-before-change-handler
  748. (defun allout-widgets-before-change-handler (beg end)
  749. "Business to be done before changes in a widgetized allout outline."
  750. ;; protect against unruly edits to structure:
  751. (cond
  752. (undo-in-progress (when (eq (get-text-property beg 'category)
  753. 'allout-icon-span-category)
  754. (save-excursion
  755. (goto-char beg)
  756. (let* ((item-widget (allout-get-item-widget)))
  757. (if item-widget
  758. (allout-widgets-exposure-undo-recorder
  759. item-widget))))))
  760. (inhibit-read-only t)
  761. ((not (and (boundp 'allout-mode) allout-mode)) t)
  762. ((equal this-command 'quoted-insert) t)
  763. ((not (text-property-any beg (if (equal end beg)
  764. (min (1+ beg) (point-max))
  765. end)
  766. 'field 'structure))
  767. t)
  768. ((yes-or-no-p "Unruly edit of outline structure - allow? ")
  769. (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
  770. inhibit-read-only t))
  771. (t
  772. ;; tell the allout-widgets-post-command-business to reestablish the hook:
  773. (setq allout-widgets-reenable-before-change-handler t)
  774. ;; and raise an error to prevent the edit (and disable the hook):
  775. (error "%s"
  776. (substitute-command-keys allout-structure-unruly-deletion-message)))))
  777. ;;;_ > allout-widgets-after-change-handler
  778. (defun allout-widgets-after-change-handler (_beg _end _prelength)
  779. "Reconcile what needs to be reconciled for allout widgets after edits."
  780. )
  781. ;;;_ > allout-current-decorated-p ()
  782. (defun allout-current-decorated-p ()
  783. "True if the current item is not decorated"
  784. (save-excursion
  785. (if (allout-back-to-current-heading)
  786. (if (> allout-recent-depth 0)
  787. (and (allout-get-item-widget) t)
  788. allout-container-item-widget))))
  789. ;;;_ > allout-widgets-hook-error-handler
  790. (defun allout-widgets-hook-error-handler (mode args)
  791. "Process errors which occurred in the course of command hook operation.
  792. We store a backtrace of the error information in the variable,
  793. `allout-widgets-last-hook-error', unset the error handlers, and
  794. reraise the error, so that processing continues to the
  795. encompassing condition-case."
  796. ;; first deconstruct special error environment so errors here propagate
  797. ;; to encompassing condition-case:
  798. (setq debugger 'debug
  799. debug-on-error nil
  800. debug-on-signal nil)
  801. (let* ((bt (with-output-to-string (backtrace)))
  802. (this "allout-widgets-hook-error-handler")
  803. (header
  804. (format "allout-widgets-last-hook-error stored, %s/%s %s %s"
  805. this mode args
  806. (format-time-string "%e-%b-%Y %r"))))
  807. ;; post to *Messages* then immediately replace with more compact notice:
  808. (message "%s" (setq allout-widgets-last-hook-error
  809. (format "%s:\n%s" header bt)))
  810. (message header) (sit-for allout-widgets-hook-error-post-time)
  811. ;; reraise the error, or one concerning this function if unexpected:
  812. (if (equal mode 'error)
  813. (apply 'signal args)
  814. (error "%s: unexpected mode, %s %s" this mode args))))
  815. ;;;_ > allout-widgets-changes-exceed-threshold-p ()
  816. (defun allout-widgets-adjusting-message (message)
  817. "Post MESSAGE when pending are likely to make a big enough delay.
  818. If posting of the MESSAGE is warranted and there already is a
  819. `current-message' in the minibuffer, the MESSAGE is appended to
  820. the current one, and the previously pending `current-message' is
  821. returned for later posting on completion.
  822. If posting of the MESSAGE is warranted, but no `current-message'
  823. is pending, then t is returned to indicate that case.
  824. If posting of the MESSAGE is not warranted, then nil is returned.
  825. See `allout-widgets-adjust-message-length-threshold',
  826. `allout-widgets-adjust-message-size-threshold' for message
  827. posting threshold criteria."
  828. (if (or (> (length allout-widgets-changes-record)
  829. allout-widgets-adjust-message-length-threshold)
  830. ;; for size, use distance from start of first to end of last:
  831. (let ((min (point-max))
  832. (max 0)
  833. first second)
  834. (mapc (function (lambda (entry)
  835. (if (eq :undone-exposure (car entry))
  836. nil
  837. (setq first (cadr entry)
  838. second (caddr entry))
  839. (if (< (min first second) min)
  840. (setq min (min first second)))
  841. (if (> (max first second) max)
  842. (setq max (max first second))))))
  843. allout-widgets-changes-record)
  844. (> (- max min) allout-widgets-adjust-message-size-threshold)))
  845. (let ((prior (current-message)))
  846. (message (if prior (concat prior " - " message) message))
  847. (or prior t))))
  848. ;;;_ > allout-widgets-changes-dispatcher ()
  849. (defun allout-widgets-changes-dispatcher ()
  850. "Dispatch CHANGES-RECORD items to respective widgets change processors."
  851. (if (not allout-widgets-mode-inhibit)
  852. (let* ((changes-record allout-widgets-changes-record)
  853. (changes-pending (and changes-record t))
  854. entry
  855. exposures
  856. additions
  857. deletions
  858. shifts)
  859. (when changes-pending
  860. (while changes-record
  861. (setq entry (pop changes-record))
  862. (case (car entry)
  863. (:exposed (push entry exposures))
  864. (:added (push entry additions))
  865. (:deleted (push entry deletions))
  866. (:shifted (push entry shifts))))
  867. (if exposures
  868. (allout-widgets-exposure-change-processor exposures))
  869. (if additions
  870. (allout-widgets-additions-processor additions))
  871. (if deletions
  872. (allout-widgets-deletions-processor deletions))
  873. (if shifts
  874. (allout-widgets-shifts-processor shifts))))
  875. (when (not (equal allout-widgets-mode-inhibit 'undecorated))
  876. (allout-widgets-undecorate-region (point-min)(point-max))
  877. (setq allout-widgets-mode-inhibit 'undecorated))))
  878. ;;;_ > allout-widgets-exposure-change-recorder (from to flag)
  879. (defun allout-widgets-exposure-change-recorder (from to flag)
  880. "Record allout exposure changes for tracking during post-command processing.
  881. Records changes in `allout-widgets-changes-record'."
  882. (push (list :exposed from to flag) allout-widgets-changes-record))
  883. ;;;_ > allout-widgets-exposure-change-processor (changes)
  884. (defun allout-widgets-exposure-change-processor (changes)
  885. "Widgetize and adjust item widgets tracking allout outline exposure changes.
  886. Generally invoked via `allout-exposure-change-functions'."
  887. (let ((changes (sort changes (function (lambda (this next)
  888. (< (cadr this) (cadr next))))))
  889. ;; have to distinguish between concealing and exposing so that, eg,
  890. ;; `allout-expose-topic's mix is handled properly.
  891. handled-expose
  892. covered
  893. deactivate-mark)
  894. (dolist (change changes)
  895. (let (handling
  896. (from (cadr change))
  897. bucket got
  898. (to (caddr change))
  899. (flag (cadddr change))
  900. parent)
  901. ;; swap from and to:
  902. (if (< to from) (setq bucket to
  903. to from
  904. from bucket))
  905. ;; have we already handled exposure changes in this region?
  906. (setq handling (if flag 'handled-conceal 'handled-expose)
  907. got (allout-range-overlaps from to (symbol-value handling))
  908. covered (car got))
  909. (set handling (cadr got))
  910. (when (not covered)
  911. (save-excursion
  912. (goto-char from)
  913. (cond
  914. ;; collapsing:
  915. (flag
  916. (allout-widgets-undecorate-region from to)
  917. (allout-beginning-of-current-line)
  918. (let ((widget (allout-get-item-widget)))
  919. (if (not widget)
  920. (allout-get-or-create-item-widget)
  921. (widget-apply widget :redecorate))))
  922. ;; expanding:
  923. (t
  924. (while (< (point) to)
  925. (allout-beginning-of-current-line)
  926. (setq parent (allout-get-item-widget))
  927. (if (not parent)
  928. (setq parent (allout-get-or-create-item-widget))
  929. (widget-apply parent :redecorate))
  930. (allout-next-visible-heading 1)
  931. (if (widget-get parent :has-subitems)
  932. (allout-redecorate-visible-subtree parent))
  933. (if (> (point) to)
  934. ;; subtree may be well beyond to - incorporate in ranges:
  935. (setq handled-expose
  936. (allout-range-overlaps from (point) handled-expose)
  937. covered (car handled-expose)
  938. handled-expose (cadr handled-expose)))
  939. (allout-next-visible-heading 1))))))))))
  940. ;;;_ > allout-widgets-additions-recorder (from to)
  941. (defun allout-widgets-additions-recorder (from to)
  942. "Record allout item additions for tracking during post-command processing.
  943. Intended for use on `allout-structure-added-functions'.
  944. FROM point at the start of the first new item and TO is point at the start
  945. of the last one.
  946. Records changes in `allout-widgets-changes-record'."
  947. (push (list :added from to) allout-widgets-changes-record))
  948. ;;;_ > allout-widgets-additions-processor (changes)
  949. (defun allout-widgets-additions-processor (changes)
  950. "Widgetize and adjust items tracking allout outline structure additions.
  951. Dispatched by `allout-widgets-post-command-business' in response to
  952. :added entries recorded by `allout-widgets-additions-recorder'."
  953. (save-excursion
  954. (let (handled
  955. covered)
  956. (dolist (change changes)
  957. (let ((from (cadr change))
  958. bucket
  959. (to (caddr change)))
  960. (if (< to from) (setq bucket to to from from bucket))
  961. ;; have we already handled exposure changes in this region?
  962. (setq handled (allout-range-overlaps from to handled)
  963. covered (car handled)
  964. handled (cadr handled))
  965. (when (not covered)
  966. (goto-char from)
  967. ;; Prior sibling and parent can both be affected.
  968. (if (allout-ascend)
  969. (allout-redecorate-visible-subtree
  970. (allout-get-or-create-item-widget 'redecorate)))
  971. (if (< (point) from)
  972. (goto-char from))
  973. (while (and (< (point) to) (not (eobp)))
  974. (allout-beginning-of-current-line)
  975. (allout-redecorate-visible-subtree
  976. (allout-get-or-create-item-widget))
  977. (allout-next-visible-heading 1))
  978. (if (> (point) to)
  979. ;; subtree may be well beyond to - incorporate in ranges:
  980. (setq handled (allout-range-overlaps from (point) handled)
  981. covered (car handled)
  982. handled (cadr handled)))))))))
  983. ;;;_ > allout-widgets-deletions-recorder (depth from)
  984. (defun allout-widgets-deletions-recorder (depth from)
  985. "Record allout item deletions for tracking during post-command processing.
  986. Intended for use on `allout-structure-deleted-functions'.
  987. DEPTH is the depth of the deleted subtree, and FROM is the point from which
  988. the subtree was deleted.
  989. Records changes in `allout-widgets-changes-record'."
  990. (push (list :deleted depth from) allout-widgets-changes-record))
  991. ;;;_ > allout-widgets-deletions-processor (changes)
  992. (defun allout-widgets-deletions-processor (changes)
  993. "Adjust items tracking allout outline structure deletions.
  994. Dispatched by `allout-widgets-post-command-business' in response to
  995. :deleted entries recorded by `allout-widgets-deletions-recorder'."
  996. (save-excursion
  997. (dolist (change changes)
  998. (let ((depth (cadr change))
  999. (from (caddr change)))
  1000. (goto-char from)
  1001. (when (allout-previous-visible-heading 1)
  1002. (if (> depth 1)
  1003. (allout-ascend-to-depth (1- depth)))
  1004. (allout-redecorate-visible-subtree
  1005. (allout-get-or-create-item-widget 'redecorate)))))))
  1006. ;;;_ > allout-widgets-shifts-recorder (shifted-amount at)
  1007. (defun allout-widgets-shifts-recorder (shifted-amount at)
  1008. "Record outline subtree shifts for tracking during post-command processing.
  1009. Intended for use on `allout-structure-shifted-functions'.
  1010. SHIFTED-AMOUNT is the depth change and AT is the point at the start of the
  1011. subtree that's been shifted.
  1012. Records changes in `allout-widgets-changes-record'."
  1013. (push (list :shifted shifted-amount at) allout-widgets-changes-record))
  1014. ;;;_ > allout-widgets-shifts-processor (changes)
  1015. (defun allout-widgets-shifts-processor (changes)
  1016. "Widgetize and adjust items tracking allout outline structure additions.
  1017. Dispatched by `allout-widgets-post-command-business' in response to
  1018. :shifted entries recorded by `allout-widgets-shifts-recorder'."
  1019. (save-excursion
  1020. (dolist (change changes)
  1021. (goto-char (caddr change))
  1022. (allout-ascend)
  1023. (allout-redecorate-visible-subtree))))
  1024. ;;;_ > allout-widgets-after-copy-or-kill-function ()
  1025. (defun allout-widgets-after-copy-or-kill-function ()
  1026. "Do allout-widgets processing of text just placed in the kill ring.
  1027. Intended for use on `allout-after-copy-or-kill-hook'."
  1028. (if (car kill-ring)
  1029. (setcar kill-ring (allout-widgets-undecorate-text (car kill-ring)))))
  1030. ;;;_ > allout-widgets-after-undo-function ()
  1031. (defun allout-widgets-after-undo-function ()
  1032. "Do allout-widgets processing of text after an undo.
  1033. Intended for use on `allout-post-undo-hook'."
  1034. (save-excursion
  1035. (if (allout-goto-prefix)
  1036. (allout-redecorate-item (allout-get-or-create-item-widget)))))
  1037. ;;;_ > allout-widgets-exposure-undo-recorder (widget from-state)
  1038. (defun allout-widgets-exposure-undo-recorder (widget)
  1039. "Record outline exposure undo for tracking during post-command processing.
  1040. Intended for use by `allout-graphics-modification-handler'.
  1041. WIDGET is the widget being changed.
  1042. Records changes in `allout-widgets-changes-record'."
  1043. ;; disregard the events if we're currently processing them.
  1044. (if (not allout-undo-exposure-in-progress)
  1045. (push widget allout-widgets-undo-exposure-record)))
  1046. ;;;_ > allout-widgets-exposure-undo-processor ()
  1047. (defun allout-widgets-exposure-undo-processor ()
  1048. "Adjust items tracking undo of allout outline structure exposure.
  1049. Dispatched by `allout-widgets-post-command-business' in response to
  1050. :undone-exposure entries recorded by `allout-widgets-exposure-undo-recorder'."
  1051. (let* ((allout-undo-exposure-in-progress t)
  1052. ;; inhibit undo recording while twiddling exposure to track undo:
  1053. (widgets allout-widgets-undo-exposure-record)
  1054. widget-start-marker widget-end-marker
  1055. from-state icon-start-point to-state
  1056. handled covered)
  1057. (setq allout-widgets-undo-exposure-record nil)
  1058. (save-excursion
  1059. (dolist (widget widgets)
  1060. (setq widget-start-marker (widget-get widget :from)
  1061. widget-end-marker (widget-get widget :to)
  1062. from-state (widget-get widget :icon-state)
  1063. icon-start-point (widget-apply widget :actual-position
  1064. :icon-start)
  1065. to-state (get-text-property icon-start-point
  1066. :icon-state))
  1067. (setq handled (allout-range-overlaps widget-start-marker
  1068. widget-end-marker
  1069. handled)
  1070. covered (car handled)
  1071. handled (cadr handled))
  1072. (when (not covered)
  1073. (goto-char (widget-get widget :from))
  1074. (when (not (allout-hidden-p))
  1075. ;; adjust actual exposure to that of to-state viz from-state
  1076. (cond ((and (eq to-state 'closed) (eq from-state 'opened))
  1077. (allout-hide-current-subtree)
  1078. (allout-decorate-item-and-context widget))
  1079. ((and (eq to-state 'opened) (eq from-state 'closed))
  1080. (save-excursion
  1081. (dolist
  1082. (expose-to (allout-chart-exposure-contour-by-icon))
  1083. (goto-char expose-to)
  1084. (allout-show-to-offshoot)))))))))))
  1085. ;;;_ > allout-chart-exposure-contour-by-icon (&optional from-depth)
  1086. (defun allout-chart-exposure-contour-by-icon (&optional from-depth)
  1087. "Return points of subtree items to which exposure should be extended.
  1088. The qualifying items are ones with a widget icon that is in the closed or
  1089. empty state, or items with undecorated subitems.
  1090. The resulting list of points is in reverse order.
  1091. Optional FROM-DEPTH is for internal use."
  1092. ;; During internal recursion, we return a pair: (at-end . result)
  1093. ;; Otherwise we just return the result.
  1094. (let ((from-depth from-depth)
  1095. start-point
  1096. at-end level-depth
  1097. this-widget
  1098. got subgot)
  1099. (if from-depth
  1100. (setq level-depth (allout-depth))
  1101. ;; at containing item:
  1102. (setq start-point (point))
  1103. (setq from-depth (allout-depth))
  1104. (setq at-end (not (allout-next-heading))
  1105. level-depth allout-recent-depth))
  1106. ;; traverse the level, recursing on deeper levels:
  1107. (while (and (not at-end)
  1108. (> allout-recent-depth from-depth)
  1109. (setq this-widget (allout-get-item-widget)))
  1110. (if (< level-depth allout-recent-depth)
  1111. ;; recurse:
  1112. (progn
  1113. (setq subgot (allout-chart-exposure-contour-by-icon level-depth)
  1114. at-end (car subgot)
  1115. subgot (cdr subgot))
  1116. (if subgot (setq got (append subgot got))))
  1117. ;; progress at this level:
  1118. (when (memq (widget-get this-widget :icon-state) '(closed empty))
  1119. (push (point) got)
  1120. (allout-end-of-subtree))
  1121. (setq at-end (not (allout-next-heading)))))
  1122. ;; tailor result depending on whether or not we're a recursion:
  1123. (if (not start-point)
  1124. (cons at-end got)
  1125. (goto-char start-point)
  1126. got)))
  1127. ;;;_ > allout-range-overlaps (from to ranges)
  1128. (defun allout-range-overlaps (from to ranges)
  1129. "Return a pair indicating overlap of FROM and TO subtree range in RANGES.
  1130. First element of result indicates whether candidate range FROM, TO
  1131. overlapped any of the existing ranges.
  1132. Second element of result is a new version of RANGES incorporating the
  1133. candidate range with overlaps consolidated.
  1134. FROM and TO must be in increasing order, as must be the pairs in RANGES."
  1135. ;; to append to the end: (rplacd next-to-last-cdr (list 'f))
  1136. (let (new-ranges
  1137. entry
  1138. ;; the start of the range that includes the candidate from:
  1139. included-from
  1140. ;; the end of the range that includes the candidate to:
  1141. included-to
  1142. ;; the candidates were inserted:
  1143. done)
  1144. (while (and ranges (not done))
  1145. (setq entry (car ranges)
  1146. ranges (cdr ranges))
  1147. (cond
  1148. (included-from
  1149. ;; some entry included the candidate from.
  1150. (cond ((> (car entry) to)
  1151. ;; current entry exceeds end of candidate range - done.
  1152. (push (list included-from to) new-ranges)
  1153. (push entry new-ranges)
  1154. (setq included-to to
  1155. done t))
  1156. ((>= (cadr entry) to)
  1157. ;; current entry includes end of candidate range - done.
  1158. (push (list included-from (cadr entry)) new-ranges)
  1159. (setq included-to (cadr entry)
  1160. done t))
  1161. ;; current entry contained in candidate range - ditch, continue:
  1162. (t nil)))
  1163. ((> (car entry) to)
  1164. ;; current entry start exceeds candidate end - done, placed as new entry
  1165. (push (list from to) new-ranges)
  1166. (push entry new-ranges)
  1167. (setq included-to to
  1168. done t))
  1169. ((>= (car entry) from)
  1170. ;; current entry start is above candidate start, but not above
  1171. ;; candidate end (by prior case).
  1172. (setq included-from from)
  1173. ;; now we have to check on whether this entry contains to, or continue:
  1174. (when (>= (cadr entry) to)
  1175. ;; current entry contains only candidate end - done:
  1176. (push (list included-from (cadr entry)) new-ranges)
  1177. (setq included-to (cadr entry)
  1178. done t))
  1179. ;; otherwise, we will continue to look for placement of candidate end.
  1180. )
  1181. ((>= (cadr entry) to)
  1182. ;; current entry properly contains candidate range.
  1183. (push entry new-ranges)
  1184. (setq included-from (car entry)
  1185. included-to (cadr entry)
  1186. done t))
  1187. ((>= (cadr entry) from)
  1188. ;; current entry contains start of candidate range.
  1189. (setq included-from (car entry)))
  1190. (t
  1191. ;; current entry is below the candidate range.
  1192. (push entry new-ranges))))
  1193. (cond ((and included-from included-to)
  1194. ;; candidates placed.
  1195. nil)
  1196. ((not (or included-from included-to))
  1197. ;; candidates found no place, must be at the end:
  1198. (push (list from to) new-ranges))
  1199. (included-from
  1200. ;; candidate start placed but end not:
  1201. (push (list included-from to) new-ranges))
  1202. ;; might be included-to and not included-from, indicating new entry.
  1203. )
  1204. (setq new-ranges (nreverse new-ranges))
  1205. (if ranges (setq new-ranges (append new-ranges ranges)))
  1206. (list (if included-from t) new-ranges)))
  1207. ;;;_ > allout-test-range-overlaps ()
  1208. (defun allout-test-range-overlaps ()
  1209. "`allout-range-overlaps' unit tests."
  1210. (let* (ranges
  1211. got
  1212. (try (lambda (from to)
  1213. (setq got (allout-range-overlaps from to ranges))
  1214. (setq ranges (cadr got))
  1215. got)))
  1216. ;; ;; biggie:
  1217. ;; (setq ranges nil)
  1218. ;; ;; ~ .02 to .1 seconds for just repeated listing args instead of funcall
  1219. ;; ;; ~ 13 seconds for doing repeated funcall
  1220. ;; (message "time-trial: %s, resulting size %s"
  1221. ;; (time-trial
  1222. ;; '(let ((size 10000)
  1223. ;; doing)
  1224. ;; (dotimes (count size)
  1225. ;; (setq doing (random size))
  1226. ;; (funcall try doing (+ doing (random 5)))
  1227. ;; ;;(list doing (+ doing (random 5)))
  1228. ;; )))
  1229. ;; (length ranges))
  1230. ;; (sit-for 2)
  1231. ;; fresh:
  1232. (setq ranges nil)
  1233. (assert (equal (funcall try 3 5) '(nil ((3 5)))))
  1234. ;; add range at end:
  1235. (assert (equal (funcall try 10 12) '(nil ((3 5) (10 12)))))
  1236. ;; add range at beginning:
  1237. (assert (equal (funcall try 1 2) '(nil ((1 2) (3 5) (10 12)))))
  1238. ;; insert range somewhere in the middle:
  1239. (assert (equal (funcall try 7 9) '(nil ((1 2) (3 5) (7 9) (10 12)))))
  1240. ;; consolidate some:
  1241. (assert (equal (funcall try 5 8) '(t ((1 2) (3 9) (10 12)))))
  1242. ;; add more:
  1243. (assert (equal (funcall try 15 17) '(nil ((1 2) (3 9) (10 12) (15 17)))))
  1244. ;; add more:
  1245. (assert (equal (funcall try 20 22)
  1246. '(nil ((1 2) (3 9) (10 12) (15 17) (20 22)))))
  1247. ;; encompass more:
  1248. (assert (equal (funcall try 4 11) '(t ((1 2) (3 12) (15 17) (20 22)))))
  1249. ;; encompass all:
  1250. (assert (equal (funcall try 2 25) '(t ((1 25)))))
  1251. ;; fresh slate:
  1252. (setq ranges nil)
  1253. (assert (equal (funcall try 20 25) '(nil ((20 25)))))
  1254. (assert (equal (funcall try 30 35) '(nil ((20 25) (30 35)))))
  1255. (assert (equal (funcall try 26 28) '(nil ((20 25) (26 28) (30 35)))))
  1256. (assert (equal (funcall try 15 20) '(t ((15 25) (26 28) (30 35)))))
  1257. (assert (equal (funcall try 10 30) '(t ((10 35)))))
  1258. (assert (equal (funcall try 5 6) '(nil ((5 6) (10 35)))))
  1259. (assert (equal (funcall try 2 100) '(t ((2 100)))))
  1260. (setq ranges nil)
  1261. ))
  1262. ;;;_ > allout-widgetize-buffer (&optional doing)
  1263. (defun allout-widgetize-buffer (&optional doing)
  1264. "EXAMPLE FUNCTION. Widgetize items in buffer using allout-chart-subtree.
  1265. We economize by just focusing on the first of local-maximum depth siblings.
  1266. Optional DOING is for internal use - a chart of the current level, for
  1267. recursive operation."
  1268. (interactive)
  1269. (if (not doing)
  1270. (save-excursion
  1271. (goto-char (point-min))
  1272. ;; Construct the chart by scanning the siblings:
  1273. (dolist (top-level-sibling (allout-chart-siblings))
  1274. (goto-char top-level-sibling)
  1275. (let ((subchart (allout-chart-subtree)))
  1276. (if subchart
  1277. (allout-widgetize-buffer subchart)))))
  1278. ;; save-excursion was done on recursion entry, not necessary here.
  1279. (let (have-sublists)
  1280. (dolist (sibling doing)
  1281. (when (listp sibling)
  1282. (setq have-sublists t)
  1283. (allout-widgetize-buffer sibling)))
  1284. (when (and (not have-sublists) (not (widget-at (car doing))))
  1285. (goto-char (car doing))
  1286. (allout-get-or-create-item-widget)))))
  1287. ;;;_ : Item widget and constructors
  1288. ;;;_ $ allout-item-widget
  1289. (define-widget 'allout-item-widget 'default
  1290. "A widget presenting an allout outline item."
  1291. 'button nil
  1292. ;; widget-field-at respects this to get item if 'field is unused.
  1293. ;; we don't use field to avoid collision with end-of-line, etc, on which
  1294. ;; allout depends.
  1295. 'real-field nil
  1296. ;; data fields:
  1297. ;; tailor the widget for a specific item
  1298. :create 'allout-decorate-item-and-context
  1299. :value-delete 'allout-widgets-undecorate-item
  1300. ;; Not Yet Converted (from original, tree-widget stab)
  1301. :expander 'allout-tree-event-dispatcher ; get children when nil :args
  1302. :expander-p 'identity ; always engage the :expander
  1303. :action 'allout-tree-widget-action
  1304. ;; :notify "when item changes"
  1305. ;; force decoration of item but not context, unless already done this tick:
  1306. :redecorate 'allout-redecorate-item
  1307. :last-decorated-tick nil
  1308. ;; recognize the actual situation of the item's text:
  1309. :parse-item 'allout-parse-item-at-point
  1310. ;; decorate the entirety of the item, sans offspring:
  1311. :decorate-item-span 'allout-decorate-item-span
  1312. ;; decorate the various item elements:
  1313. :decorate-guides 'allout-decorate-item-guides
  1314. :decorate-icon 'allout-decorate-item-icon
  1315. :decorate-cue 'allout-decorate-item-cue
  1316. :decorate-body 'allout-decorate-item-body
  1317. :actual-position 'allout-item-actual-position
  1318. ;; Layout parameters:
  1319. :is-container nil ; is this actually the encompassing file/connection?
  1320. :from nil ; item beginning - marker
  1321. :to nil ; item end - marker
  1322. :span-overlay nil ; overlay by which actual position is determined
  1323. ;; also serves as guide-end:
  1324. :icon-start nil
  1325. :icon-end nil
  1326. :distinctive-start nil
  1327. ;; also serves as cue-start:
  1328. :distinctive-end nil
  1329. ;; also serves as cue-end:
  1330. :body-start nil
  1331. :body-end nil
  1332. :depth nil
  1333. :has-subitems nil
  1334. :was-has-subitems 'init
  1335. :expanded nil
  1336. :was-expanded 'init
  1337. :brief nil
  1338. :was-brief 'init
  1339. :does-encrypt nil ; pending encryption when :is-encrypted false.
  1340. :is-encrypted nil
  1341. ;; the actual location of the item text:
  1342. :location 'allout-item-location
  1343. :button-keymap allout-item-icon-keymap ; XEmacs
  1344. :keymap allout-item-icon-keymap ; Emacs
  1345. ;; Element regions:
  1346. :guides-span nil
  1347. :icon-span nil
  1348. :cue-span nil
  1349. :bullet nil
  1350. :was-bullet nil
  1351. :body-span nil
  1352. :body-brevity-p 'allout-body-brevity-p
  1353. ;; :guide-column-flags indicate (in reverse order) whether or not the
  1354. ;; item's ancestor at the depth corresponding to the column has a
  1355. ;; subsequent sibling - ie, whether or not the corresponding column needs
  1356. ;; a descender line to connect that ancestor with its sibling.
  1357. :guide-column-flags nil
  1358. :was-guide-column-flags 'init
  1359. ;; ie, has subitems:
  1360. :populous-p 'allout-item-populous-p
  1361. :help-echo 'allout-tree-widget-help-echo
  1362. )
  1363. ;;;_ > allout-new-item-widget ()
  1364. (defsubst allout-new-item-widget ()
  1365. "create a new item widget, not yet situated anywhere."
  1366. (if allout-widgets-maintain-tally
  1367. ;; all the extra overhead is incurred only when doing the
  1368. ;; maintenance, except the condition, which can't be avoided.
  1369. (let ((widget (widget-convert 'allout-item-widget)))
  1370. (puthash widget nil allout-widgets-tally)
  1371. widget)
  1372. (widget-convert 'allout-item-widget)))
  1373. ;;;_ : Item decoration
  1374. ;;;_ > allout-decorate-item-and-context (item-widget &optional redecorate
  1375. ;;; blank-container parent)
  1376. (defun allout-decorate-item-and-context (item-widget &optional redecorate
  1377. blank-container _parent)
  1378. "Create or adjust widget decorations for ITEM-WIDGET and neighbors at point.
  1379. The neighbors include its siblings and parent.
  1380. ITEM-WIDGET can be a created or converted `allout-item-widget'.
  1381. If you're only trying to get or create a widget for an item, use
  1382. `allout-get-or-create-item-widget'. If you have the item-widget, applying
  1383. :redecorate will do the right thing.
  1384. Optional BLANK-CONTAINER is for internal use. It is used to fabricate a
  1385. container widget for an empty-bodied container, in the course of decorating
  1386. a proper (non-container) item which starts at the beginning of the file.
  1387. Optional REDECORATE causes redecoration of the item-widget and
  1388. its siblings, even if already decorated in this cycle of the command loop.
  1389. Optional PARENT, when provided, bypasses some navigation and computation
  1390. necessary to obtain the parent of the items being processed.
  1391. We return the item-widget corresponding to the item at point."
  1392. (when (or redecorate
  1393. (not (equal (widget-get item-widget :last-decorated-tick)
  1394. allout-command-counter)))
  1395. (let* ((allout-inhibit-body-modification-hook t)
  1396. (was-modified (buffer-modified-p))
  1397. (was-point (point))
  1398. prefix-start
  1399. (is-container (or blank-container
  1400. (not (setq prefix-start (allout-goto-prefix)))
  1401. (< was-point prefix-start)))
  1402. ;; steady-point (set in two steps) is reliable across parent
  1403. ;; widget-creation.
  1404. (steady-point (progn (if is-container (goto-char 1))
  1405. (point-marker)))
  1406. (steady-point (progn (set-marker-insertion-type steady-point t)
  1407. steady-point))
  1408. (parent (and (not is-container)
  1409. (allout-get-or-create-parent-widget)))
  1410. successor-sibling
  1411. doing-item
  1412. reverse-siblings-chart
  1413. (buffer-undo-list t))
  1414. ;; At this point the parent is decorated and parent-flags indicate
  1415. ;; its guide lines. We will iterate over the siblings according to a
  1416. ;; chart we create at the start, and going from last to first so we
  1417. ;; don't have to worry about text displacement caused by widgetizing.
  1418. (if is-container
  1419. (progn (widget-put item-widget :is-container t)
  1420. (setq reverse-siblings-chart (list 1)))
  1421. (goto-char (widget-apply parent :actual-position :from))
  1422. (if (widget-get parent :is-container)
  1423. ;; `allout-goto-prefix' will go to first non-container item:
  1424. (allout-goto-prefix)
  1425. (allout-next-heading))
  1426. (setq reverse-siblings-chart (list allout-recent-prefix-beginning))
  1427. (while (allout-next-sibling)
  1428. (push allout-recent-prefix-beginning reverse-siblings-chart)))
  1429. (dolist (doing-at reverse-siblings-chart)
  1430. (goto-char doing-at)
  1431. (when allout-widgets-track-decoration
  1432. (sit-for 0))
  1433. (setq doing-item (if (= doing-at steady-point)
  1434. item-widget
  1435. (or (allout-get-item-widget)
  1436. (allout-new-item-widget))))
  1437. (when (or redecorate (not (equal (widget-get doing-item
  1438. :last-decorated-tick)
  1439. allout-command-counter)))
  1440. (widget-apply doing-item :parse-item t blank-container)
  1441. (widget-apply doing-item :decorate-item-span)
  1442. (widget-apply doing-item :decorate-guides
  1443. parent (and successor-sibling t))
  1444. (widget-apply doing-item :decorate-icon)
  1445. (widget-apply doing-item :decorate-cue)
  1446. (widget-apply doing-item :decorate-body)
  1447. (widget-put doing-item :last-decorated-tick allout-command-counter))
  1448. (setq successor-sibling doing-at))
  1449. (set-buffer-modified-p was-modified)
  1450. (goto-char steady-point)
  1451. ;; must null the marker or the buffer gets clogged with impedance:
  1452. (set-marker steady-point nil)
  1453. item-widget)))
  1454. ;;;_ > allout-redecorate-item (item)
  1455. (defun allout-redecorate-item (item-widget)
  1456. "Resituate ITEM-WIDGET decorations, disregarding context.
  1457. Use this to redecorate only the item, when you know that its
  1458. situation with respect to siblings, parent, and offspring is
  1459. unchanged from its last decoration. Use
  1460. `allout-decorate-item-and-context' instead to reassess and adjust
  1461. relevant context, when suitable."
  1462. (if (not (equal (widget-get item-widget :last-decorated-tick)
  1463. allout-command-counter))
  1464. (let ((was-modified (buffer-modified-p))
  1465. (buffer-undo-list t))
  1466. (widget-apply item-widget :parse-item)
  1467. (widget-apply item-widget :decorate-guides)
  1468. (widget-apply item-widget :decorate-icon)
  1469. (widget-apply item-widget :decorate-cue)
  1470. (widget-apply item-widget :decorate-body)
  1471. (set-buffer-modified-p was-modified))))
  1472. ;;;_ > allout-redecorate-visible-subtree (&optional parent-widget
  1473. ;;; depth chart)
  1474. (defun allout-redecorate-visible-subtree (&optional parent-widget depth chart)
  1475. "Redecorate all visible items in subtree at point.
  1476. Optional PARENT-WIDGET is for optimization, when the parent
  1477. widget is already available.
  1478. Optional DEPTH restricts the excursion depth of covered.
  1479. Optional CHART is for internal recursion, to carry a chart of the
  1480. target items.
  1481. Point is left at the last sibling in the visible subtree."
  1482. ;; using a treatment that takes care of all the siblings on a level, we
  1483. ;; only need apply it to the first sibling on the level, and we can
  1484. ;; collect and pass the parent of the lower levels to recursive calls as
  1485. ;; we go.
  1486. (let ((parent-widget
  1487. (if (and parent-widget (widget-apply parent-widget
  1488. :actual-position :from))
  1489. (progn (goto-char (widget-apply parent-widget
  1490. :actual-position :from))
  1491. parent-widget)
  1492. (let ((got (allout-get-item-widget)))
  1493. (if got
  1494. (allout-decorate-item-and-context got 'redecorate)
  1495. (allout-get-or-create-item-widget 'redecorate)))))
  1496. (pending-chart (or chart (allout-chart-subtree nil 'visible)))
  1497. item-widget
  1498. previous-sibling-point
  1499. recent-sibling-point)
  1500. (setq pending-chart (nreverse pending-chart))
  1501. (dolist (sibling-point pending-chart)
  1502. (cond ((integerp sibling-point)
  1503. (when (not previous-sibling-point)
  1504. (goto-char sibling-point)
  1505. (if (setq item-widget (allout-get-item-widget nil))
  1506. (allout-decorate-item-and-context item-widget 'redecorate
  1507. nil parent-widget)
  1508. (allout-get-or-create-item-widget)))
  1509. (setq previous-sibling-point sibling-point
  1510. recent-sibling-point sibling-point))
  1511. ((listp sibling-point)
  1512. (if (or (not depth)
  1513. (> depth 1))
  1514. (allout-redecorate-visible-subtree
  1515. (if (not previous-sibling-point)
  1516. ;; containment discontinuity - sigh
  1517. parent-widget
  1518. (allout-get-or-create-item-widget 'redecorate))
  1519. (if depth (1- depth))
  1520. sibling-point)))))
  1521. (if (and recent-sibling-point (< (point) recent-sibling-point))
  1522. (goto-char recent-sibling-point))))
  1523. ;;;_ > allout-parse-item-at-point (item-widget &optional at-beginning
  1524. ;;; blank-container)
  1525. (defun allout-parse-item-at-point (item-widget &optional at-beginning
  1526. blank-container)
  1527. "Set widget ITEM-WIDGET layout parameters per item-at-point's actual layout.
  1528. If optional AT-BEGINNING is t, then point is assumed to be at the start of
  1529. the item prefix.
  1530. If optional BLANK-CONTAINER is true, then the parameters of a container
  1531. which has an empty body are set. (Though the body is blank, the object
  1532. may have subitems.)"
  1533. ;; Uncomment this sit-for to notice where decoration is happening:
  1534. ;; (sit-for .1)
  1535. (let* ((depth (allout-depth))
  1536. (depth (if blank-container 0 depth))
  1537. (is-container (or blank-container (zerop depth)))
  1538. (does-encrypt (and (not is-container)
  1539. (allout-encrypted-type-prefix)))
  1540. (is-encrypted (and does-encrypt (allout-encrypted-topic-p)))
  1541. (icon-end allout-recent-prefix-end)
  1542. (icon-start (1- icon-end))
  1543. body-start
  1544. body-end
  1545. has-subitems
  1546. )
  1547. (widget-put item-widget :depth depth)
  1548. (if is-container
  1549. (progn
  1550. (widget-put item-widget :from (allout-set-boundary-marker
  1551. :from (point-min)
  1552. (widget-get item-widget :from)))
  1553. (widget-put item-widget :icon-end nil)
  1554. (widget-put item-widget :icon-start nil)
  1555. (setq body-start (widget-put item-widget :body-start 1)))
  1556. ;; not container:
  1557. (widget-put item-widget :from (allout-set-boundary-marker
  1558. :from (if at-beginning
  1559. (point)
  1560. allout-recent-prefix-beginning)
  1561. (widget-get item-widget :from)))
  1562. (widget-put item-widget :icon-start icon-start)
  1563. (widget-put item-widget :icon-end icon-end)
  1564. (when does-encrypt
  1565. (widget-put item-widget :does-encrypt t)
  1566. (widget-put item-widget :is-encrypted is-encrypted))
  1567. ;; cue area:
  1568. (setq body-start icon-end)
  1569. (widget-put item-widget :bullet (allout-get-bullet))
  1570. (if (equal (char-after body-start) ? )
  1571. (setq body-start (1+ body-start)))
  1572. (widget-put item-widget :body-start body-start)
  1573. )
  1574. ;; Both container and regular items:
  1575. ;; :body-end (doesn't include a trailing blank line, if any) -
  1576. (widget-put item-widget :body-end (setq body-end
  1577. (if blank-container
  1578. 1
  1579. (allout-end-of-entry))))
  1580. (widget-put item-widget :to (allout-set-boundary-marker
  1581. :to (if blank-container
  1582. (point-min)
  1583. (or (allout-pre-next-prefix)
  1584. (goto-char (point-max))))
  1585. (widget-get item-widget :to)))
  1586. (widget-put item-widget :has-subitems
  1587. (setq has-subitems
  1588. (and
  1589. ;; has a subsequent item:
  1590. (not (= body-end (point-max)))
  1591. ;; subsequent item is deeper:
  1592. (< depth (allout-recent-depth)))))
  1593. ;; note :expanded - true if widget item's content is currently visible?
  1594. (widget-put item-widget :expanded
  1595. (and has-subitems
  1596. ;; subsequent item is or isn't visible:
  1597. (save-excursion
  1598. (goto-char allout-recent-prefix-beginning)
  1599. (not (allout-hidden-p)))))))
  1600. ;;;_ > allout-set-boundary-marker (boundary position &optional current-marker)
  1601. (defun allout-set-boundary-marker (_boundary position &optional current-marker)
  1602. "Set or create item widget BOUNDARY type marker at POSITION.
  1603. Optional CURRENT-MARKER is the marker currently being used for
  1604. the boundary, if any.
  1605. BOUNDARY type is either :from or :to, determining the marker insertion type."
  1606. (if (not position) (setq position (point)))
  1607. (if current-marker
  1608. (set-marker current-marker position)
  1609. (let ((marker (make-marker)))
  1610. ;; XXX dang - would like for :from boundary to advance after inserted
  1611. ;; text, but that would omit new header prefixes when allout
  1612. ;; relevels, etc. this competes with ad-hoc edits, which would
  1613. ;; better be omitted
  1614. (set-marker-insertion-type marker nil)
  1615. (set-marker marker position))))
  1616. ;;;_ > allout-decorate-item-span (item-widget)
  1617. (defun allout-decorate-item-span (item-widget)
  1618. "Equip the item with a span, as an entirety.
  1619. This span is implemented so it can be used to detect displacement
  1620. of the widget in absolute terms, and provides an offset bias for
  1621. the various element spans."
  1622. (if (and (widget-get item-widget :is-container)
  1623. ;; the only case where the span could be empty.
  1624. (eq (widget-get item-widget :from)
  1625. (widget-get item-widget :to)))
  1626. nil
  1627. (allout-item-span item-widget
  1628. (widget-get item-widget :from)
  1629. (widget-get item-widget :to))))
  1630. ;;;_ > allout-decorate-item-guides (item-widget
  1631. ;;; &optional parent-widget has-successor)
  1632. (defun allout-decorate-item-guides (item-widget
  1633. &optional parent-widget has-successor)
  1634. "Add ITEM-WIDGET guide icon-prefix descender and connector text properties.
  1635. Optional arguments provide context for deriving the guides.
  1636. In their absence, the current guide column flags are used.
  1637. Optional PARENT-WIDGET is the widget for the item's parent item.
  1638. Optional HAS-SUCCESSOR is true if the item is followed by a sibling.
  1639. We also hide the header-prefix string.
  1640. Guides are established according to the item-widget's :guide-column-flags,
  1641. when different than :was-guide-column-flags. Changing that property and
  1642. reapplying this method will rectify the glyphs."
  1643. (when (not (widget-get item-widget :is-container))
  1644. (let* ((depth (widget-get item-widget :depth))
  1645. ;; (parent-depth (and parent-widget
  1646. ;; (widget-get parent-widget :depth)))
  1647. (parent-flags (and parent-widget
  1648. (widget-get parent-widget :guide-column-flags)))
  1649. (parent-flags-depth (length parent-flags))
  1650. (extender-length (- depth (+ parent-flags-depth 2)))
  1651. (flags (or (and (> depth 1)
  1652. parent-widget
  1653. (widget-put item-widget :guide-column-flags
  1654. (append (list has-successor)
  1655. (if (< 0 extender-length)
  1656. (make-list extender-length
  1657. '-))
  1658. parent-flags)))
  1659. (widget-get item-widget :guide-column-flags)))
  1660. (was-flags (widget-get item-widget :was-guide-column-flags))
  1661. (guides-start (widget-get item-widget :from))
  1662. (guides-end (widget-get item-widget :icon-start))
  1663. (position guides-start)
  1664. (increment (length allout-header-prefix))
  1665. reverse-flags
  1666. guide-name
  1667. extenders
  1668. (inhibit-read-only t))
  1669. (when (not (equal was-flags flags))
  1670. (setq reverse-flags (reverse flags))
  1671. (while reverse-flags
  1672. (setq guide-name
  1673. (cond ((null (cdr reverse-flags))
  1674. (if (car reverse-flags)
  1675. 'mid-connector
  1676. 'end-connector))
  1677. ((eq (car reverse-flags) '-)
  1678. ;; accumulate extenders tally, to be painted on next
  1679. ;; non-extender flag, according to the flag type.
  1680. (setq extenders (1+ (or extenders 0)))
  1681. nil)
  1682. ((car reverse-flags)
  1683. 'through-descender)
  1684. (t 'skip-descender)))
  1685. (when guide-name
  1686. (put-text-property position (setq position (+ position increment))
  1687. 'display (allout-fetch-icon-image guide-name))
  1688. (if (> increment 1) (setq increment 1))
  1689. (when extenders
  1690. ;; paint extenders after a connector, else leave spaces.
  1691. (dotimes (i extenders)
  1692. (put-text-property
  1693. position (setq position (1+ position))
  1694. 'display (allout-fetch-icon-image
  1695. (if (memq guide-name '(mid-connector end-connector))
  1696. 'extender-connector
  1697. 'skip-descender))))
  1698. (setq extenders nil)))
  1699. (setq reverse-flags (cdr reverse-flags)))
  1700. (widget-put item-widget :was-guide-column-flags flags))
  1701. (allout-item-element-span-is item-widget :guides-span
  1702. guides-start guides-end))))
  1703. ;;;_ > allout-decorate-item-icon (item-widget)
  1704. (defun allout-decorate-item-icon (item-widget)
  1705. "Add item icon glyph and distinctive bullet text properties to ITEM-WIDGET."
  1706. (when (not (widget-get item-widget :is-container))
  1707. (let* ((icon-start (widget-get item-widget :icon-start))
  1708. (icon-end (widget-get item-widget :icon-end))
  1709. (bullet (widget-get item-widget :bullet))
  1710. (use-bullet bullet)
  1711. (was-bullet (widget-get item-widget :was-bullet))
  1712. (distinctive (allout-distinctive-bullet bullet))
  1713. (distinctive-start (widget-get item-widget :distinctive-start))
  1714. (distinctive-end (widget-get item-widget :distinctive-end))
  1715. (does-encrypt (widget-get item-widget :does-encrypt))
  1716. (is-encrypted (and does-encrypt (widget-get item-widget
  1717. :is-encrypted)))
  1718. (expanded (widget-get item-widget :expanded))
  1719. (has-subitems (widget-get item-widget :has-subitems))
  1720. (inhibit-read-only t)
  1721. icon-state)
  1722. (when (not (and (equal (widget-get item-widget :was-expanded) expanded)
  1723. (equal (widget-get item-widget :was-has-subitems)
  1724. has-subitems)
  1725. (equal (widget-get item-widget :was-does-encrypt)
  1726. does-encrypt)
  1727. (equal (widget-get item-widget :was-is-encrypted)
  1728. is-encrypted)))
  1729. (setq icon-state
  1730. (cond (does-encrypt (if is-encrypted
  1731. 'locked-encrypted
  1732. 'unlocked-encrypted))
  1733. (expanded 'opened)
  1734. (has-subitems 'closed)
  1735. (t 'empty)))
  1736. (put-text-property icon-start (1+ icon-start)
  1737. 'display (allout-fetch-icon-image icon-state))
  1738. (widget-put item-widget :was-expanded expanded)
  1739. (widget-put item-widget :was-has-subitems has-subitems)
  1740. (widget-put item-widget :was-does-encrypt does-encrypt)
  1741. (widget-put item-widget :was-is-encrypted is-encrypted)
  1742. ;; preserve as a widget property to track last known:
  1743. (widget-put item-widget :icon-state icon-state)
  1744. ;; preserve as a text property to track undo:
  1745. (put-text-property icon-start icon-end :icon-state icon-state))
  1746. (allout-item-element-span-is item-widget :icon-span
  1747. icon-start icon-end)
  1748. (when (not (string= was-bullet bullet))
  1749. (cond ((not distinctive)
  1750. ;; XXX we strip the prior properties without even checking if
  1751. ;; the prior bullet was distinctive, because the widget
  1752. ;; provisions to convey that info is disappearing, sigh.
  1753. (remove-text-properties icon-end (1+ icon-end) '(display))
  1754. (setq distinctive-start icon-end distinctive-end icon-end)
  1755. (widget-put item-widget :distinctive-start distinctive-start)
  1756. (widget-put item-widget :distinctive-end distinctive-end))
  1757. ((not (string= bullet allout-numbered-bullet))
  1758. (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
  1759. (does-encrypt
  1760. (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
  1761. (t
  1762. (goto-char icon-end)
  1763. (looking-at "[0-9]+")
  1764. (setq use-bullet (buffer-substring icon-end (match-end 0)))
  1765. (setq distinctive-start icon-end
  1766. distinctive-end (match-end 0))))
  1767. (put-text-property distinctive-start distinctive-end 'display
  1768. use-bullet)
  1769. (widget-put item-widget :was-bullet bullet)
  1770. (widget-put item-widget :distinctive-start distinctive-start)
  1771. (widget-put item-widget :distinctive-end distinctive-end)))))
  1772. ;;;_ > allout-decorate-item-cue (item-widget)
  1773. (defun allout-decorate-item-cue (item-widget)
  1774. "Incorporate space between bullet icon and body to the ITEM-WIDGET."
  1775. ;; NOTE: most of the cue-area
  1776. (when (not (widget-get item-widget :is-container))
  1777. (let* ((cue-start (or (widget-get item-widget :distinctive-end)
  1778. (widget-get item-widget :icon-end)))
  1779. (body-start (widget-get item-widget :body-start))
  1780. ;(expanded (widget-get item-widget :expanded))
  1781. ;(has-subitems (widget-get item-widget :has-subitems))
  1782. (inhibit-read-only t))
  1783. (allout-item-element-span-is item-widget :cue-span cue-start body-start)
  1784. (put-text-property (1- body-start) body-start 'rear-nonsticky t))))
  1785. ;;;_ > allout-decorate-item-body (item-widget &optional force)
  1786. (defun allout-decorate-item-body (item-widget &optional force)
  1787. "Incorporate item body text as part the ITEM-WIDGET.
  1788. Optional FORCE means force reassignment of the region property."
  1789. (let* ((allout-inhibit-body-modification-hook t)
  1790. (body-start (widget-get item-widget :body-start))
  1791. (body-end (widget-get item-widget :body-end))
  1792. (inhibit-read-only t))
  1793. (allout-item-element-span-is item-widget :body-span
  1794. body-start (min (1+ body-end) (point-max))
  1795. force)))
  1796. ;;;_ > allout-item-actual-position (item-widget field)
  1797. (defun allout-item-actual-position (item-widget field)
  1798. "Return ITEM-WIDGET FIELD position taking item displacement into account."
  1799. ;; The item's sub-element positions (:icon-end, :body-start, etc) are
  1800. ;; accurate when the item is parsed, but some offsets from the start
  1801. ;; drift with text added in the body.
  1802. ;;
  1803. ;; Rather than reparse an item with every change (inefficient), or derive
  1804. ;; every position from a distinct field marker/overlay (prohibitive as
  1805. ;; the number of items grows), we use the displacement tracking of the
  1806. ;; :span-overlay's markers, against the registered :from or :body-end
  1807. ;; (depending on whether the requested field value is before or after the
  1808. ;; item body), to bias the registered values.
  1809. ;;
  1810. ;; This is not necessary/useful when the item is being decorated, because
  1811. ;; that always must be preceded by a fresh item parse.
  1812. (if (not (eq field :body-end))
  1813. (widget-get item-widget :from)
  1814. (let* ((span-overlay (widget-get item-widget :span-overlay))
  1815. (body-end-position (widget-get item-widget :body-end))
  1816. (ref-marker-position (and span-overlay
  1817. (overlay-end span-overlay)))
  1818. (offset (and body-end-position span-overlay
  1819. (- (or ref-marker-position 0)
  1820. body-end-position))))
  1821. (+ (widget-get item-widget field) (or offset 0)))))
  1822. ;;;_ : Item undecoration
  1823. ;;;_ > allout-widgets-undecorate-region (start end)
  1824. (defun allout-widgets-undecorate-region (start end)
  1825. "Eliminate widgets and decorations for all items in region from START to END."
  1826. (let ((next start)
  1827. widget)
  1828. (save-excursion
  1829. (goto-char start)
  1830. (while (< (setq next (next-single-char-property-change next
  1831. 'display
  1832. (current-buffer)
  1833. end))
  1834. end)
  1835. (goto-char next)
  1836. (when (setq widget (allout-get-item-widget))
  1837. ;; if the next-property/overly progression got us to a widget:
  1838. (allout-widgets-undecorate-item widget t))))))
  1839. ;;;_ > allout-widgets-undecorate-text (text)
  1840. (defun allout-widgets-undecorate-text (text)
  1841. "Eliminate widgets and decorations for all items in TEXT."
  1842. (remove-text-properties 0 (length text)
  1843. '(display nil :icon-state nil rear-nonsticky nil
  1844. category nil button nil field nil)
  1845. text)
  1846. text)
  1847. ;;;_ > allout-widgets-undecorate-item (item-widget &optional no-expose)
  1848. (defun allout-widgets-undecorate-item (item-widget &optional no-expose)
  1849. "Remove widget decorations from ITEM-WIDGET.
  1850. Any concealed content head lines and item body is exposed, unless
  1851. optional NO-EXPOSE is non-nil."
  1852. (let ((from (widget-get item-widget :from))
  1853. (to (widget-get item-widget :to))
  1854. (text-properties-to-remove '(display nil
  1855. :icon-state nil
  1856. rear-nonsticky nil
  1857. category nil
  1858. button nil
  1859. field nil))
  1860. (span-overlay (widget-get item-widget :span-overlay))
  1861. (button-overlay (widget-get item-widget :button))
  1862. (was-modified (buffer-modified-p))
  1863. (buffer-undo-list t)
  1864. (inhibit-read-only t))
  1865. (if (not no-expose)
  1866. (allout-flag-region from to nil))
  1867. (allout-unprotected
  1868. (remove-text-properties from to text-properties-to-remove))
  1869. (when span-overlay
  1870. (delete-overlay span-overlay) (widget-put item-widget :span-overlay nil))
  1871. (when button-overlay
  1872. (delete-overlay button-overlay) (widget-put item-widget :button nil))
  1873. (set-marker from nil)
  1874. (set-marker to nil)
  1875. (if (not was-modified)
  1876. (set-buffer-modified-p nil))))
  1877. ;;;_ : Item decoration support
  1878. ;;;_ > allout-item-span (item-widget &optional start end)
  1879. (defun allout-item-span (item-widget &optional start end)
  1880. "Return or register the location of an ITEM-WIDGET's actual START and END.
  1881. If START and END are not passed in, return either a dotted pair
  1882. of the current span, if established, or nil if not yet set.
  1883. When the START and END are passed, return the distance that the
  1884. start of the item moved. We return 0 if the span was not
  1885. previously established or is not moved."
  1886. (let ((overlay (widget-get item-widget :span-overlay)))
  1887. (cond ((not overlay) (when start
  1888. (setq overlay (make-overlay start end nil t nil))
  1889. (overlay-put overlay 'button item-widget)
  1890. (overlay-put overlay 'evaporate t)
  1891. (widget-put item-widget :span-overlay overlay)
  1892. t))
  1893. ;; report:
  1894. ((not start) (cons (overlay-start overlay) (overlay-end overlay)))
  1895. ;; move:
  1896. ((or (not (equal (overlay-start overlay) start))
  1897. (not (equal (overlay-end overlay) end)))
  1898. (move-overlay overlay start end)
  1899. t)
  1900. ;; specified span already set:
  1901. (t nil))))
  1902. ;;;_ > allout-item-element-span-is (item-widget element
  1903. ;;; &optional start end force)
  1904. (defun allout-item-element-span-is (item-widget element
  1905. &optional start end force)
  1906. "Return or register the location of the indicated ITEM-WIDGET ELEMENT.
  1907. ELEMENT is one of :guides-span, :icon-span, :cue-span, or :body-span.
  1908. When optional START is specified, optional END must also be.
  1909. START and END are the actual bounds of the region, if provided.
  1910. If START and END are not passed in, we return either a dotted
  1911. pair of the current span, if established, or nil if not yet set.
  1912. When the START and END are passed, we return t if the region
  1913. changed or nil if not.
  1914. Optional FORCE means force assignment of the region's text
  1915. property, even if it's already set."
  1916. (let ((span (widget-get item-widget element)))
  1917. (cond ((or (not span) force)
  1918. (when start
  1919. (widget-put item-widget element (cons start end))
  1920. (put-text-property start end 'category
  1921. (cdr (assoc element
  1922. allout-span-to-category)))
  1923. t))
  1924. ;; report:
  1925. ((not start) span)
  1926. ;; move if necessary:
  1927. ((not (and (eq (car span) start)
  1928. (eq (cdr span) end)))
  1929. (widget-put item-widget element span)
  1930. t)
  1931. ;; specified span already set:
  1932. (t nil))))
  1933. ;;;_ : Item widget retrieval (/ high-level creation):
  1934. ;;;_ > allout-get-item-widget (&optional container)
  1935. (defun allout-get-item-widget (&optional container)
  1936. "Return the widget for the item at point, or nil if no widget yet exists.
  1937. Point must be situated *before* the start of the target item's
  1938. body, so we don't get an existing containing item when we're in
  1939. the process of creating an item in the middle of another.
  1940. Optional CONTAINER is used to obtain the container item."
  1941. (if (or container (zerop (allout-depth)))
  1942. allout-container-item-widget
  1943. ;; allout-recent-* are calibrated by (allout-depth) if we got here.
  1944. (let ((got (widget-at allout-recent-prefix-beginning)))
  1945. (if (and got (listp got))
  1946. (if (marker-position (widget-get got :from))
  1947. (and
  1948. (>= (point) (widget-apply got :actual-position :from))
  1949. (<= (point) (widget-apply got :actual-position :body-start))
  1950. got)
  1951. ;; a wacky residual item - undecorate and disregard:
  1952. (allout-widgets-undecorate-item got)
  1953. nil)))))
  1954. ;;;_ > allout-get-or-create-item-widget (&optional redecorate blank-container)
  1955. (defun allout-get-or-create-item-widget (&optional redecorate blank-container)
  1956. "Return a widget for the item at point, creating the widget if necessary.
  1957. When creating a widget, we assume there has been a context change
  1958. and decorate its siblings and parent, as well.
  1959. Optional BLANK-CONTAINER is for internal use, to fabricate a
  1960. meta-container item with an empty body when the first proper
  1961. \(non-container) item starts at the beginning of the file.
  1962. Optional REDECORATE, if non-nil, means to redecorate the widget
  1963. if it already exists."
  1964. (let ((widget (allout-get-item-widget blank-container))
  1965. (buffer-undo-list t))
  1966. (cond (widget (if redecorate
  1967. (allout-redecorate-item widget))
  1968. widget)
  1969. ((or blank-container (zerop (allout-depth)))
  1970. (or allout-container-item-widget
  1971. (setq allout-container-item-widget
  1972. (allout-decorate-item-and-context
  1973. (widget-convert 'allout-item-widget)
  1974. nil blank-container))))
  1975. ;; create a widget for a regular/non-container item:
  1976. (t (allout-decorate-item-and-context (widget-convert
  1977. 'allout-item-widget))))))
  1978. ;;;_ > allout-get-or-create-parent-widget (&optional redecorate)
  1979. (defun allout-get-or-create-parent-widget (&optional redecorate)
  1980. "Return widget for parent of item at point, decorating it if necessary.
  1981. We return the container widget if we're above the first proper item in the
  1982. file.
  1983. Optional REDECORATE, if non-nil, means to redecorate the widget if it
  1984. already exists.
  1985. Point will wind up positioned on the beginning of the parent or beginning
  1986. of the buffer."
  1987. ;; use existing widget, if there, else establish it
  1988. (if (or (bobp) (and (not (allout-ascend))
  1989. (looking-at-p allout-regexp)))
  1990. (allout-get-or-create-item-widget redecorate 'blank-container)
  1991. (allout-get-or-create-item-widget redecorate)))
  1992. ;;;_ : X- Item ancillaries
  1993. ;;;_ >X allout-body-modification-handler (beg end)
  1994. (defun allout-body-modification-handler (_beg _end)
  1995. "Do routine processing of body text before and after modification.
  1996. Operation is inhibited by `allout-inhibit-body-modification-handler'."
  1997. ;; The primary duties are:
  1998. ;;
  1999. ;; - marking of escaped prefix-like text for delayed cleanup of escapes
  2000. ;; - removal and replacement of the settings
  2001. ;; - maintenance of beginning-of-line guide lines
  2002. ;;
  2003. ;; ?? Escapes removal (before changes) is not done when edits span multiple
  2004. ;; items, recognizing that item structure is being preserved, including
  2005. ;; escaping of item-prefix-like text within bodies. See
  2006. ;; `allout-before-modification-handler' and
  2007. ;; `allout-inhibit-body-modification-handler'.
  2008. ;;
  2009. ;; Adds the overlay to the `allout-unresolved-body-mod-workhash' during
  2010. ;; before-change operation, and removes from that list during after-change
  2011. ;; operation.
  2012. (cond (allout-inhibit-body-modification-hook nil)))
  2013. ;;;_ >X allout-graphics-modification-handler (beg end)
  2014. (defun allout-graphics-modification-handler (beg _end)
  2015. "Protect against incoherent deletion of decoration graphics.
  2016. Deletes allowed only when `inhibit-read-only' is t."
  2017. (cond
  2018. (undo-in-progress (when (eq (get-text-property beg 'category)
  2019. 'allout-icon-span-category)
  2020. (save-excursion
  2021. (goto-char beg)
  2022. (let* ((item-widget (allout-get-item-widget)))
  2023. (if item-widget
  2024. (allout-widgets-exposure-undo-recorder
  2025. item-widget))))))
  2026. (inhibit-read-only t)
  2027. ((not (and (boundp 'allout-mode) allout-mode)) t)
  2028. ((equal this-command 'quoted-insert) t)
  2029. ((yes-or-no-p "Unruly edit of outline structure - allow? ")
  2030. (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
  2031. inhibit-read-only t))
  2032. (t (error "%s"
  2033. (substitute-command-keys allout-structure-unruly-deletion-message)))))
  2034. ;;;_ > allout-item-icon-key-handler ()
  2035. (defun allout-item-icon-key-handler ()
  2036. "Catchall handling of key bindings in item icon/cue hot-spots.
  2037. Applies `allout-hotspot-key-handler' and calls the result, if any, as an
  2038. interactive command."
  2039. (interactive)
  2040. (let* ((mapped-binding (allout-hotspot-key-handler)))
  2041. (when mapped-binding
  2042. (call-interactively mapped-binding))))
  2043. ;;;_ : Status
  2044. ;;;_ . allout-item-location (item-widget)
  2045. (defun allout-item-location (item-widget)
  2046. "Location of the start of the item's text."
  2047. (overlay-start (widget-get item-widget :span-overlay)))
  2048. ;;;_ : Icon management
  2049. ;;;_ > allout-fetch-icon-image (name)
  2050. (defun allout-fetch-icon-image (name)
  2051. "Fetch allout icon for symbol NAME.
  2052. We use a caching strategy, so the caller doesn't need to do so."
  2053. (let* ((types allout-widgets-icon-types)
  2054. (use-dir (if (equal (allout-frame-property nil 'background-mode)
  2055. 'light)
  2056. allout-widgets-icons-light-subdir
  2057. allout-widgets-icons-dark-subdir))
  2058. (key (list name use-dir))
  2059. (got (assoc key allout-widgets-icons-cache)))
  2060. (if got
  2061. ;; display system shows only first of subsequent adjacent
  2062. ;; `eq'-identical repeats - use copies to avoid this problem.
  2063. (allout-widgets-copy-list (cadr got))
  2064. (while (and types (not got))
  2065. (setq got
  2066. (allout-find-image
  2067. (list (append (list :type (car types)
  2068. :file (concat use-dir
  2069. (symbol-name name)
  2070. "." (symbol-name
  2071. (car types))))
  2072. (if (featurep 'xemacs)
  2073. allout-widgets-item-image-properties-xemacs
  2074. allout-widgets-item-image-properties-emacs)
  2075. ))))
  2076. (setq types (cdr types)))
  2077. (if got
  2078. (push (list key got) allout-widgets-icons-cache))
  2079. got)))
  2080. ;;;_ : Miscellaneous
  2081. ;;;_ > allout-elapsed-time-seconds (time-value time-value)
  2082. (defun allout-elapsed-time-seconds (end start)
  2083. "Return seconds between START/END time values."
  2084. (let ((elapsed (time-subtract end start)))
  2085. (float-time elapsed)))
  2086. ;;;_ > allout-frame-property (frame property)
  2087. (defalias 'allout-frame-property
  2088. (cond ((fboundp 'frame-parameter)
  2089. 'frame-parameter)
  2090. ((fboundp 'frame-property)
  2091. 'frame-property)
  2092. (t nil)))
  2093. ;;;_ > allout-find-image (specs)
  2094. (defalias 'allout-find-image
  2095. (if (fboundp 'find-image)
  2096. 'find-image
  2097. nil) ; aka, not-yet-implemented for xemacs.
  2098. )
  2099. ;;;_ > allout-widgets-copy-list (list)
  2100. (defun allout-widgets-copy-list (list)
  2101. ;; duplicated from cl.el 'copy-list' as of 2008-08-17
  2102. "Return a copy of LIST, which may be a dotted list.
  2103. The elements of LIST are not copied, just the list structure itself."
  2104. (if (consp list)
  2105. (let ((res nil))
  2106. (while (consp list) (push (pop list) res))
  2107. (prog1 (nreverse res) (setcdr res list)))
  2108. (car list)))
  2109. ;;;_ . allout-widgets-count-buttons-in-region (start end)
  2110. (defun allout-widgets-count-buttons-in-region (start end)
  2111. "Debugging/diagnostic tool - count overlays with `button' property in region."
  2112. (interactive "r")
  2113. (setq start (or start (point-min))
  2114. end (or end (point-max)))
  2115. (if (> start end) (let ((interim start)) (setq start end end interim)))
  2116. (let ((button-overlays (delq nil
  2117. (mapcar (function (lambda (o)
  2118. (if (overlay-get o 'button)
  2119. o)))
  2120. (overlays-in start end)))))
  2121. (length button-overlays)))
  2122. ;;;_ : Run unit tests:
  2123. (defun allout-widgets-run-unit-tests ()
  2124. (message "Running allout-widget tests...")
  2125. (allout-test-range-overlaps)
  2126. (message "Running allout-widget tests... Done.")
  2127. (sit-for .5))
  2128. (when allout-widgets-run-unit-tests-on-load
  2129. (allout-widgets-run-unit-tests))
  2130. ;;;_ : provide
  2131. (provide 'allout-widgets)
  2132. ;;;_. Local emacs vars.
  2133. ;;;_ , Local variables:
  2134. ;;;_ , allout-layout: (-1 : 0)
  2135. ;;;_ , End: