ido-ubiquitous.el 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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
  435. (set-default-toplevel-value sym final-value)))
  436. (defcustom ido-ubiquitous-auto-update-overrides t
  437. "Whether to add new overrides when updating ido-ubiquitous.
  438. Ido-ubiquitous comes with a default set of overrides for commands
  439. that are known to require them. New versions of ido-ubiquitous
  440. may come with updates to the default overrides as more commands
  441. are discovered to require them. However, customizing your own
  442. overrides would normally prevent you from receiving these
  443. updates, since Emacs will not overwrite your customizations.
  444. To resolve this problem, you can set this variable to `t', and
  445. then ido-ubiquitous can automatically add any new built-in
  446. overrides whenever it is updated. (Actually, the update will
  447. happen the next time Emacs is restarted after the update.) This
  448. allows you to add your own overrides but still receive updates to
  449. the default set. The default overrides will always be added with
  450. lower precedence than user-added ones.
  451. If you want ido-ubiquitous to just notify you about new default
  452. overrides instead of adding them itself, set this variable to
  453. `notify'. If you don't want this auto-update behavior at all, set
  454. it to `nil'.
  455. (Note that having this option enabled effectively prevents you
  456. from removing any of the built-in default overrides, since they
  457. will simply be re-added the next time Emacs starts. However, your
  458. custom overrides will still take precedence, so this shouldn't be
  459. a problem.)"
  460. :type '(choice :tag "When new overrides are available:"
  461. (const :menu-tag "Auto-add"
  462. :tag "Add them automatically"
  463. t)
  464. (const :menu-tag "Notify"
  465. :tag "Notify the user about them"
  466. notify)
  467. (const :menu-tag "Ignore"
  468. :tag "Ignore them"
  469. nil))
  470. :group 'ido-ubiquitous)
  471. (defcustom ido-ubiquitous-function-overrides ido-ubiquitous-default-function-overrides
  472. "List of function override specifications for ido-ubiquitous
  473. Function override specifications have a similar structure to
  474. command override specifications (see
  475. `ido-ubiquitous-command-overrides'). A function override
  476. specification has the form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'.
  477. However, `MATCH-TYPE' may ONLY be `exact'; No other match type is
  478. supported.
  479. Note: If multiple overrides are set for the same function, the
  480. first one in the list will take precedence, and the rest will be
  481. ignored and deleted from the override list, with a warning.
  482. Invalid override specifications will also be deleted with a
  483. warning.
  484. If you need to add a new specification to this list, please also file a
  485. bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues
  486. Setting this variable directly has no effect. You must set it
  487. through Customize."
  488. :type '(repeat ido-ubiquitous-function-override-spec)
  489. :set 'ido-ubiquitous-set-function-overrides
  490. :group 'ido-ubiquitous)
  491. (defcustom ido-ubiquitous-allow-on-functional-collection nil
  492. "Allow ido completion when COLLECTION is a function.
  493. The `completing-read' function allows its COLLECTION argument to
  494. be a function instead of a list of choices. Some such functions
  495. simply return a list of completions and are suitable for use with
  496. ido, but others implement more complex behavior and will result
  497. in incorrect behavior if used with ido. Since there is no way to
  498. tell the difference, this preference defaults to nil, which means
  499. that ido-ubiquitous will not work when COLLECTION is a function
  500. unless there is a specific override in effect. To disable this
  501. safeguard and GUARANTEE ERRORS on some functions, you may set
  502. this to non-nil, but this is not recommended."
  503. :type 'boolean
  504. :group 'ido-ubiquitous)
  505. ;;; ido-ubiquitous core
  506. ;; These variable are used to make ido-ubiquitous work properly in the
  507. ;; case that `completing-read' is called recursively (which is
  508. ;; possible when `enable-recursive-minibuffers' is non-nil.)
  509. (defvar ido-ubiquitous-enable-next-call nil
  510. "If non-nil, then the next call to `ido-completing-read' is by ido-ubiquitous.")
  511. (defvar ido-ubiquitous-enable-this-call nil
  512. "If non-nil, then the current call to `ido-completing-read' is by ido-ubiquitous.")
  513. (defvar ido-ubiquitous-next-override nil
  514. "This holds the override to be applied on the next call to `completing-read'.
  515. It's value can be nil or one of the symbols `disable', `enable', or
  516. `enable-old'.
  517. You should not modify this variable directly. Instead use the
  518. macro `ido-ubiquitous-with-override'.")
  519. (defvar ido-ubiquitous-active-override nil
  520. "This holds the override being applied to the current call to `completing-read'.
  521. It's value can be nil or one of the symbols `disable', `enable', or
  522. `enable-old'.
  523. You should not modify this variable directly. Instead use the
  524. macro `ido-ubiquitous-with-override'.")
  525. (defvar ido-ubiquitous-active-state nil
  526. "This holds the ido-ubiquitous mode of operation for the current call to `completing-read'.
  527. It's value can be nil or one of the symbols `disable', `enable', or
  528. `enable-old'.
  529. You should not modify this variable directly.")
  530. (defadvice ido-completing-read (around ido-ubiquitous activate)
  531. "Enable ido-ubiquitous features if this call was done through ido-ubiquitous.
  532. This advice ensures that `ido-ubiquitous-enable-this-call' is set
  533. properly while `ido-completing-read' is executing. This variable
  534. is used to determine whether to enable certain behaviors only for
  535. ido-ubiquitous, not for ordinary ido completion."
  536. ;; Set "this" and clear "next" so it doesn't apply to nested calls.
  537. (let* ((ido-ubiquitous-enable-this-call ido-ubiquitous-enable-next-call)
  538. (ido-ubiquitous-enable-next-call nil)
  539. (ido-ubiquitous-initial-item nil))
  540. ad-do-it))
  541. ;; Signal used to trigger fallback
  542. (put 'ido-ubiquitous-fallback 'error-conditions '(ido-ubiquitous-fallback error))
  543. (put 'ido-ubiquitous-fallback 'error-message "ido-ubiquitous-fallback")
  544. (defun completing-read-ido-ubiquitous
  545. (prompt collection &optional predicate
  546. require-match initial-input
  547. hist def inherit-input-method)
  548. "ido-based method for reading from the minibuffer with completion.
  549. See `completing-read' for the meaning of the arguments.
  550. This function is a wrapper for `ido-completing-read' designed to
  551. be used as the value of `completing-read-function'. Importantly,
  552. it detects edge cases that ido cannot handle and uses normal
  553. completion for them."
  554. (let (;; Save the original arguments in case we need to do the
  555. ;; fallback
  556. (orig-args
  557. (list prompt collection predicate require-match
  558. initial-input hist def inherit-input-method)))
  559. (condition-case sig
  560. (let* (;; Set the active override and clear the "next" one so it
  561. ;; doesn't apply to nested calls.
  562. (ido-ubiquitous-active-override ido-ubiquitous-next-override)
  563. (ido-ubiquitous-next-override nil)
  564. ;; Apply the active override, if any
  565. (ido-ubiquitous-active-state
  566. (or ido-ubiquitous-active-override
  567. ido-ubiquitous-default-state
  568. 'enable)))
  569. ;; If ido-ubiquitous is disabled this time, fall back
  570. (when (eq ido-ubiquitous-active-state 'disable)
  571. (signal 'ido-ubiquitous-fallback
  572. "`ido-ubiquitous-active-state' is `disable'"))
  573. ;; Handle a collection that is a function: either expand
  574. ;; completion list now or fall back
  575. (when (functionp collection)
  576. (if (or ido-ubiquitous-allow-on-functional-collection
  577. (memq ido-ubiquitous-active-override
  578. '(enable enable-old)))
  579. (setq collection (all-completions "" collection predicate))
  580. (signal 'ido-ubiquitous-fallback
  581. "COLLECTION is a function and there is no override")))
  582. (let ((ido-ubiquitous-enable-next-call t))
  583. (ido-completing-read+
  584. prompt collection predicate require-match
  585. initial-input hist def inherit-input-method)))
  586. ;; Handler for ido-ubiquitous-fallback signal
  587. (ido-ubiquitous-fallback
  588. (ido-ubiquitous--explain-fallback sig)
  589. (apply ido-cr+-fallback-function orig-args)))))
  590. (define-obsolete-function-alias 'completing-read-ido 'completing-read-ido-ubiquitous
  591. "ido-ubiquitous 3.0")
  592. ;;; Old-style default support
  593. (defvar ido-ubiquitous-initial-item nil
  594. "The first item selected when ido starts.
  595. This is initialized to the first item in the list of completions
  596. when ido starts, and is cleared when any character is entered
  597. into the prompt or the list is cycled. If it is non-nil and still
  598. equal to the first item in the completion list when ido exits,
  599. then if `ido-ubiquitous-active-state' is `enable-old', ido
  600. returns an empty string instead of the first item on the list.")
  601. (defmacro ido-ubiquitous-set-initial-item (item)
  602. "Wrapper for `(setq ido-ubiquitous-initial-item ITEM)'.
  603. This wrapper differs from simply doing `(setq
  604. ido-ubiquitous-initial-item ITEM)' in several ways. First, it has
  605. no effect (and does not evaluate ITEM) unless
  606. `ido-ubiquitous-active-state' is `enable-old'. Second, it emits
  607. appropriate debug messages."
  608. `(when (eq ido-ubiquitous-active-state 'enable-old)
  609. (let ((item ,item))
  610. (cond
  611. (item
  612. (ido-ubiquitous--debug-message
  613. "Setting `ido-ubiquitous-initial-item' to `%S'."
  614. item))
  615. (ido-ubiquitous-initial-item
  616. (ido-ubiquitous--debug-message "Clearing `ido-ubiquitous-initial-item'.")))
  617. (setq ido-ubiquitous-initial-item item))))
  618. (defadvice ido-read-internal (before ido-ubiquitous-clear-initial-item activate)
  619. (ido-ubiquitous-set-initial-item nil))
  620. (defadvice ido-make-choice-list (after ido-ubiquitous-set-initial-item activate)
  621. (ido-ubiquitous-set-initial-item
  622. (when (and ad-return-value (listp ad-return-value))
  623. (car ad-return-value))))
  624. (defadvice ido-next-match (after ido-ubiquitous-clear-initial-item activate)
  625. (ido-ubiquitous-set-initial-item nil))
  626. (defadvice ido-prev-match (after ido-ubiquitous-clear-initial-item activate)
  627. (ido-ubiquitous-set-initial-item nil))
  628. ;; Clear initial item after `self-insert-command'
  629. (defun ido-ubiquitous-post-insert-hook ()
  630. (eval '(ido-ubiquitous-set-initial-item nil)))
  631. (defun ido-ubiquitous-ido-minibuffer-setup-hook ()
  632. (add-hook
  633. 'post-self-insert-hook
  634. #'ido-ubiquitous-post-insert-hook
  635. nil
  636. 'local))
  637. (add-hook 'ido-minibuffer-setup-hook
  638. #'ido-ubiquitous-ido-minibuffer-setup-hook)
  639. (defun ido-ubiquitous-should-use-old-style-default ()
  640. "Returns non nil if ido-ubiquitous should emulate old-style default.
  641. This function simply encapsulates all the checks that need to be
  642. done in order to decide whether to swap RET and C-j. See
  643. `ido-ubiquitous-default-state' for more information."
  644. ;; These checks outside the loop don't produce debug messages
  645. (and
  646. ;; Only if old-style default enabled
  647. (eq ido-ubiquitous-active-state 'enable-old)
  648. ;; This loop is just an implementation of `and' that reports which
  649. ;; arg was nil for debugging purposes.
  650. (cl-loop
  651. for test in
  652. '((bound-and-true-p ido-cur-item)
  653. ;; Only if completing a list, not a buffer or file
  654. (eq ido-cur-item 'list)
  655. ;; Only if this call was done through ido-ubiquitous
  656. ido-ubiquitous-enable-this-call
  657. ;; Only if default is nil
  658. (null ido-default-item)
  659. ;; Only if input is empty
  660. (string= ido-text "")
  661. ;; Only if `ido-ubiquitous-initial-item' hasn't been cleared
  662. ido-ubiquitous-initial-item
  663. ;; Only if initial item hasn't changed
  664. (string= (car ido-cur-list)
  665. ido-ubiquitous-initial-item))
  666. for test-result = (eval test)
  667. if (not test-result)
  668. do (ido-ubiquitous--debug-message
  669. "Not enabling old-style default selection because `%S' is nil"
  670. test)
  671. and return nil
  672. finally do (ido-ubiquitous--debug-message
  673. "Enabling old-style default selection")
  674. finally return t)))
  675. (defadvice ido-exit-minibuffer (around ido-ubiquitous-old-style-default-compat activate)
  676. "Emulate a quirk of `completing-read'.
  677. > If the input is null, `completing-read' returns DEF, or the
  678. > first element of the list of default values, or an empty string
  679. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  680. See `ido-ubiquitous-default-state', which controls whether this
  681. advice has any effect."
  682. (condition-case nil
  683. (if (ido-ubiquitous-should-use-old-style-default)
  684. (let ((ido-ubiquitous-active-state 'enable))
  685. (ido-select-text))
  686. ad-do-it)
  687. (error
  688. (display-warning 'ido-ubiquitous "Advice on `ido-exit-minibuffer' failed." :warning)
  689. ad-do-it))
  690. (ido-ubiquitous-set-initial-item nil))
  691. (defadvice ido-select-text (around ido-ubiquitous-old-style-default-compat activate)
  692. "Emulate a quirk of `completing-read'.
  693. > If the input is null, `completing-read' returns DEF, or the
  694. > first element of the list of default values, or an empty string
  695. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  696. See `ido-ubiquitous-default-state', which controls whether this
  697. advice has any effect."
  698. (condition-case nil
  699. (if (ido-ubiquitous-should-use-old-style-default)
  700. (let ((ido-ubiquitous-active-state 'enable))
  701. (ido-exit-minibuffer))
  702. ad-do-it)
  703. (error
  704. (display-warning 'ido-ubiquitous "Advice on `ido-select-text' failed." :warning)
  705. ad-do-it))
  706. (ido-ubiquitous-set-initial-item nil))
  707. ;;; Overrides
  708. (defun ido-ubiquitous--overrides-have-same-target-p (o1 o2)
  709. (cl-destructuring-bind (oride1 type1 text1) o1
  710. (cl-destructuring-bind(oride2 type2 text2) o2
  711. (and (string= text1 text2)
  712. (eq type1 type2)))))
  713. (defun ido-ubiquitous--combine-override-lists (olist1 olist2)
  714. "Append OLIST2 to OLIST1, but remove redundant elements.
  715. Redundancy is determined using
  716. `ido-ubiquitous--overrides-have-same-target-p'."
  717. (let ((olist2
  718. (cl-remove-if
  719. (lambda (o2) (cl-member
  720. o2 olist1
  721. :test #'ido-ubiquitous--overrides-have-same-target-p))
  722. olist2)))
  723. (append olist1 olist2)))
  724. (defun ido-ubiquitous-update-overrides (&optional save quiet)
  725. "Re-add the default overrides without erasing custom overrides.
  726. This is useful after an update of ido-ubiquitous that adds new
  727. default overrides. See `ido-ubiquitous-auto-update-overrides' for
  728. more information.
  729. If SAVE is non-nil, also save the overrides to the user's custom
  730. file (but only if they were already customized). When called
  731. interactively, a prefix argument triggers a save."
  732. (interactive "P")
  733. (let ((unmodified-vars nil)
  734. (updated-vars nil)
  735. (updated-vars nil)
  736. (final-message-lines nil)
  737. (final-message-is-warning nil))
  738. (cl-loop
  739. for (var def) in
  740. '((ido-ubiquitous-command-overrides
  741. ido-ubiquitous-default-command-overrides)
  742. (ido-ubiquitous-function-overrides
  743. ido-ubiquitous-default-function-overrides))
  744. do (let* ((var-state (custom-variable-state var (eval var)))
  745. (curval (eval var))
  746. (defval (eval def))
  747. (newval (ido-ubiquitous--combine-override-lists
  748. curval defval)))
  749. (cond
  750. ;; Nothing to add to var, do nothing
  751. ((and (equal curval newval)
  752. (eq var-state 'saved))
  753. (ido-ubiquitous--debug-message
  754. "No need to modify value of option `%s'"
  755. var)
  756. (push var unmodified-vars))
  757. ;; Var is not customized, just set the new default
  758. ((eq var-state 'standard)
  759. (ido-ubiquitous--debug-message
  760. "Setting uncustomized option `%s' to its default value"
  761. var)
  762. (push var unmodified-vars)
  763. (set var defval))
  764. ;; Var is customized, set and save new value (if SAVE is t)
  765. (save
  766. (ido-ubiquitous--debug-message
  767. "Updating option `%s' with new overrides and saving it."
  768. var)
  769. (push var updated-vars)
  770. (customize-save-variable var newval))
  771. ;; Var is set but not saved (or SAVE is nil), update it but
  772. ;; don't save it
  773. (t
  774. (ido-ubiquitous--debug-message
  775. "Updating option `%s' with new overrides but not saving it for future sessions."
  776. var)
  777. (push var updated-vars)
  778. (customize-set-variable var newval)))))
  779. (unless quiet
  780. ;; Now compose a single message that summarizes what was done
  781. (if (null updated-vars)
  782. (push "No updates to ido-ubiquitous override variables were needed."
  783. final-message-lines)
  784. (push
  785. (format "Updated the following ido-ubiquitous override variables: %S"
  786. (sort updated-vars #'string<))
  787. final-message-lines)
  788. (if save
  789. (push
  790. "All updated variables were successfully saved."
  791. final-message-lines)
  792. (push
  793. "However, they have not been saved for future sessions. To save them, re-run this command with a prefix argument: `C-u M-x ido-ubiquitous-update-overrides'; or else manually inspect and save their values using `M-x customize-group ido-ubiquitous'."
  794. final-message-lines)
  795. (setq final-message-is-warning t)))
  796. (if final-message-is-warning
  797. (display-warning 'ido-ubiquitous
  798. (s-join "\n" (nreverse final-message-lines)))
  799. (message (s-join "\n" (nreverse final-message-lines)))))))
  800. (defun ido-ubiquitous--find-override-updates (current-value available-updates)
  801. (cl-set-difference (ido-ubiquitous--combine-override-lists
  802. current-value available-updates)
  803. current-value))
  804. (defun ido-ubiquitous--maybe-update-overrides ()
  805. "Maybe call `ido-ubiquitous-update-overrides.
  806. See `ido-ubiquitous-auto-update-overrides."
  807. (if ido-ubiquitous-auto-update-overrides
  808. (let* ((command-override-updates
  809. (ido-ubiquitous--find-override-updates
  810. ido-ubiquitous-command-overrides
  811. ido-ubiquitous-default-command-overrides))
  812. (function-override-updates
  813. (ido-ubiquitous--find-override-updates
  814. ido-ubiquitous-function-overrides
  815. ido-ubiquitous-default-function-overrides))
  816. (update-count
  817. (+ (length command-override-updates)
  818. (length function-override-updates))))
  819. (if (> update-count 0)
  820. (if (eq ido-ubiquitous-auto-update-overrides 'notify)
  821. (display-warning
  822. 'ido-ubiquitous
  823. (format "There are %s new overrides available. Use `M-x ido-ubiquitous-update-overrides' to enable them."
  824. update-count))
  825. (ido-ubiquitous--debug-message "Applying override updates.")
  826. (ido-ubiquitous-update-overrides t))
  827. (ido-ubiquitous--debug-message "No override updates availble.")))
  828. (ido-ubiquitous--debug-message "Skipping override updates by user preference.")))
  829. (define-obsolete-function-alias
  830. 'ido-ubiquitous-restore-default-overrides
  831. 'ido-ubiquitous-update-overrides
  832. "ido-ubiquitous 3.9")
  833. (defun ido-ubiquitous-spec-match (spec symbol)
  834. "Returns t if SPEC matches SYMBOL (which should be a function name).
  835. See `ido-ubiquitous-command-overrides'. If the match spec is
  836. invalid or any other error occurs, the error is demoted to a
  837. warning and the function returns nil."
  838. (condition-case err
  839. (when (and symbol (symbolp symbol))
  840. (cl-destructuring-bind (type text) spec
  841. (let ((matcher (cdr (assoc type ido-ubiquitous-spec-matchers)))
  842. (text (ido-ubiquitous--as-string text))
  843. (symname (ido-ubiquitous--as-string symbol)))
  844. (if matcher
  845. (funcall matcher text symname)
  846. ;; If the matcher is invalid, issue a warning and return
  847. ;; nil.
  848. (error "Unknown match spec type \"%s\". See `ido-ubiquitous-spec-matchers' for valid types." type)
  849. nil))))
  850. (error
  851. (display-warning 'ido-ubiquitous "Error during ido-ubiquitous spec matching: %S" err)
  852. nil)))
  853. (defun ido-ubiquitous-get-command-override (cmd)
  854. "Return the override associated with the command CMD.
  855. If there is no override set for CMD in
  856. `ido-ubiquitous-command-overrides', return nil."
  857. (when (and cmd (symbolp cmd))
  858. (cl-loop for override in ido-ubiquitous-command-overrides
  859. for (action . spec) = override
  860. for valid-action = (and (memq action '(disable enable enable-old nil))
  861. (assoc (car spec) ido-ubiquitous-spec-matchers))
  862. unless valid-action
  863. do (progn
  864. (display-warning
  865. 'ido-ubiquitous
  866. (format "Removing invalid override `%S' from `ido-ubiquitous-command-overrides'"
  867. (cons action spec))
  868. :warning)
  869. (setq ido-ubiquitous-command-overrides
  870. (remove override ido-ubiquitous-command-overrides)))
  871. when (and valid-action (ido-ubiquitous-spec-match spec cmd))
  872. return action
  873. finally return nil)))
  874. ;;; Workaround for https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/24
  875. ;; When `call-interactively' is advised, `called-interactively-p'
  876. ;; always returns nil. So we redefine it (and `interactive-p') to test
  877. ;; the correct condition.
  878. (defsubst ido-ubiquitous--looks-like-advised-orig (func)
  879. "Returns t if FUNC is a symbol starting with \"ad-Orig-\".
  880. Such symbols are used to store the original definitions of
  881. functions that have been advised by `defadvice' or similar."
  882. (and (symbolp func)
  883. (string-prefix-p "ad-Orig-" (symbol-name func))))
  884. (defsubst ido-ubiquitous--looks-like-call-interactively (func)
  885. "Returns t if FUNC looks like the function `call-interactively'.
  886. FUNC \"looks like\" `call-interactively' if it is the literal
  887. symbol `call-interactively', or the value of `(symbol-function
  888. 'call-interactively)', or a symbol whose `symbol-function' is the
  889. same as that of `call-interactively'.
  890. This function is used to determine whether a given function was
  891. \"called by\" `call-interactively' and therefore was called
  892. interactively."
  893. (when func
  894. (eq (symbol-function 'call-interactively)
  895. (if (symbolp func)
  896. (symbol-function func)
  897. func))))
  898. (defun ido-ubiquitous--backtrace-from (fun)
  899. "Return all backtrace frames, starting with the one for FUN.
  900. FUN may be a list of functions, in which case the first one found
  901. on the stack will be used."
  902. (let ((stack
  903. (cl-loop for i upfrom 0
  904. for frame = (backtrace-frame i)
  905. while frame
  906. collect frame))
  907. (funcs (if (functionp fun)
  908. (list fun)
  909. fun)))
  910. (while (and stack
  911. (not (memq (cl-cadar stack) funcs)))
  912. (setq stack (cdr stack)))
  913. stack))
  914. (defun ido-ubiquitous--clean-advice-from-backtrace (stack)
  915. "Takes a stack trace and cleans all evidence of advice.
  916. Specifically, for each call to a function starting with
  917. \"ad-Orig-\", that call and all prior calls up to but not
  918. including the advised function's original name are deleted from
  919. the stack."
  920. (let ((skipping-until nil))
  921. (cl-loop for frame in stack
  922. for func = (cadr frame)
  923. ;; Check if we found the frame we we're skipping to
  924. if (and skipping-until
  925. (eq func skipping-until))
  926. do (setq skipping-until nil)
  927. ;; If we're looking at an the original form of an advised
  928. ;; function, skip until the real name of that function.
  929. if (and (not skipping-until)
  930. (ido-ubiquitous--looks-like-advised-orig func))
  931. do (setq skipping-until
  932. (intern
  933. (substring (symbol-name func)
  934. (eval-when-compile (length "ad-Orig-")))))
  935. unless skipping-until collect frame)))
  936. (defsubst ido-ubiquitous--interactive-internal ()
  937. "Equivalent of the INTERACTIVE macro in the Emacs C source.
  938. This is an internal function that should never be called
  939. directly.
  940. See the C source for the logic behind this function."
  941. (and (not executing-kbd-macro)
  942. (not noninteractive)))
  943. (defun ido-ubiquitous--interactive-p-internal ()
  944. "Equivalent of C function \"interactive_p\".
  945. This is an internal function that should never be called
  946. directly.
  947. See the C source for the logic behind this function."
  948. (let ((stack
  949. ;; We clean advice from the backtrace. This ensures that we
  950. ;; get the right answer even if `call-interactively' has been
  951. ;; advised.
  952. (ido-ubiquitous--clean-advice-from-backtrace
  953. (cdr
  954. (ido-ubiquitous--backtrace-from
  955. '(called-interactively-p interactive-p))))))
  956. ;; See comments in the C function for the logic here.
  957. (while (and stack
  958. (or (eq (cl-cadar stack) 'bytecode)
  959. (null (caar stack))))
  960. (setq stack (cdr stack)))
  961. ;; Top of stack is now the function that we want to know
  962. ;; about. Pop it, then check if the next function is
  963. ;; `call-interactively', using a more permissive test than the default.
  964. (ido-ubiquitous--looks-like-call-interactively (cl-cadadr stack))))
  965. (defadvice call-interactively (around ido-ubiquitous activate)
  966. "Implements the behavior specified in `ido-ubiquitous-command-overrides'."
  967. (let* ((cmd (ad-get-arg 0))
  968. (override (ido-ubiquitous-get-command-override cmd)))
  969. (when override
  970. (ido-ubiquitous--debug-message "Using override `%s' for command `%s'"
  971. override cmd))
  972. (ido-ubiquitous-with-override override
  973. ad-do-it)))
  974. ;; Work around `called-interactively-p' in Emacs 24.3 and earlier,
  975. ;; which always returns nil when `call-interactively' is advised.
  976. (when (not (and (featurep 'nadvice)
  977. (boundp 'called-interactively-p-functions)))
  978. (defadvice interactive-p (around ido-ubiquitous activate)
  979. "Return the correct result when `call-interactively' is advised.
  980. This advice completely overrides the original definition."
  981. (condition-case nil
  982. (setq ad-return-value
  983. (and (ido-ubiquitous--interactive-internal)
  984. (ido-ubiquitous--interactive-p-internal)))
  985. ;; In case of error in the advice, fall back to the default
  986. ;; implementation
  987. (error ad-do-it)))
  988. (defadvice called-interactively-p (around ido-ubiquitous activate)
  989. "Return the correct result when `call-interactively' is advised.
  990. This advice completely overrides the original definition."
  991. (condition-case nil
  992. (setq ad-return-value
  993. (and (or (ido-ubiquitous--interactive-internal)
  994. (not (eq kind 'interactive)))
  995. (ido-ubiquitous--interactive-p-internal)))
  996. ;; In case of error in the advice, fall back to the default
  997. ;; implementation
  998. (error ad-do-it))))
  999. ;;; Other
  1000. ;; This is defined at the end so it goes at the bottom of the
  1001. ;; customization group
  1002. ;;;###autoload
  1003. (define-minor-mode ido-ubiquitous-debug-mode
  1004. "If non-nil, ido-ubiquitous will print debug info.
  1005. Debug info is printed to the *Messages* buffer."
  1006. nil
  1007. :global t
  1008. :group 'ido-ubiquitous)
  1009. (defsubst ido-ubiquitous--fixup-old-advice ()
  1010. ;; Clean up old versions of ido-ubiquitous advice if they exist
  1011. (ignore-errors (ad-remove-advice 'completing-read 'around 'ido-ubiquitous))
  1012. (ignore-errors (ad-remove-advice 'ido-completing-read 'around 'detect-replacing-cr))
  1013. (ignore-errors (ad-remove-advice 'ido-magic-forward-char 'before 'ido-ubiquitous-fallback))
  1014. (ignore-errors (ad-remove-advice 'ido-magic-backward-char 'before 'ido-ubiquitous-fallback))
  1015. (ignore-errors (ad-remove-advice 'ido-exit-minibuffer 'around 'compatibility))
  1016. (ad-activate-all))
  1017. (defsubst ido-ubiquitous--fixup-old-magit-overrides ()
  1018. (let ((old-override '(disable prefix "magit-"))
  1019. (new-override '(disable exact "magit-builtin-completing-read")))
  1020. (when (member old-override
  1021. ido-ubiquitous-command-overrides)
  1022. (customize-set-variable
  1023. 'ido-ubiquitous-command-overrides
  1024. (remove old-override ido-ubiquitous-command-overrides))
  1025. (unless (member new-override ido-ubiquitous-function-overrides)
  1026. (customize-set-variable 'ido-ubiquitous-function-overrides
  1027. (append ido-ubiquitous-function-overrides
  1028. (list new-override))))
  1029. (display-warning
  1030. 'ido-ubiquitous
  1031. "Fixing obsolete magit overrides.
  1032. Magit has changed recently such that the old override that
  1033. ido-ubiquitous defined for it now causes problems. This old
  1034. override has been automatically removed and the new one added.
  1035. Please use `M-x customize-group ido-ubiquitous' and review the
  1036. override variables and save them to your customization file."
  1037. :warning))))
  1038. (defun ido-ubiquitous-initialize ()
  1039. "Do initial setup for ido-ubiquitous.
  1040. This only needs to be called once when the file is first loaded.
  1041. It cleans up any traces of old versions of ido-ubiquitous and
  1042. then sets up the mode."
  1043. (ido-ubiquitous--fixup-old-advice)
  1044. (ido-ubiquitous--fixup-old-magit-overrides)
  1045. (ido-ubiquitous--maybe-update-overrides)
  1046. ;; Make sure the mode is turned on/off as specified by the value of
  1047. ;; the mode variable
  1048. (ido-ubiquitous-mode (if ido-ubiquitous-mode 1 0)))
  1049. (ido-ubiquitous-initialize)
  1050. (provide 'ido-ubiquitous)
  1051. ;; Local Variables:
  1052. ;; indent-tabs-mode: nil
  1053. ;; End:
  1054. ;;; ido-ubiquitous.el ends here