ido-ubiquitous.el 48 KB

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