ido-ubiquitous.el 46 KB

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