diff options
| author | Craig Jennings <c@cjennings.net> | 2026-06-25 12:15:34 -0400 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-06-25 12:15:34 -0400 |
| commit | 33621b5a6e5407da190767b89756e287698ef234 (patch) | |
| tree | 5ff74e05a81a4ace888a18245304cc28c51dfc3c | |
| parent | 4266923daf6f137fbe2897334b7669f7fb4633c8 (diff) | |
| download | emacs-wttrin-33621b5a6e5407da190767b89756e287698ef234.tar.gz emacs-wttrin-33621b5a6e5407da190767b89756e287698ef234.zip | |
feat: add current-location detection to the weather picker
Geolocation used to live in a separate command, so getting weather for where you are meant knowing a second command existed. I added a "Current location (detect)" entry pinned to the top of the M-x wttrin picker. Selecting it detects your location and shows its weather. If the guess is wrong (VPN, hotspot) the detected city is visible in the header, so you reopen the picker and type one. Press d to keep the detected city as the default.
The entry is pinned first through a completion table whose display-sort-function holds against completion frameworks that re-sort, and it never enters location history or the cache as a place.
A new wttrin-geolocation-enabled (default t) turns every geolocation surface off for users who want that: the picker entry, the auto-detect favorite, and the command. It stays on by default.
The older wttrin-set-location-from-geolocation command is now obsolete in favor of the picker. Its customize-save-variable persistence advice is replaced by savehist, which already carries the favorite across sessions.
| -rw-r--r-- | README.org | 24 | ||||
| -rw-r--r-- | tests/test-wttrin-geolocation-sentinel.el | 212 | ||||
| -rw-r--r-- | tests/test-wttrin-location-history.el | 24 | ||||
| -rw-r--r-- | tests/test-wttrin-make-default.el | 5 | ||||
| -rw-r--r-- | tests/test-wttrin-requery.el | 9 | ||||
| -rw-r--r-- | tests/test-wttrin-set-location-from-geolocation.el | 29 | ||||
| -rw-r--r-- | wttrin.el | 150 |
7 files changed, 399 insertions, 54 deletions
@@ -266,18 +266,14 @@ If a refresh fails, the emoji dims to gray and the tooltip tells you what went w *Note:* If the weather emoji appears as a monochrome symbol instead of a color icon, try setting `wttrin-mode-line-emoji-font` to match a color emoji font installed on your system. Use `M-x fc-list` or check your system fonts to see what's available. -*** Setting the Favorite Location from IP Geolocation -If you don't want to type your city by hand, wttrin can detect it for you. Two ways: +*** Weather for Your Current Location +If you don't want to type your city by hand, wttrin can detect it for you. -**Manual detection with confirmation:** +**From the picker (weather here, right now):** run =M-x wttrin= and pick the first entry, "Current location (detect)". wttrin looks up your city via IP geolocation and shows its weather. If the guess is wrong (VPN, mobile hotspot), the detected city is right there in the buffer header, so just open the picker again and type the correct city. -#+begin_src emacs-lisp - M-x wttrin-set-location-from-geolocation -#+end_src +**Make the detected city your default:** in that weather buffer, press =d=. The detected city becomes =wttrin-favorite-location= (what the mode-line tracks). With =savehist-mode= on, the favorite persists across sessions automatically, since wttrin registers it with savehist. No =customize-save-variable= step is needed. -This looks up your city via IP geolocation, shows the detected location, and sets =wttrin-favorite-location= after you confirm. To make the setting persist across Emacs sessions, run =M-x customize-save-variable RET wttrin-favorite-location RET=, or add =(setq wttrin-favorite-location "Your City, State")= to your init file. - -**Automatic detection on first use:** +**Always use my current location:** #+begin_src emacs-lisp (setq wttrin-favorite-location t) @@ -293,7 +289,15 @@ The default lookup provider is =ipapi.co=. Two alternatives ship with the packag (setq wttrin-geolocation-provider 'ipwhois) ;; ipwho.is (10k/month) #+end_src -*Note:* IP-based geolocation can be wrong when you are behind a VPN or using a mobile hotspot. The confirmation prompt lets you reject an inaccurate result. If you prefer, set =wttrin-favorite-location= directly to any city string that wttr.in understands. +*Note:* IP-based geolocation can be wrong when you are behind a VPN or using a mobile hotspot. If you prefer, set =wttrin-favorite-location= directly to any city string that wttr.in understands. + +The older =M-x wttrin-set-location-from-geolocation= command still works but is deprecated in favor of the picker entry above. + +**Turning geolocation off:** geolocation is on by default. To opt out — no "Current location" entry in the picker, no detection requests — set: + +#+begin_src emacs-lisp + (setq wttrin-geolocation-enabled nil) +#+end_src *** Theming the Faces The text wttrin draws itself uses named faces, so themes and =M-x customize-face= can restyle it. (The weather art itself is colored by the ANSI codes wttr.in returns, not by these faces.) diff --git a/tests/test-wttrin-geolocation-sentinel.el b/tests/test-wttrin-geolocation-sentinel.el new file mode 100644 index 0000000..0536173 --- /dev/null +++ b/tests/test-wttrin-geolocation-sentinel.el @@ -0,0 +1,212 @@ +;;; test-wttrin-geolocation-sentinel.el --- Tests for the picker geolocation sentinel -*- lexical-binding: t; -*- + +;; Copyright (C) 2024-2026 Craig Jennings + +;;; Commentary: +;; Unit tests for the "Current location (detect)" picker sentinel: its presence +;; and position in `wttrin--completion-candidates', the selection routing in +;; `wttrin--query-selection' (literal vs detect-then-query), and the guard that +;; keeps the sentinel out of location history. + +;;; Code: + +(require 'ert) +(require 'cl-lib) +(require 'wttrin) +(require 'wttrin-geolocation) +(require 'testutil-wttrin) + +;;; wttrin--completion-candidates — sentinel presence and position + +(ert-deftest test-wttrin-geolocation-sentinel-normal-first-candidate () + "Normal: the sentinel is the first completion candidate." + (testutil-wttrin-setup) + (unwind-protect + (let ((wttrin-favorite-location nil) + (wttrin-default-locations '("Honolulu, HI")) + (wttrin--location-history '("Tokyo"))) + (should (equal wttrin--geolocation-sentinel + (car (wttrin--completion-candidates))))) + (testutil-wttrin-teardown))) + +(ert-deftest test-wttrin-geolocation-sentinel-normal-first-even-with-favorite () + "Normal: the sentinel precedes a string favorite in the candidate list." + (testutil-wttrin-setup) + (unwind-protect + (let ((wttrin-favorite-location "New Orleans, LA") + (wttrin-default-locations '("Honolulu, HI")) + (wttrin--location-history nil)) + (should (equal (list wttrin--geolocation-sentinel + "New Orleans, LA" "Honolulu, HI") + (wttrin--completion-candidates)))) + (testutil-wttrin-teardown))) + +;;; wttrin--sort-completions — pin the sentinel first + +(ert-deftest test-wttrin-geolocation-sentinel-normal-sort-pins-first () + "Normal: the sentinel is moved to the front, the rest keep their order. +Sorting completion UIs (vertico, icomplete) call the metadata +display-sort-function, so this is what keeps the sentinel pinned." + (should (equal (list wttrin--geolocation-sentinel "Honolulu, HI" "Tokyo") + (wttrin--sort-completions + (list "Honolulu, HI" wttrin--geolocation-sentinel "Tokyo"))))) + +(ert-deftest test-wttrin-geolocation-sentinel-boundary-sort-no-sentinel-unchanged () + "Boundary: a list without the sentinel is returned in its original order." + (should (equal '("Honolulu, HI" "Tokyo") + (wttrin--sort-completions '("Honolulu, HI" "Tokyo"))))) + +(ert-deftest test-wttrin-geolocation-sentinel-boundary-sort-empty () + "Boundary: an empty candidate list sorts to empty." + (should (null (wttrin--sort-completions nil)))) + +;;; wttrin--completion-table — metadata + completion + +(ert-deftest test-wttrin-geolocation-sentinel-normal-table-metadata-sort-fn () + "Normal: the table advertises the pin-first display-sort-function." + (let* ((table (wttrin--completion-table + (list wttrin--geolocation-sentinel "Tokyo"))) + (meta (funcall table "" nil 'metadata))) + (should (eq #'wttrin--sort-completions + (cdr (assq 'display-sort-function (cdr meta))))))) + +(ert-deftest test-wttrin-geolocation-sentinel-normal-table-completes-candidates () + "Normal: the table completes over the candidates it was given." + (let ((table (wttrin--completion-table + (list wttrin--geolocation-sentinel "Tokyo" "Paris")))) + (should (equal (sort (list wttrin--geolocation-sentinel "Tokyo" "Paris") + #'string-lessp) + (sort (all-completions "" table) #'string-lessp))))) + +;;; wttrin interactive entry — delegates to routing + +(ert-deftest test-wttrin-geolocation-sentinel-normal-entry-delegates-to-query-selection () + "Normal: the interactive `wttrin' command routes its picker selection +through `wttrin--query-selection' (smoke test of the entry wrapper)." + (testutil-wttrin-setup) + (unwind-protect + (let ((routed nil)) + (cl-letf (((symbol-function 'completing-read) + (lambda (&rest _) "London, GB")) + ((symbol-function 'wttrin--query-selection) + (lambda (loc) (setq routed loc)))) + (call-interactively 'wttrin)) + (should (equal "London, GB" routed))) + (testutil-wttrin-teardown))) + +;;; wttrin--query-selection — routing + +(ert-deftest test-wttrin-geolocation-sentinel-normal-typed-location-queries-literally () + "Normal: a typed location is passed straight to `wttrin-query'." + (testutil-wttrin-setup) + (unwind-protect + (let ((captured nil)) + (cl-letf (((symbol-function 'wttrin-query) + (lambda (loc) (setq captured loc)))) + (wttrin--query-selection "Paris")) + (should (equal "Paris" captured))) + (testutil-wttrin-teardown))) + +(ert-deftest test-wttrin-geolocation-sentinel-normal-routes-to-detect-then-query () + "Normal: selecting the sentinel detects, then queries the resolved city." + (testutil-wttrin-setup) + (unwind-protect + (let ((captured nil)) + (cl-letf (((symbol-function 'wttrin-geolocation-detect) + (lambda (callback) (funcall callback "Austin, TX"))) + ((symbol-function 'wttrin-query) + (lambda (loc) (setq captured loc))) + ((symbol-function 'message) (lambda (&rest _) nil))) + (wttrin--query-selection wttrin--geolocation-sentinel)) + (should (equal "Austin, TX" captured))) + (testutil-wttrin-teardown))) + +(ert-deftest test-wttrin-geolocation-sentinel-error-detect-failure-no-query () + "Error: a failed detection does not query and does not mutate the favorite." + (testutil-wttrin-setup) + (unwind-protect + (let ((queried nil) + (wttrin-favorite-location "New Orleans, LA")) + (cl-letf (((symbol-function 'wttrin-geolocation-detect) + (lambda (callback) (funcall callback nil))) + ((symbol-function 'wttrin-query) + (lambda (_loc) (setq queried t))) + ((symbol-function 'message) (lambda (&rest _) nil))) + (wttrin--query-selection wttrin--geolocation-sentinel)) + (should-not queried) + (should (equal "New Orleans, LA" wttrin-favorite-location))) + (testutil-wttrin-teardown))) + +;;; sentinel never enters history + +(ert-deftest test-wttrin-geolocation-sentinel-boundary-never-added-to-history () + "Boundary: the sentinel string is never recorded in location history." + (testutil-wttrin-setup) + (unwind-protect + (let ((wttrin-default-locations '()) + (wttrin--location-history nil)) + (wttrin--add-to-location-history wttrin--geolocation-sentinel) + (should (null wttrin--location-history))) + (testutil-wttrin-teardown))) + +;;; wttrin-geolocation-enabled — opt-out switch + +(ert-deftest test-wttrin-geolocation-sentinel-normal-disabled-hides-sentinel () + "Normal: with geolocation disabled, the sentinel is not offered." + (testutil-wttrin-setup) + (unwind-protect + (let ((wttrin-geolocation-enabled nil) + (wttrin-favorite-location nil) + (wttrin-default-locations '("Honolulu, HI")) + (wttrin--location-history '("Tokyo"))) + (should-not (member wttrin--geolocation-sentinel + (wttrin--completion-candidates))) + (should (equal '("Honolulu, HI" "Tokyo") + (wttrin--completion-candidates)))) + (testutil-wttrin-teardown))) + +(ert-deftest test-wttrin-geolocation-sentinel-boundary-enabled-shows-sentinel () + "Boundary: with geolocation enabled (default), the sentinel is offered first." + (testutil-wttrin-setup) + (unwind-protect + (let ((wttrin-geolocation-enabled t) + (wttrin-favorite-location nil) + (wttrin-default-locations '("Honolulu, HI")) + (wttrin--location-history nil)) + (should (equal wttrin--geolocation-sentinel + (car (wttrin--completion-candidates))))) + (testutil-wttrin-teardown))) + +(ert-deftest test-wttrin-geolocation-sentinel-error-disabled-detect-then-query-no-detect () + "Error: with geolocation disabled, detect-then-query does not detect or query." + (testutil-wttrin-setup) + (unwind-protect + (let ((detected nil) + (queried nil) + (wttrin-geolocation-enabled nil)) + (cl-letf (((symbol-function 'wttrin-geolocation-detect) + (lambda (_cb) (setq detected t))) + ((symbol-function 'wttrin-query) + (lambda (_loc) (setq queried t))) + ((symbol-function 'message) (lambda (&rest _) nil))) + (wttrin--detect-then-query)) + (should-not detected) + (should-not queried)) + (testutil-wttrin-teardown))) + +(ert-deftest test-wttrin-geolocation-sentinel-boundary-disabled-favorite-no-autodetect () + "Boundary: with geolocation disabled, the t-favorite auto-detect does not fire." + (testutil-wttrin-setup) + (unwind-protect + (let ((detected nil) + (wttrin-geolocation-enabled nil) + (wttrin--favorite-location-pending nil)) + (cl-letf (((symbol-function 'wttrin-geolocation-detect) + (lambda (_cb) (setq detected t)))) + (wttrin--start-favorite-location-detect)) + (should-not detected) + (should-not wttrin--favorite-location-pending)) + (testutil-wttrin-teardown))) + +(provide 'test-wttrin-geolocation-sentinel) +;;; test-wttrin-geolocation-sentinel.el ends here diff --git a/tests/test-wttrin-location-history.el b/tests/test-wttrin-location-history.el index 61b495c..4af8235 100644 --- a/tests/test-wttrin-location-history.el +++ b/tests/test-wttrin-location-history.el @@ -113,31 +113,37 @@ ;;; wttrin--completion-candidates (ert-deftest test-wttrin-location-history-normal-candidates-defaults-then-history () - "Candidates list defaults first, then history." + "Candidates list the sentinel, then defaults, then history." (test-wttrin-location-history-setup) (unwind-protect - (let ((wttrin-default-locations '("Honolulu, HI" "Berkeley, CA")) + (let ((wttrin-favorite-location nil) + (wttrin-default-locations '("Honolulu, HI" "Berkeley, CA")) (wttrin--location-history '("Tokyo" "Paris"))) - (should (equal '("Honolulu, HI" "Berkeley, CA" "Tokyo" "Paris") + (should (equal (list wttrin--geolocation-sentinel + "Honolulu, HI" "Berkeley, CA" "Tokyo" "Paris") (wttrin--completion-candidates)))) (test-wttrin-location-history-teardown))) (ert-deftest test-wttrin-location-history-normal-candidates-only-defaults () - "With empty history, candidates are just the defaults." + "With empty history, candidates are the sentinel then the defaults." (test-wttrin-location-history-setup) (unwind-protect - (let ((wttrin-default-locations '("Honolulu, HI")) + (let ((wttrin-favorite-location nil) + (wttrin-default-locations '("Honolulu, HI")) (wttrin--location-history nil)) - (should (equal '("Honolulu, HI") (wttrin--completion-candidates)))) + (should (equal (list wttrin--geolocation-sentinel "Honolulu, HI") + (wttrin--completion-candidates)))) (test-wttrin-location-history-teardown))) (ert-deftest test-wttrin-location-history-normal-candidates-only-history () - "With empty defaults, candidates are just the history." + "With empty defaults, candidates are the sentinel then the history." (test-wttrin-location-history-setup) (unwind-protect - (let ((wttrin-default-locations '()) + (let ((wttrin-favorite-location nil) + (wttrin-default-locations '()) (wttrin--location-history '("Tokyo"))) - (should (equal '("Tokyo") (wttrin--completion-candidates)))) + (should (equal (list wttrin--geolocation-sentinel "Tokyo") + (wttrin--completion-candidates)))) (test-wttrin-location-history-teardown))) ;;; wttrin-remove-location-history diff --git a/tests/test-wttrin-make-default.el b/tests/test-wttrin-make-default.el index 623200c..e715e12 100644 --- a/tests/test-wttrin-make-default.el +++ b/tests/test-wttrin-make-default.el @@ -143,7 +143,8 @@ stale cache and fetches fresh weather for the new location immediately." (wttrin--location-history nil) (wttrin-favorite-location "Reykjavik")) (should (equal (wttrin--completion-candidates) - '("Reykjavik" "Honolulu, HI" "Berkeley, CA"))))) + (list wttrin--geolocation-sentinel + "Reykjavik" "Honolulu, HI" "Berkeley, CA"))))) ;;; Boundary Cases @@ -161,7 +162,7 @@ stale cache and fetches fresh weather for the new location immediately." (wttrin--location-history '("Oslo, NO")) (wttrin-favorite-location nil)) (should (equal (wttrin--completion-candidates) - '("Honolulu, HI" "Oslo, NO"))))) + (list wttrin--geolocation-sentinel "Honolulu, HI" "Oslo, NO"))))) ;;; -------------------------------------------------------------------------- ;;; keymap binding diff --git a/tests/test-wttrin-requery.el b/tests/test-wttrin-requery.el index d6a8beb..5d52b36 100644 --- a/tests/test-wttrin-requery.el +++ b/tests/test-wttrin-requery.el @@ -105,6 +105,8 @@ to the core requery function." (test-wttrin-requery-setup) (unwind-protect (let ((offered-collection nil) + (wttrin-favorite-location nil) + (wttrin--location-history nil) (wttrin-default-locations '("Paris" "London" "Tokyo"))) (cl-letf (((symbol-function 'completing-read) (lambda (_prompt collection &rest _args) @@ -113,7 +115,12 @@ to the core requery function." ((symbol-function 'wttrin--requery-location) (lambda (_loc) nil))) (wttrin-requery) - (should (equal offered-collection '("Paris" "London" "Tokyo"))))) + ;; The collection is now a completion table (a function) that pins + ;; the sentinel first; check the candidates it completes over. + (should (equal (list wttrin--geolocation-sentinel + "Paris" "London" "Tokyo") + (wttrin--sort-completions + (all-completions "" offered-collection)))))) (test-wttrin-requery-teardown))) (ert-deftest test-wttrin-requery-boundary-single-default-prefills () diff --git a/tests/test-wttrin-set-location-from-geolocation.el b/tests/test-wttrin-set-location-from-geolocation.el index 170d0fb..e7c3a97 100644 --- a/tests/test-wttrin-set-location-from-geolocation.el +++ b/tests/test-wttrin-set-location-from-geolocation.el @@ -118,5 +118,34 @@ messages))) (test-wttrin-set-location-from-geolocation-teardown))) +;;; Opt-out + +(ert-deftest test-wttrin-set-location-from-geolocation-boundary-disabled-no-detect () + "Boundary: with geolocation disabled, the command neither detects nor sets." + (test-wttrin-set-location-from-geolocation-setup) + (setq wttrin-favorite-location "Pre-existing, Place") + (unwind-protect + (let ((detected nil) + (wttrin-geolocation-enabled nil)) + (cl-letf (((symbol-function 'wttrin-geolocation-detect) + (lambda (_cb) (setq detected t))) + ((symbol-function 'message) (lambda (&rest _) nil))) + (wttrin-set-location-from-geolocation)) + (should-not detected) + (should (string= "Pre-existing, Place" wttrin-favorite-location))) + (test-wttrin-set-location-from-geolocation-teardown))) + +;;; Deprecation + +(ert-deftest test-wttrin-set-location-from-geolocation-normal-marked-obsolete () + "Normal: the command is marked obsolete with a steering message. +The favorite-setting behavior is preserved (see the Normal cases above); +this only asserts the obsolescence marker so callers get a deprecation +notice steering them to the picker." + (let ((info (get 'wttrin-set-location-from-geolocation 'byte-obsolete-info))) + (should info) + ;; Option 1: a steering string, not an alias to another function. + (should (stringp (nth 0 info))))) + (provide 'test-wttrin-set-location-from-geolocation) ;;; test-wttrin-set-location-from-geolocation.el ends here @@ -168,6 +168,16 @@ When cache reaches `wttrin-cache-max-entries', remove the oldest 20% to avoid frequent cleanup cycles. This value (0.20) means remove 1/5 of entries, providing a reasonable buffer before the next cleanup.") +(defcustom wttrin-geolocation-enabled t + "Whether geolocation features are available. +When non-nil (the default), the \"Current location (detect)\" entry is +offered in the picker, the `wttrin-favorite-location' = t auto-detect +runs, and the geolocation command works. Set to nil to opt out: no +geolocation surface is offered and no detection request is made. +Geolocation is on by default; you opt out, you never have to opt in." + :group 'wttrin + :type 'boolean) + (defcustom wttrin-favorite-location nil "Favorite location to display weather for. @@ -221,7 +231,8 @@ On success the resolved string is stored in `wttrin--resolved-favorite-location'. Failures (network error, parse error) leave the cache empty and clear the pending flag, so the next call retries." - (unless wttrin--favorite-location-pending + (when (and wttrin-geolocation-enabled + (not wttrin--favorite-location-pending)) (setq wttrin--favorite-location-pending t) (require 'wttrin-geolocation) (wttrin-geolocation-detect @@ -549,13 +560,20 @@ would otherwise drop the entries before they could be saved." (wttrin--savehist-register) (add-hook 'savehist-save-hook #'wttrin--savehist-register)) +(defconst wttrin--geolocation-sentinel "Current location (detect)" + "Picker candidate that triggers geolocation detection. +Selecting it routes through `wttrin--query-selection' to a +detect-then-query flow instead of being treated as a literal place +name. It is never persisted to history or the cache as a location.") + (defun wttrin--add-to-location-history (location) "Record LOCATION as a recent successful search. -No-op when LOCATION is nil, empty, or already a default location. An existing -entry is promoted to most-recent, and the list is trimmed to -`wttrin-location-history-max'." +No-op when LOCATION is nil, empty, the geolocation sentinel, or already a +default location. An existing entry is promoted to most-recent, and the list +is trimmed to `wttrin-location-history-max'." (when (and location (not (string= location "")) + (not (string= location wttrin--geolocation-sentinel)) (not (member location wttrin-default-locations))) (setq wttrin--location-history (delete location wttrin--location-history)) (push location wttrin--location-history) @@ -571,11 +589,61 @@ History already excludes defaults (see `wttrin--add-to-location-history'), and `wttrin--set-favorite-location' drops the favorite from history. The favorite \(`wttrin-favorite-location', when a string) is prepended unless it is already a default, so it always appears exactly once." - (let ((candidates (append wttrin-default-locations wttrin--location-history))) - (if (and (stringp wttrin-favorite-location) - (not (member wttrin-favorite-location candidates))) - (cons wttrin-favorite-location candidates) - candidates))) + (let* ((candidates (append wttrin-default-locations wttrin--location-history)) + (with-favorite (if (and (stringp wttrin-favorite-location) + (not (member wttrin-favorite-location candidates))) + (cons wttrin-favorite-location candidates) + candidates))) + (if wttrin-geolocation-enabled + (cons wttrin--geolocation-sentinel with-favorite) + with-favorite))) + +(defun wttrin--sort-completions (candidates) + "Return CANDIDATES with the geolocation sentinel pinned first. +The remaining candidates keep the order `wttrin--completion-candidates' +produced (favorite, defaults, then history). Used as the completion +metadata `display-sort-function' so sorting UIs (vertico, icomplete, the +default *Completions* buffer) keep the sentinel at the top instead of +re-sorting it into alphabetical position." + (if (member wttrin--geolocation-sentinel candidates) + (cons wttrin--geolocation-sentinel + (remove wttrin--geolocation-sentinel candidates)) + candidates)) + +(defun wttrin--completion-table (candidates) + "Return a completion table over CANDIDATES that pins the sentinel first. +The table answers the `metadata' action with a `display-sort-function' +of `wttrin--sort-completions', and otherwise completes over CANDIDATES. +Wrapping the list this way is what keeps the sentinel first across +completion frameworks that impose their own sort order." + (lambda (string predicate action) + (if (eq action 'metadata) + `(metadata (display-sort-function . ,#'wttrin--sort-completions)) + (complete-with-action action candidates string predicate)))) + +(defun wttrin--detect-then-query () + "Detect the current location asynchronously, then query weather for it. +No-op with a message when `wttrin-geolocation-enabled' is nil. On detection +failure, show an actionable message and leave the favorite untouched; the user +can fall back to typing a city in the picker." + (if (not wttrin-geolocation-enabled) + (message "Geolocation is disabled (set wttrin-geolocation-enabled to enable it)") + (require 'wttrin-geolocation) + (message "Detecting location...") + (wttrin-geolocation-detect + (lambda (location) + (if location + (wttrin-query location) + (message "Could not detect location (network or provider error)")))))) + +(defun wttrin--query-selection (selection) + "Route a picker SELECTION to the right query path. +The geolocation sentinel routes to `wttrin--detect-then-query'; any other +SELECTION is queried literally via `wttrin-query'. This is the single guard +that keeps the sentinel from reaching `wttrin-query' as a place name." + (if (string= selection wttrin--geolocation-sentinel) + (wttrin--detect-then-query) + (wttrin-query selection))) (defun wttrin-remove-location-history (location) "Remove LOCATION from the search history. @@ -597,13 +665,15 @@ Prompts with completion over the current history entries." "Kill current weather buffer and query NEW-LOCATION." (when (get-buffer "*wttr.in*") (kill-buffer "*wttr.in*")) - (wttrin-query new-location)) + (wttrin--query-selection new-location)) (defun wttrin-requery () "Kill buffer and requery wttrin." (interactive) (let ((new-location (completing-read - "Location Name: " (wttrin--completion-candidates) nil nil + "Location Name: " + (wttrin--completion-table (wttrin--completion-candidates)) + nil nil (when (= (length wttrin-default-locations) 1) (car wttrin-default-locations))))) (wttrin--requery-location new-location))) @@ -812,30 +882,44 @@ This creates headroom to avoid frequent cleanups." "Detect your location via IP geolocation and set it as the favorite. Uses the provider named by `wttrin-geolocation-provider' to fetch \"City, Region\", asks for confirmation, and on yes assigns the -result to `wttrin-favorite-location' for this session. +result to `wttrin-favorite-location'. -To persist the setting across Emacs sessions, either run -\\[customize-save-variable] on `wttrin-favorite-location', or add -\(setq wttrin-favorite-location ...\) to your init file. +With `savehist-mode' on, the favorite persists across sessions +automatically (wttrin registers it with savehist); no +`customize-save-variable' step is needed. IP-based geolocation can be wrong behind a VPN or a mobile hotspot. The confirmation prompt shows the detected location so you can -reject inaccurate results." +reject inaccurate results. + +This command is obsolete. Prefer the \"Current location (detect)\" +entry in \\[wttrin], then press `d' in the weather buffer to keep +the detected city as your default." (interactive) - (require 'wttrin-geolocation) - (message "Detecting location...") - (wttrin-geolocation-detect - (lambda (location) - (cond - ((null location) - (message "Could not detect location (network or provider error)")) - ((yes-or-no-p (format "Detected location: %s. Set as favorite? " - location)) - (setq wttrin-favorite-location location) - (message "Set wttrin-favorite-location to: %s. Run M-x customize-save-variable to persist." - location)) - (t - (message "Location detection cancelled")))))) + (if (not wttrin-geolocation-enabled) + (message "Geolocation is disabled (set wttrin-geolocation-enabled to enable it)") + (require 'wttrin-geolocation) + (message "Detecting location...") + (wttrin-geolocation-detect + (lambda (location) + (cond + ((null location) + (message "Could not detect location (network or provider error)")) + ((yes-or-no-p (format "Detected location: %s. Set as favorite? " + location)) + (setq wttrin-favorite-location location) + (message "Set wttrin-favorite-location to: %s%s" + location + (if (bound-and-true-p savehist-mode) + " (persisted via savehist)." + ". Enable savehist-mode to persist it across sessions."))) + (t + (message "Location detection cancelled"))))))) + +(make-obsolete + 'wttrin-set-location-from-geolocation + "use the \"Current location (detect)\" entry in `wttrin', then press `d' to keep it as the default." + "0.4.0") (defvar-local wttrin--current-location nil "Current location displayed in this weather buffer.") @@ -1158,10 +1242,12 @@ When enabled, shows weather for `wttrin-favorite-location'." Weather data is fetched asynchronously to avoid blocking Emacs." (interactive (list - (completing-read "Location Name: " (wttrin--completion-candidates) nil nil + (completing-read "Location Name: " + (wttrin--completion-table (wttrin--completion-candidates)) + nil nil (when (= (length wttrin-default-locations) 1) (car wttrin-default-locations))))) - (wttrin-query location)) + (wttrin--query-selection location)) (when wttrin-mode-line-auto-enable (wttrin-mode-line-mode 1)) |
