ido-ubiquitous.el 51 KB

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