Hi Rob,

Rob Cornish wrote:
> Currently when searching for text the '?' key searches forward in
> the document in exactly the same way as '/' does. Not sure if this
> is intentional or not, but I propose the following patch in order to
> make the functionality more like less and vi.
> 
> That is: '/' works as normal, but when a search is started via '?',
> the lowest match in the current view (in our case, on the current
> page) is matched first, and then subsequent 'n'-presses move the
> current matched result up the page rather than down.

That's a great idea! I just have a few comments about the patch below.

> ---
>  commands.c  |  7 ++++++-
>  document.c  | 23 +++++++++++++++++++++++
>  document.h  | 16 ++++++++++++++++
>  shortcuts.c | 10 +++++++++-
>  4 files changed, 54 insertions(+), 2 deletions(-)
> 
> diff --git a/commands.c b/commands.c
> index 5d568f7..9cbcc7e 100644
> --- a/commands.c
> +++ b/commands.c
> @@ -340,6 +340,7 @@ cmd_search(girara_session_t* session, const
> char* input, girara_argument_t* argu
>    bool firsthit = true;
>    zathura_error_t error = ZATHURA_ERROR_OK;
> 
> + zathura_document_set_search_direction(zathura->document, argument->n);
> 
>    unsigned int number_of_pages     =
> zathura_document_get_number_of_pages(zathura->document);

Looks like your mailer has wrapped some lines in this patch; you should
fix it, or use git send-email.

>    unsigned int current_page_number =
> zathura_document_get_current_page_number(zathura->document);
> @@ -380,7 +381,11 @@ cmd_search(girara_session_t* session, const
> char* input, girara_argument_t* argu
>        if (page_id != 0) {
>          page_set_delayed(zathura, zathura_page_get_index(page));
>        }
> -      g_object_set(page_widget, "search-current", 0, NULL);
> +      if (argument->n == BACKWARD) {
> +          g_object_set(page_widget, "search-current",
> girara_list_size(result) - 1, NULL);
> +      } else {
> +          g_object_set(page_widget, "search-current", 0, NULL);
> +      }
>        firsthit = false;
>      }
>    }
> diff --git a/document.c b/document.c
> index 1dc6637..ad9c39b 100644
> --- a/document.c
> +++ b/document.c
> @@ -47,6 +47,7 @@ struct zathura_document_s
>    void* data; /**< Custom data */
>    zathura_adjust_mode_t adjust_mode; /**< Adjust mode (best-fit, width) */
>    unsigned int page_offset; /**< Page offset */
> +  int search_direction; /**< Current search direction (whether
> using '/' or '?') */

I don't think it should be a document property; in vim, the search
direction is the same for all buffers, so it isn't a per-document
property. Maybe zathura->global would be a better place to put it?

>    /**
>     * Document pages
> @@ -119,6 +120,7 @@ zathura_document_open(zathura_plugin_manager_t*
> plugin_manager, const char*
>    document->scale       = 1.0;
>    document->plugin      = plugin;
>    document->adjust_mode = ZATHURA_ADJUST_NONE;
> +  document->search_direction = FORWARD;
> 
>    /* open document */
>    zathura_plugin_functions_t* functions =
> zathura_plugin_get_functions(plugin);
> @@ -390,6 +392,27 @@
> zathura_document_set_page_offset(zathura_document_t* document,
> unsigned int page
>    }
>  }
> 
> +int
> +zathura_document_get_search_direction(zathura_document_t* document)
> +{
> +  if (document == NULL) {
> +    return;
> +  }
> +
> +  return document->search_direction;
> +}
> +
> +void
> +zathura_document_set_search_direction(zathura_document_t* document,
> int search_direction)
> +{
> +  if (document == NULL) {
> +    return;
> +  }
> +
> +  document->search_direction = search_direction;
> +}
> +
> +
>  zathura_error_t
>  zathura_document_save_as(zathura_document_t* document, const char* path)
>  {
> diff --git a/document.h b/document.h
> index b72e4ac..4a0562f 100644
> --- a/document.h
> +++ b/document.h
> @@ -154,6 +154,22 @@ unsigned int
> zathura_document_get_page_offset(zathura_document_t* document);
>  void zathura_document_set_page_offset(zathura_document_t* document,
> unsigned int page_offset);
> 
>  /**
> + * Returns the current search direction of the document
> + *
> + * @param document The document
> + */
> +int zathura_document_get_search_direction(zathura_document_t* document);
> +
> +/**
> + * Sets the new search direction of the document
> + *
> + * @param document The document
> + * @param page_offset The new search direction
> + *    (FORWARD or BACKWARD)
> + */
> +void zathura_document_set_search_direction(zathura_document_t*
> document, int search_direction);
> +
> +/**
>   * Returns the private data of the document
>   *
>   * @param document The document
> diff --git a/shortcuts.c b/shortcuts.c
> index ecd72c5..447487a 100644
> --- a/shortcuts.c
> +++ b/shortcuts.c
> @@ -621,7 +621,15 @@ sc_search(girara_session_t* session,
> girara_argument_t* argument,
> 
>    const int num_pages =
> zathura_document_get_number_of_pages(zathura->document);
>    const int cur_page  =
> zathura_document_get_current_page_number(zathura->document);
> -  int diff = argument->n == FORWARD ? 1 : -1;
> +  int diff;
> +  switch (zathura_document_get_search_direction(zathura->document)) {
> +      case FORWARD:
> +          diff = argument->n == FORWARD ? 1 : -1;
> +          break;
> +      case BACKWARD:
> +          diff = argument->n == FORWARD ? -1 : 1;
> +          break;
> +  }
> 
>    zathura_page_t* target_page = NULL;
>    int target_idx = 0;

Cheers,

-- 
Benoît Knecht
_______________________________________________
zathura mailing list
zathura@lists.pwmt.org
http://lists.pwmt.org/mailman/listinfo/zathura

Reply via email to