ido-ubiquitous.el 51 KB

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