ido-ubiquitous.el 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. ;;; ido-ubiquitous.el --- Use ido (nearly) everywhere. -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2011-2015 Ryan C. Thompson
  3. ;; Author: Ryan C. Thompson
  4. ;; URL: https://github.com/DarwinAwardWinner/ido-ubiquitous
  5. ;; Version: 3.9
  6. ;; Created: 2011-09-01
  7. ;; Keywords: convenience, completion, ido
  8. ;; EmacsWiki: InteractivelyDoThings
  9. ;; Package-Requires: ((emacs "24.1") (ido-completing-read+ "3.9") (cl-lib "0.5") (s "0"))
  10. ;; Filename: ido-ubiquitous.el
  11. ;; This file is NOT part of GNU Emacs.
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. ;;
  14. ;;; Commentary:
  15. ;; If you use the excellent `ido-mode' for efficient completion of
  16. ;; file names and buffers, you might wonder if you can get ido-style
  17. ;; completion everywhere else too. Well, that's what this package
  18. ;; does! ido-ubiquitous is here to enable ido-style completion for
  19. ;; (almost) every function that uses the standard completion function
  20. ;; `completing-read'.
  21. ;; To use this package, call `ido-ubiquitous-mode' to enable the mode,
  22. ;; or use `M-x customize-variable ido-ubiquitous-mode' it to enable it
  23. ;; permanently. Once the mode is enabled, most functions that use
  24. ;; `completing-read' will now have ido completion. If you decide in
  25. ;; the middle of a command that you would rather not use ido, just C-f
  26. ;; or C-b at the end/beginning of the input to fall back to non-ido
  27. ;; completion (this is the same shortcut as when using ido for buffers
  28. ;; or files).
  29. ;; Note that `completing-read' has some quirks and complex behavior
  30. ;; that ido cannot emulate. Ido-ubiquitous attempts to detect some of
  31. ;; these quirks and avoid using ido when it sees them. So some
  32. ;; functions will not have ido completion even when this mode is
  33. ;; enabled. Some other functions have ido disabled in them because
  34. ;; their packages already provide support for ido via other means (for
  35. ;; example, org-mode and magit). See `M-x customize-group
  36. ;; ido-ubiquitous' and read about the override variables for more
  37. ;; information.
  38. ;; ido-ubiquitous version 3.0 is a major update, including a split
  39. ;; into two packages, and some of the configuration options have
  40. ;; changed in non-backwards-compatible ways. If you have customized
  41. ;; ido-ubiquitous, be sure to check out `M-x customize-group
  42. ;; ido-ubiquitous' and `M-x customize-group ido-completing-read+'
  43. ;; after updating to 3.0 and make sure the new settings are to your
  44. ;; liking.
  45. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  46. ;;
  47. ;; This program is free software: you can redistribute it and/or modify
  48. ;; it under the terms of the GNU General Public License as published by
  49. ;; the Free Software Foundation, either version 3 of the License, or (at
  50. ;; your option) any later version.
  51. ;;
  52. ;; This program is distributed in the hope that it will be useful, but
  53. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  54. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  55. ;; General Public License for more details.
  56. ;;
  57. ;; You should have received a copy of the GNU General Public License
  58. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  59. ;;
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61. ;;
  62. ;;; Code:
  63. (defconst ido-ubiquitous-version "3.9"
  64. "Currently running version of ido-ubiquitous.
  65. Note that when you update ido-ubiquitous, this variable may not
  66. be updated until you restart Emacs.")
  67. (eval-when-compile
  68. (when (or (not (boundp 'completing-read-function))
  69. (< emacs-major-version 24))
  70. (error "Could not find required variable `completing-read-function'. Are you using Emacs version 24 or higher? If you have Emacs 23 or lower, please downgrade to ido-ubiquitous version 1.7 (or upgrade Emacs).")))
  71. (require 'ido)
  72. (require 'advice)
  73. (require 'cl-lib)
  74. (require 'cus-edit)
  75. (require 'ido-completing-read+)
  76. (require 's)
  77. ;; Only exists in emacs 24.4 and up; we don't use this library
  78. ;; directly, but we load it here so we can test if it's available,
  79. ;; because if it isn't we need enable a workaround.
  80. (require 'nadvice nil 'noerror)
  81. ;;; Debug messages
  82. ;;;###autoload
  83. (define-minor-mode ido-ubiquitous-debug-mode
  84. "If non-nil, ido-ubiquitous will print debug info.
  85. Debug info is printed to the *Messages* buffer."
  86. nil
  87. :global t
  88. :group 'ido-ubiquitous)
  89. ;; Defined as a macro for efficiency (args are not evaluated unless
  90. ;; debug mode is on)
  91. (defmacro ido-ubiquitous--debug-message (format-string &rest args)
  92. `(when ido-ubiquitous-debug-mode
  93. (message (concat "ido-ubiquitous: " ,format-string) ,@args)))
  94. (defun ido-ubiquitous--explain-fallback (arg)
  95. ;; This function accepts a string, or an ido-ubiquitous-fallback
  96. ;; signal.
  97. (when ido-ubiquitous-debug-mode
  98. (when (and (listp arg)
  99. (eq (car arg) 'ido-ubiquitous-fallback))
  100. (setq arg (cdr arg)))
  101. (ido-ubiquitous--debug-message "Falling back to `%s' because %s."
  102. ido-cr+-fallback-function arg)))
  103. ;;; Internal utility functions
  104. (defun ido-ubiquitous--as-string (sym-or-str)
  105. "Return name of symbol, return string as is."
  106. (if (symbolp sym-or-str)
  107. (symbol-name sym-or-str)
  108. sym-or-str))
  109. (defun ido-ubiquitous--as-symbol (sym-or-str)
  110. "Return string as symbol, return symbol as is."
  111. (if (symbolp sym-or-str)
  112. sym-or-str
  113. (intern sym-or-str)))
  114. ;;; Custom widget definitions
  115. ;; We need to define some custom widget types for use in the override
  116. ;; variables.
  117. (define-widget 'lazy-notag 'lazy
  118. "Like lazy widget, but does not display its tag, only its value."
  119. :format "%v")
  120. ;; Define matcher functions and widgets for match specifications
  121. (defvar ido-ubiquitous-match-spec-widget-types nil
  122. "List of widget names for match specs.")
  123. (defvar ido-ubiquitous-spec-matchers nil
  124. "Alist of functions for matching function specs against function names.")
  125. (cl-loop for (widget-name widget-tag key field-type matcher) in
  126. '((exact-match "Exact match" exact string string=)
  127. (prefix-match "Prefix match" prefix string string-prefix-p)
  128. (regexp-match "Regexp match" regexp regexp string-match-p))
  129. do (define-widget (ido-ubiquitous--as-symbol widget-name) 'lazy-notag widget-tag
  130. :menu-tag widget-tag
  131. :type `(list :tag ,widget-tag :format "%v"
  132. (const :format ""
  133. :tag ,widget-tag
  134. ,key)
  135. (,field-type :tag ,widget-tag)))
  136. do (add-to-list 'ido-ubiquitous-match-spec-widget-types
  137. widget-name 'append)
  138. do (add-to-list 'ido-ubiquitous-spec-matchers
  139. (cons key matcher) 'append))
  140. (define-widget 'ido-ubiquitous-match-spec 'lazy-notag
  141. "Choice of exact, prefix, or regexp match."
  142. :type `(choice :tag "Match type"
  143. ,@ido-ubiquitous-match-spec-widget-types))
  144. (define-widget 'ido-ubiquitous-command-override-spec 'lazy-notag
  145. "Choice of override action plus match specification."
  146. :type '(cons :tag "Override rule"
  147. (choice :tag "For matching commands"
  148. (const :menu-tag "Disable"
  149. :tag "Disable ido-ubiquitous"
  150. disable)
  151. (const :menu-tag "Enable"
  152. :tag "Enable ido-ubiquitous in normal default mode"
  153. enable)
  154. (const :menu-tag "Enable old-style default"
  155. :tag "Enable ido-ubiquitous in old-style default mode"
  156. enable-old))
  157. ido-ubiquitous-match-spec))
  158. (define-widget 'ido-ubiquitous-function-override-spec 'lazy-notag
  159. "Choice of override action and function name. (Exact match only.)"
  160. :type '(list :tag "Override rule"
  161. (choice :tag "Do the following"
  162. (const :menu-tag "Disable"
  163. :tag "Disable ido-ubiquitous"
  164. disable)
  165. (const :menu-tag "Enable"
  166. :tag "Enable ido-ubiquitous in normal default mode"
  167. enable)
  168. (const :menu-tag "Enable old-style default"
  169. :tag "Enable ido-ubiquitous in old-style default mode"
  170. enable-old))
  171. (const :format "" exact)
  172. (string :tag "For function")))
  173. ;;; Custom Declarations
  174. (defgroup ido-ubiquitous nil
  175. "Use ido for (almost) all completion."
  176. :group 'ido
  177. :group 'ido-completing-read-plus)
  178. ;;;###autoload
  179. (define-obsolete-variable-alias 'ido-ubiquitous
  180. 'ido-ubiquitous-mode "ido-ubiquitous 0.8")
  181. ;;;###autoload
  182. (define-obsolete-function-alias 'ido-ubiquitous
  183. 'ido-ubiquitous-mode "ido-ubiquitous 0.8")
  184. ;;;###autoload
  185. (define-minor-mode ido-ubiquitous-mode
  186. "Use `ido-completing-read' instead of `completing-read' almost everywhere.
  187. If this mode causes problems for a function, you can customize
  188. when ido completion is or is not used by customizing
  189. `ido-ubiquitous-command-overrides' or
  190. `ido-ubiquitous-function-overrides'."
  191. nil
  192. :global t
  193. :group 'ido-ubiquitous
  194. ;; Actually enable/disable the mode by setting
  195. ;; `completing-read-function'.
  196. (setq completing-read-function
  197. (if ido-ubiquitous-mode
  198. #'completing-read-ido-ubiquitous
  199. ido-cr+-fallback-function)))
  200. ;; Variables for functionality that has moved to ido-completing-read+
  201. (define-obsolete-variable-alias
  202. 'ido-ubiquitous-max-items
  203. 'ido-cr+-max-items
  204. "ido-ubiquitous 3.0")
  205. (define-obsolete-variable-alias
  206. 'ido-ubiquitous-fallback-completing-read-function
  207. 'ido-cr+-fallback-function
  208. "ido-ubiquitous 3.0")
  209. (define-obsolete-variable-alias
  210. 'ido-ubiquitous-enable-compatibility-globally
  211. 'ido-ubiquitous-enable-old-style-default
  212. "ido-ubiquitous 2.0")
  213. (defcustom ido-ubiquitous-default-state 'enable
  214. "Default ido-ubiquitous mode of operation for commands with no override.
  215. This can be set to one of three options:
  216. * `enable': use normal ido completion;
  217. * `enable-old': use ido completion, but with emulation of the
  218. old-style default selection of `completing-read';
  219. * `disable': use non-ido completion.
  220. Command-specific and function-specific overrides are available to
  221. override this default for specific commands/functions. See
  222. `ido-ubiquitous-command-overrides' and
  223. `ido-ubiquitous-function-overrides'.
  224. The `enable-old' option swaps the behavior of RET and C-j but
  225. only for the first keypress after beginning completion.
  226. Specifically, on the first keypress, RET will return an empty
  227. string and C-j will return the first item on the list. The
  228. purpose of this is to emulate a legacy compatibility quirk of
  229. `completing-read'. From the `completing-read' docstring:
  230. > If the input is null, `completing-read' returns DEF, or the
  231. > first element of the list of default values, or an empty string
  232. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  233. This odd behavior is required for compatibility with an old-style
  234. usage pattern whereby the default was requested by returning an
  235. empty string. In this mode, the caller receives the empty string
  236. and handles the default case manually, while `completing-read'
  237. never has any knowledge of the default. This is a problem for
  238. ido, which normally returns the first element in the list, not an
  239. empty string, when the input is empty and you press RET. Without
  240. knowledge of the default, it cannot ensure that the default is
  241. first on the list, so returning the first item is not the correct
  242. behavior. Instead, it must return an empty string like
  243. `completing-read'.
  244. The `disable' mode is available as a default, which seems
  245. counterintuitive. But this allows you, if you so desire, to
  246. enable ido-ubiquitous selectively for only a few specific commands
  247. using overrides and disable it for everything else."
  248. :type '(choice :tag "Default mode"
  249. (const :menu-tag "Disable"
  250. :tag "Disable ido-ubiquitous"
  251. disable)
  252. (const :menu-tag "Enable"
  253. :tag "Enable ido-ubiquitous in normal default mode"
  254. enable)
  255. (const :menu-tag "Enable old-style default"
  256. :tag "Enable ido-ubiquitous in old-style default mode"
  257. enable-old))
  258. :group 'ido-ubiquitous)
  259. (make-obsolete-variable
  260. 'ido-ubiquitous-enable-old-style-default
  261. "This variable no longer has any effect. Set
  262. `ido-ubiquitous-default-state' to `enable-old' instead."
  263. "ido-ubiquitous 3.0")
  264. (defconst ido-ubiquitous-default-command-overrides
  265. '(;; If you want ido for M-x, install smex
  266. (disable exact "execute-extended-command")
  267. ;; Wanderlust uses new-style default
  268. (enable prefix "wl-")
  269. ;; Info functions use old-style default selection
  270. (enable-old prefix "Info-")
  271. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/4
  272. (enable exact "webjump")
  273. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/28
  274. (enable regexp "\\`\\(find\\|load\\|locate\\)-library\\'")
  275. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/37
  276. ;; Org already supports ido natively
  277. (disable prefix "org-")
  278. ;; https://github.com/bbatsov/prelude/issues/488
  279. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/44
  280. ;; tmm implements its own non-standard completion mechanics
  281. (disable prefix "tmm-")
  282. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/47
  283. ;; theme functions don't need old-style compatibility
  284. (enable regexp "\\`\\(load\\|enable\\|disable\\|describe\\|custom-theme-visit\\)-theme\\'")
  285. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/79
  286. (enable-old prefix "bbdb-")
  287. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/83
  288. (enable-old exact "where-is")
  289. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/85
  290. (enable prefix "xref-")
  291. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/60
  292. (disable exact "todo-add-category")
  293. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/51
  294. (enable exact "find-tag")
  295. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/89
  296. (enable prefix "etags-select-")
  297. ) ; Close paren on separate line for better VC diffs
  298. "Default value of `ido-ubiquitous-command-overrides'.
  299. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  300. (defconst ido-ubiquitous-default-function-overrides
  301. '((disable exact "read-file-name")
  302. (disable exact "read-file-name-internal")
  303. (disable exact "read-buffer")
  304. (disable exact "gnus-emacs-completing-read")
  305. (disable exact "gnus-iswitchb-completing-read")
  306. (disable exact "grep-read-files")
  307. (disable exact "magit-builtin-completing-read")
  308. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/36
  309. (enable exact "bookmark-completing-read")
  310. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/4
  311. (enable-old exact "webjump-read-choice")
  312. (enable-old exact "webjump-read-url-choice")
  313. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/9
  314. (disable exact "isearchp-read-unicode-char")
  315. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/37
  316. (disable exact "org-completing-read")
  317. (disable exact "org-completing-read-no-i")
  318. (disable exact "org-iswitchb-completing-read")
  319. (disable exact "org-icompleting-read")
  320. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/38
  321. (enable exact "read-char-by-name")
  322. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/39
  323. (disable exact "Info-read-node-name")
  324. ;; https://github.com/purcell/emacs.d/issues/182#issuecomment-44212927
  325. (disable exact "tmm-menubar")
  326. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/58
  327. ;; https://github.com/mooz/js2-mode/issues/181
  328. (enable exact "imenu--completion-buffer")
  329. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/74
  330. (enable-old exact "auto-insert"))
  331. "Default value of `ido-ubiquitous-function-overrides'.
  332. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  333. (defcustom ido-ubiquitous-command-overrides ido-ubiquitous-default-command-overrides
  334. "List of command override specifications for ido-ubiquitous
  335. Each override specification describes how ido-ubiquitous should
  336. behave one or many commands. A specification has the
  337. form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'. BEHAVIOR is one of the
  338. following:
  339. * `disable': ido-ubiquitous should not be used at all for the
  340. specified commands;
  341. * `enable': ido-ubiquitous may be used with the specified
  342. commands, without emulating the old-style default selection
  343. of `completing-read';
  344. * `enable-old': ido-ubiquitous may be used with the specified
  345. commands, and should emulate the old-style default selection
  346. of `completing-read'.
  347. MATCH-TYPE affects how MATCH-TEXT is interpreted, as follows:
  348. * `exact': the specification only affects the one command
  349. whose name is MATCH-TEXT;
  350. * `prefix': the specification affects any command whose name
  351. starts with MATCH-TEXT (This is useful for specifying a
  352. certain behavior for an entire package);
  353. * `regexp': the specification affects any command whose name
  354. matches MATCH-TEXT (with MATCH-TEXT being interpreted as a
  355. regular expression)
  356. MATCH-TEXT should be a string.
  357. Since this variable's has a somewhat complex structure, it is
  358. recommended that you set this variable through Customize.
  359. Note that this variable only affects *commands*, which are
  360. functions marked as interactive. See
  361. `ido-ubiquitous-function-overrides' for how to modify the
  362. behavior of ido-ubiquitous for arbitrary functions.
  363. Note: If multiple overrides match the same commmand, the first
  364. one in the list will take precedence.
  365. If you need to add a new specification to this list, please also
  366. file a bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  367. :type '(repeat ido-ubiquitous-command-override-spec)
  368. :group 'ido-ubiquitous)
  369. (defmacro ido-ubiquitous-with-override (override &rest body)
  370. "Eval BODY with specified OVERRIDE in place.
  371. The OVERRIDE argument is evaluated normally, so if it is a
  372. literal symbol, it must be quoted.
  373. See `ido-ubiquitous-command-overrides' for valid override types."
  374. (declare (indent 1))
  375. ;; Eval override
  376. `(let ((ido-ubiquitous-next-override ,override))
  377. ,@body))
  378. (defun ido-ubiquitous-apply-function-override (func override)
  379. "Set the override property on FUNC to OVERRIDE and set up advice to apply the override."
  380. (setq func (ido-ubiquitous--as-symbol func)
  381. override (ido-ubiquitous--as-symbol override))
  382. (if (memq override '(disable enable enable-old nil))
  383. (progn
  384. (put func 'ido-ubiquitous-override override)
  385. (when override
  386. (let ((docstring
  387. (format "Override ido-ubiquitous behavior in %s if its `ido-ubiquitous-override' property is non-nil." func)))
  388. (eval
  389. `(defadvice ,func (around ido-ubiquitous-override activate)
  390. ,docstring
  391. (let* ((func ',func)
  392. (override (get func 'ido-ubiquitous-override)))
  393. (when override
  394. (ido-ubiquitous--debug-message
  395. "Using override `%s' for function `%s'"
  396. override func))
  397. (ido-ubiquitous-with-override override
  398. ad-do-it)))))))
  399. (display-warning
  400. 'ido-ubiquitous
  401. (format "Ignoring invalid override action `%s' for function `%s' found in `ido-ubiquitous-function-overrides'."
  402. override func)
  403. :warning)))
  404. (defun ido-ubiquitous-set-function-overrides (sym newval)
  405. "Custom setter function for `ido-ubiquitous-function-overrides'.
  406. In addition to setting the variable, this also sets up advice on
  407. each function to apply the appropriate override."
  408. ;; Unset all previous overrides
  409. (when (boundp sym)
  410. (let ((oldval (eval sym)))
  411. (cl-loop for (_action _match-type func) in oldval
  412. do (ido-ubiquitous-apply-function-override func nil))))
  413. ;; Ensure that function names are strings, not symbols
  414. (setq newval
  415. (cl-loop for (action match-type func) in newval
  416. collect (list action match-type
  417. (ido-ubiquitous--as-string func))))
  418. ;; set new overrides
  419. (cl-loop for override in newval
  420. for (action match-type func) = override
  421. ;; Remove duplicate overrides
  422. if (member func overridden-functions)
  423. do (display-warning
  424. 'ido-ubiquitous
  425. (format
  426. "Removing duplicate override for function `%s'" func))
  427. ;; Apply valid overrides
  428. else if (eq match-type 'exact)
  429. do (ido-ubiquitous-apply-function-override func action)
  430. and collect func into overridden-functions
  431. and collect override into final-value
  432. ;; Remove invalid overrides
  433. else
  434. do (display-warning
  435. 'ido-ubiquitous
  436. (format
  437. "Removing invalid function override match-type `%s' for function `%s'; only match-type `exact' is supported in `ido-ubiquitous-function-overrides'."
  438. match-type func))
  439. ;; Set the value to only the overrides that were actually
  440. ;; applied.
  441. finally return
  442. (set-default sym final-value)))
  443. (defcustom ido-ubiquitous-auto-update-overrides t
  444. "Whether to add new overrides when updating ido-ubiquitous.
  445. Ido-ubiquitous comes with a default set of overrides for commands
  446. that are known to require them. New versions of ido-ubiquitous
  447. may come with updates to the default overrides as more commands
  448. are discovered to require them. However, customizing your own
  449. overrides would normally prevent you from receiving these
  450. updates, since Emacs will not overwrite your customizations.
  451. To resolve this problem, you can set this variable to `t', and
  452. then ido-ubiquitous can automatically add any new built-in
  453. overrides whenever it is updated. (Actually, the update will
  454. happen the next time Emacs is restarted after the update.) This
  455. allows you to add your own overrides but still receive updates to
  456. the default set. The default overrides will always be added with
  457. lower precedence than user-added ones.
  458. If you want ido-ubiquitous to just notify you about new default
  459. overrides instead of adding them itself, set this variable to
  460. `notify'. If you don't want this auto-update behavior at all, set
  461. it to `nil'.
  462. (Note that having this option enabled effectively prevents you
  463. from removing any of the built-in default overrides, since they
  464. will simply be re-added the next time Emacs starts. However, your
  465. custom overrides will still take precedence, so this shouldn't be
  466. a problem.)"
  467. :type '(choice :tag "When new overrides are available:"
  468. (const :menu-tag "Auto-add"
  469. :tag "Add them automatically"
  470. t)
  471. (const :menu-tag "Notify"
  472. :tag "Notify the user about them"
  473. notify)
  474. (const :menu-tag "Ignore"
  475. :tag "Ignore them"
  476. nil))
  477. :group 'ido-ubiquitous)
  478. (defcustom ido-ubiquitous-function-overrides ido-ubiquitous-default-function-overrides
  479. "List of function override specifications for ido-ubiquitous
  480. Function override specifications have a similar structure to
  481. command override specifications (see
  482. `ido-ubiquitous-command-overrides'). A function override
  483. specification has the form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'.
  484. However, `MATCH-TYPE' may ONLY be `exact'; No other match type is
  485. supported.
  486. Note: If multiple overrides are set for the same function, the
  487. first one in the list will take precedence, and the rest will be
  488. ignored and deleted from the override list, with a warning.
  489. Invalid override specifications will also be deleted with a
  490. warning.
  491. If you need to add a new specification to this list, please also file a
  492. bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues
  493. Setting this variable directly has no effect. You must set it
  494. through Customize."
  495. :type '(repeat ido-ubiquitous-function-override-spec)
  496. :set 'ido-ubiquitous-set-function-overrides
  497. :group 'ido-ubiquitous)
  498. (defcustom ido-ubiquitous-allow-on-functional-collection nil
  499. "Allow ido completion when COLLECTION is a function.
  500. The `completing-read' function allows its COLLECTION argument to
  501. be a function instead of a list of choices. Some such functions
  502. simply return a list of completions and are suitable for use with
  503. ido, but others implement more complex behavior and will result
  504. in incorrect behavior if used with ido. Since there is no way to
  505. tell the difference, this preference defaults to nil, which means
  506. that ido-ubiquitous will not work when COLLECTION is a function
  507. unless there is a specific override in effect. To disable this
  508. safeguard and GUARANTEE ERRORS on some functions, you may set
  509. this to non-nil, but this is not recommended."
  510. :type 'boolean
  511. :group 'ido-ubiquitous)
  512. ;;; ido-ubiquitous core
  513. ;; These variable are used to make ido-ubiquitous work properly in the
  514. ;; case that `completing-read' is called recursively (which is
  515. ;; possible when `enable-recursive-minibuffers' is non-nil.)
  516. (defvar ido-ubiquitous-enable-next-call nil
  517. "If non-nil, then the next call to `ido-completing-read' is by ido-ubiquitous.")
  518. (defvar ido-ubiquitous-enable-this-call nil
  519. "If non-nil, then the current call to `ido-completing-read' is by ido-ubiquitous.")
  520. (defvar ido-ubiquitous-next-override nil
  521. "This holds the override to be applied on the next call to `completing-read'.
  522. It's value can be nil or one of the symbols `disable', `enable', or
  523. `enable-old'.
  524. You should not modify this variable directly. Instead use the
  525. macro `ido-ubiquitous-with-override'.")
  526. (defvar ido-ubiquitous-active-override nil
  527. "This holds the override being applied to the current call to `completing-read'.
  528. It's value can be nil or one of the symbols `disable', `enable', or
  529. `enable-old'.
  530. You should not modify this variable directly. Instead use the
  531. macro `ido-ubiquitous-with-override'.")
  532. (defvar ido-ubiquitous-active-state nil
  533. "This holds the ido-ubiquitous mode of operation for the current call to `completing-read'.
  534. It's value can be nil or one of the symbols `disable', `enable', or
  535. `enable-old'.
  536. You should not modify this variable directly.")
  537. (defadvice ido-completing-read (around ido-ubiquitous activate)
  538. "Enable ido-ubiquitous features if this call was done through ido-ubiquitous.
  539. This advice ensures that `ido-ubiquitous-enable-this-call' is set
  540. properly while `ido-completing-read' is executing. This variable
  541. is used to determine whether to enable certain behaviors only for
  542. ido-ubiquitous, not for ordinary ido completion."
  543. ;; Set "this" and clear "next" so it doesn't apply to nested calls.
  544. (let* ((ido-ubiquitous-enable-this-call ido-ubiquitous-enable-next-call)
  545. (ido-ubiquitous-enable-next-call nil)
  546. (ido-ubiquitous-initial-item nil))
  547. ad-do-it))
  548. ;; Signal used to trigger fallback
  549. (put 'ido-ubiquitous-fallback 'error-conditions '(ido-ubiquitous-fallback error))
  550. (put 'ido-ubiquitous-fallback 'error-message "ido-ubiquitous-fallback")
  551. (defun completing-read-ido-ubiquitous
  552. (prompt collection &optional predicate
  553. require-match initial-input
  554. hist def inherit-input-method)
  555. "ido-based method for reading from the minibuffer with completion.
  556. See `completing-read' for the meaning of the arguments.
  557. This function is a wrapper for `ido-completing-read' designed to
  558. be used as the value of `completing-read-function'. Importantly,
  559. it detects edge cases that ido cannot handle and uses normal
  560. completion for them."
  561. (let (;; Save the original arguments in case we need to do the
  562. ;; fallback
  563. (orig-args
  564. (list prompt collection predicate require-match
  565. initial-input hist def inherit-input-method)))
  566. (condition-case sig
  567. (let* (;; Set the active override and clear the "next" one so it
  568. ;; doesn't apply to nested calls.
  569. (ido-ubiquitous-active-override ido-ubiquitous-next-override)
  570. (ido-ubiquitous-next-override nil)
  571. ;; Apply the active override, if any
  572. (ido-ubiquitous-active-state
  573. (or ido-ubiquitous-active-override
  574. ido-ubiquitous-default-state
  575. 'enable)))
  576. ;; If ido-ubiquitous is disabled this time, fall back
  577. (when (eq ido-ubiquitous-active-state 'disable)
  578. (signal 'ido-ubiquitous-fallback
  579. "`ido-ubiquitous-active-state' is `disable'"))
  580. ;; Handle a collection that is a function: either expand
  581. ;; completion list now or fall back
  582. (when (functionp collection)
  583. (if (or ido-ubiquitous-allow-on-functional-collection
  584. (memq ido-ubiquitous-active-override
  585. '(enable enable-old)))
  586. (setq collection (all-completions "" collection predicate))
  587. (signal 'ido-ubiquitous-fallback
  588. "COLLECTION is a function and there is no override")))
  589. (let ((ido-ubiquitous-enable-next-call t))
  590. (ido-completing-read+
  591. prompt collection predicate require-match
  592. initial-input hist def inherit-input-method)))
  593. ;; Handler for ido-ubiquitous-fallback signal
  594. (ido-ubiquitous-fallback
  595. (ido-ubiquitous--explain-fallback sig)
  596. (apply ido-cr+-fallback-function orig-args)))))
  597. (define-obsolete-function-alias 'completing-read-ido 'completing-read-ido-ubiquitous
  598. "ido-ubiquitous 3.0")
  599. ;;; Old-style default support
  600. (defvar ido-ubiquitous-initial-item nil
  601. "The first item selected when ido starts.
  602. This is initialized to the first item in the list of completions
  603. when ido starts, and is cleared when any character is entered
  604. into the prompt or the list is cycled. If it is non-nil and still
  605. equal to the first item in the completion list when ido exits,
  606. then if `ido-ubiquitous-active-state' is `enable-old', ido
  607. returns an empty string instead of the first item on the list.")
  608. (defmacro ido-ubiquitous-set-initial-item (item)
  609. "Wrapper for `(setq ido-ubiquitous-initial-item ITEM)'.
  610. This wrapper differs from simply doing `(setq
  611. ido-ubiquitous-initial-item ITEM)' in several ways. First, it has
  612. no effect (and does not evaluate ITEM) unless
  613. `ido-ubiquitous-active-state' is `enable-old'. Second, it emits
  614. appropriate debug messages."
  615. `(when (eq ido-ubiquitous-active-state 'enable-old)
  616. (let ((item ,item))
  617. (cond
  618. (item
  619. (ido-ubiquitous--debug-message
  620. "Setting `ido-ubiquitous-initial-item' to `%S'."
  621. item))
  622. (ido-ubiquitous-initial-item
  623. (ido-ubiquitous--debug-message "Clearing `ido-ubiquitous-initial-item'.")))
  624. (setq ido-ubiquitous-initial-item item))))
  625. (defadvice ido-read-internal (before ido-ubiquitous-clear-initial-item activate)
  626. (ido-ubiquitous-set-initial-item nil))
  627. (defadvice ido-make-choice-list (after ido-ubiquitous-set-initial-item activate)
  628. (ido-ubiquitous-set-initial-item
  629. (when (and ad-return-value (listp ad-return-value))
  630. (car ad-return-value))))
  631. (defadvice ido-next-match (after ido-ubiquitous-clear-initial-item activate)
  632. (ido-ubiquitous-set-initial-item nil))
  633. (defadvice ido-prev-match (after ido-ubiquitous-clear-initial-item activate)
  634. (ido-ubiquitous-set-initial-item nil))
  635. ;; Clear initial item after `self-insert-command'
  636. (defun ido-ubiquitous-post-insert-hook ()
  637. (eval '(ido-ubiquitous-set-initial-item nil)))
  638. (defun ido-ubiquitous-ido-minibuffer-setup-hook ()
  639. (add-hook
  640. 'post-self-insert-hook
  641. #'ido-ubiquitous-post-insert-hook
  642. nil
  643. 'local))
  644. (add-hook 'ido-minibuffer-setup-hook
  645. #'ido-ubiquitous-ido-minibuffer-setup-hook)
  646. (defun ido-ubiquitous-should-use-old-style-default ()
  647. "Returns non nil if ido-ubiquitous should emulate old-style default.
  648. This function simply encapsulates all the checks that need to be
  649. done in order to decide whether to swap RET and C-j. See
  650. `ido-ubiquitous-default-state' for more information."
  651. ;; These checks outside the loop don't produce debug messages
  652. (and
  653. ;; Only if old-style default enabled
  654. (eq ido-ubiquitous-active-state 'enable-old)
  655. ;; This loop is just an implementation of `and' that reports which
  656. ;; arg was nil for debugging purposes.
  657. (cl-loop
  658. for test in
  659. '((bound-and-true-p ido-cur-item)
  660. ;; Only if completing a list, not a buffer or file
  661. (eq ido-cur-item 'list)
  662. ;; Only if this call was done through ido-ubiquitous
  663. ido-ubiquitous-enable-this-call
  664. ;; Only if default is nil
  665. (null ido-default-item)
  666. ;; Only if input is empty
  667. (string= ido-text "")
  668. ;; Only if `ido-ubiquitous-initial-item' hasn't been cleared
  669. ido-ubiquitous-initial-item
  670. ;; Only if initial item hasn't changed
  671. (string= (car ido-cur-list)
  672. ido-ubiquitous-initial-item))
  673. for test-result = (eval test)
  674. if (not test-result)
  675. do (ido-ubiquitous--debug-message
  676. "Not enabling old-style default selection because `%S' is nil"
  677. test)
  678. and return nil
  679. finally do (ido-ubiquitous--debug-message
  680. "Enabling old-style default selection")
  681. finally return t)))
  682. (defadvice ido-exit-minibuffer (around ido-ubiquitous-old-style-default-compat activate)
  683. "Emulate a quirk of `completing-read'.
  684. > If the input is null, `completing-read' returns DEF, or the
  685. > first element of the list of default values, or an empty string
  686. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  687. See `ido-ubiquitous-default-state', which controls whether this
  688. advice has any effect."
  689. (condition-case nil
  690. (if (ido-ubiquitous-should-use-old-style-default)
  691. (let ((ido-ubiquitous-active-state 'enable))
  692. (ido-select-text))
  693. ad-do-it)
  694. (error
  695. (display-warning 'ido-ubiquitous "Advice on `ido-exit-minibuffer' failed." :warning)
  696. ad-do-it))
  697. (ido-ubiquitous-set-initial-item nil))
  698. (defadvice ido-select-text (around ido-ubiquitous-old-style-default-compat activate)
  699. "Emulate a quirk of `completing-read'.
  700. > If the input is null, `completing-read' returns DEF, or the
  701. > first element of the list of default values, or an empty string
  702. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  703. See `ido-ubiquitous-default-state', which controls whether this
  704. advice has any effect."
  705. (condition-case nil
  706. (if (ido-ubiquitous-should-use-old-style-default)
  707. (let ((ido-ubiquitous-active-state 'enable))
  708. (ido-exit-minibuffer))
  709. ad-do-it)
  710. (error
  711. (display-warning 'ido-ubiquitous "Advice on `ido-select-text' failed." :warning)
  712. ad-do-it))
  713. (ido-ubiquitous-set-initial-item nil))
  714. ;;; Overrides
  715. (defun ido-ubiquitous--overrides-have-same-target-p (o1 o2)
  716. (cl-destructuring-bind (oride1 type1 text1) o1
  717. (cl-destructuring-bind(oride2 type2 text2) o2
  718. ;; Avoid warnings about unused vars
  719. oride1 oride2
  720. (and (string= text1 text2)
  721. (eq type1 type2)))))
  722. (defun ido-ubiquitous--combine-override-lists (olist1 olist2)
  723. "Append OLIST2 to OLIST1, but remove redundant elements.
  724. Redundancy is determined using
  725. `ido-ubiquitous--overrides-have-same-target-p'."
  726. (let ((olist2
  727. (cl-remove-if
  728. (lambda (o2) (cl-member
  729. o2 olist1
  730. :test #'ido-ubiquitous--overrides-have-same-target-p))
  731. olist2)))
  732. (append olist1 olist2)))
  733. (defun ido-ubiquitous-update-overrides (&optional save quiet)
  734. "Re-add the default overrides without erasing custom overrides.
  735. This is useful after an update of ido-ubiquitous that adds new
  736. default overrides. See `ido-ubiquitous-auto-update-overrides' for
  737. more information.
  738. If SAVE is non-nil, also save the overrides to the user's custom
  739. file (but only if they were already customized). When called
  740. interactively, a prefix argument triggers a save."
  741. (interactive "P")
  742. (let ((unmodified-vars nil)
  743. (updated-vars nil)
  744. (final-message-lines nil)
  745. (final-message-is-warning nil))
  746. (cl-loop
  747. for (var def) in
  748. '((ido-ubiquitous-command-overrides
  749. ido-ubiquitous-default-command-overrides)
  750. (ido-ubiquitous-function-overrides
  751. ido-ubiquitous-default-function-overrides))
  752. do (let* ((var-state (custom-variable-state var (eval var)))
  753. (curval (eval var))
  754. (defval (eval def))
  755. (newval (ido-ubiquitous--combine-override-lists
  756. curval defval)))
  757. (cond
  758. ;; Nothing to add to var, do nothing
  759. ((and (equal curval newval)
  760. (eq var-state 'saved))
  761. (ido-ubiquitous--debug-message
  762. "No need to modify value of option `%s'"
  763. var)
  764. (push var unmodified-vars))
  765. ;; Var is not customized, just set the new default
  766. ((eq var-state 'standard)
  767. (ido-ubiquitous--debug-message
  768. "Setting uncustomized option `%s' to its default value"
  769. var)
  770. (push var unmodified-vars)
  771. (set var defval))
  772. ;; Var is customized, set and save new value (if SAVE is t)
  773. (save
  774. (ido-ubiquitous--debug-message
  775. "Updating option `%s' with new overrides and saving it."
  776. var)
  777. (push var updated-vars)
  778. (customize-save-variable var newval))
  779. ;; Var is set but not saved (or SAVE is nil), update it but
  780. ;; don't save it
  781. (t
  782. (ido-ubiquitous--debug-message
  783. "Updating option `%s' with new overrides but not saving it for future sessions."
  784. var)
  785. (push var updated-vars)
  786. (customize-set-variable var newval)))))
  787. (unless quiet
  788. ;; Now compose a single message that summarizes what was done
  789. (if (null updated-vars)
  790. (push "No updates to ido-ubiquitous override variables were needed."
  791. final-message-lines)
  792. (push
  793. (format "Updated the following ido-ubiquitous override variables: %S"
  794. (sort updated-vars #'string<))
  795. final-message-lines)
  796. (if save
  797. (push
  798. "All updated variables were successfully saved."
  799. final-message-lines)
  800. (push
  801. "However, they have not been saved for future sessions. To save them, re-run this command with a prefix argument: `C-u M-x ido-ubiquitous-update-overrides'; or else manually inspect and save their values using `M-x customize-group ido-ubiquitous'."
  802. final-message-lines)
  803. (setq final-message-is-warning t)))
  804. (if final-message-is-warning
  805. (display-warning 'ido-ubiquitous
  806. (s-join "\n" (nreverse final-message-lines)))
  807. (message (s-join "\n" (nreverse final-message-lines)))))))
  808. (defun ido-ubiquitous--find-override-updates (current-value available-updates)
  809. (cl-set-difference (ido-ubiquitous--combine-override-lists
  810. current-value available-updates)
  811. current-value))
  812. (defun ido-ubiquitous--maybe-update-overrides ()
  813. "Maybe call `ido-ubiquitous-update-overrides.
  814. See `ido-ubiquitous-auto-update-overrides."
  815. (if ido-ubiquitous-auto-update-overrides
  816. (let* ((command-override-updates
  817. (ido-ubiquitous--find-override-updates
  818. ido-ubiquitous-command-overrides
  819. ido-ubiquitous-default-command-overrides))
  820. (function-override-updates
  821. (ido-ubiquitous--find-override-updates
  822. ido-ubiquitous-function-overrides
  823. ido-ubiquitous-default-function-overrides))
  824. (update-count
  825. (+ (length command-override-updates)
  826. (length function-override-updates))))
  827. (if (> update-count 0)
  828. (if (eq ido-ubiquitous-auto-update-overrides 'notify)
  829. (display-warning
  830. 'ido-ubiquitous
  831. (format "There are %s new overrides available. Use `M-x ido-ubiquitous-update-overrides' to enable them."
  832. update-count))
  833. (ido-ubiquitous--debug-message "Applying override updates.")
  834. (ido-ubiquitous-update-overrides t))
  835. (ido-ubiquitous--debug-message "No override updates availble.")))
  836. (ido-ubiquitous--debug-message "Skipping override updates by user preference.")))
  837. (define-obsolete-function-alias
  838. 'ido-ubiquitous-restore-default-overrides
  839. 'ido-ubiquitous-update-overrides
  840. "ido-ubiquitous 3.9")
  841. (defun ido-ubiquitous-spec-match (spec symbol)
  842. "Returns t if SPEC matches SYMBOL (which should be a function name).
  843. See `ido-ubiquitous-command-overrides'. If the match spec is
  844. invalid or any other error occurs, the error is demoted to a
  845. warning and the function returns nil."
  846. (condition-case err
  847. (when (and symbol (symbolp symbol))
  848. (cl-destructuring-bind (type text) spec
  849. (let ((matcher (cdr (assoc type ido-ubiquitous-spec-matchers)))
  850. (text (ido-ubiquitous--as-string text))
  851. (symname (ido-ubiquitous--as-string symbol)))
  852. (if matcher
  853. (funcall matcher text symname)
  854. ;; If the matcher is invalid, issue a warning and return
  855. ;; nil.
  856. (error "Unknown match spec type \"%s\". See `ido-ubiquitous-spec-matchers' for valid types." type)
  857. nil))))
  858. (error
  859. (display-warning 'ido-ubiquitous "Error during ido-ubiquitous spec matching: %S" err)
  860. nil)))
  861. (defun ido-ubiquitous-get-command-override (cmd)
  862. "Return the override associated with the command CMD.
  863. If there is no override set for CMD in
  864. `ido-ubiquitous-command-overrides', return nil."
  865. (when (and cmd (symbolp cmd))
  866. (cl-loop for override in ido-ubiquitous-command-overrides
  867. for (action . spec) = override
  868. for valid-action = (and (memq action '(disable enable enable-old nil))
  869. (assoc (car spec) ido-ubiquitous-spec-matchers))
  870. unless valid-action
  871. do (progn
  872. (display-warning
  873. 'ido-ubiquitous
  874. (format "Removing invalid override `%S' from `ido-ubiquitous-command-overrides'"
  875. (cons action spec))
  876. :warning)
  877. (setq ido-ubiquitous-command-overrides
  878. (remove override ido-ubiquitous-command-overrides)))
  879. when (and valid-action (ido-ubiquitous-spec-match spec cmd))
  880. return action
  881. finally return nil)))
  882. ;;; Workaround for https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/24
  883. ;; When `call-interactively' is advised, `called-interactively-p'
  884. ;; always returns nil. So we redefine it (and `interactive-p') to test
  885. ;; the correct condition.
  886. (defsubst ido-ubiquitous--looks-like-advised-orig (func)
  887. "Returns t if FUNC is a symbol starting with \"ad-Orig-\".
  888. Such symbols are used to store the original definitions of
  889. functions that have been advised by `defadvice' or similar."
  890. (and (symbolp func)
  891. (string-prefix-p "ad-Orig-" (symbol-name func))))
  892. (defsubst ido-ubiquitous--looks-like-call-interactively (func)
  893. "Returns t if FUNC looks like the function `call-interactively'.
  894. FUNC \"looks like\" `call-interactively' if it is the literal
  895. symbol `call-interactively', or the value of `(symbol-function
  896. 'call-interactively)', or a symbol whose `symbol-function' is the
  897. same as that of `call-interactively'.
  898. This function is used to determine whether a given function was
  899. \"called by\" `call-interactively' and therefore was called
  900. interactively."
  901. (when func
  902. (eq (symbol-function 'call-interactively)
  903. (if (symbolp func)
  904. (symbol-function func)
  905. func))))
  906. (defun ido-ubiquitous--backtrace-from (fun)
  907. "Return all backtrace frames, starting with the one for FUN.
  908. FUN may be a list of functions, in which case the first one found
  909. on the stack will be used."
  910. (let ((stack
  911. (cl-loop for i upfrom 0
  912. for frame = (backtrace-frame i)
  913. while frame
  914. collect frame))
  915. (funcs (if (functionp fun)
  916. (list fun)
  917. fun)))
  918. (while (and stack
  919. (not (memq (cl-cadar stack) funcs)))
  920. (setq stack (cdr stack)))
  921. stack))
  922. (defun ido-ubiquitous--clean-advice-from-backtrace (stack)
  923. "Takes a stack trace and cleans all evidence of advice.
  924. Specifically, for each call to a function starting with
  925. \"ad-Orig-\", that call and all prior calls up to but not
  926. including the advised function's original name are deleted from
  927. the stack."
  928. (let ((skipping-until nil))
  929. (cl-loop for frame in stack
  930. for func = (cadr frame)
  931. ;; Check if we found the frame we we're skipping to
  932. if (and skipping-until
  933. (eq func skipping-until))
  934. do (setq skipping-until nil)
  935. ;; If we're looking at an the original form of an advised
  936. ;; function, skip until the real name of that function.
  937. if (and (not skipping-until)
  938. (ido-ubiquitous--looks-like-advised-orig func))
  939. do (setq skipping-until
  940. (intern
  941. (substring (symbol-name func)
  942. (eval-when-compile (length "ad-Orig-")))))
  943. unless skipping-until collect frame)))
  944. (defsubst ido-ubiquitous--interactive-internal ()
  945. "Equivalent of the INTERACTIVE macro in the Emacs C source.
  946. This is an internal function that should never be called
  947. directly.
  948. See the C source for the logic behind this function."
  949. (and (not executing-kbd-macro)
  950. (not noninteractive)))
  951. (defun ido-ubiquitous--interactive-p-internal ()
  952. "Equivalent of C function \"interactive_p\".
  953. This is an internal function that should never be called
  954. directly.
  955. See the C source for the logic behind this function."
  956. (let ((stack
  957. ;; We clean advice from the backtrace. This ensures that we
  958. ;; get the right answer even if `call-interactively' has been
  959. ;; advised.
  960. (ido-ubiquitous--clean-advice-from-backtrace
  961. (cdr
  962. (ido-ubiquitous--backtrace-from
  963. '(called-interactively-p interactive-p))))))
  964. ;; See comments in the C function for the logic here.
  965. (while (and stack
  966. (or (eq (cl-cadar stack) 'bytecode)
  967. (null (caar stack))))
  968. (setq stack (cdr stack)))
  969. ;; Top of stack is now the function that we want to know
  970. ;; about. Pop it, then check if the next function is
  971. ;; `call-interactively', using a more permissive test than the default.
  972. (ido-ubiquitous--looks-like-call-interactively (cl-cadadr stack))))
  973. (defadvice call-interactively (around ido-ubiquitous activate)
  974. "Implements the behavior specified in `ido-ubiquitous-command-overrides'."
  975. (let* ((cmd (ad-get-arg 0))
  976. (override (ido-ubiquitous-get-command-override cmd)))
  977. (when override
  978. (ido-ubiquitous--debug-message "Using override `%s' for command `%s'"
  979. override cmd))
  980. (ido-ubiquitous-with-override override
  981. ad-do-it)))
  982. ;; Work around `called-interactively-p' in Emacs 24.3 and earlier,
  983. ;; which always returns nil when `call-interactively' is advised.
  984. (when (not (and (featurep 'nadvice)
  985. (boundp 'called-interactively-p-functions)))
  986. (defadvice interactive-p (around ido-ubiquitous activate)
  987. "Return the correct result when `call-interactively' is advised.
  988. This advice completely overrides the original definition."
  989. (condition-case nil
  990. (setq ad-return-value
  991. (and (ido-ubiquitous--interactive-internal)
  992. (ido-ubiquitous--interactive-p-internal)))
  993. ;; In case of error in the advice, fall back to the default
  994. ;; implementation
  995. (error ad-do-it)))
  996. (defadvice called-interactively-p (around ido-ubiquitous activate)
  997. "Return the correct result when `call-interactively' is advised.
  998. This advice completely overrides the original definition."
  999. (condition-case nil
  1000. (setq ad-return-value
  1001. (and (or (ido-ubiquitous--interactive-internal)
  1002. (not (eq kind 'interactive)))
  1003. (ido-ubiquitous--interactive-p-internal)))
  1004. ;; In case of error in the advice, fall back to the default
  1005. ;; implementation
  1006. (error ad-do-it))))
  1007. ;;; Other
  1008. (defsubst ido-ubiquitous--fixup-old-advice ()
  1009. ;; Clean up old versions of ido-ubiquitous advice if they exist
  1010. (ignore-errors (ad-remove-advice 'completing-read 'around 'ido-ubiquitous))
  1011. (ignore-errors (ad-remove-advice 'ido-completing-read 'around 'detect-replacing-cr))
  1012. (ignore-errors (ad-remove-advice 'ido-magic-forward-char 'before 'ido-ubiquitous-fallback))
  1013. (ignore-errors (ad-remove-advice 'ido-magic-backward-char 'before 'ido-ubiquitous-fallback))
  1014. (ignore-errors (ad-remove-advice 'ido-exit-minibuffer 'around 'compatibility))
  1015. (ad-activate-all))
  1016. (defsubst ido-ubiquitous--fixup-old-magit-overrides ()
  1017. (let ((old-override '(disable prefix "magit-"))
  1018. (new-override '(disable exact "magit-builtin-completing-read")))
  1019. (when (member old-override
  1020. ido-ubiquitous-command-overrides)
  1021. (customize-set-variable
  1022. 'ido-ubiquitous-command-overrides
  1023. (remove old-override ido-ubiquitous-command-overrides))
  1024. (unless (member new-override ido-ubiquitous-function-overrides)
  1025. (customize-set-variable 'ido-ubiquitous-function-overrides
  1026. (append ido-ubiquitous-function-overrides
  1027. (list new-override))))
  1028. (display-warning
  1029. 'ido-ubiquitous
  1030. "Fixing obsolete magit overrides.
  1031. Magit has changed recently such that the old override that
  1032. ido-ubiquitous defined for it now causes problems. This old
  1033. override has been automatically removed and the new one added.
  1034. Please use `M-x customize-group ido-ubiquitous' and review the
  1035. override variables and save them to your customization file."
  1036. :warning))))
  1037. (defun ido-ubiquitous-initialize ()
  1038. "Do initial setup for ido-ubiquitous.
  1039. This only needs to be called once when the file is first loaded.
  1040. It cleans up any traces of old versions of ido-ubiquitous and
  1041. then sets up the mode."
  1042. (ido-ubiquitous--fixup-old-advice)
  1043. (ido-ubiquitous--fixup-old-magit-overrides)
  1044. (ido-ubiquitous--maybe-update-overrides)
  1045. ;; Make sure the mode is turned on/off as specified by the value of
  1046. ;; the mode variable
  1047. (ido-ubiquitous-mode (if ido-ubiquitous-mode 1 0)))
  1048. (ido-ubiquitous-initialize)
  1049. (provide 'ido-ubiquitous)
  1050. ;; Local Variables:
  1051. ;; indent-tabs-mode: nil
  1052. ;; End:
  1053. ;;; ido-ubiquitous.el ends here