ido-ubiquitous.el 50 KB

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