ido-ubiquitous.el 50 KB

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